erigon-pulse/cmd/devnet/scenarios/context.go
Mark Holt cc74e74a60
Devnet contract utils (#7928)
This request is extending the devnet functionality to more fully handle
contract processing by adding support for the following calls:
 * trace_call,
* trace_transaction
* debug_accountAt,
* eth_getCode
* eth_estimateGas
* eth_gasPrice

It also contains an initial rationalization of the devnet subscription
code to use the erigon client code directly rather than using its own
intermediate subscription management.

This is used to implement a general purpose block waiter - which can be
used in any scenario step - rather than being specific to transaction
processing.

This pull also contains an end to end tested sync processor for bor and
associated support services:
* Heimdall (supports sync event transfer)
* Faucet - allows the creation and funding of arbitary test specific
accounts (cross chain)

Notes and Caveats:
 
* Code generation for contracts requires `--evm-version paris`. For
chains which don't support push0 for solc over 0.8.19
* The bor log processing post the application of sync events causes a
panic - this will be the subject of a seperate smaller push as it is not
devnet specific
* The bor code seems to make repeated calls for the same sync events and
also reverts requests - this needs further investigation. This is the
behaviour of the current implementation and may be required - although
it does seem to generate repeat processing - which could be avoided.
2023-07-28 14:03:32 +01:00

154 lines
3.1 KiB
Go

package scenarios
import (
"context"
"sync"
"github.com/ledgerwatch/erigon/cmd/devnet/devnet"
)
type ctxKey int
const (
ckParams ctxKey = iota
)
func stepRunners(ctx context.Context) []*stepRunner {
return nil
}
func ContextValues(ctx context.Context) []context.Context {
if ctx == nil {
return nil
}
if compound, ok := ctx.(*CompoundContext); ok {
var contexts []context.Context
for context := range compound.contexts {
contexts = append(contexts, context)
}
return contexts
}
return []context.Context{ctx}
}
var empty struct{}
type CompoundContext struct {
context.Context
contexts map[context.Context]struct{}
mutex sync.RWMutex
}
func (join *CompoundContext) Err() error {
if join.Context.Err() != nil {
return join.Context.Err()
}
join.mutex.RLock()
defer join.mutex.RUnlock()
for context := range join.contexts {
if context.Err() != nil {
return context.Err()
}
}
return nil
}
func (join *CompoundContext) Value(key interface{}) interface{} {
join.mutex.RLock()
defer join.mutex.RUnlock()
for context := range join.contexts {
if value := context.Value(key); value != nil {
return value
}
}
return join.Context.Value(key)
}
var background = context.Background()
func JoinContexts(ctx context.Context, others ...context.Context) context.Context {
var join *CompoundContext
if ctx != nil {
if compound, ok := ctx.(*CompoundContext); ok {
join = &CompoundContext{compound.Context, map[context.Context]struct{}{}, sync.RWMutex{}}
compound.mutex.RLock()
for context := range compound.contexts {
join.contexts[context] = empty
}
compound.mutex.RUnlock()
} else {
join = &CompoundContext{background, map[context.Context]struct{}{ctx: empty}, sync.RWMutex{}}
}
} else {
join = &CompoundContext{background, map[context.Context]struct{}{}, sync.RWMutex{}}
}
for _, context := range others {
if compound, ok := context.(*CompoundContext); ok {
if compound.Context != background {
join.contexts[compound.Context] = empty
}
compound.mutex.RLock()
for context := range compound.contexts {
if context != background {
join.contexts[context] = empty
}
}
compound.mutex.RUnlock()
} else if context != nil && context != background {
join.contexts[context] = empty
}
}
return join
}
type Context interface {
devnet.Context
WithParam(name string, value interface{}) Context
}
type scenarioContext struct {
devnet.Context
}
func (c scenarioContext) WithParam(name string, value interface{}) Context {
return WithParam(c, name, value)
}
type Params map[string]interface{}
func WithParam(ctx context.Context, name string, value interface{}) Context {
if params, ok := ctx.Value(ckParams).(Params); ok {
params[name] = value
if ctx, ok := ctx.(scenarioContext); ok {
return ctx
}
return scenarioContext{devnet.AsContext(ctx)}
}
ctx = context.WithValue(ctx, ckParams, Params{name: value})
return scenarioContext{devnet.AsContext(ctx)}
}
func Param[P any](ctx context.Context, name string) (P, bool) {
if params, ok := ctx.Value(ckParams).(Params); ok {
if param, ok := params[name]; ok {
return param.(P), true
}
}
var p P
return p, false
}