2021SC@SDUSC
一、交易构建器与交易请求构建器
1.TxBuilder
这是一个交易构建器的类,调用了事务构建器的接口
首先我们先来看一下它的对象以及变量
//这是一个区块的操作集
private BlockchainOperationFactory opFactory = new BlockchainOperationFactory();
//这是二进制的算法
private CryptoAlgorithm hashAlgorithm;
//这是账本哈希
private HashDigest ledgerHash;
创建一个针对指定账本的交易构建器;
ledgerHash 账本哈希,也是账本的唯一ID;
hashAlgorithm 生成交易时使用的哈希算法的名称;
public TxBuilder(HashDigest ledgerHash, String hashAlgorithm) {
this.ledgerHash = ledgerHash;
this.hashAlgorithm = Crypto.getAlgorithm(hashAlgorithm);
}
创建一个针对指定账本的交易构建器;
edgerHash 账本哈希,也是账本的唯一ID;
hashAlgorithmCode 生成交易时使用的哈希算法的代码;
public TxBuilder(HashDigest ledgerHash, short hashAlgorithmCode) {
this.ledgerHash = ledgerHash;
this.hashAlgorithm = Crypto.getAlgorithm(hashAlgorithmCode);
}
创建一个针对指定账本的交易构建器;
ledgerHash 账本哈希,也是账本的唯一ID;
hashAlgorithmCode 生成交易时使用的哈希算法的代码;
public TxBuilder(HashDigest ledgerHash, CryptoAlgorithm hashAlgorithm) {
this.ledgerHash = ledgerHash;
this.hashAlgorithm = hashAlgorithm;
}
@Override
public HashDigest getLedgerHash() {
return ledgerHash;
}
返回已经定义的操作列表
public Collection<Operation> getOperations() {
return opFactory.getOperations();
}
这是关于各个事务交易的构造器
@Override
public TransactionRequestBuilder prepareRequest() {
return prepareRequest(System.currentTimeMillis());
}
@Override
public TransactionContent prepareContent() {
return prepareContent(System.currentTimeMillis());
}
@Override
public TransactionRequestBuilder prepareRequest(long time) {
TransactionContent txContent = prepareContent(time);
HashDigest transactionHash = computeTxContentHash(hashAlgorithm, txContent);
return new TxRequestBuilder(transactionHash, txContent);
}
@Override
public TransactionContent prepareContent(long time) {
TxContentBlob txContent = new TxContentBlob(ledgerHash);
txContent.addOperations(opFactory.getOperations());
txContent.setTime(time);
// HashDigest contentHash = computeTxContentHash(txContent);
// txContent.setHash(contentHash);
return txContent;
}
这些方法用来计算哈希值。
public static HashDigest computeTxContentHash(CryptoAlgorithm hashAlgorithm, TransactionContent txContent) {
byte[] contentBodyBytes = BinaryProtocol.encode(txContent, TransactionContent.class);
HashDigest contentHash = Crypto.getHashFunction(hashAlgorithm).hash(contentBodyBytes);
return contentHash;
}
public static HashDigest computeTxContentHash(short algorithmCode, TransactionContent txContent) {
byte[] contentBodyBytes = BinaryProtocol.encode(txContent, TransactionContent.class);
HashDigest contentHash = Crypto.getHashFunction(algorithmCode).hash(contentBodyBytes);
return contentHash;
}
public static HashDigest computeTxContentHash(String algorithmName, TransactionContent txContent) {
byte[] contentBodyBytes = BinaryProtocol.encode(txContent, TransactionContent.class);
HashDigest contentHash = Crypto.getHashFunction(algorithmName).hash(contentBodyBytes);
return contentHash;
}
public static boolean verifyTxContentHash(TransactionContent txContent, HashDigest verifiedHash) {
HashDigest hash = computeTxContentHash(verifiedHash.getAlgorithm(), txContent);
return hash.equals(verifiedHash);
}
public Collection<OperationResultHandle> getReturnValuehandlers() {
return opFactory.getReturnValuetHandlers();
}
@Override
public SecurityOperationBuilder security() {
return opFactory.security();
}
@Override
public LedgerInitOperationBuilder ledgers() {
return opFactory.ledgers();
}
@Override
public UserRegisterOperationBuilder users() {
return opFactory.users();
}
@Override
public DataAccountRegisterOperationBuilder dataAccounts() {
return opFactory.dataAccounts();
}
@Override
public DataAccountKVSetOperationBuilder dataAccount(String accountAddress) {
return opFactory.dataAccount(accountAddress);
}
@Override
public DataAccountKVSetOperationBuilder dataAccount(Bytes accountAddress) {
return opFactory.dataAccount(accountAddress);
}
@Override
public ContractCodeDeployOperationBuilder contracts() {
return opFactory.contracts();
}
public ContractEventSendOperationBuilder contractEvents() {
return opFactory.contractEvents();
}
@Override
public ParticipantRegisterOperationBuilder participants() {
return opFactory.participants();
}
@Override
public ParticipantStateUpdateOperationBuilder states() {
return opFactory.states();
}
@Override
public ConsensusSettingsUpdateOperationBuilder settings() {
return opFactory.settings();
}
@Override
public <T> T contract(Bytes address, Class<T> contractIntf) {
return opFactory.contract(address, contractIntf);
}
@Override
public ContractEventSendOperationBuilder contract() {
return opFactory.contract();
}
@Override
public EventAccountRegisterOperationBuilder eventAccounts() {
return opFactory.eventAccounts();
}
@Override
public EventPublishOperationBuilder eventAccount(String accountAddress) {
return opFactory.eventAccount(accountAddress);
}
@Override
public EventPublishOperationBuilder eventAccount(Bytes accountAddress) {
return opFactory.eventAccount(accountAddress);
}
@Override
public <T> T contract(String address, Class<T> contractIntf) {
return opFactory.contract(address, contractIntf);
}
}
2.TxRequestBuilder
//事务账户的哈希值
private HashDigest transactionHash;
//事务内容
private TransactionContent txContent;
//终止节点的指示列表
private List<DigitalSignature> endpointSignatures = new ArrayList<>();
//节点的指示列表
private List<DigitalSignature> nodeSignatures = new ArrayList<>();
这是一个基本的交易请求构造器,输入事务的哈希值以及内容,方法会构造一个txrequestbuilder。
public TxRequestBuilder(HashDigest transactionHash, TransactionContent txContent) {
this.transactionHash = transactionHash;
this.txContent = txContent;
}
这个第一个方法是获取事务的哈希值,第二个方法是用户获取事务哈希值的内容。
@Override
public HashDigest getTransactionHash() {
return transactionHash;
}
@Override
public TransactionContent getTransactionContent() {
return txContent;
}
这两个方法是用来签名用
@Override
public DigitalSignature signAsEndpoint(AsymmetricKeypair keyPair) {
DigitalSignature signature = SignatureUtils.sign(transactionHash, keyPair);
addEndpointSignature(signature);
return signature;
}
@Override
public DigitalSignature signAsNode(AsymmetricKeypair keyPair) {
DigitalSignature signature = SignatureUtils.sign(transactionHash, keyPair);
addNodeSignature(signature);
return signature;
}
这一个方法是向节点列表/终止节点列表的添加新的节点信息
@Override
public void addNodeSignature(DigitalSignature... signatures) {
if (signatures != null) {
for (DigitalSignature s : signatures) {
nodeSignatures.add(s);
}
}
}
@Override
public void addEndpointSignature(DigitalSignature... signatures) {
if (signatures != null) {
for (DigitalSignature s : signatures) {
endpointSignatures.add(s);
}
}
}
代码返回一个建立后的信息。
@Override
public TransactionRequest buildRequest() {
TxRequestMessage txMessage = new TxRequestMessage(transactionHash, txContent);
txMessage.addEndpointSignatures(endpointSignatures);
txMessage.addNodeSignatures(nodeSignatures);
return txMessage;
}