@@ -1,25 +0,0 @@ | |||||
package com.jd.blockchain.binaryproto; | |||||
import java.lang.annotation.ElementType; | |||||
import java.lang.annotation.Retention; | |||||
import java.lang.annotation.RetentionPolicy; | |||||
import java.lang.annotation.Target; | |||||
/** | |||||
* Created by zhangshuang3 on 2018/7/19. | |||||
*/ | |||||
@Target({ ElementType.METHOD, ElementType.CONSTRUCTOR }) | |||||
@Retention(RetentionPolicy.RUNTIME) | |||||
public @interface DConstructor { | |||||
/** | |||||
* 名称; | |||||
* <p> | |||||
* 默认为属性的名称; | |||||
* | |||||
* @return | |||||
*/ | |||||
String name() default ""; | |||||
} | |||||
@@ -1,25 +0,0 @@ | |||||
package com.jd.blockchain.binaryproto; | |||||
import java.lang.annotation.ElementType; | |||||
import java.lang.annotation.Retention; | |||||
import java.lang.annotation.RetentionPolicy; | |||||
import java.lang.annotation.Target; | |||||
/** | |||||
* Created by zhangshuang3 on 2018/7/19. | |||||
*/ | |||||
@Target({ ElementType.PARAMETER, ElementType.FIELD }) | |||||
@Retention(RetentionPolicy.RUNTIME) | |||||
public @interface FieldSetter { | |||||
/** | |||||
* 名称; | |||||
* <p> | |||||
* 默认为属性的名称; | |||||
* | |||||
* @return | |||||
*/ | |||||
String name() default ""; | |||||
String type() default ""; | |||||
} |
@@ -4,15 +4,12 @@ import java.util.HashMap; | |||||
import java.util.Map; | import java.util.Map; | ||||
import com.jd.blockchain.binaryproto.BinaryEncodingUtils; | import com.jd.blockchain.binaryproto.BinaryEncodingUtils; | ||||
import com.jd.blockchain.binaryproto.DConstructor; | |||||
import com.jd.blockchain.binaryproto.DataContractRegistry; | import com.jd.blockchain.binaryproto.DataContractRegistry; | ||||
import com.jd.blockchain.binaryproto.FieldSetter; | |||||
import com.jd.blockchain.crypto.AddressEncoding; | import com.jd.blockchain.crypto.AddressEncoding; | ||||
import com.jd.blockchain.crypto.HashDigest; | import com.jd.blockchain.crypto.HashDigest; | ||||
import com.jd.blockchain.crypto.PubKey; | import com.jd.blockchain.crypto.PubKey; | ||||
import com.jd.blockchain.ledger.AccountHeader; | import com.jd.blockchain.ledger.AccountHeader; | ||||
import com.jd.blockchain.ledger.CryptoSetting; | import com.jd.blockchain.ledger.CryptoSetting; | ||||
import com.jd.blockchain.ledger.LedgerTransaction; | |||||
import com.jd.blockchain.storage.service.ExPolicyKVStorage; | import com.jd.blockchain.storage.service.ExPolicyKVStorage; | ||||
import com.jd.blockchain.storage.service.VersioningKVStorage; | import com.jd.blockchain.storage.service.VersioningKVStorage; | ||||
import com.jd.blockchain.utils.Bytes; | import com.jd.blockchain.utils.Bytes; | ||||
@@ -90,17 +87,19 @@ public class AccountSet implements Transactional, MerkleProvable { | |||||
return accounts; | return accounts; | ||||
} | } | ||||
// private VersioningAccount deserialize(byte[] txBytes) { | |||||
//// return BinaryEncodingUtils.decode(txBytes, null, Account.class); | |||||
// AccountHeaderData accInfo = BinaryEncodingUtils.decode(txBytes); | |||||
//// return new BaseAccount(accInfo.getAddress(), accInfo.getPubKey(), null, cryptoSetting, | |||||
//// baseExStorage, baseVerStorage, true, accessPolicy); | |||||
// return new VersioningAccount(accInfo.getAddress(), accInfo.getPubKey(), accInfo.getRootHash(), cryptoSetting, | |||||
// keyPrefix, baseExStorage, baseVerStorage, true, accessPolicy, accInfo.); | |||||
// } | |||||
// private VersioningAccount deserialize(byte[] txBytes) { | |||||
//// return BinaryEncodingUtils.decode(txBytes, null, Account.class); | |||||
// AccountHeaderData accInfo = BinaryEncodingUtils.decode(txBytes); | |||||
//// return new BaseAccount(accInfo.getAddress(), accInfo.getPubKey(), null, | |||||
// cryptoSetting, | |||||
//// baseExStorage, baseVerStorage, true, accessPolicy); | |||||
// return new VersioningAccount(accInfo.getAddress(), accInfo.getPubKey(), | |||||
// accInfo.getRootHash(), cryptoSetting, | |||||
// keyPrefix, baseExStorage, baseVerStorage, true, accessPolicy, accInfo.); | |||||
// } | |||||
private AccountHeader deserialize(byte[] txBytes) { | private AccountHeader deserialize(byte[] txBytes) { | ||||
return BinaryEncodingUtils.decode(txBytes); | |||||
return BinaryEncodingUtils.decode(txBytes); | |||||
} | } | ||||
/** | /** | ||||
@@ -267,7 +266,8 @@ public class AccountSet implements Transactional, MerkleProvable { | |||||
// accExStorage, accVerStorage); | // accExStorage, accVerStorage); | ||||
String prefix = keyPrefix + address; | String prefix = keyPrefix + address; | ||||
VersioningAccount acc = createInstance(address, pubKey, cryptoSetting, prefix, baseExStorage, baseVerStorage, -1); | |||||
VersioningAccount acc = createInstance(address, pubKey, cryptoSetting, prefix, baseExStorage, baseVerStorage, | |||||
-1); | |||||
latestAccountsCache.put(address, acc); | latestAccountsCache.put(address, acc); | ||||
updated = true; | updated = true; | ||||
@@ -281,9 +281,10 @@ public class AccountSet implements Transactional, MerkleProvable { | |||||
} | } | ||||
} | } | ||||
private VersioningAccount createInstance(Bytes address, PubKey pubKey, CryptoSetting cryptoSetting, String keyPrefix, | |||||
ExPolicyKVStorage exStorage, VersioningKVStorage verStorage, long version) { | |||||
return new VersioningAccount(address, pubKey, cryptoSetting, keyPrefix, exStorage, verStorage, accessPolicy, version); | |||||
private VersioningAccount createInstance(Bytes address, PubKey pubKey, CryptoSetting cryptoSetting, | |||||
String keyPrefix, ExPolicyKVStorage exStorage, VersioningKVStorage verStorage, long version) { | |||||
return new VersioningAccount(address, pubKey, cryptoSetting, keyPrefix, exStorage, verStorage, accessPolicy, | |||||
version); | |||||
} | } | ||||
private VersioningAccount deserialize(byte[] bytes, CryptoSetting cryptoSetting, String keyPrefix, | private VersioningAccount deserialize(byte[] bytes, CryptoSetting cryptoSetting, String keyPrefix, | ||||
@@ -351,10 +352,7 @@ public class AccountSet implements Transactional, MerkleProvable { | |||||
private PubKey pubKey; | private PubKey pubKey; | ||||
private HashDigest rootHash; | private HashDigest rootHash; | ||||
@DConstructor(name = "AccountHeaderData") | |||||
public AccountHeaderData(@FieldSetter(name = "getAddress", type = "String") Bytes address, | |||||
@FieldSetter(name = "getPubKey", type = "PubKey") PubKey pubKey, | |||||
@FieldSetter(name = "getRootHash", type = "HashDigest") HashDigest rootHash) { | |||||
public AccountHeaderData(Bytes address, PubKey pubKey, HashDigest rootHash) { | |||||
this.address = address; | this.address = address; | ||||
this.pubKey = pubKey; | this.pubKey = pubKey; | ||||
this.rootHash = rootHash; | this.rootHash = rootHash; | ||||
@@ -1,8 +1,6 @@ | |||||
package com.jd.blockchain.ledger.core.impl; | package com.jd.blockchain.ledger.core.impl; | ||||
import com.jd.blockchain.binaryproto.DConstructor; | |||||
import com.jd.blockchain.binaryproto.DataContractRegistry; | import com.jd.blockchain.binaryproto.DataContractRegistry; | ||||
import com.jd.blockchain.binaryproto.FieldSetter; | |||||
import com.jd.blockchain.crypto.HashDigest; | import com.jd.blockchain.crypto.HashDigest; | ||||
import com.jd.blockchain.ledger.LedgerBlock; | import com.jd.blockchain.ledger.LedgerBlock; | ||||
@@ -83,10 +81,7 @@ public class LedgerBlockData implements LedgerBlock { | |||||
this.transactionSetHash = transactionSetHash; | this.transactionSetHash = transactionSetHash; | ||||
} | } | ||||
@DConstructor(name = "LedgerBlockData") | |||||
public LedgerBlockData(@FieldSetter(name = "getHeight", type = "long") long height, | |||||
@FieldSetter(name = "getLedgerHash", type = "HashDigest") HashDigest ledgerHash, | |||||
@FieldSetter(name = "getPreviousHash", type = "HashDigest") HashDigest previousHash) { | |||||
public LedgerBlockData(long height, HashDigest ledgerHash, HashDigest previousHash) { | |||||
this.height = height; | this.height = height; | ||||
this.ledgerHash = ledgerHash; | this.ledgerHash = ledgerHash; | ||||
this.previousHash = previousHash; | this.previousHash = previousHash; | ||||
@@ -11,8 +11,6 @@ import java.io.OutputStream; | |||||
import java.util.ArrayList; | import java.util.ArrayList; | ||||
import java.util.List; | import java.util.List; | ||||
import com.jd.blockchain.binaryproto.DConstructor; | |||||
import com.jd.blockchain.binaryproto.FieldSetter; | |||||
import com.jd.blockchain.crypto.AddressEncoding; | import com.jd.blockchain.crypto.AddressEncoding; | ||||
import com.jd.blockchain.crypto.CryptoAlgorithm; | import com.jd.blockchain.crypto.CryptoAlgorithm; | ||||
import com.jd.blockchain.crypto.PubKey; | import com.jd.blockchain.crypto.PubKey; | ||||
@@ -49,8 +47,7 @@ public class BlockchainIdentityData implements BytesWriter, BytesReader, Externa | |||||
this.address = AddressEncoding.generateAddress(pubKey); | this.address = AddressEncoding.generateAddress(pubKey); | ||||
} | } | ||||
@DConstructor(name = "BlockchainIdentityData") | |||||
public BlockchainIdentityData(@FieldSetter(name = "getAddress", type = "String") Bytes address, @FieldSetter(name = "getPubKey", type = "PubKey") PubKey pubKey) { | |||||
public BlockchainIdentityData(Bytes address, PubKey pubKey) { | |||||
if (!verifyAddress(address, pubKey)) { | if (!verifyAddress(address, pubKey)) { | ||||
throw new IllegalArgumentException("Blockchain address is mismatch with the pub-key!"); | throw new IllegalArgumentException("Blockchain address is mismatch with the pub-key!"); | ||||
} | } | ||||
@@ -78,7 +75,9 @@ public class BlockchainIdentityData implements BytesWriter, BytesReader, Externa | |||||
BytesEncoding.writeInShort(pubKey.toBytes(), out); | BytesEncoding.writeInShort(pubKey.toBytes(), out); | ||||
} | } | ||||
/* (non-Javadoc) | |||||
/* | |||||
* (non-Javadoc) | |||||
* | |||||
* @see com.jd.blockchain.ledger.BlockchainIdentity#getAddress() | * @see com.jd.blockchain.ledger.BlockchainIdentity#getAddress() | ||||
*/ | */ | ||||
@Override | @Override | ||||
@@ -86,7 +85,9 @@ public class BlockchainIdentityData implements BytesWriter, BytesReader, Externa | |||||
return address; | return address; | ||||
} | } | ||||
/* (non-Javadoc) | |||||
/* | |||||
* (non-Javadoc) | |||||
* | |||||
* @see com.jd.blockchain.ledger.BlockchainIdentity#getPubKey() | * @see com.jd.blockchain.ledger.BlockchainIdentity#getPubKey() | ||||
*/ | */ | ||||
@Override | @Override | ||||
@@ -1,8 +1,6 @@ | |||||
package com.jd.blockchain.ledger.data; | package com.jd.blockchain.ledger.data; | ||||
import com.jd.blockchain.binaryproto.DConstructor; | |||||
import com.jd.blockchain.binaryproto.DataContractRegistry; | import com.jd.blockchain.binaryproto.DataContractRegistry; | ||||
import com.jd.blockchain.binaryproto.FieldSetter; | |||||
import com.jd.blockchain.ledger.BlockchainIdentity; | import com.jd.blockchain.ledger.BlockchainIdentity; | ||||
import com.jd.blockchain.ledger.ContractCodeDeployOperation; | import com.jd.blockchain.ledger.ContractCodeDeployOperation; | ||||
import com.jd.blockchain.ledger.DigitalSignature; | import com.jd.blockchain.ledger.DigitalSignature; | ||||
@@ -11,18 +9,17 @@ public class ContractCodeDeployOpTemplate implements ContractCodeDeployOperation | |||||
static { | static { | ||||
DataContractRegistry.register(ContractCodeDeployOperation.class); | DataContractRegistry.register(ContractCodeDeployOperation.class); | ||||
} | } | ||||
private BlockchainIdentity contractID; | private BlockchainIdentity contractID; | ||||
private byte[] chainCode; | private byte[] chainCode; | ||||
public ContractCodeDeployOpTemplate() { | public ContractCodeDeployOpTemplate() { | ||||
} | } | ||||
@DConstructor(name="ContractCodeDeployOpTemplate") | |||||
public ContractCodeDeployOpTemplate(@FieldSetter(name="getContractID", type="BlockchainIdentity") BlockchainIdentity contractID, @FieldSetter(name="getChainCode", type="byte[]") byte[] chainCode) { | |||||
public ContractCodeDeployOpTemplate(BlockchainIdentity contractID, byte[] chainCode) { | |||||
this.contractID = contractID; | this.contractID = contractID; | ||||
this.chainCode= chainCode; | |||||
this.chainCode = chainCode; | |||||
} | } | ||||
@Override | @Override | ||||
@@ -40,6 +37,5 @@ public class ContractCodeDeployOpTemplate implements ContractCodeDeployOperation | |||||
// TODO Auto-generated method stub | // TODO Auto-generated method stub | ||||
return null; | return null; | ||||
} | } | ||||
} | } |
@@ -1,8 +1,6 @@ | |||||
package com.jd.blockchain.ledger.data; | package com.jd.blockchain.ledger.data; | ||||
import com.jd.blockchain.binaryproto.DConstructor; | |||||
import com.jd.blockchain.binaryproto.DataContractRegistry; | import com.jd.blockchain.binaryproto.DataContractRegistry; | ||||
import com.jd.blockchain.binaryproto.FieldSetter; | |||||
import com.jd.blockchain.ledger.ContractEventSendOperation; | import com.jd.blockchain.ledger.ContractEventSendOperation; | ||||
import com.jd.blockchain.utils.Bytes; | import com.jd.blockchain.utils.Bytes; | ||||
@@ -18,15 +16,12 @@ public class ContractEventSendOpTemplate implements ContractEventSendOperation { | |||||
public ContractEventSendOpTemplate() { | public ContractEventSendOpTemplate() { | ||||
} | } | ||||
@DConstructor(name="ContractEventSendOpTemplate") | |||||
public ContractEventSendOpTemplate(@FieldSetter(name="getContractAddress", type="Bytes") Bytes contractAddress, | |||||
@FieldSetter(name="getEvent", type="String") String event, | |||||
@FieldSetter(name="getArgs", type="byte[]") byte[] args) { | |||||
public ContractEventSendOpTemplate(Bytes contractAddress, String event, byte[] args) { | |||||
this.contractAddress = contractAddress; | this.contractAddress = contractAddress; | ||||
this.event = event; | this.event = event; | ||||
this.args = args; | this.args = args; | ||||
} | } | ||||
@Override | @Override | ||||
public Bytes getContractAddress() { | public Bytes getContractAddress() { | ||||
return contractAddress; | return contractAddress; | ||||
@@ -42,5 +37,4 @@ public class ContractEventSendOpTemplate implements ContractEventSendOperation { | |||||
return args; | return args; | ||||
} | } | ||||
} | } |
@@ -3,9 +3,7 @@ package com.jd.blockchain.ledger.data; | |||||
import java.util.LinkedHashMap; | import java.util.LinkedHashMap; | ||||
import java.util.Map; | import java.util.Map; | ||||
import com.jd.blockchain.binaryproto.DConstructor; | |||||
import com.jd.blockchain.binaryproto.DataContractRegistry; | import com.jd.blockchain.binaryproto.DataContractRegistry; | ||||
import com.jd.blockchain.binaryproto.FieldSetter; | |||||
import com.jd.blockchain.ledger.BytesValue; | import com.jd.blockchain.ledger.BytesValue; | ||||
import com.jd.blockchain.ledger.DataAccountKVSetOperation; | import com.jd.blockchain.ledger.DataAccountKVSetOperation; | ||||
import com.jd.blockchain.utils.Bytes; | import com.jd.blockchain.utils.Bytes; | ||||
@@ -14,16 +12,15 @@ public class DataAccountKVSetOpTemplate implements DataAccountKVSetOperation { | |||||
static { | static { | ||||
DataContractRegistry.register(DataAccountKVSetOperation.class); | DataContractRegistry.register(DataAccountKVSetOperation.class); | ||||
} | } | ||||
private Bytes accountAddress; | private Bytes accountAddress; | ||||
private Map<String, KVWriteEntry> kvset = new LinkedHashMap<>(); | private Map<String, KVWriteEntry> kvset = new LinkedHashMap<>(); | ||||
public DataAccountKVSetOpTemplate() { | public DataAccountKVSetOpTemplate() { | ||||
} | } | ||||
@DConstructor(name="DataAccountKVSetOpTemplate") | |||||
public DataAccountKVSetOpTemplate(@FieldSetter(name="getAccountAddress", type="String") Bytes accountAddress) { | |||||
public DataAccountKVSetOpTemplate(Bytes accountAddress) { | |||||
this.accountAddress = accountAddress; | this.accountAddress = accountAddress; | ||||
} | } | ||||
@@ -44,7 +41,7 @@ public class DataAccountKVSetOpTemplate implements DataAccountKVSetOperation { | |||||
public void setWriteSet(Object[] kvEntries) { | public void setWriteSet(Object[] kvEntries) { | ||||
for (Object object : kvEntries) { | for (Object object : kvEntries) { | ||||
KVWriteEntry kvEntry = (KVWriteEntry)object; | |||||
KVWriteEntry kvEntry = (KVWriteEntry) object; | |||||
set(kvEntry.getKey(), kvEntry.getValue(), kvEntry.getExpectedVersion()); | set(kvEntry.getKey(), kvEntry.getValue(), kvEntry.getExpectedVersion()); | ||||
} | } | ||||
return; | return; | ||||
@@ -65,5 +62,4 @@ public class DataAccountKVSetOpTemplate implements DataAccountKVSetOperation { | |||||
kvset.put(kvData.getKey(), kvData); | kvset.put(kvData.getKey(), kvData); | ||||
} | } | ||||
} | } |
@@ -1,8 +1,6 @@ | |||||
package com.jd.blockchain.ledger.data; | package com.jd.blockchain.ledger.data; | ||||
import com.jd.blockchain.binaryproto.DConstructor; | |||||
import com.jd.blockchain.binaryproto.DataContractRegistry; | import com.jd.blockchain.binaryproto.DataContractRegistry; | ||||
import com.jd.blockchain.binaryproto.FieldSetter; | |||||
import com.jd.blockchain.ledger.BlockchainIdentity; | import com.jd.blockchain.ledger.BlockchainIdentity; | ||||
import com.jd.blockchain.ledger.DataAccountKVSetOperation; | import com.jd.blockchain.ledger.DataAccountKVSetOperation; | ||||
import com.jd.blockchain.ledger.DataAccountRegisterOperation; | import com.jd.blockchain.ledger.DataAccountRegisterOperation; | ||||
@@ -18,8 +16,7 @@ public class DataAccountRegisterOpTemplate implements DataAccountRegisterOperati | |||||
public DataAccountRegisterOpTemplate() { | public DataAccountRegisterOpTemplate() { | ||||
} | } | ||||
@DConstructor(name="DataAccountRegisterOpTemplate") | |||||
public DataAccountRegisterOpTemplate(@FieldSetter(name="getAccountID", type="BlockchainIdentity") BlockchainIdentity accountID) { | |||||
public DataAccountRegisterOpTemplate(BlockchainIdentity accountID) { | |||||
this.accountID = accountID; | this.accountID = accountID; | ||||
} | } | ||||
@@ -33,6 +30,5 @@ public class DataAccountRegisterOpTemplate implements DataAccountRegisterOperati | |||||
// TODO Auto-generated method stub | // TODO Auto-generated method stub | ||||
return null; | return null; | ||||
} | } | ||||
} | } |
@@ -1,39 +1,35 @@ | |||||
package com.jd.blockchain.ledger.data; | package com.jd.blockchain.ledger.data; | ||||
import com.jd.blockchain.binaryproto.DConstructor; | |||||
import com.jd.blockchain.binaryproto.FieldSetter; | |||||
import com.jd.blockchain.ledger.BytesValue; | import com.jd.blockchain.ledger.BytesValue; | ||||
import com.jd.blockchain.ledger.DataAccountKVSetOperation.KVWriteEntry; | import com.jd.blockchain.ledger.DataAccountKVSetOperation.KVWriteEntry; | ||||
public class KVData implements KVWriteEntry { | public class KVData implements KVWriteEntry { | ||||
private String key; | |||||
private BytesValue value; | |||||
private long expectedVersion; | |||||
@DConstructor(name="KVData") | |||||
public KVData(@FieldSetter(name="getKey", type="String") String key, @FieldSetter(name="getValue", type="BytesValue") BytesValue value, @FieldSetter(name="getExpectedVersion", type="long")long expectedVersion) { | |||||
this.key = key; | |||||
this.value = value; | |||||
this.expectedVersion = expectedVersion; | |||||
} | |||||
@Override | |||||
public String getKey() { | |||||
return key; | |||||
} | |||||
@Override | |||||
public BytesValue getValue() { | |||||
return value; | |||||
} | |||||
@Override | |||||
public long getExpectedVersion() { | |||||
return expectedVersion; | |||||
} | |||||
} | |||||
private String key; | |||||
private BytesValue value; | |||||
private long expectedVersion; | |||||
public KVData(String key, BytesValue value, long expectedVersion) { | |||||
this.key = key; | |||||
this.value = value; | |||||
this.expectedVersion = expectedVersion; | |||||
} | |||||
@Override | |||||
public String getKey() { | |||||
return key; | |||||
} | |||||
@Override | |||||
public BytesValue getValue() { | |||||
return value; | |||||
} | |||||
@Override | |||||
public long getExpectedVersion() { | |||||
return expectedVersion; | |||||
} | |||||
} |
@@ -1,8 +1,6 @@ | |||||
package com.jd.blockchain.ledger.data; | package com.jd.blockchain.ledger.data; | ||||
import com.jd.blockchain.binaryproto.DConstructor; | |||||
import com.jd.blockchain.binaryproto.DataContractRegistry; | import com.jd.blockchain.binaryproto.DataContractRegistry; | ||||
import com.jd.blockchain.binaryproto.FieldSetter; | |||||
import com.jd.blockchain.ledger.LedgerInitOperation; | import com.jd.blockchain.ledger.LedgerInitOperation; | ||||
import com.jd.blockchain.ledger.LedgerInitSetting; | import com.jd.blockchain.ledger.LedgerInitSetting; | ||||
@@ -10,17 +8,16 @@ public class LedgerInitOpTemplate implements LedgerInitOperation { | |||||
static { | static { | ||||
DataContractRegistry.register(LedgerInitOperation.class); | DataContractRegistry.register(LedgerInitOperation.class); | ||||
} | } | ||||
private LedgerInitSetting initSetting; | private LedgerInitSetting initSetting; | ||||
public LedgerInitOpTemplate() { | public LedgerInitOpTemplate() { | ||||
} | } | ||||
@DConstructor(name="LedgerInitOpTemplate") | |||||
public LedgerInitOpTemplate(@FieldSetter(name="getInitSetting", type="LedgerInitSetting") LedgerInitSetting initSetting) { | |||||
public LedgerInitOpTemplate(LedgerInitSetting initSetting) { | |||||
this.initSetting = initSetting; | this.initSetting = initSetting; | ||||
} | } | ||||
@Override | @Override | ||||
public LedgerInitSetting getInitSetting() { | public LedgerInitSetting getInitSetting() { | ||||
return initSetting; | return initSetting; | ||||
@@ -4,8 +4,6 @@ import java.util.ArrayList; | |||||
import java.util.Collection; | import java.util.Collection; | ||||
import java.util.List; | import java.util.List; | ||||
import com.jd.blockchain.binaryproto.DConstructor; | |||||
import com.jd.blockchain.binaryproto.FieldSetter; | |||||
import com.jd.blockchain.crypto.HashDigest; | import com.jd.blockchain.crypto.HashDigest; | ||||
import com.jd.blockchain.ledger.Operation; | import com.jd.blockchain.ledger.Operation; | ||||
import com.jd.blockchain.ledger.TransactionContent; | import com.jd.blockchain.ledger.TransactionContent; | ||||
@@ -33,8 +31,7 @@ public class TxContentBlob implements TransactionContent { | |||||
private HashDigest ledgerHash; | private HashDigest ledgerHash; | ||||
@DConstructor(name ="TxContentBlob") | |||||
public TxContentBlob(@FieldSetter(name="getLedgerHash", type="HashDigest") HashDigest ledgerHash) { | |||||
public TxContentBlob(HashDigest ledgerHash) { | |||||
this.ledgerHash = ledgerHash; | this.ledgerHash = ledgerHash; | ||||
} | } | ||||
@@ -45,11 +42,12 @@ public class TxContentBlob implements TransactionContent { | |||||
public HashDigest getHash() { | public HashDigest getHash() { | ||||
return this.hash; | return this.hash; | ||||
} | } | ||||
/** | /** | ||||
* 更新交易内容的哈希值; | * 更新交易内容的哈希值; | ||||
* <p> | * <p> | ||||
* 注:当前对象只充当值对象,不校验指定哈希值的完整性,调用者应该在外部实施完整性校验; | * 注:当前对象只充当值对象,不校验指定哈希值的完整性,调用者应该在外部实施完整性校验; | ||||
* | |||||
* @param hash | * @param hash | ||||
*/ | */ | ||||
public void setHash(HashDigest hash) { | public void setHash(HashDigest hash) { | ||||
@@ -65,6 +63,7 @@ public class TxContentBlob implements TransactionContent { | |||||
public HashDigest getLedgerHash() { | public HashDigest getLedgerHash() { | ||||
return ledgerHash; | return ledgerHash; | ||||
} | } | ||||
public void setLedgerHash(HashDigest ledgerHash) { | public void setLedgerHash(HashDigest ledgerHash) { | ||||
this.ledgerHash = ledgerHash; | this.ledgerHash = ledgerHash; | ||||
} | } | ||||
@@ -74,10 +73,10 @@ public class TxContentBlob implements TransactionContent { | |||||
return operationList.toArray(new Operation[operationList.size()]); | return operationList.toArray(new Operation[operationList.size()]); | ||||
} | } | ||||
public void setOperations(Object[] operations) { | |||||
//in array's case ,cast will failed! | |||||
public void setOperations(Object[] operations) { | |||||
// in array's case ,cast will failed! | |||||
for (Object operation : operations) { | for (Object operation : operations) { | ||||
Operation op = (Operation)operation; | |||||
Operation op = (Operation) operation; | |||||
addOperation(op); | addOperation(op); | ||||
} | } | ||||
} | } | ||||
@@ -85,7 +84,7 @@ public class TxContentBlob implements TransactionContent { | |||||
public void addOperation(Operation operation) { | public void addOperation(Operation operation) { | ||||
operationList.add(operation); | operationList.add(operation); | ||||
} | } | ||||
public void addOperations(Collection<Operation> operations) { | public void addOperations(Collection<Operation> operations) { | ||||
operationList.addAll(operations); | operationList.addAll(operations); | ||||
} | } | ||||
@@ -4,12 +4,15 @@ import java.util.LinkedHashMap; | |||||
import java.util.List; | import java.util.List; | ||||
import java.util.Map; | import java.util.Map; | ||||
import com.jd.blockchain.binaryproto.DConstructor; | |||||
import com.jd.blockchain.binaryproto.DataContractRegistry; | import com.jd.blockchain.binaryproto.DataContractRegistry; | ||||
import com.jd.blockchain.binaryproto.FieldSetter; | |||||
import com.jd.blockchain.crypto.AddressEncoding; | import com.jd.blockchain.crypto.AddressEncoding; | ||||
import com.jd.blockchain.crypto.HashDigest; | import com.jd.blockchain.crypto.HashDigest; | ||||
import com.jd.blockchain.ledger.*; | |||||
import com.jd.blockchain.ledger.BlockchainIdentity; | |||||
import com.jd.blockchain.ledger.DigitalSignature; | |||||
import com.jd.blockchain.ledger.MagicNumber; | |||||
import com.jd.blockchain.ledger.NodeRequest; | |||||
import com.jd.blockchain.ledger.TransactionContent; | |||||
import com.jd.blockchain.ledger.TransactionRequest; | |||||
import com.jd.blockchain.utils.Bytes; | import com.jd.blockchain.utils.Bytes; | ||||
/** | /** | ||||
@@ -39,345 +42,344 @@ import com.jd.blockchain.utils.Bytes; | |||||
*/ | */ | ||||
public class TxRequestMessage implements TransactionRequest {// , Externalizable { | public class TxRequestMessage implements TransactionRequest {// , Externalizable { | ||||
/** | |||||
* 交易参与者的个数的最大值; | |||||
*/ | |||||
public static final int MAX_TX_PARTICIPANT_COUNT = 0xFF; | |||||
private HashDigest hash; | |||||
private TransactionContent transactionContent; | |||||
private Map<Bytes, DigitalSignature> endpointSignatureMap = new LinkedHashMap<>(); | |||||
private Map<Bytes, DigitalSignature> nodeSignatureMap = new LinkedHashMap<>(); | |||||
// private CryptoAlgorithm defaultHashAlgorithm = CryptoAlgorithm.SHA_256; | |||||
// public TxRequestMessage() { | |||||
// } | |||||
static { | |||||
DataContractRegistry.register(NodeRequest.class); | |||||
} | |||||
@DConstructor(name = "TxRequestMessage") | |||||
public TxRequestMessage(@FieldSetter(name = "getTransactionContent", type = "TransactionContent ")TransactionContent txContent) { | |||||
// if (!(txContent instanceof BytesWriter)) { | |||||
// throw new IllegalArgumentException("The tx content must be instance of BytesWriter!"); | |||||
// } | |||||
this.transactionContent = txContent; | |||||
} | |||||
public TxRequestMessage(TransactionRequest txRequest) { | |||||
this.transactionContent = txRequest.getTransactionContent(); | |||||
setHash(txRequest.getHash()); | |||||
setEndpointSignatures(txRequest.getEndpointSignatures()); | |||||
setNodeSignatures(txRequest.getNodeSignatures()); | |||||
} | |||||
@Override | |||||
public TransactionContent getTransactionContent() { | |||||
return this.transactionContent; | |||||
} | |||||
@Override | |||||
public DigitalSignature[] getEndpointSignatures() { | |||||
return endpointSignatureMap.values().toArray(new DigitalSignature[endpointSignatureMap.size()]); | |||||
} | |||||
@Override | |||||
public DigitalSignature[] getNodeSignatures() { | |||||
return nodeSignatureMap.values().toArray(new DigitalSignature[nodeSignatureMap.size()]); | |||||
} | |||||
public void setEndpointSignatures(Object[] endpointSignatures) { | |||||
if (endpointSignatures != null) { | |||||
for (Object object : endpointSignatures) { | |||||
DigitalSignature endpointSignature = (DigitalSignature)object; | |||||
addEndpointSignatures(endpointSignature); | |||||
} | |||||
} | |||||
return; | |||||
} | |||||
public void setNodeSignatures(Object[] nodeSignatures) { | |||||
if (nodeSignatures != null) { | |||||
for (Object object : nodeSignatures) { | |||||
DigitalSignature nodeSignature = (DigitalSignature)object; | |||||
addNodeSignatures(nodeSignature); | |||||
} | |||||
} | |||||
return; | |||||
} | |||||
private void doAddEndpointSignature(DigitalSignature signature) { | |||||
Bytes address = AddressEncoding.generateAddress(signature.getPubKey()); | |||||
if (endpointSignatureMap.containsKey(address)) { | |||||
throw new IllegalArgumentException( | |||||
String.format("Participant signature of Address[%s] already exist!", address)); | |||||
} | |||||
endpointSignatureMap.put(address, signature); | |||||
} | |||||
/** | |||||
* 从参与者签名列表中检查是否包含指定的参与者; | |||||
* | |||||
* @param userBid | |||||
* 参与者的身份; | |||||
* @return | |||||
*/ | |||||
public boolean containsEndpointSignature(BlockchainIdentity userBid) { | |||||
return endpointSignatureMap.containsKey(userBid.getAddress()); | |||||
} | |||||
public boolean containsEndpointSignature(Bytes userAddress) { | |||||
return endpointSignatureMap.containsKey(userAddress); | |||||
} | |||||
public void addEndpointSignatures(DigitalSignature... signature) { | |||||
for (DigitalSignature sign : signature) { | |||||
doAddEndpointSignature(sign); | |||||
} | |||||
} | |||||
public void addEndpointSignatures(List<DigitalSignature> signature) { | |||||
for (DigitalSignature sign : signature) { | |||||
doAddEndpointSignature(sign); | |||||
} | |||||
} | |||||
/** | |||||
* 从节点签名列表中检查是否包含指定的节点; | |||||
* | |||||
* @param nodeBid | |||||
* 节点的身份; | |||||
* @return | |||||
*/ | |||||
public boolean containsNodeSignature(BlockchainIdentity nodeBid) { | |||||
return nodeSignatureMap.containsKey(nodeBid.getAddress()); | |||||
} | |||||
public boolean containsNodeSignature(Bytes nodeAddress) { | |||||
return nodeSignatureMap.containsKey(nodeAddress); | |||||
} | |||||
private void doAddNodeSignatures(DigitalSignature signature) { | |||||
Bytes address = AddressEncoding.generateAddress(signature.getPubKey()); | |||||
if (nodeSignatureMap.containsKey(address)) { | |||||
throw new IllegalArgumentException(String.format("Node signature of Address[%s] already exist!", address)); | |||||
} | |||||
nodeSignatureMap.put(address, signature); | |||||
} | |||||
public void addNodeSignatures(DigitalSignature... signature) { | |||||
for (DigitalSignature sign : signature) { | |||||
doAddNodeSignatures(sign); | |||||
} | |||||
} | |||||
public void addNodeSignatures(List<DigitalSignature> signature) { | |||||
for (DigitalSignature sign : signature) { | |||||
doAddNodeSignatures(sign); | |||||
} | |||||
} | |||||
@Override | |||||
public HashDigest getHash() { | |||||
return hash; | |||||
} | |||||
public void setHash(HashDigest hash) { | |||||
this.hash = hash; | |||||
} | |||||
// public HashDigest updateHash() { | |||||
// return computeHash(this.defaultHashAlgorithm); | |||||
// } | |||||
// public HashDigest updateHash(CryptoAlgorithm hashAlgorithm) { | |||||
// return computeHash(hashAlgorithm); | |||||
// } | |||||
// | |||||
// private HashDigest computeHash(CryptoAlgorithm hashAlgorithm) { | |||||
// byte[] reqBody = getRequestBody(); | |||||
// this.hash = CryptoUtils.hash(hashAlgorithm).hash(reqBody); | |||||
// return this.hash; | |||||
// } | |||||
// @Override | |||||
// public void resolvFrom(InputStream in) throws IOException { | |||||
// // 解析校验交易请求魔数; | |||||
// byte[] buff = new byte[1]; | |||||
// int len = in.read(buff, 0, 1); | |||||
// if (len < 1) { | |||||
// throw new IllegalArgumentException("No bytes was read for the magic number | |||||
// [TX_REQUEST]!"); | |||||
// } | |||||
// if (MagicNumber.TX_REQUEST != buff[0]) { | |||||
// throw new IllegalArgumentException("Magic number [TX_REQUEST] dismatch!"); | |||||
// } | |||||
// | |||||
// // 解析交易内容; | |||||
// TxContentBlob txContentBlob = new TxContentBlob(); | |||||
// txContentBlob.resolvFrom(in); | |||||
// | |||||
// // 解析参与者签名列表; | |||||
// int participantCount = NumberMask.TINY.resolveMaskedNumber(in); | |||||
// List<DigitalSignature> partiSignList = new ArrayList<>(); | |||||
// for (int i = 0; i < participantCount; i++) { | |||||
// DigitalSignatureBlob signature = new DigitalSignatureBlob(); | |||||
// signature.resolvFrom(in); | |||||
// | |||||
// partiSignList.add(signature); | |||||
// } | |||||
// | |||||
// // 解析节点签名列表; | |||||
// int nodeCount = NumberMask.TINY.resolveMaskedNumber(in); | |||||
// List<DigitalSignature> nodeSignList = new ArrayList<>(); | |||||
// for (int i = 0; i < nodeCount; i++) { | |||||
// DigitalSignatureBlob nodeSign = new DigitalSignatureBlob(); | |||||
// nodeSign.resolvFrom(in); | |||||
// nodeSignList.add(nodeSign); | |||||
// } | |||||
// | |||||
// // 解析哈希算法标识符; | |||||
// HashAlgorithm hashAlgorithm = HashAlgorithm.valueOf((byte) in.read()); | |||||
// | |||||
// // 解析原始的哈希; | |||||
// ByteArray hash = HashEncoding.read(in); | |||||
// | |||||
// this.txContent = txContentBlob; | |||||
// addParticipantSignatures(partiSignList); | |||||
// addNodeSignatures(nodeSignList); | |||||
// this.hash = hash; | |||||
// | |||||
// // 校验原始哈希; | |||||
// byte[] bodyBytes = getRequestBody(); | |||||
// ByteArray rHash = HashEncoding.computeHash(bodyBytes, hashAlgorithm); | |||||
// if (!rHash.equals(hash)) { | |||||
// throw new IllegalArgumentException("The hash is not match with request | |||||
// content!"); | |||||
// } | |||||
// } | |||||
// | |||||
// /** | |||||
// * 输出交易请求消息; | |||||
// * | |||||
// * 注:此方法不会自动重新计算hash;如果消息的内容发生改变后,需要调用主动调用 {@link #updateHash()} 方法重新计算 hash; | |||||
// */ | |||||
// @Override | |||||
// public void writeTo(OutputStream out) throws IOException { | |||||
// if (this.hash == null) { | |||||
// updateHash(); | |||||
// } | |||||
// | |||||
// buildRequestBody(out); | |||||
// | |||||
// // 写入 hash 值; | |||||
// HashEncoding.write(hash, out); | |||||
// } | |||||
// /** | |||||
// * 生成请求体,包括:交易请求魔数、交易内容、参与者签名者列表、哈希算法代号; | |||||
// * | |||||
// * @param out | |||||
// * @throws IOException | |||||
// */ | |||||
// private void buildRequestBody(OutputStream out) throws IOException { | |||||
// | |||||
// buildParticipantRequest(out); | |||||
// | |||||
// // 写入节点签名列表; | |||||
// NumberMask.TINY.writeMask(nodeSignatureMap.size(), out); | |||||
// for (DigitalSignature nodeSignatureBlob : nodeSignatureMap.values()) { | |||||
// nodeSignatureBlob.writeTo(out); | |||||
// } | |||||
// | |||||
// // 写入 hash 算法代号; | |||||
// out.write(hashAlgorithm.getAlgorithm()); | |||||
// } | |||||
// /** | |||||
// * 生成参与者的请求数据; | |||||
// * | |||||
// * <br> | |||||
// * 参与者的请求数据仅包含“交易请求模数({@link MagicNumber#TX_REQUEST })” | |||||
// * “交易内容({@link #getTransactionContent()})” | |||||
// * 和“参与者签名列表({@link #getParticipantSignatures()})”三项属性; | |||||
// * | |||||
// * @param out | |||||
// */ | |||||
// public void buildParticipantRequest(OutputStream out) { | |||||
// try { | |||||
// // 写入魔数; | |||||
// out.write(MagicNumber.TX_REQUEST); | |||||
// | |||||
// // 写入交易内容; | |||||
// txContent.writeTo(out); | |||||
// | |||||
// // 写入 1 个字节的参与者签名数量; | |||||
// if (participantSignatureMap.size() > MAX_TX_PARTICIPANT_COUNT) { | |||||
// throw new IllegalArgumentException("The number of participant signatures is | |||||
// out of the max count[" | |||||
// + MAX_TX_PARTICIPANT_COUNT + "]!"); | |||||
// } | |||||
// | |||||
// NumberMask.TINY.writeMask(participantSignatureMap.size(), out); | |||||
// // 写入参与者签名列表; | |||||
// for (DigitalSignature digitalSignatureBlob : | |||||
// participantSignatureMap.values()) { | |||||
// digitalSignatureBlob.writeTo(out); | |||||
// } | |||||
// | |||||
// } catch (IOException e) { | |||||
// throw new RuntimeIOException(e.getMessage(), e); | |||||
// } | |||||
// } | |||||
// | |||||
// @Override | |||||
// public void writeExternal(ObjectOutput out) throws IOException { | |||||
// ByteArrayOutputStream os = new ByteArrayOutputStream(); | |||||
// writeTo(os); | |||||
// byte[] bts = os.toByteArray(); | |||||
// out.writeInt(bts.length); | |||||
// out.write(bts); | |||||
// } | |||||
// | |||||
// @Override | |||||
// public void readExternal(ObjectInput in) throws IOException, | |||||
// ClassNotFoundException { | |||||
// int len = in.readInt(); | |||||
// byte[] bts = new byte[len]; | |||||
// in.readFully(bts); | |||||
// this.resolvFrom(new ByteArrayInputStream(bts)); | |||||
// } | |||||
// @Override | |||||
// public byte[] toBytes() { | |||||
// ByteArrayOutputStream out = new ByteArrayOutputStream(); | |||||
// try { | |||||
// writeTo(out); | |||||
// } catch (IOException e) { | |||||
// throw new RuntimeIOException(e.getMessage(), e); | |||||
// } | |||||
// return out.toByteArray(); | |||||
// } | |||||
// @Override | |||||
// public ByteArray getHashData() { | |||||
// return ByteArray.wrap(getRequestBody()); | |||||
// } | |||||
// private byte[] getRequestBody() { | |||||
// try { | |||||
// ByteArrayOutputStream out = new ByteArrayOutputStream(); | |||||
// buildRequestBody(out); | |||||
// | |||||
// return out.toByteArray(); | |||||
// } catch (IOException e) { | |||||
// throw new RuntimeIOException(e.getMessage(), e); | |||||
// } | |||||
// } | |||||
/** | |||||
* 交易参与者的个数的最大值; | |||||
*/ | |||||
public static final int MAX_TX_PARTICIPANT_COUNT = 0xFF; | |||||
private HashDigest hash; | |||||
private TransactionContent transactionContent; | |||||
private Map<Bytes, DigitalSignature> endpointSignatureMap = new LinkedHashMap<>(); | |||||
private Map<Bytes, DigitalSignature> nodeSignatureMap = new LinkedHashMap<>(); | |||||
// private CryptoAlgorithm defaultHashAlgorithm = CryptoAlgorithm.SHA_256; | |||||
// public TxRequestMessage() { | |||||
// } | |||||
static { | |||||
DataContractRegistry.register(NodeRequest.class); | |||||
} | |||||
public TxRequestMessage(TransactionContent txContent) { | |||||
// if (!(txContent instanceof BytesWriter)) { | |||||
// throw new IllegalArgumentException("The tx content must be instance of | |||||
// BytesWriter!"); | |||||
// } | |||||
this.transactionContent = txContent; | |||||
} | |||||
public TxRequestMessage(TransactionRequest txRequest) { | |||||
this.transactionContent = txRequest.getTransactionContent(); | |||||
setHash(txRequest.getHash()); | |||||
setEndpointSignatures(txRequest.getEndpointSignatures()); | |||||
setNodeSignatures(txRequest.getNodeSignatures()); | |||||
} | |||||
@Override | |||||
public TransactionContent getTransactionContent() { | |||||
return this.transactionContent; | |||||
} | |||||
@Override | |||||
public DigitalSignature[] getEndpointSignatures() { | |||||
return endpointSignatureMap.values().toArray(new DigitalSignature[endpointSignatureMap.size()]); | |||||
} | |||||
@Override | |||||
public DigitalSignature[] getNodeSignatures() { | |||||
return nodeSignatureMap.values().toArray(new DigitalSignature[nodeSignatureMap.size()]); | |||||
} | |||||
public void setEndpointSignatures(Object[] endpointSignatures) { | |||||
if (endpointSignatures != null) { | |||||
for (Object object : endpointSignatures) { | |||||
DigitalSignature endpointSignature = (DigitalSignature) object; | |||||
addEndpointSignatures(endpointSignature); | |||||
} | |||||
} | |||||
return; | |||||
} | |||||
public void setNodeSignatures(Object[] nodeSignatures) { | |||||
if (nodeSignatures != null) { | |||||
for (Object object : nodeSignatures) { | |||||
DigitalSignature nodeSignature = (DigitalSignature) object; | |||||
addNodeSignatures(nodeSignature); | |||||
} | |||||
} | |||||
return; | |||||
} | |||||
private void doAddEndpointSignature(DigitalSignature signature) { | |||||
Bytes address = AddressEncoding.generateAddress(signature.getPubKey()); | |||||
if (endpointSignatureMap.containsKey(address)) { | |||||
throw new IllegalArgumentException( | |||||
String.format("Participant signature of Address[%s] already exist!", address)); | |||||
} | |||||
endpointSignatureMap.put(address, signature); | |||||
} | |||||
/** | |||||
* 从参与者签名列表中检查是否包含指定的参与者; | |||||
* | |||||
* @param userBid | |||||
* 参与者的身份; | |||||
* @return | |||||
*/ | |||||
public boolean containsEndpointSignature(BlockchainIdentity userBid) { | |||||
return endpointSignatureMap.containsKey(userBid.getAddress()); | |||||
} | |||||
public boolean containsEndpointSignature(Bytes userAddress) { | |||||
return endpointSignatureMap.containsKey(userAddress); | |||||
} | |||||
public void addEndpointSignatures(DigitalSignature... signature) { | |||||
for (DigitalSignature sign : signature) { | |||||
doAddEndpointSignature(sign); | |||||
} | |||||
} | |||||
public void addEndpointSignatures(List<DigitalSignature> signature) { | |||||
for (DigitalSignature sign : signature) { | |||||
doAddEndpointSignature(sign); | |||||
} | |||||
} | |||||
/** | |||||
* 从节点签名列表中检查是否包含指定的节点; | |||||
* | |||||
* @param nodeBid | |||||
* 节点的身份; | |||||
* @return | |||||
*/ | |||||
public boolean containsNodeSignature(BlockchainIdentity nodeBid) { | |||||
return nodeSignatureMap.containsKey(nodeBid.getAddress()); | |||||
} | |||||
public boolean containsNodeSignature(Bytes nodeAddress) { | |||||
return nodeSignatureMap.containsKey(nodeAddress); | |||||
} | |||||
private void doAddNodeSignatures(DigitalSignature signature) { | |||||
Bytes address = AddressEncoding.generateAddress(signature.getPubKey()); | |||||
if (nodeSignatureMap.containsKey(address)) { | |||||
throw new IllegalArgumentException(String.format("Node signature of Address[%s] already exist!", address)); | |||||
} | |||||
nodeSignatureMap.put(address, signature); | |||||
} | |||||
public void addNodeSignatures(DigitalSignature... signature) { | |||||
for (DigitalSignature sign : signature) { | |||||
doAddNodeSignatures(sign); | |||||
} | |||||
} | |||||
public void addNodeSignatures(List<DigitalSignature> signature) { | |||||
for (DigitalSignature sign : signature) { | |||||
doAddNodeSignatures(sign); | |||||
} | |||||
} | |||||
@Override | |||||
public HashDigest getHash() { | |||||
return hash; | |||||
} | |||||
public void setHash(HashDigest hash) { | |||||
this.hash = hash; | |||||
} | |||||
// public HashDigest updateHash() { | |||||
// return computeHash(this.defaultHashAlgorithm); | |||||
// } | |||||
// public HashDigest updateHash(CryptoAlgorithm hashAlgorithm) { | |||||
// return computeHash(hashAlgorithm); | |||||
// } | |||||
// | |||||
// private HashDigest computeHash(CryptoAlgorithm hashAlgorithm) { | |||||
// byte[] reqBody = getRequestBody(); | |||||
// this.hash = CryptoUtils.hash(hashAlgorithm).hash(reqBody); | |||||
// return this.hash; | |||||
// } | |||||
// @Override | |||||
// public void resolvFrom(InputStream in) throws IOException { | |||||
// // 解析校验交易请求魔数; | |||||
// byte[] buff = new byte[1]; | |||||
// int len = in.read(buff, 0, 1); | |||||
// if (len < 1) { | |||||
// throw new IllegalArgumentException("No bytes was read for the magic number | |||||
// [TX_REQUEST]!"); | |||||
// } | |||||
// if (MagicNumber.TX_REQUEST != buff[0]) { | |||||
// throw new IllegalArgumentException("Magic number [TX_REQUEST] dismatch!"); | |||||
// } | |||||
// | |||||
// // 解析交易内容; | |||||
// TxContentBlob txContentBlob = new TxContentBlob(); | |||||
// txContentBlob.resolvFrom(in); | |||||
// | |||||
// // 解析参与者签名列表; | |||||
// int participantCount = NumberMask.TINY.resolveMaskedNumber(in); | |||||
// List<DigitalSignature> partiSignList = new ArrayList<>(); | |||||
// for (int i = 0; i < participantCount; i++) { | |||||
// DigitalSignatureBlob signature = new DigitalSignatureBlob(); | |||||
// signature.resolvFrom(in); | |||||
// | |||||
// partiSignList.add(signature); | |||||
// } | |||||
// | |||||
// // 解析节点签名列表; | |||||
// int nodeCount = NumberMask.TINY.resolveMaskedNumber(in); | |||||
// List<DigitalSignature> nodeSignList = new ArrayList<>(); | |||||
// for (int i = 0; i < nodeCount; i++) { | |||||
// DigitalSignatureBlob nodeSign = new DigitalSignatureBlob(); | |||||
// nodeSign.resolvFrom(in); | |||||
// nodeSignList.add(nodeSign); | |||||
// } | |||||
// | |||||
// // 解析哈希算法标识符; | |||||
// HashAlgorithm hashAlgorithm = HashAlgorithm.valueOf((byte) in.read()); | |||||
// | |||||
// // 解析原始的哈希; | |||||
// ByteArray hash = HashEncoding.read(in); | |||||
// | |||||
// this.txContent = txContentBlob; | |||||
// addParticipantSignatures(partiSignList); | |||||
// addNodeSignatures(nodeSignList); | |||||
// this.hash = hash; | |||||
// | |||||
// // 校验原始哈希; | |||||
// byte[] bodyBytes = getRequestBody(); | |||||
// ByteArray rHash = HashEncoding.computeHash(bodyBytes, hashAlgorithm); | |||||
// if (!rHash.equals(hash)) { | |||||
// throw new IllegalArgumentException("The hash is not match with request | |||||
// content!"); | |||||
// } | |||||
// } | |||||
// | |||||
// /** | |||||
// * 输出交易请求消息; | |||||
// * | |||||
// * 注:此方法不会自动重新计算hash;如果消息的内容发生改变后,需要调用主动调用 {@link #updateHash()} 方法重新计算 hash; | |||||
// */ | |||||
// @Override | |||||
// public void writeTo(OutputStream out) throws IOException { | |||||
// if (this.hash == null) { | |||||
// updateHash(); | |||||
// } | |||||
// | |||||
// buildRequestBody(out); | |||||
// | |||||
// // 写入 hash 值; | |||||
// HashEncoding.write(hash, out); | |||||
// } | |||||
// /** | |||||
// * 生成请求体,包括:交易请求魔数、交易内容、参与者签名者列表、哈希算法代号; | |||||
// * | |||||
// * @param out | |||||
// * @throws IOException | |||||
// */ | |||||
// private void buildRequestBody(OutputStream out) throws IOException { | |||||
// | |||||
// buildParticipantRequest(out); | |||||
// | |||||
// // 写入节点签名列表; | |||||
// NumberMask.TINY.writeMask(nodeSignatureMap.size(), out); | |||||
// for (DigitalSignature nodeSignatureBlob : nodeSignatureMap.values()) { | |||||
// nodeSignatureBlob.writeTo(out); | |||||
// } | |||||
// | |||||
// // 写入 hash 算法代号; | |||||
// out.write(hashAlgorithm.getAlgorithm()); | |||||
// } | |||||
// /** | |||||
// * 生成参与者的请求数据; | |||||
// * | |||||
// * <br> | |||||
// * 参与者的请求数据仅包含“交易请求模数({@link MagicNumber#TX_REQUEST })” | |||||
// * “交易内容({@link #getTransactionContent()})” | |||||
// * 和“参与者签名列表({@link #getParticipantSignatures()})”三项属性; | |||||
// * | |||||
// * @param out | |||||
// */ | |||||
// public void buildParticipantRequest(OutputStream out) { | |||||
// try { | |||||
// // 写入魔数; | |||||
// out.write(MagicNumber.TX_REQUEST); | |||||
// | |||||
// // 写入交易内容; | |||||
// txContent.writeTo(out); | |||||
// | |||||
// // 写入 1 个字节的参与者签名数量; | |||||
// if (participantSignatureMap.size() > MAX_TX_PARTICIPANT_COUNT) { | |||||
// throw new IllegalArgumentException("The number of participant signatures is | |||||
// out of the max count[" | |||||
// + MAX_TX_PARTICIPANT_COUNT + "]!"); | |||||
// } | |||||
// | |||||
// NumberMask.TINY.writeMask(participantSignatureMap.size(), out); | |||||
// // 写入参与者签名列表; | |||||
// for (DigitalSignature digitalSignatureBlob : | |||||
// participantSignatureMap.values()) { | |||||
// digitalSignatureBlob.writeTo(out); | |||||
// } | |||||
// | |||||
// } catch (IOException e) { | |||||
// throw new RuntimeIOException(e.getMessage(), e); | |||||
// } | |||||
// } | |||||
// | |||||
// @Override | |||||
// public void writeExternal(ObjectOutput out) throws IOException { | |||||
// ByteArrayOutputStream os = new ByteArrayOutputStream(); | |||||
// writeTo(os); | |||||
// byte[] bts = os.toByteArray(); | |||||
// out.writeInt(bts.length); | |||||
// out.write(bts); | |||||
// } | |||||
// | |||||
// @Override | |||||
// public void readExternal(ObjectInput in) throws IOException, | |||||
// ClassNotFoundException { | |||||
// int len = in.readInt(); | |||||
// byte[] bts = new byte[len]; | |||||
// in.readFully(bts); | |||||
// this.resolvFrom(new ByteArrayInputStream(bts)); | |||||
// } | |||||
// @Override | |||||
// public byte[] toBytes() { | |||||
// ByteArrayOutputStream out = new ByteArrayOutputStream(); | |||||
// try { | |||||
// writeTo(out); | |||||
// } catch (IOException e) { | |||||
// throw new RuntimeIOException(e.getMessage(), e); | |||||
// } | |||||
// return out.toByteArray(); | |||||
// } | |||||
// @Override | |||||
// public ByteArray getHashData() { | |||||
// return ByteArray.wrap(getRequestBody()); | |||||
// } | |||||
// private byte[] getRequestBody() { | |||||
// try { | |||||
// ByteArrayOutputStream out = new ByteArrayOutputStream(); | |||||
// buildRequestBody(out); | |||||
// | |||||
// return out.toByteArray(); | |||||
// } catch (IOException e) { | |||||
// throw new RuntimeIOException(e.getMessage(), e); | |||||
// } | |||||
// } | |||||
} | } |
@@ -1,8 +1,6 @@ | |||||
package com.jd.blockchain.ledger.data; | package com.jd.blockchain.ledger.data; | ||||
import com.jd.blockchain.binaryproto.DConstructor; | |||||
import com.jd.blockchain.binaryproto.DataContractRegistry; | import com.jd.blockchain.binaryproto.DataContractRegistry; | ||||
import com.jd.blockchain.binaryproto.FieldSetter; | |||||
import com.jd.blockchain.ledger.BlockchainIdentity; | import com.jd.blockchain.ledger.BlockchainIdentity; | ||||
import com.jd.blockchain.ledger.UserRegisterOperation; | import com.jd.blockchain.ledger.UserRegisterOperation; | ||||
@@ -11,17 +9,16 @@ public class UserRegisterOpTemplate implements UserRegisterOperation { | |||||
static { | static { | ||||
DataContractRegistry.register(UserRegisterOperation.class); | DataContractRegistry.register(UserRegisterOperation.class); | ||||
} | } | ||||
private BlockchainIdentity userID; | private BlockchainIdentity userID; | ||||
public UserRegisterOpTemplate() { | public UserRegisterOpTemplate() { | ||||
} | } | ||||
@DConstructor(name="UserRegisterOpTemplate") | |||||
public UserRegisterOpTemplate(@FieldSetter(name="getUserId", type="BlockchainIdentity") BlockchainIdentity userID) { | |||||
public UserRegisterOpTemplate(BlockchainIdentity userID) { | |||||
this.userID = userID; | this.userID = userID; | ||||
} | } | ||||
@Override | @Override | ||||
public BlockchainIdentity getUserID() { | public BlockchainIdentity getUserID() { | ||||
return userID; | return userID; | ||||