Skip to content

Commit 9ed65e6

Browse files
authored
Merge branch 'master' into large_distance
2 parents 03f8a7e + a1606d2 commit 9ed65e6

File tree

13 files changed

+199
-86
lines changed

13 files changed

+199
-86
lines changed

arbnode/node.go

Lines changed: 82 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -285,19 +285,21 @@ type Node struct {
285285
}
286286

287287
type SnapSyncConfig struct {
288-
Enabled bool
289-
PrevBatchMessageCount uint64
290-
PrevDelayedRead uint64
291-
BatchCount uint64
292-
DelayedCount uint64
288+
Enabled bool
289+
PrevBatchMessageCount uint64
290+
PrevDelayedRead uint64
291+
BatchCount uint64
292+
DelayedCount uint64
293+
ParentChainAssertionBlock uint64
293294
}
294295

295296
var DefaultSnapSyncConfig = SnapSyncConfig{
296-
Enabled: false,
297-
PrevBatchMessageCount: 0,
298-
BatchCount: 0,
299-
DelayedCount: 0,
300-
PrevDelayedRead: 0,
297+
Enabled: false,
298+
PrevBatchMessageCount: 0,
299+
PrevDelayedRead: 0,
300+
BatchCount: 0,
301+
DelayedCount: 0,
302+
ParentChainAssertionBlock: 0,
301303
}
302304

