Roy Crihfield
9f967abfb9
refactor packages, flags, subscriptions also DRY up builder tests use mockgen
109 lines
2.8 KiB
Go
109 lines
2.8 KiB
Go
package statediff
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"math/big"
|
|
|
|
"github.com/ethereum/go-ethereum/common"
|
|
"github.com/ethereum/go-ethereum/core"
|
|
"github.com/ethereum/go-ethereum/core/types"
|
|
"github.com/ethereum/go-ethereum/event"
|
|
|
|
plugeth "github.com/openrelayxyz/plugeth-utils/core"
|
|
// "github.com/openrelayxyz/plugeth-utils/restricted/types"
|
|
|
|
"github.com/cerc-io/plugeth-statediff/utils"
|
|
)
|
|
|
|
type blockChain interface {
|
|
// SubscribeChainEvent(ch chan<- plugeth.ChainEvent) plugeth.Subscription
|
|
SubscribeChainEvent(ch chan<- core.ChainEvent) event.Subscription
|
|
CurrentBlock() *types.Header
|
|
GetBlockByHash(hash common.Hash) *types.Block
|
|
GetBlockByNumber(number uint64) *types.Block
|
|
GetReceiptsByHash(hash common.Hash) types.Receipts
|
|
GetTd(hash common.Hash, number uint64) *big.Int
|
|
// TODO LockTrie is never used
|
|
// UnlockTrie(root core.Hash)
|
|
StateCache() StateView
|
|
}
|
|
|
|
// Adapts the plugeth Backend to the blockChain interface
|
|
type backendBlockChain struct {
|
|
plugeth.Backend
|
|
ctx context.Context
|
|
// middleware?
|
|
}
|
|
|
|
type backendStateView struct {
|
|
plugeth.Backend
|
|
}
|
|
|
|
func asBlockChain(backend plugeth.Backend) blockChain {
|
|
return backendBlockChain{
|
|
Backend: backend,
|
|
ctx: context.Background(),
|
|
}
|
|
}
|
|
|
|
func (b backendBlockChain) SubscribeChainEvent(ch chan<- core.ChainEvent) event.Subscription {
|
|
bufferChan := make(chan plugeth.ChainEvent, chainEventChanSize)
|
|
sub := b.Backend.SubscribeChainEvent(bufferChan)
|
|
go func() {
|
|
for event := range bufferChan {
|
|
block := utils.MustDecode[types.Block](event.Block)
|
|
// TODO: apparently we ignore the logs
|
|
// logs := utils.MustDecode[types.Log](chainEvent.Logs)
|
|
ch <- core.ChainEvent{
|
|
Block: block,
|
|
Hash: common.Hash(event.Hash),
|
|
}
|
|
}
|
|
}()
|
|
return sub
|
|
}
|
|
|
|
func (b backendBlockChain) CurrentBlock() *types.Header {
|
|
buf := b.Backend.CurrentBlock()
|
|
return utils.MustDecode[types.Header](buf)
|
|
}
|
|
|
|
func (b backendBlockChain) GetBlockByHash(hash common.Hash) *types.Block {
|
|
buf, err := b.Backend.BlockByHash(b.ctx, plugeth.Hash(hash))
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
return utils.MustDecode[types.Block](buf)
|
|
}
|
|
|
|
func (b backendBlockChain) GetBlockByNumber(number uint64) *types.Block {
|
|
buf, err := b.Backend.BlockByNumber(b.ctx, int64(number))
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
return utils.MustDecode[types.Block](buf)
|
|
}
|
|
|
|
func (b backendBlockChain) GetReceiptsByHash(hash common.Hash) types.Receipts {
|
|
buf, err := b.Backend.GetReceipts(b.ctx, plugeth.Hash(hash))
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
var receipts types.Receipts
|
|
err = json.Unmarshal(buf, &receipts)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
return receipts
|
|
}
|
|
|
|
func (b backendBlockChain) GetTd(hash common.Hash, number uint64) *big.Int {
|
|
return b.Backend.GetTd(b.ctx, plugeth.Hash(hash))
|
|
}
|
|
|
|
func (b backendBlockChain) StateCache() StateView {
|
|
// TODO
|
|
return nil
|
|
}
|