Skip to content

Commit d43940a

Browse files
authored
Merge pull request ethereum#33 from hash-laboratories-au/XIN-98-commit-workflow
Commit grand grand parent block(continous rounds) if enough votes or …
2 parents 35eebab + 15275de commit d43940a

File tree

9 files changed

+238
-64
lines changed

9 files changed

+238
-64
lines changed

consensus/XDPoS/engines/engine_v2/engine.go

Lines changed: 38 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -831,6 +831,7 @@ func (x *XDPoS_v2) sendVote(chainReader consensus.ChainReader, blockInfo *utils.
831831

832832
signedHash, err := x.signSignature(utils.VoteSigHash(blockInfo))
833833
if err != nil {
834+
log.Error("signSignature when sending out Vote", "BlockInfoHash", blockInfo.Hash, "Error", err)
834835
return err
835836
}
836837

@@ -840,7 +841,11 @@ func (x *XDPoS_v2) sendVote(chainReader consensus.ChainReader, blockInfo *utils.
840841
Signature: signedHash,
841842
}
842843

843-
x.voteHandler(chainReader, voteMsg)
844+
err = x.voteHandler(chainReader, voteMsg)
845+
if err != nil {
846+
log.Error("sendVote error", "BlockInfoHash", blockInfo.Hash, "Error", err)
847+
return err
848+
}
844849
x.broadcastToBftChannel(voteMsg)
845850
return nil
846851
}
@@ -854,14 +859,19 @@ func (x *XDPoS_v2) sendVote(chainReader consensus.ChainReader, blockInfo *utils.
854859
func (x *XDPoS_v2) sendTimeout() error {
855860
signedHash, err := x.signSignature(utils.TimeoutSigHash(&x.currentRound))
856861
if err != nil {
862+
log.Error("signSignature when sending out TC", "Error", err)
857863
return err
858864
}
859865
timeoutMsg := &utils.Timeout{
860866
Round: x.currentRound,
861867
Signature: signedHash,
862868
}
863869

864-
x.timeoutHandler(timeoutMsg)
870+
err = x.timeoutHandler(timeoutMsg)
871+
if err != nil {
872+
log.Error("TimeoutHandler error", "TimeoutRound", timeoutMsg.Round, "Error", err)
873+
return err
874+
}
865875
x.broadcastToBftChannel(timeoutMsg)
866876
return nil
867877
}
@@ -930,36 +940,49 @@ func (x *XDPoS_v2) getSyncInfo() *utils.SyncInfo {
930940
}
931941
}
932942

