From d88feabb77efe853efe2c972faf3263bebe44921 Mon Sep 17 00:00:00 2001 From: zhangshuang Date: Tue, 3 Sep 2019 16:39:40 +0800 Subject: [PATCH] modify participant state update bug --- .../handles/ParticipantRegisterOperationHandle.java | 12 ++++++------ .../handles/ParticipantStateUpdateOperationHandle.java | 18 ++++++++++++++++++ .../blockchain/ledger/ParticipantStateUpdateInfo.java | 12 +++++++++++- .../ledger/ParticipantStateUpdateInfoData.java | 14 +++++++++++++- .../SDK_GateWay_Participant_State_Update_Test_.java | 4 +++- .../test/com/jd/blockchain/intgr/IntegrationBase.java | 4 +++- 6 files changed, 54 insertions(+), 10 deletions(-) diff --git a/source/ledger/ledger-core/src/main/java/com/jd/blockchain/ledger/core/impl/handles/ParticipantRegisterOperationHandle.java b/source/ledger/ledger-core/src/main/java/com/jd/blockchain/ledger/core/impl/handles/ParticipantRegisterOperationHandle.java index 255a8eb9..d0e47e52 100644 --- a/source/ledger/ledger-core/src/main/java/com/jd/blockchain/ledger/core/impl/handles/ParticipantRegisterOperationHandle.java +++ b/source/ledger/ledger-core/src/main/java/com/jd/blockchain/ledger/core/impl/handles/ParticipantRegisterOperationHandle.java @@ -20,11 +20,11 @@ public class ParticipantRegisterOperationHandle implements OperationHandle { ParticipantInfo participantInfo = participantRegOp.getParticipantInfo(); - ConsensusProvider provider = ConsensusProviders.getProvider(adminAccount.getSetting().getConsensusProvider()); +// ConsensusProvider provider = ConsensusProviders.getProvider(adminAccount.getSetting().getConsensusProvider()); ParticipantNode participantNode = new PartNode((int)(adminAccount.getParticipantCount()), participantInfo.getName(), participantInfo.getPubKey(), ParticipantNodeState.REGISTED); - LedgerAdminAccount.LedgerMetadataImpl metadata = (LedgerAdminAccount.LedgerMetadataImpl) adminAccount.getMetadata(); +// LedgerAdminAccount.LedgerMetadataImpl metadata = (LedgerAdminAccount.LedgerMetadataImpl) adminAccount.getMetadata(); PubKey pubKey = participantNode.getPubKey(); @@ -32,12 +32,12 @@ public class ParticipantRegisterOperationHandle implements OperationHandle { BlockchainIdentityData identityData = new BlockchainIdentityData(pubKey); //update consensus setting - Bytes newConsensusSettings = provider.getSettingsFactory().getConsensusSettingsBuilder().updateSettings(metadata.getSetting().getConsensusSetting(), participantInfo); +// Bytes newConsensusSettings = provider.getSettingsFactory().getConsensusSettingsBuilder().updateSettings(metadata.getSetting().getConsensusSetting(), participantInfo); - LedgerSetting ledgerSetting = new LedgerConfiguration(adminAccount.getSetting().getConsensusProvider(), - newConsensusSettings, metadata.getSetting().getCryptoSetting()); +// LedgerSetting ledgerSetting = new LedgerConfiguration(adminAccount.getSetting().getConsensusProvider(), +// newConsensusSettings, metadata.getSetting().getCryptoSetting()); - metadata.setSetting(ledgerSetting); +// metadata.setSetting(ledgerSetting); // metadata.setViewId(metadata.getViewId() + 1); //reg participant as user diff --git a/source/ledger/ledger-core/src/main/java/com/jd/blockchain/ledger/core/impl/handles/ParticipantStateUpdateOperationHandle.java b/source/ledger/ledger-core/src/main/java/com/jd/blockchain/ledger/core/impl/handles/ParticipantStateUpdateOperationHandle.java index bb0d5b65..0976651f 100644 --- a/source/ledger/ledger-core/src/main/java/com/jd/blockchain/ledger/core/impl/handles/ParticipantStateUpdateOperationHandle.java +++ b/source/ledger/ledger-core/src/main/java/com/jd/blockchain/ledger/core/impl/handles/ParticipantStateUpdateOperationHandle.java @@ -1,10 +1,13 @@ package com.jd.blockchain.ledger.core.impl.handles; +import com.jd.blockchain.consensus.ConsensusProvider; +import com.jd.blockchain.consensus.ConsensusProviders; import com.jd.blockchain.crypto.AddressEncoding; import com.jd.blockchain.crypto.PubKey; import com.jd.blockchain.ledger.*; import com.jd.blockchain.ledger.core.*; import com.jd.blockchain.ledger.core.impl.OperationHandleContext; +import com.jd.blockchain.utils.Bytes; public class ParticipantStateUpdateOperationHandle implements OperationHandle { @@ -20,6 +23,10 @@ public class ParticipantStateUpdateOperationHandle implements OperationHandle { LedgerAdminAccount adminAccount = newBlockDataset.getAdminAccount(); + ConsensusProvider provider = ConsensusProviders.getProvider(adminAccount.getSetting().getConsensusProvider()); + + LedgerAdminAccount.LedgerMetadataImpl metadata = (LedgerAdminAccount.LedgerMetadataImpl) adminAccount.getMetadata(); + ParticipantNode[] participants = adminAccount.getParticipants(); ParticipantNode participantNode = null; @@ -27,9 +34,20 @@ public class ParticipantStateUpdateOperationHandle implements OperationHandle { for(int i = 0; i < participants.length; i++) { if (stateUpdateOperation.getStateUpdateInfo().getPubKey().equals(participants[i].getPubKey())) { participantNode = new PartNode(participants[i].getId(), participants[i].getName(), participants[i].getPubKey(), ParticipantNodeState.CONSENSUSED); + break; } } + //update consensus setting + ParticipantInfo participantInfo = new ParticipantInfoData("", participantNode.getName(), participantNode.getPubKey(), stateUpdateOperation.getStateUpdateInfo().getNetworkAddress()); + + Bytes newConsensusSettings = provider.getSettingsFactory().getConsensusSettingsBuilder().updateSettings(metadata.getSetting().getConsensusSetting(), participantInfo); + + LedgerSetting ledgerSetting = new LedgerConfiguration(adminAccount.getSetting().getConsensusProvider(), + newConsensusSettings, metadata.getSetting().getCryptoSetting()); + + metadata.setSetting(ledgerSetting); + adminAccount.updateParticipant(participantNode); return null; diff --git a/source/ledger/ledger-model/src/main/java/com/jd/blockchain/ledger/ParticipantStateUpdateInfo.java b/source/ledger/ledger-model/src/main/java/com/jd/blockchain/ledger/ParticipantStateUpdateInfo.java index 8452ac92..70c89f3f 100644 --- a/source/ledger/ledger-model/src/main/java/com/jd/blockchain/ledger/ParticipantStateUpdateInfo.java +++ b/source/ledger/ledger-model/src/main/java/com/jd/blockchain/ledger/ParticipantStateUpdateInfo.java @@ -5,6 +5,7 @@ import com.jd.blockchain.binaryproto.DataField; import com.jd.blockchain.binaryproto.PrimitiveType; import com.jd.blockchain.consts.DataCodes; import com.jd.blockchain.crypto.PubKey; +import com.jd.blockchain.utils.net.NetworkAddress; /** * 参与方状态更新信息; @@ -13,6 +14,7 @@ import com.jd.blockchain.crypto.PubKey; */ @DataContract(code = DataCodes.METADATA_PARTICIPANT_STATE_INFO) public interface ParticipantStateUpdateInfo { + /** * 公钥; * @@ -22,10 +24,18 @@ public interface ParticipantStateUpdateInfo { PubKey getPubKey(); /** + * 共识协议的网络地址; + * + * @return + */ + @DataField(order = 2, primitiveType = PrimitiveType.BYTES) + NetworkAddress getNetworkAddress(); + + /** * 参与方状态; * * @return */ - @DataField(order = 2, refEnum = true) + @DataField(order = 3, refEnum = true) ParticipantNodeState getState(); } diff --git a/source/ledger/ledger-model/src/main/java/com/jd/blockchain/ledger/ParticipantStateUpdateInfoData.java b/source/ledger/ledger-model/src/main/java/com/jd/blockchain/ledger/ParticipantStateUpdateInfoData.java index 1f4a4fc5..5e11c178 100644 --- a/source/ledger/ledger-model/src/main/java/com/jd/blockchain/ledger/ParticipantStateUpdateInfoData.java +++ b/source/ledger/ledger-model/src/main/java/com/jd/blockchain/ledger/ParticipantStateUpdateInfoData.java @@ -1,14 +1,17 @@ package com.jd.blockchain.ledger; import com.jd.blockchain.crypto.PubKey; +import com.jd.blockchain.utils.net.NetworkAddress; public class ParticipantStateUpdateInfoData implements ParticipantStateUpdateInfo { private PubKey pubKey; private ParticipantNodeState state; + private NetworkAddress networkAddress; - public ParticipantStateUpdateInfoData(PubKey pubKey, ParticipantNodeState state) { + public ParticipantStateUpdateInfoData(PubKey pubKey, ParticipantNodeState state, NetworkAddress networkAddress) { this.pubKey = pubKey; this.state = state; + this.networkAddress = networkAddress; } public void setPubKey(PubKey pubKey) { @@ -20,6 +23,15 @@ public class ParticipantStateUpdateInfoData implements ParticipantStateUpdateInf return pubKey; } + public void setNetworkAddress(NetworkAddress networkAddress) { + this.networkAddress = networkAddress; + } + + @Override + public NetworkAddress getNetworkAddress() { + return networkAddress; + } + public void setState(ParticipantNodeState state) { this.state = state; } diff --git a/source/sdk/sdk-samples/src/test/java/test/com/jd/blockchain/sdk/test/SDK_GateWay_Participant_State_Update_Test_.java b/source/sdk/sdk-samples/src/test/java/test/com/jd/blockchain/sdk/test/SDK_GateWay_Participant_State_Update_Test_.java index f77397c8..ea04ef69 100644 --- a/source/sdk/sdk-samples/src/test/java/test/com/jd/blockchain/sdk/test/SDK_GateWay_Participant_State_Update_Test_.java +++ b/source/sdk/sdk-samples/src/test/java/test/com/jd/blockchain/sdk/test/SDK_GateWay_Participant_State_Update_Test_.java @@ -7,6 +7,7 @@ import com.jd.blockchain.sdk.BlockchainService; import com.jd.blockchain.sdk.client.GatewayServiceFactory; import com.jd.blockchain.sdk.samples.SDKDemo_Constant; import com.jd.blockchain.tools.keygen.KeyGenCommand; +import com.jd.blockchain.utils.net.NetworkAddress; import org.junit.Before; import org.junit.Test; @@ -76,8 +77,9 @@ public class SDK_GateWay_Participant_State_Update_Test_ { System.out.println("Address = "+AddressEncoding.generateAddress(pubKey)); + NetworkAddress networkAddress = new NetworkAddress("127.0.0.1", 20000); - ParticipantStateUpdateInfo stateUpdateInfo = new ParticipantStateUpdateInfoData(pubKey, ParticipantNodeState.CONSENSUSED); + ParticipantStateUpdateInfo stateUpdateInfo = new ParticipantStateUpdateInfoData(pubKey, ParticipantNodeState.CONSENSUSED, networkAddress); txTemp.states().update(stateUpdateInfo); // TX 准备就绪; diff --git a/source/test/test-integration/src/test/java/test/com/jd/blockchain/intgr/IntegrationBase.java b/source/test/test-integration/src/test/java/test/com/jd/blockchain/intgr/IntegrationBase.java index 32c02b75..d5d8f3af 100644 --- a/source/test/test-integration/src/test/java/test/com/jd/blockchain/intgr/IntegrationBase.java +++ b/source/test/test-integration/src/test/java/test/com/jd/blockchain/intgr/IntegrationBase.java @@ -201,7 +201,9 @@ public class IntegrationBase { // 定义交易; TransactionTemplate txTpl = blockchainService.newTransaction(ledgerHash); - ParticipantStateUpdateInfo stateUpdateInfo = new ParticipantStateUpdateInfoData(participantKeyPair.getPubKey(), ParticipantNodeState.CONSENSUSED); + ParticipantInfoData participantInfoData = new ParticipantInfoData("add", "peer4", participantKeyPair.getPubKey(), new NetworkAddress("127.0.0.1", 20000)); + + ParticipantStateUpdateInfo stateUpdateInfo = new ParticipantStateUpdateInfoData(participantKeyPair.getPubKey(), ParticipantNodeState.CONSENSUSED, participantInfoData.getNetworkAddress()); txTpl.states().update(stateUpdateInfo);