ACS4 - Consensus Standard¶
ACS4 is used to customize consensus mechanisms.
Interface¶
If you want to customize the consensus mechanism, you need to implement the following five interfaces:
- GetConsensusCommand, whose parameter is a binary array, returns ConsensusCommand defined in acs4.proto. This type is used to indicate the start time of the next block, the block time limit, and the final cut-off time for the account calling GetConsensus Command;
- GetConsensusExtraData, the parameters and return values are binary arrays, which are used to generate consensus block header information through consensus contracts when a new block is produced;
- GenerateConsensusTransactions, the parameter is a binary array, and the return value is of type TransactionList. It is used to generate a consensus system transaction when a block is generated. Each block will contain only one consensus transaction, which is used to write the latest consensus information to the State database;
- ValidateConsensusBeforeExecution, the parameter is a binary array, and the return value is of type ValidationResult, is used to verify whether the consensus information in the block header is correct before the block executes;
- ValidateConsensusAfterExecution, with the same parameter and return value, is used to verify that the consensus information written to the State is correct after the block executes.
ConsensusCommand, ValidationResult and TransactionList are defined as:
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;
}
Usage¶
The five interfaces defined in ACS4 basically correspond to the five methods of the IConsensusService interface in the AElf.Kernel.Consensus project:
ACS4 | IConsensusService | Methodology | The Timing To Call |
---|---|---|---|
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. |
Example¶
You can refer to the implementation of the AEDPoS contract.