Skip to content

Commit

Permalink
Merge branch 'master' into multiple-producers-redisstream
Browse files Browse the repository at this point in the history
  • Loading branch information
ganeshvanahalli authored Aug 27, 2024
2 parents 2a3dc1d + c424cc0 commit ecd2722
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 21 deletions.
2 changes: 1 addition & 1 deletion cmd/nitro/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -558,7 +558,7 @@ func openInitializeChainDb(ctx context.Context, stack *node.Node, config *NodeCo
startBlockHash = latestBlock.Hash()
}
log.Info("Starting or continuing rebuilding of wasm store", "codeHash", position, "startBlockHash", startBlockHash)
if err := gethexec.RebuildWasmStore(ctx, wasmDb, chainDb, config.Execution.RPC.MaxRecreateStateDepth, l2BlockChain, position, startBlockHash); err != nil {
if err := gethexec.RebuildWasmStore(ctx, wasmDb, chainDb, config.Execution.RPC.MaxRecreateStateDepth, &config.Execution.StylusTarget, l2BlockChain, position, startBlockHash); err != nil {
return nil, nil, fmt.Errorf("error rebuilding of wasm store: %w", err)
}
}
Expand Down
15 changes: 11 additions & 4 deletions execution/gethexec/executionengine.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,10 +150,7 @@ func (s *ExecutionEngine) MarkFeedStart(to arbutil.MessageIndex) {
}
}

