use patched plugeth GetContractCode

This commit is contained in:
Roy Crihfield 2023-06-22 15:17:01 +08:00
parent 0d9a5f3828
commit e24178a8a9
2 changed files with 8 additions and 36 deletions

View File

@ -3,9 +3,7 @@ package statediff
import ( import (
"context" "context"
"encoding/json" "encoding/json"
"errors"
"math/big" "math/big"
"sync"
"github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core" "github.com/ethereum/go-ethereum/core"
@ -32,26 +30,16 @@ type BlockChain interface {
StateCache() adapt.StateView StateCache() adapt.StateView
} }
// CodeStore stores contract code hashes to data in a synchronized map
type CodeStore struct {
code map[plugeth.Hash][]byte
codeMtx sync.RWMutex
}
// pluginBlockChain adapts the plugeth Backend to the blockChain interface // pluginBlockChain adapts the plugeth Backend to the blockChain interface
type pluginBlockChain struct { type pluginBlockChain struct {
restricted.Backend restricted.Backend
ctx context.Context ctx context.Context
code *CodeStore
} }
var _ BlockChain = (*pluginBlockChain)(nil) func NewPluginBlockChain(backend restricted.Backend) BlockChain {
func NewPluginBlockChain(backend restricted.Backend, code *CodeStore) BlockChain {
return &pluginBlockChain{ return &pluginBlockChain{
Backend: backend, Backend: backend,
ctx: context.Background(), ctx: context.Background(),
code: code,
} }
} }
@ -132,21 +120,5 @@ func (p *pluginStateView) OpenTrie(root common.Hash) (adapt.StateTrie, error) {
} }
func (p *pluginStateView) ContractCode(hash common.Hash) ([]byte, error) { func (p *pluginStateView) ContractCode(hash common.Hash) ([]byte, error) {
return p.backend.code.Get(plugeth.Hash(hash)) return p.backend.GetContractCode(plugeth.Hash(hash))
}
func (b *CodeStore) Get(hash plugeth.Hash) ([]byte, error) {
b.codeMtx.RLock()
defer b.codeMtx.RUnlock()
code, has := b.code[hash]
if !has {
return nil, errors.New("code not found")
}
return []byte(string(code)), nil
}
func (b *CodeStore) Set(hash plugeth.Hash, code []byte) {
b.codeMtx.Lock()
defer b.codeMtx.Unlock()
b.code[hash] = code
} }

View File

@ -20,7 +20,6 @@ var (
gethContext core.Context gethContext core.Context
service *statediff.Service service *statediff.Service
blockchain statediff.BlockChain blockchain statediff.BlockChain
codeStore statediff.CodeStore
) )
func Initialize(ctx core.Context, pl core.PluginLoader, logger core.Logger) { func Initialize(ctx core.Context, pl core.PluginLoader, logger core.Logger) {
@ -48,7 +47,7 @@ func InitializeNode(stack core.Node, b core.Backend) {
return return
} }
serviceConfig := GetConfig() serviceConfig := GetConfig()
blockchain = statediff.NewPluginBlockChain(backend, &codeStore) blockchain = statediff.NewPluginBlockChain(backend)
var indexer interfaces.StateDiffIndexer var indexer interfaces.StateDiffIndexer
if serviceConfig.IndexerConfig != nil { if serviceConfig.IndexerConfig != nil {
@ -101,7 +100,8 @@ func StateUpdate(
return return
} }
for hash, code := range codeUpdates { // for hash, code := range codeUpdates {
codeStore.Set(hash, code) // log.Debug("UPDATING CODE", "hash", hash)
} // codeStore.Set(hash, code)
// }
} }