From cfb5c6ce39eaa9960fde083a8c31d442d974df02 Mon Sep 17 00:00:00 2001 From: Jerome Date: Tue, 5 Jul 2022 22:01:36 +1000 Subject: [PATCH] pass the forensics Id at root level (#107) --- .../XDPoS/engines/engine_v2/forensics.go | 30 +++++++++++++------ .../tests/engine_v2_tests/forensics_test.go | 2 +- core/types/forensics.go | 2 +- 3 files changed, 23 insertions(+), 11 deletions(-) diff --git a/consensus/XDPoS/engines/engine_v2/forensics.go b/consensus/XDPoS/engines/engine_v2/forensics.go index f305b59c1aa1..613240ce2f20 100644 --- a/consensus/XDPoS/engines/engine_v2/forensics.go +++ b/consensus/XDPoS/engines/engine_v2/forensics.go @@ -166,7 +166,6 @@ func (f *Forensics) SendForensicProof(chain consensus.ChainReader, engine *XDPoS } content, err := json.Marshal(&types.ForensicsContent{ - Id: generateForensicsId(ancestorHash.Hex(), &lowerRoundQC, &higherRoundQC), DivergingBlockHash: ancestorHash.Hex(), AcrossEpoch: accrossEpoches, DivergingBlockNumber: ancestorBlock.Number.Uint64(), @@ -188,6 +187,7 @@ func (f *Forensics) SendForensicProof(chain consensus.ChainReader, engine *XDPoS } forensicsProof := &types.ForensicProof{ + Id: generateForensicsId(ancestorHash.Hex(), &lowerRoundQC, &higherRoundQC), ForensicsType: "QC", Content: string(content), } @@ -325,8 +325,8 @@ func (f *Forensics) FindAncestorBlockHash(chain consensus.ChainReader, firstBloc lowerBlockNumHash := firstBlockInfo.Hash higherBlockNumberHash := secondBlockInfo.Hash - var ancestorToLowerBlockNumHashPath []string - var ancestorToHigherBlockNumHashPath []string + var lowerBlockNumToAncestorHashPath []string + var higherBlockToAncestorNumHashPath []string orderSwapped := false blockNumberDifference := big.NewInt(0).Sub(secondBlockInfo.Number, firstBlockInfo.Number).Int64() @@ -336,17 +336,17 @@ func (f *Forensics) FindAncestorBlockHash(chain consensus.ChainReader, firstBloc blockNumberDifference = -blockNumberDifference // and make it positive orderSwapped = true } - ancestorToLowerBlockNumHashPath = append(ancestorToLowerBlockNumHashPath, lowerBlockNumHash.Hex()) - ancestorToHigherBlockNumHashPath = append(ancestorToHigherBlockNumHashPath, higherBlockNumberHash.Hex()) + lowerBlockNumToAncestorHashPath = append(lowerBlockNumToAncestorHashPath, lowerBlockNumHash.Hex()) + higherBlockToAncestorNumHashPath = append(higherBlockToAncestorNumHashPath, higherBlockNumberHash.Hex()) // First, make their block number the same to start with for i := 0; i < int(blockNumberDifference); i++ { ph := chain.GetHeaderByHash(higherBlockNumberHash) if ph == nil { - return common.Hash{}, ancestorToLowerBlockNumHashPath, ancestorToHigherBlockNumHashPath, fmt.Errorf("unable to find parent block of hash %v", higherBlockNumberHash) + return common.Hash{}, lowerBlockNumToAncestorHashPath, higherBlockToAncestorNumHashPath, fmt.Errorf("unable to find parent block of hash %v", higherBlockNumberHash) } higherBlockNumberHash = ph.ParentHash - ancestorToHigherBlockNumHashPath = append(ancestorToHigherBlockNumHashPath, ph.ParentHash.Hex()) + higherBlockToAncestorNumHashPath = append(higherBlockToAncestorNumHashPath, ph.ParentHash.Hex()) } // Now, they are on the same starting line, we try find the common ancestor @@ -354,9 +354,13 @@ func (f *Forensics) FindAncestorBlockHash(chain consensus.ChainReader, firstBloc lowerBlockNumHash = chain.GetHeaderByHash(lowerBlockNumHash).ParentHash higherBlockNumberHash = chain.GetHeaderByHash(higherBlockNumberHash).ParentHash // Append the path - ancestorToLowerBlockNumHashPath = append(ancestorToLowerBlockNumHashPath, lowerBlockNumHash.Hex()) - ancestorToHigherBlockNumHashPath = append(ancestorToHigherBlockNumHashPath, higherBlockNumberHash.Hex()) + lowerBlockNumToAncestorHashPath = append(lowerBlockNumToAncestorHashPath, lowerBlockNumHash.Hex()) + higherBlockToAncestorNumHashPath = append(higherBlockToAncestorNumHashPath, higherBlockNumberHash.Hex()) } + + // Reverse the list order as it's from ancestor to X block path. + ancestorToLowerBlockNumHashPath := reverse(lowerBlockNumToAncestorHashPath) + ancestorToHigherBlockNumHashPath := reverse(higherBlockToAncestorNumHashPath) // Swap back the order. We must return in the order that matches what we acceptted in the parameter of firstBlock & secondBlock if orderSwapped { return lowerBlockNumHash, ancestorToHigherBlockNumHashPath, ancestorToLowerBlockNumHashPath, nil @@ -368,3 +372,11 @@ func generateForensicsId(divergingHash string, qc1 *types.QuorumCert, qc2 *types keysList := []string{divergingHash, qc1.ProposedBlockInfo.Hash.Hex(), qc2.ProposedBlockInfo.Hash.Hex()} return strings.Join(keysList[:], ":") } + +func reverse(ss []string) []string { + last := len(ss) - 1 + for i := 0; i < len(ss)/2; i++ { + ss[i], ss[last-i] = ss[last-i], ss[i] + } + return ss +} diff --git a/consensus/tests/engine_v2_tests/forensics_test.go b/consensus/tests/engine_v2_tests/forensics_test.go index bbae32f55154..9b66133aa9f6 100644 --- a/consensus/tests/engine_v2_tests/forensics_test.go +++ b/consensus/tests/engine_v2_tests/forensics_test.go @@ -295,7 +295,7 @@ func TestForensicsAcrossEpoch(t *testing.T) { json.Unmarshal([]byte(forensics.ForensicsProof.Content), &content) idToCompare := content.DivergingBlockHash + ":" + content.SmallerRoundInfo.QuorumCert.ProposedBlockInfo.Hash.Hex() + ":" + content.LargerRoundInfo.QuorumCert.ProposedBlockInfo.Hash.Hex() - assert.Equal(t, idToCompare, content.Id) + assert.Equal(t, idToCompare, forensics.ForensicsProof.Id) assert.True(t, content.AcrossEpoch) assert.Equal(t, types.Round(900), content.SmallerRoundInfo.QuorumCert.ProposedBlockInfo.Round) assert.Equal(t, uint64(1800), content.SmallerRoundInfo.QuorumCert.ProposedBlockInfo.Number.Uint64()) diff --git a/core/types/forensics.go b/core/types/forensics.go index 9af6a5833d85..6f6f0413e7dd 100644 --- a/core/types/forensics.go +++ b/core/types/forensics.go @@ -7,7 +7,6 @@ type ForensicsInfo struct { } type ForensicsContent struct { - Id string `json:"id"` DivergingBlockNumber uint64 `json:"divergingBlockNumber"` DivergingBlockHash string `json:"divergingBlockHash"` AcrossEpoch bool `json:"acrossEpoch"` @@ -16,6 +15,7 @@ type ForensicsContent struct { } type ForensicProof struct { + Id string `json:"id"` ForensicsType string `json:"forensicsType"` // QC or VOTE Content string `json:"content"` // Json string of the forensics data }