933-
//TODO: find parent and grandparent and grandgrandparent block, check round number, if so, commit grandgrandparent
934-
func (x *XDPoS_v2) commitBlocks(blockCahinReader consensus.ChainReader, proposedBlockHeader *types.Header, proposedBlockRound *utils.Round) (bool, error) {
943+
//Find parent and grandparent, check round number, if so, commit grandparent(grandGrandParent of currentBlock)
944+
func (x *XDPoS_v2) commitBlocks(blockChainReader consensus.ChainReader, proposedBlockHeader *types.Header, proposedBlockRound *utils.Round) (bool, error) {
935945
// XDPoS v1.0 switch to v2.0, skip commit
936946
if big.NewInt(0).Sub(proposedBlockHeader.Number, big.NewInt(2)).Cmp(x.config.XDPoSV2Block) <= 0 {
937947
return false, nil
938948
}
939-
// Find the last two parent block and check their rounds are the continous
940-
parentBlock := blockCahinReader.GetHeaderByHash(proposedBlockHeader.ParentHash)
949+
// Find the last two parent block and check their rounds are the continuous
950+
parentBlock := blockChainReader.GetHeaderByHash(proposedBlockHeader.ParentHash)
941951

942952
var decodedExtraField utils.ExtraFields_v2
943953
err := utils.DecodeBytesExtraFields(parentBlock.Extra, &decodedExtraField)
944954
if err != nil {
955+
log.Error("Fail to execute first DecodeBytesExtraFields for commiting block", "ProposedBlockHash", proposedBlockHeader.Hash())
945956
return false, err
946957
}
947958
if *proposedBlockRound-1 != decodedExtraField.Round {
959+
log.Debug("[commitBlocks] Rounds not continuous(parent) found when committing block", "proposedBlockRound", proposedBlockRound, "decodedExtraField.Round", decodedExtraField.Round, "proposedBlockHeaderHash", proposedBlockHeader.Hash())
948960
return false, nil
949961
}
950962

951-
// If parent round is continous, we check grandparent
952-
grandParentBlock := blockCahinReader.GetHeaderByHash(parentBlock.ParentHash)
963+
// If parent round is continuous, we check grandparent
964+
grandParentBlock := blockChainReader.GetHeaderByHash(parentBlock.ParentHash)
953965
err = utils.DecodeBytesExtraFields(grandParentBlock.Extra, &decodedExtraField)
954966
if err != nil {
967+
log.Error("Fail to execute second DecodeBytesExtraFields for commiting block", "parentBlockHash", parentBlock.Hash())
955968
return false, err
956969
}
957970
if *proposedBlockRound-2 != decodedExtraField.Round {
971+
log.Debug("[commitBlocks] Rounds not continuous(grand parent) found when committing block", "proposedBlockRound", proposedBlockRound, "decodedExtraField.Round", decodedExtraField.Round, "proposedBlockHeaderHash", proposedBlockHeader.Hash())
958972
return false, nil
959973
}
960-
// TODO: Commit the grandParent block
961-
962-
return true, nil
974+
// Commit the grandParent block
975+
if x.highestCommitBlock == nil || (x.highestCommitBlock.Round < decodedExtraField.Round && x.highestCommitBlock.Number.Cmp(grandParentBlock.Number) == -1) {
976+
x.highestCommitBlock = &utils.BlockInfo{
977+
Number: grandParentBlock.Number,
978+
Hash: grandParentBlock.Hash(),
979+
Round: decodedExtraField.Round,
980+
}
981+
log.Debug("👴 Successfully committed block", "Committed block Hash", x.highestCommitBlock.Hash, "Committed round", x.highestCommitBlock.Round)
982+
return true, nil
983+
}
984+
// Everything else, fail to commit
985+
return false, nil
963986
}
964987

965988
func (x *XDPoS_v2) isExtendingFromAncestor(blockChainReader consensus.ChainReader, currentBlock *utils.BlockInfo, ancestorBlock *utils.BlockInfo) (bool, error) {
@@ -969,10 +992,11 @@ func (x *XDPoS_v2) isExtendingFromAncestor(blockChainReader consensus.ChainReade
969992
for i := 0; i < blockNumDiff; i++ {
970993
parentBlock := blockChainReader.GetHeaderByHash(nextBlockHash)
971994
if parentBlock == nil {
972-
return false, fmt.Errorf("Could not find its parent block when checking whether currentBlock %v is extending from the ancestorBlock %v", currentBlock.Number, ancestorBlock.Number)
995+
return false, fmt.Errorf("Could not find its parent block when checking whether currentBlock %v with hash %v is extending from the ancestorBlock %v", currentBlock.Number, currentBlock.Hash, ancestorBlock.Number)
973996
} else {
974997
nextBlockHash = parentBlock.ParentHash
975998
}
999+
log.Debug("[isExtendingFromAncestor] Found parent block", "CurrentBlockHash", currentBlock.Hash, "ParentHash", nextBlockHash)
9761000
}
9771001

9781002
if nextBlockHash == ancestorBlock.Hash {
@@ -1001,8 +1025,8 @@ func (x *XDPoS_v2) GetCurrentRound() utils.Round {
10011025
}
10021026

10031027
// Utils for test to check currentRound value
1004-
func (x *XDPoS_v2) GetProperties() (utils.Round, *utils.QuorumCert, *utils.QuorumCert, utils.Round) {
1028+
func (x *XDPoS_v2) GetProperties() (utils.Round, *utils.QuorumCert, *utils.QuorumCert, utils.Round, *utils.BlockInfo) {
10051029
x.lock.Lock()
10061030
defer x.lock.Unlock()
1007-
return x.currentRound, x.lockQuorumCert, x.highestQuorumCert, x.highestVotedRound
1031+
return x.currentRound, x.lockQuorumCert, x.highestQuorumCert, x.highestVotedRound, x.highestCommitBlock
10081032
}

consensus/tests/adaptor_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import (
1313
)
1414

1515
func TestAdaptorShouldGetAuthorForDifferentConsensusVersion(t *testing.T) {
16-
blockchain, backend, currentBlock, _, _ := PrepareXDCTestBlockChainForV2Engine(t, 10, params.TestXDPoSMockChainConfigWithV2Engine, 0)
16+
blockchain, backend, currentBlock, _, _, _ := PrepareXDCTestBlockChainForV2Engine(t, 10, params.TestXDPoSMockChainConfigWithV2Engine, 0)
1717
adaptor := blockchain.Engine().(*XDPoS.XDPoS)
1818

1919
addressFromAdaptor, errorAdaptor := adaptor.Author(currentBlock.Header())

consensus/tests/countdown_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import (
1010
)
1111

1212
func TestCountdownTimeoutToSendTimeoutMessage(t *testing.T) {
13-
blockchain, _, _, _, _ := PrepareXDCTestBlockChainForV2Engine(t, 11, params.TestXDPoSMockChainConfigWithV2Engine, 0)
13+
blockchain, _, _, _, _, _ := PrepareXDCTestBlockChainForV2Engine(t, 11, params.TestXDPoSMockChainConfigWithV2Engine, 0)
1414
engineV2 := blockchain.Engine().(*XDPoS.XDPoS).EngineV2
1515

1616
engineV2.SetNewRoundFaker(utils.Round(1), true)

0 commit comments

Comments
 (0)