utils refactor work from the week

This commit is contained in:
philip-morlier 2021-09-03 15:20:49 -07:00
parent 0d5af1c7dc
commit c36c999383
No known key found for this signature in database
GPG Key ID: 0323A143B7B6F663
4 changed files with 364 additions and 278 deletions

View File

@ -4,24 +4,40 @@ import (
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/log"
"github.com/ethereum/go-ethereum/plugins"
"github.com/opoenrelayxyz/plugeth-utils/core"
"github.com/openrelayxyz/plugeth-utils/core"
)
// TODO (philip): change common.Hash to core.Hash,
func PluginStateUpdate(pl *plugins.PluginLoader, blockRoot, parentRoot core.Hash, destructs map[core.Hash]struct{}, accounts map[core.Hash][]byte, storage map[core.Hash]map[core.Hash][]byte) {
func PluginStateUpdate(pl *plugins.PluginLoader, blockRoot, parentRoot common.Hash, destructs map[common.Hash]struct{}, accounts map[common.Hash][]byte, storage map[common.Hash]map[common.Hash][]byte) {
fnList := pl.Lookup("StateUpdate", func(item interface{}) bool {
_, ok := item.(func(core.Hash, core.Hash, map[core.Hash]struct{}, map[core.Hash][]byte, map[core.Hash]map[core.Hash][]byte))
_, ok := item.(func(common.Hash, common.Hash, map[common.Hash]struct{}, map[common.Hash][]byte, map[common.Hash]map[common.Hash][]byte))
return ok
})
coreDestructs := make(map[core.Hash]struct{})
for k, v := range destructs {
coreDestructs[core.Hash(k)] = v
}
coreAccounts := make(map[core.Hash][]byte)
for k, v := range accounts {
coreAccounts[core.Hash(k)] = v
}
coreStorage := make(map[core.Hash]map[core.Hash][]byte)
for k, v := range storage {
coreStorage[core.Hash(k)] = make(map[core.Hash][]byte)
for h, d := range v {
coreStorage[core.Hash(k)][core.Hash(h)] = d
}
}
for _, fni := range fnList {
if fn, ok := fni.(func(core.Hash, core.Hash, map[core.Hash]struct{}, map[core.Hash][]byte, map[core.Hash]map[core.Hash][]byte)); ok {
fn(blockRoot, parentRoot, destructs, accounts, storage)
fn(core.Hash(blockRoot), core.Hash(parentRoot), coreDestructs, coreAccounts, coreStorage)
}
}
}
func pluginStateUpdate(blockRoot, parentRoot core.Hash, destructs map[core.Hash]struct{}, accounts map[core.Hash][]byte, storage map[core.Hash]map[core.Hash][]byte) {
func pluginStateUpdate(blockRoot, parentRoot common.Hash, destructs map[common.Hash]struct{}, accounts map[common.Hash][]byte, storage map[common.Hash]map[common.Hash][]byte) {
if plugins.DefaultPluginLoader == nil {
log.Warn("Attempting StateUpdate, but default PluginLoader has not been initialized")
return

View File

@ -145,7 +145,7 @@ func New(stack *node.Node, config *ethconfig.Config) (*Ethereum, error) {
chainDb: chainDb,
eventMux: stack.EventMux(),
accountManager: stack.AccountManager(),
engine: pluginCreateConsensusEngine(stack, chainConfig, &ethashConfig, config.Miner.Notify, config.Miner.Noverify, chainDb),
engine: ethconfig.CreateConsensusEngine(stack, chainConfig, &ethashConfig, config.Miner.Notify, config.Miner.Noverify, chainDb),
closeBloomHandler: make(chan struct{}),
networkID: config.NetworkId,
gasPrice: config.Miner.GasPrice,

4
go.mod
View File

@ -49,7 +49,7 @@ require (
github.com/naoina/go-stringutil v0.1.0 // indirect
github.com/naoina/toml v0.1.2-0.20170918210437-9fafd6967416
github.com/olekukonko/tablewriter v0.0.5
github.com/openrelayxyz/plugeth-utils v0.0.1
github.com/openrelayxyz/plugeth-utils v0.0.3
github.com/peterh/liner v1.1.1-0.20190123174540-a2c9a5303de7
github.com/prometheus/tsdb v0.7.1
github.com/rjeczalik/notify v0.9.1
@ -70,3 +70,5 @@ require (
gopkg.in/urfave/cli.v1 v1.20.0
gotest.tools v2.2.0+incompatible // indirect
)
replace github.com/openrelayxyz/plugeth-utils => /home/philip/src/rivet/plugeth-utils

View File

@ -2,24 +2,75 @@ package wrappers
import (
"context"
"math/big"
"encoding/json"
"math/big"
"sync"
"github.com/ethereum/go-ethereum"
"github.com/ethereum/go-ethereum/log"
"github.com/ethereum/go-ethereum/rlp"
"github.com/ethereum/go-ethereum/rpc"
"github.com/ethereum/go-ethereum/event"
"github.com/ethereum/go-ethereum/node"
"github.com/ethereum/go-ethereum/eth/downloader"
"github.com/ethereum/go-ethereum/common"
gcore "github.com/ethereum/go-ethereum/core"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/core/vm"
"github.com/ethereum/go-ethereum/eth/downloader"
"github.com/ethereum/go-ethereum/event"
"github.com/ethereum/go-ethereum/log"
"github.com/ethereum/go-ethereum/node"
"github.com/ethereum/go-ethereum/plugins/interfaces"
"github.com/ethereum/go-ethereum/rlp"
"github.com/ethereum/go-ethereum/rpc"
"github.com/openrelayxyz/plugeth-utils/core"
"github.com/openrelayxyz/plugeth-utils/restricted"
"sync"
)
type WrapedScopeContext struct {
s *vm.ScopeContext
}
func (w *WrapedScopeContext) Memory() core.Memory {
return w.s.Memory
}
func (w *WrapedScopeContext) Stack() core.Stack {
return w.s.Stack
}
// type Contract interface { <= this is the core.Contract
// AsDelegate() Contract
// GetOp(n uint64) OpCode
// GetByte(n uint64) byte
// Caller() Address
// Address() Address
// Value() *big.Int
// }
type WrappedContract struct {
c *vm.Contract
}
func (w WrappedContract) AsDelegate() core.Contract {
return WrappedContract{w.c.AsDelegate()}
}
func (w WrappedContract) GetOp(n uint64) core.OpCode {
return core.OpCode(w.c.GetOp(n))
}
func (w WrappedContract) GetByte(n uint64) byte {
return w.c.GetByte(n)
}
func (w WrappedContract) Caller() core.Address {
return core.Address(w.c.Caller())
}
func (w WrappedContract) Address() core.Address {
return core.Address(w.c.Address())
}
func (w WrappedContract) Value() *big.Int {
return w.c.Value()
}
type Node struct {
n *node.Node
}
@ -96,12 +147,16 @@ func (b *Backend) SetHead(number uint64) {
}
func (b *Backend) HeaderByNumber(ctx context.Context, number int64) ([]byte, error) {
header, err := b.b.HeaderByNumber(ctx, rpc.BlockNumber(number))
if err != nil { return nil, err }
if err != nil {
return nil, err
}
return rlp.EncodeToBytes(header)
}
func (b *Backend) HeaderByHash(ctx context.Context, hash core.Hash) ([]byte, error) {
header, err := b.b.HeaderByHash(ctx, common.Hash(hash))
if err != nil { return nil, err }
if err != nil {
return nil, err
}
return rlp.EncodeToBytes(header)
}
func (b *Backend) CurrentHeader() []byte {
@ -114,17 +169,23 @@ func (b *Backend) CurrentBlock() []byte {
}
func (b *Backend) BlockByNumber(ctx context.Context, number int64) ([]byte, error) {
block, err := b.b.BlockByNumber(ctx, rpc.BlockNumber(number))
if err != nil { return nil, err }
if err != nil {
return nil, err
}
return rlp.EncodeToBytes(block)
}
func (b *Backend) BlockByHash(ctx context.Context, hash core.Hash) ([]byte, error) {
block, err := b.b.BlockByHash(ctx, common.Hash(hash))
if err != nil { return nil, err }
if err != nil {
return nil, err
}
return rlp.EncodeToBytes(block)
}
func (b *Backend) GetReceipts(ctx context.Context, hash core.Hash) ([]byte, error) {
receipts, err := b.b.GetReceipts(ctx, common.Hash(hash))
if err != nil { return nil, err }
if err != nil {
return nil, err
}
return json.Marshal(receipts)
}
func (b *Backend) GetTd(ctx context.Context, hash core.Hash) *big.Int {
@ -139,13 +200,17 @@ func (b *Backend) SendTx(ctx context.Context, signedTx []byte) error {
}
func (b *Backend) GetTransaction(ctx context.Context, txHash core.Hash) ([]byte, core.Hash, uint64, uint64, error) { // RLP Encoded transaction {
tx, blockHash, blockNumber, index, err := b.b.GetTransaction(ctx, common.Hash(txHash))
if err != nil { return nil, core.Hash(blockHash), blockNumber, index, err }
if err != nil {
return nil, core.Hash(blockHash), blockNumber, index, err
}
enc, err := tx.MarshalBinary()
return enc, core.Hash(blockHash), blockNumber, index, err
}
func (b *Backend) GetPoolTransactions() ([][]byte, error) {
txs, err := b.b.GetPoolTransactions()
if err != nil { return nil, err }
if err != nil {
return nil, err
}
results := make([][]byte, len(txs))
for i, tx := range txs {
results[i], _ = rlp.EncodeToBytes(tx)
@ -154,7 +219,9 @@ func (b *Backend) GetPoolTransactions() ([][]byte, error) {
}
func (b *Backend) GetPoolTransaction(txHash core.Hash) []byte {
tx := b.b.GetPoolTransaction(common.Hash(txHash))
if tx == nil { return []byte{} }
if tx == nil {
return []byte{}
}
enc, _ := rlp.EncodeToBytes(tx)
return enc
}
@ -186,7 +253,9 @@ func (b *Backend) BloomStatus() (uint64, uint64) {
}
func (b *Backend) GetLogs(ctx context.Context, blockHash core.Hash) ([][]byte, error) {
logs, err := b.b.GetLogs(ctx, common.Hash(blockHash))
if err != nil { return nil, err }
if err != nil {
return nil, err
}
encLogs := make([][]byte, len(logs))
for i, log := range logs {
encLogs[i], _ = rlp.EncodeToBytes(log)
@ -194,7 +263,6 @@ func (b *Backend) GetLogs(ctx context.Context, blockHash core.Hash) ([][]byte, e
return encLogs, nil
} // []RLP encoded logs
type dl struct {
dl *downloader.Downloader
}