cmd/era: clearer comments

This commit is contained in:
lightclient 2024-01-30 14:33:37 -07:00
parent 2a4cbda115
commit 0fa8947925
No known key found for this signature in database
GPG key ID: 75C916AFEE20183E

View file

@ -79,7 +79,7 @@ var (
verifyCommand = &cli.Command{ verifyCommand = &cli.Command{
Name: "verify", Name: "verify",
ArgsUsage: "<expected>", ArgsUsage: "<expected>",
Usage: "verifies each era against expected accumulator root", Usage: "verifies each era1 against expected accumulator root",
Action: verify, Action: verify,
} }
) )
@ -112,7 +112,7 @@ func block(ctx *cli.Context) error {
} }
e, err := open(ctx, num/uint64(ctx.Int(eraSizeFlag.Name))) e, err := open(ctx, num/uint64(ctx.Int(eraSizeFlag.Name)))
if err != nil { if err != nil {
return fmt.Errorf("error opening era: %w", err) return fmt.Errorf("error opening era1: %w", err)
} }
defer e.Close() defer e.Close()
// Read block with number. // Read block with number.
@ -260,16 +260,18 @@ func checkAccumulator(e *era.Era) error {
if err != nil { if err != nil {
return fmt.Errorf("error making era iterator: %w", err) return fmt.Errorf("error making era iterator: %w", err)
} }
// Starting at epoch 0, iterate through all available era1 files and // To fully verify an era the following attributes must be checked:
// check the following: // 1) the block index is constructed correctly
// * the block index is constructed correctly // 2) the tx root matches the value in the block
// * the starting total difficulty value is correct // 3) the receipts root matches the value in the block
// * the accumulator is correct by recomputing it locally, // 4) the starting total difficulty value is correct
// which verifies the blocks are all correct (via hash) // 5) the accumulator is correct by recomputing it locally, which verifies
// * the receipts root matches the value in the block // the blocks are all correct (via hash)
//
// The attributes 1), 2), and 3) are checked for each block. 4) and 5) require
// accumulation accross the entire set and are verified at the end.
for it.Next() { for it.Next() {
// next() walks the block index, so we're able to // 1) next() walks the block index, so we're able to implicitly verify it.
// implicitly verify it.
if it.Error() != nil { if it.Error() != nil {
return fmt.Errorf("error reading block %d: %w", it.Number(), err) return fmt.Errorf("error reading block %d: %w", it.Number(), err)
} }
@ -277,12 +279,12 @@ func checkAccumulator(e *era.Era) error {
if it.Error() != nil { if it.Error() != nil {
return fmt.Errorf("error reading block %d: %w", it.Number(), err) return fmt.Errorf("error reading block %d: %w", it.Number(), err)
} }
// 2) recompute tx root and verify against header.
tr := types.DeriveSha(block.Transactions(), trie.NewStackTrie(nil)) tr := types.DeriveSha(block.Transactions(), trie.NewStackTrie(nil))
if tr != block.TxHash() { if tr != block.TxHash() {
return fmt.Errorf("tx root in block %d mismatch: want %s, got %s", block.NumberU64(), block.TxHash(), tr) return fmt.Errorf("tx root in block %d mismatch: want %s, got %s", block.NumberU64(), block.TxHash(), tr)
} }
// Calculate receipt root from receipt list and check // 3) recompute receipt root and check value against block.
// value against block.
rr := types.DeriveSha(receipts, trie.NewStackTrie(nil)) rr := types.DeriveSha(receipts, trie.NewStackTrie(nil))
if rr != block.ReceiptHash() { if rr != block.ReceiptHash() {
return fmt.Errorf("receipt root in block %d mismatch: want %s, got %s", block.NumberU64(), block.ReceiptHash(), rr) return fmt.Errorf("receipt root in block %d mismatch: want %s, got %s", block.NumberU64(), block.ReceiptHash(), rr)
@ -291,6 +293,7 @@ func checkAccumulator(e *era.Era) error {
td.Add(td, block.Difficulty()) td.Add(td, block.Difficulty())
tds = append(tds, new(big.Int).Set(td)) tds = append(tds, new(big.Int).Set(td))
} }
// 4+5) Verify accumulator and total difficulty.
got, err := era.ComputeAccumulator(hashes, tds) got, err := era.ComputeAccumulator(hashes, tds)
if err != nil { if err != nil {
return fmt.Errorf("error computing accumulator: %w", err) return fmt.Errorf("error computing accumulator: %w", err)