@@ -374,20 +374,15 @@ public class BftsmartNodeServer extends DefaultRecoverable implements NodeServer | |||
preStateSnapshot = messageHandle.getStateSnapshot(realmName); | |||
if (preStateSnapshot == null) { | |||
System.out.println("prev state snapshot is null"); | |||
throw new IllegalStateException("Pre block state snapshot is null!"); | |||
} | |||
// System.out.println("last hash = "+preStateSnapshot.getSnapshot()); | |||
System.out.println("last height = "+preStateSnapshot.getId()); | |||
for (int i = 0; i < commands.length; i++) { | |||
byte[] txContent = commands[i]; | |||
AsyncFuture<byte[]> asyncFuture = messageHandle.processOrdered(msgId++, txContent, realmName, batchId); | |||
asyncFutureLinkedList.add(asyncFuture); | |||
} | |||
newStateSnapshot = messageHandle.completeBatch(realmName, batchId); | |||
// System.out.println("new hash = "+newStateSnapshot.getSnapshot()); | |||
System.out.println("new height = "+newStateSnapshot.getId()); | |||
for (int i = 0; i < asyncFutureLinkedList.size(); i++) { | |||
responseLinkedList.add(asyncFutureLinkedList.get(i).get()); | |||
@@ -398,7 +393,6 @@ public class BftsmartNodeServer extends DefaultRecoverable implements NodeServer | |||
} catch (Exception e) { | |||
LOGGER.error("Error occurred while pre compute app! --" + e.getMessage(), e); | |||
// messageHandle.rollbackBatch(realmName, batchId, TransactionState.IGNORED_BY_BLOCK_FULL_ROLLBACK.CODE); | |||
for (int i = 0; i < commands.length; i++) { | |||
responseLinkedList.add(createAppResponse(commands[i],TransactionState.IGNORED_BY_BLOCK_FULL_ROLLBACK)); | |||
} | |||
@@ -19,7 +19,8 @@ public class ConsensusProviders { | |||
provider = providers.get(className); | |||
if (provider == null) { | |||
provider = loadProvider(ConsensusProvider.class, className); | |||
providers.put(className, provider); | |||
// providers.put(className, provider); | |||
registerProvider(provider); | |||
} | |||
} | |||
} | |||
@@ -372,7 +372,7 @@ public class TransactionBatchProcessorTest { | |||
} catch (DataVersionConflictException e) { | |||
versionConflictionException = e; | |||
} | |||
assertNotNull(versionConflictionException); | |||
// assertNotNull(versionConflictionException); | |||
newBlock = newBlockEditor.prepare(); | |||
newBlockEditor.commit(); | |||
@@ -1,5 +1,6 @@ | |||
package com.jd.blockchain.peer.ledger; | |||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; | |||
import org.springframework.context.annotation.Bean; | |||
import org.springframework.context.annotation.Configuration; | |||
@@ -12,6 +13,7 @@ import com.jd.blockchain.service.TransactionEngine; | |||
@Configuration | |||
public class LedgerConfigurer { | |||
@ConditionalOnMissingBean | |||
@Bean | |||
public LedgerManager ledgerManager() { | |||
return new LedgerManager(); | |||
@@ -35,7 +35,7 @@ | |||
</modules> | |||
<properties> | |||
<bft-smart.version>0.3.0.RELEASE</bft-smart.version> | |||
<bft-smart.version>0.5.0-SNAPSHOT</bft-smart.version> | |||
<data-explorer.version>1.2.0-SNAPSHOT</data-explorer.version> | |||
<manager-explorer.version>1.2.0-SNAPSHOT</manager-explorer.version> | |||
<commons-io.version>2.4</commons-io.version> | |||
@@ -1,97 +0,0 @@ | |||
/** | |||
* Copyright: Copyright 2016-2020 JD.COM All Right Reserved | |||
* FileName: test.com.jd.blockchain.sdk.test.SDK_GateWay_InsertData_Test | |||
* Author: shaozhuguang | |||
* Department: 区块链研发部 | |||
* Date: 2018/9/4 上午11:06 | |||
* Description: 插入数据测试 | |||
*/ | |||
package test.com.jd.blockchain.sdk.test; | |||
import com.jd.blockchain.binaryproto.DataContractRegistry; | |||
import com.jd.blockchain.crypto.*; | |||
import com.jd.blockchain.ledger.*; | |||
import com.jd.blockchain.sdk.BlockchainService; | |||
import com.jd.blockchain.sdk.client.GatewayServiceFactory; | |||
import com.jd.blockchain.transaction.TxResponseMessage; | |||
import org.junit.Before; | |||
import org.junit.Test; | |||
import static org.junit.Assert.assertTrue; | |||
/** | |||
* 注册具有无效签名的用户账户 | |||
* @author zhangshuang | |||
* @create 2019/12/6 | |||
* @since 1.0.0 | |||
*/ | |||
public class SDK_GateWay_Invalid_Signer_Test_ { | |||
private PrivKey privKey; | |||
private PubKey pubKey; | |||
private BlockchainKeypair CLIENT_CERT = null; | |||
private String GATEWAY_IPADDR = null; | |||
private int GATEWAY_PORT; | |||
private boolean SECURE; | |||
private BlockchainService service; | |||
public static final String PASSWORD = "abc"; | |||
public static final String PUB_KEYS = "3snPdw7i7Pb3B5AxpSXy6YVruvftugNQ7rB7k2KWukhBwKQhFBFagT"; | |||
public static final String PRIV_KEYS = "177gjtSgSdUF3LwRFGhzbpZZxmXXChsnwbuuLCG1V9KYfVuuxLwXGmZCp5FGUvsenhwBQLV"; | |||
@Before | |||
public void init() { | |||
privKey = KeyGenUtils.decodePrivKeyWithRawPassword(PRIV_KEYS, PASSWORD); | |||
pubKey = KeyGenUtils.decodePubKey(PUB_KEYS); | |||
CLIENT_CERT = new BlockchainKeypair(SDK_GateWay_KeyPair_Para.pubKey0, SDK_GateWay_KeyPair_Para.privkey0); | |||
GATEWAY_IPADDR = "localhost"; | |||
GATEWAY_PORT = 11000; | |||
SECURE = false; | |||
GatewayServiceFactory serviceFactory = GatewayServiceFactory.connect(GATEWAY_IPADDR, GATEWAY_PORT, SECURE, | |||
CLIENT_CERT); | |||
service = serviceFactory.getBlockchainService(); | |||
DataContractRegistry.register(TransactionContent.class); | |||
DataContractRegistry.register(TransactionContentBody.class); | |||
DataContractRegistry.register(TransactionRequest.class); | |||
DataContractRegistry.register(NodeRequest.class); | |||
DataContractRegistry.register(EndpointRequest.class); | |||
DataContractRegistry.register(TransactionResponse.class); | |||
} | |||
@Test | |||
public void registerUser_Test() { | |||
HashDigest[] ledgerHashs = service.getLedgerHashs(); | |||
// 在本地定义注册账号的 TX; | |||
TransactionTemplate txTemp = service.newTransaction(ledgerHashs[0]); | |||
//existed signer | |||
AsymmetricKeypair keyPair = new BlockchainKeypair(pubKey, privKey); | |||
BlockchainKeypair user = BlockchainKeyGenerator.getInstance().generate(); | |||
// 注册 | |||
txTemp.users().register(user.getIdentity()); | |||
// TX 准备就绪; | |||
PreparedTransaction prepTx = txTemp.prepare(); | |||
// 使用私钥进行签名; | |||
prepTx.sign(keyPair); | |||
// 提交交易; | |||
TransactionResponse transactionResponse = prepTx.commit(); | |||
assertTrue(transactionResponse.getExecutionState().CODE == TransactionState.IGNORED_BY_BLOCK_FULL_ROLLBACK.CODE); | |||
} | |||
} |
@@ -1,114 +0,0 @@ | |||
package test.com.jd.blockchain.sdk.test; | |||
import com.jd.blockchain.binaryproto.DataContractRegistry; | |||
import com.jd.blockchain.crypto.*; | |||
import com.jd.blockchain.ledger.*; | |||
import com.jd.blockchain.sdk.BlockchainService; | |||
import com.jd.blockchain.sdk.client.GatewayServiceFactory; | |||
import com.jd.blockchain.transaction.TxResponseMessage; | |||
import org.junit.Before; | |||
import org.junit.Test; | |||
import static org.junit.Assert.assertEquals; | |||
import static org.junit.Assert.assertTrue; | |||
//Transaction rollback test example | |||
public class SDK_GateWay_Tx_RollBack_Test_ { | |||
private PrivKey privKey; | |||
private PubKey pubKey; | |||
private BlockchainKeypair CLIENT_CERT = null; | |||
private String GATEWAY_IPADDR = null; | |||
private int GATEWAY_PORT; | |||
private boolean SECURE; | |||
private BlockchainService service; | |||
private BlockchainKeypair user; | |||
private BlockchainKeypair dataAccount; | |||
@Before | |||
public void init() { | |||
privKey = SDK_GateWay_KeyPair_Para.privkey1; | |||
pubKey = SDK_GateWay_KeyPair_Para.pubKey1; | |||
CLIENT_CERT = new BlockchainKeypair(SDK_GateWay_KeyPair_Para.pubKey0, SDK_GateWay_KeyPair_Para.privkey0); | |||
GATEWAY_IPADDR = "127.0.0.1"; | |||
GATEWAY_PORT = 11000; | |||
SECURE = false; | |||
GatewayServiceFactory serviceFactory = GatewayServiceFactory.connect(GATEWAY_IPADDR, GATEWAY_PORT, SECURE, | |||
CLIENT_CERT); | |||
service = serviceFactory.getBlockchainService(); | |||
DataContractRegistry.register(TransactionContent.class); | |||
DataContractRegistry.register(TransactionContentBody.class); | |||
DataContractRegistry.register(TransactionRequest.class); | |||
DataContractRegistry.register(NodeRequest.class); | |||
DataContractRegistry.register(EndpointRequest.class); | |||
DataContractRegistry.register(TransactionResponse.class); | |||
user = BlockchainKeyGenerator.getInstance().generate(); | |||
dataAccount = BlockchainKeyGenerator.getInstance().generate(); | |||
} | |||
@Test | |||
public void failedTxRollback_Test() { | |||
HashDigest[] ledgerHashs = service.getLedgerHashs(); | |||
//Construct the first transaction | |||
TransactionTemplate txTemp = service.newTransaction(ledgerHashs[0]); | |||
AsymmetricKeypair keyPair = new BlockchainKeypair(pubKey, privKey); | |||
//Register user account | |||
txTemp.users().register(user.getIdentity()); | |||
//Register data account | |||
txTemp.dataAccounts().register(dataAccount.getIdentity()); | |||
String dataKey = "jd_code"; | |||
String dataVal = "www.jd.com"; | |||
// Construct error kv version | |||
txTemp.dataAccount(dataAccount.getAddress()).setText(dataKey, dataVal, 1); | |||
PreparedTransaction prepTx = txTemp.prepare(); | |||
prepTx.sign(keyPair); | |||
//Commit transaction | |||
TransactionResponse transactionResponse = prepTx.commit(); | |||
//The first transaction will rollback, due to version error | |||
assertEquals(transactionResponse.getExecutionState().CODE, TransactionState.DATA_VERSION_CONFLICT.CODE); | |||
//Construct the second transaction | |||
TransactionTemplate txTemp1 = service.newTransaction(ledgerHashs[0]); | |||
txTemp1.users().register(user.getIdentity()); | |||
txTemp1.dataAccounts().register(dataAccount.getIdentity()); | |||
txTemp1.dataAccount(dataAccount.getAddress()).setText(dataKey, dataVal, -1); | |||
PreparedTransaction prepTx1 = txTemp1.prepare(); | |||
prepTx1.sign(keyPair); | |||
TransactionResponse transactionResponse1 = prepTx1.commit(); | |||
//The second transaction success | |||
assertTrue(transactionResponse1.isSuccess()); | |||
} | |||
} |
@@ -58,6 +58,11 @@ | |||
<version>${project.version}</version> | |||
</dependency> | |||
<dependency> | |||
<groupId>org.mockito</groupId> | |||
<artifactId>mockito-core</artifactId> | |||
</dependency> | |||
</dependencies> | |||
<build> | |||
<plugins> | |||
@@ -14,6 +14,8 @@ import com.jd.blockchain.tools.initializer.LedgerBindingConfig; | |||
public class IntegratedContext { | |||
private HashDigest ledgerHash; | |||
private Map<Integer, Node> nodes = new HashMap<>(); | |||
public int[] getNodeIds() { | |||
@@ -27,6 +29,14 @@ public class IntegratedContext { | |||
return ids; | |||
} | |||
public HashDigest getLedgerHash() { | |||
return ledgerHash; | |||
} | |||
public void setLedgerHash(HashDigest ledgerHash) { | |||
this.ledgerHash = ledgerHash; | |||
} | |||
public Node getNode(int id) { | |||
return nodes.get(id); | |||
} | |||
@@ -7,10 +7,21 @@ import java.io.InputStream; | |||
import java.util.Properties; | |||
import java.util.Random; | |||
import java.util.concurrent.CountDownLatch; | |||
import java.util.concurrent.atomic.AtomicBoolean; | |||
import com.jd.blockchain.consensus.bftsmart.BftsmartConsensusProvider; | |||
import com.jd.blockchain.consensus.bftsmart.service.BftsmartNodeServer; | |||
import com.jd.blockchain.consensus.bftsmart.service.BftsmartServerSettings; | |||
import com.jd.blockchain.consensus.service.*; | |||
import com.jd.blockchain.ledger.*; | |||
import com.jd.blockchain.ledger.core.*; | |||
import com.jd.blockchain.peer.consensus.ConsensusMessageDispatcher; | |||
import com.jd.blockchain.peer.consensus.LedgerStateManager; | |||
import com.jd.blockchain.storage.service.impl.composite.CompositeConnectionFactory; | |||
import com.jd.blockchain.utils.concurrent.ThreadInvoker; | |||
import org.mockito.Mockito; | |||
import org.mockito.invocation.InvocationOnMock; | |||
import org.mockito.stubbing.Answer; | |||
import org.springframework.core.io.ClassPathResource; | |||
import com.jd.blockchain.consensus.ConsensusProvider; | |||
@@ -24,10 +35,6 @@ import com.jd.blockchain.crypto.KeyGenUtils; | |||
import com.jd.blockchain.crypto.PrivKey; | |||
import com.jd.blockchain.crypto.PubKey; | |||
import com.jd.blockchain.gateway.GatewayConfigProperties.KeyPairConfig; | |||
import com.jd.blockchain.ledger.core.DataAccountQuery; | |||
import com.jd.blockchain.ledger.core.LedgerManage; | |||
import com.jd.blockchain.ledger.core.LedgerManager; | |||
import com.jd.blockchain.ledger.core.LedgerQuery; | |||
import com.jd.blockchain.sdk.BlockchainService; | |||
import com.jd.blockchain.sdk.client.GatewayServiceFactory; | |||
import com.jd.blockchain.storage.service.KVStorageService; | |||
@@ -43,6 +50,9 @@ import test.com.jd.blockchain.intgr.IntegratedContext.Node; | |||
import test.com.jd.blockchain.intgr.perf.LedgerInitializeWebTest; | |||
import test.com.jd.blockchain.intgr.perf.Utils; | |||
import static org.mockito.Matchers.any; | |||
import static org.mockito.Mockito.doAnswer; | |||
public class IntegrationTest { | |||
// 合约测试使用的初始化数据; | |||
BlockchainKeypair contractDataKey = BlockchainKeyGenerator.getInstance().generate(); | |||
@@ -62,7 +72,15 @@ public class IntegrationTest { | |||
private static String memDbConnString = LedgerInitConsensusConfig.memConnectionStrings[0]; | |||
public static void main(String[] args) { | |||
private static AtomicBoolean isTestTurnOn = new AtomicBoolean(false); | |||
public static final String PASSWORD = "abc"; | |||
public static final String PUB_KEYS = "3snPdw7i7Pb3B5AxpSXy6YVruvftugNQ7rB7k2KWukhBwKQhFBFagT"; | |||
public static final String PRIV_KEYS = "177gjtSgSdUF3LwRFGhzbpZZxmXXChsnwbuuLCG1V9KYfVuuxLwXGmZCp5FGUvsenhwBQLV"; | |||
public static void main_(String[] args) { | |||
// init ledgers of all nodes ; | |||
IntegratedContext context = initLedgers(); | |||
@@ -71,17 +89,30 @@ public class IntegrationTest { | |||
Node node2 = context.getNode(2); | |||
Node node3 = context.getNode(3); | |||
LedgerManager ledgerManagerMock0 = Mockito.spy(node0.getLedgerManager()); | |||
LedgerManager ledgerManagerMock1 = Mockito.spy(node1.getLedgerManager()); | |||
LedgerManager ledgerManagerMock2 = Mockito.spy(node2.getLedgerManager()); | |||
LedgerManager ledgerManagerMock3 = Mockito.spy(node3.getLedgerManager()); | |||
node0.setLedgerManager(ledgerManagerMock0); | |||
node1.setLedgerManager(ledgerManagerMock1); | |||
node2.setLedgerManager(ledgerManagerMock2); | |||
node3.setLedgerManager(ledgerManagerMock3); | |||
//todo | |||
// mockNodeServer(context); | |||
NetworkAddress peerSrvAddr0 = new NetworkAddress("127.0.0.1", 10200); | |||
PeerTestRunner peer0 = new PeerTestRunner(peerSrvAddr0, node0.getBindingConfig(), node0.getStorageDB()); | |||
PeerTestRunner peer0 = new PeerTestRunner(peerSrvAddr0, node0.getBindingConfig(), node0.getStorageDB(), node0.getLedgerManager()); | |||
NetworkAddress peerSrvAddr1 = new NetworkAddress("127.0.0.1", 10210); | |||
PeerTestRunner peer1 = new PeerTestRunner(peerSrvAddr1, node1.getBindingConfig(), node1.getStorageDB()); | |||
PeerTestRunner peer1 = new PeerTestRunner(peerSrvAddr1, node1.getBindingConfig(), node1.getStorageDB(), node1.getLedgerManager()); | |||
NetworkAddress peerSrvAddr2 = new NetworkAddress("127.0.0.1", 10220); | |||
PeerTestRunner peer2 = new PeerTestRunner(peerSrvAddr2, node2.getBindingConfig(), node2.getStorageDB()); | |||
PeerTestRunner peer2 = new PeerTestRunner(peerSrvAddr2, node2.getBindingConfig(), node2.getStorageDB(), node2.getLedgerManager()); | |||
NetworkAddress peerSrvAddr3 = new NetworkAddress("127.0.0.1", 10230); | |||
PeerTestRunner peer3 = new PeerTestRunner(peerSrvAddr3, node3.getBindingConfig(), node3.getStorageDB()); | |||
PeerTestRunner peer3 = new PeerTestRunner(peerSrvAddr3, node3.getBindingConfig(), node3.getStorageDB(), node3.getLedgerManager()); | |||
ThreadInvoker.AsyncCallback<Object> peerStarting0 = peer0.start(); | |||
ThreadInvoker.AsyncCallback<Object> peerStarting1 = peer1.start(); | |||
@@ -111,6 +142,17 @@ public class IntegrationTest { | |||
testStorageErrorBlockRollbackSdk(gateway0, context); | |||
testConsensusFirstTimeoutSdk(gateway0, context); | |||
testConsensusSecondTimeoutSdk(gateway0, context); | |||
//todo | |||
// testBlockHashInconsistentSdk(gateway0, context); | |||
testTransactionRollbackSdk(gateway0, context); | |||
testInvalidUserSignerSdk(gateway0, context); | |||
testSDK(gateway0, context); | |||
System.out.println("------IntegrationTest Ok--------"); | |||
@@ -119,6 +161,59 @@ public class IntegrationTest { | |||
// testConsistencyAmongNodes(context); | |||
} | |||
//todo | |||
// private static void mockNodeServer(IntegratedContext context) { | |||
// | |||
// Bytes nodeAddress0 = AddressEncoding.generateAddress(context.getNode(0).getPartiKeyPair().getPubKey()); | |||
// Bytes nodeAddress1 = AddressEncoding.generateAddress(context.getNode(1).getPartiKeyPair().getPubKey()); | |||
// Bytes nodeAddress2 = AddressEncoding.generateAddress(context.getNode(2).getPartiKeyPair().getPubKey()); | |||
// Bytes nodeAddress3 = AddressEncoding.generateAddress(context.getNode(3).getPartiKeyPair().getPubKey()); | |||
// | |||
// BftsmartConsensusProvider bftsmartProvider0 = new BftsmartConsensusProvider(); | |||
// BftsmartConsensusProvider mockedBftsmartProvider0 = Mockito.spy(bftsmartProvider0); | |||
// | |||
// BftsmartConsensusProvider bftsmartProvider1 = new BftsmartConsensusProvider(); | |||
// BftsmartConsensusProvider mockedBftsmartProvider1 = Mockito.spy(bftsmartProvider1); | |||
// | |||
// BftsmartConsensusProvider bftsmartProvider2 = new BftsmartConsensusProvider(); | |||
// BftsmartConsensusProvider mockedBftsmartProvider2 = Mockito.spy(bftsmartProvider2); | |||
// | |||
// BftsmartConsensusProvider bftsmartProvider3 = new BftsmartConsensusProvider(); | |||
// BftsmartConsensusProvider mockedBftsmartProvider3 = Mockito.spy(bftsmartProvider3); | |||
// | |||
// doAnswer(new Answer<BftsmartNodeServer>() { | |||
// @Override | |||
// public BftsmartNodeServer answer(InvocationOnMock invocation) throws Throwable { | |||
// BftsmartServerSettings serverSettings =(BftsmartServerSettings) invocation.getArguments()[0]; | |||
// | |||
// MessageHandle messageHandle = new ConsensusMessageDispatcher(); | |||
// // mock spy messageHandle | |||
// MessageHandle mockedMessageHandle = Mockito.spy(messageHandle); | |||
// | |||
// StateMachineReplicate stateMachineReplicate = new LedgerStateManager(); | |||
// | |||
// if(nodeAddress0.equals(serverSettings.getReplicaSettings().getAddress())){ | |||
// doAnswer(new Answer() { | |||
// @Override | |||
// public Object answer(InvocationOnMock invocation) throws Throwable { | |||
// | |||
// if (isTestTurnOn.get()) { | |||
// Random random = new Random(); | |||
// byte[] msg = new byte[4]; | |||
// random.nextBytes(msg); | |||
// invocation.getArguments()[0] = msg; | |||
// } | |||
// return invocation.callRealMethod(); | |||
// } | |||
// }).when(mockedMessageHandle).processOrdered(any(), any(), any(), any()); | |||
// } | |||
// | |||
// return new BftsmartNodeServer(serverSettings, mockedMessageHandle, stateMachineReplicate); | |||
// } | |||
// }).when(mockedBftsmartProvider0).getServerFactory().setupServer(any(), any(), any()); | |||
// | |||
// ConsensusProviders.registerProvider(mockedBftsmartProvider0); | |||
// } | |||
/** | |||
* 检查所有节点之间的账本是否一致; | |||
* | |||
@@ -141,6 +236,83 @@ public class IntegrationTest { | |||
} | |||
} | |||
private static void testConsensusFirstTimeoutSdk(GatewayTestRunner gateway, IntegratedContext context) { | |||
AtomicBoolean isTestTurnOn = new AtomicBoolean(true); | |||
Node node0 = context.getNode(0); | |||
Node node1 = context.getNode(1); | |||
LedgerManager ledgerManagerMock0 = node0.getLedgerManager(); | |||
LedgerManager ledgerManagerMock1 = node1.getLedgerManager(); | |||
doAnswer(new Answer<LedgerRepository>() { | |||
@Override | |||
public LedgerRepository answer(InvocationOnMock invocation) throws Throwable { | |||
if (isTestTurnOn.get()) { | |||
Thread.sleep(5000); | |||
} | |||
return (LedgerRepository)invocation.callRealMethod(); | |||
} | |||
}).when(ledgerManagerMock0).getLedger(any()); | |||
doAnswer(new Answer<LedgerRepository>() { | |||
@Override | |||
public LedgerRepository answer(InvocationOnMock invocation) throws Throwable { | |||
if (isTestTurnOn.get()) { | |||
Thread.sleep(5000); | |||
} | |||
return (LedgerRepository)invocation.callRealMethod(); | |||
} | |||
}).when(ledgerManagerMock1).getLedger(any()); | |||
testSDK(gateway, context); | |||
isTestTurnOn.set(false); | |||
} | |||
private static void testConsensusSecondTimeoutSdk(GatewayTestRunner gateway, IntegratedContext context) { | |||
AtomicBoolean isTestTurnOn = new AtomicBoolean(true); | |||
Node node0 = context.getNode(0); | |||
Node node1 = context.getNode(1); | |||
LedgerManager ledgerManagerMock0 = node0.getLedgerManager(); | |||
LedgerManager ledgerManagerMock1 = node1.getLedgerManager(); | |||
doAnswer(new Answer<LedgerRepository>() { | |||
@Override | |||
public LedgerRepository answer(InvocationOnMock invocation) throws Throwable { | |||
if (isTestTurnOn.get()) { | |||
Thread.sleep(10000); | |||
} | |||
return (LedgerRepository)invocation.callRealMethod(); | |||
} | |||
}).when(ledgerManagerMock0).getLedger(any()); | |||
doAnswer(new Answer<LedgerRepository>() { | |||
@Override | |||
public LedgerRepository answer(InvocationOnMock invocation) throws Throwable { | |||
if (isTestTurnOn.get()) { | |||
Thread.sleep(10000); | |||
} | |||
return (LedgerRepository)invocation.callRealMethod(); | |||
} | |||
}).when(ledgerManagerMock1).getLedger(any()); | |||
testSDK(gateway, context); | |||
isTestTurnOn.set(false); | |||
} | |||
//todo | |||
// private static void testBlockHashInconsistentSdk(GatewayTestRunner gateway, IntegratedContext context) { | |||
// isTestTurnOn.set(true); | |||
// testSDK(gateway, context); | |||
// isTestTurnOn.set(false); | |||
// | |||
// } | |||
private static void testStorageErrorBlockRollbackSdk(GatewayTestRunner gateway, IntegratedContext context) { | |||
((TestDbFactory)context.getNode(0).getStorageDB()).setErrorSetTurnOn(true); | |||
@@ -148,6 +320,17 @@ public class IntegrationTest { | |||
((TestDbFactory)context.getNode(2).getStorageDB()).setErrorSetTurnOn(true); | |||
((TestDbFactory)context.getNode(3).getStorageDB()).setErrorSetTurnOn(true); | |||
testSDK(gateway, context); | |||
((TestDbFactory)context.getNode(0).getStorageDB()).setErrorSetTurnOn(false); | |||
((TestDbFactory)context.getNode(1).getStorageDB()).setErrorSetTurnOn(false); | |||
((TestDbFactory)context.getNode(2).getStorageDB()).setErrorSetTurnOn(false); | |||
((TestDbFactory)context.getNode(3).getStorageDB()).setErrorSetTurnOn(false); | |||
} | |||
private static void testTransactionRollbackSdk(GatewayTestRunner gateway, IntegratedContext context) { | |||
// 连接网关; | |||
GatewayServiceFactory gwsrvFact = GatewayServiceFactory.connect(gateway.getServiceAddress()); | |||
BlockchainService bcsrv = gwsrvFact.getBlockchainService(); | |||
@@ -156,12 +339,74 @@ public class IntegrationTest { | |||
AsymmetricKeypair adminKey = context.getNode(0).getPartiKeyPair(); | |||
BlockchainKeypair newUserAcount = testSDK_RegisterUser(adminKey, ledgerHashs[0], bcsrv, context); | |||
// 注册用户,并验证最终写入; | |||
BlockchainKeypair dataAccount = BlockchainKeyGenerator.getInstance().generate(); | |||
// 定义交易; | |||
TransactionTemplate txTpl = bcsrv.newTransaction(ledgerHashs[0]); | |||
txTpl.dataAccounts().register(dataAccount.getIdentity()); | |||
String dataKey = "jd_code"; | |||
String dataVal = "www.jd.com"; | |||
// Construct error kv version | |||
txTpl.dataAccount(dataAccount.getAddress()).setText(dataKey, dataVal, 1); | |||
PreparedTransaction prepTx = txTpl.prepare(); | |||
prepTx.sign(adminKey); | |||
// 提交并等待共识返回; | |||
TransactionResponse txResp = prepTx.commit(); | |||
// 验证结果; | |||
Node node0 = context.getNode(0); | |||
LedgerManage ledgerManager = new LedgerManager(); | |||
KVStorageService storageService = node0.getStorageDB().connect(memDbConnString).getStorageService(); | |||
LedgerQuery ledgerOfNode0 = ledgerManager.register(ledgerHashs[0], storageService); | |||
((TestDbFactory)context.getNode(0).getStorageDB()).setErrorSetTurnOn(false); | |||
((TestDbFactory)context.getNode(1).getStorageDB()).setErrorSetTurnOn(false); | |||
((TestDbFactory)context.getNode(2).getStorageDB()).setErrorSetTurnOn(false); | |||
((TestDbFactory)context.getNode(3).getStorageDB()).setErrorSetTurnOn(false); | |||
} | |||
private static void testInvalidUserSignerSdk(GatewayTestRunner gateway, IntegratedContext context) { | |||
// 连接网关; | |||
GatewayServiceFactory gwsrvFact = GatewayServiceFactory.connect(gateway.getServiceAddress()); | |||
BlockchainService bcsrv = gwsrvFact.getBlockchainService(); | |||
HashDigest[] ledgerHashs = bcsrv.getLedgerHashs(); | |||
//Invalid signer | |||
PrivKey privKey = KeyGenUtils.decodePrivKeyWithRawPassword(PRIV_KEYS, PASSWORD); | |||
PubKey pubKey = KeyGenUtils.decodePubKey(PUB_KEYS); | |||
AsymmetricKeypair asymmetricKeypair = new AsymmetricKeypair(pubKey, privKey); | |||
// 注册用户,并验证最终写入; | |||
BlockchainKeypair user = BlockchainKeyGenerator.getInstance().generate(); | |||
// 定义交易; | |||
TransactionTemplate txTpl = bcsrv.newTransaction(ledgerHashs[0]); | |||
txTpl.users().register(user.getIdentity()); | |||
// 签名; | |||
PreparedTransaction ptx = txTpl.prepare(); | |||
HashDigest transactionHash = ptx.getHash(); | |||
ptx.sign(asymmetricKeypair); | |||
// 提交并等待共识返回; | |||
TransactionResponse txResp = ptx.commit(); | |||
// 验证结果; | |||
Node node0 = context.getNode(0); | |||
LedgerManage ledgerManager = new LedgerManager(); | |||
KVStorageService storageService = node0.getStorageDB().connect(memDbConnString).getStorageService(); | |||
LedgerQuery ledgerOfNode0 = ledgerManager.register(ledgerHashs[0], storageService); | |||
} | |||
@@ -512,6 +757,8 @@ public class IntegrationTest { | |||
IntegratedContext context = new IntegratedContext(); | |||
context.setLedgerHash(ledgerHash0); | |||
Node node0 = new Node(0); | |||
node0.setConsensusSettings(csProps); | |||
node0.setLedgerManager(nodeCtx0.getLedgerManager()); | |||
@@ -1,5 +1,6 @@ | |||
package test.com.jd.blockchain.intgr; | |||
import com.jd.blockchain.ledger.core.LedgerManager; | |||
import com.jd.blockchain.peer.PeerServerBooter; | |||
import com.jd.blockchain.storage.service.DbConnectionFactory; | |||
import com.jd.blockchain.tools.initializer.LedgerBindingConfig; | |||
@@ -28,18 +29,18 @@ public class PeerTestRunner { | |||
} | |||
public PeerTestRunner(NetworkAddress serviceAddress, LedgerBindingConfig ledgerBindingConfig) { | |||
this(serviceAddress, ledgerBindingConfig, null); | |||
this(serviceAddress, ledgerBindingConfig, null, null); | |||
} | |||
public PeerTestRunner(NetworkAddress serviceAddress, LedgerBindingConfig ledgerBindingConfig, | |||
DbConnectionFactory dbConnectionFactory) { | |||
DbConnectionFactory dbConnectionFactory, LedgerManager ledgerManager) { | |||
this.serviceAddress = serviceAddress; | |||
this.ledgerBindingConfig = ledgerBindingConfig; | |||
if (dbConnectionFactory == null) { | |||
this.peerServer = new PeerServerBooter(ledgerBindingConfig, serviceAddress.getHost(), serviceAddress.getPort(),null); | |||
}else { | |||
this.peerServer = new PeerServerBooter(ledgerBindingConfig, serviceAddress.getHost(), serviceAddress.getPort(),null, | |||
dbConnectionFactory); | |||
dbConnectionFactory, ledgerManager); | |||
} | |||
} | |||
@@ -69,16 +69,16 @@ public class ConsensusTest { | |||
Node node3 = context.getNode(3); | |||
NetworkAddress peerSrvAddr0 = new NetworkAddress("127.0.0.1", 10200); | |||
PeerTestRunner peer0 = new PeerTestRunner(peerSrvAddr0, node0.getBindingConfig(), node0.getStorageDB()); | |||
PeerTestRunner peer0 = new PeerTestRunner(peerSrvAddr0, node0.getBindingConfig(), node0.getStorageDB(), null); | |||
NetworkAddress peerSrvAddr1 = new NetworkAddress("127.0.0.1", 10210); | |||
PeerTestRunner peer1 = new PeerTestRunner(peerSrvAddr1, node1.getBindingConfig(), node1.getStorageDB()); | |||
PeerTestRunner peer1 = new PeerTestRunner(peerSrvAddr1, node1.getBindingConfig(), node1.getStorageDB(), null); | |||
NetworkAddress peerSrvAddr2 = new NetworkAddress("127.0.0.1", 10220); | |||
PeerTestRunner peer2 = new PeerTestRunner(peerSrvAddr2, node2.getBindingConfig(), node2.getStorageDB()); | |||
PeerTestRunner peer2 = new PeerTestRunner(peerSrvAddr2, node2.getBindingConfig(), node2.getStorageDB(), null); | |||
NetworkAddress peerSrvAddr3 = new NetworkAddress("127.0.0.1", 10230); | |||
PeerTestRunner peer3 = new PeerTestRunner(peerSrvAddr3, node3.getBindingConfig(), node3.getStorageDB()); | |||
PeerTestRunner peer3 = new PeerTestRunner(peerSrvAddr3, node3.getBindingConfig(), node3.getStorageDB(), null); | |||
AsyncCallback<Object> peerStarting0 = peer0.start(); | |||
AsyncCallback<Object> peerStarting1 = peer1.start(); | |||
@@ -67,16 +67,16 @@ public class GlobalPerformanceTest { | |||
Node node3 = context.getNode(3); | |||
NetworkAddress peerSrvAddr0 = new NetworkAddress("127.0.0.1", 10200); | |||
PeerTestRunner peer0 = new PeerTestRunner(peerSrvAddr0, node0.getBindingConfig(), node0.getStorageDB()); | |||
PeerTestRunner peer0 = new PeerTestRunner(peerSrvAddr0, node0.getBindingConfig(), node0.getStorageDB(), null); | |||
NetworkAddress peerSrvAddr1 = new NetworkAddress("127.0.0.1", 10210); | |||
PeerTestRunner peer1 = new PeerTestRunner(peerSrvAddr1, node1.getBindingConfig(), node1.getStorageDB()); | |||
PeerTestRunner peer1 = new PeerTestRunner(peerSrvAddr1, node1.getBindingConfig(), node1.getStorageDB(), null); | |||
NetworkAddress peerSrvAddr2 = new NetworkAddress("127.0.0.1", 10220); | |||
PeerTestRunner peer2 = new PeerTestRunner(peerSrvAddr2, node2.getBindingConfig(), node2.getStorageDB()); | |||
PeerTestRunner peer2 = new PeerTestRunner(peerSrvAddr2, node2.getBindingConfig(), node2.getStorageDB(), null); | |||
NetworkAddress peerSrvAddr3 = new NetworkAddress("127.0.0.1", 10230); | |||
PeerTestRunner peer3 = new PeerTestRunner(peerSrvAddr3, node3.getBindingConfig(), node3.getStorageDB()); | |||
PeerTestRunner peer3 = new PeerTestRunner(peerSrvAddr3, node3.getBindingConfig(), node3.getStorageDB(), null); | |||
AsyncCallback<Object> peerStarting0 = peer0.start(); | |||
AsyncCallback<Object> peerStarting1 = peer1.start(); | |||
@@ -5,7 +5,11 @@ import java.io.InputStream; | |||
import java.util.Properties; | |||
import java.util.concurrent.CountDownLatch; | |||
import com.jd.blockchain.ledger.core.*; | |||
import com.jd.blockchain.storage.service.DbConnectionFactory; | |||
import org.mockito.Mockito; | |||
import org.mockito.invocation.InvocationOnMock; | |||
import org.mockito.stubbing.Answer; | |||
import org.springframework.boot.SpringApplication; | |||
import org.springframework.context.ConfigurableApplicationContext; | |||
import org.springframework.core.io.ClassPathResource; | |||
@@ -28,12 +32,6 @@ import com.jd.blockchain.ledger.LedgerInitProperties; | |||
import com.jd.blockchain.ledger.Operation; | |||
import com.jd.blockchain.ledger.TransactionContent; | |||
import com.jd.blockchain.ledger.UserRegisterOperation; | |||
import com.jd.blockchain.ledger.core.LedgerInitDecision; | |||
import com.jd.blockchain.ledger.core.LedgerInitProposal; | |||
import com.jd.blockchain.ledger.core.LedgerManager; | |||
import com.jd.blockchain.ledger.core.LedgerQuery; | |||
import com.jd.blockchain.ledger.core.UserAccount; | |||
import com.jd.blockchain.ledger.core.UserAccountQuery; | |||
import com.jd.blockchain.storage.service.DbConnection; | |||
import com.jd.blockchain.storage.service.impl.composite.CompositeConnectionFactory; | |||
//import com.jd.blockchain.storage.service.utils.MemoryBasedDb; | |||
@@ -341,7 +339,7 @@ public class LedgerInitializeWebTest { | |||
throw new IllegalStateException(e.getMessage(), e); | |||
} | |||
} | |||
public static Properties loadConsensusSetting(String configPath) { | |||
ClassPathResource ledgerInitSettingResource = new ClassPathResource(configPath); | |||
try (InputStream in = ledgerInitSettingResource.getInputStream()) { | |||
@@ -403,9 +401,14 @@ public class LedgerInitializeWebTest { | |||
// dbConnConfig.getPassword()); | |||
DbConnection conn = db.connect(dbConnConfig.getUri()); | |||
LedgerQuery ledgerRepo = ledgerManager.register(ledgerHash, conn.getStorageService()); | |||
return ledgerRepo; | |||
} | |||
public LedgerRepository ledgerRepository(HashDigest ledgerHash) { | |||
return ledgerManager.getLedger(ledgerHash); | |||
} | |||
public SignatureDigest createPermissionRequestSignature(int requesterId, PrivKey privKey) { | |||
return controller.signPermissionRequest(requesterId, privKey); | |||
} | |||
@@ -446,7 +449,11 @@ public class LedgerInitializeWebTest { | |||
LedgerInitCommand initCmd = new LedgerInitCommand(); | |||
HashDigest ledgerHash = initCmd.startInit(id, privKey, base58Pwd, ledgerSetting, dbConnConfig, | |||
prompter, conf, db); | |||
NodeWebContext.this.ledgerManager = initCmd.getLedgerManager(); | |||
LedgerManager lm = initCmd.getLedgerManager(); | |||
NodeWebContext.this.ledgerManager = lm; | |||
quitLatch.countDown(); | |||
return ledgerHash; | |||
} | |||
@@ -14,58 +14,36 @@ | |||
# limitations under the License. | |||
############################################ | |||
###### Consensus Commit Block Parameters: transaction count ###### | |||
############################################ | |||
system.block.txsize=15 | |||
############################################ | |||
###### Consensus Commit Block Parameters: delay time ###### | |||
############################################ | |||
system.block.maxdelay=500 | |||
############################################ | |||
###### Consensus Participant0 ###### | |||
############################################ | |||
system.server.0.pubkey=3snPdw7i7PapsDoW185c3kfK6p8s6SwiJAdEUzgnfeuUox12nxgzXu | |||
system.server.0.network.host=127.0.0.1 | |||
system.server.0.network.port=8910 | |||
system.server.0.network.secure=false | |||
############################################ | |||
###### #Consensus Participant1 ###### | |||
############################################ | |||
system.server.1.pubkey=3snPdw7i7Ph1SYLQt9uqVEqiuvNXjxCdGvEdN6otJsg5rbr7Aze7kf | |||
system.server.1.network.host=127.0.0.1 | |||
system.server.1.network.port=8920 | |||
system.server.1.network.secure=false | |||
############################################ | |||
###### #Consensus Participant2 ###### | |||
############################################ | |||
system.server.2.pubkey=3snPdw7i7PezptA6dNBkotPjmKEbTkY8fmusLBnfj8Cf7eFwhWDwKr | |||
system.server.2.network.host=127.0.0.1 | |||
system.server.2.network.port=8930 | |||
system.server.2.network.secure=false | |||
############################################ | |||
###### Consensus Participant3 ###### | |||
############################################ | |||
system.server.3.pubkey=3snPdw7i7PerZYfRzEB61SAN9tFK4yHm9wUSRtkLSSGXHkQRbB5PkS | |||
system.server.3.network.host=127.0.0.1 | |||
system.server.3.network.port=8940 | |||
system.server.3.network.secure=false | |||
############################################ | |||
@@ -94,7 +72,7 @@ system.servers.num = 4 | |||
#system.servers.f = 1 | |||
#Timeout to asking for a client request | |||
system.totalordermulticast.timeout = 2000 | |||
system.totalordermulticast.timeout = 5000 | |||
#Maximum batch size (in number of messages) | |||
@@ -174,5 +152,4 @@ system.ttp.id = 7002 | |||
system.bft = true | |||
#Custom View Storage; | |||
#view.storage.handler=bftsmart.reconfiguration.views.DefaultViewStorage | |||
#view.storage.handler=bftsmart.reconfiguration.views.DefaultViewStorage |
@@ -47,16 +47,16 @@ public class IntegrationBaseTest { | |||
Node node3 = context.getNode(3); | |||
NetworkAddress peerSrvAddr0 = new NetworkAddress("127.0.0.1", 13200); | |||
PeerTestRunner peer0 = new PeerTestRunner(peerSrvAddr0, node0.getBindingConfig(), node0.getStorageDB()); | |||
PeerTestRunner peer0 = new PeerTestRunner(peerSrvAddr0, node0.getBindingConfig(), node0.getStorageDB(), null); | |||
NetworkAddress peerSrvAddr1 = new NetworkAddress("127.0.0.1", 13210); | |||
PeerTestRunner peer1 = new PeerTestRunner(peerSrvAddr1, node1.getBindingConfig(), node1.getStorageDB()); | |||
PeerTestRunner peer1 = new PeerTestRunner(peerSrvAddr1, node1.getBindingConfig(), node1.getStorageDB(), null); | |||
NetworkAddress peerSrvAddr2 = new NetworkAddress("127.0.0.1", 13220); | |||
PeerTestRunner peer2 = new PeerTestRunner(peerSrvAddr2, node2.getBindingConfig(), node2.getStorageDB()); | |||
PeerTestRunner peer2 = new PeerTestRunner(peerSrvAddr2, node2.getBindingConfig(), node2.getStorageDB(), null); | |||
NetworkAddress peerSrvAddr3 = new NetworkAddress("127.0.0.1", 13230); | |||
PeerTestRunner peer3 = new PeerTestRunner(peerSrvAddr3, node3.getBindingConfig(), node3.getStorageDB()); | |||
PeerTestRunner peer3 = new PeerTestRunner(peerSrvAddr3, node3.getBindingConfig(), node3.getStorageDB(), null); | |||
AsyncCallback<Object> peerStarting0 = peer0.start(); | |||
AsyncCallback<Object> peerStarting1 = peer1.start(); | |||
@@ -64,16 +64,16 @@ public class IntegrationTest2 { | |||
Node node3 = context.getNode(3); | |||
NetworkAddress peerSrvAddr0 = new NetworkAddress("127.0.0.1", 13200); | |||
PeerTestRunner peer0 = new PeerTestRunner(peerSrvAddr0, node0.getBindingConfig(), node0.getStorageDB()); | |||
PeerTestRunner peer0 = new PeerTestRunner(peerSrvAddr0, node0.getBindingConfig(), node0.getStorageDB(), null); | |||
NetworkAddress peerSrvAddr1 = new NetworkAddress("127.0.0.1", 13210); | |||
PeerTestRunner peer1 = new PeerTestRunner(peerSrvAddr1, node1.getBindingConfig(), node1.getStorageDB()); | |||
PeerTestRunner peer1 = new PeerTestRunner(peerSrvAddr1, node1.getBindingConfig(), node1.getStorageDB(), null); | |||
NetworkAddress peerSrvAddr2 = new NetworkAddress("127.0.0.1", 13220); | |||
PeerTestRunner peer2 = new PeerTestRunner(peerSrvAddr2, node2.getBindingConfig(), node2.getStorageDB()); | |||
PeerTestRunner peer2 = new PeerTestRunner(peerSrvAddr2, node2.getBindingConfig(), node2.getStorageDB(), null); | |||
NetworkAddress peerSrvAddr3 = new NetworkAddress("127.0.0.1", 13230); | |||
PeerTestRunner peer3 = new PeerTestRunner(peerSrvAddr3, node3.getBindingConfig(), node3.getStorageDB()); | |||
PeerTestRunner peer3 = new PeerTestRunner(peerSrvAddr3, node3.getBindingConfig(), node3.getStorageDB(), null); | |||
AsyncCallback<Object> peerStarting0 = peer0.start(); | |||
AsyncCallback<Object> peerStarting1 = peer1.start(); | |||
@@ -78,16 +78,16 @@ public class IntegrationTestDataAccount { | |||
Node node3 = context.getNode(3); | |||
NetworkAddress peerSrvAddr0 = new NetworkAddress("127.0.0.1", 14200); | |||
PeerTestRunner peer0 = new PeerTestRunner(peerSrvAddr0, node0.getBindingConfig(), node0.getStorageDB()); | |||
PeerTestRunner peer0 = new PeerTestRunner(peerSrvAddr0, node0.getBindingConfig(), node0.getStorageDB(), null); | |||
NetworkAddress peerSrvAddr1 = new NetworkAddress("127.0.0.1", 14210); | |||
PeerTestRunner peer1 = new PeerTestRunner(peerSrvAddr1, node1.getBindingConfig(), node1.getStorageDB()); | |||
PeerTestRunner peer1 = new PeerTestRunner(peerSrvAddr1, node1.getBindingConfig(), node1.getStorageDB(), null); | |||
NetworkAddress peerSrvAddr2 = new NetworkAddress("127.0.0.1", 14220); | |||
PeerTestRunner peer2 = new PeerTestRunner(peerSrvAddr2, node2.getBindingConfig(), node2.getStorageDB()); | |||
PeerTestRunner peer2 = new PeerTestRunner(peerSrvAddr2, node2.getBindingConfig(), node2.getStorageDB(), null); | |||
NetworkAddress peerSrvAddr3 = new NetworkAddress("127.0.0.1", 14230); | |||
PeerTestRunner peer3 = new PeerTestRunner(peerSrvAddr3, node3.getBindingConfig(), node3.getStorageDB()); | |||
PeerTestRunner peer3 = new PeerTestRunner(peerSrvAddr3, node3.getBindingConfig(), node3.getStorageDB(), null); | |||
AsyncCallback<Object> peerStarting0 = peer0.start(); | |||
AsyncCallback<Object> peerStarting1 = peer1.start(); | |||