@@ -831,6 +831,7 @@ func (x *XDPoS_v2) sendVote(chainReader consensus.ChainReader, blockInfo *utils.
831
831
832
832
signedHash , err := x .signSignature (utils .VoteSigHash (blockInfo ))
833
833
if err != nil {
834
+ log .Error ("signSignature when sending out Vote" , "BlockInfoHash" , blockInfo .Hash , "Error" , err )
834
835
return err
835
836
}
836
837
@@ -840,7 +841,11 @@ func (x *XDPoS_v2) sendVote(chainReader consensus.ChainReader, blockInfo *utils.
840
841
Signature : signedHash ,
841
842
}
842
843
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
+ }
844
849
x .broadcastToBftChannel (voteMsg )
845
850
return nil
846
851
}
@@ -854,14 +859,19 @@ func (x *XDPoS_v2) sendVote(chainReader consensus.ChainReader, blockInfo *utils.
854
859
func (x * XDPoS_v2 ) sendTimeout () error {
855
860
signedHash , err := x .signSignature (utils .TimeoutSigHash (& x .currentRound ))
856
861
if err != nil {
862
+ log .Error ("signSignature when sending out TC" , "Error" , err )
857
863
return err
858
864
}
859
865
timeoutMsg := & utils.Timeout {
860
866
Round : x .currentRound ,
861
867
Signature : signedHash ,
862
868
}
863
869
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
+ }
865
875
x .broadcastToBftChannel (timeoutMsg )
866
876
return nil
867
877
}
@@ -930,36 +940,49 @@ func (x *XDPoS_v2) getSyncInfo() *utils.SyncInfo {
930
940
}
931
941
}
932
942
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 ) {
935
945
// XDPoS v1.0 switch to v2.0, skip commit
936
946
if big .NewInt (0 ).Sub (proposedBlockHeader .Number , big .NewInt (2 )).Cmp (x .config .XDPoSV2Block ) <= 0 {
937
947
return false , nil
938
948
}
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 )
941
951
942
952
var decodedExtraField utils.ExtraFields_v2
943
953
err := utils .DecodeBytesExtraFields (parentBlock .Extra , & decodedExtraField )
944
954
if err != nil {
955
+ log .Error ("Fail to execute first DecodeBytesExtraFields for commiting block" , "ProposedBlockHash" , proposedBlockHeader .Hash ())
945
956
return false , err
946
957
}
947
958
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 ())
948
960
return false , nil
949
961
}
950
962
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 )
953
965
err = utils .DecodeBytesExtraFields (grandParentBlock .Extra , & decodedExtraField )
954
966
if err != nil {
967
+ log .Error ("Fail to execute second DecodeBytesExtraFields for commiting block" , "parentBlockHash" , parentBlock .Hash ())
955
968
return false , err
956
969
}
957
970
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 ())
958
972
return false , nil
959
973
}
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
963
986
}
964
987
965
988
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
969
992
for i := 0 ; i < blockNumDiff ; i ++ {
970
993
parentBlock := blockChainReader .GetHeaderByHash (nextBlockHash )
971
994
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 )
973
996
} else {
974
997
nextBlockHash = parentBlock .ParentHash
975
998
}
999
+ log .Debug ("[isExtendingFromAncestor] Found parent block" , "CurrentBlockHash" , currentBlock .Hash , "ParentHash" , nextBlockHash )
976
1000
}
977
1001
978
1002
if nextBlockHash == ancestorBlock .Hash {
@@ -1001,8 +1025,8 @@ func (x *XDPoS_v2) GetCurrentRound() utils.Round {
1001
1025
}
1002
1026
1003
1027
// 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 ) {
1005
1029
x .lock .Lock ()
1006
1030
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
1008
1032
}
0 commit comments