func (s *ExecutionEngine) Initialize(rustCacheSize uint32, targetConfig *StylusTargetConfig) error {
if rustCacheSize != 0 {
programs.ResizeWasmLruCache(rustCacheSize)
}
func populateStylusTargetCache(targetConfig *StylusTargetConfig) error {
var effectiveStylusTarget string
target := rawdb.LocalTarget()
switch target {
Expand All @@ -171,6 +168,16 @@ func (s *ExecutionEngine) Initialize(rustCacheSize uint32, targetConfig *StylusT
return nil
}

func (s *ExecutionEngine) Initialize(rustCacheSize uint32, targetConfig *StylusTargetConfig) error {
if rustCacheSize != 0 {
programs.ResizeWasmLruCache(rustCacheSize)
}
if err := populateStylusTargetCache(targetConfig); err != nil {
return fmt.Errorf("error populating stylus target cache: %w", err)
}
return nil
}

func (s *ExecutionEngine) SetRecorder(recorder *BlockRecorder) {
if s.Started() {
panic("trying to set recorder after start")
Expand Down
31 changes: 23 additions & 8 deletions execution/gethexec/stylus_tracer.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package gethexec

import (
"encoding/json"
"errors"
"fmt"
"math/big"
"strings"
Expand All @@ -14,7 +15,8 @@ import (
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/core/vm"
"github.com/ethereum/go-ethereum/eth/tracers"
"github.com/offchainlabs/nitro/util/stack"
"github.com/ethereum/go-ethereum/log"
"github.com/offchainlabs/nitro/util/containers"
)

func init() {
Expand All @@ -24,8 +26,8 @@ func init() {
// stylusTracer captures Stylus HostIOs and returns them in a structured format to be used in Cargo
// Stylus Replay.
type stylusTracer struct {
open *stack.Stack[HostioTraceInfo]
stack *stack.Stack[*stack.Stack[HostioTraceInfo]]
open *containers.Stack[HostioTraceInfo]
stack *containers.Stack[*containers.Stack[HostioTraceInfo]]
interrupt atomic.Bool
reason error
}
Expand Down Expand Up @@ -55,7 +57,7 @@ type HostioTraceInfo struct {
Address *common.Address `json:"address,omitempty"`

// For *call HostIOs, the steps performed by the called contract.
Steps *stack.Stack[HostioTraceInfo] `json:"steps,omitempty"`
Steps *containers.Stack[HostioTraceInfo] `json:"steps,omitempty"`
}

// nestsHostios contains the hostios with nested calls.
Expand All @@ -67,8 +69,8 @@ var nestsHostios = map[string]bool{

func newStylusTracer(ctx *tracers.Context, _ json.RawMessage) (tracers.Tracer, error) {
return &stylusTracer{
open: stack.NewStack[HostioTraceInfo](),
stack: stack.NewStack[*stack.Stack[HostioTraceInfo]](),
open: containers.NewStack[HostioTraceInfo](),
stack: containers.NewStack[*containers.Stack[HostioTraceInfo]](),
}, nil
}

Expand Down Expand Up @@ -126,7 +128,7 @@ func (t *stylusTracer) CaptureEnter(typ vm.OpCode, from common.Address, to commo
name = "evm_self_destruct"
}

inner := stack.NewStack[HostioTraceInfo]()
inner := containers.NewStack[HostioTraceInfo]()
info := HostioTraceInfo{
Name: name,
Address: &to,
Expand All @@ -152,9 +154,22 @@ func (t *stylusTracer) GetResult() (json.RawMessage, error) {
if t.reason != nil {
return nil, t.reason
}

var internalErr error
if t.open == nil {
return nil, fmt.Errorf("trace is nil")
internalErr = errors.Join(internalErr, fmt.Errorf("tracer.open is nil"))
}
if t.stack == nil {
internalErr = errors.Join(internalErr, fmt.Errorf("tracer.stack is nil"))
}
if !t.stack.Empty() {
internalErr = errors.Join(internalErr, fmt.Errorf("tracer.stack should be empty, but has %d values", t.stack.Len()))
}
if internalErr != nil {
log.Error("stylusTracer: internal error when generating a trace", "error", internalErr)
return nil, fmt.Errorf("internal error: %w", internalErr)
}

msg, err := json.Marshal(t.open)
if err != nil {
return nil, err
Expand Down
7 changes: 6 additions & 1 deletion execution/gethexec/wasmstorerebuilder.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,14 @@ func WriteToKeyValueStore[T any](store ethdb.KeyValueStore, key []byte, val T) e
// It also stores a special value that is only set once when rebuilding commenced in RebuildingStartBlockHashKey as the block
// time of the latest block when rebuilding was first called, this is used to avoid recomputing of assembly and module of
// contracts that were created after rebuilding commenced since they would anyway already be added during sync.
func RebuildWasmStore(ctx context.Context, wasmStore ethdb.KeyValueStore, chainDb ethdb.Database, maxRecreateStateDepth int64, l2Blockchain *core.BlockChain, position, rebuildingStartBlockHash common.Hash) error {
func RebuildWasmStore(ctx context.Context, wasmStore ethdb.KeyValueStore, chainDb ethdb.Database, maxRecreateStateDepth int64, targetConfig *StylusTargetConfig, l2Blockchain *core.BlockChain, position, rebuildingStartBlockHash common.Hash) error {
var err error
var stateDb *state.StateDB

if err := populateStylusTargetCache(targetConfig); err != nil {
return fmt.Errorf("error populating stylus target cache: %w", err)
}

latestHeader := l2Blockchain.CurrentBlock()
// Attempt to get state at the start block when rebuilding commenced, if not available (in case of non-archival nodes) use latest state
rebuildingStartHeader := l2Blockchain.GetHeaderByHash(rebuildingStartBlockHash)
Expand Down
3 changes: 2 additions & 1 deletion system_tests/program_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1896,7 +1896,8 @@ func TestWasmStoreRebuilding(t *testing.T) {

// Start rebuilding and wait for it to finish
log.Info("starting rebuilding of wasm store")
Require(t, gethexec.RebuildWasmStore(ctx, wasmDbAfterDelete, nodeB.ExecNode.ChainDB, nodeB.ExecNode.ConfigFetcher().RPC.MaxRecreateStateDepth, bc, common.Hash{}, bc.CurrentBlock().Hash()))
execConfig := nodeB.ExecNode.ConfigFetcher()
Require(t, gethexec.RebuildWasmStore(ctx, wasmDbAfterDelete, nodeB.ExecNode.ChainDB, execConfig.RPC.MaxRecreateStateDepth, &execConfig.StylusTarget, bc, common.Hash{}, bc.CurrentBlock().Hash()))

wasmDbAfterRebuild := nodeB.ExecNode.Backend.ArbInterface().BlockChain().StateCache().WasmStore()

Expand Down
8 changes: 4 additions & 4 deletions system_tests/stylus_tracer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
"github.com/google/go-cmp/cmp"
"github.com/offchainlabs/nitro/execution/gethexec"
"github.com/offchainlabs/nitro/solgen/go/mocksgen"
"github.com/offchainlabs/nitro/util/stack"
"github.com/offchainlabs/nitro/util/containers"
"github.com/offchainlabs/nitro/util/testhelpers"
)

Expand Down Expand Up @@ -89,7 +89,7 @@ func TestStylusTracer(t *testing.T) {
Args: append(stylusMulticall.Bytes(), common.Hex2Bytes("ffffffffffffffff000000000000000000000000000000000000000000000000000000000000000000")...),
Outs: common.Hex2Bytes("0000000000"),
Address: &stylusMulticall,
Steps: (*stack.Stack[gethexec.HostioTraceInfo])(&[]gethexec.HostioTraceInfo{
Steps: (*containers.Stack[gethexec.HostioTraceInfo])(&[]gethexec.HostioTraceInfo{
{Name: "user_entrypoint", Args: intToBe32(1)},
{Name: "pay_for_memory_grow", Args: []byte{0x00, 0x01}},
{Name: "read_args", Outs: []byte{0x00}},
Expand Down Expand Up @@ -117,7 +117,7 @@ func TestStylusTracer(t *testing.T) {
Args: append(evmMulticall.Bytes(), common.Hex2Bytes("ffffffffffffffff000000000000000000000000000000000000000000000000000000000000000000")...),
Outs: common.Hex2Bytes("0000000000"),
Address: &evmMulticall,
Steps: stack.NewStack[gethexec.HostioTraceInfo](),
Steps: containers.NewStack[gethexec.HostioTraceInfo](),
},
{Name: "storage_flush_cache", Args: []byte{0x00}},
{Name: "write_result"},
Expand All @@ -133,7 +133,7 @@ func TestStylusTracer(t *testing.T) {
{
Name: "evm_call_contract",
Address: &stylusMulticall,
Steps: (*stack.Stack[gethexec.HostioTraceInfo])(&[]gethexec.HostioTraceInfo{
Steps: (*containers.Stack[gethexec.HostioTraceInfo])(&[]gethexec.HostioTraceInfo{
{Name: "user_entrypoint", Args: intToBe32(1)},
{Name: "pay_for_memory_grow", Args: []byte{0x00, 0x01}},
{Name: "read_args", Outs: []byte{0x00}},
Expand Down
15 changes: 13 additions & 2 deletions util/stack/stack.go → util/containers/stack.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright 2024, Offchain Labs, Inc.
// For license information, see https://github.com/OffchainLabs/nitro/blob/master/LICENSE

package stack
package containers

import (
"fmt"
Expand All @@ -28,7 +28,7 @@ func (s *Stack[T]) Pop() (T, error) {
var zeroVal T
return zeroVal, fmt.Errorf("trying to pop nil stack")
}
if len(*s) == 0 {
if s.Empty() {
var zeroVal T
return zeroVal, fmt.Errorf("trying to pop empty stack")
}
Expand All @@ -37,3 +37,14 @@ func (s *Stack[T]) Pop() (T, error) {
*s = (*s)[:i]
return val, nil
}

func (s *Stack[T]) Empty() bool {
return s == nil || len(*s) == 0
}

func (s *Stack[T]) Len() int {
if s == nil {
return 0
}
return len(*s)
}

0 comments on commit ecd2722

Please sign in to comment.