Skip to content

Commit

Permalink
Merge pull request #1396 from OffchainLabs/fix-log-subscription
Browse files Browse the repository at this point in the history
Fix block hash in receipts in ws log subscription
  • Loading branch information
PlasmaPower authored Dec 21, 2022
2 parents e67291e + 35d6c75 commit ab34773
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 5 deletions.
8 changes: 4 additions & 4 deletions arbos/block_processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -398,7 +398,8 @@ func ProduceBlockAdvanced(
}

binary.BigEndian.PutUint64(header.Nonce[:], delayedMessagesRead)
header.Root = statedb.IntermediateRoot(true)

FinalizeBlock(header, complete, statedb, chainConfig)

// Touch up the block hashes in receipts
tmpBlock := types.NewBlock(header, complete, nil, receipts, trie.NewStackTrie(nil))
Expand All @@ -411,9 +412,6 @@ func ProduceBlockAdvanced(
}
}

FinalizeBlock(header, complete, statedb, chainConfig)
header.Root = statedb.IntermediateRoot(true)

block := types.NewBlock(header, complete, nil, receipts, trie.NewStackTrie(nil))

if len(block.Transactions()) != len(receipts) {
Expand All @@ -434,6 +432,7 @@ func ProduceBlockAdvanced(
return block, receipts, nil
}

// Also sets header.Root
func FinalizeBlock(header *types.Header, txs types.Transactions, statedb *state.StateDB, chainConfig *params.ChainConfig) {
if header != nil {
if header.Number.Uint64() < chainConfig.ArbitrumChainParams.GenesisBlockNum {
Expand Down Expand Up @@ -467,5 +466,6 @@ func FinalizeBlock(header *types.Header, txs types.Transactions, statedb *state.
ArbOSFormatVersion: arbosVersion,
}
arbitrumHeader.UpdateHeaderWithInfo(header)
header.Root = statedb.IntermediateRoot(true)
}
}
1 change: 0 additions & 1 deletion arbos/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ func (e Engine) Prepare(chain consensus.ChainHeaderReader, header *types.Header)

func (e Engine) Finalize(chain consensus.ChainHeaderReader, header *types.Header, state *state.StateDB, txs []*types.Transaction, uncles []*types.Header) {
FinalizeBlock(header, txs, state, chain.Config())
header.Root = state.IntermediateRoot(true)
}

func (e Engine) FinalizeAndAssemble(chain consensus.ChainHeaderReader, header *types.Header, state *state.StateDB, txs []*types.Transaction,
Expand Down
57 changes: 57 additions & 0 deletions system_tests/log_subscription_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// Copyright 2021-2022, Offchain Labs, Inc.
// For license information, see https://github.com/nitro/blob/master/LICENSE

package arbtest

import (
"context"
"reflect"
"testing"
"time"

"github.com/ethereum/go-ethereum"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/offchainlabs/nitro/solgen/go/precompilesgen"
)

func TestLogSubscription(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()

l2info, node, client := CreateTestL2(t, ctx)
defer node.StopAndWait()

auth := l2info.GetDefaultTransactOpts("Owner", ctx)
arbSys, err := precompilesgen.NewArbSys(types.ArbSysAddress, client)
Require(t, err)

logChan := make(chan types.Log, 128)
subscription, err := client.SubscribeFilterLogs(ctx, ethereum.FilterQuery{}, logChan)
Require(t, err)
defer subscription.Unsubscribe()

tx, err := arbSys.WithdrawEth(&auth, common.Address{})
Require(t, err)
receipt, err := EnsureTxSucceeded(ctx, client, tx)
Require(t, err)

if len(receipt.Logs) != 1 {
Fail(t, "Unexpected number of logs", len(receipt.Logs))
}

var receiptLog types.Log = *receipt.Logs[0]
var subscriptionLog types.Log
timer := time.NewTimer(time.Second * 5)
defer timer.Stop()
select {
case <-timer.C:
Fail(t, "Hit timeout waiting for log from subscription")
case subscriptionLog = <-logChan:
}
if !reflect.DeepEqual(receiptLog, subscriptionLog) {
Fail(t, "Receipt log", receiptLog, "is different than subscription log", subscriptionLog)
}
_, err = client.BlockByHash(ctx, subscriptionLog.BlockHash)
Require(t, err)
}

0 comments on commit ab34773

Please sign in to comment.