303305
type ConfigFetcher interface {
@@ -596,7 +598,29 @@ func createNodeImpl(
596598
if err != nil {
597599
return nil, err
598600
}
599-
inboxReader, err := NewInboxReader(inboxTracker, l1client, l1Reader, new(big.Int).SetUint64(deployInfo.DeployedAt), delayedBridge, sequencerInbox, func() *InboxReaderConfig { return &configFetcher.Get().InboxReader })
601+
firstMessageBlock := new(big.Int).SetUint64(deployInfo.DeployedAt)
602+
if config.SnapSyncTest.Enabled {
603+
batchCount := config.SnapSyncTest.BatchCount
604+
delayedMessageNumber, err := exec.NextDelayedMessageNumber()
605+
if err != nil {
606+
return nil, err
607+
}
608+
if batchCount > delayedMessageNumber {
609+
batchCount = delayedMessageNumber
610+
}
611+
// Find the first block containing the batch count.
612+
// Subtract 1 to get the block before the needed batch count,
613+
// this is done to fetch previous batch metadata needed for snap sync.
614+
if batchCount > 0 {
615+
batchCount--
616+
}
617+
block, err := FindBlockContainingBatchCount(ctx, deployInfo.Bridge, l1client, config.SnapSyncTest.ParentChainAssertionBlock, batchCount)
618+
if err != nil {
619+
return nil, err
620+
}
621+
firstMessageBlock.SetUint64(block)
622+
}
623+
inboxReader, err := NewInboxReader(inboxTracker, l1client, l1Reader, firstMessageBlock, delayedBridge, sequencerInbox, func() *InboxReaderConfig { return &configFetcher.Get().InboxReader })
600624
if err != nil {
601625
return nil, err
602626
}
@@ -772,6 +796,53 @@ func createNodeImpl(
772796
}, nil
773797
}
774798

799+
func FindBlockContainingBatchCount(ctx context.Context, bridgeAddress common.Address, l1Client *ethclient.Client, parentChainAssertionBlock uint64, batchCount uint64) (uint64, error) {
800+
bridge, err := bridgegen.NewIBridge(bridgeAddress, l1Client)
801+
if err != nil {
802+
return 0, err
803+
}
804+
high := parentChainAssertionBlock
805+
low := uint64(0)
806+
reduceBy := uint64(100)
807+
if high > reduceBy {
808+
low = high - reduceBy
809+
}
810+
// Reduce high and low by 100 until lowNode.InboxMaxCount < batchCount
811+
// This will give us a range (low to high) of blocks that contain the batch count.
812+
for low > 0 {
813+
lowCount, err := bridge.SequencerMessageCount(&bind.CallOpts{Context: ctx, BlockNumber: new(big.Int).SetUint64(low)})
814+
if err != nil {
815+
return 0, err
816+
}
817+
if lowCount.Uint64() > batchCount {
818+
high = low
819+
reduceBy = reduceBy * 2
820+
if low > reduceBy {
821+
low = low - reduceBy
822+
} else {
823+
low = 0
824+
}
825+
} else {
826+
break
827+
}
828+
}
829+
// Then binary search between low and high to find the block containing the batch count.
830+
for low < high {
831+
mid := low + (high-low)/2
832+
833+
midCount, err := bridge.SequencerMessageCount(&bind.CallOpts{Context: ctx, BlockNumber: new(big.Int).SetUint64(mid)})
834+
if err != nil {
835+
return 0, err
836+
}
837+
if midCount.Uint64() < batchCount {
838+
low = mid + 1
839+
} else {
840+
high = mid
841+
}
842+
}
843+
return low, nil
844+
}
845+
775846
func (n *Node) OnConfigReload(_ *Config, _ *Config) error {
776847
// TODO
777848
return nil

arbos/arbosState/arbosstate.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ import (
1313
"github.com/ethereum/go-ethereum/core/rawdb"
1414
"github.com/ethereum/go-ethereum/core/state"
1515
"github.com/ethereum/go-ethereum/core/vm"
16-
"github.com/ethereum/go-ethereum/log"
1716
"github.com/ethereum/go-ethereum/params"
1817
"github.com/ethereum/go-ethereum/triedb"
1918
"github.com/ethereum/go-ethereum/triedb/hashdb"
@@ -123,13 +122,13 @@ func NewArbosMemoryBackedArbOSState() (*ArbosState, *state.StateDB) {
123122
db := state.NewDatabaseWithConfig(raw, trieConfig)
124123
statedb, err := state.New(common.Hash{}, db, nil)
125124
if err != nil {
126-
log.Crit("failed to init empty statedb", "error", err)
125+
panic("failed to init empty statedb: " + err.Error())
127126
}
128127
burner := burn.NewSystemBurner(nil, false)
129128
chainConfig := chaininfo.ArbitrumDevTestChainConfig()
130129
newState, err := InitializeArbosState(statedb, burner, chainConfig, arbostypes.TestInitMessage)
131130
if err != nil {
132-
log.Crit("failed to open the ArbOS state", "error", err)
131+
panic("failed to open the ArbOS state: " + err.Error())
133132
}
134133
return newState, statedb
135134
}
@@ -139,7 +138,7 @@ func ArbOSVersion(stateDB vm.StateDB) uint64 {
139138
backingStorage := storage.NewGeth(stateDB, burn.NewSystemBurner(nil, false))
140139
arbosVersion, err := backingStorage.GetUint64ByUint64(uint64(versionOffset))
141140
if err != nil {
142-
log.Crit("failed to get the ArbOS version", "error", err)
141+
panic("failed to get the ArbOS version: " + err.Error())
143142
}
144143
return arbosVersion
145144
}

arbos/arbosState/initialize.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ func InitializeArbosInDatabase(db ethdb.Database, cacheConfig *core.CacheConfig,
6666
}()
6767
statedb, err := state.New(common.Hash{}, stateDatabase, nil)
6868
if err != nil {
69-
log.Crit("failed to init empty statedb", "error", err)
69+
panic("failed to init empty statedb :" + err.Error())
7070
}
7171

7272
noStateTrieChangesToCommitError := regexp.MustCompile("^triedb layer .+ is disk layer$")
@@ -96,7 +96,7 @@ func InitializeArbosInDatabase(db ethdb.Database, cacheConfig *core.CacheConfig,
9696
burner := burn.NewSystemBurner(nil, false)
9797
arbosState, err := InitializeArbosState(statedb, burner, chainConfig, initMessage)
9898
if err != nil {
99-
log.Crit("failed to open the ArbOS state", "error", err)
99+
panic("failed to open the ArbOS state :" + err.Error())
100100
}
101101

102102
chainOwner, err := initData.GetChainOwner()

arbos/programs/api.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,13 @@
44
package programs
55

66
import (
7+
"strconv"
8+
79
"github.com/holiman/uint256"
810

911
"github.com/ethereum/go-ethereum/common"
1012
"github.com/ethereum/go-ethereum/core/types"
1113
"github.com/ethereum/go-ethereum/core/vm"
12-
"github.com/ethereum/go-ethereum/log"
1314
"github.com/ethereum/go-ethereum/params"
1415

1516
"github.com/offchainlabs/nitro/arbos/util"
@@ -151,7 +152,7 @@ func newApiClosures(
151152
case vm.STATICCALL:
152153
ret, returnGas, err = evm.StaticCall(scope.Contract, contract, input, gas)
153154
default:
154-
log.Crit("unsupported call type", "opcode", opcode)
155+
panic("unsupported call type: " + opcode.String())
155156
}
156157

157158
interpreter.SetReturnData(ret)
@@ -266,7 +267,7 @@ func newApiClosures(
266267
original := input
267268

268269
crash := func(reason string) {
269-
log.Crit("bad API call", "reason", reason, "request", req, "len", len(original), "remaining", len(input))
270+
panic("bad API call reason: " + reason + " request: " + strconv.Itoa(int(req)) + " len: " + strconv.Itoa(len(original)) + " remaining: " + strconv.Itoa(len(input)))
270271
}
271272
takeInput := func(needed int, reason string) []byte {
272273
if len(input) < needed {
@@ -338,7 +339,7 @@ func newApiClosures(
338339
case StaticCall:
339340
opcode = vm.STATICCALL
340341
default:
341-
log.Crit("unsupported call type", "opcode", opcode)
342+
panic("unsupported call type opcode: " + opcode.String())
342343
}
343344
contract := takeAddress()
344345
value := takeU256()
@@ -414,8 +415,7 @@ func newApiClosures(
414415
captureHostio(name, args, outs, startInk, endInk)
415416
return []byte{}, nil, 0
416417
default:
417-
log.Crit("unsupported call type", "req", req)
418-
return []byte{}, nil, 0
418+
panic("unsupported call type: " + strconv.Itoa(int(req)))
419419
}
420420
}
421421
}

arbos/programs/native_api.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,11 @@ import "C"
2525

2626
import (
2727
"runtime"
28+
"strconv"
2829
"sync"
2930
"sync/atomic"
3031

3132
"github.com/ethereum/go-ethereum/core/vm"
32-
"github.com/ethereum/go-ethereum/log"
3333

3434
"github.com/offchainlabs/nitro/arbos/util"
3535
"github.com/offchainlabs/nitro/arbutil"
@@ -69,11 +69,11 @@ func newApi(
6969
func getApi(id usize) NativeApi {
7070
any, ok := apiObjects.Load(uintptr(id))
7171
if !ok {
72-
log.Crit("failed to load stylus Go API", "id", id)
72+
panic("failed to load stylus Go API id: " + strconv.Itoa(int(id)))
7373
}
7474
api, ok := any.(NativeApi)
7575
if !ok {
76-
log.Crit("wrong type for stylus Go API", "id", id)
76+
panic("wrong type for stylus Go API id: " + strconv.Itoa(int(id)))
7777
}
7878
return api
7979
}

arbos/programs/programs.go

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"github.com/ethereum/go-ethereum/core/state"
1414
"github.com/ethereum/go-ethereum/core/vm"
1515
"github.com/ethereum/go-ethereum/log"
16+
"github.com/ethereum/go-ethereum/metrics"
1617
gethParams "github.com/ethereum/go-ethereum/params"
1718

1819
"github.com/offchainlabs/nitro/arbcompress"
@@ -163,6 +164,21 @@ func (p Programs) ActivateProgram(evm *vm.EVM, address common.Address, arbosVers
163164
return stylusVersion, codeHash, info.moduleHash, dataFee, false, p.setProgram(codeHash, programData)
164165
}
165166

167+
func runModeToString(runmode core.MessageRunMode) string {
168+
switch runmode {
169+
case core.MessageCommitMode:
170+
return "commit_runmode"
171+
case core.MessageGasEstimationMode:
172+
return "gas_estimation_runmode"
173+
case core.MessageEthcallMode:
174+
return "eth_call_runmode"
175+
case core.MessageReplayMode:
176+
return "replay_runmode"
177+
default:
178+
return "unknown_runmode"
179+
}
180+
}
181+
166182
func (p Programs) CallProgram(
167183
scope *vm.ScopeContext,
168184
statedb vm.StateDB,
@@ -219,8 +235,7 @@ func (p Programs) CallProgram(
219235

220236
localAsm, err := getLocalAsm(statedb, moduleHash, contract.Address(), contract.Code, contract.CodeHash, params.PageLimit, evm.Context.Time, debugMode, program)
221237
if err != nil {
222-
log.Crit("failed to get local wasm for activated program", "program", contract.Address())
223-
return nil, err
238+
panic("failed to get local wasm for activated program: " + contract.Address().Hex())
224239
}
225240

226241
evmData := &EvmData{
@@ -250,17 +265,23 @@ func (p Programs) CallProgram(
250265
if runmode == core.MessageCommitMode {
251266
arbos_tag = statedb.Database().WasmCacheTag()
252267
}
268+
269+
metrics.GetOrRegisterCounter(fmt.Sprintf("arb/arbos/stylus/program_calls/%s", runModeToString(runmode)), nil).Inc(1)
253270
ret, err := callProgram(address, moduleHash, localAsm, scope, interpreter, tracingInfo, calldata, evmData, goParams, model, arbos_tag)
254271
if len(ret) > 0 && arbosVersion >= gethParams.ArbosVersion_StylusFixes {
255272
// Ensure that return data costs as least as much as it would in the EVM.
256273
evmCost := evmMemoryCost(uint64(len(ret)))
257274
if startingGas < evmCost {
258275
contract.Gas = 0
276+
// #nosec G115
277+
metrics.GetOrRegisterCounter(fmt.Sprintf("arb/arbos/stylus/gas_used/%s", runModeToString(runmode)), nil).Inc(int64(startingGas))
259278
return nil, vm.ErrOutOfGas
260279
}
261280
maxGasToReturn := startingGas - evmCost
262281
contract.Gas = am.MinInt(contract.Gas, maxGasToReturn)
263282
}
283+
// #nosec G115
284+
metrics.GetOrRegisterCounter(fmt.Sprintf("arb/arbos/stylus/gas_used/%s", runModeToString(runmode)), nil).Inc(int64(startingGas - contract.Gas))
264285
return ret, err
265286
}
266287

arbstate/inbox.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,11 +85,11 @@ func parseSequencerMessage(ctx context.Context, batchNum uint64, batchBlockHash
8585
// Matches the way keyset validation was done inside DAS readers i.e logging the error
8686
// But other daproviders might just want to return the error
8787
if errors.Is(err, daprovider.ErrSeqMsgValidation) && daprovider.IsDASMessageHeaderByte(payload[0]) {
88-
logLevel := log.Error
8988
if keysetValidationMode == daprovider.KeysetPanicIfInvalid {
90-
logLevel = log.Crit
89+
panic(err.Error())
90+
} else {
91+
log.Error(err.Error())
9192
}
92-
logLevel(err.Error())
9393
} else {
9494
return nil, err
9595
}

0 commit comments

Comments
 (0)