@@ -1,85 +0,0 @@ | |||||
//package com.jd.blockchain.ledger.core.impl; | |||||
// | |||||
//import com.jd.blockchain.crypto.hash.HashDigest; | |||||
//import com.jd.blockchain.ledger.TransactionState; | |||||
//import com.jd.blockchain.ledger.LedgerBlock; | |||||
//import com.jd.blockchain.ledger.LedgerTransaction; | |||||
//import com.jd.blockchain.ledger.TransactionRequest; | |||||
//import com.jd.blockchain.ledger.core.LedgerDataSet; | |||||
//import com.jd.blockchain.ledger.core.LedgerEditor; | |||||
//import com.jd.blockchain.ledger.core.LedgerManage; | |||||
//import com.jd.blockchain.ledger.core.LedgerTransactionContext; | |||||
//import com.jd.blockchain.ledger.core.PrefixAppender; | |||||
//import com.jd.blockchain.storage.service.ExPolicyKVStorage; | |||||
//import com.jd.blockchain.storage.service.KVStorageService; | |||||
//import com.jd.blockchain.storage.service.VersioningKVStorage; | |||||
//import com.jd.blockchain.storage.service.utils.BufferedKVStorage; | |||||
// | |||||
///** | |||||
// * 账本初始化;<br> | |||||
// * | |||||
// * 初始生成账本时,所有的KV数据先缓冲写入到内存中,待计算得到账本 hash 之后,再重写入到与账本hash相关的持久化存储; | |||||
// * | |||||
// * @author huanghaiquan | |||||
// * | |||||
// */ | |||||
//class LedgerInitializer implements LedgerEditor { | |||||
// | |||||
// private KVStorageService baseStorage; | |||||
// | |||||
// private GenesisLedgerStorageProxy ledgerStorageProxy; | |||||
// | |||||
// private BufferedKVStorage genesisBufferedStorage; | |||||
// | |||||
// private LedgerEditor genesisBlockEditor; | |||||
// | |||||
// private LedgerBlock genesisBlock; | |||||
// | |||||
// private LedgerManage ledgerManager; | |||||
// | |||||
// LedgerInitializer(LedgerEditor genesisBlockEditor, BufferedKVStorage bufferedStorage, | |||||
// GenesisLedgerStorageProxy ledgerStorageProxy, KVStorageService kvStorage, LedgerManage ledgerManager) { | |||||
// this.genesisBlockEditor = genesisBlockEditor; | |||||
// this.genesisBufferedStorage = bufferedStorage; | |||||
// this.ledgerStorageProxy = ledgerStorageProxy; | |||||
// this.baseStorage = kvStorage; | |||||
// | |||||
// this.ledgerManager = ledgerManager; | |||||
// } | |||||
// | |||||
// @Override | |||||
// public LedgerTransactionContext newTransaction(TransactionRequest txRequest) { | |||||
// return genesisBlockEditor.newTransaction(txRequest); | |||||
// } | |||||
// | |||||
// @Override | |||||
// public LedgerBlock prepare() { | |||||
// // create genesis block; | |||||
// genesisBlock = genesisBlockEditor.prepare(); | |||||
// | |||||
// return genesisBlock; | |||||
// } | |||||
// | |||||
// @Override | |||||
// public void commit() { | |||||
// // commit data of editor; it will flush data to genesisBufferedStorage; | |||||
// genesisBlockEditor.commit(); | |||||
// | |||||
// // redirect persistence to storage which created for this new ledger with ledger hash; | |||||
// HashDigest ledgerHash = genesisBlock.getHash(); | |||||
// String ledgerPrefix =LedgerManager.getLedgerStoragePrefix(ledgerHash); | |||||
// ExPolicyKVStorage ledgerExStorage = PrefixAppender.prefix(ledgerPrefix, baseStorage.getExPolicyKVStorage()); | |||||
// VersioningKVStorage ledgerVerStorage = PrefixAppender.prefix(ledgerPrefix, baseStorage.getVersioningKVStorage()); | |||||
// | |||||
// // ready to persistent; | |||||
// ledgerStorageProxy.setPersistentStorage(ledgerExStorage, ledgerVerStorage); | |||||
// | |||||
// // flush output; | |||||
// genesisBufferedStorage.flush(); | |||||
// } | |||||
// | |||||
// @Override | |||||
// public void cancel() { | |||||
// genesisBlockEditor.cancel(); | |||||
// } | |||||
//} |
@@ -68,17 +68,17 @@ public class LedgerEditerTest { | |||||
MemoryKVStorage storage = new MemoryKVStorage(); | MemoryKVStorage storage = new MemoryKVStorage(); | ||||
// 创建初始化配置; | // 创建初始化配置; | ||||
LedgerInitSetting initSetting = createLedgerInitSetting(); | |||||
LedgerInitSetting initSetting = LedgerTestUtils.createLedgerInitSetting(); | |||||
// 创建账本; | // 创建账本; | ||||
return LedgerTransactionalEditor.createEditor(initSetting, LEDGER_KEY_PREFIX, storage, storage); | return LedgerTransactionalEditor.createEditor(initSetting, LEDGER_KEY_PREFIX, storage, storage); | ||||
} | } | ||||
private LedgerTransactionContext createGenisisTx(LedgerEditor ldgEdt) { | private LedgerTransactionContext createGenisisTx(LedgerEditor ldgEdt) { | ||||
TransactionRequest genesisTxReq = LedgerTestUtils.createTxRequest(null, signatureFunction); | |||||
TransactionRequest genesisTxReq = LedgerTestUtils.createTxRequest_UserReg(null); | |||||
LedgerTransactionContext txCtx = ldgEdt.newTransaction(genesisTxReq); | LedgerTransactionContext txCtx = ldgEdt.newTransaction(genesisTxReq); | ||||
return txCtx; | return txCtx; | ||||
} | } | ||||
@@ -88,8 +88,7 @@ public class LedgerEditerTest { | |||||
LedgerEditor ldgEdt = createLedgerInitEditor(); | LedgerEditor ldgEdt = createLedgerInitEditor(); | ||||
LedgerTransactionContext genisisTxCtx = createGenisisTx(ldgEdt); | LedgerTransactionContext genisisTxCtx = createGenisisTx(ldgEdt); | ||||
LedgerDataSet ldgDS = genisisTxCtx.getDataSet(); | LedgerDataSet ldgDS = genisisTxCtx.getDataSet(); | ||||
AsymmetricKeypair cryptoKeyPair = signatureFunction.generateKeypair(); | AsymmetricKeypair cryptoKeyPair = signatureFunction.generateKeypair(); | ||||
BlockchainKeypair dataKP = new BlockchainKeypair(cryptoKeyPair.getPubKey(), cryptoKeyPair.getPrivKey()); | BlockchainKeypair dataKP = new BlockchainKeypair(cryptoKeyPair.getPubKey(), cryptoKeyPair.getPrivKey()); | ||||
@@ -147,42 +146,5 @@ public class LedgerEditerTest { | |||||
ldgEdt.commit(); | ldgEdt.commit(); | ||||
} | } | ||||
private LedgerInitSetting createLedgerInitSetting() { | |||||
SignatureFunction signFunc = Crypto.getSignatureFunction("ED25519"); | |||||
CryptoProvider[] supportedProviders = new CryptoProvider[SUPPORTED_PROVIDERS.length]; | |||||
for (int i = 0; i < SUPPORTED_PROVIDERS.length; i++) { | |||||
supportedProviders[i] = Crypto.getProvider(SUPPORTED_PROVIDERS[i]); | |||||
} | |||||
CryptoConfig defCryptoSetting = new CryptoConfig(); | |||||
defCryptoSetting.setSupportedProviders(supportedProviders); | |||||
defCryptoSetting.setAutoVerifyHash(true); | |||||
defCryptoSetting.setHashAlgorithm(ClassicAlgorithm.SHA256); | |||||
LedgerInitSettingData initSetting = new LedgerInitSettingData(); | |||||
initSetting.setLedgerSeed(BytesUtils.toBytes("A Test Ledger seed!", "UTF-8")); | |||||
initSetting.setCryptoSetting(defCryptoSetting); | |||||
ConsensusParticipantData[] parties = new ConsensusParticipantData[2]; | |||||
parties[0] = new ConsensusParticipantData(); | |||||
parties[0].setId(0); | |||||
parties[0].setName("John"); | |||||
AsymmetricKeypair kp0 = signFunc.generateKeypair(); | |||||
parties[0].setPubKey(kp0.getPubKey()); | |||||
parties[0].setAddress(AddressEncoding.generateAddress(kp0.getPubKey()).toBase58()); | |||||
parties[0].setHostAddress(new NetworkAddress("192.168.1.6", 9000)); | |||||
parties[1] = new ConsensusParticipantData(); | |||||
parties[1].setId(1); | |||||
parties[1].setName("John"); | |||||
AsymmetricKeypair kp1 = signFunc.generateKeypair(); | |||||
parties[1].setPubKey(kp1.getPubKey()); | |||||
parties[1].setAddress(AddressEncoding.generateAddress(kp1.getPubKey()).toBase58()); | |||||
parties[1].setHostAddress(new NetworkAddress("192.168.1.7", 9000)); | |||||
initSetting.setConsensusParticipants(parties); | |||||
return initSetting; | |||||
} | |||||
} | } |
@@ -83,13 +83,13 @@ public class LedgerManagerTest { | |||||
LedgerEditor ldgEdt = ledgerManager.newLedger(initSetting, storage); | LedgerEditor ldgEdt = ledgerManager.newLedger(initSetting, storage); | ||||
// 创建一个模拟的创世交易; | // 创建一个模拟的创世交易; | ||||
TransactionRequest genesisTxReq = LedgerTestUtils.createTxRequest(null, signatureFunction); | |||||
TransactionRequest genesisTxReq = LedgerTestUtils.createTxRequest_UserReg(null); | |||||
// 记录交易,注册用户; | // 记录交易,注册用户; | ||||
LedgerTransactionContext txCtx = ldgEdt.newTransaction(genesisTxReq); | LedgerTransactionContext txCtx = ldgEdt.newTransaction(genesisTxReq); | ||||
LedgerDataSet ldgDS = txCtx.getDataSet(); | LedgerDataSet ldgDS = txCtx.getDataSet(); | ||||
BlockchainKeypair userKP = BlockchainKeyGenerator.getInstance().generate(); | BlockchainKeypair userKP = BlockchainKeyGenerator.getInstance().generate(); | ||||
; | |||||
UserAccount userAccount = ldgDS.getUserAccountSet().register(userKP.getAddress(), userKP.getPubKey()); | UserAccount userAccount = ldgDS.getUserAccountSet().register(userKP.getAddress(), userKP.getPubKey()); | ||||
userAccount.setProperty("Name", "孙悟空", -1); | userAccount.setProperty("Name", "孙悟空", -1); | ||||
userAccount.setProperty("Age", "10000", -1); | userAccount.setProperty("Age", "10000", -1); | ||||
@@ -97,7 +97,7 @@ public class LedgerManagerTest { | |||||
System.out.println("UserAddress=" + userAccount.getAddress()); | System.out.println("UserAddress=" + userAccount.getAddress()); | ||||
// 提交交易结果; | // 提交交易结果; | ||||
LedgerTransaction tx = txCtx.commit(TransactionState.SUCCESS, null); | |||||
LedgerTransaction tx = txCtx.commit(TransactionState.SUCCESS); | |||||
assertEquals(genesisTxReq.getTransactionContent().getHash(), tx.getTransactionContent().getHash()); | assertEquals(genesisTxReq.getTransactionContent().getHash(), tx.getTransactionContent().getHash()); | ||||
assertEquals(0, tx.getBlockHeight()); | assertEquals(0, tx.getBlockHeight()); | ||||
@@ -137,7 +137,7 @@ public class LedgerManagerTest { | |||||
LedgerTransactionContext txCtx1 = editor1.newTransaction(txRequest); | LedgerTransactionContext txCtx1 = editor1.newTransaction(txRequest); | ||||
txCtx1.getDataSet().getDataAccountSet().register(dataKey.getAddress(), dataKey.getPubKey(), null); | txCtx1.getDataSet().getDataAccountSet().register(dataKey.getAddress(), dataKey.getPubKey(), null); | ||||
txCtx1.commit(TransactionState.SUCCESS, null); | |||||
txCtx1.commit(TransactionState.SUCCESS); | |||||
LedgerBlock block1 = editor1.prepare(); | LedgerBlock block1 = editor1.prepare(); | ||||
editor1.commit(); | editor1.commit(); | ||||
@@ -2,69 +2,102 @@ package test.com.jd.blockchain.ledger; | |||||
import java.util.Random; | import java.util.Random; | ||||
import com.jd.blockchain.crypto.AsymmetricKeypair; | |||||
import com.jd.blockchain.crypto.AddressEncoding; | |||||
import com.jd.blockchain.crypto.Crypto; | import com.jd.blockchain.crypto.Crypto; | ||||
import com.jd.blockchain.crypto.CryptoAlgorithm; | |||||
import com.jd.blockchain.crypto.CryptoProvider; | import com.jd.blockchain.crypto.CryptoProvider; | ||||
import com.jd.blockchain.crypto.HashDigest; | import com.jd.blockchain.crypto.HashDigest; | ||||
import com.jd.blockchain.crypto.PubKey; | |||||
import com.jd.blockchain.crypto.SignatureFunction; | import com.jd.blockchain.crypto.SignatureFunction; | ||||
import com.jd.blockchain.crypto.service.classic.ClassicAlgorithm; | import com.jd.blockchain.crypto.service.classic.ClassicAlgorithm; | ||||
import com.jd.blockchain.crypto.service.classic.ClassicCryptoService; | import com.jd.blockchain.crypto.service.classic.ClassicCryptoService; | ||||
import com.jd.blockchain.crypto.service.sm.SMCryptoService; | import com.jd.blockchain.crypto.service.sm.SMCryptoService; | ||||
import com.jd.blockchain.ledger.BlockchainIdentityData; | |||||
import com.jd.blockchain.ledger.BlockchainKeyGenerator; | |||||
import com.jd.blockchain.ledger.BlockchainKeypair; | |||||
import com.jd.blockchain.ledger.CryptoSetting; | import com.jd.blockchain.ledger.CryptoSetting; | ||||
import com.jd.blockchain.ledger.PreparedTransaction; | |||||
import com.jd.blockchain.ledger.LedgerInitSetting; | |||||
import com.jd.blockchain.ledger.TransactionRequest; | import com.jd.blockchain.ledger.TransactionRequest; | ||||
import com.jd.blockchain.ledger.TransactionRequestBuilder; | |||||
import com.jd.blockchain.ledger.TransactionResponse; | import com.jd.blockchain.ledger.TransactionResponse; | ||||
import com.jd.blockchain.ledger.core.CryptoConfig; | import com.jd.blockchain.ledger.core.CryptoConfig; | ||||
import com.jd.blockchain.ledger.core.impl.TransactionStagedSnapshot; | import com.jd.blockchain.ledger.core.impl.TransactionStagedSnapshot; | ||||
import com.jd.blockchain.transaction.ConsensusParticipantData; | |||||
import com.jd.blockchain.transaction.LedgerInitSettingData; | |||||
import com.jd.blockchain.transaction.TransactionService; | import com.jd.blockchain.transaction.TransactionService; | ||||
import com.jd.blockchain.transaction.TxTemplate; | |||||
import com.jd.blockchain.transaction.TxBuilder; | |||||
import com.jd.blockchain.utils.io.BytesUtils; | |||||
import com.jd.blockchain.utils.net.NetworkAddress; | |||||
public class LedgerTestUtils { | public class LedgerTestUtils { | ||||
// private static ThreadLocalRandom rand = ThreadLocalRandom.current(); | |||||
public static final SignatureFunction ED25519_SIGN_FUNC = Crypto.getSignatureFunction("ED25519"); | |||||
public static final CryptoAlgorithm ED25519 = ED25519_SIGN_FUNC.getAlgorithm(); | |||||
private static final String[] SUPPORTED_PROVIDERS = { ClassicCryptoService.class.getName(), | private static final String[] SUPPORTED_PROVIDERS = { ClassicCryptoService.class.getName(), | ||||
SMCryptoService.class.getName() }; | SMCryptoService.class.getName() }; | ||||
private static Random rand = new Random(); | private static Random rand = new Random(); | ||||
public static TransactionRequest createTxRequest(HashDigest ledgerHash) { | |||||
SignatureFunction signFunc = Crypto.getSignatureFunction("ED25519"); | |||||
return createTxRequest(ledgerHash, signFunc); | |||||
public static TransactionRequest createTxRequest_UserReg(HashDigest ledgerHash) { | |||||
BlockchainKeypair key = BlockchainKeyGenerator.getInstance().generate(ED25519); | |||||
return createTxRequest_UserReg(ledgerHash, key); | |||||
} | |||||
public static LedgerInitSetting createLedgerInitSetting() { | |||||
BlockchainKeypair[] partiKeys = new BlockchainKeypair[2]; | |||||
partiKeys[0] = BlockchainKeyGenerator.getInstance().generate(); | |||||
partiKeys[1] = BlockchainKeyGenerator.getInstance().generate(); | |||||
return createLedgerInitSetting(partiKeys); | |||||
} | } | ||||
public static TransactionRequest createTxRequest(HashDigest ledgerHash, SignatureFunction signatureFunction) { | |||||
TxHandle txHandle = new TxHandle(); | |||||
public static LedgerInitSetting createLedgerInitSetting(BlockchainKeypair[] partiKeys) { | |||||
CryptoProvider[] supportedProviders = new CryptoProvider[SUPPORTED_PROVIDERS.length]; | |||||
for (int i = 0; i < SUPPORTED_PROVIDERS.length; i++) { | |||||
supportedProviders[i] = Crypto.getProvider(SUPPORTED_PROVIDERS[i]); | |||||
} | |||||
CryptoConfig defCryptoSetting = new CryptoConfig(); | |||||
defCryptoSetting.setSupportedProviders(supportedProviders); | |||||
defCryptoSetting.setAutoVerifyHash(true); | |||||
defCryptoSetting.setHashAlgorithm(ClassicAlgorithm.SHA256); | |||||
LedgerInitSettingData initSetting = new LedgerInitSettingData(); | |||||
TxTemplate txTemp = new TxTemplate(ledgerHash, txHandle); | |||||
initSetting.setLedgerSeed(BytesUtils.toBytes("A Test Ledger seed!", "UTF-8")); | |||||
initSetting.setCryptoSetting(defCryptoSetting); | |||||
ConsensusParticipantData[] parties = new ConsensusParticipantData[partiKeys.length]; | |||||
for (int i = 0; i < parties.length; i++) { | |||||
parties[i] = new ConsensusParticipantData(); | |||||
parties[i].setId(0); | |||||
parties[i].setName("Parti-" + i); | |||||
parties[i].setPubKey(partiKeys[i].getPubKey()); | |||||
parties[i].setAddress(AddressEncoding.generateAddress(partiKeys[i].getPubKey()).toBase58()); | |||||
parties[i].setHostAddress(new NetworkAddress("192.168.1." + (10 + i), 9000)); | |||||
AsymmetricKeypair cryptoKeyPair = signatureFunction.generateKeypair(); | |||||
PubKey pubKey = cryptoKeyPair.getPubKey(); | |||||
txTemp.users().register(new BlockchainIdentityData(pubKey)); | |||||
PreparedTransaction ptx = txTemp.prepare(); | |||||
ptx.sign(cryptoKeyPair); | |||||
ptx.commit(); | |||||
return txHandle.txRequest; | |||||
} | |||||
initSetting.setConsensusParticipants(parties); | |||||
return initSetting; | |||||
} | |||||
public static TransactionRequest createTxRequest_UserReg(HashDigest ledgerHash, BlockchainKeypair userKeypair) { | |||||
return createTxRequest_UserReg(ledgerHash, userKeypair, null); | |||||
} | } | ||||
// public static TransactionRequest createContractEventTxRequest(HashDigest ledgerHash, | |||||
// SignatureFunction signatureFunction, String contractAddress, String event, byte[] args) { | |||||
// TxHandle txHandle = new TxHandle(); | |||||
// | |||||
// TxTemplate txTemp = new TxTemplate(ledgerHash, txHandle); | |||||
// | |||||
// txTemp.contractEvents().send(contractAddress, event, args); | |||||
// | |||||
// AsymmetricKeypair cryptoKeyPair = signatureFunction.generateKeypair(); | |||||
// PubKey pubKey = cryptoKeyPair.getPubKey(); | |||||
// txTemp.users().register(new BlockchainIdentityData(pubKey)); | |||||
// PreparedTransaction ptx = txTemp.prepare(); | |||||
// ptx.sign(cryptoKeyPair); | |||||
// ptx.commit(); | |||||
// return txHandle.txRequest; | |||||
// } | |||||
public static TransactionRequest createTxRequest_UserReg(HashDigest ledgerHash, BlockchainKeypair userKeypair, | |||||
BlockchainKeypair gatewayKeypair) { | |||||
TxBuilder txBuilder = new TxBuilder(ledgerHash); | |||||
txBuilder.users().register(userKeypair.getIdentity()); | |||||
TransactionRequestBuilder txReqBuilder = txBuilder.prepareRequest(); | |||||
txReqBuilder.signAsEndpoint(userKeypair); | |||||
if (gatewayKeypair != null) { | |||||
txReqBuilder.signAsNode(gatewayKeypair); | |||||
} | |||||
return txReqBuilder.buildRequest(); | |||||
} | |||||
public static TransactionStagedSnapshot generateRandomSnapshot() { | public static TransactionStagedSnapshot generateRandomSnapshot() { | ||||
TransactionStagedSnapshot txDataSnapshot = new TransactionStagedSnapshot(); | TransactionStagedSnapshot txDataSnapshot = new TransactionStagedSnapshot(); | ||||
@@ -1,392 +1,140 @@ | |||||
//package test.com.jd.blockchain.ledger; | |||||
// | |||||
//import static org.junit.Assert.assertArrayEquals; | |||||
//import static org.junit.Assert.assertEquals; | |||||
//import static org.junit.Assert.assertNotNull; | |||||
//import static org.junit.Assert.assertTrue; | |||||
// | |||||
//import java.io.File; | |||||
//import java.io.FileInputStream; | |||||
//import java.io.IOException; | |||||
//import java.io.InputStream; | |||||
//import java.net.URISyntaxException; | |||||
//import java.net.URL; | |||||
//import java.nio.file.Paths; | |||||
// | |||||
//import org.junit.Before; | |||||
//import org.junit.Test; | |||||
// | |||||
//import com.jd.blockchain.binaryproto.DataContractRegistry; | |||||
//import com.jd.blockchain.contract.model.ContractPath; | |||||
//import com.jd.blockchain.crypto.CryptoAlgorithm; | |||||
//import com.jd.blockchain.crypto.CryptoUtils; | |||||
//import com.jd.blockchain.crypto.asymmetric.CryptoKeyPair; | |||||
//import com.jd.blockchain.crypto.asymmetric.PubKey; | |||||
//import com.jd.blockchain.crypto.hash.HashDigest; | |||||
//import com.jd.blockchain.ledger.BlockchainKeyGenerator; | |||||
//import com.jd.blockchain.ledger.BlockchainKeyPair; | |||||
//import com.jd.blockchain.ledger.ContractCodeDeployOperation; | |||||
//import com.jd.blockchain.ledger.ContractEventSendOperation; | |||||
//import com.jd.blockchain.ledger.DataAccountKVSetOperation; | |||||
//import com.jd.blockchain.ledger.DataAccountRegisterOperation; | |||||
//import com.jd.blockchain.ledger.EndpointRequest; | |||||
//import com.jd.blockchain.ledger.LedgerBlock; | |||||
//import com.jd.blockchain.ledger.LedgerInitSetting; | |||||
//import com.jd.blockchain.ledger.LedgerTransaction; | |||||
//import com.jd.blockchain.ledger.NodeRequest; | |||||
//import com.jd.blockchain.ledger.TransactionBuilder; | |||||
//import com.jd.blockchain.ledger.TransactionContent; | |||||
//import com.jd.blockchain.ledger.TransactionContentBody; | |||||
//import com.jd.blockchain.ledger.TransactionRequest; | |||||
//import com.jd.blockchain.ledger.TransactionRequestBuilder; | |||||
//import com.jd.blockchain.ledger.TransactionResponse; | |||||
//import com.jd.blockchain.ledger.TransactionState; | |||||
//import com.jd.blockchain.ledger.UserRegisterOperation; | |||||
//import com.jd.blockchain.ledger.core.CryptoConfig; | |||||
//import com.jd.blockchain.ledger.core.DataAccountSet; | |||||
//import com.jd.blockchain.ledger.core.LedgerDataSet; | |||||
//import com.jd.blockchain.ledger.core.LedgerEditor; | |||||
//import com.jd.blockchain.ledger.core.LedgerRepository; | |||||
//import com.jd.blockchain.ledger.core.LedgerTransactionContext; | |||||
//import com.jd.blockchain.ledger.core.TransactionSet; | |||||
//import com.jd.blockchain.ledger.core.UserAccount; | |||||
//import com.jd.blockchain.ledger.core.impl.DefaultOperationHandleRegisteration; | |||||
//import com.jd.blockchain.ledger.core.impl.LedgerManager; | |||||
//import com.jd.blockchain.ledger.core.impl.OperationHandleRegisteration; | |||||
//import com.jd.blockchain.ledger.core.impl.TransactionBatchProcessor; | |||||
//import com.jd.blockchain.ledger.data.AddressEncoding; | |||||
//import com.jd.blockchain.ledger.data.ConsensusParticipantData; | |||||
//import com.jd.blockchain.ledger.data.LedgerInitSettingData; | |||||
//import com.jd.blockchain.ledger.data.TxBuilder; | |||||
//import com.jd.blockchain.storage.service.utils.MemoryKVStorage; | |||||
// | |||||
//import my.utils.Bytes; | |||||
//import my.utils.io.BytesUtils; | |||||
//import my.utils.net.NetworkAddress; | |||||
// | |||||
//public class TransactionBatchProcessorTest { | |||||
// private HashDigest ledgerHash = null; | |||||
// BlockchainKeyPair userKey = BlockchainKeyGenerator.getInstance().generate(); | |||||
// BlockchainKeyPair dataKey = BlockchainKeyGenerator.getInstance().generate(); | |||||
// BlockchainKeyPair sponsorKey = BlockchainKeyGenerator.getInstance().generate(); | |||||
// BlockchainKeyPair gatewayKey = BlockchainKeyGenerator.getInstance().generate(); | |||||
// BlockchainKeyPair contractKey = BlockchainKeyGenerator.getInstance().generate(); | |||||
// TransactionRequest transactionRequest; | |||||
// String pubKeyVal = "jd.com"+Thread.currentThread(); | |||||
//// String userPubKeyVal = "this is user's pubKey"; | |||||
// byte[] chainCode; | |||||
// // 保存资产总数的键; | |||||
// private static final String KEY_TOTAL = "TOTAL"; | |||||
// //第二个参数; | |||||
// private static final String KEY_ABC = "abc"; | |||||
// // 采用基于内存的 Storage; | |||||
// MemoryKVStorage storage = new MemoryKVStorage(); | |||||
// | |||||
// @Before | |||||
// public void setUp(){ | |||||
// DataContractRegistry.register(TransactionContent.class); | |||||
// DataContractRegistry.register(TransactionContentBody.class); | |||||
// DataContractRegistry.register(TransactionRequest.class); | |||||
// DataContractRegistry.register(NodeRequest.class); | |||||
// DataContractRegistry.register(EndpointRequest.class); | |||||
// DataContractRegistry.register(TransactionResponse.class); | |||||
// DataContractRegistry.register(UserRegisterOperation.class); | |||||
// } | |||||
// | |||||
//// @After | |||||
// public void after() { | |||||
// //清理所有使用的临时文件; | |||||
// String outputPath = null; | |||||
// try { | |||||
// outputPath = ContractPath.getOutputPath(); | |||||
// }finally { | |||||
// deleteDir(new File(outputPath)); | |||||
// } | |||||
// } | |||||
// | |||||
// /** | |||||
// * 递归删除目录下的所有文件及子目录下所有文件 | |||||
// * @param dir 将要删除的文件目录 | |||||
// * @return boolean Returns "true" if all deletions were successful. | |||||
// * If a deletion fails, the method stops attempting to | |||||
// * delete and returns "false". | |||||
// */ | |||||
// private static boolean deleteDir(File dir) { | |||||
// if (dir.isDirectory()) { | |||||
// String[] children = dir.list(); | |||||
// //递归删除目录中的子目录下 | |||||
// for (int i=0; i<children.length; i++) { | |||||
// boolean success = deleteDir(new File(dir, children[i])); | |||||
// if (!success) { | |||||
// return false; | |||||
// } | |||||
// } | |||||
// } | |||||
// // 目录此时为空,可以删除 | |||||
// return dir.delete(); | |||||
// } | |||||
// | |||||
// @Test | |||||
// public void testContractOperations() { | |||||
// LedgerManager ledgerManager = new LedgerManager(); | |||||
// | |||||
// ledgerHash = initLedger(ledgerManager, storage, sponsorKey,gatewayKey,contractKey); | |||||
// LedgerRepository ledgerRepo = ledgerManager.register(ledgerHash, storage); | |||||
// | |||||
// LedgerDataSet previousBlockDataset = ledgerRepo.getDataSet(ledgerRepo.getLatestBlock()); | |||||
// UserAccount userAcc = previousBlockDataset.getUserAccountSet().getUser(sponsorKey.getAddress()); | |||||
// assertNotNull(userAcc); | |||||
// boolean sponsorRegistered = previousBlockDataset.getUserAccountSet().contains(sponsorKey.getAddress()); | |||||
// assertTrue(sponsorRegistered); | |||||
// | |||||
// LedgerEditor newBlockEditor = ledgerRepo.createNextBlock(); | |||||
// | |||||
// OperationHandleRegisteration opReg = new DefaultOperationHandleRegisteration(); | |||||
// TransactionBatchProcessor txbatchProcessor = new TransactionBatchProcessor(newBlockEditor, previousBlockDataset, | |||||
// opReg, ledgerManager); | |||||
// | |||||
// // TODO: 生成合约交易;以及执行合约交易; | |||||
// transactionRequest = this.contractDeploy(ledgerHash, userKey, dataKey,sponsorKey,gatewayKey,contractKey); | |||||
// txbatchProcessor.schedule(transactionRequest); | |||||
// | |||||
// LedgerBlock newBlock = newBlockEditor.prepare(); | |||||
// newBlockEditor.commit(); | |||||
// | |||||
// //验证正确性; | |||||
// ledgerManager = new LedgerManager(); | |||||
// ledgerRepo = ledgerManager.register(ledgerHash, storage); | |||||
// | |||||
// //验证合约部署 | |||||
// byte[] actualChainCode = ledgerRepo.getContractAccountSet(newBlock).getContract(contractKey.getAddress()).getChainCode(); | |||||
// assertArrayEquals(chainCode, actualChainCode); | |||||
// | |||||
// LedgerBlock latestBlock = ledgerRepo.getLatestBlock(); | |||||
// assertEquals(newBlock.getHash(), latestBlock.getHash()); | |||||
// | |||||
// //执行事件; | |||||
// LedgerEditor newBlockEditor1 = ledgerRepo.createNextBlock(); | |||||
// LedgerDataSet previousBlockDataset1 = ledgerRepo.getDataSet(ledgerRepo.getLatestBlock()); | |||||
//// 采用schedule方式; | |||||
// txbatchProcessor = new TransactionBatchProcessor(newBlockEditor1, previousBlockDataset1, | |||||
// opReg, ledgerManager); | |||||
// txbatchProcessor.schedule(this.exeContractReq(ledgerHash, sponsorKey,gatewayKey,contractKey)); | |||||
// //采用newTransaction方式处理; | |||||
// /** | |||||
// LedgerTransactionContext txCtx = newBlockEditor1.newTransaction( | |||||
// this.exeContractReq(ledgerHash, sponsorKey,gatewayKey,contractKey)); | |||||
// LedgerDataSet ldgDS = txCtx.getDataSet(); | |||||
// SignatureDigest signDigest = CryptoUtils.sign(CryptoAlgorithm.ED25519).sign(contractKey.getPrivKey(), Base58Utils.decode(contractKey.getAddress())); | |||||
// DigitalSignatureBlob dgtSign = new DigitalSignatureBlob(contractKey.getPubKey(), signDigest); | |||||
// // DataAccount dataAccount = ldgDS.getDataAccountSet().register(dataKey.getAddress(), dataKey.getPubKey(),null); | |||||
// // dataAccount.setBytes("latestBlockHash", ledgerRepo.getLatestBlock().getPreviousHash().getRawDigest(), -1); | |||||
// // 提交交易结果; | |||||
// LedgerTransaction tx = txCtx.commit(TransactionState.SUCCESS); | |||||
// */ | |||||
// | |||||
// LedgerBlock newBlock1 = newBlockEditor1.prepare(); | |||||
// newBlockEditor1.commit(); | |||||
//// assertEquals(expected, actual); | |||||
// | |||||
// //验证合约中的赋值,外部可以获得; | |||||
// //base test; | |||||
// DataAccountSet dataAccountSet = ledgerRepo.getDataAccountSet(newBlock1); | |||||
// PubKey pubKey = new PubKey(CryptoAlgorithm.ED25519, pubKeyVal.getBytes()); | |||||
// Bytes dataAddress = AddressEncoding.generateAddress(pubKey); | |||||
// assertEquals(dataAddress,dataAccountSet.getDataAccount(dataAddress).getAddress()); | |||||
// assertEquals("hello",new String(dataAccountSet.getDataAccount(dataAddress).getBytes(KEY_TOTAL,-1))); | |||||
// //验证userAccount,从合约内部赋值,然后外部验证; | |||||
//// UserAccountSet userAccountSet = ledgerRepo.getUserAccountSet(newBlock1); | |||||
//// PubKey userPubKey = new PubKey(CryptoAlgorithm.ED25519, userPubKeyVal.getBytes()); | |||||
//// String userAddress = AddressEncoding.generateAddress(userPubKey); | |||||
//// assertEquals(userAddress,userAccountSet.getUser(userAddress).getAddress()); | |||||
// | |||||
// } | |||||
// | |||||
// private TransactionRequest contractDeploy(HashDigest ledgerHash, BlockchainKeyPair userKey, BlockchainKeyPair dataKey, | |||||
// BlockchainKeyPair sponsorKey, BlockchainKeyPair gatewayKey, BlockchainKeyPair contractKey){ | |||||
// // Build transaction request; | |||||
// TransactionBuilder txBuilder = new TxBuilder(ledgerHash); | |||||
// UserRegisterOperation userRegOp = txBuilder.users().register(userKey.getIdentity()); | |||||
// DataAccountRegisterOperation dataAccRegOp = txBuilder.dataAccounts().register(dataKey.getIdentity()); | |||||
// | |||||
// DataAccountKVSetOperation kvsetOP = txBuilder.dataAccount(dataKey.getAddress()) | |||||
// .set("A", "Value_A_0".getBytes(), -1) | |||||
// .set("B", "Value_B_0".getBytes(), -1) | |||||
// .set(KEY_TOTAL, "total value,dataAccount".getBytes(), -1) | |||||
// .set(KEY_ABC, "abc value,dataAccount".getBytes(), -1) | |||||
// //所有的模拟数据都在这个dataAccount中填充; | |||||
// .set("ledgerHash", ledgerHash.getRawDigest(), -1) | |||||
//// .set("latestBlockHash", ledgerBlock.getPreviousHash().getRawDigest(), -1) | |||||
// .getOperation(); | |||||
// chainCode = this.getChainCodeBytes(); | |||||
// ContractCodeDeployOperation contractDplOP = txBuilder.contracts().deploy(contractKey.getIdentity(), this.getChainCodeBytes()); | |||||
//// ContractEventSendOperation contractEvtSendOP = txBuilder.contractEvents().send(contractKey.getAddress(), "test", | |||||
//// "TestContractArgs".getBytes()); | |||||
// TransactionRequestBuilder txReqBuilder = txBuilder.prepareRequest(); | |||||
// txReqBuilder.signAsEndpoint(sponsorKey); | |||||
// txReqBuilder.signAsNode(gatewayKey); | |||||
// return txReqBuilder.buildRequest(); | |||||
// } | |||||
// | |||||
// private TransactionRequest exeContractReq(HashDigest ledgerHash, BlockchainKeyPair sponsorKey, BlockchainKeyPair gatewayKey, BlockchainKeyPair contractKey){ | |||||
// // Build transaction request; | |||||
// TransactionBuilder txBuilder = new TxBuilder(ledgerHash); | |||||
// LedgerManager ledgerManager = new LedgerManager(); | |||||
// LedgerRepository ledgerRepo = ledgerManager.register(ledgerHash, storage); | |||||
// LedgerBlock ledgerBlock = ledgerRepo.getLatestBlock(); | |||||
//// txBuilder.dataAccounts().register(dataKey1.getIdentity()); | |||||
//// txBuilder.dataAccount(dataKey.getAddress()) | |||||
//// .set("latestBlockHash", ledgerBlock.getPreviousHash().getRawDigest(), -1).getOperation(); | |||||
//// HashDigest txRootHashDigest = ledgerRepo.getTransactionSet(ledgerBlock).getRootHash(); | |||||
// | |||||
// LedgerDataSet previousBlockDataset = ledgerRepo.getDataSet(ledgerRepo.getLatestBlock()); | |||||
// previousBlockDataset.getUserAccountSet().getUser(userKey.getAddress()); | |||||
// | |||||
// | |||||
// ContractEventSendOperation contractEvtSendOP = txBuilder.contractEvents().send(contractKey.getAddress(), "issue-asset", | |||||
// ("888##abc##"+dataKey.getAddress()+"##"+ledgerBlock.getPreviousHash().toBase58()+"##"+ | |||||
// userKey.getAddress()+"##"+contractKey.getAddress()+"##"+transactionRequest.getTransactionContent().getHash().toBase58() | |||||
// +"##"+pubKeyVal).getBytes()); | |||||
// | |||||
// TransactionSet txset = ledgerRepo.getTransactionSet(ledgerBlock); | |||||
// txset.get(transactionRequest.getTransactionContent().getHash());//此处有value; | |||||
// | |||||
// TransactionRequestBuilder txReqBuilder = txBuilder.prepareRequest(); | |||||
// txReqBuilder.signAsEndpoint(sponsorKey); | |||||
// txReqBuilder.signAsNode(gatewayKey); | |||||
// return txReqBuilder.buildRequest(); | |||||
// } | |||||
// | |||||
// private HashDigest initLedger(LedgerManager ledgerManager, MemoryKVStorage storage, BlockchainKeyPair userKP, | |||||
// BlockchainKeyPair gatewayKey,BlockchainKeyPair contractKey) { | |||||
// // 创建账本初始化配置; | |||||
// LedgerInitSetting initSetting = createLedgerInitSetting(); | |||||
// | |||||
// // 新建账本; | |||||
// LedgerEditor ldgEdt = ledgerManager.newLedger(initSetting, storage); | |||||
// | |||||
// // 创建一个模拟的创世交易; | |||||
// TransactionRequest genesisTxReq = LedgerTestUtils.createTxRequest(ledgerHash, | |||||
// CryptoUtils.sign(CryptoAlgorithm.ED25519)); | |||||
// | |||||
// // 记录交易,注册用户; | |||||
// LedgerTransactionContext txCtx = ldgEdt.newTransaction(genesisTxReq); | |||||
// LedgerDataSet ldgDS = txCtx.getDataSet(); | |||||
// UserAccount userAccount = ldgDS.getUserAccountSet().register(userKP.getAddress(), userKP.getPubKey()); | |||||
// userAccount.setProperty("Name", "孙悟空", -1); | |||||
// userAccount.setProperty("Age", "10000", -1); | |||||
// System.out.println("UserAddress=" + userAccount.getAddress()); | |||||
// | |||||
// UserAccount gatewayUserAccount = ldgDS.getUserAccountSet().register(gatewayKey.getAddress(), gatewayKey.getPubKey()); | |||||
// userAccount.setProperty("Name", "齐天大圣", -1); | |||||
// userAccount.setProperty("Age", "11111", -1); | |||||
// System.out.println("gatewayUserAccount=" + userAccount.getAddress()); | |||||
// | |||||
// //注册contract; | |||||
//// SignatureDigest signDigest = CryptoUtils.sign(CryptoAlgorithm.ED25519).sign(contractKey.getPrivKey(), Base58Utils.decode(contractKey.getAddress())); | |||||
//// DigitalSignatureBlob dgtSign = new DigitalSignatureBlob(contractKey.getPubKey(), signDigest); | |||||
//// ldgDS.getContractAccountSet().deploy(contractKey.getAddress(),contractKey.getPubKey(),dgtSign,this.contractBytes()); | |||||
// | |||||
// // 提交交易结果; | |||||
// LedgerTransaction tx = txCtx.commit(TransactionState.SUCCESS); | |||||
// | |||||
// // 生成区块; | |||||
// LedgerBlock genesisBlock = ldgEdt.prepare(); | |||||
// ldgEdt.commit(); | |||||
// return genesisBlock.getHash(); | |||||
// } | |||||
// | |||||
//// private HashDigest handleContractEvent(LedgerEditor ldgEdt,BlockchainKeyPair userKP, | |||||
//// BlockchainKeyPair gatewayKey,BlockchainKeyPair contractKey) { | |||||
//// // 记录交易,注册用户; | |||||
//// LedgerTransactionContext txCtx = ldgEdt.newTransaction( | |||||
//// this.genContractReq(ledgerHash,sponsorKey,gatewayKey,contractKey)); | |||||
//// LedgerDataSet ldgDS = txCtx.getDataSet(); | |||||
//// | |||||
//// //注册contract; | |||||
//// SignatureDigest signDigest = CryptoUtils.sign(CryptoAlgorithm.ED25519).sign(contractKey.getPrivKey(), Base58Utils.decode(contractKey.getAddress())); | |||||
//// DigitalSignatureBlob dgtSign = new DigitalSignatureBlob(contractKey.getPubKey(), signDigest); | |||||
//// ldgDS.getContractAccountSet().deploy(contractKey.getAddress(),contractKey.getPubKey(),dgtSign,this.getChainCodeBytes()); | |||||
//// | |||||
//// // 提交交易结果; | |||||
//// LedgerTransaction tx = txCtx.commit(TransactionState.SUCCESS); | |||||
//// | |||||
//// // 生成区块; | |||||
//// LedgerBlock newBlock = ldgEdt.prepare(); | |||||
//// ldgEdt.commit(); | |||||
//// return newBlock.getHash(); | |||||
//// } | |||||
// | |||||
// private byte[] getChainCodeBytes(){ | |||||
// //构建合约的字节数组; | |||||
// byte[] contractCode= null; | |||||
// File file = null; | |||||
// InputStream input = null; | |||||
// try { | |||||
// String contractZipName = "AssetContract1.contract"; | |||||
// URL url = Paths.get(ContractPath.getOutputPath() + contractZipName).toUri().toURL(); | |||||
// file = new File(url.toURI()); | |||||
// assertTrue("contract zip file is not exist.",file.exists()==true); | |||||
// input = new FileInputStream(file); | |||||
// //这种暴力的读取压缩包,在class解析时有问题,所有需要改进; | |||||
// contractCode = new byte[input.available()]; | |||||
// input.read(contractCode); | |||||
// } catch (URISyntaxException e) { | |||||
// e.printStackTrace(); | |||||
// } catch (IOException e) { | |||||
// e.printStackTrace(); | |||||
// } finally { | |||||
// try { | |||||
// if(input!=null){ | |||||
// input.close(); | |||||
// } | |||||
// } catch (IOException e) { | |||||
// e.printStackTrace(); | |||||
// } | |||||
// } | |||||
// return contractCode; | |||||
// } | |||||
// | |||||
// private LedgerInitSetting createLedgerInitSetting() { | |||||
// CryptoConfig defCryptoSetting = new CryptoConfig(); | |||||
// defCryptoSetting.setAutoVerifyHash(true); | |||||
// defCryptoSetting.setHashAlgorithm(CryptoAlgorithm.SHA256); | |||||
// | |||||
// LedgerInitSettingData initSetting = new LedgerInitSettingData(); | |||||
// | |||||
// initSetting.setLedgerSeed(BytesUtils.toBytes("A Test Ledger seed!", "UTF-8")); | |||||
// initSetting.setCryptoSetting(defCryptoSetting); | |||||
// ConsensusParticipantData[] parties = new ConsensusParticipantData[4]; | |||||
// parties[0] = new ConsensusParticipantData(); | |||||
// parties[0].setId(0); | |||||
// parties[0].setName("John"); | |||||
// CryptoKeyPair kp0 = CryptoUtils.sign(CryptoAlgorithm.ED25519).generateKeyPair(); | |||||
// parties[0].setPubKey(kp0.getPubKey()); | |||||
// parties[0].setHostAddress(new NetworkAddress("127.0.0.1", 9000)); | |||||
// | |||||
// parties[1] = new ConsensusParticipantData(); | |||||
// parties[1].setId(1); | |||||
// parties[1].setName("Mary"); | |||||
// CryptoKeyPair kp1 = CryptoUtils.sign(CryptoAlgorithm.ED25519).generateKeyPair(); | |||||
// parties[1].setPubKey(kp1.getPubKey()); | |||||
// parties[1].setHostAddress(new NetworkAddress("127.0.0.1", 9010)); | |||||
// | |||||
// parties[2] = new ConsensusParticipantData(); | |||||
// parties[2].setId(2); | |||||
// parties[2].setName("Jerry"); | |||||
// CryptoKeyPair kp2 = CryptoUtils.sign(CryptoAlgorithm.ED25519).generateKeyPair(); | |||||
// parties[2].setPubKey(kp1.getPubKey()); | |||||
// parties[2].setHostAddress(new NetworkAddress("127.0.0.1", 9020)); | |||||
// | |||||
// parties[3] = new ConsensusParticipantData(); | |||||
// parties[3].setId(3); | |||||
// parties[3].setName("Tom"); | |||||
// CryptoKeyPair kp3 = CryptoUtils.sign(CryptoAlgorithm.ED25519).generateKeyPair(); | |||||
// parties[3].setPubKey(kp1.getPubKey()); | |||||
// parties[3].setHostAddress(new NetworkAddress("127.0.0.1", 9030)); | |||||
// | |||||
// initSetting.setConsensusParticipants(parties); | |||||
// | |||||
// return initSetting; | |||||
// } | |||||
//} | |||||
package test.com.jd.blockchain.ledger; | |||||
import static org.junit.Assert.assertEquals; | |||||
import static org.junit.Assert.assertNotNull; | |||||
import static org.junit.Assert.assertNull; | |||||
import static org.junit.Assert.assertTrue; | |||||
import org.junit.Test; | |||||
import com.jd.blockchain.binaryproto.DataContractRegistry; | |||||
import com.jd.blockchain.crypto.HashDigest; | |||||
import com.jd.blockchain.ledger.BlockchainKeyGenerator; | |||||
import com.jd.blockchain.ledger.BlockchainKeypair; | |||||
import com.jd.blockchain.ledger.EndpointRequest; | |||||
import com.jd.blockchain.ledger.LedgerBlock; | |||||
import com.jd.blockchain.ledger.LedgerInitSetting; | |||||
import com.jd.blockchain.ledger.LedgerTransaction; | |||||
import com.jd.blockchain.ledger.NodeRequest; | |||||
import com.jd.blockchain.ledger.TransactionContent; | |||||
import com.jd.blockchain.ledger.TransactionContentBody; | |||||
import com.jd.blockchain.ledger.TransactionRequest; | |||||
import com.jd.blockchain.ledger.TransactionResponse; | |||||
import com.jd.blockchain.ledger.TransactionState; | |||||
import com.jd.blockchain.ledger.UserRegisterOperation; | |||||
import com.jd.blockchain.ledger.core.LedgerDataSet; | |||||
import com.jd.blockchain.ledger.core.LedgerEditor; | |||||
import com.jd.blockchain.ledger.core.LedgerRepository; | |||||
import com.jd.blockchain.ledger.core.LedgerTransactionContext; | |||||
import com.jd.blockchain.ledger.core.UserAccount; | |||||
import com.jd.blockchain.ledger.core.impl.DefaultOperationHandleRegisteration; | |||||
import com.jd.blockchain.ledger.core.impl.LedgerManager; | |||||
import com.jd.blockchain.ledger.core.impl.LedgerTransactionalEditor; | |||||
import com.jd.blockchain.ledger.core.impl.OperationHandleRegisteration; | |||||
import com.jd.blockchain.ledger.core.impl.TransactionBatchProcessor; | |||||
import com.jd.blockchain.storage.service.utils.MemoryKVStorage; | |||||
public class TransactionBatchProcessorTest { | |||||
static { | |||||
DataContractRegistry.register(TransactionContent.class); | |||||
DataContractRegistry.register(TransactionContentBody.class); | |||||
DataContractRegistry.register(TransactionRequest.class); | |||||
DataContractRegistry.register(NodeRequest.class); | |||||
DataContractRegistry.register(EndpointRequest.class); | |||||
DataContractRegistry.register(TransactionResponse.class); | |||||
DataContractRegistry.register(UserRegisterOperation.class); | |||||
} | |||||
private static final String LEDGER_KEY_PREFIX = "LDG://"; | |||||
private HashDigest ledgerHash = null; | |||||
private BlockchainKeypair parti0 = BlockchainKeyGenerator.getInstance().generate(); | |||||
private BlockchainKeypair parti1 = BlockchainKeyGenerator.getInstance().generate(); | |||||
private BlockchainKeypair parti2 = BlockchainKeyGenerator.getInstance().generate(); | |||||
private BlockchainKeypair parti3 = BlockchainKeyGenerator.getInstance().generate(); | |||||
private TransactionRequest transactionRequest; | |||||
// 采用基于内存的 Storage; | |||||
MemoryKVStorage storage = new MemoryKVStorage(); | |||||
@Test | |||||
public void testContractOperations() { | |||||
// 初始化账本到指定的存储库; | |||||
ledgerHash = initLedger(storage, parti0, parti1, parti2, parti3); | |||||
// 加载账本; | |||||
LedgerManager ledgerManager = new LedgerManager(); | |||||
LedgerRepository ledgerRepo = ledgerManager.register(ledgerHash, storage); | |||||
// 验证参与方账户的存在; | |||||
LedgerDataSet previousBlockDataset = ledgerRepo.getDataSet(ledgerRepo.getLatestBlock()); | |||||
UserAccount user0 = previousBlockDataset.getUserAccountSet().getUser(parti0.getAddress()); | |||||
assertNotNull(user0); | |||||
boolean partiRegistered = previousBlockDataset.getUserAccountSet().contains(parti0.getAddress()); | |||||
assertTrue(partiRegistered); | |||||
// 生成新区块; | |||||
LedgerEditor newBlockEditor = ledgerRepo.createNextBlock(); | |||||
OperationHandleRegisteration opReg = new DefaultOperationHandleRegisteration(); | |||||
TransactionBatchProcessor txbatchProcessor = new TransactionBatchProcessor(newBlockEditor, previousBlockDataset, | |||||
opReg, ledgerManager); | |||||
// 注册新用户; | |||||
BlockchainKeypair userKeypair = BlockchainKeyGenerator.getInstance().generate(); | |||||
transactionRequest = LedgerTestUtils.createTxRequest_UserReg(ledgerHash, userKeypair, parti0); | |||||
txbatchProcessor.schedule(transactionRequest); | |||||
LedgerBlock newBlock = newBlockEditor.prepare(); | |||||
newBlockEditor.commit(); | |||||
// 验证正确性; | |||||
ledgerManager = new LedgerManager(); | |||||
ledgerRepo = ledgerManager.register(ledgerHash, storage); | |||||
LedgerBlock latestBlock = ledgerRepo.getLatestBlock(); | |||||
assertEquals(newBlock.getHash(), latestBlock.getHash()); | |||||
assertEquals(1, newBlock.getHeight()); | |||||
} | |||||
private HashDigest initLedger(MemoryKVStorage storage, BlockchainKeypair... partiKeys) { | |||||
// 创建初始化配置; | |||||
LedgerInitSetting initSetting = LedgerTestUtils.createLedgerInitSetting(partiKeys); | |||||
// 创建账本; | |||||
LedgerEditor ldgEdt = LedgerTransactionalEditor.createEditor(initSetting, LEDGER_KEY_PREFIX, storage, storage); | |||||
TransactionRequest genesisTxReq = LedgerTestUtils.createTxRequest_UserReg(null); | |||||
LedgerTransactionContext genisisTxCtx = ldgEdt.newTransaction(genesisTxReq); | |||||
LedgerDataSet ldgDS = genisisTxCtx.getDataSet(); | |||||
for (int i = 0; i < partiKeys.length; i++) { | |||||
UserAccount userAccount = ldgDS.getUserAccountSet().register(partiKeys[i].getAddress(), | |||||
partiKeys[i].getPubKey()); | |||||
userAccount.setProperty("Name", "参与方-" + i, -1); | |||||
userAccount.setProperty("Share", "" + (10 + i), -1); | |||||
} | |||||
LedgerTransaction tx = genisisTxCtx.commit(TransactionState.SUCCESS); | |||||
assertEquals(genesisTxReq.getTransactionContent().getHash(), tx.getTransactionContent().getHash()); | |||||
assertEquals(0, tx.getBlockHeight()); | |||||
LedgerBlock block = ldgEdt.prepare(); | |||||
assertEquals(0, block.getHeight()); | |||||
assertNotNull(block.getHash()); | |||||
assertNull(block.getPreviousHash()); | |||||
assertEquals(block.getHash(), block.getLedgerHash()); | |||||
// 提交数据,写入存储; | |||||
ldgEdt.commit(); | |||||
HashDigest ledgerHash = block.getHash(); | |||||
return ledgerHash; | |||||
} | |||||
} |
@@ -1,18 +1,13 @@ | |||||
package com.jd.blockchain.transaction; | package com.jd.blockchain.transaction; | ||||
import com.jd.blockchain.contract.Contract; | |||||
import com.jd.blockchain.contract.ContractEvent; | |||||
import com.jd.blockchain.utils.BaseConstant; | |||||
import com.jd.blockchain.utils.Bytes; | |||||
import com.jd.blockchain.utils.IllegalDataException; | |||||
import java.lang.annotation.Annotation; | import java.lang.annotation.Annotation; | ||||
import java.lang.reflect.Method; | |||||
import java.lang.reflect.Proxy; | import java.lang.reflect.Proxy; | ||||
import java.util.HashMap; | |||||
import java.util.Map; | import java.util.Map; | ||||
import java.util.concurrent.ConcurrentHashMap; | import java.util.concurrent.ConcurrentHashMap; | ||||
import com.jd.blockchain.contract.Contract; | |||||
import com.jd.blockchain.utils.Bytes; | |||||
public class ContractInvocationProxyBuilder { | public class ContractInvocationProxyBuilder { | ||||
private Map<Class<?>, ContractType> contractTypes = new ConcurrentHashMap<>(); | private Map<Class<?>, ContractType> contractTypes = new ConcurrentHashMap<>(); | ||||
@@ -477,40 +477,40 @@ public class IntegrationBase { | |||||
assertArrayEquals(contractCode, contractCodeInDb); | assertArrayEquals(contractCode, contractCodeInDb); | ||||
// execute the contract; | // execute the contract; | ||||
testContractExe(adminKey, ledgerHash, keyPairResponse.keyPair, blockchainService, ledgerRepository); | |||||
// testContractExe(adminKey, ledgerHash, keyPairResponse.keyPair, blockchainService, ledgerRepository); | |||||
// testContractExe1(adminKey, ledgerHash, keyPairResponse.keyPair, blockchainService, ledgerRepository); | // testContractExe1(adminKey, ledgerHash, keyPairResponse.keyPair, blockchainService, ledgerRepository); | ||||
return block; | return block; | ||||
} | } | ||||
private static <T> void testContractExe(AsymmetricKeypair adminKey, HashDigest ledgerHash, BlockchainKeypair dataKey, | |||||
BlockchainService blockchainService, LedgerRepository ledgerRepository) { | |||||
LedgerInfo ledgerInfo = blockchainService.getLedger(ledgerHash); | |||||
LedgerBlock previousBlock = blockchainService.getBlock(ledgerHash, ledgerInfo.getLatestBlockHeight() - 1); | |||||
// 定义交易; | |||||
TransactionTemplate txTpl = blockchainService.newTransaction(ledgerHash); | |||||
Byte byteObj = Byte.parseByte("123"); | |||||
// private static <T> void testContractExe(AsymmetricKeypair adminKey, HashDigest ledgerHash, BlockchainKeypair dataKey, | |||||
// BlockchainService blockchainService, LedgerRepository ledgerRepository) { | |||||
// LedgerInfo ledgerInfo = blockchainService.getLedger(ledgerHash); | |||||
// LedgerBlock previousBlock = blockchainService.getBlock(ledgerHash, ledgerInfo.getLatestBlockHeight() - 1); | |||||
// | |||||
// // 定义交易; | |||||
// TransactionTemplate txTpl = blockchainService.newTransaction(ledgerHash); | |||||
// | |||||
// Byte byteObj = Byte.parseByte("123"); | |||||
//// txTpl.contract(contractDeployKey.getAddress(),AssetContract2.class).issue(byteObj, | |||||
//// contractDeployKey.getAddress().toBase58(),321123); | |||||
// txTpl.contract(contractDeployKey.getAddress(),AssetContract2.class).issue(byteObj, | // txTpl.contract(contractDeployKey.getAddress(),AssetContract2.class).issue(byteObj, | ||||
// contractDeployKey.getAddress().toBase58(),321123); | |||||
txTpl.contract(contractDeployKey.getAddress(),AssetContract2.class).issue(byteObj, | |||||
dataKey.getAddress().toBase58(),Bytes.fromString("123321")); | |||||
// 签名; | |||||
PreparedTransaction ptx = txTpl.prepare(); | |||||
ptx.sign(adminKey); | |||||
// 提交并等待共识返回; | |||||
TransactionResponse txResp = ptx.commit(); | |||||
// 验证结果; | |||||
Assert.assertTrue(txResp.isSuccess()); | |||||
assertEquals(ptx.getHash(),txResp.getContentHash()); | |||||
LedgerBlock block = ledgerRepository.getBlock(txResp.getBlockHeight()); | |||||
KVDataEntry[] kvDataEntries = ledgerRepository.getDataAccountSet(block).getDataAccount(dataKey.getAddress()).getDataEntries(0,1); | |||||
assertEquals("100",kvDataEntries[0].getValue().toString()); | |||||
} | |||||
// dataKey.getAddress().toBase58(),Bytes.fromString("123321")); | |||||
// | |||||
// // 签名; | |||||
// PreparedTransaction ptx = txTpl.prepare(); | |||||
// ptx.sign(adminKey); | |||||
// | |||||
// // 提交并等待共识返回; | |||||
// TransactionResponse txResp = ptx.commit(); | |||||
// | |||||
// // 验证结果; | |||||
// Assert.assertTrue(txResp.isSuccess()); | |||||
// assertEquals(ptx.getHash(),txResp.getContentHash()); | |||||
// LedgerBlock block = ledgerRepository.getBlock(txResp.getBlockHeight()); | |||||
// KVDataEntry[] kvDataEntries = ledgerRepository.getDataAccountSet(block).getDataAccount(dataKey.getAddress()).getDataEntries(0,1); | |||||
// assertEquals("100",kvDataEntries[0].getValue().toString()); | |||||
// } | |||||
// private static <T> void testContractExe1(AsymmetricKeypair adminKey, HashDigest ledgerHash, BlockchainKeypair dataKey, | // private static <T> void testContractExe1(AsymmetricKeypair adminKey, HashDigest ledgerHash, BlockchainKeypair dataKey, | ||||
// BlockchainService blockchainService,LedgerRepository ledgerRepository) { | // BlockchainService blockchainService,LedgerRepository ledgerRepository) { | ||||
@@ -1,41 +0,0 @@ | |||||
package test.com.jd.blockchain.intgr.contract; | |||||
import com.jd.blockchain.contract.Contract; | |||||
import com.jd.blockchain.contract.ContractEvent; | |||||
import com.jd.blockchain.utils.Bytes; | |||||
/** | |||||
* 示例:一个“资产管理”智能合约; | |||||
* | |||||
* @author zhaogw | |||||
*/ | |||||
@Contract | |||||
public interface AssetContract2 { | |||||
// /** | |||||
// * 发行资产; | |||||
// * 新发行的资产数量; | |||||
// * @param assetHolderAddress | |||||
// * 新发行的资产的持有账户; | |||||
// */ | |||||
// @ContractEvent(name = "issue-asset-0") | |||||
// void issue(ContractBizContent contractBizContent, String assetHolderAddress); | |||||
// | |||||
// /** | |||||
// * 发行资产; | |||||
// * 新发行的资产数量; | |||||
// * @param assetHolderAddress | |||||
// * 新发行的资产的持有账户; | |||||
// */ | |||||
// @ContractEvent(name = "issue-asset") | |||||
// void issue(ContractBizContent contractBizContent, String assetHolderAddress, long cashNumber); | |||||
@ContractEvent(name = "issue-asset-2") | |||||
void issue(Bytes bytes, String assetHolderAddress, long cashNumber); | |||||
@ContractEvent(name = "issue-asset-3") | |||||
void issue(Byte byteObj, String assetHolderAddress, long cashNumber); | |||||
@ContractEvent(name = "issue-asset-4") | |||||
void issue(Byte byteObj, String assetHolderAddress, Bytes cashNumber); | |||||
} |
@@ -0,0 +1,5 @@ | |||||
package test.com.jd.blockchain.intgr.contract; | |||||
public class AssetContractImpl { | |||||
} |
@@ -0,0 +1,14 @@ | |||||
package test.com.jd.blockchain.intgr.contract; | |||||
import static org.junit.Assert.*; | |||||
import org.junit.Test; | |||||
public class ContractInvocationTest { | |||||
@Test | |||||
public void test() { | |||||
} | |||||
} |
@@ -58,8 +58,6 @@ public class LedgerBlockGeneratingTest { | |||||
DefaultOperationHandleRegisteration opHandler = new DefaultOperationHandleRegisteration(); | DefaultOperationHandleRegisteration opHandler = new DefaultOperationHandleRegisteration(); | ||||
System.gc(); | |||||
test(ledgerHash, node.getPartiKey(), ledgerManager, opHandler, 1000, 5); | test(ledgerHash, node.getPartiKey(), ledgerManager, opHandler, 1000, 5); | ||||
} | } | ||||
@@ -442,37 +442,44 @@ public class FileUtils { | |||||
} | } | ||||
public static void deleteFile(File file) { | public static void deleteFile(File file) { | ||||
deleteFile(file, false); | |||||
deletePath(file, false); | |||||
} | } | ||||
public static void deleteFile(String dir, boolean silent) { | public static void deleteFile(String dir, boolean silent) { | ||||
File directory = new File(dir); | File directory = new File(dir); | ||||
deleteFile(directory, silent); | |||||
deletePath(directory, silent); | |||||
} | } | ||||
/** | /** | ||||
* 删除文件; | |||||
* 删除文件;<br> | |||||
* | * | ||||
* @param file | |||||
* @param silent 是否静默删除;如果为 true ,则吞噬删除过程中的异常,意味着方法即便正常返回时也有可能删除不完全; | |||||
* @param path 如果指定的路径是单个文件,则删除该文件;如果指定路径是目录,则删除该目录及其下的全部文件; | |||||
* @param silent 是否静默删除;如果为 true ,则吞噬删除过程中的异常, 意味着方法即便正常返回时也有可能删除不完全; | |||||
* @return 如果删除成功,则返回 true; 否则返回 false,或者抛出 {@link RuntimeIOException}; | |||||
*/ | */ | ||||
public static void deleteFile(File file, boolean silent) { | |||||
if (file.isFile()) { | |||||
public static boolean deletePath(File path, boolean silent) { | |||||
if (path.isFile()) { | |||||
try { | try { | ||||
file.delete(); | |||||
return; | |||||
path.delete(); | |||||
return true; | |||||
} catch (Exception e) { | } catch (Exception e) { | ||||
if (!silent) { | if (!silent) { | ||||
throw new RuntimeIOException(e.getMessage(), e); | throw new RuntimeIOException(e.getMessage(), e); | ||||
} | } | ||||
} | } | ||||
return false; | |||||
} | } | ||||
File[] files = file.listFiles(); | |||||
// delete dir; | |||||
File[] files = path.listFiles(); | |||||
if (files == null) { | if (files == null) { | ||||
return; | |||||
return false; | |||||
} | } | ||||
for (File f : files) { | for (File f : files) { | ||||
deleteFile(f, silent); | |||||
deletePath(f, silent); | |||||
} | } | ||||
return path.delete(); | |||||
} | } | ||||
} | } |