update internal database interface

This commit is contained in:
i-norden 2023-02-28 11:34:21 -06:00
parent 1741e5f790
commit 82555a5319

View File

@ -3,11 +3,10 @@ package ipld_eth_statedb
import ( import (
"context" "context"
"errors" "errors"
"fmt"
"github.com/VictoriaMetrics/fastcache" "github.com/VictoriaMetrics/fastcache"
"github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/state" "github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/ethdb" "github.com/ethereum/go-ethereum/ethdb"
"github.com/ethereum/go-ethereum/statediff/indexer/ipld" "github.com/ethereum/go-ethereum/statediff/indexer/ipld"
"github.com/ethereum/go-ethereum/trie" "github.com/ethereum/go-ethereum/trie"
@ -28,26 +27,28 @@ var (
errNotFound = errors.New("not found") errNotFound = errors.New("not found")
) )
// Database interface is a union of the subset of the geth state.Database interface required
// to support the vm.StateDB implementation as well as methods specific to this Postgres based implementation
type Database interface { type Database interface {
ContractCode(addrHash common.Hash, codeHash common.Hash) ([]byte, error) ContractCode(addrHash common.Hash, codeHash common.Hash) ([]byte, error)
ContractCodeSize(addrHash common.Hash, codeHash common.Hash) (int, error) ContractCodeSize(addrHash common.Hash, codeHash common.Hash) (int, error)
OpenTrie(root common.Hash) (state.Trie, error) StateAccount(address common.Address) (*types.StateAccount, error)
OpenStorageTrie(addrHash common.Hash, root common.Hash) (state.Trie, error) StorageSlot(addressHash, slotHash common.Hash) ([]byte, error)
CopyTrie(trie state.Trie) state.Trie
} }
var _ Database = &stateDatabase{}
type stateDatabase struct { type stateDatabase struct {
pgdb pgxpool.Pool pgdb *pgxpool.Pool
trieDB *trie.Database trieDB *trie.Database
codeSizeCache *lru.Cache codeSizeCache *lru.Cache
codeCache *fastcache.Cache codeCache *fastcache.Cache
} }
func NewStateDatabase(pgdb pgxpool.Pool, ethdb ethdb.Database, config *trie.Config) (*stateDatabase, error) { func NewStateDatabase(pgdb *pgxpool.Pool, ethdb ethdb.Database, config *trie.Config) (*stateDatabase, error) {
csc, _ := lru.New(codeSizeCacheSize) csc, _ := lru.New(codeSizeCacheSize)
return &stateDatabase{ return &stateDatabase{
pgdb: pgdb, pgdb: pgdb,
trieDB: trie.NewDatabaseWithConfig(ethdb, config), trieDB: trie.NewDatabaseWithConfig(ethdb, config),
codeSizeCache: csc, codeSizeCache: csc,
codeCache: fastcache.New(codeCacheSize), codeCache: fastcache.New(codeCacheSize),
@ -79,27 +80,10 @@ func (sd *stateDatabase) ContractCodeSize(_, codeHash common.Hash) (int, error)
return len(code), err return len(code), err
} }
func (sd *stateDatabase) OpenTrie(root common.Hash) (state.Trie, error) { func (sd *stateDatabase) StateAccount(address common.Address) (*types.StateAccount, error) {
tr, err := trie.NewStateTrie(common.Hash{}, root, sd.trieDB) panic("implement me")
if err != nil {
return nil, err
}
return tr, nil
} }
func (sd *stateDatabase) OpenStorageTrie(addrHash common.Hash, root common.Hash) (state.Trie, error) { func (sd *stateDatabase) StorageSlot(addressHash, slotHash common.Hash) ([]byte, error) {
tr, err := trie.NewStateTrie(addrHash, root, sd.trieDB) panic("implement me")
if err != nil {
return nil, err
}
return tr, nil
}
func (sd *stateDatabase) CopyTrie(t state.Trie) state.Trie {
switch t := t.(type) {
case *trie.StateTrie:
return t.Copy()
default:
panic(fmt.Errorf("unknown trie type %T", t))
}
} }