From 7fe3d9fed5dea2c4e736732dee716a7afcf37cf6 Mon Sep 17 00:00:00 2001 From: Aman Sanghi Date: Fri, 26 Jul 2024 18:18:53 +0530 Subject: [PATCH 01/38] Support seamless switchover redis for sequencer coordinator --- arbnode/node.go | 2 +- arbnode/seq_coordinator.go | 66 +++++++++++++++++++++++-- system_tests/seq_coordinator_test.go | 74 ++++++++++++++++++++++++++++ util/redisutil/redis_coordinator.go | 1 + 4 files changed, 138 insertions(+), 5 deletions(-) diff --git a/arbnode/node.go b/arbnode/node.go index ac18a6c7d4..78b308387e 100644 --- a/arbnode/node.go +++ b/arbnode/node.go @@ -446,7 +446,7 @@ func createNodeImpl( } if config.SeqCoordinator.Enable { - coordinator, err = NewSeqCoordinator(dataSigner, bpVerifier, txStreamer, exec, syncMonitor, config.SeqCoordinator) + coordinator, err = NewSeqCoordinator(dataSigner, bpVerifier, txStreamer, exec, config.SeqCoordinator) if err != nil { return nil, err } diff --git a/arbnode/seq_coordinator.go b/arbnode/seq_coordinator.go index 98c19ce361..bee1a93704 100644 --- a/arbnode/seq_coordinator.go +++ b/arbnode/seq_coordinator.go @@ -38,6 +38,8 @@ type SeqCoordinator struct { stopwaiter.StopWaiter redisutil.RedisCoordinator + prevRedisCoordinator *redisutil.RedisCoordinator + prevRedisMessageCount arbutil.MessageIndex streamer *TransactionStreamer sequencer execution.ExecutionSequencer @@ -60,6 +62,7 @@ type SeqCoordinatorConfig struct { Enable bool `koanf:"enable"` ChosenHealthcheckAddr string `koanf:"chosen-healthcheck-addr"` RedisUrl string `koanf:"redis-url"` + NewRedisUrl string `koanf:"new-redis-url"` LockoutDuration time.Duration `koanf:"lockout-duration"` LockoutSpare time.Duration `koanf:"lockout-spare"` SeqNumDuration time.Duration `koanf:"seq-num-duration"` @@ -84,6 +87,7 @@ func (c *SeqCoordinatorConfig) Url() string { func SeqCoordinatorConfigAddOptions(prefix string, f *flag.FlagSet) { f.Bool(prefix+".enable", DefaultSeqCoordinatorConfig.Enable, "enable sequence coordinator") f.String(prefix+".redis-url", DefaultSeqCoordinatorConfig.RedisUrl, "the Redis URL to coordinate via") + f.String(prefix+".new-redis-url", DefaultSeqCoordinatorConfig.NewRedisUrl, "switch to the new Redis URL to coordinate via") f.String(prefix+".chosen-healthcheck-addr", DefaultSeqCoordinatorConfig.ChosenHealthcheckAddr, "if non-empty, launch an HTTP service binding to this address that returns status code 200 when chosen and 503 otherwise") f.Duration(prefix+".lockout-duration", DefaultSeqCoordinatorConfig.LockoutDuration, "") f.Duration(prefix+".lockout-spare", DefaultSeqCoordinatorConfig.LockoutSpare, "") @@ -102,6 +106,7 @@ var DefaultSeqCoordinatorConfig = SeqCoordinatorConfig{ Enable: false, ChosenHealthcheckAddr: "", RedisUrl: "", + NewRedisUrl: "", LockoutDuration: time.Minute, LockoutSpare: 30 * time.Second, SeqNumDuration: 24 * time.Hour, @@ -118,6 +123,7 @@ var DefaultSeqCoordinatorConfig = SeqCoordinatorConfig{ var TestSeqCoordinatorConfig = SeqCoordinatorConfig{ Enable: false, RedisUrl: "", + NewRedisUrl: "", LockoutDuration: time.Second * 2, LockoutSpare: time.Millisecond * 10, SeqNumDuration: time.Minute * 10, @@ -136,7 +142,6 @@ func NewSeqCoordinator( bpvalidator *contracts.AddressVerifier, streamer *TransactionStreamer, sequencer execution.ExecutionSequencer, - sync *SyncMonitor, config SeqCoordinatorConfig, ) (*SeqCoordinator, error) { redisCoordinator, err := redisutil.NewRedisCoordinator(config.RedisUrl) @@ -522,12 +527,28 @@ func (c *SeqCoordinator) update(ctx context.Context) time.Duration { log.Error("cannot read message count", "err", err) return c.config.UpdateInterval } + // Cache the previous redis coordinator's message count + if c.prevRedisCoordinator != nil && c.prevRedisMessageCount == 0 { + prevRemoteMsgCount, err := c.getRemoteMsgCountImpl(ctx, c.prevRedisCoordinator.Client) + if err != nil { + log.Warn("cannot get remote message count", "err", err) + return c.retryAfterRedisError() + } + c.prevRedisMessageCount = prevRemoteMsgCount + } remoteMsgCount, err := c.GetRemoteMsgCount() if err != nil { log.Warn("cannot get remote message count", "err", err) return c.retryAfterRedisError() } readUntil := remoteMsgCount + client := c.Client + // If we have a previous redis coordinator, + // we can read from it until the local message count catches up to the prev coordinator's message count + if c.prevRedisMessageCount > localMsgCount { + readUntil = c.prevRedisMessageCount + client = c.prevRedisCoordinator.Client + } if readUntil > localMsgCount+c.config.MsgPerPoll { readUntil = localMsgCount + c.config.MsgPerPoll } @@ -536,7 +557,7 @@ func (c *SeqCoordinator) update(ctx context.Context) time.Duration { var msgReadErr error for msgToRead < readUntil { var resString string - resString, msgReadErr = c.Client.Get(ctx, redisutil.MessageKeyFor(msgToRead)).Result() + resString, msgReadErr = client.Get(ctx, redisutil.MessageKeyFor(msgToRead)).Result() if msgReadErr != nil { log.Warn("coordinator failed reading message", "pos", msgToRead, "err", msgReadErr) break @@ -545,7 +566,7 @@ func (c *SeqCoordinator) update(ctx context.Context) time.Duration { var sigString string var sigBytes []byte sigSeparateKey := true - sigString, msgReadErr = c.Client.Get(ctx, redisutil.MessageSigKeyFor(msgToRead)).Result() + sigString, msgReadErr = client.Get(ctx, redisutil.MessageSigKeyFor(msgToRead)).Result() if errors.Is(msgReadErr, redis.Nil) { // no separate signature. Try reading old-style sig if len(rsBytes) < 32 { @@ -731,12 +752,49 @@ func (c *SeqCoordinator) launchHealthcheckServer(ctx context.Context) { func (c *SeqCoordinator) Start(ctxIn context.Context) { c.StopWaiter.Start(ctxIn, c) - c.CallIteratively(c.update) + c.CallIteratively(c.chooseRedisAndUpdate) if c.config.ChosenHealthcheckAddr != "" { c.StopWaiter.LaunchThread(c.launchHealthcheckServer) } } +func (c *SeqCoordinator) chooseRedisAndUpdate(ctx context.Context) time.Duration { + // If we have a new redis coordinator, and we haven't switched to it yet, try to switch. + if c.config.NewRedisUrl != "" && c.prevRedisCoordinator == nil { + // If we fail to try to switch, we'll retry soon. + if err := c.trySwitchingRedis(ctx); err != nil { + log.Warn("error while trying to switch redis coordinator", "err", err) + return c.retryAfterRedisError() + } + } + return c.update(ctx) +} + +func (c *SeqCoordinator) trySwitchingRedis(ctx context.Context) error { + current, err := c.Client.Get(ctx, redisutil.CHOSENSEQ_KEY).Result() + var wasEmpty bool + if errors.Is(err, redis.Nil) { + wasEmpty = true + err = nil + } + if err != nil { + log.Warn("failed to get current chosen sequencer", "err", err) + return err + } + // If the chosen key is set to switch, we need to switch to the new redis coordinator. + if !wasEmpty && (current == redisutil.SWITCHED_REDIS) { + newRedisCoordinator, err := redisutil.NewRedisCoordinator(c.config.NewRedisUrl) + if err != nil { + log.Warn("failed to create new redis coordinator", "err", + err, "newRedisUrl", c.config.NewRedisUrl) + return err + } + c.prevRedisCoordinator = &c.RedisCoordinator + c.RedisCoordinator = *newRedisCoordinator + } + return nil +} + // Calls check() every c.config.RetryInterval until it returns true, or the context times out. func (c *SeqCoordinator) waitFor(ctx context.Context, check func() bool) bool { for { diff --git a/system_tests/seq_coordinator_test.go b/system_tests/seq_coordinator_test.go index 1b8926a1b9..a93f287546 100644 --- a/system_tests/seq_coordinator_test.go +++ b/system_tests/seq_coordinator_test.go @@ -373,3 +373,77 @@ func TestRedisSeqCoordinatorMessageSync(t *testing.T) { func TestRedisSeqCoordinatorWrongKeyMessageSync(t *testing.T) { testCoordinatorMessageSync(t, false) } + +func TestRedisSwitchover(t *testing.T) { + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() + + builder := NewNodeBuilder(ctx).DefaultConfig(t, true) + builder.nodeConfig.SeqCoordinator.Enable = true + builder.nodeConfig.SeqCoordinator.RedisUrl = redisutil.CreateTestRedis(ctx, t) + builder.nodeConfig.SeqCoordinator.NewRedisUrl = redisutil.CreateTestRedis(ctx, t) + builder.nodeConfig.BatchPoster.Enable = false + + nodeNames := []string{"stdio://A", "stdio://B"} + initRedisForTest(t, ctx, builder.nodeConfig.SeqCoordinator.RedisUrl, nodeNames) + initRedisForTest(t, ctx, builder.nodeConfig.SeqCoordinator.NewRedisUrl, nodeNames) + builder.nodeConfig.SeqCoordinator.MyUrl = nodeNames[0] + + cleanup := builder.Build(t) + defer cleanup() + + redisClient, err := redisutil.RedisClientFromURL(builder.nodeConfig.SeqCoordinator.RedisUrl) + Require(t, err) + + // wait for sequencerA to become master + for { + err := redisClient.Get(ctx, redisutil.CHOSENSEQ_KEY).Err() + if errors.Is(err, redis.Nil) { + time.Sleep(builder.nodeConfig.SeqCoordinator.UpdateInterval) + continue + } + Require(t, err) + break + } + + builder.L2Info.GenerateAccount("User2") + + nodeConfigDup := *builder.nodeConfig + builder.nodeConfig = &nodeConfigDup + builder.nodeConfig.Feed.Output = *newBroadcasterConfigTest() + builder.nodeConfig.SeqCoordinator.MyUrl = nodeNames[1] + testClientB, cleanupB := builder.Build2ndNode(t, &SecondNodeParams{nodeConfig: builder.nodeConfig}) + defer cleanupB() + + verifyTxIsProcessed(t, ctx, builder, testClientB, 1e12) + + redisClient.Set(ctx, redisutil.CHOSENSEQ_KEY, redisutil.SWITCHED_REDIS, time.Duration(-1)) + + verifyTxIsProcessed(t, ctx, builder, testClientB, 1e12*2) + + // Wait for all messages to be processed, before closing old redisClient + time.Sleep(1 * time.Second) + err = redisClient.Close() + Require(t, err) + + verifyTxIsProcessed(t, ctx, builder, testClientB, 1e12*3) + +} + +func verifyTxIsProcessed(t *testing.T, ctx context.Context, builder *NodeBuilder, testClientB *TestClient, balance int64) { + tx := builder.L2Info.PrepareTx("Owner", "User2", builder.L2Info.TransferGas, big.NewInt(1e12), nil) + + err := builder.L2.Client.SendTransaction(ctx, tx) + Require(t, err) + + _, err = builder.L2.EnsureTxSucceeded(tx) + Require(t, err) + + _, err = WaitForTx(ctx, testClientB.Client, tx.Hash(), time.Second*5) + Require(t, err) + l2balance, err := testClientB.Client.BalanceAt(ctx, builder.L2Info.GetAddress("User2"), nil) + Require(t, err) + if l2balance.Cmp(big.NewInt(balance)) != 0 { + t.Fatal("Unexpected balance:", l2balance) + } +} diff --git a/util/redisutil/redis_coordinator.go b/util/redisutil/redis_coordinator.go index 59e3b0e0f9..cbb562d576 100644 --- a/util/redisutil/redis_coordinator.go +++ b/util/redisutil/redis_coordinator.go @@ -20,6 +20,7 @@ const WANTS_LOCKOUT_KEY_PREFIX string = "coordinator.liveliness." // Per server. const MESSAGE_KEY_PREFIX string = "coordinator.msg." // Per Message. Only written by sequencer holding CHOSEN const SIGNATURE_KEY_PREFIX string = "coordinator.msg.sig." // Per Message. Only written by sequencer holding CHOSEN const WANTS_LOCKOUT_VAL string = "OK" +const SWITCHED_REDIS string = "SWITCHED_REDIS" const INVALID_VAL string = "INVALID" const INVALID_URL string = "" From 6ec1de8f104525abe5c50b1feab4dbd7f85b57f4 Mon Sep 17 00:00:00 2001 From: Aman Sanghi Date: Tue, 6 Aug 2024 21:27:10 +0530 Subject: [PATCH 02/38] Changes based on PR comments --- arbnode/seq_coordinator.go | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/arbnode/seq_coordinator.go b/arbnode/seq_coordinator.go index bee1a93704..431ea43512 100644 --- a/arbnode/seq_coordinator.go +++ b/arbnode/seq_coordinator.go @@ -358,20 +358,20 @@ func (c *SeqCoordinator) GetRemoteMsgCount() (arbutil.MessageIndex, error) { return c.getRemoteMsgCountImpl(c.GetContext(), c.Client) } -func (c *SeqCoordinator) wantsLockoutUpdate(ctx context.Context) error { +func (c *SeqCoordinator) wantsLockoutUpdate(ctx context.Context, client redis.UniversalClient) error { c.wantsLockoutMutex.Lock() defer c.wantsLockoutMutex.Unlock() - return c.wantsLockoutUpdateWithMutex(ctx) + return c.wantsLockoutUpdateWithMutex(ctx, client) } // Requires the caller hold the wantsLockoutMutex -func (c *SeqCoordinator) wantsLockoutUpdateWithMutex(ctx context.Context) error { +func (c *SeqCoordinator) wantsLockoutUpdateWithMutex(ctx context.Context, client redis.UniversalClient) error { if c.avoidLockout > 0 { return nil } myWantsLockoutKey := redisutil.WantsLockoutKeyFor(c.config.Url()) wantsLockoutUntil := time.Now().Add(c.config.LockoutDuration) - pipe := c.Client.TxPipeline() + pipe := client.TxPipeline() initialDuration := c.config.LockoutDuration if initialDuration < 2*time.Second { initialDuration = 2 * time.Second @@ -657,7 +657,7 @@ func (c *SeqCoordinator) update(ctx context.Context) time.Duration { // this could be just new messages we didn't get yet - even then, we should retry soon log.Info("sequencer failed to become chosen", "err", err, "msgcount", localMsgCount) // make sure we're marked as wanting the lockout - if err := c.wantsLockoutUpdate(ctx); err != nil { + if err := c.wantsLockoutUpdate(ctx, c.Client); err != nil { log.Warn("failed to update wants lockout key", "err", err) } c.prevChosenSequencer = "" @@ -685,7 +685,7 @@ func (c *SeqCoordinator) update(ctx context.Context) time.Duration { // update wanting the lockout var wantsLockoutErr error if synced && !c.AvoidingLockout() { - wantsLockoutErr = c.wantsLockoutUpdate(ctx) + wantsLockoutErr = c.wantsLockoutUpdate(ctx, c.Client) } else { wantsLockoutErr = c.wantsLockoutRelease(ctx) } @@ -789,6 +789,14 @@ func (c *SeqCoordinator) trySwitchingRedis(ctx context.Context) error { err, "newRedisUrl", c.config.NewRedisUrl) return err } + err = c.wantsLockoutUpdate(ctx, c.Client) + if err != nil { + return err + } + err = c.wantsLockoutUpdate(ctx, newRedisCoordinator.Client) + if err != nil { + return err + } c.prevRedisCoordinator = &c.RedisCoordinator c.RedisCoordinator = *newRedisCoordinator } @@ -908,7 +916,7 @@ func (c *SeqCoordinator) SeekLockout(ctx context.Context) { log.Info("seeking lockout", "myUrl", c.config.Url()) if c.sequencer.Synced() { // Even if this errors we still internally marked ourselves as wanting the lockout - err := c.wantsLockoutUpdateWithMutex(ctx) + err := c.wantsLockoutUpdateWithMutex(ctx, c.Client) if err != nil { log.Warn("failed to set wants lockout key in redis after seeking lockout again", "err", err) } From e31fcecbd6538b718f1596348df4f8852fb284b2 Mon Sep 17 00:00:00 2001 From: Aman Sanghi Date: Fri, 30 Aug 2024 17:33:07 +0530 Subject: [PATCH 03/38] Changes based on PR comments --- arbnode/seq_coordinator.go | 31 +++++++++++++++++-------------- 1 file changed, 17 insertions(+), 14 deletions(-) diff --git a/arbnode/seq_coordinator.go b/arbnode/seq_coordinator.go index 431ea43512..521b17340e 100644 --- a/arbnode/seq_coordinator.go +++ b/arbnode/seq_coordinator.go @@ -752,17 +752,26 @@ func (c *SeqCoordinator) launchHealthcheckServer(ctx context.Context) { func (c *SeqCoordinator) Start(ctxIn context.Context) { c.StopWaiter.Start(ctxIn, c) - c.CallIteratively(c.chooseRedisAndUpdate) + var newRedisCoordinator *redisutil.RedisCoordinator + if c.config.NewRedisUrl != "" { + var err error + newRedisCoordinator, err = redisutil.NewRedisCoordinator(c.config.NewRedisUrl) + if err != nil { + log.Warn("failed to create new redis coordinator", "err", + err, "newRedisUrl", c.config.NewRedisUrl) + } + } + c.CallIteratively(func(ctx context.Context) time.Duration { return c.chooseRedisAndUpdate(ctx, newRedisCoordinator) }) if c.config.ChosenHealthcheckAddr != "" { c.StopWaiter.LaunchThread(c.launchHealthcheckServer) } } -func (c *SeqCoordinator) chooseRedisAndUpdate(ctx context.Context) time.Duration { +func (c *SeqCoordinator) chooseRedisAndUpdate(ctx context.Context, newRedisCoordinator *redisutil.RedisCoordinator) time.Duration { // If we have a new redis coordinator, and we haven't switched to it yet, try to switch. if c.config.NewRedisUrl != "" && c.prevRedisCoordinator == nil { // If we fail to try to switch, we'll retry soon. - if err := c.trySwitchingRedis(ctx); err != nil { + if err := c.trySwitchingRedis(ctx, newRedisCoordinator); err != nil { log.Warn("error while trying to switch redis coordinator", "err", err) return c.retryAfterRedisError() } @@ -770,7 +779,11 @@ func (c *SeqCoordinator) chooseRedisAndUpdate(ctx context.Context) time.Duration return c.update(ctx) } -func (c *SeqCoordinator) trySwitchingRedis(ctx context.Context) error { +func (c *SeqCoordinator) trySwitchingRedis(ctx context.Context, newRedisCoordinator *redisutil.RedisCoordinator) error { + err := c.wantsLockoutUpdate(ctx, newRedisCoordinator.Client) + if err != nil { + return err + } current, err := c.Client.Get(ctx, redisutil.CHOSENSEQ_KEY).Result() var wasEmpty bool if errors.Is(err, redis.Nil) { @@ -783,20 +796,10 @@ func (c *SeqCoordinator) trySwitchingRedis(ctx context.Context) error { } // If the chosen key is set to switch, we need to switch to the new redis coordinator. if !wasEmpty && (current == redisutil.SWITCHED_REDIS) { - newRedisCoordinator, err := redisutil.NewRedisCoordinator(c.config.NewRedisUrl) - if err != nil { - log.Warn("failed to create new redis coordinator", "err", - err, "newRedisUrl", c.config.NewRedisUrl) - return err - } err = c.wantsLockoutUpdate(ctx, c.Client) if err != nil { return err } - err = c.wantsLockoutUpdate(ctx, newRedisCoordinator.Client) - if err != nil { - return err - } c.prevRedisCoordinator = &c.RedisCoordinator c.RedisCoordinator = *newRedisCoordinator } From 6dd8934f94ab1a2a5a367838ba3a5c3c86ae9a07 Mon Sep 17 00:00:00 2001 From: Aman Sanghi Date: Fri, 30 Aug 2024 17:50:24 +0530 Subject: [PATCH 04/38] fix build --- arbnode/node.go | 2 +- arbnode/seq_coordinator.go | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/arbnode/node.go b/arbnode/node.go index 569f0e1b08..93b58e800f 100644 --- a/arbnode/node.go +++ b/arbnode/node.go @@ -446,7 +446,7 @@ func createNodeImpl( } if config.SeqCoordinator.Enable { - coordinator, err = NewSeqCoordinator(dataSigner, bpVerifier, txStreamer, exec, config.SeqCoordinator) + coordinator, err = NewSeqCoordinator(dataSigner, bpVerifier, txStreamer, exec, syncMonitor, config.SeqCoordinator) if err != nil { return nil, err } diff --git a/arbnode/seq_coordinator.go b/arbnode/seq_coordinator.go index 2c02541683..7d058031f5 100644 --- a/arbnode/seq_coordinator.go +++ b/arbnode/seq_coordinator.go @@ -147,6 +147,7 @@ func NewSeqCoordinator( bpvalidator *contracts.AddressVerifier, streamer *TransactionStreamer, sequencer execution.ExecutionSequencer, + sync *SyncMonitor, config SeqCoordinatorConfig, ) (*SeqCoordinator, error) { redisCoordinator, err := redisutil.NewRedisCoordinator(config.RedisUrl) From 79c3aa2a7f9077225ca193ec9d8f41ca3d3f4f93 Mon Sep 17 00:00:00 2001 From: Aman Sanghi Date: Fri, 30 Aug 2024 17:52:40 +0530 Subject: [PATCH 05/38] simplify --- arbnode/seq_coordinator.go | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/arbnode/seq_coordinator.go b/arbnode/seq_coordinator.go index 7d058031f5..6071925780 100644 --- a/arbnode/seq_coordinator.go +++ b/arbnode/seq_coordinator.go @@ -631,17 +631,14 @@ func (c *SeqCoordinator) update(ctx context.Context) time.Duration { log.Warn("cannot get remote message count", "err", err) return c.retryAfterRedisError() } - readUntil := remoteMsgCount + readUntil := min(localMsgCount+c.config.MsgPerPoll, remoteMsgCount) client := c.Client // If we have a previous redis coordinator, // we can read from it until the local message count catches up to the prev coordinator's message count if c.prevRedisMessageCount > localMsgCount { - readUntil = c.prevRedisMessageCount + readUntil = min(readUntil, c.prevRedisMessageCount) client = c.prevRedisCoordinator.Client } - if readUntil > localMsgCount+c.config.MsgPerPoll { - readUntil = localMsgCount + c.config.MsgPerPoll - } var messages []arbostypes.MessageWithMetadata msgToRead := localMsgCount var msgReadErr error From 79d5a813c5b1471d08ccabd2e3187833ea3213fe Mon Sep 17 00:00:00 2001 From: Lee Bousfield Date: Sat, 7 Sep 2024 10:43:54 -0500 Subject: [PATCH 06/38] Be more precise when selecting firstMsg in the batch poster --- arbnode/batch_poster.go | 36 +++++++++++++++++++++++++----------- 1 file changed, 25 insertions(+), 11 deletions(-) diff --git a/arbnode/batch_poster.go b/arbnode/batch_poster.go index 6b4b95f8e0..ea41b52574 100644 --- a/arbnode/batch_poster.go +++ b/arbnode/batch_poster.go @@ -1176,12 +1176,6 @@ func (b *BatchPoster) maybePostSequencerBatch(ctx context.Context) (bool, error) // There's nothing after the newest batch, therefore batch posting was not required return false, nil } - firstMsg, err := b.streamer.GetMessage(batchPosition.MessageCount) - if err != nil { - return false, err - } - // #nosec G115 - firstMsgTime := time.Unix(int64(firstMsg.Message.Header.Timestamp), 0) lastPotentialMsg, err := b.streamer.GetMessage(msgCount - 1) if err != nil { @@ -1189,7 +1183,7 @@ func (b *BatchPoster) maybePostSequencerBatch(ctx context.Context) (bool, error) } config := b.config() - forcePostBatch := config.MaxDelay <= 0 || time.Since(firstMsgTime) >= config.MaxDelay + forcePostBatch := config.MaxDelay <= 0 var l1BoundMaxBlockNumber uint64 = math.MaxUint64 var l1BoundMaxTimestamp uint64 = math.MaxUint64 @@ -1257,6 +1251,8 @@ func (b *BatchPoster) maybePostSequencerBatch(ctx context.Context) (bool, error) } } + var firstNonDelayedMsg *arbostypes.MessageWithMetadata + var firstUsefulMsg *arbostypes.MessageWithMetadata for b.building.msgCount < msgCount { msg, err := b.streamer.GetMessage(b.building.msgCount) if err != nil { @@ -1299,6 +1295,9 @@ func (b *BatchPoster) maybePostSequencerBatch(ctx context.Context) (bool, error) forcePostBatch = true } b.building.haveUsefulMessage = true + if firstUsefulMsg == nil { + firstUsefulMsg = msg + } break } if config.CheckBatchCorrectness { @@ -1309,13 +1308,28 @@ func (b *BatchPoster) maybePostSequencerBatch(ctx context.Context) (bool, error) } if msg.Message.Header.Kind != arbostypes.L1MessageType_BatchPostingReport { b.building.haveUsefulMessage = true + if firstUsefulMsg == nil { + firstUsefulMsg = msg + } + } + if !isDelayed && firstNonDelayedMsg == nil { + firstNonDelayedMsg = msg } b.building.msgCount++ } - if hasL1Bound && config.ReorgResistanceMargin > 0 { - firstMsgBlockNumber := firstMsg.Message.Header.BlockNumber - firstMsgTimeStamp := firstMsg.Message.Header.Timestamp + firstUsefulMsgTime := time.Now() + if firstUsefulMsg != nil { + // #nosec G115 + firstUsefulMsgTime = time.Unix(int64(firstUsefulMsg.Message.Header.Timestamp), 0) + if time.Since(firstUsefulMsgTime) >= config.MaxDelay { + forcePostBatch = true + } + } + + if firstNonDelayedMsg != nil && hasL1Bound && config.ReorgResistanceMargin > 0 { + firstMsgBlockNumber := firstNonDelayedMsg.Message.Header.BlockNumber + firstMsgTimeStamp := firstNonDelayedMsg.Message.Header.Timestamp batchNearL1BoundMinBlockNumber := firstMsgBlockNumber <= arbmath.SaturatingUAdd(l1BoundMinBlockNumber, uint64(config.ReorgResistanceMargin/ethPosBlockTime)) batchNearL1BoundMinTimestamp := firstMsgTimeStamp <= arbmath.SaturatingUAdd(l1BoundMinTimestamp, uint64(config.ReorgResistanceMargin/time.Second)) if batchNearL1BoundMinTimestamp || batchNearL1BoundMinBlockNumber { @@ -1463,7 +1477,7 @@ func (b *BatchPoster) maybePostSequencerBatch(ctx context.Context) (bool, error) } tx, err := b.dataPoster.PostTransaction(ctx, - firstMsgTime, + firstUsefulMsgTime, nonce, newMeta, b.seqInboxAddr, From e950d4bc0d7f531544d245c2b0b397342707fbe3 Mon Sep 17 00:00:00 2001 From: Lee Bousfield Date: Mon, 9 Sep 2024 11:46:00 -0500 Subject: [PATCH 07/38] Store firstUsefulMsg and firstNonDelayedMsg in buildingBatch --- arbnode/batch_poster.go | 38 +++++++++++++++++++------------------- 1 file changed, 19 insertions(+), 19 deletions(-) diff --git a/arbnode/batch_poster.go b/arbnode/batch_poster.go index ea41b52574..072b3254ce 100644 --- a/arbnode/batch_poster.go +++ b/arbnode/batch_poster.go @@ -716,12 +716,14 @@ type batchSegments struct { } type buildingBatch struct { - segments *batchSegments - startMsgCount arbutil.MessageIndex - msgCount arbutil.MessageIndex - haveUsefulMessage bool - use4844 bool - muxBackend *simulatedMuxBackend + segments *batchSegments + startMsgCount arbutil.MessageIndex + msgCount arbutil.MessageIndex + haveUsefulMessage bool + use4844 bool + muxBackend *simulatedMuxBackend + firstNonDelayedMsg *arbostypes.MessageWithMetadata + firstUsefulMsg *arbostypes.MessageWithMetadata } func newBatchSegments(firstDelayed uint64, config *BatchPosterConfig, backlog uint64, use4844 bool) *batchSegments { @@ -1251,8 +1253,6 @@ func (b *BatchPoster) maybePostSequencerBatch(ctx context.Context) (bool, error) } } - var firstNonDelayedMsg *arbostypes.MessageWithMetadata - var firstUsefulMsg *arbostypes.MessageWithMetadata for b.building.msgCount < msgCount { msg, err := b.streamer.GetMessage(b.building.msgCount) if err != nil { @@ -1295,8 +1295,8 @@ func (b *BatchPoster) maybePostSequencerBatch(ctx context.Context) (bool, error) forcePostBatch = true } b.building.haveUsefulMessage = true - if firstUsefulMsg == nil { - firstUsefulMsg = msg + if b.building.firstUsefulMsg == nil { + b.building.firstUsefulMsg = msg } break } @@ -1308,28 +1308,28 @@ func (b *BatchPoster) maybePostSequencerBatch(ctx context.Context) (bool, error) } if msg.Message.Header.Kind != arbostypes.L1MessageType_BatchPostingReport { b.building.haveUsefulMessage = true - if firstUsefulMsg == nil { - firstUsefulMsg = msg + if b.building.firstUsefulMsg == nil { + b.building.firstUsefulMsg = msg } } - if !isDelayed && firstNonDelayedMsg == nil { - firstNonDelayedMsg = msg + if !isDelayed && b.building.firstNonDelayedMsg == nil { + b.building.firstNonDelayedMsg = msg } b.building.msgCount++ } firstUsefulMsgTime := time.Now() - if firstUsefulMsg != nil { + if b.building.firstUsefulMsg != nil { // #nosec G115 - firstUsefulMsgTime = time.Unix(int64(firstUsefulMsg.Message.Header.Timestamp), 0) + firstUsefulMsgTime = time.Unix(int64(b.building.firstUsefulMsg.Message.Header.Timestamp), 0) if time.Since(firstUsefulMsgTime) >= config.MaxDelay { forcePostBatch = true } } - if firstNonDelayedMsg != nil && hasL1Bound && config.ReorgResistanceMargin > 0 { - firstMsgBlockNumber := firstNonDelayedMsg.Message.Header.BlockNumber - firstMsgTimeStamp := firstNonDelayedMsg.Message.Header.Timestamp + if b.building.firstNonDelayedMsg != nil && hasL1Bound && config.ReorgResistanceMargin > 0 { + firstMsgBlockNumber := b.building.firstNonDelayedMsg.Message.Header.BlockNumber + firstMsgTimeStamp := b.building.firstNonDelayedMsg.Message.Header.Timestamp batchNearL1BoundMinBlockNumber := firstMsgBlockNumber <= arbmath.SaturatingUAdd(l1BoundMinBlockNumber, uint64(config.ReorgResistanceMargin/ethPosBlockTime)) batchNearL1BoundMinTimestamp := firstMsgTimeStamp <= arbmath.SaturatingUAdd(l1BoundMinTimestamp, uint64(config.ReorgResistanceMargin/time.Second)) if batchNearL1BoundMinTimestamp || batchNearL1BoundMinBlockNumber { From 798d7177e8c9c160c1a36c954daa982a20e95306 Mon Sep 17 00:00:00 2001 From: Ganesh Vanahalli Date: Fri, 4 Oct 2024 17:15:18 +0530 Subject: [PATCH 08/38] Add a test to keep server_api json.go in sync with parse_inputs.rs --- arbitrator/prover/src/lib.rs | 36 ++++++ arbitrator/prover/src/parse_input.rs | 27 +++- .../validationinputjson_rustfiledata_test.go | 120 ++++++++++++++++++ validator/server_arb/machine.go | 14 ++ 4 files changed, 192 insertions(+), 5 deletions(-) create mode 100644 system_tests/validationinputjson_rustfiledata_test.go diff --git a/arbitrator/prover/src/lib.rs b/arbitrator/prover/src/lib.rs index 08473c2598..993ed1b365 100644 --- a/arbitrator/prover/src/lib.rs +++ b/arbitrator/prover/src/lib.rs @@ -33,9 +33,12 @@ use machine::{ PreimageResolver, }; use once_cell::sync::OnceCell; +use parse_input::FileData; use static_assertions::const_assert_eq; use std::{ ffi::CStr, + fs::File, + io::BufReader, num::NonZeroUsize, os::raw::{c_char, c_int}, path::Path, @@ -84,6 +87,39 @@ pub unsafe extern "C" fn arbitrator_load_machine( } } +#[no_mangle] +pub unsafe extern "C" fn arbitrator_deserialize_and_serialize_file_data( + read_path: *const c_char, + write_path: *const c_char, +) -> c_int { + let read_path = cstr_to_string(read_path); + let write_path = cstr_to_string(write_path); + + let file = File::open(read_path); + let reader = match file { + Ok(file) => BufReader::new(file), + Err(err) => { + eprintln!("Failed to open read_path of FileData: {}", err); + return 1; + } + }; + let data = match FileData::from_reader(reader) { + Ok(data) => data, + Err(err) => { + eprintln!("Failed to deserialize FileData: {}", err); + return 2; + } + }; + + match data.write_to_file(&write_path) { + Ok(()) => 0, + Err(err) => { + eprintln!("Failed to serialize FileData: {}", err); + 3 + } + } +} + unsafe fn arbitrator_load_machine_impl( binary_path: *const c_char, library_paths: *const *const c_char, diff --git a/arbitrator/prover/src/parse_input.rs b/arbitrator/prover/src/parse_input.rs index fa7adb4c41..edfaddf26f 100644 --- a/arbitrator/prover/src/parse_input.rs +++ b/arbitrator/prover/src/parse_input.rs @@ -1,12 +1,14 @@ use arbutil::Bytes32; use serde::Deserialize; +use serde::Serialize; use serde_json; use serde_with::base64::Base64; use serde_with::As; use serde_with::DisplayFromStr; use std::{ collections::HashMap, - io::{self, BufRead}, + fs::File, + io::{self, BufRead, BufWriter}, }; /// prefixed_hex deserializes hex strings which are prefixed with `0x` @@ -16,7 +18,7 @@ use std::{ /// It is an error to use this deserializer on a string that does not /// begin with `0x`. mod prefixed_hex { - use serde::{self, Deserialize, Deserializer}; + use serde::{self, Deserialize, Deserializer, Serializer}; pub fn deserialize<'de, D>(deserializer: D) -> Result, D::Error> where @@ -29,6 +31,14 @@ mod prefixed_hex { Err(serde::de::Error::custom("missing 0x prefix")) } } + + pub fn serialize(bytes: &Vec, serializer: S) -> Result + where + S: Serializer, + { + let hex_string = format!("0x{}", hex::encode(bytes)); + serializer.serialize_str(&hex_string) + } } #[derive(Debug)] @@ -61,7 +71,7 @@ impl From> for UserWasm { } } -#[derive(Debug, Clone, Deserialize)] +#[derive(Debug, Clone, Deserialize, Serialize)] #[serde(rename_all = "PascalCase")] pub struct BatchInfo { pub number: u64, @@ -69,7 +79,7 @@ pub struct BatchInfo { pub data_b64: Vec, } -#[derive(Debug, Deserialize)] +#[derive(Debug, Deserialize, Serialize)] #[serde(rename_all = "PascalCase")] pub struct StartState { #[serde(with = "prefixed_hex")] @@ -88,7 +98,7 @@ pub struct StartState { /// /// Note: It is important to change this file whenever the go JSON /// serialization changes. -#[derive(Debug, Deserialize)] +#[derive(Debug, Deserialize, Serialize)] #[serde(rename_all = "PascalCase")] pub struct FileData { pub id: u64, @@ -109,4 +119,11 @@ impl FileData { let data = serde_json::from_reader(&mut reader)?; Ok(data) } + + pub fn write_to_file(&self, file_path: &str) -> io::Result<()> { + let file = File::create(file_path)?; + let writer = BufWriter::new(file); + serde_json::to_writer_pretty(writer, &self)?; + Ok(()) + } } diff --git a/system_tests/validationinputjson_rustfiledata_test.go b/system_tests/validationinputjson_rustfiledata_test.go new file mode 100644 index 0000000000..a27a68b12c --- /dev/null +++ b/system_tests/validationinputjson_rustfiledata_test.go @@ -0,0 +1,120 @@ +package arbtest + +import ( + "encoding/base64" + "encoding/json" + "fmt" + "os" + "path/filepath" + "reflect" + "testing" + + "github.com/ethereum/go-ethereum/common" + "github.com/ethereum/go-ethereum/ethdb" + + "github.com/offchainlabs/nitro/arbutil" + "github.com/offchainlabs/nitro/validator" + "github.com/offchainlabs/nitro/validator/server_api" + "github.com/offchainlabs/nitro/validator/server_arb" +) + +func getWriteDataFromRustSide(t *testing.T, inputJSON *server_api.InputJSON) []byte { + t.Helper() + dir := t.TempDir() + + readData, err := inputJSON.Marshal() + Require(t, err) + readPath := filepath.Join(dir, fmt.Sprintf("block_inputs_%d_read.json", inputJSON.Id)) + Require(t, os.WriteFile(readPath, readData, 0600)) + + writePath := filepath.Join(dir, fmt.Sprintf("block_inputs_%d_write.json", inputJSON.Id)) + Require(t, server_arb.DeserializeAndSerializeFileData(readPath, writePath)) + writeData, err := os.ReadFile(writePath) + Require(t, err) + + return writeData +} + +func TestGoInputJSONRustFileDataRoundtripWithoutUserWasms(t *testing.T) { + preimages := make(map[arbutil.PreimageType]map[common.Hash][]byte) + preimages[arbutil.Keccak256PreimageType] = make(map[common.Hash][]byte) + preimages[arbutil.Keccak256PreimageType][common.MaxHash] = []byte{1} + + // Don't include DebugChain as it isn't used on rust side + sampleValidationInput := &validator.ValidationInput{ + Id: 1, + HasDelayedMsg: true, + DelayedMsgNr: 2, + Preimages: preimages, + BatchInfo: []validator.BatchInfo{{Number: 3}}, + DelayedMsg: []byte{4}, + StartState: validator.GoGlobalState{ + BlockHash: common.MaxHash, + SendRoot: common.MaxHash, + Batch: 5, + PosInBatch: 6, + }, + } + sampleValidationInputJSON := server_api.ValidationInputToJson(sampleValidationInput) + writeData := getWriteDataFromRustSide(t, sampleValidationInputJSON) + + var resWithoutUserWasms server_api.InputJSON + Require(t, json.Unmarshal(writeData, &resWithoutUserWasms)) + if !reflect.DeepEqual(*sampleValidationInputJSON, resWithoutUserWasms) { + t.Fatal("ValidationInputJSON without UserWasms, mismatch on rust and go side") + } + +} + +type inputJSONWithUserWasmsOnly struct { + UserWasms map[ethdb.WasmTarget]map[common.Hash][]byte +} + +// UnmarshalJSON is a custom function defined to encapsulate how UserWasms are handled on the rust side. +// When ValidationInputToJson is called on ValidationInput, it compresses the wasm data byte array and +// then encodes this to a base64 string, this when deserialized on the rust side through FileData- the +// compressed data is first uncompressed and also the module hash (Bytes32) is read without the 0x prefix, +// so we define a custom UnmarshalJSON to extract UserWasms map from the data written by rust side. +func (u *inputJSONWithUserWasmsOnly) UnmarshalJSON(data []byte) error { + type rawUserWasms struct { + UserWasms map[ethdb.WasmTarget]map[string]string + } + var rawUWs rawUserWasms + if err := json.Unmarshal(data, &rawUWs); err != nil { + return err + } + tmp := make(map[ethdb.WasmTarget]map[common.Hash][]byte) + for wasmTarget, innerMap := range rawUWs.UserWasms { + tmp[wasmTarget] = make(map[common.Hash][]byte) + for hashKey, value := range innerMap { + valBytes, err := base64.StdEncoding.DecodeString(value) + if err != nil { + return err + } + tmp[wasmTarget][common.HexToHash("0x"+hashKey)] = valBytes + } + } + u.UserWasms = tmp + return nil +} + +func TestGoInputJSONRustFileDataRoundtripWithUserWasms(t *testing.T) { + userWasms := make(map[ethdb.WasmTarget]map[common.Hash][]byte) + userWasms["arch1"] = make(map[common.Hash][]byte) + userWasms["arch1"][common.MaxHash] = []byte{2} + + // Don't include DebugChain as it isn't used on rust side + sampleValidationInput := &validator.ValidationInput{ + Id: 1, + UserWasms: userWasms, + BatchInfo: []validator.BatchInfo{{Number: 1}}, // This needs to be set for FileData to successfully deserialize, else it errors for invalid type null + } + sampleValidationInputJSON := server_api.ValidationInputToJson(sampleValidationInput) + writeData := getWriteDataFromRustSide(t, sampleValidationInputJSON) + + var resUserWasmsOnly inputJSONWithUserWasmsOnly + Require(t, json.Unmarshal(writeData, &resUserWasmsOnly)) + if !reflect.DeepEqual(userWasms, resUserWasmsOnly.UserWasms) { + t.Fatal("ValidationInputJSON with UserWasms only, mismatch on rust and go side") + } +} diff --git a/validator/server_arb/machine.go b/validator/server_arb/machine.go index 1e73e6b212..d11f015916 100644 --- a/validator/server_arb/machine.go +++ b/validator/server_arb/machine.go @@ -312,6 +312,20 @@ func (m *ArbitratorMachine) DeserializeAndReplaceState(path string) error { } } +func DeserializeAndSerializeFileData(readPath, writePath string) error { + cReadPath := C.CString(readPath) + cWritePath := C.CString(writePath) + status := C.arbitrator_deserialize_and_serialize_file_data(cReadPath, cWritePath) + C.free(unsafe.Pointer(cReadPath)) + C.free(unsafe.Pointer(cWritePath)) + + if status != 0 { + return fmt.Errorf("failed to call arbitrator_deserialize_and_serialize_file_data. Error code: %d", status) + } else { + return nil + } +} + func (m *ArbitratorMachine) AddSequencerInboxMessage(index uint64, data []byte) error { defer runtime.KeepAlive(m) m.mutex.Lock() From 44e6fba5d1cce7b0bf76f3551e8bf95e795e0804 Mon Sep 17 00:00:00 2001 From: Aman Sanghi Date: Tue, 8 Oct 2024 20:19:47 +0530 Subject: [PATCH 09/38] fix race issue --- arbnode/maintenance.go | 2 +- arbnode/seq_coordinator.go | 61 ++++++++++++++++++++------------- arbnode/seq_coordinator_test.go | 18 +++++----- system_tests/forwarder_test.go | 2 +- 4 files changed, 48 insertions(+), 35 deletions(-) diff --git a/arbnode/maintenance.go b/arbnode/maintenance.go index 53d038a0f9..7397229c2e 100644 --- a/arbnode/maintenance.go +++ b/arbnode/maintenance.go @@ -101,7 +101,7 @@ func NewMaintenanceRunner(config MaintenanceConfigFetcher, seqCoordinator *SeqCo if seqCoordinator != nil { c := func() *redislock.SimpleCfg { return &cfg.Lock } r := func() bool { return true } // always ready to lock - rl, err := redislock.NewSimple(seqCoordinator.Client, c, r) + rl, err := redislock.NewSimple(seqCoordinator.RedisCoordinator().Client, c, r) if err != nil { return nil, fmt.Errorf("creating new simple redis lock: %w", err) } diff --git a/arbnode/seq_coordinator.go b/arbnode/seq_coordinator.go index f2bebbf472..4fbc31a108 100644 --- a/arbnode/seq_coordinator.go +++ b/arbnode/seq_coordinator.go @@ -37,7 +37,8 @@ var ( type SeqCoordinator struct { stopwaiter.StopWaiter - redisutil.RedisCoordinator + redisCoordinatorMutex sync.RWMutex + redisCoordinator redisutil.RedisCoordinator prevRedisCoordinator *redisutil.RedisCoordinator prevRedisMessageCount arbutil.MessageIndex @@ -159,7 +160,7 @@ func NewSeqCoordinator( return nil, err } coordinator := &SeqCoordinator{ - RedisCoordinator: *redisCoordinator, + redisCoordinator: *redisCoordinator, sync: sync, streamer: streamer, sequencer: sequencer, @@ -180,6 +181,19 @@ func (c *SeqCoordinator) SetDelayedSequencer(delayedSequencer *DelayedSequencer) c.delayedSequencer = delayedSequencer } +func (c *SeqCoordinator) RedisCoordinator() *redisutil.RedisCoordinator { + c.redisCoordinatorMutex.RLock() + defer c.redisCoordinatorMutex.RUnlock() + return &c.redisCoordinator +} + +func (c *SeqCoordinator) setRedisCoordinator(redisCoordinator *redisutil.RedisCoordinator) { + c.redisCoordinatorMutex.Lock() + defer c.redisCoordinatorMutex.Unlock() + c.prevRedisCoordinator = &c.redisCoordinator + c.redisCoordinator = *redisCoordinator +} + func StandaloneSeqCoordinatorInvalidateMsgIndex(ctx context.Context, redisClient redis.UniversalClient, keyConfig string, msgIndex arbutil.MessageIndex) error { signerConfig := signature.EmptySimpleHmacConfig if keyConfig == "" { @@ -282,7 +296,7 @@ func (c *SeqCoordinator) acquireLockoutAndWriteMessage(ctx context.Context, msgC defer c.wantsLockoutMutex.Unlock() setWantsLockout := c.avoidLockout <= 0 lockoutUntil := time.Now().Add(c.config.LockoutDuration) - err = c.Client.Watch(ctx, func(tx *redis.Tx) error { + err = c.RedisCoordinator().Client.Watch(ctx, func(tx *redis.Tx) error { current, err := tx.Get(ctx, redisutil.CHOSENSEQ_KEY).Result() var wasEmpty bool if errors.Is(err, redis.Nil) { @@ -351,7 +365,7 @@ func (c *SeqCoordinator) acquireLockoutAndWriteMessage(ctx context.Context, msgC } func (c *SeqCoordinator) getRemoteFinalizedMsgCount(ctx context.Context) (arbutil.MessageIndex, error) { - resStr, err := c.Client.Get(ctx, redisutil.FINALIZED_MSG_COUNT_KEY).Result() + resStr, err := c.RedisCoordinator().Client.Get(ctx, redisutil.FINALIZED_MSG_COUNT_KEY).Result() if err != nil { return 0, err } @@ -370,7 +384,7 @@ func (c *SeqCoordinator) getRemoteMsgCountImpl(ctx context.Context, r redis.Cmda } func (c *SeqCoordinator) GetRemoteMsgCount() (arbutil.MessageIndex, error) { - return c.getRemoteMsgCountImpl(c.GetContext(), c.Client) + return c.getRemoteMsgCountImpl(c.GetContext(), c.RedisCoordinator().Client) } func (c *SeqCoordinator) wantsLockoutUpdate(ctx context.Context, client redis.UniversalClient) error { @@ -404,7 +418,7 @@ func (c *SeqCoordinator) wantsLockoutUpdateWithMutex(ctx context.Context, client func (c *SeqCoordinator) chosenOneRelease(ctx context.Context) error { atomicTimeWrite(&c.lockoutUntil, time.Time{}) isActiveSequencer.Update(0) - releaseErr := c.Client.Watch(ctx, func(tx *redis.Tx) error { + releaseErr := c.RedisCoordinator().Client.Watch(ctx, func(tx *redis.Tx) error { current, err := tx.Get(ctx, redisutil.CHOSENSEQ_KEY).Result() if errors.Is(err, redis.Nil) { return nil @@ -427,7 +441,7 @@ func (c *SeqCoordinator) chosenOneRelease(ctx context.Context) error { return nil } // got error - was it still released? - current, readErr := c.Client.Get(ctx, redisutil.CHOSENSEQ_KEY).Result() + current, readErr := c.RedisCoordinator().Client.Get(ctx, redisutil.CHOSENSEQ_KEY).Result() if errors.Is(readErr, redis.Nil) { return nil } @@ -444,10 +458,10 @@ func (c *SeqCoordinator) wantsLockoutRelease(ctx context.Context) error { return nil } myWantsLockoutKey := redisutil.WantsLockoutKeyFor(c.config.Url()) - releaseErr := c.Client.Del(ctx, myWantsLockoutKey).Err() + releaseErr := c.RedisCoordinator().Client.Del(ctx, myWantsLockoutKey).Err() if releaseErr != nil { // got error - was it still deleted? - readErr := c.Client.Get(ctx, myWantsLockoutKey).Err() + readErr := c.RedisCoordinator().Client.Get(ctx, myWantsLockoutKey).Err() if !errors.Is(readErr, redis.Nil) { return releaseErr } @@ -531,7 +545,7 @@ func (c *SeqCoordinator) deleteFinalizedMsgsFromRedis(ctx context.Context, final // In non-init cases it doesn't matter how we delete as we always try to delete from prevFinalized to finalized batchDeleteCount := 1000 for i := len(keys); i > 0; i -= batchDeleteCount { - if err := c.Client.Del(ctx, keys[max(0, i-batchDeleteCount):i]...).Err(); err != nil { + if err := c.RedisCoordinator().Client.Del(ctx, keys[max(0, i-batchDeleteCount):i]...).Err(); err != nil { return fmt.Errorf("error deleting finalized messages and their signatures from redis: %w", err) } } @@ -540,7 +554,7 @@ func (c *SeqCoordinator) deleteFinalizedMsgsFromRedis(ctx context.Context, final if err != nil { return err } - if err = c.Client.Set(ctx, redisutil.FINALIZED_MSG_COUNT_KEY, finalizedBytes, c.config.SeqNumDuration).Err(); err != nil { + if err = c.RedisCoordinator().Client.Set(ctx, redisutil.FINALIZED_MSG_COUNT_KEY, finalizedBytes, c.config.SeqNumDuration).Err(); err != nil { return fmt.Errorf("couldn't set %s key to current finalizedMsgCount in redis: %w", redisutil.FINALIZED_MSG_COUNT_KEY, err) } return nil @@ -549,7 +563,7 @@ func (c *SeqCoordinator) deleteFinalizedMsgsFromRedis(ctx context.Context, final if errors.Is(err, redis.Nil) { var keys []string for msg := finalized - 1; msg > 0; msg-- { - exists, err := c.Client.Exists(ctx, redisutil.MessageKeyFor(msg), redisutil.MessageSigKeyFor(msg)).Result() + exists, err := c.RedisCoordinator().Client.Exists(ctx, redisutil.MessageKeyFor(msg), redisutil.MessageSigKeyFor(msg)).Result() if err != nil { // If there is an error deleting finalized messages during init, we retry later either from this sequencer or from another return err @@ -564,7 +578,7 @@ func (c *SeqCoordinator) deleteFinalizedMsgsFromRedis(ctx context.Context, final } else if err != nil { return fmt.Errorf("error getting finalizedMsgCount value from redis: %w", err) } - remoteMsgCount, err := c.getRemoteMsgCountImpl(ctx, c.Client) + remoteMsgCount, err := c.getRemoteMsgCountImpl(ctx, c.RedisCoordinator().Client) if err != nil { return fmt.Errorf("cannot get remote message count: %w", err) } @@ -580,7 +594,7 @@ func (c *SeqCoordinator) deleteFinalizedMsgsFromRedis(ctx context.Context, final } func (c *SeqCoordinator) update(ctx context.Context) time.Duration { - chosenSeq, err := c.RecommendSequencerWantingLockout(ctx) + chosenSeq, err := c.RedisCoordinator().RecommendSequencerWantingLockout(ctx) if err != nil { log.Warn("coordinator failed finding sequencer wanting lockout", "err", err) return c.retryAfterRedisError() @@ -632,7 +646,7 @@ func (c *SeqCoordinator) update(ctx context.Context) time.Duration { return c.retryAfterRedisError() } readUntil := min(localMsgCount+c.config.MsgPerPoll, remoteMsgCount) - client := c.Client + client := c.RedisCoordinator().Client // If we have a previous redis coordinator, // we can read from it until the local message count catches up to the prev coordinator's message count if c.prevRedisMessageCount > localMsgCount { @@ -744,7 +758,7 @@ func (c *SeqCoordinator) update(ctx context.Context) time.Duration { // this could be just new messages we didn't get yet - even then, we should retry soon log.Info("sequencer failed to become chosen", "err", err, "msgcount", localMsgCount) // make sure we're marked as wanting the lockout - if err := c.wantsLockoutUpdate(ctx, c.Client); err != nil { + if err := c.wantsLockoutUpdate(ctx, c.RedisCoordinator().Client); err != nil { log.Warn("failed to update wants lockout key", "err", err) } c.prevChosenSequencer = "" @@ -772,7 +786,7 @@ func (c *SeqCoordinator) update(ctx context.Context) time.Duration { // update wanting the lockout var wantsLockoutErr error if synced && !c.AvoidingLockout() { - wantsLockoutErr = c.wantsLockoutUpdate(ctx, c.Client) + wantsLockoutErr = c.wantsLockoutUpdate(ctx, c.RedisCoordinator().Client) } else { wantsLockoutErr = c.wantsLockoutRelease(ctx) } @@ -871,7 +885,7 @@ func (c *SeqCoordinator) trySwitchingRedis(ctx context.Context, newRedisCoordina if err != nil { return err } - current, err := c.Client.Get(ctx, redisutil.CHOSENSEQ_KEY).Result() + current, err := c.RedisCoordinator().Client.Get(ctx, redisutil.CHOSENSEQ_KEY).Result() var wasEmpty bool if errors.Is(err, redis.Nil) { wasEmpty = true @@ -883,12 +897,11 @@ func (c *SeqCoordinator) trySwitchingRedis(ctx context.Context, newRedisCoordina } // If the chosen key is set to switch, we need to switch to the new redis coordinator. if !wasEmpty && (current == redisutil.SWITCHED_REDIS) { - err = c.wantsLockoutUpdate(ctx, c.Client) + err = c.wantsLockoutUpdate(ctx, c.RedisCoordinator().Client) if err != nil { return err } - c.prevRedisCoordinator = &c.RedisCoordinator - c.RedisCoordinator = *newRedisCoordinator + c.setRedisCoordinator(newRedisCoordinator) } return nil } @@ -942,7 +955,7 @@ func (c *SeqCoordinator) StopAndWait() { time.Sleep(c.retryAfterRedisError()) } } - _ = c.Client.Close() + _ = c.RedisCoordinator().Client.Close() } func (c *SeqCoordinator) CurrentlyChosen() bool { @@ -984,7 +997,7 @@ func (c *SeqCoordinator) TryToHandoffChosenOne(ctx context.Context) bool { return !c.CurrentlyChosen() }) if success { - wantsLockout, err := c.RecommendSequencerWantingLockout(ctx) + wantsLockout, err := c.RedisCoordinator().RecommendSequencerWantingLockout(ctx) if err == nil { log.Info("released chosen one status; a new sequencer hopefully wants to acquire it", "delay", c.config.SafeShutdownDelay, "wantsLockout", wantsLockout) } else { @@ -1006,7 +1019,7 @@ func (c *SeqCoordinator) SeekLockout(ctx context.Context) { log.Info("seeking lockout", "myUrl", c.config.Url()) if c.sequencer.Synced() { // Even if this errors we still internally marked ourselves as wanting the lockout - err := c.wantsLockoutUpdateWithMutex(ctx, c.Client) + err := c.wantsLockoutUpdateWithMutex(ctx, c.RedisCoordinator().Client) if err != nil { log.Warn("failed to set wants lockout key in redis after seeking lockout again", "err", err) } diff --git a/arbnode/seq_coordinator_test.go b/arbnode/seq_coordinator_test.go index 6498543f3a..3f35011c20 100644 --- a/arbnode/seq_coordinator_test.go +++ b/arbnode/seq_coordinator_test.go @@ -125,7 +125,7 @@ func TestRedisSeqCoordinatorAtomic(t *testing.T) { redisCoordinator, err := redisutil.NewRedisCoordinator(config.RedisUrl) Require(t, err) coordinator := &SeqCoordinator{ - RedisCoordinator: *redisCoordinator, + redisCoordinator: *redisCoordinator, config: config, signer: nullSigner, } @@ -181,7 +181,7 @@ func TestSeqCoordinatorDeletesFinalizedMessages(t *testing.T) { redisCoordinator, err := redisutil.NewRedisCoordinator(config.RedisUrl) Require(t, err) coordinator := &SeqCoordinator{ - RedisCoordinator: *redisCoordinator, + redisCoordinator: *redisCoordinator, config: config, signer: nullSigner, } @@ -191,18 +191,18 @@ func TestSeqCoordinatorDeletesFinalizedMessages(t *testing.T) { msgBytes, err := coordinator.msgCountToSignedBytes(0) Require(t, err) for i := arbutil.MessageIndex(1); i <= 10; i++ { - err = coordinator.Client.Set(ctx, redisutil.MessageKeyFor(i), msgBytes, time.Hour).Err() + err = coordinator.RedisCoordinator().Client.Set(ctx, redisutil.MessageKeyFor(i), msgBytes, time.Hour).Err() Require(t, err) - err = coordinator.Client.Set(ctx, redisutil.MessageSigKeyFor(i), msgBytes, time.Hour).Err() + err = coordinator.RedisCoordinator().Client.Set(ctx, redisutil.MessageSigKeyFor(i), msgBytes, time.Hour).Err() Require(t, err) keys = append(keys, redisutil.MessageKeyFor(i), redisutil.MessageSigKeyFor(i)) } // Set msgCount key msgCountBytes, err := coordinator.msgCountToSignedBytes(11) Require(t, err) - err = coordinator.Client.Set(ctx, redisutil.MSG_COUNT_KEY, msgCountBytes, time.Hour).Err() + err = coordinator.RedisCoordinator().Client.Set(ctx, redisutil.MSG_COUNT_KEY, msgCountBytes, time.Hour).Err() Require(t, err) - exists, err := coordinator.Client.Exists(ctx, keys...).Result() + exists, err := coordinator.RedisCoordinator().Client.Exists(ctx, keys...).Result() Require(t, err) if exists != 20 { t.Fatal("couldn't find all messages and signatures in redis") @@ -213,7 +213,7 @@ func TestSeqCoordinatorDeletesFinalizedMessages(t *testing.T) { Require(t, err) // Check if messages and signatures were deleted successfully - exists, err = coordinator.Client.Exists(ctx, keys[:8]...).Result() + exists, err = coordinator.RedisCoordinator().Client.Exists(ctx, keys[:8]...).Result() Require(t, err) if exists != 0 { t.Fatal("finalized messages and signatures in range 1 to 4 were not deleted") @@ -229,7 +229,7 @@ func TestSeqCoordinatorDeletesFinalizedMessages(t *testing.T) { // Try deleting finalized messages when theres already a finalizedMsgCount err = coordinator.deleteFinalizedMsgsFromRedis(ctx, 7) Require(t, err) - exists, err = coordinator.Client.Exists(ctx, keys[8:12]...).Result() + exists, err = coordinator.RedisCoordinator().Client.Exists(ctx, keys[8:12]...).Result() Require(t, err) if exists != 0 { t.Fatal("finalized messages and signatures in range 5 to 6 were not deleted") @@ -241,7 +241,7 @@ func TestSeqCoordinatorDeletesFinalizedMessages(t *testing.T) { } // Check that non-finalized messages are still available in redis - exists, err = coordinator.Client.Exists(ctx, keys[12:]...).Result() + exists, err = coordinator.RedisCoordinator().Client.Exists(ctx, keys[12:]...).Result() Require(t, err) if exists != 8 { t.Fatal("non-finalized messages and signatures in range 7 to 10 are not fully available") diff --git a/system_tests/forwarder_test.go b/system_tests/forwarder_test.go index 57381ca84e..6a1d1c68d8 100644 --- a/system_tests/forwarder_test.go +++ b/system_tests/forwarder_test.go @@ -170,7 +170,7 @@ func waitForSequencerLockout(ctx context.Context, node *arbnode.Node, duration t case <-time.After(duration): return fmt.Errorf("no sequencer was chosen") default: - if c, err := node.SeqCoordinator.CurrentChosenSequencer(ctx); err == nil && c != "" { + if c, err := node.SeqCoordinator.RedisCoordinator().CurrentChosenSequencer(ctx); err == nil && c != "" { return nil } time.Sleep(100 * time.Millisecond) From 115afdc4d5c9ebadb985a581e6e19a73b635a59b Mon Sep 17 00:00:00 2001 From: Ganesh Vanahalli Date: Wed, 9 Oct 2024 17:10:48 +0530 Subject: [PATCH 10/38] Jit prover should accept InputJSON format and execute a full block --- arbitrator/jit/src/machine.rs | 120 ++++++++++++++++++---------------- arbitrator/jit/src/main.rs | 5 ++ arbitrator/jit/src/prepare.rs | 73 +++++++++++++++++++++ system_tests/common_test.go | 6 +- system_tests/program_test.go | 8 ++- 5 files changed, 150 insertions(+), 62 deletions(-) create mode 100644 arbitrator/jit/src/prepare.rs diff --git a/arbitrator/jit/src/machine.rs b/arbitrator/jit/src/machine.rs index 02523f740a..0d74c74ef6 100644 --- a/arbitrator/jit/src/machine.rs +++ b/arbitrator/jit/src/machine.rs @@ -2,8 +2,8 @@ // For license information, see https://github.com/nitro/blob/master/LICENSE use crate::{ - arbcompress, caller_env::GoRuntimeState, program, socket, stylus_backend::CothreadHandler, - wasip1_stub, wavmio, Opts, + arbcompress, caller_env::GoRuntimeState, prepare::prepare_env, program, socket, + stylus_backend::CothreadHandler, wasip1_stub, wavmio, Opts, }; use arbutil::{Bytes32, Color, PreimageType}; use eyre::{bail, ErrReport, Result, WrapErr}; @@ -215,72 +215,76 @@ pub struct WasmEnv { impl WasmEnv { pub fn cli(opts: &Opts) -> Result { - let mut env = WasmEnv::default(); - env.process.forks = opts.forks; - env.process.debug = opts.debug; + if let Some(json_inputs) = opts.json_inputs.clone() { + prepare_env(json_inputs, opts.debug) + } else { + let mut env = WasmEnv::default(); + env.process.forks = opts.forks; + env.process.debug = opts.debug; - let mut inbox_position = opts.inbox_position; - let mut delayed_position = opts.delayed_inbox_position; + let mut inbox_position = opts.inbox_position; + let mut delayed_position = opts.delayed_inbox_position; - for path in &opts.inbox { - let mut msg = vec![]; - File::open(path)?.read_to_end(&mut msg)?; - env.sequencer_messages.insert(inbox_position, msg); - inbox_position += 1; - } - for path in &opts.delayed_inbox { - let mut msg = vec![]; - File::open(path)?.read_to_end(&mut msg)?; - env.delayed_messages.insert(delayed_position, msg); - delayed_position += 1; - } + for path in &opts.inbox { + let mut msg = vec![]; + File::open(path)?.read_to_end(&mut msg)?; + env.sequencer_messages.insert(inbox_position, msg); + inbox_position += 1; + } + for path in &opts.delayed_inbox { + let mut msg = vec![]; + File::open(path)?.read_to_end(&mut msg)?; + env.delayed_messages.insert(delayed_position, msg); + delayed_position += 1; + } - if let Some(path) = &opts.preimages { - let mut file = BufReader::new(File::open(path)?); - let mut preimages = Vec::new(); - let filename = path.to_string_lossy(); - loop { - let mut size_buf = [0u8; 8]; - match file.read_exact(&mut size_buf) { - Ok(()) => {} - Err(err) if err.kind() == ErrorKind::UnexpectedEof => break, - Err(err) => bail!("Failed to parse {filename}: {}", err), + if let Some(path) = &opts.preimages { + let mut file = BufReader::new(File::open(path)?); + let mut preimages = Vec::new(); + let filename = path.to_string_lossy(); + loop { + let mut size_buf = [0u8; 8]; + match file.read_exact(&mut size_buf) { + Ok(()) => {} + Err(err) if err.kind() == ErrorKind::UnexpectedEof => break, + Err(err) => bail!("Failed to parse {filename}: {}", err), + } + let size = u64::from_le_bytes(size_buf) as usize; + let mut buf = vec![0u8; size]; + file.read_exact(&mut buf)?; + preimages.push(buf); + } + let keccak_preimages = env.preimages.entry(PreimageType::Keccak256).or_default(); + for preimage in preimages { + let mut hasher = Keccak256::new(); + hasher.update(&preimage); + let hash = hasher.finalize().into(); + keccak_preimages.insert(hash, preimage); } - let size = u64::from_le_bytes(size_buf) as usize; - let mut buf = vec![0u8; size]; - file.read_exact(&mut buf)?; - preimages.push(buf); - } - let keccak_preimages = env.preimages.entry(PreimageType::Keccak256).or_default(); - for preimage in preimages { - let mut hasher = Keccak256::new(); - hasher.update(&preimage); - let hash = hasher.finalize().into(); - keccak_preimages.insert(hash, preimage); } - } - fn parse_hex(arg: &Option, name: &str) -> Result { - match arg { - Some(arg) => { - let mut arg = arg.as_str(); - if arg.starts_with("0x") { - arg = &arg[2..]; + fn parse_hex(arg: &Option, name: &str) -> Result { + match arg { + Some(arg) => { + let mut arg = arg.as_str(); + if arg.starts_with("0x") { + arg = &arg[2..]; + } + let mut bytes32 = [0u8; 32]; + hex::decode_to_slice(arg, &mut bytes32) + .wrap_err_with(|| format!("failed to parse {} contents", name))?; + Ok(bytes32.into()) } - let mut bytes32 = [0u8; 32]; - hex::decode_to_slice(arg, &mut bytes32) - .wrap_err_with(|| format!("failed to parse {} contents", name))?; - Ok(bytes32.into()) + None => Ok(Bytes32::default()), } - None => Ok(Bytes32::default()), } - } - let last_block_hash = parse_hex(&opts.last_block_hash, "--last-block-hash")?; - let last_send_root = parse_hex(&opts.last_send_root, "--last-send-root")?; - env.small_globals = [opts.inbox_position, opts.position_within_message]; - env.large_globals = [last_block_hash, last_send_root]; - Ok(env) + let last_block_hash = parse_hex(&opts.last_block_hash, "--last-block-hash")?; + let last_send_root = parse_hex(&opts.last_send_root, "--last-send-root")?; + env.small_globals = [opts.inbox_position, opts.position_within_message]; + env.large_globals = [last_block_hash, last_send_root]; + Ok(env) + } } pub fn send_results(&mut self, error: Option, memory_used: Pages) { diff --git a/arbitrator/jit/src/main.rs b/arbitrator/jit/src/main.rs index e432dc215c..6e44500215 100644 --- a/arbitrator/jit/src/main.rs +++ b/arbitrator/jit/src/main.rs @@ -10,6 +10,7 @@ use structopt::StructOpt; mod arbcompress; mod caller_env; mod machine; +mod prepare; mod program; mod socket; mod stylus_backend; @@ -46,6 +47,10 @@ pub struct Opts { debug: bool, #[structopt(long)] require_success: bool, + // JSON inputs supercede any of the command-line inputs which could + // be specified in the JSON file. + #[structopt(long)] + json_inputs: Option, } fn main() -> Result<()> { diff --git a/arbitrator/jit/src/prepare.rs b/arbitrator/jit/src/prepare.rs new file mode 100644 index 0000000000..7cf46143cc --- /dev/null +++ b/arbitrator/jit/src/prepare.rs @@ -0,0 +1,73 @@ +// Copyright 2022-2024, Offchain Labs, Inc. +// For license information, see https://github.com/nitro/blob/master/LICENSE + +use crate::WasmEnv; +use arbutil::{Bytes32, PreimageType}; +use eyre::Ok; +use prover::parse_input::FileData; +use std::env; +use std::fs::File; +use std::io::BufReader; +use std::path::PathBuf; + +// local_target matches rawdb.LocalTarget() on the go side. +// While generating json_inputs file, one should make sure user_wasms map +// has entry for the system's arch that jit validation is being run on +pub fn local_target() -> String { + if env::consts::OS == "linux" { + match env::consts::ARCH { + "arm64" => "arm64".to_string(), + "amd64" => "amd64".to_string(), + _ => "host".to_string(), + } + } else { + "host".to_string() + } +} + +pub fn prepare_env(json_inputs: PathBuf, debug: bool) -> eyre::Result { + let file = File::open(json_inputs)?; + let reader = BufReader::new(file); + + let data = FileData::from_reader(reader)?; + + let mut env = WasmEnv::default(); + env.process.forks = false; // Should be set to false when using json_inputs + env.process.debug = debug; + + let block_hash: [u8; 32] = data.start_state.block_hash.try_into().unwrap(); + let block_hash: Bytes32 = block_hash.into(); + let send_root: [u8; 32] = data.start_state.send_root.try_into().unwrap(); + let send_root: Bytes32 = send_root.into(); + let bytes32_vals: [Bytes32; 2] = [block_hash, send_root]; + let u64_vals: [u64; 2] = [data.start_state.batch, data.start_state.pos_in_batch]; + env.small_globals = u64_vals; + env.large_globals = bytes32_vals; + + for batch_info in data.batch_info.iter() { + env.sequencer_messages + .insert(batch_info.number, batch_info.data_b64.clone()); + } + + if data.delayed_msg_nr != 0 && data.delayed_msg_b64.len() != 0 { + env.delayed_messages + .insert(data.delayed_msg_nr, data.delayed_msg_b64.clone()); + } + + for (ty, inner_map) in data.preimages_b64 { + let preimage_ty = PreimageType::try_from(ty as u8)?; + let map = env.preimages.entry(preimage_ty).or_default(); + for (hash, preimage) in inner_map { + map.insert(hash, preimage); + } + } + + if let Some(user_wasms) = data.user_wasms.get(&local_target()) { + for (module_hash, module_asm) in user_wasms.iter() { + env.module_asms + .insert(*module_hash, module_asm.as_vec().into()); + } + } + + Ok(env) +} diff --git a/system_tests/common_test.go b/system_tests/common_test.go index 93c38b5eae..5667c4bcd7 100644 --- a/system_tests/common_test.go +++ b/system_tests/common_test.go @@ -1699,8 +1699,8 @@ func logParser[T any](t *testing.T, source string, name string) func(*types.Log) // recordBlock writes a json file with all of the data needed to validate a block. // -// This can be used as an input to the arbitrator prover to validate a block. -func recordBlock(t *testing.T, block uint64, builder *NodeBuilder) { +// This can be used as an input to the arbitrator's prover (target=rawdb.TargetWavm) and jit (target=rawdb.LocalTarget()) binaries to validate a block. +func recordBlock(t *testing.T, block uint64, builder *NodeBuilder, target ethdb.WasmTarget) { t.Helper() ctx := builder.ctx inboxPos := arbutil.MessageIndex(block) @@ -1716,7 +1716,7 @@ func recordBlock(t *testing.T, block uint64, builder *NodeBuilder) { } validationInputsWriter, err := inputs.NewWriter(inputs.WithSlug(t.Name())) Require(t, err) - inputJson, err := builder.L2.ConsensusNode.StatelessBlockValidator.ValidationInputsAt(ctx, inboxPos, rawdb.TargetWavm) + inputJson, err := builder.L2.ConsensusNode.StatelessBlockValidator.ValidationInputsAt(ctx, inboxPos, target) if err != nil { Fatal(t, "failed to get validation inputs", block, err) } diff --git a/system_tests/program_test.go b/system_tests/program_test.go index cf8cd72559..4e7fc8dfd8 100644 --- a/system_tests/program_test.go +++ b/system_tests/program_test.go @@ -425,7 +425,7 @@ func storageTest(t *testing.T, jit bool) { // Captures a block_input_.json file for the block that included the // storage write transaction. - recordBlock(t, receipt.BlockNumber.Uint64(), builder) + recordBlock(t, receipt.BlockNumber.Uint64(), builder, rawdb.TargetWavm) } func TestProgramTransientStorage(t *testing.T) { @@ -492,6 +492,12 @@ func transientStorageTest(t *testing.T, jit bool) { } validateBlocks(t, 7, jit, builder) + + // Captures a block_input_.json file for the block that included the storage + // related transaction. Has userwasms for WasmTarget recognized by jit binary + receipt, err := EnsureTxSucceeded(ctx, l2client, tx) + Require(t, err) + recordBlock(t, receipt.BlockNumber.Uint64(), builder, rawdb.LocalTarget()) } func TestProgramMath(t *testing.T) { From 3a6e8681b630cacaace4c8b9af98bf2d2a4cb459 Mon Sep 17 00:00:00 2001 From: Ganesh Vanahalli Date: Wed, 9 Oct 2024 19:40:51 +0530 Subject: [PATCH 11/38] fix clippy error --- arbitrator/jit/src/prepare.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/arbitrator/jit/src/prepare.rs b/arbitrator/jit/src/prepare.rs index 7cf46143cc..6c1b0b84de 100644 --- a/arbitrator/jit/src/prepare.rs +++ b/arbitrator/jit/src/prepare.rs @@ -49,7 +49,7 @@ pub fn prepare_env(json_inputs: PathBuf, debug: bool) -> eyre::Result { .insert(batch_info.number, batch_info.data_b64.clone()); } - if data.delayed_msg_nr != 0 && data.delayed_msg_b64.len() != 0 { + if data.delayed_msg_nr != 0 && !data.delayed_msg_b64.is_empty() { env.delayed_messages .insert(data.delayed_msg_nr, data.delayed_msg_b64.clone()); } From be58669225e8ecf604182a2d65c1d115cf12686d Mon Sep 17 00:00:00 2001 From: Ganesh Vanahalli Date: Thu, 10 Oct 2024 11:56:28 +0530 Subject: [PATCH 12/38] change implimentation and add CI step to execute arbitrator prover using block input json --- .github/workflows/ci.yml | 12 ++ arbitrator/prover/src/lib.rs | 36 ------ arbitrator/prover/src/parse_input.rs | 27 +--- system_tests/common_test.go | 12 +- system_tests/program_test.go | 5 +- .../validationinputjson_rustfiledata_test.go | 120 ------------------ validator/server_arb/machine.go | 14 -- 7 files changed, 26 insertions(+), 200 deletions(-) delete mode 100644 system_tests/validationinputjson_rustfiledata_test.go diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index a944f08f40..0b5592dafc 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -169,6 +169,18 @@ jobs: run: | echo "Running redis tests" >> full.log TEST_REDIS=redis://localhost:6379/0 gotestsum --format short-verbose -- -p 1 -run TestRedis ./arbnode/... ./system_tests/... -coverprofile=coverage-redis.txt -covermode=atomic -coverpkg=./... + + - name: create block input json file + if: matrix.test-mode == 'defaults' + run: | + echo "BLOCK_INPUT_JSON_PATH=$(pwd)/target/block_input.json" >> "$GITHUB_ENV" + ${{ github.workspace }}/.github/workflows/gotestsum.sh --run TestProgramStorage$ --count 1 + + - name: run arbitrator prover on block input json + if: matrix.test-mode == 'defaults' + run: | + make build-prover-bin + target/bin/prover target/machines/latest/machine.wavm.br -b --json-inputs=$BLOCK_INPUT_JSON_PATH - name: run challenge tests if: matrix.test-mode == 'challenge' diff --git a/arbitrator/prover/src/lib.rs b/arbitrator/prover/src/lib.rs index 993ed1b365..08473c2598 100644 --- a/arbitrator/prover/src/lib.rs +++ b/arbitrator/prover/src/lib.rs @@ -33,12 +33,9 @@ use machine::{ PreimageResolver, }; use once_cell::sync::OnceCell; -use parse_input::FileData; use static_assertions::const_assert_eq; use std::{ ffi::CStr, - fs::File, - io::BufReader, num::NonZeroUsize, os::raw::{c_char, c_int}, path::Path, @@ -87,39 +84,6 @@ pub unsafe extern "C" fn arbitrator_load_machine( } } -#[no_mangle] -pub unsafe extern "C" fn arbitrator_deserialize_and_serialize_file_data( - read_path: *const c_char, - write_path: *const c_char, -) -> c_int { - let read_path = cstr_to_string(read_path); - let write_path = cstr_to_string(write_path); - - let file = File::open(read_path); - let reader = match file { - Ok(file) => BufReader::new(file), - Err(err) => { - eprintln!("Failed to open read_path of FileData: {}", err); - return 1; - } - }; - let data = match FileData::from_reader(reader) { - Ok(data) => data, - Err(err) => { - eprintln!("Failed to deserialize FileData: {}", err); - return 2; - } - }; - - match data.write_to_file(&write_path) { - Ok(()) => 0, - Err(err) => { - eprintln!("Failed to serialize FileData: {}", err); - 3 - } - } -} - unsafe fn arbitrator_load_machine_impl( binary_path: *const c_char, library_paths: *const *const c_char, diff --git a/arbitrator/prover/src/parse_input.rs b/arbitrator/prover/src/parse_input.rs index edfaddf26f..fa7adb4c41 100644 --- a/arbitrator/prover/src/parse_input.rs +++ b/arbitrator/prover/src/parse_input.rs @@ -1,14 +1,12 @@ use arbutil::Bytes32; use serde::Deserialize; -use serde::Serialize; use serde_json; use serde_with::base64::Base64; use serde_with::As; use serde_with::DisplayFromStr; use std::{ collections::HashMap, - fs::File, - io::{self, BufRead, BufWriter}, + io::{self, BufRead}, }; /// prefixed_hex deserializes hex strings which are prefixed with `0x` @@ -18,7 +16,7 @@ use std::{ /// It is an error to use this deserializer on a string that does not /// begin with `0x`. mod prefixed_hex { - use serde::{self, Deserialize, Deserializer, Serializer}; + use serde::{self, Deserialize, Deserializer}; pub fn deserialize<'de, D>(deserializer: D) -> Result, D::Error> where @@ -31,14 +29,6 @@ mod prefixed_hex { Err(serde::de::Error::custom("missing 0x prefix")) } } - - pub fn serialize(bytes: &Vec, serializer: S) -> Result - where - S: Serializer, - { - let hex_string = format!("0x{}", hex::encode(bytes)); - serializer.serialize_str(&hex_string) - } } #[derive(Debug)] @@ -71,7 +61,7 @@ impl From> for UserWasm { } } -#[derive(Debug, Clone, Deserialize, Serialize)] +#[derive(Debug, Clone, Deserialize)] #[serde(rename_all = "PascalCase")] pub struct BatchInfo { pub number: u64, @@ -79,7 +69,7 @@ pub struct BatchInfo { pub data_b64: Vec, } -#[derive(Debug, Deserialize, Serialize)] +#[derive(Debug, Deserialize)] #[serde(rename_all = "PascalCase")] pub struct StartState { #[serde(with = "prefixed_hex")] @@ -98,7 +88,7 @@ pub struct StartState { /// /// Note: It is important to change this file whenever the go JSON /// serialization changes. -#[derive(Debug, Deserialize, Serialize)] +#[derive(Debug, Deserialize)] #[serde(rename_all = "PascalCase")] pub struct FileData { pub id: u64, @@ -119,11 +109,4 @@ impl FileData { let data = serde_json::from_reader(&mut reader)?; Ok(data) } - - pub fn write_to_file(&self, file_path: &str) -> io::Result<()> { - let file = File::create(file_path)?; - let writer = BufWriter::new(file); - serde_json::to_writer_pretty(writer, &self)?; - Ok(()) - } } diff --git a/system_tests/common_test.go b/system_tests/common_test.go index dbb2b86f13..95e8883e6b 100644 --- a/system_tests/common_test.go +++ b/system_tests/common_test.go @@ -36,7 +36,6 @@ import ( "github.com/offchainlabs/nitro/util/headerreader" "github.com/offchainlabs/nitro/util/redisutil" "github.com/offchainlabs/nitro/util/signature" - "github.com/offchainlabs/nitro/validator/inputs" "github.com/offchainlabs/nitro/validator/server_api" "github.com/offchainlabs/nitro/validator/server_common" "github.com/offchainlabs/nitro/validator/valnode" @@ -1719,7 +1718,7 @@ func logParser[T any](t *testing.T, source string, name string) func(*types.Log) // recordBlock writes a json file with all of the data needed to validate a block. // // This can be used as an input to the arbitrator prover to validate a block. -func recordBlock(t *testing.T, block uint64, builder *NodeBuilder) { +func recordBlock(t *testing.T, block uint64, builder *NodeBuilder, blockInputJSONPath string) { t.Helper() ctx := builder.ctx inboxPos := arbutil.MessageIndex(block) @@ -1733,15 +1732,14 @@ func recordBlock(t *testing.T, block uint64, builder *NodeBuilder) { break } } - validationInputsWriter, err := inputs.NewWriter(inputs.WithSlug(t.Name())) - Require(t, err) inputJson, err := builder.L2.ConsensusNode.StatelessBlockValidator.ValidationInputsAt(ctx, inboxPos, rawdb.TargetWavm) if err != nil { Fatal(t, "failed to get validation inputs", block, err) } - if err := validationInputsWriter.Write(&inputJson); err != nil { - Fatal(t, "failed to write validation inputs", block, err) - } + contents, err := json.Marshal(inputJson) + Require(t, err) + err = os.WriteFile(blockInputJSONPath, contents, 0600) + Require(t, err) } func populateMachineDir(t *testing.T, cr *github.ConsensusRelease) string { diff --git a/system_tests/program_test.go b/system_tests/program_test.go index 4755096b26..d2c3336af7 100644 --- a/system_tests/program_test.go +++ b/system_tests/program_test.go @@ -425,7 +425,10 @@ func storageTest(t *testing.T, jit bool) { // Captures a block_input_.json file for the block that included the // storage write transaction. - recordBlock(t, receipt.BlockNumber.Uint64(), builder) + blockInputJSONPath := os.Getenv("BLOCK_INPUT_JSON_PATH") + if blockInputJSONPath != "" { + recordBlock(t, receipt.BlockNumber.Uint64(), builder, blockInputJSONPath) + } } func TestProgramTransientStorage(t *testing.T) { diff --git a/system_tests/validationinputjson_rustfiledata_test.go b/system_tests/validationinputjson_rustfiledata_test.go deleted file mode 100644 index a27a68b12c..0000000000 --- a/system_tests/validationinputjson_rustfiledata_test.go +++ /dev/null @@ -1,120 +0,0 @@ -package arbtest - -import ( - "encoding/base64" - "encoding/json" - "fmt" - "os" - "path/filepath" - "reflect" - "testing" - - "github.com/ethereum/go-ethereum/common" - "github.com/ethereum/go-ethereum/ethdb" - - "github.com/offchainlabs/nitro/arbutil" - "github.com/offchainlabs/nitro/validator" - "github.com/offchainlabs/nitro/validator/server_api" - "github.com/offchainlabs/nitro/validator/server_arb" -) - -func getWriteDataFromRustSide(t *testing.T, inputJSON *server_api.InputJSON) []byte { - t.Helper() - dir := t.TempDir() - - readData, err := inputJSON.Marshal() - Require(t, err) - readPath := filepath.Join(dir, fmt.Sprintf("block_inputs_%d_read.json", inputJSON.Id)) - Require(t, os.WriteFile(readPath, readData, 0600)) - - writePath := filepath.Join(dir, fmt.Sprintf("block_inputs_%d_write.json", inputJSON.Id)) - Require(t, server_arb.DeserializeAndSerializeFileData(readPath, writePath)) - writeData, err := os.ReadFile(writePath) - Require(t, err) - - return writeData -} - -func TestGoInputJSONRustFileDataRoundtripWithoutUserWasms(t *testing.T) { - preimages := make(map[arbutil.PreimageType]map[common.Hash][]byte) - preimages[arbutil.Keccak256PreimageType] = make(map[common.Hash][]byte) - preimages[arbutil.Keccak256PreimageType][common.MaxHash] = []byte{1} - - // Don't include DebugChain as it isn't used on rust side - sampleValidationInput := &validator.ValidationInput{ - Id: 1, - HasDelayedMsg: true, - DelayedMsgNr: 2, - Preimages: preimages, - BatchInfo: []validator.BatchInfo{{Number: 3}}, - DelayedMsg: []byte{4}, - StartState: validator.GoGlobalState{ - BlockHash: common.MaxHash, - SendRoot: common.MaxHash, - Batch: 5, - PosInBatch: 6, - }, - } - sampleValidationInputJSON := server_api.ValidationInputToJson(sampleValidationInput) - writeData := getWriteDataFromRustSide(t, sampleValidationInputJSON) - - var resWithoutUserWasms server_api.InputJSON - Require(t, json.Unmarshal(writeData, &resWithoutUserWasms)) - if !reflect.DeepEqual(*sampleValidationInputJSON, resWithoutUserWasms) { - t.Fatal("ValidationInputJSON without UserWasms, mismatch on rust and go side") - } - -} - -type inputJSONWithUserWasmsOnly struct { - UserWasms map[ethdb.WasmTarget]map[common.Hash][]byte -} - -// UnmarshalJSON is a custom function defined to encapsulate how UserWasms are handled on the rust side. -// When ValidationInputToJson is called on ValidationInput, it compresses the wasm data byte array and -// then encodes this to a base64 string, this when deserialized on the rust side through FileData- the -// compressed data is first uncompressed and also the module hash (Bytes32) is read without the 0x prefix, -// so we define a custom UnmarshalJSON to extract UserWasms map from the data written by rust side. -func (u *inputJSONWithUserWasmsOnly) UnmarshalJSON(data []byte) error { - type rawUserWasms struct { - UserWasms map[ethdb.WasmTarget]map[string]string - } - var rawUWs rawUserWasms - if err := json.Unmarshal(data, &rawUWs); err != nil { - return err - } - tmp := make(map[ethdb.WasmTarget]map[common.Hash][]byte) - for wasmTarget, innerMap := range rawUWs.UserWasms { - tmp[wasmTarget] = make(map[common.Hash][]byte) - for hashKey, value := range innerMap { - valBytes, err := base64.StdEncoding.DecodeString(value) - if err != nil { - return err - } - tmp[wasmTarget][common.HexToHash("0x"+hashKey)] = valBytes - } - } - u.UserWasms = tmp - return nil -} - -func TestGoInputJSONRustFileDataRoundtripWithUserWasms(t *testing.T) { - userWasms := make(map[ethdb.WasmTarget]map[common.Hash][]byte) - userWasms["arch1"] = make(map[common.Hash][]byte) - userWasms["arch1"][common.MaxHash] = []byte{2} - - // Don't include DebugChain as it isn't used on rust side - sampleValidationInput := &validator.ValidationInput{ - Id: 1, - UserWasms: userWasms, - BatchInfo: []validator.BatchInfo{{Number: 1}}, // This needs to be set for FileData to successfully deserialize, else it errors for invalid type null - } - sampleValidationInputJSON := server_api.ValidationInputToJson(sampleValidationInput) - writeData := getWriteDataFromRustSide(t, sampleValidationInputJSON) - - var resUserWasmsOnly inputJSONWithUserWasmsOnly - Require(t, json.Unmarshal(writeData, &resUserWasmsOnly)) - if !reflect.DeepEqual(userWasms, resUserWasmsOnly.UserWasms) { - t.Fatal("ValidationInputJSON with UserWasms only, mismatch on rust and go side") - } -} diff --git a/validator/server_arb/machine.go b/validator/server_arb/machine.go index d11f015916..1e73e6b212 100644 --- a/validator/server_arb/machine.go +++ b/validator/server_arb/machine.go @@ -312,20 +312,6 @@ func (m *ArbitratorMachine) DeserializeAndReplaceState(path string) error { } } -func DeserializeAndSerializeFileData(readPath, writePath string) error { - cReadPath := C.CString(readPath) - cWritePath := C.CString(writePath) - status := C.arbitrator_deserialize_and_serialize_file_data(cReadPath, cWritePath) - C.free(unsafe.Pointer(cReadPath)) - C.free(unsafe.Pointer(cWritePath)) - - if status != 0 { - return fmt.Errorf("failed to call arbitrator_deserialize_and_serialize_file_data. Error code: %d", status) - } else { - return nil - } -} - func (m *ArbitratorMachine) AddSequencerInboxMessage(index uint64, data []byte) error { defer runtime.KeepAlive(m) m.mutex.Lock() From f0d14fb2f681d4f10c4844bf83141cec1380d98d Mon Sep 17 00:00:00 2001 From: Ganesh Vanahalli Date: Thu, 10 Oct 2024 12:33:35 +0530 Subject: [PATCH 13/38] fix ci step and comment other steps in test(defaults) to speedup verification. will be undone after debug fix env variable setting impl fix gotestsum invocation fix gotestsum invocation fix gotestsum invocation --- .github/workflows/ci.yml | 45 ++++++++++++++++++++-------------------- 1 file changed, 22 insertions(+), 23 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 0b5592dafc..bd69e8c905 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -140,21 +140,21 @@ jobs: echo "GOGC=80" >> "$GITHUB_ENV" echo "GITHUB_TOKEN=${{ secrets.GITHUB_TOKEN }}" >> "$GITHUB_ENV" - - name: run tests without race detection and path state scheme - if: matrix.test-mode == 'defaults' - env: - TEST_STATE_SCHEME: path - run: | - echo "Running tests with Path Scheme" >> full.log - ${{ github.workspace }}/.github/workflows/gotestsum.sh --tags cionly --timeout 20m --cover - - - name: run tests without race detection and hash state scheme - if: matrix.test-mode == 'defaults' - env: - TEST_STATE_SCHEME: hash - run: | - echo "Running tests with Hash Scheme" >> full.log - ${{ github.workspace }}/.github/workflows/gotestsum.sh --tags cionly --timeout 20m + # - name: run tests without race detection and path state scheme + # if: matrix.test-mode == 'defaults' + # env: + # TEST_STATE_SCHEME: path + # run: | + # echo "Running tests with Path Scheme" >> full.log + # ${{ github.workspace }}/.github/workflows/gotestsum.sh --tags cionly --timeout 20m --cover + + # - name: run tests without race detection and hash state scheme + # if: matrix.test-mode == 'defaults' + # env: + # TEST_STATE_SCHEME: hash + # run: | + # echo "Running tests with Hash Scheme" >> full.log + # ${{ github.workspace }}/.github/workflows/gotestsum.sh --tags cionly --timeout 20m - name: run tests with race detection and hash state scheme if: matrix.test-mode == 'race' @@ -164,23 +164,22 @@ jobs: echo "Running tests with Hash Scheme" >> full.log ${{ github.workspace }}/.github/workflows/gotestsum.sh --race --timeout 30m - - name: run redis tests - if: matrix.test-mode == 'defaults' - run: | - echo "Running redis tests" >> full.log - TEST_REDIS=redis://localhost:6379/0 gotestsum --format short-verbose -- -p 1 -run TestRedis ./arbnode/... ./system_tests/... -coverprofile=coverage-redis.txt -covermode=atomic -coverpkg=./... + # - name: run redis tests + # if: matrix.test-mode == 'defaults' + # run: | + # echo "Running redis tests" >> full.log + # TEST_REDIS=redis://localhost:6379/0 gotestsum --format short-verbose -- -p 1 -run TestRedis ./arbnode/... ./system_tests/... -coverprofile=coverage-redis.txt -covermode=atomic -coverpkg=./... - name: create block input json file if: matrix.test-mode == 'defaults' run: | - echo "BLOCK_INPUT_JSON_PATH=$(pwd)/target/block_input.json" >> "$GITHUB_ENV" - ${{ github.workspace }}/.github/workflows/gotestsum.sh --run TestProgramStorage$ --count 1 + BLOCK_INPUT_JSON_PATH="${{ github.workspace }}/target/block_input.json" gotestsum --format short-verbose -- -run TestProgramStorage$ --count 1 - name: run arbitrator prover on block input json if: matrix.test-mode == 'defaults' run: | make build-prover-bin - target/bin/prover target/machines/latest/machine.wavm.br -b --json-inputs=$BLOCK_INPUT_JSON_PATH + target/bin/prover target/machines/latest/machine.wavm.br -b --json-inputs="${{ github.workspace }}/target/block_input.json" - name: run challenge tests if: matrix.test-mode == 'challenge' From d50926ed69c02028ddce01bcb32732d0324dd7a7 Mon Sep 17 00:00:00 2001 From: Ganesh Vanahalli Date: Thu, 10 Oct 2024 13:44:18 +0530 Subject: [PATCH 14/38] fix gotestsum invocation --- .github/workflows/ci.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index bd69e8c905..c194a4936b 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -173,7 +173,7 @@ jobs: - name: create block input json file if: matrix.test-mode == 'defaults' run: | - BLOCK_INPUT_JSON_PATH="${{ github.workspace }}/target/block_input.json" gotestsum --format short-verbose -- -run TestProgramStorage$ --count 1 + BLOCK_INPUT_JSON_PATH="${{ github.workspace }}/target/block_input.json" gotestsum --format short-verbose -- -run TestProgramStorage$ ./system_tests/... --count 1 - name: run arbitrator prover on block input json if: matrix.test-mode == 'defaults' From a1cb06dfd95c4043234eef0c1eba0fd932af4459 Mon Sep 17 00:00:00 2001 From: Ganesh Vanahalli Date: Thu, 10 Oct 2024 13:55:47 +0530 Subject: [PATCH 15/38] uncomment CI steps --- .github/workflows/ci.yml | 40 ++++++++++++++++++++-------------------- 1 file changed, 20 insertions(+), 20 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index c194a4936b..dcb3b9f422 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -140,21 +140,21 @@ jobs: echo "GOGC=80" >> "$GITHUB_ENV" echo "GITHUB_TOKEN=${{ secrets.GITHUB_TOKEN }}" >> "$GITHUB_ENV" - # - name: run tests without race detection and path state scheme - # if: matrix.test-mode == 'defaults' - # env: - # TEST_STATE_SCHEME: path - # run: | - # echo "Running tests with Path Scheme" >> full.log - # ${{ github.workspace }}/.github/workflows/gotestsum.sh --tags cionly --timeout 20m --cover - - # - name: run tests without race detection and hash state scheme - # if: matrix.test-mode == 'defaults' - # env: - # TEST_STATE_SCHEME: hash - # run: | - # echo "Running tests with Hash Scheme" >> full.log - # ${{ github.workspace }}/.github/workflows/gotestsum.sh --tags cionly --timeout 20m + - name: run tests without race detection and path state scheme + if: matrix.test-mode == 'defaults' + env: + TEST_STATE_SCHEME: path + run: | + echo "Running tests with Path Scheme" >> full.log + ${{ github.workspace }}/.github/workflows/gotestsum.sh --tags cionly --timeout 20m --cover + + - name: run tests without race detection and hash state scheme + if: matrix.test-mode == 'defaults' + env: + TEST_STATE_SCHEME: hash + run: | + echo "Running tests with Hash Scheme" >> full.log + ${{ github.workspace }}/.github/workflows/gotestsum.sh --tags cionly --timeout 20m - name: run tests with race detection and hash state scheme if: matrix.test-mode == 'race' @@ -164,11 +164,11 @@ jobs: echo "Running tests with Hash Scheme" >> full.log ${{ github.workspace }}/.github/workflows/gotestsum.sh --race --timeout 30m - # - name: run redis tests - # if: matrix.test-mode == 'defaults' - # run: | - # echo "Running redis tests" >> full.log - # TEST_REDIS=redis://localhost:6379/0 gotestsum --format short-verbose -- -p 1 -run TestRedis ./arbnode/... ./system_tests/... -coverprofile=coverage-redis.txt -covermode=atomic -coverpkg=./... + - name: run redis tests + if: matrix.test-mode == 'defaults' + run: | + echo "Running redis tests" >> full.log + TEST_REDIS=redis://localhost:6379/0 gotestsum --format short-verbose -- -p 1 -run TestRedis ./arbnode/... ./system_tests/... -coverprofile=coverage-redis.txt -covermode=atomic -coverpkg=./... - name: create block input json file if: matrix.test-mode == 'defaults' From 70c5b3f40c4851b682208cdb6a645a84eca25160 Mon Sep 17 00:00:00 2001 From: Ganesh Vanahalli Date: Thu, 10 Oct 2024 14:56:05 +0530 Subject: [PATCH 16/38] add ci step to run jit binary on the block input json file --- .github/workflows/ci.yml | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index dcb3b9f422..6722b38ed6 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -180,6 +180,12 @@ jobs: run: | make build-prover-bin target/bin/prover target/machines/latest/machine.wavm.br -b --json-inputs="${{ github.workspace }}/target/block_input.json" + + - name: run jit prover on block input json + if: matrix.test-mode == 'defaults' + run: | + make build-jit + target/bin/jit --binary target/machines/latest/replay.wasm --cranelift --json-inputs="${{ github.workspace }}/target/block_input.json" - name: run challenge tests if: matrix.test-mode == 'challenge' From 62d76d08d653e846aff3b5275c207d9aa2223db3 Mon Sep 17 00:00:00 2001 From: Ganesh Vanahalli Date: Thu, 10 Oct 2024 16:02:06 +0530 Subject: [PATCH 17/38] check if theres a local target mismatch on go and rust side --- .github/workflows/ci.yml | 52 +++++++++++++++++------------------ arbitrator/jit/src/prepare.rs | 2 +- system_tests/program_test.go | 1 + 3 files changed, 28 insertions(+), 27 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 6722b38ed6..d50fe5da59 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -140,21 +140,21 @@ jobs: echo "GOGC=80" >> "$GITHUB_ENV" echo "GITHUB_TOKEN=${{ secrets.GITHUB_TOKEN }}" >> "$GITHUB_ENV" - - name: run tests without race detection and path state scheme - if: matrix.test-mode == 'defaults' - env: - TEST_STATE_SCHEME: path - run: | - echo "Running tests with Path Scheme" >> full.log - ${{ github.workspace }}/.github/workflows/gotestsum.sh --tags cionly --timeout 20m --cover - - - name: run tests without race detection and hash state scheme - if: matrix.test-mode == 'defaults' - env: - TEST_STATE_SCHEME: hash - run: | - echo "Running tests with Hash Scheme" >> full.log - ${{ github.workspace }}/.github/workflows/gotestsum.sh --tags cionly --timeout 20m + # - name: run tests without race detection and path state scheme + # if: matrix.test-mode == 'defaults' + # env: + # TEST_STATE_SCHEME: path + # run: | + # echo "Running tests with Path Scheme" >> full.log + # ${{ github.workspace }}/.github/workflows/gotestsum.sh --tags cionly --timeout 20m --cover + + # - name: run tests without race detection and hash state scheme + # if: matrix.test-mode == 'defaults' + # env: + # TEST_STATE_SCHEME: hash + # run: | + # echo "Running tests with Hash Scheme" >> full.log + # ${{ github.workspace }}/.github/workflows/gotestsum.sh --tags cionly --timeout 20m - name: run tests with race detection and hash state scheme if: matrix.test-mode == 'race' @@ -164,22 +164,22 @@ jobs: echo "Running tests with Hash Scheme" >> full.log ${{ github.workspace }}/.github/workflows/gotestsum.sh --race --timeout 30m - - name: run redis tests - if: matrix.test-mode == 'defaults' - run: | - echo "Running redis tests" >> full.log - TEST_REDIS=redis://localhost:6379/0 gotestsum --format short-verbose -- -p 1 -run TestRedis ./arbnode/... ./system_tests/... -coverprofile=coverage-redis.txt -covermode=atomic -coverpkg=./... + # - name: run redis tests + # if: matrix.test-mode == 'defaults' + # run: | + # echo "Running redis tests" >> full.log + # TEST_REDIS=redis://localhost:6379/0 gotestsum --format short-verbose -- -p 1 -run TestRedis ./arbnode/... ./system_tests/... -coverprofile=coverage-redis.txt -covermode=atomic -coverpkg=./... - name: create block input json file if: matrix.test-mode == 'defaults' run: | - BLOCK_INPUT_JSON_PATH="${{ github.workspace }}/target/block_input.json" gotestsum --format short-verbose -- -run TestProgramStorage$ ./system_tests/... --count 1 + BLOCK_INPUT_JSON_PATH="${{ github.workspace }}/target/block_input.json" gotestsum --format standard-verbose -- -run TestProgramStorage$ ./system_tests/... --count 1 - - name: run arbitrator prover on block input json - if: matrix.test-mode == 'defaults' - run: | - make build-prover-bin - target/bin/prover target/machines/latest/machine.wavm.br -b --json-inputs="${{ github.workspace }}/target/block_input.json" + # - name: run arbitrator prover on block input json + # if: matrix.test-mode == 'defaults' + # run: | + # make build-prover-bin + # target/bin/prover target/machines/latest/machine.wavm.br -b --json-inputs="${{ github.workspace }}/target/block_input.json" - name: run jit prover on block input json if: matrix.test-mode == 'defaults' diff --git a/arbitrator/jit/src/prepare.rs b/arbitrator/jit/src/prepare.rs index 6c1b0b84de..b766936f3e 100644 --- a/arbitrator/jit/src/prepare.rs +++ b/arbitrator/jit/src/prepare.rs @@ -68,6 +68,6 @@ pub fn prepare_env(json_inputs: PathBuf, debug: bool) -> eyre::Result { .insert(*module_hash, module_asm.as_vec().into()); } } - + eprintln!("localTarget {}", local_target()); Ok(env) } diff --git a/system_tests/program_test.go b/system_tests/program_test.go index 92aeddaf53..1f195630a6 100644 --- a/system_tests/program_test.go +++ b/system_tests/program_test.go @@ -429,6 +429,7 @@ func storageTest(t *testing.T, jit bool) { if blockInputJSONPath != "" { recordBlock(t, receipt.BlockNumber.Uint64(), builder, []ethdb.WasmTarget{rawdb.TargetWavm, rawdb.LocalTarget()}, blockInputJSONPath) } + log.Info("printing debug info", "localTarget", rawdb.LocalTarget()) } func TestProgramTransientStorage(t *testing.T) { From 93fca3d126de1e64fefaf1a1ce1223b5b0fbf12c Mon Sep 17 00:00:00 2001 From: Ganesh Vanahalli Date: Thu, 10 Oct 2024 16:17:02 +0530 Subject: [PATCH 18/38] add more debug info --- arbitrator/jit/src/prepare.rs | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/arbitrator/jit/src/prepare.rs b/arbitrator/jit/src/prepare.rs index b766936f3e..68cdcd3479 100644 --- a/arbitrator/jit/src/prepare.rs +++ b/arbitrator/jit/src/prepare.rs @@ -68,6 +68,13 @@ pub fn prepare_env(json_inputs: PathBuf, debug: bool) -> eyre::Result { .insert(*module_hash, module_asm.as_vec().into()); } } + + eprintln!( + "env Info. OS- {}, Arch- {}", + env::consts::OS, + env::consts::ARCH + ); eprintln!("localTarget {}", local_target()); + Ok(env) } From 5e355bc623e30b1ccc787bead4c6161ccf301639 Mon Sep 17 00:00:00 2001 From: Ganesh Vanahalli Date: Thu, 10 Oct 2024 16:33:10 +0530 Subject: [PATCH 19/38] check if jit proving succeeds in CI --- .github/workflows/ci.yml | 7 +++++-- arbitrator/jit/src/prepare.rs | 11 ++--------- system_tests/program_test.go | 1 - 3 files changed, 7 insertions(+), 12 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index d50fe5da59..40215fa163 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -173,7 +173,7 @@ jobs: - name: create block input json file if: matrix.test-mode == 'defaults' run: | - BLOCK_INPUT_JSON_PATH="${{ github.workspace }}/target/block_input.json" gotestsum --format standard-verbose -- -run TestProgramStorage$ ./system_tests/... --count 1 + BLOCK_INPUT_JSON_PATH="${{ github.workspace }}/target/block_input.json" gotestsum --format short-verbose -- -run TestProgramStorage$ ./system_tests/... --count 1 # - name: run arbitrator prover on block input json # if: matrix.test-mode == 'defaults' @@ -185,7 +185,10 @@ jobs: if: matrix.test-mode == 'defaults' run: | make build-jit - target/bin/jit --binary target/machines/latest/replay.wasm --cranelift --json-inputs="${{ github.workspace }}/target/block_input.json" + if [ -n "$(target/bin/jit --binary target/machines/latest/replay.wasm --cranelift --json-inputs='${{ github.workspace }}/target/block_input.json')" ]; then + echo "Error: Command produced output." + exit 1 + fi - name: run challenge tests if: matrix.test-mode == 'challenge' diff --git a/arbitrator/jit/src/prepare.rs b/arbitrator/jit/src/prepare.rs index 68cdcd3479..e7a7ba0f4d 100644 --- a/arbitrator/jit/src/prepare.rs +++ b/arbitrator/jit/src/prepare.rs @@ -16,8 +16,8 @@ use std::path::PathBuf; pub fn local_target() -> String { if env::consts::OS == "linux" { match env::consts::ARCH { - "arm64" => "arm64".to_string(), - "amd64" => "amd64".to_string(), + "aarch64" => "arm64".to_string(), + "x86_64" => "amd64".to_string(), _ => "host".to_string(), } } else { @@ -69,12 +69,5 @@ pub fn prepare_env(json_inputs: PathBuf, debug: bool) -> eyre::Result { } } - eprintln!( - "env Info. OS- {}, Arch- {}", - env::consts::OS, - env::consts::ARCH - ); - eprintln!("localTarget {}", local_target()); - Ok(env) } diff --git a/system_tests/program_test.go b/system_tests/program_test.go index 1f195630a6..92aeddaf53 100644 --- a/system_tests/program_test.go +++ b/system_tests/program_test.go @@ -429,7 +429,6 @@ func storageTest(t *testing.T, jit bool) { if blockInputJSONPath != "" { recordBlock(t, receipt.BlockNumber.Uint64(), builder, []ethdb.WasmTarget{rawdb.TargetWavm, rawdb.LocalTarget()}, blockInputJSONPath) } - log.Info("printing debug info", "localTarget", rawdb.LocalTarget()) } func TestProgramTransientStorage(t *testing.T) { From 7259ae122d694f08b005d8efc0a461f8314d729e Mon Sep 17 00:00:00 2001 From: Ganesh Vanahalli Date: Thu, 10 Oct 2024 16:51:02 +0530 Subject: [PATCH 20/38] remove debug statements and comments --- .github/workflows/ci.yml | 50 ++++++++++++++++++++-------------------- 1 file changed, 25 insertions(+), 25 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 40215fa163..f0f44744f5 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -140,21 +140,21 @@ jobs: echo "GOGC=80" >> "$GITHUB_ENV" echo "GITHUB_TOKEN=${{ secrets.GITHUB_TOKEN }}" >> "$GITHUB_ENV" - # - name: run tests without race detection and path state scheme - # if: matrix.test-mode == 'defaults' - # env: - # TEST_STATE_SCHEME: path - # run: | - # echo "Running tests with Path Scheme" >> full.log - # ${{ github.workspace }}/.github/workflows/gotestsum.sh --tags cionly --timeout 20m --cover - - # - name: run tests without race detection and hash state scheme - # if: matrix.test-mode == 'defaults' - # env: - # TEST_STATE_SCHEME: hash - # run: | - # echo "Running tests with Hash Scheme" >> full.log - # ${{ github.workspace }}/.github/workflows/gotestsum.sh --tags cionly --timeout 20m + - name: run tests without race detection and path state scheme + if: matrix.test-mode == 'defaults' + env: + TEST_STATE_SCHEME: path + run: | + echo "Running tests with Path Scheme" >> full.log + ${{ github.workspace }}/.github/workflows/gotestsum.sh --tags cionly --timeout 20m --cover + + - name: run tests without race detection and hash state scheme + if: matrix.test-mode == 'defaults' + env: + TEST_STATE_SCHEME: hash + run: | + echo "Running tests with Hash Scheme" >> full.log + ${{ github.workspace }}/.github/workflows/gotestsum.sh --tags cionly --timeout 20m - name: run tests with race detection and hash state scheme if: matrix.test-mode == 'race' @@ -164,22 +164,22 @@ jobs: echo "Running tests with Hash Scheme" >> full.log ${{ github.workspace }}/.github/workflows/gotestsum.sh --race --timeout 30m - # - name: run redis tests - # if: matrix.test-mode == 'defaults' - # run: | - # echo "Running redis tests" >> full.log - # TEST_REDIS=redis://localhost:6379/0 gotestsum --format short-verbose -- -p 1 -run TestRedis ./arbnode/... ./system_tests/... -coverprofile=coverage-redis.txt -covermode=atomic -coverpkg=./... + - name: run redis tests + if: matrix.test-mode == 'defaults' + run: | + echo "Running redis tests" >> full.log + TEST_REDIS=redis://localhost:6379/0 gotestsum --format short-verbose -- -p 1 -run TestRedis ./arbnode/... ./system_tests/... -coverprofile=coverage-redis.txt -covermode=atomic -coverpkg=./... - name: create block input json file if: matrix.test-mode == 'defaults' run: | BLOCK_INPUT_JSON_PATH="${{ github.workspace }}/target/block_input.json" gotestsum --format short-verbose -- -run TestProgramStorage$ ./system_tests/... --count 1 - # - name: run arbitrator prover on block input json - # if: matrix.test-mode == 'defaults' - # run: | - # make build-prover-bin - # target/bin/prover target/machines/latest/machine.wavm.br -b --json-inputs="${{ github.workspace }}/target/block_input.json" + - name: run arbitrator prover on block input json + if: matrix.test-mode == 'defaults' + run: | + make build-prover-bin + target/bin/prover target/machines/latest/machine.wavm.br -b --json-inputs="${{ github.workspace }}/target/block_input.json" - name: run jit prover on block input json if: matrix.test-mode == 'defaults' From 057fcb3e40619fda5c00cc45c7f86579703e8276 Mon Sep 17 00:00:00 2001 From: Aman Sanghi Date: Thu, 10 Oct 2024 20:34:24 +0530 Subject: [PATCH 21/38] Create a build diagnostic tool --- diagnose.sh | 132 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 132 insertions(+) create mode 100755 diagnose.sh diff --git a/diagnose.sh b/diagnose.sh new file mode 100755 index 0000000000..8cfde2694f --- /dev/null +++ b/diagnose.sh @@ -0,0 +1,132 @@ +#!/bin/bash + +# Color codes +RED='\033[0;31m' +GREEN='\033[0;32m' +YELLOW='\033[1;33m' +BLUE='\033[0;34m' +NC='\033[0m' # No Color + +# Documentation link for installation instructions +INSTALLATION_DOCS_URL="Refer to https://docs.arbitrum.io/run-arbitrum-node/nitro/build-nitro-locally for installation." + +# Function to check if a command exists +command_exists() { + command -v "$1" >/dev/null 2>&1 +} + +# Detect operating system +OS=$(uname -s) +echo -e "${BLUE}Detected OS: $OS${NC}" +echo -e "${BLUE}Checking prerequisites for building Nitro locally...${NC}" + +# Step 1: Check Docker Installation +if command_exists docker; then + echo -e "${GREEN}Docker is installed.${NC}" +else + echo -e "${RED}Docker is not installed. $INSTALLATION_DOCS_URL${NC}" +fi + +# Step 2: Check if Docker service is running +if [[ "$OS" == "Linux" ]] && ! sudo service docker status >/dev/null; then + echo -e "${YELLOW}Docker service is not running on Linux. Start it with: sudo service docker start${NC}" +elif [[ "$OS" == "Darwin" ]] && ! docker info >/dev/null 2>&1; then + echo -e "${YELLOW}Docker service is not running on macOS. Ensure Docker Desktop is started.${NC}" +else + echo -e "${GREEN}Docker service is running.${NC}" +fi + +# Step 3: Check if the correct Nitro branch is checked out and submodules are updated +EXPECTED_BRANCH="v3.2.1" +CURRENT_BRANCH=$(git branch --show-current) +if [ "$CURRENT_BRANCH" != "$EXPECTED_BRANCH" ]; then + echo -e "${YELLOW}Switch to the correct branch using: git fetch origin && git checkout $EXPECTED_BRANCH${NC}" +else + echo -e "${GREEN}The Nitro repository is on the correct branch: $EXPECTED_BRANCH.${NC}" +fi + +# Check if submodules are properly initialized and updated +if git submodule status | grep -qE '^-|\+'; then + echo -e "${YELLOW}Submodules are not properly initialized or updated. Run: git submodule update --init --recursive --force${NC}" +else + echo -e "${GREEN}All submodules are properly initialized and up to date.${NC}" +fi + +# Step 4: Check if Nitro Docker Image is built +if docker images | grep -q "nitro-node"; then + echo -e "${GREEN}Nitro Docker image is built.${NC}" +else + echo -e "${RED}Nitro Docker image is not built. Build it using: docker build . --tag nitro-node${NC}" +fi + +# Step 5: Check prerequisites for building binaries +echo -e "${BLUE}Checking prerequisites for building Nitro's binaries...${NC}" +if [[ "$OS" == "Linux" ]]; then + prerequisites=(git curl build-essential cmake npm golang clang make gotestsum wasm2wat lld-13 python3 yarn) +else + prerequisites=(git curl make cmake npm go gvm golangci-lint wasm2wat clang gotestsum yarn) +fi + +for pkg in "${prerequisites[@]}"; do + if command_exists "$pkg"; then + [[ "$pkg" == "wasm2wat" ]] && pkg="wabt" + [[ "$pkg" == "clang" ]] && pkg="llvm" + + # Check for specific symbolic links related to wasm-ld on Linux and macOS + if [[ "$pkg" == "llvm" ]]; then + if [[ "$OS" == "Linux" ]]; then + if [ ! -L /usr/local/bin/wasm-ld ]; then + echo -e "${YELLOW}Creating symbolic link for wasm-ld on Linux.${NC}" + sudo ln -s /usr/bin/wasm-ld-13 /usr/local/bin/wasm-ld + else + echo -e "${GREEN}Symbolic link for wasm-ld on Linux is already present.${NC}" + fi + elif [[ "$OS" == "Darwin" ]]; then + if [ ! -L /usr/local/bin/wasm-ld ]; then + echo -e "${YELLOW}Creating symbolic link for wasm-ld on macOS.${NC}" + sudo mkdir -p /usr/local/bin + sudo ln -s /opt/homebrew/opt/llvm/bin/wasm-ld /usr/local/bin/wasm-ld + else + echo -e "${GREEN}Symbolic link for wasm-ld on macOS is already present.${NC}" + fi + fi + fi + + echo -e "${GREEN}$pkg is installed.${NC}" + else + [[ "$pkg" == "wasm2wat" ]] && pkg="wabt" + [[ "$pkg" == "clang" ]] && pkg="llvm" + echo -e "${RED}$pkg is not installed. Please install $pkg. $INSTALLATION_DOCS_URL${NC}" + fi +done + +# Step 6: Check Node.js version +if command_exists node && node -v | grep -q "v18"; then + echo -e "${GREEN}Node.js version 18 is installed.${NC}" +else + echo -e "${RED}Node.js version 18 not installed. $INSTALLATION_DOCS_URL${NC}" +fi + +# Step 7: Check Rust version +if command_exists rustc && rustc --version | grep -q "1.80.1"; then + echo -e "${GREEN}Rust version 1.80.1 is installed.${NC}" +else + echo -e "${RED}Rust version 1.80.1 not installed. $INSTALLATION_DOCS_URL${NC}" +fi + +# Step 8: Check Go version +if command_exists go && go version | grep -q "go1.23"; then + echo -e "${GREEN}Go version 1.23 is installed.${NC}" +else + echo -e "${RED}Go version 1.23 not installed. $INSTALLATION_DOCS_URL${NC}" +fi + +# Step 9: Check Foundry installation +if command_exists foundryup; then + echo -e "${GREEN}Foundry is installed.${NC}" +else + echo -e "${RED}Foundry is not installed. $INSTALLATION_DOCS_URL${NC}" +fi + +echo -e "${BLUE}Verification complete.${NC}" +echo -e "${YELLOW}Refer to https://docs.arbitrum.io/run-arbitrum-node/nitro/build-nitro-locally if the build fails for any other reason.${NC}" From 61348e4e570ffeced53714c1ae6141c4f7f46acf Mon Sep 17 00:00:00 2001 From: Aman Sanghi Date: Thu, 10 Oct 2024 20:46:19 +0530 Subject: [PATCH 22/38] minor changes --- diagnose.sh => prerequisite_nitro_local_build.sh | 4 ++++ 1 file changed, 4 insertions(+) rename diagnose.sh => prerequisite_nitro_local_build.sh (93%) diff --git a/diagnose.sh b/prerequisite_nitro_local_build.sh similarity index 93% rename from diagnose.sh rename to prerequisite_nitro_local_build.sh index 8cfde2694f..915ca0ae42 100755 --- a/diagnose.sh +++ b/prerequisite_nitro_local_build.sh @@ -1,4 +1,5 @@ #!/bin/bash +# This script checks the prerequisites for building Arbitrum Nitro locally. # Color codes RED='\033[0;31m' @@ -69,6 +70,9 @@ fi for pkg in "${prerequisites[@]}"; do if command_exists "$pkg"; then + # There is no way to check for wabt / llvm directly, since they install multiple tools + # So instead, we check for wasm2wat and clang, which are part of wabt and llvm respectively + # and if they are installed, we assume wabt / llvm is installed else we ask the user to install wabt / llvm [[ "$pkg" == "wasm2wat" ]] && pkg="wabt" [[ "$pkg" == "clang" ]] && pkg="llvm" From bb5e8b7456a491901e68fa9e8e8213ec240648b9 Mon Sep 17 00:00:00 2001 From: Ganesh Vanahalli Date: Thu, 10 Oct 2024 21:24:46 +0530 Subject: [PATCH 23/38] address PR comments --- arbnode/api.go | 2 +- staker/stateless_block_validator.go | 2 +- system_tests/common_test.go | 4 ++-- system_tests/program_test.go | 2 +- 4 files changed, 5 insertions(+), 5 deletions(-) diff --git a/arbnode/api.go b/arbnode/api.go index 8afb78770b..2dabd41bff 100644 --- a/arbnode/api.go +++ b/arbnode/api.go @@ -59,5 +59,5 @@ func (a *BlockValidatorDebugAPI) ValidateMessageNumber( func (a *BlockValidatorDebugAPI) ValidationInputsAt(ctx context.Context, msgNum hexutil.Uint64, target ethdb.WasmTarget, ) (server_api.InputJSON, error) { - return a.val.ValidationInputsAt(ctx, arbutil.MessageIndex(msgNum), []ethdb.WasmTarget{target}) + return a.val.ValidationInputsAt(ctx, arbutil.MessageIndex(msgNum), target) } diff --git a/staker/stateless_block_validator.go b/staker/stateless_block_validator.go index 786cf3aa94..d9c9c5446b 100644 --- a/staker/stateless_block_validator.go +++ b/staker/stateless_block_validator.go @@ -511,7 +511,7 @@ func (v *StatelessBlockValidator) ValidateResult( return true, &entry.End, nil } -func (v *StatelessBlockValidator) ValidationInputsAt(ctx context.Context, pos arbutil.MessageIndex, targets []ethdb.WasmTarget) (server_api.InputJSON, error) { +func (v *StatelessBlockValidator) ValidationInputsAt(ctx context.Context, pos arbutil.MessageIndex, targets ...ethdb.WasmTarget) (server_api.InputJSON, error) { entry, err := v.CreateReadyValidationEntry(ctx, pos) if err != nil { return server_api.InputJSON{}, err diff --git a/system_tests/common_test.go b/system_tests/common_test.go index 287882655f..6bc3fe4af8 100644 --- a/system_tests/common_test.go +++ b/system_tests/common_test.go @@ -1718,7 +1718,7 @@ func logParser[T any](t *testing.T, source string, name string) func(*types.Log) // recordBlock writes a json file with all of the data needed to validate a block. // // This can be used as an input to the arbitrator prover to validate a block. -func recordBlock(t *testing.T, block uint64, builder *NodeBuilder, targets []ethdb.WasmTarget, blockInputJSONPath string) { +func recordBlock(t *testing.T, block uint64, builder *NodeBuilder, blockInputJSONPath string, targets ...ethdb.WasmTarget) { t.Helper() ctx := builder.ctx inboxPos := arbutil.MessageIndex(block) @@ -1732,7 +1732,7 @@ func recordBlock(t *testing.T, block uint64, builder *NodeBuilder, targets []eth break } } - inputJson, err := builder.L2.ConsensusNode.StatelessBlockValidator.ValidationInputsAt(ctx, inboxPos, targets) + inputJson, err := builder.L2.ConsensusNode.StatelessBlockValidator.ValidationInputsAt(ctx, inboxPos, targets...) if err != nil { Fatal(t, "failed to get validation inputs", block, err) } diff --git a/system_tests/program_test.go b/system_tests/program_test.go index 92aeddaf53..9d5b173fc3 100644 --- a/system_tests/program_test.go +++ b/system_tests/program_test.go @@ -427,7 +427,7 @@ func storageTest(t *testing.T, jit bool) { // storage write transaction. Include wasm targets necessary for arbitrator prover and jit binaries blockInputJSONPath := os.Getenv("BLOCK_INPUT_JSON_PATH") if blockInputJSONPath != "" { - recordBlock(t, receipt.BlockNumber.Uint64(), builder, []ethdb.WasmTarget{rawdb.TargetWavm, rawdb.LocalTarget()}, blockInputJSONPath) + recordBlock(t, receipt.BlockNumber.Uint64(), builder, blockInputJSONPath, rawdb.TargetWavm, rawdb.LocalTarget()) } } From f83a10fd3d98b25d831c8a94df74003f26b0dd35 Mon Sep 17 00:00:00 2001 From: Ganesh Vanahalli Date: Fri, 11 Oct 2024 19:22:27 +0530 Subject: [PATCH 24/38] use inputs.Writer to generate the json file --- .github/workflows/ci.yml | 6 ++--- system_tests/common_test.go | 16 +++++++++---- system_tests/program_test.go | 9 ++++--- validator/inputs/writer.go | 46 +++++++++++++++++++++++++----------- 4 files changed, 52 insertions(+), 25 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index f0f44744f5..c1ca8aa698 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -173,19 +173,19 @@ jobs: - name: create block input json file if: matrix.test-mode == 'defaults' run: | - BLOCK_INPUT_JSON_PATH="${{ github.workspace }}/target/block_input.json" gotestsum --format short-verbose -- -run TestProgramStorage$ ./system_tests/... --count 1 + gotestsum --format short-verbose -- -run TestProgramStorage$ ./system_tests/... --count 1 --validator.inputswriter.basedir=""${{ github.workspace }}/target" - name: run arbitrator prover on block input json if: matrix.test-mode == 'defaults' run: | make build-prover-bin - target/bin/prover target/machines/latest/machine.wavm.br -b --json-inputs="${{ github.workspace }}/target/block_input.json" + target/bin/prover target/machines/latest/machine.wavm.br -b --json-inputs="${{ github.workspace }}/target/block_inputs.json" - name: run jit prover on block input json if: matrix.test-mode == 'defaults' run: | make build-jit - if [ -n "$(target/bin/jit --binary target/machines/latest/replay.wasm --cranelift --json-inputs='${{ github.workspace }}/target/block_input.json')" ]; then + if [ -n "$(target/bin/jit --binary target/machines/latest/replay.wasm --cranelift --json-inputs='${{ github.workspace }}/target/block_inputs.json')" ]; then echo "Error: Command produced output." exit 1 fi diff --git a/system_tests/common_test.go b/system_tests/common_test.go index 6bc3fe4af8..189e83c965 100644 --- a/system_tests/common_test.go +++ b/system_tests/common_test.go @@ -36,6 +36,7 @@ import ( "github.com/offchainlabs/nitro/util/headerreader" "github.com/offchainlabs/nitro/util/redisutil" "github.com/offchainlabs/nitro/util/signature" + "github.com/offchainlabs/nitro/validator/inputs" "github.com/offchainlabs/nitro/validator/server_api" "github.com/offchainlabs/nitro/validator/server_common" "github.com/offchainlabs/nitro/validator/valnode" @@ -1718,7 +1719,7 @@ func logParser[T any](t *testing.T, source string, name string) func(*types.Log) // recordBlock writes a json file with all of the data needed to validate a block. // // This can be used as an input to the arbitrator prover to validate a block. -func recordBlock(t *testing.T, block uint64, builder *NodeBuilder, blockInputJSONPath string, targets ...ethdb.WasmTarget) { +func recordBlock(t *testing.T, block uint64, builder *NodeBuilder, baseDirectory string, targets ...ethdb.WasmTarget) { t.Helper() ctx := builder.ctx inboxPos := arbutil.MessageIndex(block) @@ -1732,14 +1733,19 @@ func recordBlock(t *testing.T, block uint64, builder *NodeBuilder, blockInputJSO break } } + validationInputsWriter, err := inputs.NewWriter( + inputs.WithBaseDir(baseDirectory), + inputs.WithTimestampDirEnabled(false), + inputs.WithBlockIdInFileNameEnabled(false), + ) + Require(t, err) inputJson, err := builder.L2.ConsensusNode.StatelessBlockValidator.ValidationInputsAt(ctx, inboxPos, targets...) if err != nil { Fatal(t, "failed to get validation inputs", block, err) } - contents, err := json.Marshal(inputJson) - Require(t, err) - err = os.WriteFile(blockInputJSONPath, contents, 0600) - Require(t, err) + if err := validationInputsWriter.Write(&inputJson); err != nil { + Fatal(t, "failed to write validation inputs", block, err) + } } func populateMachineDir(t *testing.T, cr *github.ConsensusRelease) string { diff --git a/system_tests/program_test.go b/system_tests/program_test.go index 9d5b173fc3..d4c64d7436 100644 --- a/system_tests/program_test.go +++ b/system_tests/program_test.go @@ -7,6 +7,7 @@ import ( "bytes" "context" "encoding/binary" + "flag" "fmt" "math" "math/big" @@ -393,6 +394,8 @@ func errorTest(t *testing.T, jit bool) { validateBlocks(t, 7, jit, builder) } +var validatorInputsWriterBaseDir = flag.String("validator.inputswriter.basedir", "", "Base directory for validationInputsWriter") + func TestProgramStorage(t *testing.T) { t.Parallel() storageTest(t, true) @@ -425,9 +428,9 @@ func storageTest(t *testing.T, jit bool) { // Captures a block_input_.json file for the block that included the // storage write transaction. Include wasm targets necessary for arbitrator prover and jit binaries - blockInputJSONPath := os.Getenv("BLOCK_INPUT_JSON_PATH") - if blockInputJSONPath != "" { - recordBlock(t, receipt.BlockNumber.Uint64(), builder, blockInputJSONPath, rawdb.TargetWavm, rawdb.LocalTarget()) + flag.Parse() + if *validatorInputsWriterBaseDir != "" { + recordBlock(t, receipt.BlockNumber.Uint64(), builder, *validatorInputsWriterBaseDir, rawdb.TargetWavm, rawdb.LocalTarget()) } } diff --git a/validator/inputs/writer.go b/validator/inputs/writer.go index a45e584f52..fc7456b8bf 100644 --- a/validator/inputs/writer.go +++ b/validator/inputs/writer.go @@ -17,20 +17,25 @@ import ( // // The path can be nested under a slug directory so callers can provide a // recognizable name to differentiate various contexts in which the InputJSON -// is being written. If the Writer is configured by calling SetSlug, then the +// is being written. If the Writer is configured by calling WithSlug, then the // path will be like: // // $HOME/.arbuitrum/validation-inputs///block_inputs_.json // +// The inclusion of BlockId in the file's name is on by default, however that can be disabled +// by calling WithBlockIdInFileNameEnabled(false). In which case, the path will be like: +// +// $HOME/.arbuitrum/validation-inputs///block_inputs.json +// // The inclusion of a timestamp directory is on by default to avoid conflicts which // would result in files being overwritten. However, the Writer can be configured // to not use a timestamp directory. If the Writer is configured by calling -// SetUseTimestampDir(false), then the path will be like: +// WithTimestampDirEnabled(false), then the path will be like: // // $HOME/.arbuitrum/validation-inputs//block_inputs_.json // // Finally, to give complete control to the clients, the base directory can be -// set directly with SetBaseDir. In which case, the path will be like: +// set directly with WithBaseDir. In which case, the path will be like: // // /block_inputs_.json // or @@ -38,10 +43,11 @@ import ( // or // ///block_inputs_.json type Writer struct { - clock Clock - baseDir string - slug string - useTimestampDir bool + clock Clock + baseDir string + slug string + useTimestampDir bool + useBlockIdInFileName bool } // WriterOption is a function that configures a Writer. @@ -66,10 +72,11 @@ func NewWriter(options ...WriterOption) (*Writer, error) { } baseDir := filepath.Join(homeDir, ".arbitrum", "validation-inputs") w := &Writer{ - clock: realClock{}, - baseDir: baseDir, - slug: "", - useTimestampDir: true, + clock: realClock{}, + baseDir: baseDir, + slug: "", + useTimestampDir: true, + useBlockIdInFileName: true, } for _, o := range options { o(w) @@ -114,6 +121,13 @@ func WithTimestampDirEnabled(useTimestampDir bool) WriterOption { } } +// WithBlockIdInFileNameEnabled controls the inclusion of Block Id in the input json file's name +func WithBlockIdInFileNameEnabled(useBlockIdInFileName bool) WriterOption { + return func(w *Writer) { + w.useBlockIdInFileName = useBlockIdInFileName + } +} + // Write writes the given InputJSON to a file in JSON format. func (w *Writer) Write(json *server_api.InputJSON) error { dir := w.baseDir @@ -132,9 +146,13 @@ func (w *Writer) Write(json *server_api.InputJSON) error { if err != nil { return err } - if err = os.WriteFile( - filepath.Join(dir, fmt.Sprintf("block_inputs_%d.json", json.Id)), - contents, 0600); err != nil { + var fileName string + if w.useBlockIdInFileName { + fileName = filepath.Join(dir, fmt.Sprintf("block_inputs_%d.json", json.Id)) + } else { + fileName = filepath.Join(dir, "block_inputs.json") + } + if err = os.WriteFile(fileName, contents, 0600); err != nil { return err } return nil From 05be76e4686254d9070884a6f221b535ff75e2de Mon Sep 17 00:00:00 2001 From: Ganesh Vanahalli Date: Fri, 11 Oct 2024 19:31:39 +0530 Subject: [PATCH 25/38] use inputs.Writer to generate the json file --- .github/workflows/ci.yml | 4 ++-- system_tests/common_test.go | 16 +++++++++---- system_tests/program_test.go | 11 +++++---- validator/inputs/writer.go | 46 +++++++++++++++++++++++++----------- 4 files changed, 52 insertions(+), 25 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index dcb3b9f422..671615233a 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -173,13 +173,13 @@ jobs: - name: create block input json file if: matrix.test-mode == 'defaults' run: | - BLOCK_INPUT_JSON_PATH="${{ github.workspace }}/target/block_input.json" gotestsum --format short-verbose -- -run TestProgramStorage$ ./system_tests/... --count 1 + gotestsum --format short-verbose -- -run TestProgramStorage$ ./system_tests/... --count 1 --validator.inputswriter.basedir=""${{ github.workspace }}/target" - name: run arbitrator prover on block input json if: matrix.test-mode == 'defaults' run: | make build-prover-bin - target/bin/prover target/machines/latest/machine.wavm.br -b --json-inputs="${{ github.workspace }}/target/block_input.json" + target/bin/prover target/machines/latest/machine.wavm.br -b --json-inputs="${{ github.workspace }}/target/block_inputs.json" - name: run challenge tests if: matrix.test-mode == 'challenge' diff --git a/system_tests/common_test.go b/system_tests/common_test.go index 95e8883e6b..f4e2edad42 100644 --- a/system_tests/common_test.go +++ b/system_tests/common_test.go @@ -36,6 +36,7 @@ import ( "github.com/offchainlabs/nitro/util/headerreader" "github.com/offchainlabs/nitro/util/redisutil" "github.com/offchainlabs/nitro/util/signature" + "github.com/offchainlabs/nitro/validator/inputs" "github.com/offchainlabs/nitro/validator/server_api" "github.com/offchainlabs/nitro/validator/server_common" "github.com/offchainlabs/nitro/validator/valnode" @@ -1718,7 +1719,7 @@ func logParser[T any](t *testing.T, source string, name string) func(*types.Log) // recordBlock writes a json file with all of the data needed to validate a block. // // This can be used as an input to the arbitrator prover to validate a block. -func recordBlock(t *testing.T, block uint64, builder *NodeBuilder, blockInputJSONPath string) { +func recordBlock(t *testing.T, block uint64, builder *NodeBuilder, baseDirectory string) { t.Helper() ctx := builder.ctx inboxPos := arbutil.MessageIndex(block) @@ -1732,14 +1733,19 @@ func recordBlock(t *testing.T, block uint64, builder *NodeBuilder, blockInputJSO break } } + validationInputsWriter, err := inputs.NewWriter( + inputs.WithBaseDir(baseDirectory), + inputs.WithTimestampDirEnabled(false), + inputs.WithBlockIdInFileNameEnabled(false), + ) + Require(t, err) inputJson, err := builder.L2.ConsensusNode.StatelessBlockValidator.ValidationInputsAt(ctx, inboxPos, rawdb.TargetWavm) if err != nil { Fatal(t, "failed to get validation inputs", block, err) } - contents, err := json.Marshal(inputJson) - Require(t, err) - err = os.WriteFile(blockInputJSONPath, contents, 0600) - Require(t, err) + if err := validationInputsWriter.Write(&inputJson); err != nil { + Fatal(t, "failed to write validation inputs", block, err) + } } func populateMachineDir(t *testing.T, cr *github.ConsensusRelease) string { diff --git a/system_tests/program_test.go b/system_tests/program_test.go index d2c3336af7..d007a40ddd 100644 --- a/system_tests/program_test.go +++ b/system_tests/program_test.go @@ -7,6 +7,7 @@ import ( "bytes" "context" "encoding/binary" + "flag" "fmt" "math" "math/big" @@ -393,6 +394,8 @@ func errorTest(t *testing.T, jit bool) { validateBlocks(t, 7, jit, builder) } +var validatorInputsWriterBaseDir = flag.String("validator.inputswriter.basedir", "", "Base directory for validationInputsWriter") + func TestProgramStorage(t *testing.T) { t.Parallel() storageTest(t, true) @@ -424,10 +427,10 @@ func storageTest(t *testing.T, jit bool) { validateBlocks(t, 2, jit, builder) // Captures a block_input_.json file for the block that included the - // storage write transaction. - blockInputJSONPath := os.Getenv("BLOCK_INPUT_JSON_PATH") - if blockInputJSONPath != "" { - recordBlock(t, receipt.BlockNumber.Uint64(), builder, blockInputJSONPath) + // storage write transaction. Include wasm targets necessary for arbitrator prover and jit binaries + flag.Parse() + if *validatorInputsWriterBaseDir != "" { + recordBlock(t, receipt.BlockNumber.Uint64(), builder, *validatorInputsWriterBaseDir) } } diff --git a/validator/inputs/writer.go b/validator/inputs/writer.go index a45e584f52..fc7456b8bf 100644 --- a/validator/inputs/writer.go +++ b/validator/inputs/writer.go @@ -17,20 +17,25 @@ import ( // // The path can be nested under a slug directory so callers can provide a // recognizable name to differentiate various contexts in which the InputJSON -// is being written. If the Writer is configured by calling SetSlug, then the +// is being written. If the Writer is configured by calling WithSlug, then the // path will be like: // // $HOME/.arbuitrum/validation-inputs///block_inputs_.json // +// The inclusion of BlockId in the file's name is on by default, however that can be disabled +// by calling WithBlockIdInFileNameEnabled(false). In which case, the path will be like: +// +// $HOME/.arbuitrum/validation-inputs///block_inputs.json +// // The inclusion of a timestamp directory is on by default to avoid conflicts which // would result in files being overwritten. However, the Writer can be configured // to not use a timestamp directory. If the Writer is configured by calling -// SetUseTimestampDir(false), then the path will be like: +// WithTimestampDirEnabled(false), then the path will be like: // // $HOME/.arbuitrum/validation-inputs//block_inputs_.json // // Finally, to give complete control to the clients, the base directory can be -// set directly with SetBaseDir. In which case, the path will be like: +// set directly with WithBaseDir. In which case, the path will be like: // // /block_inputs_.json // or @@ -38,10 +43,11 @@ import ( // or // ///block_inputs_.json type Writer struct { - clock Clock - baseDir string - slug string - useTimestampDir bool + clock Clock + baseDir string + slug string + useTimestampDir bool + useBlockIdInFileName bool } // WriterOption is a function that configures a Writer. @@ -66,10 +72,11 @@ func NewWriter(options ...WriterOption) (*Writer, error) { } baseDir := filepath.Join(homeDir, ".arbitrum", "validation-inputs") w := &Writer{ - clock: realClock{}, - baseDir: baseDir, - slug: "", - useTimestampDir: true, + clock: realClock{}, + baseDir: baseDir, + slug: "", + useTimestampDir: true, + useBlockIdInFileName: true, } for _, o := range options { o(w) @@ -114,6 +121,13 @@ func WithTimestampDirEnabled(useTimestampDir bool) WriterOption { } } +// WithBlockIdInFileNameEnabled controls the inclusion of Block Id in the input json file's name +func WithBlockIdInFileNameEnabled(useBlockIdInFileName bool) WriterOption { + return func(w *Writer) { + w.useBlockIdInFileName = useBlockIdInFileName + } +} + // Write writes the given InputJSON to a file in JSON format. func (w *Writer) Write(json *server_api.InputJSON) error { dir := w.baseDir @@ -132,9 +146,13 @@ func (w *Writer) Write(json *server_api.InputJSON) error { if err != nil { return err } - if err = os.WriteFile( - filepath.Join(dir, fmt.Sprintf("block_inputs_%d.json", json.Id)), - contents, 0600); err != nil { + var fileName string + if w.useBlockIdInFileName { + fileName = filepath.Join(dir, fmt.Sprintf("block_inputs_%d.json", json.Id)) + } else { + fileName = filepath.Join(dir, "block_inputs.json") + } + if err = os.WriteFile(fileName, contents, 0600); err != nil { return err } return nil From ceb785ef3237e85ce6a036855034d2be2682574a Mon Sep 17 00:00:00 2001 From: Ganesh Vanahalli Date: Fri, 11 Oct 2024 20:12:46 +0530 Subject: [PATCH 26/38] typo fix --- .github/workflows/ci.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 671615233a..660f9f4dea 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -173,7 +173,7 @@ jobs: - name: create block input json file if: matrix.test-mode == 'defaults' run: | - gotestsum --format short-verbose -- -run TestProgramStorage$ ./system_tests/... --count 1 --validator.inputswriter.basedir=""${{ github.workspace }}/target" + gotestsum --format short-verbose -- -run TestProgramStorage$ ./system_tests/... --count 1 --validator.inputswriter.basedir="${{ github.workspace }}/target" - name: run arbitrator prover on block input json if: matrix.test-mode == 'defaults' From 2dcc837ce179351bf4215c6a70eee9e2f3cbf358 Mon Sep 17 00:00:00 2001 From: Aman Sanghi Date: Fri, 11 Oct 2024 21:44:32 +0530 Subject: [PATCH 27/38] Changes based on PR comments --- .../check-build.sh | 33 +++++++++++++------ 1 file changed, 23 insertions(+), 10 deletions(-) rename prerequisite_nitro_local_build.sh => util/check-build.sh (86%) diff --git a/prerequisite_nitro_local_build.sh b/util/check-build.sh similarity index 86% rename from prerequisite_nitro_local_build.sh rename to util/check-build.sh index 915ca0ae42..755cfda9ad 100755 --- a/prerequisite_nitro_local_build.sh +++ b/util/check-build.sh @@ -26,29 +26,28 @@ if command_exists docker; then echo -e "${GREEN}Docker is installed.${NC}" else echo -e "${RED}Docker is not installed. $INSTALLATION_DOCS_URL${NC}" + exit 1 fi # Step 2: Check if Docker service is running if [[ "$OS" == "Linux" ]] && ! sudo service docker status >/dev/null; then echo -e "${YELLOW}Docker service is not running on Linux. Start it with: sudo service docker start${NC}" + exit 1 elif [[ "$OS" == "Darwin" ]] && ! docker info >/dev/null 2>&1; then echo -e "${YELLOW}Docker service is not running on macOS. Ensure Docker Desktop is started.${NC}" + exit 1 else echo -e "${GREEN}Docker service is running.${NC}" fi -# Step 3: Check if the correct Nitro branch is checked out and submodules are updated -EXPECTED_BRANCH="v3.2.1" -CURRENT_BRANCH=$(git branch --show-current) -if [ "$CURRENT_BRANCH" != "$EXPECTED_BRANCH" ]; then - echo -e "${YELLOW}Switch to the correct branch using: git fetch origin && git checkout $EXPECTED_BRANCH${NC}" -else - echo -e "${GREEN}The Nitro repository is on the correct branch: $EXPECTED_BRANCH.${NC}" -fi +# Step 3: Check the version tag +VERSION_TAG=$(git tag --points-at HEAD | sed '/-/!s/$/_/' | sort -rV | sed 's/_$//' | head -n 1 | grep ^ || git show -s --pretty=%D | sed 's/, /\n/g' | grep -v '^origin/' | grep -v '^grafted\|HEAD\|master\|main$' || echo "dev") +echo -e "${YELLOW}You are on the version tag: $VERSION_TAG${NC}" # Check if submodules are properly initialized and updated if git submodule status | grep -qE '^-|\+'; then echo -e "${YELLOW}Submodules are not properly initialized or updated. Run: git submodule update --init --recursive --force${NC}" + exit 1 else echo -e "${GREEN}All submodules are properly initialized and up to date.${NC}" fi @@ -58,6 +57,7 @@ if docker images | grep -q "nitro-node"; then echo -e "${GREEN}Nitro Docker image is built.${NC}" else echo -e "${RED}Nitro Docker image is not built. Build it using: docker build . --tag nitro-node${NC}" + exit 1 fi # Step 5: Check prerequisites for building binaries @@ -101,6 +101,7 @@ for pkg in "${prerequisites[@]}"; do [[ "$pkg" == "wasm2wat" ]] && pkg="wabt" [[ "$pkg" == "clang" ]] && pkg="llvm" echo -e "${RED}$pkg is not installed. Please install $pkg. $INSTALLATION_DOCS_URL${NC}" + exit 1 fi done @@ -109,13 +110,23 @@ if command_exists node && node -v | grep -q "v18"; then echo -e "${GREEN}Node.js version 18 is installed.${NC}" else echo -e "${RED}Node.js version 18 not installed. $INSTALLATION_DOCS_URL${NC}" + exit 1 fi -# Step 7: Check Rust version +# Step 7a: Check Rust version if command_exists rustc && rustc --version | grep -q "1.80.1"; then echo -e "${GREEN}Rust version 1.80.1 is installed.${NC}" else echo -e "${RED}Rust version 1.80.1 not installed. $INSTALLATION_DOCS_URL${NC}" + exit 1 +fi + +# Step 7b: Check Rust nightly toolchain +if rustup toolchain list | grep -q "nightly"; then + echo -e "${GREEN}Rust nightly toolchain is installed.${NC}" +else + echo -e "${RED}Rust nightly toolchain is not installed. Install it using: rustup toolchain install nightly${NC}" + exit 1 fi # Step 8: Check Go version @@ -123,6 +134,7 @@ if command_exists go && go version | grep -q "go1.23"; then echo -e "${GREEN}Go version 1.23 is installed.${NC}" else echo -e "${RED}Go version 1.23 not installed. $INSTALLATION_DOCS_URL${NC}" + exit 1 fi # Step 9: Check Foundry installation @@ -130,7 +142,8 @@ if command_exists foundryup; then echo -e "${GREEN}Foundry is installed.${NC}" else echo -e "${RED}Foundry is not installed. $INSTALLATION_DOCS_URL${NC}" + exit 1 fi echo -e "${BLUE}Verification complete.${NC}" -echo -e "${YELLOW}Refer to https://docs.arbitrum.io/run-arbitrum-node/nitro/build-nitro-locally if the build fails for any other reason.${NC}" +exit 0 From 4a12f9c8620260a7a388aad9ee747bf49693ba4a Mon Sep 17 00:00:00 2001 From: Lee Bousfield Date: Wed, 18 Sep 2024 10:13:59 -0500 Subject: [PATCH 28/38] Add Gas and Ink types in rust --- arbitrator/arbutil/src/evm/api.rs | 118 ++++++++++++++---- arbitrator/arbutil/src/evm/mod.rs | 49 ++++---- arbitrator/arbutil/src/evm/req.rs | 67 +++++----- arbitrator/arbutil/src/evm/storage.rs | 20 +-- arbitrator/arbutil/src/pricing.rs | 14 ++- arbitrator/jit/src/program.rs | 5 +- arbitrator/jit/src/stylus_backend.rs | 12 +- arbitrator/prover/src/machine.rs | 2 +- arbitrator/prover/src/merkle.rs | 5 +- arbitrator/prover/src/programs/config.rs | 9 +- arbitrator/prover/src/programs/memory.rs | 22 ++-- arbitrator/prover/src/programs/meter.rs | 40 +++--- arbitrator/prover/src/test.rs | 4 +- arbitrator/stylus/src/env.rs | 16 +-- arbitrator/stylus/src/evm_api.rs | 6 +- arbitrator/stylus/src/host.rs | 14 +-- arbitrator/stylus/src/lib.rs | 8 +- arbitrator/stylus/src/native.rs | 4 +- arbitrator/stylus/src/run.rs | 8 +- arbitrator/stylus/src/test/api.rs | 54 ++++---- arbitrator/stylus/src/test/mod.rs | 16 +-- arbitrator/stylus/src/test/native.rs | 22 ++-- arbitrator/stylus/src/test/wavm.rs | 11 +- .../wasm-libraries/user-host-trait/src/lib.rs | 28 ++--- 24 files changed, 319 insertions(+), 235 deletions(-) diff --git a/arbitrator/arbutil/src/evm/api.rs b/arbitrator/arbutil/src/evm/api.rs index 9d4c78c0de..a9f8d927fe 100644 --- a/arbitrator/arbutil/src/evm/api.rs +++ b/arbitrator/arbutil/src/evm/api.rs @@ -73,19 +73,93 @@ impl DataReader for VecReader { } } +macro_rules! derive_math { + ($t:ident) => { + impl std::ops::Add for $t { + type Output = Self; + + fn add(self, rhs: Self) -> Self { + Self(self.0 + rhs.0) + } + } + + impl std::ops::AddAssign for $t { + fn add_assign(&mut self, rhs: Self) { + self.0 += rhs.0; + } + } + + impl std::ops::Sub for $t { + type Output = Self; + + fn sub(self, rhs: Self) -> Self { + Self(self.0 - rhs.0) + } + } + + impl std::ops::SubAssign for $t { + fn sub_assign(&mut self, rhs: Self) { + self.0 -= rhs.0; + } + } + + impl std::ops::Mul for $t { + type Output = Self; + + fn mul(self, rhs: u64) -> Self { + Self(self.0 * rhs) + } + } + + impl std::ops::Mul<$t> for u64 { + type Output = $t; + + fn mul(self, rhs: $t) -> $t { + $t(self * rhs.0) + } + } + + impl $t { + pub const fn saturating_add(self, rhs: Self) -> Self { + Self(self.0.saturating_add(rhs.0)) + } + + pub const fn saturating_sub(self, rhs: Self) -> Self { + Self(self.0.saturating_sub(rhs.0)) + } + + pub fn to_be_bytes(self) -> [u8; 8] { + self.0.to_be_bytes() + } + } + }; +} + +#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)] +#[must_use] +pub struct Gas(pub u64); + +derive_math!(Gas); + +#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)] +#[must_use] +pub struct Ink(pub u64); + +derive_math!(Ink); + pub trait EvmApi: Send + 'static { /// Reads the 32-byte value in the EVM state trie at offset `key`. /// Returns the value and the access cost in gas. /// Analogous to `vm.SLOAD`. - fn get_bytes32(&mut self, key: Bytes32, evm_api_gas_to_use: u64) -> (Bytes32, u64); + fn get_bytes32(&mut self, key: Bytes32, evm_api_gas_to_use: Gas) -> (Bytes32, Gas); /// Stores the given value at the given key in Stylus VM's cache of the EVM state trie. /// Note that the actual values only get written after calls to `set_trie_slots`. - fn cache_bytes32(&mut self, key: Bytes32, value: Bytes32) -> u64; + fn cache_bytes32(&mut self, key: Bytes32, value: Bytes32) -> Gas; /// Persists any dirty values in the storage cache to the EVM state trie, dropping the cache entirely if requested. /// Analogous to repeated invocations of `vm.SSTORE`. - fn flush_storage_cache(&mut self, clear: bool, gas_left: u64) -> Result; + fn flush_storage_cache(&mut self, clear: bool, gas_left: Gas) -> Result; /// Reads the 32-byte value in the EVM's transient state trie at offset `key`. /// Analogous to `vm.TLOAD`. @@ -102,10 +176,10 @@ pub trait EvmApi: Send + 'static { &mut self, contract: Bytes20, calldata: &[u8], - gas_left: u64, - gas_req: u64, + gas_left: Gas, + gas_req: Gas, value: Bytes32, - ) -> (u32, u64, UserOutcomeKind); + ) -> (u32, Gas, UserOutcomeKind); /// Delegate-calls the contract at the given address. /// Returns the EVM return data's length, the gas cost, and whether the call succeeded. @@ -114,9 +188,9 @@ pub trait EvmApi: Send + 'static { &mut self, contract: Bytes20, calldata: &[u8], - gas_left: u64, - gas_req: u64, - ) -> (u32, u64, UserOutcomeKind); + gas_left: Gas, + gas_req: Gas, + ) -> (u32, Gas, UserOutcomeKind); /// Static-calls the contract at the given address. /// Returns the EVM return data's length, the gas cost, and whether the call succeeded. @@ -125,9 +199,9 @@ pub trait EvmApi: Send + 'static { &mut self, contract: Bytes20, calldata: &[u8], - gas_left: u64, - gas_req: u64, - ) -> (u32, u64, UserOutcomeKind); + gas_left: Gas, + gas_req: Gas, + ) -> (u32, Gas, UserOutcomeKind); /// Deploys a new contract using the init code provided. /// Returns the new contract's address on success, or the error reason on failure. @@ -137,8 +211,8 @@ pub trait EvmApi: Send + 'static { &mut self, code: Vec, endowment: Bytes32, - gas: u64, - ) -> (eyre::Result, u32, u64); + gas: Gas, + ) -> (eyre::Result, u32, Gas); /// Deploys a new contract using the init code provided, with an address determined in part by the `salt`. /// Returns the new contract's address on success, or the error reason on failure. @@ -149,8 +223,8 @@ pub trait EvmApi: Send + 'static { code: Vec, endowment: Bytes32, salt: Bytes32, - gas: u64, - ) -> (eyre::Result, u32, u64); + gas: Gas, + ) -> (eyre::Result, u32, Gas); /// Returns the EVM return data. /// Analogous to `vm.RETURNDATACOPY`. @@ -164,21 +238,21 @@ pub trait EvmApi: Send + 'static { /// Gets the balance of the given account. /// Returns the balance and the access cost in gas. /// Analogous to `vm.BALANCE`. - fn account_balance(&mut self, address: Bytes20) -> (Bytes32, u64); + fn account_balance(&mut self, address: Bytes20) -> (Bytes32, Gas); /// Returns the code and the access cost in gas. /// Analogous to `vm.EXTCODECOPY`. - fn account_code(&mut self, address: Bytes20, gas_left: u64) -> (D, u64); + fn account_code(&mut self, address: Bytes20, gas_left: Gas) -> (D, Gas); /// Gets the hash of the given address's code. /// Returns the hash and the access cost in gas. /// Analogous to `vm.EXTCODEHASH`. - fn account_codehash(&mut self, address: Bytes20) -> (Bytes32, u64); + fn account_codehash(&mut self, address: Bytes20) -> (Bytes32, Gas); /// Determines the cost in gas of allocating additional wasm pages. /// Note: has the side effect of updating Geth's memory usage tracker. /// Not analogous to any EVM opcode. - fn add_pages(&mut self, pages: u16) -> u64; + fn add_pages(&mut self, pages: u16) -> Gas; /// Captures tracing information for hostio invocations during native execution. fn capture_hostio( @@ -186,7 +260,7 @@ pub trait EvmApi: Send + 'static { name: &str, args: &[u8], outs: &[u8], - start_ink: u64, - end_ink: u64, + start_ink: Ink, + end_ink: Ink, ); } diff --git a/arbitrator/arbutil/src/evm/mod.rs b/arbitrator/arbutil/src/evm/mod.rs index 36dadd906a..063194b0c6 100644 --- a/arbitrator/arbutil/src/evm/mod.rs +++ b/arbitrator/arbutil/src/evm/mod.rs @@ -2,6 +2,7 @@ // For license information, see https://github.com/OffchainLabs/nitro/blob/master/LICENSE use crate::{Bytes20, Bytes32}; +use api::Gas; pub mod api; pub mod req; @@ -9,70 +10,70 @@ pub mod storage; pub mod user; // params.SstoreSentryGasEIP2200 -pub const SSTORE_SENTRY_GAS: u64 = 2300; +pub const SSTORE_SENTRY_GAS: Gas = Gas(2300); // params.ColdAccountAccessCostEIP2929 -pub const COLD_ACCOUNT_GAS: u64 = 2600; +pub const COLD_ACCOUNT_GAS: Gas = Gas(2600); // params.ColdSloadCostEIP2929 -pub const COLD_SLOAD_GAS: u64 = 2100; +pub const COLD_SLOAD_GAS: Gas = Gas(2100); // params.WarmStorageReadCostEIP2929 -pub const WARM_SLOAD_GAS: u64 = 100; +pub const WARM_SLOAD_GAS: Gas = Gas(100); // params.WarmStorageReadCostEIP2929 (see enable1153 in jump_table.go) -pub const TLOAD_GAS: u64 = WARM_SLOAD_GAS; -pub const TSTORE_GAS: u64 = WARM_SLOAD_GAS; +pub const TLOAD_GAS: Gas = WARM_SLOAD_GAS; +pub const TSTORE_GAS: Gas = WARM_SLOAD_GAS; // params.LogGas and params.LogDataGas -pub const LOG_TOPIC_GAS: u64 = 375; -pub const LOG_DATA_GAS: u64 = 8; +pub const LOG_TOPIC_GAS: Gas = Gas(375); +pub const LOG_DATA_GAS: Gas = Gas(8); // params.CopyGas -pub const COPY_WORD_GAS: u64 = 3; +pub const COPY_WORD_GAS: Gas = Gas(3); // params.Keccak256Gas -pub const KECCAK_256_GAS: u64 = 30; -pub const KECCAK_WORD_GAS: u64 = 6; +pub const KECCAK_256_GAS: Gas = Gas(30); +pub const KECCAK_WORD_GAS: Gas = Gas(6); // vm.GasQuickStep (see gas.go) -pub const GAS_QUICK_STEP: u64 = 2; +pub const GAS_QUICK_STEP: Gas = Gas(2); // vm.GasQuickStep (see jump_table.go) -pub const ADDRESS_GAS: u64 = GAS_QUICK_STEP; +pub const ADDRESS_GAS: Gas = GAS_QUICK_STEP; // vm.GasQuickStep (see eips.go) -pub const BASEFEE_GAS: u64 = GAS_QUICK_STEP; +pub const BASEFEE_GAS: Gas = GAS_QUICK_STEP; // vm.GasQuickStep (see eips.go) -pub const CHAINID_GAS: u64 = GAS_QUICK_STEP; +pub const CHAINID_GAS: Gas = GAS_QUICK_STEP; // vm.GasQuickStep (see jump_table.go) -pub const COINBASE_GAS: u64 = GAS_QUICK_STEP; +pub const COINBASE_GAS: Gas = GAS_QUICK_STEP; // vm.GasQuickStep (see jump_table.go) -pub const GASLIMIT_GAS: u64 = GAS_QUICK_STEP; +pub const GASLIMIT_GAS: Gas = GAS_QUICK_STEP; // vm.GasQuickStep (see jump_table.go) -pub const NUMBER_GAS: u64 = GAS_QUICK_STEP; +pub const NUMBER_GAS: Gas = GAS_QUICK_STEP; // vm.GasQuickStep (see jump_table.go) -pub const TIMESTAMP_GAS: u64 = GAS_QUICK_STEP; +pub const TIMESTAMP_GAS: Gas = GAS_QUICK_STEP; // vm.GasQuickStep (see jump_table.go) -pub const GASLEFT_GAS: u64 = GAS_QUICK_STEP; +pub const GASLEFT_GAS: Gas = GAS_QUICK_STEP; // vm.GasQuickStep (see jump_table.go) -pub const CALLER_GAS: u64 = GAS_QUICK_STEP; +pub const CALLER_GAS: Gas = GAS_QUICK_STEP; // vm.GasQuickStep (see jump_table.go) -pub const CALLVALUE_GAS: u64 = GAS_QUICK_STEP; +pub const CALLVALUE_GAS: Gas = GAS_QUICK_STEP; // vm.GasQuickStep (see jump_table.go) -pub const GASPRICE_GAS: u64 = GAS_QUICK_STEP; +pub const GASPRICE_GAS: Gas = GAS_QUICK_STEP; // vm.GasQuickStep (see jump_table.go) -pub const ORIGIN_GAS: u64 = GAS_QUICK_STEP; +pub const ORIGIN_GAS: Gas = GAS_QUICK_STEP; pub const ARBOS_VERSION_STYLUS_CHARGING_FIXES: u64 = 32; diff --git a/arbitrator/arbutil/src/evm/req.rs b/arbitrator/arbutil/src/evm/req.rs index 0304f2d378..621f41e951 100644 --- a/arbitrator/arbutil/src/evm/req.rs +++ b/arbitrator/arbutil/src/evm/req.rs @@ -12,8 +12,10 @@ use crate::{ use eyre::{bail, eyre, Result}; use std::collections::hash_map::Entry; +use super::api::{Gas, Ink}; + pub trait RequestHandler: Send + 'static { - fn request(&mut self, req_type: EvmApiMethod, req_data: impl AsRef<[u8]>) -> (Vec, D, u64); + fn request(&mut self, req_type: EvmApiMethod, req_data: impl AsRef<[u8]>) -> (Vec, D, Gas); } pub struct EvmApiRequestor> { @@ -33,7 +35,7 @@ impl> EvmApiRequestor { } } - fn request(&mut self, req_type: EvmApiMethod, req_data: impl AsRef<[u8]>) -> (Vec, D, u64) { + fn request(&mut self, req_type: EvmApiMethod, req_data: impl AsRef<[u8]>) -> (Vec, D, Gas) { self.handler.request(req_type, req_data) } @@ -43,10 +45,10 @@ impl> EvmApiRequestor { call_type: EvmApiMethod, contract: Bytes20, input: &[u8], - gas_left: u64, - gas_req: u64, + gas_left: Gas, + gas_req: Gas, value: Bytes32, - ) -> (u32, u64, UserOutcomeKind) { + ) -> (u32, Gas, UserOutcomeKind) { let mut request = Vec::with_capacity(20 + 32 + 8 + 8 + input.len()); request.extend(contract); request.extend(value); @@ -71,8 +73,8 @@ impl> EvmApiRequestor { code: Vec, endowment: Bytes32, salt: Option, - gas: u64, - ) -> (Result, u32, u64) { + gas: Gas, + ) -> (Result, u32, Gas) { let mut request = Vec::with_capacity(8 + 2 * 32 + code.len()); request.extend(gas.to_be_bytes()); request.extend(endowment); @@ -98,7 +100,7 @@ impl> EvmApiRequestor { } impl> EvmApi for EvmApiRequestor { - fn get_bytes32(&mut self, key: Bytes32, evm_api_gas_to_use: u64) -> (Bytes32, u64) { + fn get_bytes32(&mut self, key: Bytes32, evm_api_gas_to_use: Gas) -> (Bytes32, Gas) { let cache = &mut self.storage_cache; let mut cost = cache.read_gas(); @@ -110,7 +112,7 @@ impl> EvmApi for EvmApiRequestor { (value.value, cost) } - fn cache_bytes32(&mut self, key: Bytes32, value: Bytes32) -> u64 { + fn cache_bytes32(&mut self, key: Bytes32, value: Bytes32) -> Gas { let cost = self.storage_cache.write_gas(); match self.storage_cache.entry(key) { Entry::Occupied(mut key) => key.get_mut().value = value, @@ -119,7 +121,7 @@ impl> EvmApi for EvmApiRequestor { cost } - fn flush_storage_cache(&mut self, clear: bool, gas_left: u64) -> Result { + fn flush_storage_cache(&mut self, clear: bool, gas_left: Gas) -> Result { let mut data = Vec::with_capacity(64 * self.storage_cache.len() + 8); data.extend(gas_left.to_be_bytes()); @@ -134,7 +136,7 @@ impl> EvmApi for EvmApiRequestor { self.storage_cache.clear(); } if data.len() == 8 { - return Ok(0); // no need to make request + return Ok(Gas(0)); // no need to make request } let (res, _, cost) = self.request(EvmApiMethod::SetTrieSlots, data); @@ -174,10 +176,10 @@ impl> EvmApi for EvmApiRequestor { &mut self, contract: Bytes20, input: &[u8], - gas_left: u64, - gas_req: u64, + gas_left: Gas, + gas_req: Gas, value: Bytes32, - ) -> (u32, u64, UserOutcomeKind) { + ) -> (u32, Gas, UserOutcomeKind) { self.call_request( EvmApiMethod::ContractCall, contract, @@ -192,9 +194,9 @@ impl> EvmApi for EvmApiRequestor { &mut self, contract: Bytes20, input: &[u8], - gas_left: u64, - gas_req: u64, - ) -> (u32, u64, UserOutcomeKind) { + gas_left: Gas, + gas_req: Gas, + ) -> (u32, Gas, UserOutcomeKind) { self.call_request( EvmApiMethod::DelegateCall, contract, @@ -209,9 +211,9 @@ impl> EvmApi for EvmApiRequestor { &mut self, contract: Bytes20, input: &[u8], - gas_left: u64, - gas_req: u64, - ) -> (u32, u64, UserOutcomeKind) { + gas_left: Gas, + gas_req: Gas, + ) -> (u32, Gas, UserOutcomeKind) { self.call_request( EvmApiMethod::StaticCall, contract, @@ -226,8 +228,8 @@ impl> EvmApi for EvmApiRequestor { &mut self, code: Vec, endowment: Bytes32, - gas: u64, - ) -> (Result, u32, u64) { + gas: Gas, + ) -> (Result, u32, Gas) { self.create_request(EvmApiMethod::Create1, code, endowment, None, gas) } @@ -236,8 +238,8 @@ impl> EvmApi for EvmApiRequestor { code: Vec, endowment: Bytes32, salt: Bytes32, - gas: u64, - ) -> (Result, u32, u64) { + gas: Gas, + ) -> (Result, u32, Gas) { self.create_request(EvmApiMethod::Create2, code, endowment, Some(salt), gas) } @@ -258,15 +260,15 @@ impl> EvmApi for EvmApiRequestor { Ok(()) } - fn account_balance(&mut self, address: Bytes20) -> (Bytes32, u64) { + fn account_balance(&mut self, address: Bytes20) -> (Bytes32, Gas) { let (res, _, cost) = self.request(EvmApiMethod::AccountBalance, address); (res.try_into().unwrap(), cost) } - fn account_code(&mut self, address: Bytes20, gas_left: u64) -> (D, u64) { + fn account_code(&mut self, address: Bytes20, gas_left: Gas) -> (D, Gas) { if let Some((stored_address, data)) = self.last_code.as_ref() { if address == *stored_address { - return (data.clone(), 0); + return (data.clone(), Gas(0)); } } let mut req = Vec::with_capacity(20 + 8); @@ -278,12 +280,12 @@ impl> EvmApi for EvmApiRequestor { (data, cost) } - fn account_codehash(&mut self, address: Bytes20) -> (Bytes32, u64) { + fn account_codehash(&mut self, address: Bytes20) -> (Bytes32, Gas) { let (res, _, cost) = self.request(EvmApiMethod::AccountCodeHash, address); (res.try_into().unwrap(), cost) } - fn add_pages(&mut self, pages: u16) -> u64 { + fn add_pages(&mut self, pages: u16) -> Gas { self.request(EvmApiMethod::AddPages, pages.to_be_bytes()).2 } @@ -292,8 +294,8 @@ impl> EvmApi for EvmApiRequestor { name: &str, args: &[u8], outs: &[u8], - start_ink: u64, - end_ink: u64, + start_ink: Ink, + end_ink: Ink, ) { let mut request = Vec::with_capacity(2 * 8 + 3 * 2 + name.len() + args.len() + outs.len()); request.extend(start_ink.to_be_bytes()); @@ -305,6 +307,7 @@ impl> EvmApi for EvmApiRequestor { request.extend(name.as_bytes()); request.extend(args); request.extend(outs); - self.request(EvmApiMethod::CaptureHostIO, request); + // ignore response (including gas) as we're just tracing + _ = self.request(EvmApiMethod::CaptureHostIO, request); } } diff --git a/arbitrator/arbutil/src/evm/storage.rs b/arbitrator/arbutil/src/evm/storage.rs index 32b60dd21b..5f688364d7 100644 --- a/arbitrator/arbutil/src/evm/storage.rs +++ b/arbitrator/arbutil/src/evm/storage.rs @@ -5,6 +5,8 @@ use crate::Bytes32; use fnv::FnvHashMap as HashMap; use std::ops::{Deref, DerefMut}; +use super::api::Gas; + /// Represents the EVM word at a given key. #[derive(Debug)] pub struct StorageWord { @@ -37,23 +39,23 @@ pub struct StorageCache { } impl StorageCache { - pub const REQUIRED_ACCESS_GAS: u64 = 10; + pub const REQUIRED_ACCESS_GAS: Gas = Gas(10); - pub fn read_gas(&mut self) -> u64 { + pub fn read_gas(&mut self) -> Gas { self.reads += 1; match self.reads { - 0..=32 => 0, - 33..=128 => 2, - _ => 10, + 0..=32 => Gas(0), + 33..=128 => Gas(2), + _ => Gas(10), } } - pub fn write_gas(&mut self) -> u64 { + pub fn write_gas(&mut self) -> Gas { self.writes += 1; match self.writes { - 0..=8 => 0, - 9..=64 => 7, - _ => 10, + 0..=8 => Gas(0), + 9..=64 => Gas(7), + _ => Gas(10), } } } diff --git a/arbitrator/arbutil/src/pricing.rs b/arbitrator/arbutil/src/pricing.rs index 4614b02a2a..91de739303 100644 --- a/arbitrator/arbutil/src/pricing.rs +++ b/arbitrator/arbutil/src/pricing.rs @@ -1,20 +1,22 @@ // Copyright 2023, Offchain Labs, Inc. // For license information, see https://github.com/OffchainLabs/nitro/blob/master/LICENSE +use crate::evm::api::Ink; + /// For hostios that may return something. -pub const HOSTIO_INK: u64 = 8400; +pub const HOSTIO_INK: Ink = Ink(8400); /// For hostios that include pointers. -pub const PTR_INK: u64 = 13440 - HOSTIO_INK; +pub const PTR_INK: Ink = Ink(13440 - HOSTIO_INK.0); /// For hostios that involve an API cost. -pub const EVM_API_INK: u64 = 59673; +pub const EVM_API_INK: Ink = Ink(59673); /// For hostios that involve a div or mod. -pub const DIV_INK: u64 = 20000; +pub const DIV_INK: Ink = Ink(20000); /// For hostios that involve a mulmod. -pub const MUL_MOD_INK: u64 = 24100; +pub const MUL_MOD_INK: Ink = Ink(24100); /// For hostios that involve an addmod. -pub const ADD_MOD_INK: u64 = 21000; +pub const ADD_MOD_INK: Ink = Ink(21000); diff --git a/arbitrator/jit/src/program.rs b/arbitrator/jit/src/program.rs index 084afe96bc..f10a059748 100644 --- a/arbitrator/jit/src/program.rs +++ b/arbitrator/jit/src/program.rs @@ -6,6 +6,7 @@ use crate::caller_env::JitEnv; use crate::machine::{Escape, MaybeEscape, WasmEnvMut}; use crate::stylus_backend::exec_wasm; +use arbutil::evm::api::Gas; use arbutil::Bytes32; use arbutil::{evm::EvmData, format::DebugBytes, heapify}; use caller_env::{GuestPtr, MemAccess}; @@ -131,7 +132,7 @@ pub fn new_program( // buy ink let pricing = config.stylus.pricing; - let ink = pricing.gas_to_ink(gas); + let ink = pricing.gas_to_ink(Gas(gas)); let Some(module) = exec.module_asms.get(&compiled_hash).cloned() else { return Err(Escape::Failure(format!( @@ -217,7 +218,7 @@ pub fn set_response( let raw_data = mem.read_slice(raw_data_ptr, raw_data_len as usize); let thread = exec.threads.last_mut().unwrap(); - thread.set_response(id, result, raw_data, gas) + thread.set_response(id, result, raw_data, Gas(gas)) } /// sends previos response diff --git a/arbitrator/jit/src/stylus_backend.rs b/arbitrator/jit/src/stylus_backend.rs index 61dbf258d4..0d8c477c6c 100644 --- a/arbitrator/jit/src/stylus_backend.rs +++ b/arbitrator/jit/src/stylus_backend.rs @@ -4,7 +4,7 @@ #![allow(clippy::too_many_arguments)] use crate::machine::{Escape, MaybeEscape}; -use arbutil::evm::api::VecReader; +use arbutil::evm::api::{Gas, Ink, VecReader}; use arbutil::evm::{ api::{EvmApiMethod, EVM_API_METHOD_REQ_OFFSET}, req::EvmApiRequestor, @@ -28,7 +28,7 @@ use stylus::{native::NativeInstance, run::RunProgram}; struct MessageToCothread { result: Vec, raw_data: Vec, - cost: u64, + cost: Gas, } #[derive(Clone)] @@ -47,7 +47,7 @@ impl RequestHandler for CothreadRequestor { &mut self, req_type: EvmApiMethod, req_data: impl AsRef<[u8]>, - ) -> (Vec, VecReader, u64) { + ) -> (Vec, VecReader, Gas) { let msg = MessageFromCothread { req_type: req_type as u32 + EVM_API_METHOD_REQ_OFFSET, req_data: req_data.as_ref().to_vec(), @@ -104,7 +104,7 @@ impl CothreadHandler { id: u32, result: Vec, raw_data: Vec, - cost: u64, + cost: Gas, ) -> MaybeEscape { let Some(msg) = self.last_request.clone() else { return Escape::hostio("trying to set response but no message pending"); @@ -131,7 +131,7 @@ pub fn exec_wasm( compile: CompileConfig, config: StylusConfig, evm_data: EvmData, - ink: u64, + ink: Ink, ) -> Result { let (tothread_tx, tothread_rx) = mpsc::sync_channel::(0); let (fromthread_tx, fromthread_rx) = mpsc::sync_channel::(0); @@ -150,7 +150,7 @@ pub fn exec_wasm( let outcome = instance.run_main(&calldata, config, ink); let ink_left = match outcome.as_ref() { - Ok(UserOutcome::OutOfStack) => 0, // take all ink when out of stack + Ok(UserOutcome::OutOfStack) => Ink(0), // take all ink when out of stack _ => instance.ink_left().into(), }; diff --git a/arbitrator/prover/src/machine.rs b/arbitrator/prover/src/machine.rs index 4ece1f7bf2..66992019f9 100644 --- a/arbitrator/prover/src/machine.rs +++ b/arbitrator/prover/src/machine.rs @@ -1816,7 +1816,7 @@ impl Machine { } #[cfg(feature = "native")] - pub fn call_user_func(&mut self, func: &str, args: Vec, ink: u64) -> Result> { + pub fn call_user_func(&mut self, func: &str, args: Vec, ink: arbutil::evm::api::Ink) -> Result> { self.set_ink(ink); self.call_function("user", func, args) } diff --git a/arbitrator/prover/src/merkle.rs b/arbitrator/prover/src/merkle.rs index 4a1278b4cb..fbd704dfc6 100644 --- a/arbitrator/prover/src/merkle.rs +++ b/arbitrator/prover/src/merkle.rs @@ -549,8 +549,7 @@ mod test { let mut empty_node = Bytes32([ 57, 29, 211, 154, 252, 227, 18, 99, 65, 126, 203, 166, 252, 232, 32, 3, 98, 194, 254, 186, 118, 14, 139, 192, 101, 156, 55, 194, 101, 11, 11, 168, - ]) - .clone(); + ]); for _ in 0..64 { print!("Bytes32(["); for i in 0..32 { @@ -607,7 +606,7 @@ mod test { for layer in 0..64 { // empty_hash_at is just a lookup, but empty_hash is calculated iteratively. assert_eq!(empty_hash_at(ty, layer), &empty_hash); - empty_hash = hash_node(ty, &empty_hash, &empty_hash); + empty_hash = hash_node(ty, empty_hash, empty_hash); } } } diff --git a/arbitrator/prover/src/programs/config.rs b/arbitrator/prover/src/programs/config.rs index 0353589358..bd6fb3a843 100644 --- a/arbitrator/prover/src/programs/config.rs +++ b/arbitrator/prover/src/programs/config.rs @@ -4,6 +4,7 @@ #![allow(clippy::field_reassign_with_default)] use crate::{programs::meter, value::FunctionType}; +use arbutil::evm::api::{Gas, Ink}; use derivative::Derivative; use fnv::FnvHashMap as HashMap; use std::fmt::Debug; @@ -72,12 +73,12 @@ impl PricingParams { Self { ink_price } } - pub fn gas_to_ink(&self, gas: u64) -> u64 { - gas.saturating_mul(self.ink_price.into()) + pub fn gas_to_ink(&self, gas: Gas) -> Ink { + Ink(gas.0.saturating_mul(self.ink_price.into())) } - pub fn ink_to_gas(&self, ink: u64) -> u64 { - ink / self.ink_price as u64 // never 0 + pub fn ink_to_gas(&self, ink: Ink) -> Gas { + Gas(ink.0 / self.ink_price as u64) // ink_price is never 0 } } diff --git a/arbitrator/prover/src/programs/memory.rs b/arbitrator/prover/src/programs/memory.rs index 7253b59dc4..758f2f3e82 100644 --- a/arbitrator/prover/src/programs/memory.rs +++ b/arbitrator/prover/src/programs/memory.rs @@ -1,6 +1,8 @@ // Copyright 2023, Offchain Labs, Inc. // For license information, see https://github.com/OffchainLabs/nitro/blob/master/LICENSE +use arbutil::evm::api::Gas; + #[derive(Clone, Copy, Debug)] #[repr(C)] pub struct MemoryModel { @@ -28,20 +30,20 @@ impl MemoryModel { } /// Determines the gas cost of allocating `new` pages given `open` are active and `ever` have ever been. - pub fn gas_cost(&self, new: u16, open: u16, ever: u16) -> u64 { + pub fn gas_cost(&self, new: u16, open: u16, ever: u16) -> Gas { let new_open = open.saturating_add(new); let new_ever = ever.max(new_open); // free until expansion beyond the first few if new_ever <= self.free_pages { - return 0; + return Gas(0); } let credit = |pages: u16| pages.saturating_sub(self.free_pages); let adding = credit(new_open).saturating_sub(credit(open)) as u64; let linear = adding.saturating_mul(self.page_gas.into()); let expand = Self::exp(new_ever) - Self::exp(ever); - linear.saturating_add(expand) + Gas(linear.saturating_add(expand)) } fn exp(pages: u16) -> u64 { @@ -85,7 +87,7 @@ fn test_model() { let mut pages = 0; while pages < 128 { let jump = jump.min(128 - pages); - total += model.gas_cost(jump, pages, pages); + total += model.gas_cost(jump, pages, pages).0; pages += jump; } assert_eq!(total, 31999998); @@ -98,7 +100,7 @@ fn test_model() { let mut adds = 0; while ever < 128 { let jump = jump.min(128 - open); - total += model.gas_cost(jump, open, ever); + total += model.gas_cost(jump, open, ever).0; open += jump; ever = ever.max(open); @@ -114,12 +116,12 @@ fn test_model() { } // check saturation - assert_eq!(u64::MAX, model.gas_cost(129, 0, 0)); - assert_eq!(u64::MAX, model.gas_cost(u16::MAX, 0, 0)); + assert_eq!(Gas(u64::MAX), model.gas_cost(129, 0, 0)); + assert_eq!(Gas(u64::MAX), model.gas_cost(u16::MAX, 0, 0)); // check free pages let model = MemoryModel::new(128, 1000); - assert_eq!(0, model.gas_cost(128, 0, 0)); - assert_eq!(0, model.gas_cost(128, 0, 128)); - assert_eq!(u64::MAX, model.gas_cost(129, 0, 0)); + assert_eq!(Gas(0), model.gas_cost(128, 0, 0)); + assert_eq!(Gas(0), model.gas_cost(128, 0, 128)); + assert_eq!(Gas(u64::MAX), model.gas_cost(129, 0, 0)); } diff --git a/arbitrator/prover/src/programs/meter.rs b/arbitrator/prover/src/programs/meter.rs index ab069fd911..fe98bbcbc1 100644 --- a/arbitrator/prover/src/programs/meter.rs +++ b/arbitrator/prover/src/programs/meter.rs @@ -9,7 +9,7 @@ use crate::{ value::FunctionType, Machine, }; -use arbutil::{evm, operator::OperatorInfo, Bytes32}; +use arbutil::{evm::{self, api::{Gas, Ink}}, operator::OperatorInfo, Bytes32}; use derivative::Derivative; use eyre::Result; use fnv::FnvHashMap as HashMap; @@ -188,15 +188,15 @@ impl<'a, F: OpcodePricer> FuncMiddleware<'a> for FuncMeter<'a, F> { #[derive(Clone, Copy, Debug, PartialEq, Eq)] pub enum MachineMeter { - Ready(u64), + Ready(Ink), Exhausted, } impl MachineMeter { - pub fn ink(self) -> u64 { + pub fn ink(self) -> Ink { match self { Self::Ready(ink) => ink, - Self::Exhausted => 0, + Self::Exhausted => Ink(0), } } @@ -210,8 +210,8 @@ impl MachineMeter { /// We don't implement `From` since it's unclear what 0 would map to #[allow(clippy::from_over_into)] -impl Into for MachineMeter { - fn into(self) -> u64 { +impl Into for MachineMeter { + fn into(self) -> Ink { self.ink() } } @@ -219,7 +219,7 @@ impl Into for MachineMeter { impl Display for MachineMeter { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { match self { - Self::Ready(ink) => write!(f, "{ink} ink"), + Self::Ready(ink) => write!(f, "{} ink", ink.0), Self::Exhausted => write!(f, "exhausted"), } } @@ -241,7 +241,7 @@ pub trait MeteredMachine { fn ink_left(&self) -> MachineMeter; fn set_meter(&mut self, meter: MachineMeter); - fn set_ink(&mut self, ink: u64) { + fn set_ink(&mut self, ink: Ink) { self.set_meter(MachineMeter::Ready(ink)); } @@ -250,14 +250,14 @@ pub trait MeteredMachine { Err(OutOfInkError) } - fn ink_ready(&mut self) -> Result { + fn ink_ready(&mut self) -> Result { let MachineMeter::Ready(ink_left) = self.ink_left() else { return self.out_of_ink(); }; Ok(ink_left) } - fn buy_ink(&mut self, ink: u64) -> Result<(), OutOfInkError> { + fn buy_ink(&mut self, ink: Ink) -> Result<(), OutOfInkError> { let ink_left = self.ink_ready()?; if ink_left < ink { return self.out_of_ink(); @@ -267,7 +267,7 @@ pub trait MeteredMachine { } /// Checks if the user has enough ink, but doesn't burn any - fn require_ink(&mut self, ink: u64) -> Result<(), OutOfInkError> { + fn require_ink(&mut self, ink: Ink) -> Result<(), OutOfInkError> { let ink_left = self.ink_ready()?; if ink_left < ink { return self.out_of_ink(); @@ -277,18 +277,18 @@ pub trait MeteredMachine { /// Pays for a write into the client. fn pay_for_write(&mut self, bytes: u32) -> Result<(), OutOfInkError> { - self.buy_ink(sat_add_mul(5040, 30, bytes.saturating_sub(32))) + self.buy_ink(Ink(sat_add_mul(5040, 30, bytes.saturating_sub(32)))) } /// Pays for a read into the host. fn pay_for_read(&mut self, bytes: u32) -> Result<(), OutOfInkError> { - self.buy_ink(sat_add_mul(16381, 55, bytes.saturating_sub(32))) + self.buy_ink(Ink(sat_add_mul(16381, 55, bytes.saturating_sub(32)))) } /// Pays for both I/O and keccak. fn pay_for_keccak(&mut self, bytes: u32) -> Result<(), OutOfInkError> { let words = evm::evm_words(bytes).saturating_sub(2); - self.buy_ink(sat_add_mul(121800, 21000, words)) + self.buy_ink(Ink(sat_add_mul(121800, 21000, words))) } /// Pays for copying bytes from geth. @@ -305,14 +305,14 @@ pub trait MeteredMachine { false => break, } } - self.buy_ink(3000 + exp * 17500) + self.buy_ink(Ink(3000 + exp * 17500)) } } pub trait GasMeteredMachine: MeteredMachine { fn pricing(&self) -> PricingParams; - fn gas_left(&self) -> Result { + fn gas_left(&self) -> Result { let pricing = self.pricing(); match self.ink_left() { MachineMeter::Ready(ink) => Ok(pricing.ink_to_gas(ink)), @@ -320,13 +320,13 @@ pub trait GasMeteredMachine: MeteredMachine { } } - fn buy_gas(&mut self, gas: u64) -> Result<(), OutOfInkError> { + fn buy_gas(&mut self, gas: Gas) -> Result<(), OutOfInkError> { let pricing = self.pricing(); self.buy_ink(pricing.gas_to_ink(gas)) } /// Checks if the user has enough gas, but doesn't burn any - fn require_gas(&mut self, gas: u64) -> Result<(), OutOfInkError> { + fn require_gas(&mut self, gas: Gas) -> Result<(), OutOfInkError> { let pricing = self.pricing(); self.require_ink(pricing.gas_to_ink(gas)) } @@ -350,7 +350,7 @@ impl MeteredMachine for Machine { }}; } - let ink = || convert!(self.get_global(STYLUS_INK_LEFT)); + let ink = || Ink(convert!(self.get_global(STYLUS_INK_LEFT))); let status: u32 = convert!(self.get_global(STYLUS_INK_STATUS)); match status { @@ -362,7 +362,7 @@ impl MeteredMachine for Machine { fn set_meter(&mut self, meter: MachineMeter) { let ink = meter.ink(); let status = meter.status(); - self.set_global(STYLUS_INK_LEFT, ink.into()).unwrap(); + self.set_global(STYLUS_INK_LEFT, ink.0.into()).unwrap(); self.set_global(STYLUS_INK_STATUS, status.into()).unwrap(); } } diff --git a/arbitrator/prover/src/test.rs b/arbitrator/prover/src/test.rs index 97170441ff..4fd739342e 100644 --- a/arbitrator/prover/src/test.rs +++ b/arbitrator/prover/src/test.rs @@ -1,8 +1,6 @@ // Copyright 2022-2024, Offchain Labs, Inc. // For license information, see https://github.com/nitro/blob/master/LICENSE -#![cfg(test)] - use crate::binary; use brotli::Dictionary; use eyre::Result; @@ -64,7 +62,7 @@ pub fn test_compress() -> Result<()> { let deflate = brotli::compress(data, 11, 22, dict).unwrap(); let inflate = brotli::decompress(&deflate, dict).unwrap(); assert_eq!(hex::encode(inflate), hex::encode(data)); - assert!(&deflate != &last); + assert!(deflate != last); last = deflate; } Ok(()) diff --git a/arbitrator/stylus/src/env.rs b/arbitrator/stylus/src/env.rs index 69d542070d..a153fb5bf1 100644 --- a/arbitrator/stylus/src/env.rs +++ b/arbitrator/stylus/src/env.rs @@ -3,7 +3,7 @@ use arbutil::{ evm::{ - api::{DataReader, EvmApi}, + api::{DataReader, EvmApi, Ink}, EvmData, }, pricing, @@ -74,7 +74,7 @@ impl> WasmEnv { pub fn start<'a>( env: &'a mut WasmEnvMut<'_, D, E>, - ink: u64, + ink: Ink, ) -> Result, Escape> { let mut info = Self::program(env)?; info.buy_ink(pricing::HOSTIO_INK + ink)?; @@ -88,7 +88,7 @@ impl> WasmEnv { env, memory, store, - start_ink: 0, + start_ink: Ink(0), }; if info.env.evm_data.tracing { info.start_ink = info.ink_ready()?; @@ -114,16 +114,16 @@ pub struct MeterData { } impl MeterData { - pub fn ink(&self) -> u64 { - unsafe { self.ink_left.as_ref().val.u64 } + pub fn ink(&self) -> Ink { + Ink(unsafe { self.ink_left.as_ref().val.u64 }) } pub fn status(&self) -> u32 { unsafe { self.ink_status.as_ref().val.u32 } } - pub fn set_ink(&mut self, ink: u64) { - unsafe { self.ink_left.as_mut().val = RawValue { u64: ink } } + pub fn set_ink(&mut self, ink: Ink) { + unsafe { self.ink_left.as_mut().val = RawValue { u64: ink.0 } } } pub fn set_status(&mut self, status: u32) { @@ -140,7 +140,7 @@ pub struct HostioInfo<'a, D: DataReader, E: EvmApi> { pub env: &'a mut WasmEnv, pub memory: Memory, pub store: StoreMut<'a>, - pub start_ink: u64, + pub start_ink: Ink, } impl<'a, D: DataReader, E: EvmApi> HostioInfo<'a, D, E> { diff --git a/arbitrator/stylus/src/evm_api.rs b/arbitrator/stylus/src/evm_api.rs index d267372827..0dd27e3f8c 100644 --- a/arbitrator/stylus/src/evm_api.rs +++ b/arbitrator/stylus/src/evm_api.rs @@ -3,7 +3,7 @@ use crate::{GoSliceData, RustSlice}; use arbutil::evm::{ - api::{EvmApiMethod, EVM_API_METHOD_REQ_OFFSET}, + api::{EvmApiMethod, Gas, EVM_API_METHOD_REQ_OFFSET}, req::RequestHandler, }; @@ -31,7 +31,7 @@ impl RequestHandler for NativeRequestHandler { &mut self, req_type: EvmApiMethod, req_data: impl AsRef<[u8]>, - ) -> (Vec, GoSliceData, u64) { + ) -> (Vec, GoSliceData, Gas) { let mut result = GoSliceData::null(); let mut raw_data = GoSliceData::null(); let mut cost = 0; @@ -45,6 +45,6 @@ impl RequestHandler for NativeRequestHandler { ptr!(raw_data), ) }; - (result.slice().to_vec(), raw_data, cost) + (result.slice().to_vec(), raw_data, Gas(cost)) } } diff --git a/arbitrator/stylus/src/host.rs b/arbitrator/stylus/src/host.rs index 1afc1b4e51..c72cafc316 100644 --- a/arbitrator/stylus/src/host.rs +++ b/arbitrator/stylus/src/host.rs @@ -6,7 +6,7 @@ use crate::env::{Escape, HostioInfo, MaybeEscape, WasmEnv, WasmEnvMut}; use arbutil::{ evm::{ - api::{DataReader, EvmApi}, + api::{DataReader, EvmApi, Gas, Ink}, EvmData, }, Color, @@ -82,7 +82,7 @@ where println!("{} {text}", "Stylus says:".yellow()); } - fn trace(&mut self, name: &str, args: &[u8], outs: &[u8], end_ink: u64) { + fn trace(&mut self, name: &str, args: &[u8], outs: &[u8], end_ink: Ink) { let start_ink = self.start_ink; self.evm_api .capture_hostio(name, args, outs, start_ink, end_ink); @@ -168,7 +168,7 @@ pub(crate) fn call_contract>( ) -> Result { hostio!( env, - call_contract(contract, data, data_len, value, gas, ret_len) + call_contract(contract, data, data_len, value, Gas(gas), ret_len) ) } @@ -182,7 +182,7 @@ pub(crate) fn delegate_call_contract>( ) -> Result { hostio!( env, - delegate_call_contract(contract, data, data_len, gas, ret_len) + delegate_call_contract(contract, data, data_len, Gas(gas), ret_len) ) } @@ -196,7 +196,7 @@ pub(crate) fn static_call_contract>( ) -> Result { hostio!( env, - static_call_contract(contract, data, data_len, gas, ret_len) + static_call_contract(contract, data, data_len, Gas(gas), ret_len) ) } @@ -334,13 +334,13 @@ pub(crate) fn contract_address>( pub(crate) fn evm_gas_left>( mut env: WasmEnvMut, ) -> Result { - hostio!(env, evm_gas_left()) + hostio!(env, evm_gas_left()).map(|g| g.0) } pub(crate) fn evm_ink_left>( mut env: WasmEnvMut, ) -> Result { - hostio!(env, evm_ink_left()) + hostio!(env, evm_ink_left()).map(|i| i.0) } pub(crate) fn math_div>( diff --git a/arbitrator/stylus/src/lib.rs b/arbitrator/stylus/src/lib.rs index 5962817d78..e7f10c2400 100644 --- a/arbitrator/stylus/src/lib.rs +++ b/arbitrator/stylus/src/lib.rs @@ -3,7 +3,7 @@ use arbutil::{ evm::{ - api::DataReader, + api::{DataReader, Gas, Ink}, req::EvmApiRequestor, user::{UserOutcome, UserOutcomeKind}, EvmData, @@ -279,7 +279,7 @@ pub unsafe extern "C" fn stylus_call( let evm_api = EvmApiRequestor::new(req_handler); let pricing = config.pricing; let output = &mut *output; - let ink = pricing.gas_to_ink(*gas); + let ink = pricing.gas_to_ink(Gas(*gas)); // Safety: module came from compile_user_wasm and we've paid for memory expansion let instance = unsafe { @@ -302,10 +302,10 @@ pub unsafe extern "C" fn stylus_call( Ok(outcome) => output.write_outcome(outcome), }; let ink_left = match status { - UserOutcomeKind::OutOfStack => 0, // take all gas when out of stack + UserOutcomeKind::OutOfStack => Ink(0), // take all gas when out of stack _ => instance.ink_left().into(), }; - *gas = pricing.ink_to_gas(ink_left); + *gas = pricing.ink_to_gas(ink_left).0; status } diff --git a/arbitrator/stylus/src/native.rs b/arbitrator/stylus/src/native.rs index c751a670cc..0fbdb342f3 100644 --- a/arbitrator/stylus/src/native.rs +++ b/arbitrator/stylus/src/native.rs @@ -8,7 +8,7 @@ use crate::{ }; use arbutil::{ evm::{ - api::{DataReader, EvmApi}, + api::{DataReader, EvmApi, Ink}, EvmData, }, operator::OperatorCode, @@ -270,7 +270,7 @@ impl> NativeInstance { global.set(store, value.into()).map_err(ErrReport::msg) } - pub fn call_func(&mut self, func: TypedFunction<(), R>, ink: u64) -> Result + pub fn call_func(&mut self, func: TypedFunction<(), R>, ink: Ink) -> Result where R: WasmTypeList, { diff --git a/arbitrator/stylus/src/run.rs b/arbitrator/stylus/src/run.rs index 8e673a25e5..6cbb0cfb42 100644 --- a/arbitrator/stylus/src/run.rs +++ b/arbitrator/stylus/src/run.rs @@ -4,18 +4,18 @@ #![allow(clippy::redundant_closure_call)] use crate::{env::Escape, native::NativeInstance}; -use arbutil::evm::api::{DataReader, EvmApi}; +use arbutil::evm::api::{DataReader, EvmApi, Ink}; use arbutil::evm::user::UserOutcome; use eyre::{eyre, Result}; use prover::machine::Machine; use prover::programs::{prelude::*, STYLUS_ENTRY_POINT}; pub trait RunProgram { - fn run_main(&mut self, args: &[u8], config: StylusConfig, ink: u64) -> Result; + fn run_main(&mut self, args: &[u8], config: StylusConfig, ink: Ink) -> Result; } impl RunProgram for Machine { - fn run_main(&mut self, args: &[u8], config: StylusConfig, ink: u64) -> Result { + fn run_main(&mut self, args: &[u8], config: StylusConfig, ink: Ink) -> Result { macro_rules! call { ($module:expr, $func:expr, $args:expr) => { call!($module, $func, $args, |error| UserOutcome::Failure(error)) @@ -65,7 +65,7 @@ impl RunProgram for Machine { } impl> RunProgram for NativeInstance { - fn run_main(&mut self, args: &[u8], config: StylusConfig, ink: u64) -> Result { + fn run_main(&mut self, args: &[u8], config: StylusConfig, ink: Ink) -> Result { use UserOutcome::*; self.set_ink(ink); diff --git a/arbitrator/stylus/src/test/api.rs b/arbitrator/stylus/src/test/api.rs index 66d600a6f7..7a5af6f89f 100644 --- a/arbitrator/stylus/src/test/api.rs +++ b/arbitrator/stylus/src/test/api.rs @@ -4,7 +4,7 @@ use crate::{native, run::RunProgram}; use arbutil::{ evm::{ - api::{EvmApi, VecReader}, + api::{EvmApi, Gas, Ink, VecReader}, user::UserOutcomeKind, EvmData, }, @@ -68,24 +68,24 @@ impl TestEvmApi { } impl EvmApi for TestEvmApi { - fn get_bytes32(&mut self, key: Bytes32, _evm_api_gas_to_use: u64) -> (Bytes32, u64) { + fn get_bytes32(&mut self, key: Bytes32, _evm_api_gas_to_use: Gas) -> (Bytes32, Gas) { let storage = &mut self.storage.lock(); let storage = storage.get_mut(&self.program).unwrap(); let value = storage.get(&key).cloned().unwrap_or_default(); - (value, 2100) // pretend worst case + (value, Gas(2100)) // pretend worst case } - fn cache_bytes32(&mut self, key: Bytes32, value: Bytes32) -> u64 { + fn cache_bytes32(&mut self, key: Bytes32, value: Bytes32) -> Gas { let storage = &mut self.storage.lock(); let storage = storage.get_mut(&self.program).unwrap(); storage.insert(key, value); - 0 + Gas(0) } - fn flush_storage_cache(&mut self, _clear: bool, _gas_left: u64) -> Result { + fn flush_storage_cache(&mut self, _clear: bool, _gas_left: Gas) -> Result { let storage = &mut self.storage.lock(); let storage = storage.get_mut(&self.program).unwrap(); - Ok(22100 * storage.len() as u64) // pretend worst case + Ok(Gas(22100) * storage.len() as u64) // pretend worst case } fn get_transient_bytes32(&mut self, _key: Bytes32) -> Bytes32 { @@ -102,10 +102,10 @@ impl EvmApi for TestEvmApi { &mut self, contract: Bytes20, calldata: &[u8], - _gas_left: u64, - gas_req: u64, + _gas_left: Gas, + gas_req: Gas, _value: Bytes32, - ) -> (u32, u64, UserOutcomeKind) { + ) -> (u32, Gas, UserOutcomeKind) { let compile = self.compile.clone(); let evm_data = self.evm_data; let config = *self.configs.lock().get(&contract).unwrap(); @@ -122,7 +122,7 @@ impl EvmApi for TestEvmApi { let (status, outs) = outcome.into_data(); let outs_len = outs.len() as u32; - let ink_left: u64 = native.ink_left().into(); + let ink_left: Ink = native.ink_left().into(); let gas_left = config.pricing.ink_to_gas(ink_left); *self.write_result.lock() = outs; (outs_len, gas - gas_left, status) @@ -132,9 +132,9 @@ impl EvmApi for TestEvmApi { &mut self, _contract: Bytes20, _calldata: &[u8], - _gas_left: u64, - _gas_req: u64, - ) -> (u32, u64, UserOutcomeKind) { + _gas_left: Gas, + _gas_req: Gas, + ) -> (u32, Gas, UserOutcomeKind) { todo!("delegate call not yet supported") } @@ -142,9 +142,9 @@ impl EvmApi for TestEvmApi { &mut self, contract: Bytes20, calldata: &[u8], - gas_left: u64, - gas_req: u64, - ) -> (u32, u64, UserOutcomeKind) { + gas_left: Gas, + gas_req: Gas, + ) -> (u32, Gas, UserOutcomeKind) { println!("note: overriding static call with call"); self.contract_call(contract, calldata, gas_left, gas_req, Bytes32::default()) } @@ -153,8 +153,8 @@ impl EvmApi for TestEvmApi { &mut self, _code: Vec, _endowment: Bytes32, - _gas: u64, - ) -> (Result, u32, u64) { + _gas: Gas, + ) -> (Result, u32, Gas) { unimplemented!("create1 not supported") } @@ -163,8 +163,8 @@ impl EvmApi for TestEvmApi { _code: Vec, _endowment: Bytes32, _salt: Bytes32, - _gas: u64, - ) -> (Result, u32, u64) { + _gas: Gas, + ) -> (Result, u32, Gas) { unimplemented!("create2 not supported") } @@ -176,19 +176,19 @@ impl EvmApi for TestEvmApi { Ok(()) // pretend a log was emitted } - fn account_balance(&mut self, _address: Bytes20) -> (Bytes32, u64) { + fn account_balance(&mut self, _address: Bytes20) -> (Bytes32, Gas) { unimplemented!() } - fn account_code(&mut self, _address: Bytes20, _gas_left: u64) -> (VecReader, u64) { + fn account_code(&mut self, _address: Bytes20, _gas_left: Gas) -> (VecReader, Gas) { unimplemented!() } - fn account_codehash(&mut self, _address: Bytes20) -> (Bytes32, u64) { + fn account_codehash(&mut self, _address: Bytes20) -> (Bytes32, Gas) { unimplemented!() } - fn add_pages(&mut self, new: u16) -> u64 { + fn add_pages(&mut self, new: u16) -> Gas { let model = MemoryModel::new(2, 1000); let (open, ever) = *self.pages.lock(); @@ -203,8 +203,8 @@ impl EvmApi for TestEvmApi { _name: &str, _args: &[u8], _outs: &[u8], - _start_ink: u64, - _end_ink: u64, + _start_ink: Ink, + _end_ink: Ink, ) { unimplemented!() } diff --git a/arbitrator/stylus/src/test/mod.rs b/arbitrator/stylus/src/test/mod.rs index 00c9c62ae4..830b9cd23f 100644 --- a/arbitrator/stylus/src/test/mod.rs +++ b/arbitrator/stylus/src/test/mod.rs @@ -3,7 +3,7 @@ use crate::{env::WasmEnv, native::NativeInstance, run::RunProgram, test::api::TestEvmApi}; use arbutil::{ - evm::{api::VecReader, user::UserOutcome}, + evm::{api::{Ink, VecReader}, user::UserOutcome}, Bytes20, Bytes32, Color, }; use eyre::{bail, Result}; @@ -41,7 +41,7 @@ impl TestInstance { }; let mut native = Self::new_from_store(path, store, imports)?; native.set_meter_data(); - native.set_ink(u64::MAX); + native.set_ink(Ink(u64::MAX)); native.set_stack(u32::MAX); Ok(native) } @@ -107,8 +107,8 @@ fn expensive_add(op: &Operator, _tys: &SigMap) -> u64 { } } -pub fn random_ink(min: u64) -> u64 { - rand::thread_rng().gen_range(min..=u64::MAX) +pub fn random_ink(min: u64) -> Ink { + Ink(rand::thread_rng().gen_range(min..=u64::MAX)) } pub fn random_bytes20() -> Bytes20 { @@ -135,7 +135,7 @@ fn uniform_cost_config() -> StylusConfig { stylus_config } -fn test_configs() -> (CompileConfig, StylusConfig, u64) { +fn test_configs() -> (CompileConfig, StylusConfig, Ink) { ( test_compile_config(), uniform_cost_config(), @@ -165,12 +165,12 @@ fn new_test_machine(path: &str, compile: &CompileConfig) -> Result { Arc::new(|_, _, _| panic!("tried to read preimage")), Some(stylus_data), )?; - mach.set_ink(u64::MAX); + mach.set_ink(Ink(u64::MAX)); mach.set_stack(u32::MAX); Ok(mach) } -fn run_native(native: &mut TestInstance, args: &[u8], ink: u64) -> Result> { +fn run_native(native: &mut TestInstance, args: &[u8], ink: Ink) -> Result> { let config = native.env().config.expect("no config"); match native.run_main(args, config, ink)? { UserOutcome::Success(output) => Ok(output), @@ -182,7 +182,7 @@ fn run_machine( machine: &mut Machine, args: &[u8], config: StylusConfig, - ink: u64, + ink: Ink, ) -> Result> { match machine.run_main(args, config, ink)? { UserOutcome::Success(output) => Ok(output), diff --git a/arbitrator/stylus/src/test/native.rs b/arbitrator/stylus/src/test/native.rs index 9669932a03..672bdd179c 100644 --- a/arbitrator/stylus/src/test/native.rs +++ b/arbitrator/stylus/src/test/native.rs @@ -16,7 +16,7 @@ use crate::{ use arbutil::{ crypto, evm::{ - api::EvmApi, + api::{EvmApi, Gas, Ink}, user::{UserOutcome, UserOutcomeKind}, }, format, Bytes20, Bytes32, Color, @@ -48,8 +48,8 @@ fn test_ink() -> Result<()> { macro_rules! exhaust { ($ink:expr) => { - native.set_ink($ink); - assert_eq!(native.ink_left(), MachineMeter::Ready($ink)); + native.set_ink(Ink($ink)); + assert_eq!(native.ink_left(), MachineMeter::Ready(Ink($ink))); assert!(add_one.call(&mut native.store, 32).is_err()); assert_eq!(native.ink_left(), MachineMeter::Exhausted); }; @@ -59,12 +59,12 @@ fn test_ink() -> Result<()> { exhaust!(50); exhaust!(99); - let mut ink_left = 500; + let mut ink_left = Ink(500); native.set_ink(ink_left); - while ink_left > 0 { + while ink_left > Ink(0) { assert_eq!(native.ink_left(), MachineMeter::Ready(ink_left)); assert_eq!(add_one.call(&mut native.store, 64)?, 65); - ink_left -= 100; + ink_left -= Ink(100); } assert!(add_one.call(&mut native.store, 32).is_err()); assert_eq!(native.ink_left(), MachineMeter::Exhausted); @@ -198,7 +198,7 @@ fn test_import_export_safety() -> Result<()> { let mut bin = bin?; assert!(bin.clone().instrument(&compile, codehash).is_err()); compile.debug.debug_info = false; - assert!(bin.instrument(&compile, &codehash).is_err()); + assert!(bin.instrument(&compile, codehash).is_err()); if both { assert!(TestInstance::new_test(file, compile).is_err()); @@ -268,7 +268,7 @@ fn test_heap() -> Result<()> { assert_eq!(pages, 128); let used = config.pricing.ink_to_gas(ink - native.ink_ready()?); - ensure!((used as i64 - 32_000_000).abs() < 3_000, "wrong ink"); + ensure!((used.0 as i64 - 32_000_000).abs() < 3_000, "wrong ink"); assert_eq!(native.memory_size(), Pages(128)); if step == extra { @@ -283,7 +283,7 @@ fn test_heap() -> Result<()> { // the cost should exceed a maximum u32, consuming more gas than can ever be bought let (mut native, _) = TestInstance::new_with_evm("tests/memory2.wat", &compile, config)?; - let outcome = native.run_main(&[], config, config.pricing.ink_to_gas(u32::MAX.into()))?; + let outcome = native.run_main(&[], config, config.pricing.gas_to_ink(Gas(u32::MAX.into())))?; assert_eq!(outcome.kind(), UserOutcomeKind::OutOfInk); // ensure we reject programs with excessive footprints @@ -381,7 +381,7 @@ fn test_storage() -> Result<()> { let (mut native, mut evm) = TestInstance::new_with_evm(filename, &compile, config)?; run_native(&mut native, &store_args, ink)?; - assert_eq!(evm.get_bytes32(key.into(), 0).0, Bytes32(value)); + assert_eq!(evm.get_bytes32(key.into(), Gas(0)).0, Bytes32(value)); assert_eq!(run_native(&mut native, &load_args, ink)?, value); let mut machine = Machine::from_user_path(Path::new(filename), &compile)?; @@ -465,7 +465,7 @@ fn test_calls() -> Result<()> { run_native(&mut native, &args, ink)?; for (key, value) in slots { - assert_eq!(evm.get_bytes32(key, 0).0, value); + assert_eq!(evm.get_bytes32(key, Gas(0)).0, value); } Ok(()) } diff --git a/arbitrator/stylus/src/test/wavm.rs b/arbitrator/stylus/src/test/wavm.rs index e707cf490a..729cfebf2f 100644 --- a/arbitrator/stylus/src/test/wavm.rs +++ b/arbitrator/stylus/src/test/wavm.rs @@ -2,6 +2,7 @@ // For license information, see https://github.com/nitro/blob/master/LICENSE use crate::test::{new_test_machine, test_compile_config}; +use arbutil::evm::api::Ink; use eyre::Result; use prover::{programs::prelude::*, Machine}; @@ -15,8 +16,8 @@ fn test_ink() -> Result<()> { macro_rules! exhaust { ($ink:expr) => { - machine.set_ink($ink); - assert_eq!(machine.ink_left(), MachineMeter::Ready($ink)); + machine.set_ink(Ink($ink)); + assert_eq!(machine.ink_left(), MachineMeter::Ready(Ink($ink))); assert!(call(machine, 32).is_err()); assert_eq!(machine.ink_left(), MachineMeter::Exhausted); }; @@ -26,12 +27,12 @@ fn test_ink() -> Result<()> { exhaust!(50); exhaust!(99); - let mut ink_left = 500; + let mut ink_left = Ink(500); machine.set_ink(ink_left); - while ink_left > 0 { + while ink_left > Ink(0) { assert_eq!(machine.ink_left(), MachineMeter::Ready(ink_left)); assert_eq!(call(machine, 64)?, vec![65_u32.into()]); - ink_left -= 100; + ink_left -= Ink(100); } assert!(call(machine, 32).is_err()); assert_eq!(machine.ink_left(), MachineMeter::Exhausted); diff --git a/arbitrator/wasm-libraries/user-host-trait/src/lib.rs b/arbitrator/wasm-libraries/user-host-trait/src/lib.rs index 12a6bdbed2..35a4a31347 100644 --- a/arbitrator/wasm-libraries/user-host-trait/src/lib.rs +++ b/arbitrator/wasm-libraries/user-host-trait/src/lib.rs @@ -5,7 +5,7 @@ use arbutil::{ crypto, evm::{ self, - api::{DataReader, EvmApi}, + api::{DataReader, EvmApi, Gas, Ink}, storage::StorageCache, user::UserOutcomeKind, EvmData, ARBOS_VERSION_STYLUS_CHARGING_FIXES, @@ -88,7 +88,7 @@ pub trait UserHost: GasMeteredMachine { } fn say(&self, text: D); - fn trace(&mut self, name: &str, args: &[u8], outs: &[u8], end_ink: u64); + fn trace(&mut self, name: &str, args: &[u8], outs: &[u8], end_ink: Ink); fn write_bytes20(&self, ptr: GuestPtr, src: Bytes20) -> Result<(), Self::MemoryErr> { self.write_slice(ptr, &src.0) @@ -147,7 +147,7 @@ pub trait UserHost: GasMeteredMachine { // require for cache-miss case, preserve wrong behavior for old arbos let evm_api_gas_to_use = if arbos_version < ARBOS_VERSION_STYLUS_CHARGING_FIXES { - EVM_API_INK + Gas(EVM_API_INK.0) } else { self.pricing().ink_to_gas(EVM_API_INK) }; @@ -253,7 +253,7 @@ pub trait UserHost: GasMeteredMachine { data: GuestPtr, data_len: u32, value: GuestPtr, - gas: u64, + gas: Gas, ret_len: GuestPtr, ) -> Result { let value = Some(value); @@ -282,7 +282,7 @@ pub trait UserHost: GasMeteredMachine { contract: GuestPtr, data: GuestPtr, data_len: u32, - gas: u64, + gas: Gas, ret_len: GuestPtr, ) -> Result { let call = |api: &mut Self::A, contract, data: &_, left, req, _| { @@ -312,7 +312,7 @@ pub trait UserHost: GasMeteredMachine { contract: GuestPtr, data: GuestPtr, data_len: u32, - gas: u64, + gas: Gas, ret_len: GuestPtr, ) -> Result { let call = |api: &mut Self::A, contract, data: &_, left, req, _| { @@ -329,7 +329,7 @@ pub trait UserHost: GasMeteredMachine { calldata: GuestPtr, calldata_len: u32, value: Option, - gas: u64, + gas: Gas, return_data_len: GuestPtr, call: F, name: &str, @@ -339,10 +339,10 @@ pub trait UserHost: GasMeteredMachine { &mut Self::A, Address, &[u8], - u64, - u64, + Gas, + Gas, Option, - ) -> (u32, u64, UserOutcomeKind), + ) -> (u32, Gas, UserOutcomeKind), { self.buy_ink(HOSTIO_INK + 3 * PTR_INK + EVM_API_INK)?; self.pay_for_read(calldata_len)?; @@ -465,12 +465,12 @@ pub trait UserHost: GasMeteredMachine { salt: Option, contract: GuestPtr, revert_data_len: GuestPtr, - cost: u64, + cost: Ink, call: F, name: &str, ) -> Result<(), Self::Err> where - F: FnOnce(&mut Self::A, Vec, Bytes32, Option, u64) -> (Result
, u32, u64), + F: FnOnce(&mut Self::A, Vec, Bytes32, Option, Gas) -> (Result
, u32, Gas), { self.buy_ink(HOSTIO_INK + cost)?; self.pay_for_read(code_len)?; @@ -745,7 +745,7 @@ pub trait UserHost: GasMeteredMachine { /// equivalent to that of the EVM's [`GAS`] opcode. /// /// [`GAS`]: https://www.evm.codes/#5a - fn evm_gas_left(&mut self) -> Result { + fn evm_gas_left(&mut self) -> Result { self.buy_ink(HOSTIO_INK)?; let gas = self.gas_left()?; trace!("evm_gas_left", self, &[], be!(gas), gas) @@ -757,7 +757,7 @@ pub trait UserHost: GasMeteredMachine { /// /// [`GAS`]: https://www.evm.codes/#5a /// [`Ink and Gas`]: https://developer.arbitrum.io/TODO - fn evm_ink_left(&mut self) -> Result { + fn evm_ink_left(&mut self) -> Result { self.buy_ink(HOSTIO_INK)?; let ink = self.ink_ready()?; trace!("evm_ink_left", self, &[], be!(ink), ink) From 788de6ed78a4c2626302a4a65c6b287d9848bc85 Mon Sep 17 00:00:00 2001 From: Lee Bousfield Date: Mon, 14 Oct 2024 14:59:34 -0500 Subject: [PATCH 29/38] Fix user-host compilation --- arbitrator/arbutil/src/evm/api.rs | 4 +-- .../wasm-libraries/user-host/src/host.rs | 29 +++++++++++++++---- .../wasm-libraries/user-host/src/ink.rs | 5 ++-- .../wasm-libraries/user-host/src/link.rs | 18 +++++++----- .../wasm-libraries/user-host/src/program.rs | 12 ++++---- 5 files changed, 45 insertions(+), 23 deletions(-) diff --git a/arbitrator/arbutil/src/evm/api.rs b/arbitrator/arbutil/src/evm/api.rs index a9f8d927fe..8715f5ac8c 100644 --- a/arbitrator/arbutil/src/evm/api.rs +++ b/arbitrator/arbutil/src/evm/api.rs @@ -135,13 +135,13 @@ macro_rules! derive_math { }; } -#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)] +#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Debug, Default)] #[must_use] pub struct Gas(pub u64); derive_math!(Gas); -#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)] +#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Debug, Default)] #[must_use] pub struct Ink(pub u64); diff --git a/arbitrator/wasm-libraries/user-host/src/host.rs b/arbitrator/wasm-libraries/user-host/src/host.rs index abe55b8c12..5ec2ece2c3 100644 --- a/arbitrator/wasm-libraries/user-host/src/host.rs +++ b/arbitrator/wasm-libraries/user-host/src/host.rs @@ -2,7 +2,7 @@ // For license information, see https://github.com/nitro/blob/master/LICENSE use crate::program::Program; -use arbutil::evm::user::UserOutcomeKind; +use arbutil::evm::{api::Gas, user::UserOutcomeKind}; use caller_env::GuestPtr; use user_host_trait::UserHost; @@ -77,7 +77,14 @@ pub unsafe extern "C" fn user_host__call_contract( gas: u64, ret_len: GuestPtr, ) -> u8 { - hostio!(call_contract(contract, data, data_len, value, gas, ret_len)) + hostio!(call_contract( + contract, + data, + data_len, + value, + Gas(gas), + ret_len + )) } #[no_mangle] @@ -89,7 +96,11 @@ pub unsafe extern "C" fn user_host__delegate_call_contract( ret_len: GuestPtr, ) -> u8 { hostio!(delegate_call_contract( - contract, data, data_len, gas, ret_len + contract, + data, + data_len, + Gas(gas), + ret_len )) } @@ -101,7 +112,13 @@ pub unsafe extern "C" fn user_host__static_call_contract( gas: u64, ret_len: GuestPtr, ) -> u8 { - hostio!(static_call_contract(contract, data, data_len, gas, ret_len)) + hostio!(static_call_contract( + contract, + data, + data_len, + Gas(gas), + ret_len + )) } #[no_mangle] @@ -207,12 +224,12 @@ pub unsafe extern "C" fn user_host__contract_address(ptr: GuestPtr) { #[no_mangle] pub unsafe extern "C" fn user_host__evm_gas_left() -> u64 { - hostio!(evm_gas_left()) + hostio!(evm_gas_left()).0 } #[no_mangle] pub unsafe extern "C" fn user_host__evm_ink_left() -> u64 { - hostio!(evm_ink_left()) + hostio!(evm_ink_left()).0 } #[no_mangle] diff --git a/arbitrator/wasm-libraries/user-host/src/ink.rs b/arbitrator/wasm-libraries/user-host/src/ink.rs index e01e616e07..bde7cfc1c0 100644 --- a/arbitrator/wasm-libraries/user-host/src/ink.rs +++ b/arbitrator/wasm-libraries/user-host/src/ink.rs @@ -2,6 +2,7 @@ // For license information, see https://github.com/OffchainLabs/nitro/blob/master/LICENSE use crate::program::Program; +use arbutil::evm::api::Ink; use prover::programs::{ config::PricingParams, prelude::{GasMeteredMachine, MachineMeter, MeteredMachine}, @@ -18,7 +19,7 @@ impl MeteredMachine for Program { fn ink_left(&self) -> MachineMeter { unsafe { match user_ink_status() { - 0 => MachineMeter::Ready(user_ink_left()), + 0 => MachineMeter::Ready(Ink(user_ink_left())), _ => MachineMeter::Exhausted, } } @@ -26,7 +27,7 @@ impl MeteredMachine for Program { fn set_meter(&mut self, meter: MachineMeter) { unsafe { - user_set_ink(meter.ink(), meter.status()); + user_set_ink(meter.ink().0, meter.status()); } } } diff --git a/arbitrator/wasm-libraries/user-host/src/link.rs b/arbitrator/wasm-libraries/user-host/src/link.rs index f4c402fd97..cb9f046cdb 100644 --- a/arbitrator/wasm-libraries/user-host/src/link.rs +++ b/arbitrator/wasm-libraries/user-host/src/link.rs @@ -3,7 +3,11 @@ use crate::program::Program; use arbutil::{ - evm::{user::UserOutcomeKind, EvmData}, + evm::{ + api::{Gas, Ink}, + user::UserOutcomeKind, + EvmData, + }, format::DebugBytes, heapify, Bytes20, Bytes32, }; @@ -120,11 +124,11 @@ pub unsafe extern "C" fn programs__new_program( // buy ink let pricing = config.pricing; - let ink = pricing.gas_to_ink(gas); + let ink = pricing.gas_to_ink(Gas(gas)); // link the program and ready its instrumentation let module = wavm_link_module(&MemoryLeaf(*module_hash)); - program_set_ink(module, ink); + program_set_ink(module, ink.0); program_set_stack(module, config.max_depth); // provide arguments @@ -175,7 +179,7 @@ pub unsafe extern "C" fn programs__set_response( id, STATIC_MEM.read_slice(result_ptr, result_len), STATIC_MEM.read_slice(raw_data_ptr, raw_data_len), - gas, + Gas(gas), ); } @@ -207,7 +211,7 @@ pub unsafe extern "C" fn program_internal__set_done(mut status: UserOutcomeKind) let program = Program::current(); let module = program.module; let mut outs = program.outs.as_slice(); - let mut ink_left = program_ink_left(module); + let mut ink_left = Ink(program_ink_left(module)); // apply any early exit codes if let Some(early) = program.early_exit { @@ -218,12 +222,12 @@ pub unsafe extern "C" fn program_internal__set_done(mut status: UserOutcomeKind) if program_ink_status(module) != 0 { status = OutOfInk; outs = &[]; - ink_left = 0; + ink_left = Ink(0); } if program_stack_left(module) == 0 { status = OutOfStack; outs = &[]; - ink_left = 0; + ink_left = Ink(0); } let gas_left = program.config.pricing.ink_to_gas(ink_left); diff --git a/arbitrator/wasm-libraries/user-host/src/program.rs b/arbitrator/wasm-libraries/user-host/src/program.rs index 4199a691f7..7b3782b2e5 100644 --- a/arbitrator/wasm-libraries/user-host/src/program.rs +++ b/arbitrator/wasm-libraries/user-host/src/program.rs @@ -3,7 +3,7 @@ use arbutil::{ evm::{ - api::{EvmApiMethod, VecReader, EVM_API_METHOD_REQ_OFFSET}, + api::{EvmApiMethod, Gas, Ink, VecReader, EVM_API_METHOD_REQ_OFFSET}, req::{EvmApiRequestor, RequestHandler}, user::UserOutcomeKind, EvmData, @@ -49,7 +49,7 @@ static mut LAST_REQUEST_ID: u32 = 0x10000; #[derive(Clone)] pub(crate) struct UserHostRequester { data: Option>, - answer: Option<(Vec, VecReader, u64)>, + answer: Option<(Vec, VecReader, Gas)>, req_type: u32, id: u32, } @@ -95,7 +95,7 @@ impl UserHostRequester { req_id: u32, result: Vec, raw_data: Vec, - gas: u64, + gas: Gas, ) { self.answer = Some((result, VecReader::new(raw_data), gas)); if req_id != self.id { @@ -130,7 +130,7 @@ impl UserHostRequester { } #[no_mangle] - unsafe fn send_request(&mut self, req_type: u32, data: Vec) -> (Vec, VecReader, u64) { + unsafe fn send_request(&mut self, req_type: u32, data: Vec) -> (Vec, VecReader, Gas) { let req_id = self.set_request(req_type, &data); compiler_fence(Ordering::SeqCst); @@ -149,7 +149,7 @@ impl RequestHandler for UserHostRequester { &mut self, req_type: EvmApiMethod, req_data: impl AsRef<[u8]>, - ) -> (Vec, VecReader, u64) { + ) -> (Vec, VecReader, Gas) { unsafe { self.send_request( req_type as u32 + EVM_API_METHOD_REQ_OFFSET, @@ -265,7 +265,7 @@ impl UserHost for Program { println!("{} {text}", "Stylus says:".yellow()); } - fn trace(&mut self, name: &str, args: &[u8], outs: &[u8], _end_ink: u64) { + fn trace(&mut self, name: &str, args: &[u8], outs: &[u8], _end_ink: Ink) { let args = hex::encode(args); let outs = hex::encode(outs); println!("Error: unexpected hostio tracing info for {name} while proving: {args}, {outs}"); From faadf7968161c1f8b30757af4e6386be64343264 Mon Sep 17 00:00:00 2001 From: Lee Bousfield Date: Mon, 14 Oct 2024 16:49:58 -0500 Subject: [PATCH 30/38] Fix user-test compilation --- .../wasm-libraries/user-test/src/host.rs | 28 ++++++++-- .../wasm-libraries/user-test/src/ink.rs | 5 +- .../wasm-libraries/user-test/src/program.rs | 54 +++++++++---------- 3 files changed, 53 insertions(+), 34 deletions(-) diff --git a/arbitrator/wasm-libraries/user-test/src/host.rs b/arbitrator/wasm-libraries/user-test/src/host.rs index f2912eaae3..f1b4506414 100644 --- a/arbitrator/wasm-libraries/user-test/src/host.rs +++ b/arbitrator/wasm-libraries/user-test/src/host.rs @@ -2,6 +2,7 @@ // For license information, see https://github.com/nitro/blob/master/LICENSE use crate::program::Program; +use arbutil::evm::api::Gas; use caller_env::GuestPtr; use user_host_trait::UserHost; @@ -63,7 +64,14 @@ pub unsafe extern "C" fn vm_hooks__call_contract( gas: u64, ret_len: GuestPtr, ) -> u8 { - hostio!(call_contract(contract, data, data_len, value, gas, ret_len)) + hostio!(call_contract( + contract, + data, + data_len, + value, + Gas(gas), + ret_len + )) } #[no_mangle] @@ -75,7 +83,11 @@ pub unsafe extern "C" fn vm_hooks__delegate_call_contract( ret_len: GuestPtr, ) -> u8 { hostio!(delegate_call_contract( - contract, data, data_len, gas, ret_len + contract, + data, + data_len, + Gas(gas), + ret_len )) } @@ -87,7 +99,13 @@ pub unsafe extern "C" fn vm_hooks__static_call_contract( gas: u64, ret_len: GuestPtr, ) -> u8 { - hostio!(static_call_contract(contract, data, data_len, gas, ret_len)) + hostio!(static_call_contract( + contract, + data, + data_len, + Gas(gas), + ret_len + )) } #[no_mangle] @@ -189,12 +207,12 @@ pub unsafe extern "C" fn vm_hooks__contract_address(ptr: GuestPtr) { #[no_mangle] pub unsafe extern "C" fn vm_hooks__evm_gas_left() -> u64 { - hostio!(evm_gas_left()) + hostio!(evm_gas_left()).0 } #[no_mangle] pub unsafe extern "C" fn vm_hooks__evm_ink_left() -> u64 { - hostio!(evm_ink_left()) + hostio!(evm_ink_left()).0 } #[no_mangle] diff --git a/arbitrator/wasm-libraries/user-test/src/ink.rs b/arbitrator/wasm-libraries/user-test/src/ink.rs index fca658e59b..72ecfadd96 100644 --- a/arbitrator/wasm-libraries/user-test/src/ink.rs +++ b/arbitrator/wasm-libraries/user-test/src/ink.rs @@ -2,6 +2,7 @@ // For license information, see https://github.com/OffchainLabs/nitro/blob/master/LICENSE use crate::{program::Program, CONFIG}; +use arbutil::evm::api::Ink; use prover::programs::{ config::PricingParams, prelude::{GasMeteredMachine, MachineMeter, MeteredMachine}, @@ -18,7 +19,7 @@ impl MeteredMachine for Program { fn ink_left(&self) -> MachineMeter { unsafe { match user_ink_status() { - 0 => MachineMeter::Ready(user_ink_left()), + 0 => MachineMeter::Ready(Ink(user_ink_left())), _ => MachineMeter::Exhausted, } } @@ -26,7 +27,7 @@ impl MeteredMachine for Program { fn set_meter(&mut self, meter: MachineMeter) { unsafe { - user_set_ink(meter.ink(), meter.status()); + user_set_ink(meter.ink().0, meter.status()); } } } diff --git a/arbitrator/wasm-libraries/user-test/src/program.rs b/arbitrator/wasm-libraries/user-test/src/program.rs index 85b522ee74..299fca08c3 100644 --- a/arbitrator/wasm-libraries/user-test/src/program.rs +++ b/arbitrator/wasm-libraries/user-test/src/program.rs @@ -4,7 +4,7 @@ use crate::{ARGS, EVER_PAGES, EVM_DATA, KEYS, LOGS, OPEN_PAGES, OUTS}; use arbutil::{ evm::{ - api::{EvmApi, VecReader}, + api::{EvmApi, Gas, Ink, VecReader}, user::UserOutcomeKind, EvmData, }, @@ -80,7 +80,7 @@ impl UserHost for Program { println!("{} {text}", "Stylus says:".yellow()); } - fn trace(&mut self, name: &str, args: &[u8], outs: &[u8], _end_ink: u64) { + fn trace(&mut self, name: &str, args: &[u8], outs: &[u8], _end_ink: Ink) { let args = hex::encode(args); let outs = hex::encode(outs); println!("Error: unexpected hostio tracing info for {name} while proving: {args}, {outs}"); @@ -102,18 +102,18 @@ impl Program { pub struct MockEvmApi; impl EvmApi for MockEvmApi { - fn get_bytes32(&mut self, key: Bytes32, _evm_api_gas_to_use: u64) -> (Bytes32, u64) { + fn get_bytes32(&mut self, key: Bytes32, _evm_api_gas_to_use: Gas) -> (Bytes32, Gas) { let value = KEYS.lock().get(&key).cloned().unwrap_or_default(); - (value, 2100) // pretend worst case + (value, Gas(2100)) // pretend worst case } - fn cache_bytes32(&mut self, key: Bytes32, value: Bytes32) -> u64 { + fn cache_bytes32(&mut self, key: Bytes32, value: Bytes32) -> Gas { KEYS.lock().insert(key, value); - 0 + Gas(0) } - fn flush_storage_cache(&mut self, _clear: bool, _gas_left: u64) -> Result { - Ok(22100 * KEYS.lock().len() as u64) // pretend worst case + fn flush_storage_cache(&mut self, _clear: bool, _gas_left: Gas) -> Result { + Ok(Gas(22100) * KEYS.lock().len() as u64) // pretend worst case } fn get_transient_bytes32(&mut self, _key: Bytes32) -> Bytes32 { @@ -130,10 +130,10 @@ impl EvmApi for MockEvmApi { &mut self, _contract: Bytes20, _calldata: &[u8], - _gas_left: u64, - _gas_req: u64, + _gas_left: Gas, + _gas_req: Gas, _value: Bytes32, - ) -> (u32, u64, UserOutcomeKind) { + ) -> (u32, Gas, UserOutcomeKind) { unimplemented!() } @@ -141,9 +141,9 @@ impl EvmApi for MockEvmApi { &mut self, _contract: Bytes20, _calldata: &[u8], - _gas_left: u64, - _gas_req: u64, - ) -> (u32, u64, UserOutcomeKind) { + _gas_left: Gas, + _gas_req: Gas, + ) -> (u32, Gas, UserOutcomeKind) { unimplemented!() } @@ -151,9 +151,9 @@ impl EvmApi for MockEvmApi { &mut self, _contract: Bytes20, _calldata: &[u8], - _gas_left: u64, - _gas_req: u64, - ) -> (u32, u64, UserOutcomeKind) { + _gas_left: Gas, + _gas_req: Gas, + ) -> (u32, Gas, UserOutcomeKind) { unimplemented!() } @@ -161,8 +161,8 @@ impl EvmApi for MockEvmApi { &mut self, _code: Vec, _endowment: Bytes32, - _gas: u64, - ) -> (Result, u32, u64) { + _gas: Gas, + ) -> (Result, u32, Gas) { unimplemented!() } @@ -171,8 +171,8 @@ impl EvmApi for MockEvmApi { _code: Vec, _endowment: Bytes32, _salt: Bytes32, - _gas: u64, - ) -> (Result, u32, u64) { + _gas: Gas, + ) -> (Result, u32, Gas) { unimplemented!() } @@ -185,19 +185,19 @@ impl EvmApi for MockEvmApi { Ok(()) } - fn account_balance(&mut self, _address: Bytes20) -> (Bytes32, u64) { + fn account_balance(&mut self, _address: Bytes20) -> (Bytes32, Gas) { unimplemented!() } - fn account_code(&mut self, _address: Bytes20, _gas_left: u64) -> (VecReader, u64) { + fn account_code(&mut self, _address: Bytes20, _gas_left: Gas) -> (VecReader, Gas) { unimplemented!() } - fn account_codehash(&mut self, _address: Bytes20) -> (Bytes32, u64) { + fn account_codehash(&mut self, _address: Bytes20) -> (Bytes32, Gas) { unimplemented!() } - fn add_pages(&mut self, pages: u16) -> u64 { + fn add_pages(&mut self, pages: u16) -> Gas { let model = MemoryModel::new(2, 1000); unsafe { let (open, ever) = (OPEN_PAGES, EVER_PAGES); @@ -212,8 +212,8 @@ impl EvmApi for MockEvmApi { _name: &str, _args: &[u8], _outs: &[u8], - _start_ink: u64, - _end_ink: u64, + _start_ink: Ink, + _end_ink: Ink, ) { unimplemented!() } From 7bc39d3e0feb879d8ea0f1daacb1c2806d8babdf Mon Sep 17 00:00:00 2001 From: Lee Bousfield Date: Mon, 14 Oct 2024 20:09:06 -0500 Subject: [PATCH 31/38] Fix formatting --- arbitrator/prover/src/machine.rs | 7 ++++++- arbitrator/prover/src/programs/meter.rs | 9 ++++++++- arbitrator/stylus/src/test/mod.rs | 5 ++++- 3 files changed, 18 insertions(+), 3 deletions(-) diff --git a/arbitrator/prover/src/machine.rs b/arbitrator/prover/src/machine.rs index 66992019f9..dec355ac7c 100644 --- a/arbitrator/prover/src/machine.rs +++ b/arbitrator/prover/src/machine.rs @@ -1816,7 +1816,12 @@ impl Machine { } #[cfg(feature = "native")] - pub fn call_user_func(&mut self, func: &str, args: Vec, ink: arbutil::evm::api::Ink) -> Result> { + pub fn call_user_func( + &mut self, + func: &str, + args: Vec, + ink: arbutil::evm::api::Ink, + ) -> Result> { self.set_ink(ink); self.call_function("user", func, args) } diff --git a/arbitrator/prover/src/programs/meter.rs b/arbitrator/prover/src/programs/meter.rs index fe98bbcbc1..0d7b3151d7 100644 --- a/arbitrator/prover/src/programs/meter.rs +++ b/arbitrator/prover/src/programs/meter.rs @@ -9,7 +9,14 @@ use crate::{ value::FunctionType, Machine, }; -use arbutil::{evm::{self, api::{Gas, Ink}}, operator::OperatorInfo, Bytes32}; +use arbutil::{ + evm::{ + self, + api::{Gas, Ink}, + }, + operator::OperatorInfo, + Bytes32, +}; use derivative::Derivative; use eyre::Result; use fnv::FnvHashMap as HashMap; diff --git a/arbitrator/stylus/src/test/mod.rs b/arbitrator/stylus/src/test/mod.rs index 830b9cd23f..3fd0faede8 100644 --- a/arbitrator/stylus/src/test/mod.rs +++ b/arbitrator/stylus/src/test/mod.rs @@ -3,7 +3,10 @@ use crate::{env::WasmEnv, native::NativeInstance, run::RunProgram, test::api::TestEvmApi}; use arbutil::{ - evm::{api::{Ink, VecReader}, user::UserOutcome}, + evm::{ + api::{Ink, VecReader}, + user::UserOutcome, + }, Bytes20, Bytes32, Color, }; use eyre::{bail, Result}; From eb2c5be47bca0f1089a61a1365884fbec17a3c02 Mon Sep 17 00:00:00 2001 From: Ganesh Vanahalli Date: Tue, 15 Oct 2024 14:55:06 +0530 Subject: [PATCH 32/38] address PR comments --- .github/workflows/ci.yml | 4 ++-- system_tests/common_test.go | 28 ++++++++++++++++++++++------ system_tests/program_test.go | 8 +++----- validator/inputs/writer.go | 8 +++----- 4 files changed, 30 insertions(+), 18 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 660f9f4dea..b4966a9e95 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -173,13 +173,13 @@ jobs: - name: create block input json file if: matrix.test-mode == 'defaults' run: | - gotestsum --format short-verbose -- -run TestProgramStorage$ ./system_tests/... --count 1 --validator.inputswriter.basedir="${{ github.workspace }}/target" + gotestsum --format short-verbose -- -run TestProgramStorage$ ./system_tests/... --count 1 --recordBlockInputs.WithBaseDir="${{ github.workspace }}/target" --recordBlockInputs.WithTimestampDirEnabled=false --recordBlockInputs.WithBlockIdInFileNameEnabled=false - name: run arbitrator prover on block input json if: matrix.test-mode == 'defaults' run: | make build-prover-bin - target/bin/prover target/machines/latest/machine.wavm.br -b --json-inputs="${{ github.workspace }}/target/block_inputs.json" + target/bin/prover target/machines/latest/machine.wavm.br -b --json-inputs="${{ github.workspace }}/target/TestProgramStorage/block_inputs.json" - name: run challenge tests if: matrix.test-mode == 'challenge' diff --git a/system_tests/common_test.go b/system_tests/common_test.go index f4e2edad42..7700ec28e4 100644 --- a/system_tests/common_test.go +++ b/system_tests/common_test.go @@ -9,6 +9,7 @@ import ( "encoding/binary" "encoding/hex" "encoding/json" + "flag" "io" "math/big" "net" @@ -1716,10 +1717,18 @@ func logParser[T any](t *testing.T, source string, name string) func(*types.Log) } } +var ( + recordBlockInputsEnable = flag.Bool("recordBlockInputs.enable", true, "Whether to record block inputs as a json file") + recordBlockInputsWithSlug = flag.String("recordBlockInputs.WithSlug", "", "Slug directory for validationInputsWriter") + recordBlockInputsWithBaseDir = flag.String("recordBlockInputs.WithBaseDir", "", "Base directory for validationInputsWriter") + recordBlockInputsWithTimestampDirEnabled = flag.Bool("recordBlockInputs.WithTimestampDirEnabled", true, "Whether to add timestamp directory while recording block inputs") + recordBlockInputsWithBlockIdInFileNameEnabled = flag.Bool("recordBlockInputs.WithBlockIdInFileNameEnabled", true, "Whether to record block inputs using test specific block_id") +) + // recordBlock writes a json file with all of the data needed to validate a block. // // This can be used as an input to the arbitrator prover to validate a block. -func recordBlock(t *testing.T, block uint64, builder *NodeBuilder, baseDirectory string) { +func recordBlock(t *testing.T, block uint64, builder *NodeBuilder) { t.Helper() ctx := builder.ctx inboxPos := arbutil.MessageIndex(block) @@ -1733,11 +1742,18 @@ func recordBlock(t *testing.T, block uint64, builder *NodeBuilder, baseDirectory break } } - validationInputsWriter, err := inputs.NewWriter( - inputs.WithBaseDir(baseDirectory), - inputs.WithTimestampDirEnabled(false), - inputs.WithBlockIdInFileNameEnabled(false), - ) + var options []inputs.WriterOption + options = append(options, inputs.WithTimestampDirEnabled(*recordBlockInputsWithTimestampDirEnabled)) + options = append(options, inputs.WithBlockIdInFileNameEnabled(*recordBlockInputsWithBlockIdInFileNameEnabled)) + if *recordBlockInputsWithBaseDir != "" { + options = append(options, inputs.WithBaseDir(*recordBlockInputsWithBaseDir)) + } + if *recordBlockInputsWithSlug != "" { + options = append(options, inputs.WithSlug(*recordBlockInputsWithSlug)) + } else { + options = append(options, inputs.WithSlug(t.Name())) + } + validationInputsWriter, err := inputs.NewWriter(options...) Require(t, err) inputJson, err := builder.L2.ConsensusNode.StatelessBlockValidator.ValidationInputsAt(ctx, inboxPos, rawdb.TargetWavm) if err != nil { diff --git a/system_tests/program_test.go b/system_tests/program_test.go index d007a40ddd..d6324df301 100644 --- a/system_tests/program_test.go +++ b/system_tests/program_test.go @@ -394,8 +394,6 @@ func errorTest(t *testing.T, jit bool) { validateBlocks(t, 7, jit, builder) } -var validatorInputsWriterBaseDir = flag.String("validator.inputswriter.basedir", "", "Base directory for validationInputsWriter") - func TestProgramStorage(t *testing.T) { t.Parallel() storageTest(t, true) @@ -426,11 +424,11 @@ func storageTest(t *testing.T, jit bool) { validateBlocks(t, 2, jit, builder) - // Captures a block_input_.json file for the block that included the + // Captures a block_inputs json file for the block that included the // storage write transaction. Include wasm targets necessary for arbitrator prover and jit binaries flag.Parse() - if *validatorInputsWriterBaseDir != "" { - recordBlock(t, receipt.BlockNumber.Uint64(), builder, *validatorInputsWriterBaseDir) + if *recordBlockInputsEnable { + recordBlock(t, receipt.BlockNumber.Uint64(), builder) } } diff --git a/validator/inputs/writer.go b/validator/inputs/writer.go index fc7456b8bf..1a476c52a3 100644 --- a/validator/inputs/writer.go +++ b/validator/inputs/writer.go @@ -146,13 +146,11 @@ func (w *Writer) Write(json *server_api.InputJSON) error { if err != nil { return err } - var fileName string + fileName := "block_inputs.json" if w.useBlockIdInFileName { - fileName = filepath.Join(dir, fmt.Sprintf("block_inputs_%d.json", json.Id)) - } else { - fileName = filepath.Join(dir, "block_inputs.json") + fileName = fmt.Sprintf("block_inputs_%d.json", json.Id) } - if err = os.WriteFile(fileName, contents, 0600); err != nil { + if err = os.WriteFile(filepath.Join(dir, fileName), contents, 0600); err != nil { return err } return nil From 2b1d7a26cd557333183f4358f7180ecb0d74aff5 Mon Sep 17 00:00:00 2001 From: Ganesh Vanahalli Date: Tue, 15 Oct 2024 19:22:20 +0530 Subject: [PATCH 33/38] prioritize test flag over the test's decision to record a block --- system_tests/common_test.go | 4 ++++ system_tests/program_test.go | 6 +----- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/system_tests/common_test.go b/system_tests/common_test.go index 7700ec28e4..274e368b74 100644 --- a/system_tests/common_test.go +++ b/system_tests/common_test.go @@ -1730,6 +1730,10 @@ var ( // This can be used as an input to the arbitrator prover to validate a block. func recordBlock(t *testing.T, block uint64, builder *NodeBuilder) { t.Helper() + flag.Parse() + if !*recordBlockInputsEnable { + return + } ctx := builder.ctx inboxPos := arbutil.MessageIndex(block) for { diff --git a/system_tests/program_test.go b/system_tests/program_test.go index d6324df301..9aac1eb589 100644 --- a/system_tests/program_test.go +++ b/system_tests/program_test.go @@ -7,7 +7,6 @@ import ( "bytes" "context" "encoding/binary" - "flag" "fmt" "math" "math/big" @@ -426,10 +425,7 @@ func storageTest(t *testing.T, jit bool) { // Captures a block_inputs json file for the block that included the // storage write transaction. Include wasm targets necessary for arbitrator prover and jit binaries - flag.Parse() - if *recordBlockInputsEnable { - recordBlock(t, receipt.BlockNumber.Uint64(), builder) - } + recordBlock(t, receipt.BlockNumber.Uint64(), builder) } func TestProgramTransientStorage(t *testing.T) { From 087e59b4982eaed7d517ae55368777ef27928812 Mon Sep 17 00:00:00 2001 From: Lee Bousfield Date: Tue, 15 Oct 2024 21:57:23 -0500 Subject: [PATCH 34/38] Address PR comments --- arbitrator/arbutil/src/evm/api.rs | 10 ++++++++++ arbitrator/arbutil/src/pricing.rs | 2 +- arbitrator/prover/src/programs/memory.rs | 6 +++--- 3 files changed, 14 insertions(+), 4 deletions(-) diff --git a/arbitrator/arbutil/src/evm/api.rs b/arbitrator/arbutil/src/evm/api.rs index 8715f5ac8c..0a603a3bb2 100644 --- a/arbitrator/arbutil/src/evm/api.rs +++ b/arbitrator/arbutil/src/evm/api.rs @@ -120,6 +120,16 @@ macro_rules! derive_math { } impl $t { + /// Equivalent to the Add trait, but const. + pub const fn add(self, rhs: Self) -> Self { + Self(self.0 + rhs.0) + } + + /// Equivalent to the Sub trait, but const. + pub const fn sub(self, rhs: Self) -> Self { + Self(self.0 - rhs.0) + } + pub const fn saturating_add(self, rhs: Self) -> Self { Self(self.0.saturating_add(rhs.0)) } diff --git a/arbitrator/arbutil/src/pricing.rs b/arbitrator/arbutil/src/pricing.rs index 91de739303..4d6bf827be 100644 --- a/arbitrator/arbutil/src/pricing.rs +++ b/arbitrator/arbutil/src/pricing.rs @@ -7,7 +7,7 @@ use crate::evm::api::Ink; pub const HOSTIO_INK: Ink = Ink(8400); /// For hostios that include pointers. -pub const PTR_INK: Ink = Ink(13440 - HOSTIO_INK.0); +pub const PTR_INK: Ink = Ink(13440).sub(HOSTIO_INK); /// For hostios that involve an API cost. pub const EVM_API_INK: Ink = Ink(59673); diff --git a/arbitrator/prover/src/programs/memory.rs b/arbitrator/prover/src/programs/memory.rs index 758f2f3e82..82c4d4469d 100644 --- a/arbitrator/prover/src/programs/memory.rs +++ b/arbitrator/prover/src/programs/memory.rs @@ -83,14 +83,14 @@ fn test_model() { let model = MemoryModel::new(2, 1000); for jump in 1..=128 { - let mut total = 0; + let mut total = Gas(0); let mut pages = 0; while pages < 128 { let jump = jump.min(128 - pages); - total += model.gas_cost(jump, pages, pages).0; + total += model.gas_cost(jump, pages, pages); pages += jump; } - assert_eq!(total, 31999998); + assert_eq!(total, Gas(31999998)); } for jump in 1..=128 { From 8f961b0fc9f3ba5997c0d957759fb60a08552802 Mon Sep 17 00:00:00 2001 From: Lee Bousfield Date: Tue, 15 Oct 2024 22:23:10 -0500 Subject: [PATCH 35/38] Regenerate data-poster external-signer test certs --- arbnode/dataposter/testdata/client.crt | 45 ++++++++-------- arbnode/dataposter/testdata/client.key | 52 +++++++++---------- arbnode/dataposter/testdata/localhost.crt | 48 ++++++++--------- arbnode/dataposter/testdata/localhost.key | 52 +++++++++---------- .../dataposter/testdata/regenerate-certs.sh | 8 +++ 5 files changed, 103 insertions(+), 102 deletions(-) create mode 100755 arbnode/dataposter/testdata/regenerate-certs.sh diff --git a/arbnode/dataposter/testdata/client.crt b/arbnode/dataposter/testdata/client.crt index 3d494be820..9171094ba5 100644 --- a/arbnode/dataposter/testdata/client.crt +++ b/arbnode/dataposter/testdata/client.crt @@ -1,28 +1,25 @@ -----BEGIN CERTIFICATE----- -MIIE0jCCA7qgAwIBAgIUPaBB3/hHMpZfGB3VOw1+mHG4LnUwDQYJKoZIhvcNAQEL +MIIEIjCCAwqgAwIBAgIUV1axsouzA9h1Vgr2cPv17AvUrKswDQYJKoZIhvcNAQEL BQAwgYMxCzAJBgNVBAYTAkNIMQswCQYDVQQIDAJaSDEPMA0GA1UEBwwGWnVyaWNo MRYwFAYDVQQKDA1PZmZjaGFpbiBMYWJzMRIwEAYDVQQDDAlsb2NhbGhvc3QxKjAo -BgkqhkiG9w0BCQEWG25vdGFiaWdkZWFsQG9mZmNoYWlubGFicy5jaDAeFw0yMzEw -MTYxNDU2MjhaFw0yNDEwMTUxNDU2MjhaMIGDMQswCQYDVQQGEwJDSDELMAkGA1UE -CAwCWkgxDzANBgNVBAcMBlp1cmljaDEWMBQGA1UECgwNT2ZmY2hhaW4gTGFiczES -MBAGA1UEAwwJbG9jYWxob3N0MSowKAYJKoZIhvcNAQkBFhtub3RhYmlnZGVhbEBv -ZmZjaGFpbmxhYnMuY2gwggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEKAoIBAQC1 -1asfUzv07QTVwlM4o3g51ilIFEApPkpdQej/GIItLEVRQW+GI9jYuEM07wdwMhSH -JPFNbZB3dmBuqDLx13hY03ufyeY+nab0/sO6x13kXChvIqgPRyJtkEAoYkMM3W0D -S6HeL/6DFoTQ2xAlZb/7i/9deuUwDL3MNVSjPCm9PjFzSOFgAQQud2uUT7aENGuG -Whw3oXz9gU/8gv3keLzcIa2PHyEW5M7jeGSYMjfW3wr0d+Z5mSNRc/U6kncKi06c -QrMKrgFfF7a5kHgxUL7bRCGgCMemXe7VfrW6oKT11JcLWDKhe+uo6bNXUptek55H -HfQi6x8cbM46/h3riZA3AgMBAAGjggE6MIIBNjAdBgNVHQ4EFgQUQD2BOems0+JQ -br234cW5noMmXRIwga0GA1UdIwSBpTCBoqGBiaSBhjCBgzELMAkGA1UEBhMCQ0gx -CzAJBgNVBAgMAlpIMQ8wDQYDVQQHDAZadXJpY2gxFjAUBgNVBAoMDU9mZmNoYWlu -IExhYnMxEjAQBgNVBAMMCWxvY2FsaG9zdDEqMCgGCSqGSIb3DQEJARYbbm90YWJp -Z2RlYWxAb2ZmY2hhaW5sYWJzLmNoghQ9oEHf+Ecyll8YHdU7DX6YcbgudTAJBgNV -HRMEAjAAMAsGA1UdDwQEAwIFoDAfBgNVHREEGDAWgglsb2NhbGhvc3SCCTEyNy4w -LjAuMTAsBglghkgBhvhCAQ0EHxYdT3BlblNTTCBHZW5lcmF0ZWQgQ2VydGlmaWNh -dGUwDQYJKoZIhvcNAQELBQADggEBAF4EVkOZZeMIvv0JViP7NsmIl2ke/935x6Hd -hQiLUw13XHYXzMa5/8Y5fnKjttBODpFoQlwjgI18vzuYzItYMBc2cabQJcpfG+Wq -M3m/wl1TC2XOuHj1E4RA/nU3tslntahtXG+vkks9RN+f9irHUhDRR6AGSnSB2Gi/ -B2OGmXn7S4Qge8+fGHAjN+tlu+tOoEWP6R3if/a9UIe5EGM8QTe4zw6lr+iPrOhC -M94pK5IEWn5IIGhr3zJIYkm/Dp+rFqhV1sqPOjjFLVCA7KJ3jVVVHlcm4Xa/+fyk -CIm7/VAmnbeUNlMbkXNOfQMeku8Iwsu80pvf3kjhU/PgO/5oojk= +BgkqhkiG9w0BCQEWG25vdGFiaWdkZWFsQG9mZmNoYWlubGFicy5jaDAgFw0yNDEw +MTYwMzI1NDdaGA8yMTI0MDkyMjAzMjU0N1owgYMxCzAJBgNVBAYTAkNIMQswCQYD +VQQIDAJaSDEPMA0GA1UEBwwGWnVyaWNoMRYwFAYDVQQKDA1PZmZjaGFpbiBMYWJz +MRIwEAYDVQQDDAlsb2NhbGhvc3QxKjAoBgkqhkiG9w0BCQEWG25vdGFiaWdkZWFs +QG9mZmNoYWlubGFicy5jaDCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEB +AKU5q5iwYV4/gPeWcyys561pTGV4pk+sRY2q0znsZFOYcxrjaXEjj2HGNkvH1rKy +8Cv1ZoFW+1ejQZeLtd0qL9v5fDkdLsmCZaIYI5Bvo2CfY6KLUZ5c1q2K2GZgQk8i +eSbqBXq+F/EwziDfheXkhDoAE05hOg684titb21eJ0ZK7f7Koam7cmbQI0lqUCrt +MLp0cJzWnfW0SpCzahnCZ5h31BZeZIRLOxsTvg5N1wOivrdWLXGVbprNCGGhVg0E +ZxhwI00pU/E/K4mcKjtPy/5fqe71jH7/iLYNmhRp6PrA78GilxTT79rro8ooantD +GyQbm+Qkk2tMHHum3GOcjuECAwEAAaOBiTCBhjAdBgNVHQ4EFgQUIUFF6jA0NkRA +1kZhJKH0W/9zJWIwCQYDVR0TBAIwADALBgNVHQ8EBAMCBaAwHwYDVR0RBBgwFoIJ +bG9jYWxob3N0ggkxMjcuMC4wLjEwLAYJYIZIAYb4QgENBB8WHU9wZW5TU0wgR2Vu +ZXJhdGVkIENlcnRpZmljYXRlMA0GCSqGSIb3DQEBCwUAA4IBAQCkfknujeFa0yf4 +YX/3ltP9itq4hLtAYnQF7M/uC86QdyDPsrNqhvj54qC0BnR5wGeZP3c144J2mAUr +4j4Y/ztgFVBR4rLyatHgm0/tL/fy/UgjeSmpY4UOr1QnpNP3fIzL7hxacS4uO8v4 +wcc5KlG/xjHRcrzJaaWLldCogBMb8vlModcbeKrkvQ4hUF+zf138RtpRfcRf1X5c +EaAtUZk+BxVYS79qL7YyESRD8YYMhIImLuiyPt2V3HQRhrjqa3mzODBLhUbNRWPX +8/CH2UZ6TD9Hy4FVX0VZzLoDZjfi4KCTgXI3WGrDoL4FF26cSiK8HVx0qJzAWw4a +tkkj5jtd -----END CERTIFICATE----- diff --git a/arbnode/dataposter/testdata/client.key b/arbnode/dataposter/testdata/client.key index b14941dd9f..4313d0f124 100644 --- a/arbnode/dataposter/testdata/client.key +++ b/arbnode/dataposter/testdata/client.key @@ -1,28 +1,28 @@ -----BEGIN PRIVATE KEY----- -MIIEvwIBADANBgkqhkiG9w0BAQEFAASCBKkwggSlAgEAAoIBAQC11asfUzv07QTV -wlM4o3g51ilIFEApPkpdQej/GIItLEVRQW+GI9jYuEM07wdwMhSHJPFNbZB3dmBu -qDLx13hY03ufyeY+nab0/sO6x13kXChvIqgPRyJtkEAoYkMM3W0DS6HeL/6DFoTQ -2xAlZb/7i/9deuUwDL3MNVSjPCm9PjFzSOFgAQQud2uUT7aENGuGWhw3oXz9gU/8 -gv3keLzcIa2PHyEW5M7jeGSYMjfW3wr0d+Z5mSNRc/U6kncKi06cQrMKrgFfF7a5 -kHgxUL7bRCGgCMemXe7VfrW6oKT11JcLWDKhe+uo6bNXUptek55HHfQi6x8cbM46 -/h3riZA3AgMBAAECggEADUboCYMCpm+LqIhzNCtqswQD6QsiSwCmqs8nuKZGk9ue -+hmZj5IpgMJZLrgvWY4s+PGfgiRR/28QCBrVXkETiZ5zirQFN4tvLlKcSK4xZf29 -FBRUCiPxck36NhiqrBNOi1Mn8BKedl4cESkvSu1cvcmeOh100HPcHfLDVqHx3qsl -D/5yMkT2+zdhtLa+X3nkAa+3aibOvgtyfkV679e20CG6h89N9GBKkTXO8ioLZZVm -84ksnd4FcpTo7ebJJxElEB+ZA4akPHbF6ArUmcpqtGso5GtwqqO2ZlguSn2XQT0d -jqvOG4DwfSXk6SpE/dpWvU92fmxWAxZvGrZNgDyJ2QKBgQDyQ8NN4b80Yza/YXar -LWx8A6B0eMc1dXgt9m3UUI+titt45jEcaXhCX01FRFTznWGmWFtJmcWBoaQVPVel -IcDYQSxEuBUrCeI75ocv/IQtENaiX3TK7Nlz5RHfpQpfDVJq45lpiD38CGkYkAif -9pSzC8aup4W3WR0JJZ1AOHUZaQKBgQDAJNJnaSNzB+eDWTKCIN5V9X3QMkmjsuir -Nf2lBXHYARnlYWAbtYFG12wLJQMTNX5ewVQQrWtsdPkGPpCnPLelUTxMssrsXjej -JlLzYUfzRBqEXMI3AA9bVdiauxId2RTcp2F81SM1keCMcuHYxrzVkBSOC9u3wCnb -Whb6+feInwKBgQCbzgC5AcoaQwReqKvNAvWV/C8hONvFAbs8tBOGTBlbHsZvRnun -Lh1tciUbuwp3cmvuszxiZUakS/RexIitZrvDWIbD2y+h8kVRCL1Am0HWSdH/syxF -pXVkF5obHuVApCyxGZb8S+axRCdy6I7jcY3IaHZqtMpGVEVcMJilSKnmoQKBgQCC -tEmgaMfhhx34nqOaG4vDA4T7LEolnh1h4g9RwztnCZC5FZ1QHA79xqrLhfjqhzgY -cwChe6aYl5WSptq1uLrgLTuMnQ8m7QyB4h8JSkKse8ZiBctjqJnJssLutpSjUzk6 -xG2vgjk6RqpuP/PcB40K5cDlw7FJ9OFEQqthPMsi1wKBgQC0/vv5bY3DQ+wV6gUy -nFoSa/XNHaa8y7jmmlCnWJqs6DAAQQ3VW0tPX03GYL/NDcI+PwzYDHDkSB6Qa/o8 -VzVGK1/kr/+bveNvqmi0vNb54fMFLveGgsY4Cu1cffiw8m6nYJ/V4eCsHfpF1B5L -5HDnt5rFKt1Mi9WsUSRtxipxBA== +MIIEvAIBADANBgkqhkiG9w0BAQEFAASCBKYwggSiAgEAAoIBAQClOauYsGFeP4D3 +lnMsrOetaUxleKZPrEWNqtM57GRTmHMa42lxI49hxjZLx9aysvAr9WaBVvtXo0GX +i7XdKi/b+Xw5HS7JgmWiGCOQb6Ngn2Oii1GeXNatithmYEJPInkm6gV6vhfxMM4g +34Xl5IQ6ABNOYToOvOLYrW9tXidGSu3+yqGpu3Jm0CNJalAq7TC6dHCc1p31tEqQ +s2oZwmeYd9QWXmSESzsbE74OTdcDor63Vi1xlW6azQhhoVYNBGcYcCNNKVPxPyuJ +nCo7T8v+X6nu9Yx+/4i2DZoUaej6wO/BopcU0+/a66PKKGp7QxskG5vkJJNrTBx7 +ptxjnI7hAgMBAAECggEAAagHWVuDTl+SmmjOtMby96ETm/zOpgPTGq14up7tDo17 +sexPtUum91L2XmIde+MhVz95jJhjoqhHUw6afyIaIrlojmYFfw2omSxmxt7no2NV +q158Lfs+R7UZoEUcxRBSaJp1/ZoEQW2800WKYRieXrp7dxCwdU9dctCiSlVkTWcU +w+f9xr084dUIKgtICbLWRdGDvGFmr99MBZXzHg5+x8MiAVtpiNcggRfQKIg2QYQv +xdUtfxrKHuRcbeo4QOgSR6fb772F5eO6hPfwgl0AqmSX9XyRaPox/vKcq531S95S +JvGeAdS47Qo8Elh9rIlC/pxVdJ/Gz4sbmlYCfcXDRQKBgQDbSFUrEaOnH5ITEDy/ +SDTCbdQ3bP1FmHVoLdCRoBohb0xHZrJoOn6cmxyyHWR6dwpv+rvi1bJCScDdphJM +zV8W5sG94PaM/dwCws4CFAwaAlMakrsNUXtMgIeub27mzX5OMTss8vjKdKbVDyAv +XCT4idJY1EOdLA3R+JTLSszxJwKBgQDA5CSKLn4HpZI6qmR5g7HRZ5g249BbX/q8 +oszAUIFfY0ME5aujWYRmTfdWno0Y2yG9x4g9QDVNK9fUH3Ii6pNRFGc/yF3HkbsP +kT6UW6rw9CeyyYPKjrFx7M+2kBWJ16+5noVvzWhLScMCt7IcVCKaqiJFapOkS75t +zBYH1IX0twKBgCtFBqlM/cIIlMZ2OcZ09RQ4n9ugAgotn11DTRivQvi+AYtFVIcE +o987LFppOl6ABus5ysFj8Zzq+MfD8XB+Rfk655gUQBJqNXPGBOicFBc9xjBEK+zg +2zepVRyymGuquPWs+URRXY51nkYEihFOWW1BpOQqXn0xKDj6mEHVLMOZAoGAc7Ol +k1ll8ZJIT3ZLxHPRYqmALVSjc1v0G9iPdsATijMRTUuyk84rU+5qcYOzYPh4mcyp +FQyBrGOjF7MxFG6epSDW+fRnBEGO8jyOTBFcTSI2+dBUhFjpaUvCIGD2+nLtDitf +IPwWFisNlYC4jrOM+jcZTYgrPX7NoDCt+k5pd6sCgYB7NMYEC9XjoXV8S3n2yni5 +y0oGLox39hh31LJ90yQ/l+oFJWn1BPnzTI6xeJTTzJjQf7padyvfw45b+3BywZHM +TI5LWcIaA6L+tiBlgBjYa7gE3CCxh0AdV+pUa8L/R6OWAK6+lg2zNt1/ommZ2sKg +LcbNAqMiMWH1a1el7idoBA== -----END PRIVATE KEY----- diff --git a/arbnode/dataposter/testdata/localhost.crt b/arbnode/dataposter/testdata/localhost.crt index ca33dfc8cc..8b7fb02c9a 100644 --- a/arbnode/dataposter/testdata/localhost.crt +++ b/arbnode/dataposter/testdata/localhost.crt @@ -1,28 +1,24 @@ -----BEGIN CERTIFICATE----- -MIIEwzCCA6ugAwIBAgIUHx3SdpCP5jXZE7USUqX5uRNFKPIwDQYJKoZIhvcNAQEL -BQAwfzELMAkGA1UEBhMCQ0gxCzAJBgNVBAgMAlpIMQ8wDQYDVQQHDAZadXJpY2gx -FjAUBgNVBAoMDU9mZmNoYWluIExhYnMxEjAQBgNVBAMMCWxvY2FsaG9zdDEmMCQG -CSqGSIb3DQEJARYXYmlnZGVhbEBvZmZjaGFpbmxhYnMuY2gwHhcNMjMxMDE2MTQ0 -MDA1WhcNMjQxMDE1MTQ0MDA1WjB/MQswCQYDVQQGEwJDSDELMAkGA1UECAwCWkgx -DzANBgNVBAcMBlp1cmljaDEWMBQGA1UECgwNT2ZmY2hhaW4gTGFiczESMBAGA1UE -AwwJbG9jYWxob3N0MSYwJAYJKoZIhvcNAQkBFhdiaWdkZWFsQG9mZmNoYWlubGFi -cy5jaDCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEBALg7XwaIh4l2Fp8a -MfNMdTQSMPMR0zpnicVTn/eiozWsqlAKaxmQM3PxJ0oVWW3iJ89p4rv5m+UjK6Dr -vsUQOzl8isgyGCTMnkLtxFlyallDNRDawRcuTPuNI9NkdJm+Zz7HooLzFeBDeS13 -iRPEXr1T/4af9MjOxqFvbw5xBY9k4tc2hPp6q00948gPWKIB9Mz4thoB2Hl2rQBY -X/WhjSnre9o9qoyBO0XAsG0mssBs1vPa9/aEp7C5cDY0HCuM1RIjhXnRpb8lC9VQ -aC+FozDffmm23EGVpLmyPs590UOtVJdTUd6Q0TAT6d7fjCRUJ12DendQf2uMFV90 -u6Yj0zUCAwEAAaOCATUwggExMB0GA1UdDgQWBBT2B3FTGFQ49JyBgDGLoZREOIGD -DTCBqAYDVR0jBIGgMIGdoYGEpIGBMH8xCzAJBgNVBAYTAkNIMQswCQYDVQQIDAJa -SDEPMA0GA1UEBwwGWnVyaWNoMRYwFAYDVQQKDA1PZmZjaGFpbiBMYWJzMRIwEAYD -VQQDDAlsb2NhbGhvc3QxJjAkBgkqhkiG9w0BCQEWF2JpZ2RlYWxAb2ZmY2hhaW5s -YWJzLmNoghQfHdJ2kI/mNdkTtRJSpfm5E0Uo8jAJBgNVHRMEAjAAMAsGA1UdDwQE -AwIFoDAfBgNVHREEGDAWgglsb2NhbGhvc3SCCTEyNy4wLjAuMTAsBglghkgBhvhC -AQ0EHxYdT3BlblNTTCBHZW5lcmF0ZWQgQ2VydGlmaWNhdGUwDQYJKoZIhvcNAQEL -BQADggEBAIkhBcnLeeNwUwb+sSG4Qm8JdeplHPMeViNfFIflUfIIYS00JA2q9w8W -+6Nh8s6Dn20lQETUnesYj97BdqzLjFuJYAlblhE+zP8g/3Mkpu+wZAGvQjUIRyGT -C17BEtQQgAnv5pD22jr9hpLl2KowN6Oo1gzilCA+AtMkNZFIGDOxzuIv2u8rSD89 -R/V6UEDMCgusFJnZ/GzKkUNbsrAfNUezNUal+KzMhHGHBwg4jfCNhnAAB43eRtJA -0pSRMMLcUEQnVotXDXYC3DhJmkYp1uXOH/tWs6z9xForOkWFxNMVj+zUWBi7n3Jw -N2BXlb64D96uor13U0dmvQJ72ooJc+A= +MIIEFzCCAv+gAwIBAgITSiI3ITH8yVNHC4Bh412fjdSR2TANBgkqhkiG9w0BAQsF +ADB/MQswCQYDVQQGEwJDSDELMAkGA1UECAwCWkgxDzANBgNVBAcMBlp1cmljaDEW +MBQGA1UECgwNT2ZmY2hhaW4gTGFiczESMBAGA1UEAwwJbG9jYWxob3N0MSYwJAYJ +KoZIhvcNAQkBFhdiaWdkZWFsQG9mZmNoYWlubGFicy5jaDAgFw0yNDEwMTYwMzI1 +NDdaGA8yMTI0MDkyMjAzMjU0N1owfzELMAkGA1UEBhMCQ0gxCzAJBgNVBAgMAlpI +MQ8wDQYDVQQHDAZadXJpY2gxFjAUBgNVBAoMDU9mZmNoYWluIExhYnMxEjAQBgNV +BAMMCWxvY2FsaG9zdDEmMCQGCSqGSIb3DQEJARYXYmlnZGVhbEBvZmZjaGFpbmxh +YnMuY2gwggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEKAoIBAQDK4BRVZ0nU98/f +l+QC4fF60oNtlMPCC/2R6GZoWz7VyhrrXBuol+F9vboAePxLgxeIr/pwHAbFWlW2 +ueAsN9dorC2waf5PDhfOE0gI6w7LysTkO5n7oMFf1KYPSpPJ15WxlobZR8qWeroR +we7z44tQ2F+es+HaqBrrk7jm0GS9AqaledN/ay9SP4CBu029F6nWDnK+VpNWuoN4 +A/pnwGFWrxDf0ftN7BxnxzzdsWs64+kYfz91Mojce2UKGuDTuk/oqOnHhX34bFDc +/e9KGAQqP1I+RuCJmQXW5b55+3WgpvT3u3Mp7478C+AK8GthPjja7go48nHp3uby +drNpTw+bAgMBAAGjgYkwgYYwHQYDVR0OBBYEFG09BO7OJcjB3fRFhPCsjQ6ICb2E +MAkGA1UdEwQCMAAwCwYDVR0PBAQDAgWgMB8GA1UdEQQYMBaCCWxvY2FsaG9zdIIJ +MTI3LjAuMC4xMCwGCWCGSAGG+EIBDQQfFh1PcGVuU1NMIEdlbmVyYXRlZCBDZXJ0 +aWZpY2F0ZTANBgkqhkiG9w0BAQsFAAOCAQEAJHG/5WOmpO7wg3BtTt4b0DoqDJjh +eQb9Woq5xbvliZ1RCp8E6m6BmOr66i4qu5r+31DEeAeQ2M9pG2nJKoayCVi2ygaQ +RAulxIH7o5+JUcZtX6FbRBsS7Go+SLmtkkJ89YVSIF40+2CAQs7loqQjHNeo9/iO +rVKt1Fa6rQhXmv4ItVOwRaMBvXRVw4gc3ObmH0ZBYZrvsE7uQkKX5f6sVKXOX3mm +ofyB+22QMYmx3XvEEQm8ELnjIr5Q8LxqQxHqjLFjyrcrXYVi4+3/PfjIdRr5+qes +H8JWJlAbF/SNncdXRb1jtkdxit56Qo7/Mz/c4Yuh1WLiYcQGJeBpr53dmg== -----END CERTIFICATE----- diff --git a/arbnode/dataposter/testdata/localhost.key b/arbnode/dataposter/testdata/localhost.key index aad9b40b3d..f56aef1e77 100644 --- a/arbnode/dataposter/testdata/localhost.key +++ b/arbnode/dataposter/testdata/localhost.key @@ -1,28 +1,28 @@ -----BEGIN PRIVATE KEY----- -MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQC4O18GiIeJdhaf -GjHzTHU0EjDzEdM6Z4nFU5/3oqM1rKpQCmsZkDNz8SdKFVlt4ifPaeK7+ZvlIyug -677FEDs5fIrIMhgkzJ5C7cRZcmpZQzUQ2sEXLkz7jSPTZHSZvmc+x6KC8xXgQ3kt -d4kTxF69U/+Gn/TIzsahb28OcQWPZOLXNoT6eqtNPePID1iiAfTM+LYaAdh5dq0A -WF/1oY0p63vaPaqMgTtFwLBtJrLAbNbz2vf2hKewuXA2NBwrjNUSI4V50aW/JQvV -UGgvhaMw335pttxBlaS5sj7OfdFDrVSXU1HekNEwE+ne34wkVCddg3p3UH9rjBVf -dLumI9M1AgMBAAECggEAHuc8oyKrQ5xmooUZHGP2pAeqJNfYXAtqoYpLwtUJ9hKy -1e7NdNIKw3fP/J4UrHk7btAm65us8hSCeMGatEErAhNZT0gR4zhcksMCBPQLkVIT -+HINYjdOzAJqoEbRRUnaVT5VDQy8HmyLCtyqhoGR18XbjshNnhKLYKCJ2z0Lrvf2 -3rU7bbt7/rvLitVhxVL8SIe2jWSfIgcEmEAZMigB9WAnUyQ/tAfbPy1I764LLfzD -nLXn7E2OH7GrxkLjOsH9kfERlur7V7IhC9NE/wI0q+rnILRa7Q3+ifRu8qla3bo1 -iyHl1ZmsYJ8Jnzbu9exzZaQmk42OoFPcMFm0mRe+2QKBgQDvRv0Q5JhBuVurkU98 -lzATwEO0uYmeWDMnHzrFSWAKr/x4LNQ9ytSCfe1aLxgOkZq6dQ3TyZiCYzpmwGz9 -K7/gghxmsVDKeCqiGVZOgFAWy7AhQyF6zM60oqqwSvJHhmGTsA/B5LPUiYe9lITW -ZSLVYkOzha7Coa++U8vPzI5VaQKBgQDFG4reFT79j8RKEm9jie6PdRdYMzOSDWty -Gjj5N9Jnlp1k/6RzCxjmp7w7yIorq/7fWZsQtt0UqgayOn25+I8dZeGC0BradUSB -tZbGElxPsF8Jg00ZvvK3G5mpZYDrJCud8Q05EaUZPXv9GuZhozEsTQgylVecVzsN -wyEK8VuZ7QKBgQChx9adUGIdtgzkILiknbh08j8U94mz1SCo5/WdpLHaKAlE29KZ -AQXUQP51Rng2iX4bab9yndCPADZheON3/debHX3EdUkRzFPPC+CN7TW5Y/jvVGtT -kxyDh6Ru1A2iDJr290iAKXjpUB/GL5/tMa5upiTuQYnasOWZgyC/nCf0WQKBgEwn -pRLDMLA1IMjhsInL3BEvU1KvjahLaQ0P1p1rlO6TAcLpBrewPPG5MwACLmhLLtFK -xJ/Dl02Jl8a61KLKxzi7iVLKZuWq00ouR8/FfkcHxOBfC6X74bkff9I0NogjVHrU -jKBVEe3blJEpGIP20mPka1tn2g68oUNi9dxNfm/NAoGAWj/Q0pgnNq0MQ8Lj6m99 -1baaXSo8biks3E3A3cqhHQm/j3SRnkf0lueQW8+r9yR9IWdYFXz5Waq13qK+lopE -KDmww0xr8dyMUYTP1vde7np2XKa/OX3iejDzbI3RcZN/DEV+dCBY8pqHHfaAaESu -fwBWvfD8wtwCZzB3lOZEi80= +MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQDK4BRVZ0nU98/f +l+QC4fF60oNtlMPCC/2R6GZoWz7VyhrrXBuol+F9vboAePxLgxeIr/pwHAbFWlW2 +ueAsN9dorC2waf5PDhfOE0gI6w7LysTkO5n7oMFf1KYPSpPJ15WxlobZR8qWeroR +we7z44tQ2F+es+HaqBrrk7jm0GS9AqaledN/ay9SP4CBu029F6nWDnK+VpNWuoN4 +A/pnwGFWrxDf0ftN7BxnxzzdsWs64+kYfz91Mojce2UKGuDTuk/oqOnHhX34bFDc +/e9KGAQqP1I+RuCJmQXW5b55+3WgpvT3u3Mp7478C+AK8GthPjja7go48nHp3uby +drNpTw+bAgMBAAECggEANE2Q8HOwlTdOYFbIcfXOS9v6BkZUMbLlrLg9rqnXiUaR +qhwVBWIiwEgpq/WFFfK2HodACacwF7EyZ+mD4eKDpni9Tr4E0lzPxlEyQRpYtjGQ +kUbMbBMFx68LIOYZM/Bgp2gnW90mXaVGU02sTTRctnsSK9g0Yir0xcdP5DHVxuRy +cReMIDjHu7/PN5X12oHpBxT1w7TKYZk3M2ZbmRcGfRSv1UIg06hXjxY4Tonqxv5b +Yv/R/r+RYsHE6cO3hLV7FE/ypaRFGt/qcHhqwmHKlpc+8/FNEpb5truXCbf5LhjM +YD2eMOpiBaiyufs+2BpUy/iDKToYz+fdusT5N3FgAQKBgQDuemOauY/ytbETUabB +Vt11fCvXvsx4L+45vqtVcRWl6jcy73rMnyA80D/ndzyxkuw2NRAllIUvsdVG26vM +8nWAxrsY7rrZ4kdRAAKAPYmTtT2O4mvhvJOYHA/ueEUamrKqZMgyLik1Wl1y2mt2 +Iak0zNB0GWoHsgDF20TTbxKTkwKBgQDZyAa/r3qhokKfvg/7LhwXH7vdiJd35zp1 +K1KZVeZf97FeReL4DLfTHZ92yFyVhWepp97Icd2WtQZ8jVVcesTP9C+mVoFEGXZf +nWx5y2WD92dBP/kKYNayXuBFQnRfS4AC7ALK8fKrU/Fzc7OctnkvHHWZfaKQ2sQ5 +xydj6Rso2QKBgBpgvT2zAsIU6MY7RNej1REWr/7IIvO0UYRfm7HytTNJ6dsfdBTI +ERfI7RicLsFxf+ErE2Mkv2qcH/wbdjBQLUEWOkGyvkY1ajACcURgCiSlam6wisBI +TIcJq5V0BijALbz9MsuiIXq+SRHYKQTDCmVFtlTxLrI1NTKtYzqD0akzAoGAB5fW +zGYk42/R3Nn2mq5f4lqD5VR224JfYmhxR9Fb5+qt73iGUlm3KxA0WCLiP4BYPe0R +cnGt5SxInp0a5c+N/yYnZyhK94Hfw7OsbY6u6mv82KSPXVJFChEOxrtrbUsnmnJ6 +InNPH7QcjgbxszwVe5QFcaWUvnIyN0V/VRdyj/kCgYEA0kPCIPR7t7OFEnQsw50W +AclrTlGRB0TzmCYZN4DTKC+1ZAV80XQUR4umJpHeyArdRZpFv+HBLF7fHsBnkmiU +ixBdbWgW4mxjAhyZkLMjcCkoiLmRPDUvELyFs4xpM+IEKoAk60PPbGi4CeIMHb5k +E47NZaw1bdG1tv2ya1FlQ/k= -----END PRIVATE KEY----- diff --git a/arbnode/dataposter/testdata/regenerate-certs.sh b/arbnode/dataposter/testdata/regenerate-certs.sh new file mode 100755 index 0000000000..6bcbd27b8e --- /dev/null +++ b/arbnode/dataposter/testdata/regenerate-certs.sh @@ -0,0 +1,8 @@ +#!/bin/bash +set -eu +cd "$(dirname "$0")" +for name in localhost client; do + openssl genrsa -out "$name.key" 2048 + csr="$(openssl req -new -key "$name.key" -config "$name.cnf" -batch)" + openssl x509 -req -signkey "$name.key" -out "$name.crt" -days 36500 -extensions req_ext -extfile "$name.cnf" <<< "$csr" +done From 247fb1f1a413113c5159e46d00254b85c85bbcb8 Mon Sep 17 00:00:00 2001 From: Aman Sanghi Date: Wed, 16 Oct 2024 13:20:28 +0530 Subject: [PATCH 36/38] Add log when switchover is complete --- arbnode/seq_coordinator.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/arbnode/seq_coordinator.go b/arbnode/seq_coordinator.go index 4fbc31a108..930f42ffe4 100644 --- a/arbnode/seq_coordinator.go +++ b/arbnode/seq_coordinator.go @@ -653,6 +653,9 @@ func (c *SeqCoordinator) update(ctx context.Context) time.Duration { readUntil = min(readUntil, c.prevRedisMessageCount) client = c.prevRedisCoordinator.Client } + if c.prevRedisMessageCount != 0 && localMsgCount >= c.prevRedisMessageCount { + log.Info("coordinator caught up to prev redis coordinator", "msgcount", localMsgCount, "prevMsgCount", c.prevRedisMessageCount) + } var messages []arbostypes.MessageWithMetadata msgToRead := localMsgCount var msgReadErr error From 3c1daf15bc737c6f8e8ab2e7a0ad2f30d9fb5be6 Mon Sep 17 00:00:00 2001 From: Aman Sanghi Date: Wed, 16 Oct 2024 13:30:15 +0530 Subject: [PATCH 37/38] Changes based on PR comments --- util/check-build.sh | 35 ++++++++++++++++++++--------------- 1 file changed, 20 insertions(+), 15 deletions(-) diff --git a/util/check-build.sh b/util/check-build.sh index 755cfda9ad..ddf540e45c 100755 --- a/util/check-build.sh +++ b/util/check-build.sh @@ -16,6 +16,8 @@ command_exists() { command -v "$1" >/dev/null 2>&1 } +EXIT_CODE=0 + # Detect operating system OS=$(uname -s) echo -e "${BLUE}Detected OS: $OS${NC}" @@ -26,16 +28,16 @@ if command_exists docker; then echo -e "${GREEN}Docker is installed.${NC}" else echo -e "${RED}Docker is not installed. $INSTALLATION_DOCS_URL${NC}" - exit 1 + EXIT_CODE=1 fi # Step 2: Check if Docker service is running if [[ "$OS" == "Linux" ]] && ! sudo service docker status >/dev/null; then echo -e "${YELLOW}Docker service is not running on Linux. Start it with: sudo service docker start${NC}" - exit 1 + EXIT_CODE=1 elif [[ "$OS" == "Darwin" ]] && ! docker info >/dev/null 2>&1; then echo -e "${YELLOW}Docker service is not running on macOS. Ensure Docker Desktop is started.${NC}" - exit 1 + EXIT_CODE=1 else echo -e "${GREEN}Docker service is running.${NC}" fi @@ -47,7 +49,7 @@ echo -e "${YELLOW}You are on the version tag: $VERSION_TAG${NC}" # Check if submodules are properly initialized and updated if git submodule status | grep -qE '^-|\+'; then echo -e "${YELLOW}Submodules are not properly initialized or updated. Run: git submodule update --init --recursive --force${NC}" - exit 1 + EXIT_CODE=1 else echo -e "${GREEN}All submodules are properly initialized and up to date.${NC}" fi @@ -57,7 +59,7 @@ if docker images | grep -q "nitro-node"; then echo -e "${GREEN}Nitro Docker image is built.${NC}" else echo -e "${RED}Nitro Docker image is not built. Build it using: docker build . --tag nitro-node${NC}" - exit 1 + EXIT_CODE=1 fi # Step 5: Check prerequisites for building binaries @@ -101,7 +103,7 @@ for pkg in "${prerequisites[@]}"; do [[ "$pkg" == "wasm2wat" ]] && pkg="wabt" [[ "$pkg" == "clang" ]] && pkg="llvm" echo -e "${RED}$pkg is not installed. Please install $pkg. $INSTALLATION_DOCS_URL${NC}" - exit 1 + EXIT_CODE=1 fi done @@ -110,7 +112,7 @@ if command_exists node && node -v | grep -q "v18"; then echo -e "${GREEN}Node.js version 18 is installed.${NC}" else echo -e "${RED}Node.js version 18 not installed. $INSTALLATION_DOCS_URL${NC}" - exit 1 + EXIT_CODE=1 fi # Step 7a: Check Rust version @@ -118,7 +120,7 @@ if command_exists rustc && rustc --version | grep -q "1.80.1"; then echo -e "${GREEN}Rust version 1.80.1 is installed.${NC}" else echo -e "${RED}Rust version 1.80.1 not installed. $INSTALLATION_DOCS_URL${NC}" - exit 1 + EXIT_CODE=1 fi # Step 7b: Check Rust nightly toolchain @@ -126,15 +128,16 @@ if rustup toolchain list | grep -q "nightly"; then echo -e "${GREEN}Rust nightly toolchain is installed.${NC}" else echo -e "${RED}Rust nightly toolchain is not installed. Install it using: rustup toolchain install nightly${NC}" - exit 1 + EXIT_CODE=1 fi # Step 8: Check Go version -if command_exists go && go version | grep -q "go1.23"; then - echo -e "${GREEN}Go version 1.23 is installed.${NC}" +go_version_needed=$(grep "^go " go.mod | awk '{print $2}') +if command_exists go && go version | grep -q "$go_version_needed"; then + echo -e "${GREEN}Go version $go_version_needed is installed.${NC}" else - echo -e "${RED}Go version 1.23 not installed. $INSTALLATION_DOCS_URL${NC}" - exit 1 + echo -e "${RED}Go version $go_version_needed not installed. $INSTALLATION_DOCS_URL${NC}" + EXIT_CODE=1 fi # Step 9: Check Foundry installation @@ -142,8 +145,10 @@ if command_exists foundryup; then echo -e "${GREEN}Foundry is installed.${NC}" else echo -e "${RED}Foundry is not installed. $INSTALLATION_DOCS_URL${NC}" - exit 1 + EXIT_CODE=1 fi echo -e "${BLUE}Verification complete.${NC}" -exit 0 + +exit $EXIT_CODE + From bb92595db74dd3d9b4790d36cddde922e8d8074b Mon Sep 17 00:00:00 2001 From: Joshua Colvin Date: Thu, 17 Oct 2024 23:52:01 -0700 Subject: [PATCH 38/38] Update check-build Updated dependencies for MacOS and fixed issues with running on Ubuntu --- {util => scripts}/check-build.sh | 54 +++++++++++--------------------- 1 file changed, 19 insertions(+), 35 deletions(-) rename {util => scripts}/check-build.sh (66%) diff --git a/util/check-build.sh b/scripts/check-build.sh similarity index 66% rename from util/check-build.sh rename to scripts/check-build.sh index ddf540e45c..d654405c49 100755 --- a/util/check-build.sh +++ b/scripts/check-build.sh @@ -27,7 +27,7 @@ echo -e "${BLUE}Checking prerequisites for building Nitro locally...${NC}" if command_exists docker; then echo -e "${GREEN}Docker is installed.${NC}" else - echo -e "${RED}Docker is not installed. $INSTALLATION_DOCS_URL${NC}" + echo -e "${RED}Docker is not installed.${NC}" EXIT_CODE=1 fi @@ -58,51 +58,31 @@ fi if docker images | grep -q "nitro-node"; then echo -e "${GREEN}Nitro Docker image is built.${NC}" else - echo -e "${RED}Nitro Docker image is not built. Build it using: docker build . --tag nitro-node${NC}" - EXIT_CODE=1 + echo -e "${YELLOW}Nitro Docker image is not built. Build it using: docker build . --tag nitro-node${NC}" fi # Step 5: Check prerequisites for building binaries echo -e "${BLUE}Checking prerequisites for building Nitro's binaries...${NC}" if [[ "$OS" == "Linux" ]]; then - prerequisites=(git curl build-essential cmake npm golang clang make gotestsum wasm2wat lld-13 python3 yarn) + prerequisites=(git curl make cmake npm golang clang make gotestsum wasm2wat wasm-ld python3 yarn) else - prerequisites=(git curl make cmake npm go gvm golangci-lint wasm2wat clang gotestsum yarn) + prerequisites=(git curl make cmake npm go golangci-lint wasm2wat clang wasm-ld gotestsum yarn) fi for pkg in "${prerequisites[@]}"; do - if command_exists "$pkg"; then + EXISTS=$(command_exists "$pkg") + [[ "$pkg" == "make" ]] && pkg="build-essential" + [[ "$pkg" == "wasm2wat" ]] && pkg="wabt" + [[ "$pkg" == "clang" ]] && pkg="llvm" + [[ "$pkg" == "wasm-ld" ]] && pkg="lld" + if $EXISTS; then # There is no way to check for wabt / llvm directly, since they install multiple tools # So instead, we check for wasm2wat and clang, which are part of wabt and llvm respectively # and if they are installed, we assume wabt / llvm is installed else we ask the user to install wabt / llvm - [[ "$pkg" == "wasm2wat" ]] && pkg="wabt" - [[ "$pkg" == "clang" ]] && pkg="llvm" - - # Check for specific symbolic links related to wasm-ld on Linux and macOS - if [[ "$pkg" == "llvm" ]]; then - if [[ "$OS" == "Linux" ]]; then - if [ ! -L /usr/local/bin/wasm-ld ]; then - echo -e "${YELLOW}Creating symbolic link for wasm-ld on Linux.${NC}" - sudo ln -s /usr/bin/wasm-ld-13 /usr/local/bin/wasm-ld - else - echo -e "${GREEN}Symbolic link for wasm-ld on Linux is already present.${NC}" - fi - elif [[ "$OS" == "Darwin" ]]; then - if [ ! -L /usr/local/bin/wasm-ld ]; then - echo -e "${YELLOW}Creating symbolic link for wasm-ld on macOS.${NC}" - sudo mkdir -p /usr/local/bin - sudo ln -s /opt/homebrew/opt/llvm/bin/wasm-ld /usr/local/bin/wasm-ld - else - echo -e "${GREEN}Symbolic link for wasm-ld on macOS is already present.${NC}" - fi - fi - fi echo -e "${GREEN}$pkg is installed.${NC}" else - [[ "$pkg" == "wasm2wat" ]] && pkg="wabt" - [[ "$pkg" == "clang" ]] && pkg="llvm" - echo -e "${RED}$pkg is not installed. Please install $pkg. $INSTALLATION_DOCS_URL${NC}" + echo -e "${RED}$pkg is not installed. Please install $pkg.${NC}" EXIT_CODE=1 fi done @@ -111,7 +91,7 @@ done if command_exists node && node -v | grep -q "v18"; then echo -e "${GREEN}Node.js version 18 is installed.${NC}" else - echo -e "${RED}Node.js version 18 not installed. $INSTALLATION_DOCS_URL${NC}" + echo -e "${RED}Node.js version 18 not installed.${NC}" EXIT_CODE=1 fi @@ -119,7 +99,7 @@ fi if command_exists rustc && rustc --version | grep -q "1.80.1"; then echo -e "${GREEN}Rust version 1.80.1 is installed.${NC}" else - echo -e "${RED}Rust version 1.80.1 not installed. $INSTALLATION_DOCS_URL${NC}" + echo -e "${RED}Rust version 1.80.1 not installed.${NC}" EXIT_CODE=1 fi @@ -136,7 +116,7 @@ go_version_needed=$(grep "^go " go.mod | awk '{print $2}') if command_exists go && go version | grep -q "$go_version_needed"; then echo -e "${GREEN}Go version $go_version_needed is installed.${NC}" else - echo -e "${RED}Go version $go_version_needed not installed. $INSTALLATION_DOCS_URL${NC}" + echo -e "${RED}Go version $go_version_needed not installed.${NC}" EXIT_CODE=1 fi @@ -144,11 +124,15 @@ fi if command_exists foundryup; then echo -e "${GREEN}Foundry is installed.${NC}" else - echo -e "${RED}Foundry is not installed. $INSTALLATION_DOCS_URL${NC}" + echo -e "${RED}Foundry is not installed.${NC}" EXIT_CODE=1 fi echo -e "${BLUE}Verification complete.${NC}" +if [ $EXIT_CODE != 0 ]; then + echo -e "${RED}One or more dependencies missing. $INSTALLATION_DOCS_URL${NC}" +fi + exit $EXIT_CODE