pass the forensics Id at root level (#107)

This commit is contained in:
Jerome 2022-07-05 22:01:36 +10:00 committed by GitHub
parent 533fe250db
commit cfb5c6ce39
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 23 additions and 11 deletions

View file

@ -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
}

View file

@ -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())

View file

@ -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
}