ACS4 - 共识标准¶
ACS4用于定制共识机制。
接口¶
如果想要定制共识机制,需要实现以下五个接口:
- GetConsensusCommand,其参数为二进制数组,返回值为acs4.proto中定义的ConsensusCommand类型,该类型用于提示调用GetConsensusCommand的账户的下一次出块开始时间、出块时间限制和出块最终截止时间;
- GetConsensusExtraData,参数和返回值都为二进制数组,用来在出块的时候,通过共识合约生成共识区块头信息,该信息被共识合约包装为二进制数组;
- GenerateConsensusTransactions,参数为二进制数组,返回值为TransactionList类型,用来在出块的时候,通过共识合约生成共识系统交易,每个区块中都会且仅会包含一个共识交易,用来把最近的共识信息写入State数据库;
- ValidateConsensusBeforeExecution,参数为二进制数组,返回值为ValidationResult类型,用来在区块被执行之前验证区块头中的共识信息是否正确;
- ValidateConsensusAfterExecution,参数和返回值同上,用来在区块执行之后验证写入State中的共识信息是否正确。
其中ConsensusCommand、ValidationResult和TransactionList的定义为:
message ConsensusCommand {
int32 limit_milliseconds_of_mining_block = 1;// Time limit of mining next block.
bytes hint = 2;// Context of Hint is diverse according to the consensus protocol we choose, so we use bytes.
google.protobuf.Timestamp arranged_mining_time = 3;
google.protobuf.Timestamp mining_due_time = 4;
}
message ValidationResult {
bool success = 1;
string message = 2;
bool is_re_trigger = 3;
}
message TransactionList {
repeated aelf.Transaction transactions = 1;
}
应用¶
ACS4中所定义的五个接口基本一一对应AElf.Kernel.Consensus项目中IConsensusService接口的五个方法:
ACS4 | IConsensusService | 原理 | 调用时机 |
---|---|---|---|
GetConsensusCommand |
Task TriggerConsensusAsync (ChainContext chainContext); |
When TriggerConsensusAsync is called, it will use the account configured by the node to call the GetConsensusCommand method of the consensus contract to obtain block information ConsensusCommand), and use it to (see IConsensusScheduler implementation) . |
EventData event is thrown;
data fails and the consensus needs to be triggered again (The IsReTrigger field of the ValidationResult type is true); |
|
Task<byte[]> GetConsensus ExtraDataAsync(ChainContext chainContext); |
When a node produces a block, it will generate block header information for the new block by IBlockExtraDataService. This service is implemented to traverse all IBlockExtraDataProvider implementations, and they generate binary array information into the ExtraData field of BlockHeader. The consensus block header information is provided by ConsensusExtraDataProvider, in which the GetConsensusExtraDataAsync of the IConsensusService in the consensus contract is called, and the GetConsensusExtraDataAsync method is implemented by calling the GetConsensusExtraData in the consensus contract. |
At the time that the node produces a new block. |
|
Task<List<Transaction>> GenerateConsensus- TransactionsAsync( ChainContext chainContext); |
In the process of generating new blocks, a consensus transaction needs to be generated as one of the system transactions. The basic principle is the same as GetConsensusExtraData. |
At the time that the node produces a new block. |
|
Task<bool> ValidateConsensus BeforeExecutionAsync( chainContext, byte[] consensusExtraData); |
As long as the IBlockValidationProvider interface is implemented, a new block validator can be added. The consensus validator is ConsensusValidationProvider , where ValidateBlockBeforeExecuteAsync is implemented by calling the ValidateConsensusBeforeExecution method of the consensus contract. |
At the time that the node produces a new block. |
|
Task<bool> ValidateConsensus AfterExecutionAsync ( ChainContext chainContext, byte[] consensusExtraData); |
The implementation of ValidateBlockAfterExecuteAsync in ConsensusValidationProvider is to call the ValidateConsensusAfterExecution in the consensus contract. |
At the time that the node produces a new block. |
示例¶
可以参考AEDPoS合约的实现。