Skip to content

Commit

Permalink
pass the forensics Id at root level (ethereum#107)
Browse files Browse the repository at this point in the history
  • Loading branch information
wjrjerome authored Jul 5, 2022
1 parent 533fe25 commit cfb5c6c
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 11 deletions.
30 changes: 21 additions & 9 deletions consensus/XDPoS/engines/engine_v2/forensics.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand All @@ -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),
}
Expand Down Expand Up @@ -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()
Expand All @@ -336,27 +336,31 @@ 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
for lowerBlockNumHash.Hex() != higherBlockNumberHash.Hex() {
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
Expand All @@ -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
}
2 changes: 1 addition & 1 deletion consensus/tests/engine_v2_tests/forensics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down
2 changes: 1 addition & 1 deletion core/types/forensics.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand All @@ -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
}
Expand Down

0 comments on commit cfb5c6c

Please sign in to comment.