2021-02-21 18:41:59 +00:00
|
|
|
package integrity
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2021-07-16 13:17:40 +00:00
|
|
|
"context"
|
2021-02-21 18:41:59 +00:00
|
|
|
"encoding/binary"
|
|
|
|
"fmt"
|
|
|
|
"math/bits"
|
|
|
|
"time"
|
|
|
|
|
2021-07-29 11:53:13 +00:00
|
|
|
"github.com/ledgerwatch/erigon-lib/kv"
|
2021-05-20 18:25:53 +00:00
|
|
|
"github.com/ledgerwatch/erigon/common"
|
|
|
|
"github.com/ledgerwatch/erigon/common/hexutil"
|
2022-05-19 03:38:53 +00:00
|
|
|
"github.com/ledgerwatch/erigon/common/math"
|
2021-05-20 18:25:53 +00:00
|
|
|
"github.com/ledgerwatch/erigon/ethdb"
|
|
|
|
"github.com/ledgerwatch/erigon/turbo/trie"
|
2021-07-29 10:23:23 +00:00
|
|
|
"github.com/ledgerwatch/log/v3"
|
2022-05-19 03:38:53 +00:00
|
|
|
"go.uber.org/atomic"
|
2021-02-21 18:41:59 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// AssertSubset a & b == a - checks whether a is subset of b
|
|
|
|
func AssertSubset(prefix []byte, a, b uint16) {
|
|
|
|
if (a & b) != a {
|
|
|
|
panic(fmt.Errorf("invariant 'is subset' failed: %x, %b, %b", prefix, a, b))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-19 03:38:53 +00:00
|
|
|
func Trie(db kv.RoDB, tx kv.Tx, slowChecks bool, ctx context.Context) {
|
2021-07-16 13:17:40 +00:00
|
|
|
quit := ctx.Done()
|
2022-05-24 05:24:20 +00:00
|
|
|
readAheadCtx, cancel := context.WithCancel(ctx)
|
|
|
|
defer cancel()
|
|
|
|
|
2021-02-21 18:41:59 +00:00
|
|
|
logEvery := time.NewTicker(10 * time.Second)
|
|
|
|
defer logEvery.Stop()
|
|
|
|
seek := make([]byte, 256)
|
|
|
|
buf := make([]byte, 256)
|
|
|
|
buf2 := make([]byte, 256)
|
|
|
|
|
|
|
|
{
|
2021-07-28 02:47:38 +00:00
|
|
|
c, err := tx.Cursor(kv.TrieOfAccounts)
|
2021-04-02 06:36:49 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2022-05-24 05:24:20 +00:00
|
|
|
defer c.Close()
|
|
|
|
kv.ReadAhead(readAheadCtx, db, atomic.NewBool(false), kv.TrieOfAccounts, nil, math.MaxInt32)
|
|
|
|
|
2021-07-28 02:47:38 +00:00
|
|
|
trieAcc2, err := tx.Cursor(kv.TrieOfAccounts)
|
2021-04-02 06:36:49 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2022-05-24 05:24:20 +00:00
|
|
|
defer trieAcc2.Close()
|
|
|
|
|
2021-07-28 02:47:38 +00:00
|
|
|
accC, err := tx.Cursor(kv.HashedAccounts)
|
2021-04-02 06:36:49 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2021-02-21 18:41:59 +00:00
|
|
|
defer accC.Close()
|
2022-05-24 05:24:20 +00:00
|
|
|
kv.ReadAhead(readAheadCtx, db, atomic.NewBool(false), kv.HashedAccounts, nil, math.MaxInt32)
|
|
|
|
|
2021-02-21 18:41:59 +00:00
|
|
|
for k, v, errc := c.First(); k != nil; k, v, errc = c.Next() {
|
|
|
|
if errc != nil {
|
|
|
|
panic(errc)
|
|
|
|
}
|
2021-03-12 09:26:39 +00:00
|
|
|
select {
|
|
|
|
default:
|
|
|
|
case <-quit:
|
|
|
|
return
|
|
|
|
case <-logEvery.C:
|
|
|
|
log.Info("trie account integrity", "key", fmt.Sprintf("%x", k))
|
|
|
|
}
|
|
|
|
|
2021-03-13 02:30:54 +00:00
|
|
|
hasState, hasTree, hasHash, hashes, _ := trie.UnmarshalTrieNode(v)
|
|
|
|
AssertSubset(k, hasTree, hasState)
|
2021-02-21 18:41:59 +00:00
|
|
|
AssertSubset(k, hasHash, hasState)
|
|
|
|
if bits.OnesCount16(hasHash) != len(hashes)/common.HashLength {
|
|
|
|
panic(fmt.Errorf("invariant bits.OnesCount16(hasHash) == len(hashes) failed: %d, %d", bits.OnesCount16(hasHash), len(v[6:])/common.HashLength))
|
|
|
|
}
|
|
|
|
found := false
|
|
|
|
var parentK []byte
|
|
|
|
|
2021-03-13 02:30:54 +00:00
|
|
|
// must have parent with right hasTree bit
|
2021-02-21 18:41:59 +00:00
|
|
|
for i := len(k) - 1; i > 0 && !found; i-- {
|
|
|
|
parentK = k[:i]
|
|
|
|
kParent, vParent, err := trieAcc2.SeekExact(parentK)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
if kParent == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
found = true
|
2021-03-13 02:30:54 +00:00
|
|
|
parenthasTree := binary.BigEndian.Uint16(vParent[2:])
|
|
|
|
parentHasBit := 1<<uint16(k[len(parentK)])&parenthasTree != 0
|
2021-02-21 18:41:59 +00:00
|
|
|
if !parentHasBit {
|
2021-03-13 02:30:54 +00:00
|
|
|
panic(fmt.Errorf("for %x found parent %x, but it has no branchBit: %016b", k, parentK, parenthasTree))
|
2021-02-21 18:41:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if !found && len(k) > 1 {
|
|
|
|
panic(fmt.Errorf("trie hash %x has no parent", k))
|
|
|
|
}
|
|
|
|
|
|
|
|
// must have all children
|
|
|
|
seek = seek[:len(k)+1]
|
|
|
|
copy(seek, k)
|
|
|
|
for i := uint16(0); i < 16; i++ {
|
2021-03-13 02:30:54 +00:00
|
|
|
if 1<<i&hasTree == 0 {
|
2021-02-21 18:41:59 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
seek[len(seek)-1] = uint8(i)
|
|
|
|
k2, _, err := trieAcc2.Seek(seek)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
if k2 == nil {
|
2021-03-13 02:30:54 +00:00
|
|
|
panic(fmt.Errorf("key %x has branches %016b, but there is no child %d in db; last seen key: %x->nil", k, hasTree, i, seek))
|
2021-02-21 18:41:59 +00:00
|
|
|
}
|
|
|
|
if !bytes.HasPrefix(k2, seek) {
|
2021-03-13 02:30:54 +00:00
|
|
|
panic(fmt.Errorf("key %x has branches %016b, but there is no child %d in db; last seen key: %x->%x", k, hasTree, i, seek, k2))
|
2021-02-21 18:41:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if !slowChecks {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
// each AccTrie must cover some state
|
|
|
|
buf = buf[:len(k)+1]
|
|
|
|
copy(buf, k)
|
|
|
|
for i := uint16(0); i < 16; i++ {
|
|
|
|
if 1<<i&hasState == 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
found := false
|
|
|
|
buf = buf[:len(k)+1]
|
|
|
|
buf[len(buf)-1] = uint8(i)
|
|
|
|
bitsToMatch := len(buf) * 4
|
|
|
|
if len(buf)%2 == 1 {
|
|
|
|
buf = append(buf, 0)
|
|
|
|
}
|
|
|
|
hexutil.CompressNibbles(buf, &seek)
|
|
|
|
if err := ethdb.Walk(accC, seek, bitsToMatch, func(k, v []byte) (bool, error) {
|
|
|
|
found = true
|
|
|
|
return false, nil
|
|
|
|
}); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
if !found {
|
|
|
|
panic(fmt.Errorf("key %x has state %016b, but there is no child %d,%x in state", k, hasState, i, seek))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
{
|
2021-07-28 02:47:38 +00:00
|
|
|
c, err := tx.Cursor(kv.TrieOfStorage)
|
2021-04-02 06:36:49 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2022-05-24 05:24:20 +00:00
|
|
|
defer c.Close()
|
|
|
|
kv.ReadAhead(readAheadCtx, db, atomic.NewBool(false), kv.TrieOfStorage, nil, math.MaxInt32)
|
|
|
|
|
2021-07-28 02:47:38 +00:00
|
|
|
trieStorage, err := tx.Cursor(kv.TrieOfStorage)
|
2021-04-02 06:36:49 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2022-05-24 05:24:20 +00:00
|
|
|
defer trieStorage.Close()
|
|
|
|
|
2021-07-28 02:47:38 +00:00
|
|
|
storageC, err := tx.Cursor(kv.HashedStorage)
|
2021-04-02 06:36:49 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2021-02-21 18:41:59 +00:00
|
|
|
defer storageC.Close()
|
2022-05-24 05:24:20 +00:00
|
|
|
kv.ReadAhead(readAheadCtx, db, atomic.NewBool(false), kv.HashedStorage, nil, math.MaxInt32)
|
2021-02-21 18:41:59 +00:00
|
|
|
|
|
|
|
for k, v, errc := c.First(); k != nil; k, v, errc = c.Next() {
|
|
|
|
if errc != nil {
|
|
|
|
panic(errc)
|
|
|
|
}
|
2021-03-12 09:26:39 +00:00
|
|
|
select {
|
|
|
|
default:
|
|
|
|
case <-quit:
|
|
|
|
return
|
|
|
|
case <-logEvery.C:
|
|
|
|
log.Info("trie storage integrity", "key", fmt.Sprintf("%x", k))
|
|
|
|
}
|
2021-02-21 18:41:59 +00:00
|
|
|
|
2021-03-13 02:30:54 +00:00
|
|
|
hasState, hasTree, hasHash, hashes, _ := trie.UnmarshalTrieNode(v)
|
|
|
|
AssertSubset(k, hasTree, hasState)
|
2021-02-21 18:41:59 +00:00
|
|
|
AssertSubset(k, hasHash, hasState)
|
|
|
|
if bits.OnesCount16(hasHash) != len(hashes)/common.HashLength {
|
|
|
|
panic(fmt.Errorf("invariant bits.OnesCount16(hasHash) == len(hashes) failed: %d, %d", bits.OnesCount16(hasHash), len(hashes)/common.HashLength))
|
|
|
|
}
|
|
|
|
|
|
|
|
found := false
|
|
|
|
var parentK []byte
|
|
|
|
|
2021-03-13 02:30:54 +00:00
|
|
|
// must have parent with right hasTree bit
|
2021-02-21 18:41:59 +00:00
|
|
|
for i := len(k) - 1; i >= 40 && !found; i-- {
|
|
|
|
parentK = k[:i]
|
|
|
|
kParent, vParent, err := trieStorage.SeekExact(parentK)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
if kParent == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
found = true
|
|
|
|
parentBranches := binary.BigEndian.Uint16(vParent[2:])
|
|
|
|
parentHasBit := 1<<uint16(k[len(parentK)])&parentBranches != 0
|
|
|
|
if !parentHasBit {
|
|
|
|
panic(fmt.Errorf("for %x found parent %x, but it has no branchBit for child: %016b", k, parentK, parentBranches))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !found && len(k) > 40 {
|
|
|
|
panic(fmt.Errorf("trie hash %x has no parent. Last checked: %x", k, parentK))
|
|
|
|
}
|
|
|
|
|
|
|
|
// must have all children
|
|
|
|
seek = seek[:len(k)+1]
|
|
|
|
copy(seek, k)
|
|
|
|
for i := uint16(0); i < 16; i++ {
|
2021-03-13 02:30:54 +00:00
|
|
|
if 1<<i&hasTree == 0 {
|
2021-02-21 18:41:59 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
seek[len(seek)-1] = uint8(i)
|
|
|
|
k2, _, err := trieStorage.Seek(seek)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
if !bytes.HasPrefix(k2, seek) {
|
2021-03-13 02:30:54 +00:00
|
|
|
panic(fmt.Errorf("key %x has branches %016b, but there is no child %d in db", k, hasTree, i))
|
2021-02-21 18:41:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if !slowChecks {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
// each AccTrie must cover some state
|
|
|
|
buf = buf[:len(k)-40+1]
|
|
|
|
copy(buf, k[40:])
|
|
|
|
for i := uint16(0); i < 16; i++ {
|
|
|
|
if 1<<i&hasState == 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
found := false
|
|
|
|
buf = buf[:len(k)-40+1]
|
|
|
|
buf[len(buf)-1] = uint8(i)
|
|
|
|
bitsToMatch := 40*8 + len(buf)*4
|
|
|
|
if len(buf)%2 == 1 {
|
|
|
|
buf = append(buf, 0)
|
|
|
|
}
|
|
|
|
hexutil.CompressNibbles(buf, &buf2)
|
|
|
|
seek = seek[:40+len(buf2)]
|
|
|
|
copy(seek, k[:40])
|
|
|
|
copy(seek[40:], buf2)
|
|
|
|
if err := ethdb.Walk(storageC, seek, bitsToMatch, func(k, v []byte) (bool, error) {
|
|
|
|
found = true
|
|
|
|
return false, nil
|
|
|
|
}); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
if !found {
|
|
|
|
panic(fmt.Errorf("key %x has state %016b, but there is no child %d,%x in state", k, hasState, i, seek))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|