plugeth/plugins/wrappers/wrappers.go

271 lines
7.7 KiB
Go
Raw Normal View History

2021-08-31 20:29:09 +00:00
package wrappers
import (
2021-09-03 22:20:49 +00:00
"math/big"
"encoding/json"
2021-09-03 22:20:49 +00:00
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/state"
2021-09-03 22:20:49 +00:00
"github.com/ethereum/go-ethereum/core/vm"
"github.com/ethereum/go-ethereum/node"
2021-09-03 22:20:49 +00:00
"github.com/openrelayxyz/plugeth-utils/core"
2021-08-31 20:29:09 +00:00
)
type WrappedScopeContext struct {
2021-09-03 22:20:49 +00:00
s *vm.ScopeContext
}
func NewWrappedScopeContext(s *vm.ScopeContext) *WrappedScopeContext {
return &WrappedScopeContext{s}
}
func (w *WrappedScopeContext) Memory() core.Memory {
2021-09-03 22:20:49 +00:00
return w.s.Memory
}
func (w *WrappedScopeContext) Stack() core.Stack {
2021-09-03 22:20:49 +00:00
return w.s.Stack
}
func (w *WrappedScopeContext) Contract() core.Contract {
return &WrappedContract{w.s.Contract}
}
2021-09-03 22:20:49 +00:00
type WrappedContract struct {
c *vm.Contract
}
func (w *WrappedContract) AsDelegate() core.Contract {
return &WrappedContract{w.c.AsDelegate()}
2021-09-03 22:20:49 +00:00
}
func (w *WrappedContract) GetOp(n uint64) core.OpCode {
2021-09-03 22:20:49 +00:00
return core.OpCode(w.c.GetOp(n))
}
func (w *WrappedContract) GetByte(n uint64) byte {
2021-12-27 22:43:35 +00:00
return byte(w.c.GetOp(n))
2021-09-03 22:20:49 +00:00
}
func (w *WrappedContract) Caller() core.Address {
2021-09-03 22:20:49 +00:00
return core.Address(w.c.Caller())
}
func (w *WrappedContract) Address() core.Address {
2021-09-03 22:20:49 +00:00
return core.Address(w.c.Address())
}
func (w *WrappedContract) Value() *big.Int {
2021-09-03 22:20:49 +00:00
return w.c.Value()
}
func (w *WrappedContract) Input() []byte {
return w.c.Input
}
func (w *WrappedContract) Code() []byte {
return w.c.Code
}
// added UseGas bc compiler compained without it. Should investigate if the false return with effect performance.
// take this out of core.interface
func (w *WrappedContract) UseGas(gas uint64) (ok bool) {
return false
}
type WrappedTracer struct {
r core.TracerResult
}
func NewWrappedTracer(r core.TracerResult) *WrappedTracer {
return &WrappedTracer{r}
}
func (w WrappedTracer) CapturePreStart(from common.Address, to *common.Address, input []byte, gas uint64, value *big.Int) {
if v, ok := w.r.(core.PreTracer); ok {
v.CapturePreStart(core.Address(from), (*core.Address)(to), input, gas, value)}
}
func (w WrappedTracer) CaptureStart(env *vm.EVM, from common.Address, to common.Address, create bool, input []byte, gas uint64, value *big.Int) {
w.r.CaptureStart(core.Address(from), core.Address(to), create, input, gas, value)
}
2021-11-27 00:43:34 +00:00
func (w WrappedTracer) CaptureState(pc uint64, op vm.OpCode, gas, cost uint64, scope *vm.ScopeContext, rData []byte, depth int, err error) {
w.r.CaptureState(pc, core.OpCode(op), gas, cost, &WrappedScopeContext{scope}, rData, depth, err)
}
func (w WrappedTracer) CaptureEnter(typ vm.OpCode, from common.Address, to common.Address, input []byte, gas uint64, value *big.Int) {
w.r.CaptureEnter(core.OpCode(typ), core.Address(from), core.Address(to), input, gas, value)
}
func (w WrappedTracer) CaptureExit(output []byte, gasUsed uint64, err error) {
w.r.CaptureExit(output, gasUsed, err)
}
2021-11-27 00:43:34 +00:00
func (w WrappedTracer) CaptureFault(pc uint64, op vm.OpCode, gas, cost uint64, scope *vm.ScopeContext, depth int, err error) {
w.r.CaptureFault(pc, core.OpCode(op), gas, cost, &WrappedScopeContext{scope}, depth, err)
}
// passing zero as a dummy value is foundation PluGeth only, it is being done to preserve compatability with other networks
func (w WrappedTracer) CaptureEnd(output []byte, gasUsed uint64, err error) {
w.r.CaptureEnd(output, gasUsed, 0, err)
2021-11-27 00:43:34 +00:00
}
func (w WrappedTracer) GetResult() (json.RawMessage, error) {
data, err := w.r.Result()
if err != nil { return nil, err}
result, err := json.Marshal(data)
return json.RawMessage(result), err
}
func (w WrappedTracer) CaptureTxStart (gasLimit uint64) {}
func (w WrappedTracer) CaptureTxEnd (restGas uint64) {}
func (w WrappedTracer) Stop(err error) {}
type WrappedStateDB struct {
s *state.StateDB
}
func NewWrappedStateDB(d *state.StateDB) *WrappedStateDB {
return &WrappedStateDB{d}
}
// GetBalance(Address) *big.Int
func (w *WrappedStateDB) GetBalance(addr core.Address) *big.Int {
return w.s.GetBalance(common.Address(addr))
}
// GetNonce(Address) uint64
func (w *WrappedStateDB) GetNonce(addr core.Address) uint64 {
return w.s.GetNonce(common.Address(addr))
}
// GetCodeHash(Address) Hash
func (w *WrappedStateDB) GetCodeHash(addr core.Address) core.Hash {
return core.Hash(w.s.GetCodeHash(common.Address(addr)))
} // sort this out
// GetCode(Address) []byte
func (w *WrappedStateDB) GetCode(addr core.Address) []byte {
return w.s.GetCode(common.Address(addr))
}
// GetCodeSize(Address) int
func (w *WrappedStateDB) GetCodeSize(addr core.Address) int {
return w.s.GetCodeSize(common.Address(addr))
}
//GetRefund() uint64
func (w *WrappedStateDB) GetRefund() uint64 { //are we sure we want to include this? getting a refund seems like changing state
return w.s.GetRefund()
}
// GetCommittedState(Address, Hash) Hash
func (w *WrappedStateDB) GetCommittedState(addr core.Address, hsh core.Hash) core.Hash {
return core.Hash(w.s.GetCommittedState(common.Address(addr), common.Hash(hsh)))
}
// GetState(Address, Hash) Hash
func (w *WrappedStateDB) GetState(addr core.Address, hsh core.Hash) core.Hash {
return core.Hash(w.s.GetState(common.Address(addr), common.Hash(hsh)))
}
// HasSuicided(Address) bool
func (w *WrappedStateDB) HasSuicided(addr core.Address) bool {
return w.s.HasSelfDestructed(common.Address(addr))
}
// // Exist reports whether the given account exists in state.
// // Notably this should also return true for suicided accounts.
// Exist(Address) bool
func (w *WrappedStateDB) Exist(addr core.Address) bool {
return w.s.Exist(common.Address(addr))
}
// // Empty returns whether the given account is empty. Empty
// // is defined according to EIP161 (balance = nonce = code = 0).
// Empty(Address) bool
func (w *WrappedStateDB) Empty(addr core.Address) bool {
return w.s.Empty(common.Address(addr))
}
// AddressInAccessList(addr Address) bool
func (w *WrappedStateDB) AddressInAccessList(addr core.Address) bool {
return w.s.AddressInAccessList(common.Address(addr))
}
// SlotInAccessList(addr Address, slot Hash) (addressOk bool, slotOk bool)
func (w *WrappedStateDB) SlotInAccessList(addr core.Address, slot core.Hash) (addressOK, slotOk bool) {
return w.s.SlotInAccessList(common.Address(addr), common.Hash(slot))
}
// IntermediateRoot(deleteEmptyObjects bool) common.Hash
func (w *WrappedStateDB) IntermediateRoot(deleteEmptyObjects bool) core.Hash {
return core.Hash(w.s.IntermediateRoot(deleteEmptyObjects))
}
2023-10-10 21:32:52 +00:00
func (w *WrappedStateDB) AddBalance(addr core.Address, amount *big.Int) {
w.s.AddBalance(common.Address(addr), amount)
}
2021-08-31 20:29:09 +00:00
type Node struct {
2021-09-03 22:20:49 +00:00
n *node.Node
2021-08-31 20:29:09 +00:00
}
func NewNode(n *node.Node) *Node {
2021-09-03 22:20:49 +00:00
return &Node{n}
2021-08-31 20:29:09 +00:00
}
func (n *Node) Server() core.Server {
2021-09-15 20:09:43 +00:00
return n.n.Server()
2021-08-31 20:29:09 +00:00
}
func (n *Node) DataDir() string {
2021-09-03 22:20:49 +00:00
return n.n.DataDir()
2021-08-31 20:29:09 +00:00
}
func (n *Node) InstanceDir() string {
2021-09-03 22:20:49 +00:00
return n.n.InstanceDir()
2021-08-31 20:29:09 +00:00
}
func (n *Node) IPCEndpoint() string {
2021-09-03 22:20:49 +00:00
return n.n.IPCEndpoint()
2021-08-31 20:29:09 +00:00
}
func (n *Node) HTTPEndpoint() string {
2021-09-03 22:20:49 +00:00
return n.n.HTTPEndpoint()
2021-08-31 20:29:09 +00:00
}
func (n *Node) WSEndpoint() string {
2021-09-03 22:20:49 +00:00
return n.n.WSEndpoint()
2021-08-31 20:29:09 +00:00
}
func (n *Node) ResolvePath(x string) string {
2021-09-03 22:20:49 +00:00
return n.n.ResolvePath(x)
2021-08-31 20:29:09 +00:00
}
func (n *Node) Attach() (core.Client, error) {
return n.n.Attach(), nil
}
func (n *Node) Close() error {
return n.n.Close()
}
type WrappedBlockContext struct {
b vm.BlockContext
}
// type WrappedBlockContext vm.BlockContext
func NewWrappedBlockContext(c vm.BlockContext) *WrappedBlockContext {
return &WrappedBlockContext{c}
}
func (w *WrappedBlockContext) Coinbase() core.Address {
return core.Address(w.b.Coinbase)
}
func (w *WrappedBlockContext) GasLimit() uint64 {
return w.b.GasLimit
}
func (w *WrappedBlockContext) BlockNumber() *big.Int {
return w.b.BlockNumber
}
func (w *WrappedBlockContext) Time() *big.Int {
return new(big.Int).SetInt64(int64(w.b.Time))
}
func (w *WrappedBlockContext) Difficulty() *big.Int {
return w.b.Difficulty
}
func (w *WrappedBlockContext) BaseFee() *big.Int {
return w.b.BaseFee
}