2019-05-27 13:51:49 +00:00
|
|
|
package trie
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
|
|
|
"sort"
|
|
|
|
|
2020-01-24 10:58:01 +00:00
|
|
|
"github.com/ledgerwatch/turbo-geth/common"
|
2019-05-27 13:51:49 +00:00
|
|
|
"github.com/ledgerwatch/turbo-geth/ethdb"
|
|
|
|
)
|
|
|
|
|
|
|
|
var emptyHash [32]byte
|
|
|
|
|
2020-03-23 22:10:36 +00:00
|
|
|
type ResolveFunc func(*Resolver) error
|
|
|
|
|
2019-10-18 13:03:09 +00:00
|
|
|
// Resolver looks up (resolves) some keys and corresponding values from a database.
|
|
|
|
// One resolver per trie (prefix).
|
|
|
|
// See also ResolveRequest in trie.go
|
|
|
|
type Resolver struct {
|
2020-01-24 10:58:01 +00:00
|
|
|
accounts bool // Is this a resolver for accounts or for storage
|
|
|
|
historical bool
|
2020-01-31 07:31:50 +00:00
|
|
|
collectWitnesses bool // if true, stores witnesses for all the subtries that are being resolved
|
2020-01-24 10:58:01 +00:00
|
|
|
blockNr uint64
|
2020-01-31 07:31:50 +00:00
|
|
|
topLevels int // How many top levels of the trie to keep (not roll into hashes)
|
|
|
|
requests []*ResolveRequest
|
2020-03-23 22:10:36 +00:00
|
|
|
codeRequests []*ResolveRequestForCode
|
2020-01-24 10:58:01 +00:00
|
|
|
witnesses []*Witness // list of witnesses for resolved subtries, nil if `collectWitnesses` is false
|
2019-05-27 13:51:49 +00:00
|
|
|
}
|
|
|
|
|
2019-10-18 13:03:09 +00:00
|
|
|
func NewResolver(topLevels int, forAccounts bool, blockNr uint64) *Resolver {
|
|
|
|
tr := Resolver{
|
2020-03-23 22:10:36 +00:00
|
|
|
accounts: forAccounts,
|
|
|
|
requests: []*ResolveRequest{},
|
|
|
|
codeRequests: []*ResolveRequestForCode{},
|
|
|
|
blockNr: blockNr,
|
|
|
|
topLevels: topLevels,
|
2019-05-27 13:51:49 +00:00
|
|
|
}
|
|
|
|
return &tr
|
|
|
|
}
|
|
|
|
|
2020-01-30 13:16:12 +00:00
|
|
|
func (tr *Resolver) Reset(topLevels int, forAccounts bool, blockNr uint64) {
|
|
|
|
tr.topLevels = topLevels
|
|
|
|
tr.accounts = forAccounts
|
|
|
|
tr.blockNr = blockNr
|
|
|
|
tr.requests = tr.requests[:0]
|
2020-03-23 22:10:36 +00:00
|
|
|
tr.codeRequests = tr.codeRequests[:0]
|
2020-01-30 13:16:12 +00:00
|
|
|
tr.witnesses = nil
|
|
|
|
tr.collectWitnesses = false
|
|
|
|
tr.historical = false
|
|
|
|
}
|
|
|
|
|
2020-01-24 10:58:01 +00:00
|
|
|
func (tr *Resolver) CollectWitnesses(c bool) {
|
|
|
|
tr.collectWitnesses = c
|
|
|
|
}
|
|
|
|
|
|
|
|
// PopCollectedWitnesses returns all the collected witnesses and clears the storage in this resolver
|
|
|
|
func (tr *Resolver) PopCollectedWitnesses() []*Witness {
|
|
|
|
result := tr.witnesses
|
|
|
|
tr.witnesses = nil
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
2019-10-18 13:03:09 +00:00
|
|
|
func (tr *Resolver) SetHistorical(h bool) {
|
2019-05-27 13:51:49 +00:00
|
|
|
tr.historical = h
|
|
|
|
}
|
|
|
|
|
2020-03-23 22:10:36 +00:00
|
|
|
// AddCodeRequest add a request for code resolution
|
|
|
|
func (tr *Resolver) AddCodeRequest(req *ResolveRequestForCode) {
|
|
|
|
tr.codeRequests = append(tr.codeRequests, req)
|
|
|
|
}
|
|
|
|
|
2019-10-18 13:03:09 +00:00
|
|
|
// Resolver implements sort.Interface
|
2019-05-27 13:51:49 +00:00
|
|
|
// and sorts by resolve requests
|
|
|
|
// (more general requests come first)
|
2019-10-18 13:03:09 +00:00
|
|
|
func (tr *Resolver) Len() int {
|
2019-05-27 13:51:49 +00:00
|
|
|
return len(tr.requests)
|
|
|
|
}
|
|
|
|
|
|
|
|
func min(a, b int) int {
|
|
|
|
if a < b {
|
|
|
|
return a
|
|
|
|
}
|
|
|
|
return b
|
|
|
|
}
|
|
|
|
|
2019-10-18 13:03:09 +00:00
|
|
|
func (tr *Resolver) Less(i, j int) bool {
|
2019-05-27 13:51:49 +00:00
|
|
|
ci := tr.requests[i]
|
|
|
|
cj := tr.requests[j]
|
|
|
|
m := min(ci.resolvePos, cj.resolvePos)
|
|
|
|
c := bytes.Compare(ci.contract, cj.contract)
|
|
|
|
if c != 0 {
|
|
|
|
return c < 0
|
|
|
|
}
|
|
|
|
c = bytes.Compare(ci.resolveHex[:m], cj.resolveHex[:m])
|
|
|
|
if c != 0 {
|
|
|
|
return c < 0
|
|
|
|
}
|
|
|
|
return ci.resolvePos < cj.resolvePos
|
|
|
|
}
|
|
|
|
|
2019-10-18 13:03:09 +00:00
|
|
|
func (tr *Resolver) Swap(i, j int) {
|
2019-05-27 13:51:49 +00:00
|
|
|
tr.requests[i], tr.requests[j] = tr.requests[j], tr.requests[i]
|
|
|
|
}
|
|
|
|
|
2019-10-18 13:03:09 +00:00
|
|
|
func (tr *Resolver) AddRequest(req *ResolveRequest) {
|
2019-05-27 13:51:49 +00:00
|
|
|
tr.requests = append(tr.requests, req)
|
|
|
|
}
|
|
|
|
|
2019-10-18 13:03:09 +00:00
|
|
|
func (tr *Resolver) Print() {
|
2019-05-27 13:51:49 +00:00
|
|
|
for _, req := range tr.requests {
|
|
|
|
fmt.Printf("%s\n", req.String())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-21 12:54:47 +00:00
|
|
|
// Various values of the account field set
|
|
|
|
const (
|
2019-11-15 22:48:49 +00:00
|
|
|
AccountFieldNonceOnly uint32 = 0x01
|
|
|
|
AccountFieldBalanceOnly uint32 = 0x02
|
|
|
|
AccountFieldRootOnly uint32 = 0x04
|
|
|
|
AccountFieldCodeHashOnly uint32 = 0x08
|
|
|
|
AccountFieldSSizeOnly uint32 = 0x10
|
2019-10-21 12:54:47 +00:00
|
|
|
AccountFieldSetNotAccount uint32 = 0x00
|
|
|
|
AccountFieldSetNotContract uint32 = 0x03 // Bit 0 is set for nonce, bit 1 is set for balance
|
|
|
|
AccountFieldSetContract uint32 = 0x0f // Bits 0-3 are set for nonce, balance, storageRoot and codeHash
|
|
|
|
AccountFieldSetContractWithSize uint32 = 0x1f // Bits 0-4 are set for nonce, balance, storageRoot, codeHash and storageSize
|
|
|
|
)
|
|
|
|
|
2020-01-24 10:58:01 +00:00
|
|
|
// ResolveWithDb resolves and hooks subtries using a state database.
|
2020-04-01 16:04:41 +00:00
|
|
|
func (tr *Resolver) ResolveWithDb(db ethdb.Database, blockNr uint64, trace bool) error {
|
2020-03-30 16:01:24 +00:00
|
|
|
if !tr.historical {
|
2020-04-01 16:04:41 +00:00
|
|
|
return tr.ResolveStatefulCached(db, blockNr, trace)
|
2020-02-12 13:52:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return tr.ResolveStateful(db, blockNr)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (tr *Resolver) ResolveStateful(db ethdb.Database, blockNr uint64) error {
|
2020-01-24 10:58:01 +00:00
|
|
|
var hf hookFunction
|
|
|
|
if tr.collectWitnesses {
|
|
|
|
hf = tr.extractWitnessAndHookSubtrie
|
|
|
|
} else {
|
|
|
|
hf = hookSubtrie
|
|
|
|
}
|
|
|
|
|
|
|
|
sort.Stable(tr)
|
2020-01-31 07:31:50 +00:00
|
|
|
|
2020-02-12 13:52:59 +00:00
|
|
|
resolver := NewResolverStateful(tr.topLevels, tr.requests, hf)
|
2020-03-23 22:10:36 +00:00
|
|
|
if err := resolver.RebuildTrie(db, blockNr, tr.accounts, tr.historical); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return resolver.AttachRequestedCode(db, tr.codeRequests)
|
2020-02-12 13:52:59 +00:00
|
|
|
}
|
|
|
|
|
2020-04-01 16:04:41 +00:00
|
|
|
func (tr *Resolver) ResolveStatefulCached(db ethdb.Database, blockNr uint64, trace bool) error {
|
2020-02-12 13:52:59 +00:00
|
|
|
var hf hookFunction
|
|
|
|
if tr.collectWitnesses {
|
|
|
|
hf = tr.extractWitnessAndHookSubtrie
|
|
|
|
} else {
|
|
|
|
hf = hookSubtrie
|
2020-01-31 07:31:50 +00:00
|
|
|
}
|
|
|
|
|
2020-02-12 13:52:59 +00:00
|
|
|
sort.Stable(tr)
|
|
|
|
|
|
|
|
resolver := NewResolverStatefulCached(tr.topLevels, tr.requests, hf)
|
2020-04-01 16:04:41 +00:00
|
|
|
if err := resolver.RebuildTrie(db, blockNr, tr.accounts, tr.historical, trace); err != nil {
|
2020-03-23 22:10:36 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
return resolver.AttachRequestedCode(db, tr.codeRequests)
|
2020-01-24 10:58:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ResolveStateless resolves and hooks subtries using a witnesses database instead of
|
|
|
|
// the state DB.
|
|
|
|
func (tr *Resolver) ResolveStateless(db WitnessStorage, blockNr uint64, trieLimit uint32, startPos int64) (int64, error) {
|
|
|
|
sort.Stable(tr)
|
|
|
|
resolver := NewResolverStateless(tr.requests, hookSubtrie)
|
2020-03-23 22:10:36 +00:00
|
|
|
// we expect CodeNodes to be already attached to the trie in stateless resolution
|
2020-01-24 10:58:01 +00:00
|
|
|
return resolver.RebuildTrie(db, blockNr, trieLimit, startPos)
|
|
|
|
}
|
|
|
|
|
|
|
|
func hookSubtrie(currentReq *ResolveRequest, hbRoot node, hbHash common.Hash) error {
|
|
|
|
if currentReq.RequiresRLP {
|
|
|
|
hasher := newHasher(false)
|
|
|
|
defer returnHasherToPool(hasher)
|
|
|
|
h, err := hasher.hashChildren(hbRoot, 0)
|
|
|
|
if err != nil {
|
2019-11-21 15:12:38 +00:00
|
|
|
return err
|
2019-05-27 13:51:49 +00:00
|
|
|
}
|
2020-01-24 10:58:01 +00:00
|
|
|
currentReq.NodeRLP = h
|
2019-05-27 13:51:49 +00:00
|
|
|
}
|
2020-01-24 10:58:01 +00:00
|
|
|
|
|
|
|
var hookKey []byte
|
|
|
|
if currentReq.contract == nil {
|
|
|
|
hookKey = currentReq.resolveHex[:currentReq.resolvePos]
|
|
|
|
} else {
|
|
|
|
contractHex := keybytesToHex(currentReq.contract)
|
|
|
|
contractHex = contractHex[:len(contractHex)-1-16] // Remove terminal nibble and incarnation bytes
|
|
|
|
hookKey = append(contractHex, currentReq.resolveHex[:currentReq.resolvePos]...)
|
2019-05-27 13:51:49 +00:00
|
|
|
}
|
2020-01-24 10:58:01 +00:00
|
|
|
|
|
|
|
//fmt.Printf("hookKey: %x, %s\n", hookKey, hbRoot.fstring(""))
|
|
|
|
currentReq.t.hook(hookKey, hbRoot)
|
|
|
|
if len(currentReq.resolveHash) > 0 && !bytes.Equal(currentReq.resolveHash, hbHash[:]) {
|
|
|
|
return fmt.Errorf("mismatching hash: %s %x for prefix %x, resolveHex %x, resolvePos %d",
|
|
|
|
currentReq.resolveHash, hbHash, currentReq.contract, currentReq.resolveHex, currentReq.resolvePos)
|
|
|
|
}
|
|
|
|
|
2019-11-21 15:12:38 +00:00
|
|
|
return nil
|
2019-05-27 13:51:49 +00:00
|
|
|
}
|
|
|
|
|
2020-01-24 10:58:01 +00:00
|
|
|
func (tr *Resolver) extractWitnessAndHookSubtrie(currentReq *ResolveRequest, hbRoot node, hbHash common.Hash) error {
|
|
|
|
if tr.witnesses == nil {
|
|
|
|
tr.witnesses = make([]*Witness, 0)
|
2019-05-27 13:51:49 +00:00
|
|
|
}
|
2020-01-24 10:58:01 +00:00
|
|
|
|
2020-03-23 22:10:36 +00:00
|
|
|
witness, err := extractWitnessFromRootNode(hbRoot, tr.blockNr, false /*tr.hb.trace*/, nil)
|
2019-05-27 13:51:49 +00:00
|
|
|
if err != nil {
|
2020-01-24 10:58:01 +00:00
|
|
|
return fmt.Errorf("error while extracting witness for resolver: %w", err)
|
2019-05-27 13:51:49 +00:00
|
|
|
}
|
2020-01-24 10:58:01 +00:00
|
|
|
|
|
|
|
tr.witnesses = append(tr.witnesses, witness)
|
|
|
|
|
|
|
|
return hookSubtrie(currentReq, hbRoot, hbHash)
|
2019-05-27 13:51:49 +00:00
|
|
|
}
|