Skip to content

Commit

Permalink
Merge branch 'master' into koanf-cont-cont
Browse files Browse the repository at this point in the history
  • Loading branch information
anodar authored Sep 14, 2023
2 parents 4dab051 + 89d4a99 commit 4d72f53
Show file tree
Hide file tree
Showing 10 changed files with 66 additions and 42 deletions.
40 changes: 24 additions & 16 deletions arbnode/batch_poster.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"fmt"
"math"
"math/big"
"strings"
"sync/atomic"
"time"

Expand Down Expand Up @@ -262,12 +263,13 @@ func NewBatchPoster(dataPosterDB ethdb.Database, l1Reader *headerreader.HeaderRe
// contain reverted batch_poster transaction.
// It returns true if it finds batch posting needs to halt, which is true if a batch reverts
// unless the data poster is configured with noop storage which can tolerate reverts.
func (b *BatchPoster) checkReverts(ctx context.Context, from, to int64) (bool, error) {
if from > to {
return false, fmt.Errorf("wrong range, from: %d is more to: %d", from, to)
// From must be a pointer to the starting block, which is updated after each block is checked for reverts
func (b *BatchPoster) checkReverts(ctx context.Context, from *int64, to int64) (bool, error) {
if *from > to {
return false, fmt.Errorf("wrong range, from: %d > to: %d", from, to)
}
for idx := from; idx <= to; idx++ {
number := big.NewInt(idx)
for ; *from <= to; *from++ {
number := big.NewInt(*from)
block, err := b.l1Reader.Client().BlockByNumber(ctx, number)
if err != nil {
return false, fmt.Errorf("getting block: %v by number: %w", number, err)
Expand All @@ -277,7 +279,7 @@ func (b *BatchPoster) checkReverts(ctx context.Context, from, to int64) (bool, e
if err != nil {
return false, fmt.Errorf("getting sender of transaction tx: %v, %w", tx.Hash(), err)
}
if bytes.Equal(from.Bytes(), b.dataPoster.Sender().Bytes()) {
if from == b.dataPoster.Sender() {
r, err := b.l1Reader.Client().TransactionReceipt(ctx, tx.Hash())
if err != nil {
return false, fmt.Errorf("getting a receipt for transaction: %v, %w", tx.Hash(), err)
Expand All @@ -303,7 +305,7 @@ func (b *BatchPoster) pollForReverts(ctx context.Context) {
headerCh, unsubscribe := b.l1Reader.Subscribe(false)
defer unsubscribe()

last := int64(0) // number of last seen block
nextToCheck := int64(0) // the first unchecked block
for {
// Poll until:
// - L1 headers reader channel is closed, or
Expand All @@ -312,31 +314,37 @@ func (b *BatchPoster) pollForReverts(ctx context.Context) {
select {
case h, ok := <-headerCh:
if !ok {
log.Info("L1 headers channel has been closed")
log.Info("L1 headers channel checking for batch poster reverts has been closed")
return
}
// If this is the first block header, set last seen as number-1.
// We may see same block number again if there is L1 reorg, in that
// case we check the block again.
if last == 0 || last == h.Number.Int64() {
last = h.Number.Int64() - 1
if nextToCheck == 0 || nextToCheck == h.Number.Int64() {
nextToCheck = h.Number.Int64()
}
if h.Number.Int64()-last > 100 {
log.Warn("Large gap between last seen and current block number, skipping check for reverts", "last", last, "current", h.Number)
last = h.Number.Int64()
if h.Number.Int64()-nextToCheck > 100 {
log.Warn("Large gap between last seen and current block number, skipping check for reverts", "last", nextToCheck, "current", h.Number)
nextToCheck = h.Number.Int64()
continue
}

reverted, err := b.checkReverts(ctx, last+1, h.Number.Int64())
reverted, err := b.checkReverts(ctx, &nextToCheck, h.Number.Int64())
if err != nil {
log.Error("Checking batch reverts", "error", err)
logLevel := log.Error
if strings.Contains(err.Error(), "not found") {
// Just parent chain node inconsistency
// One node sent us a block, but another didn't have it
// We'll try to check this block again next loop
logLevel = log.Debug
}
logLevel("Error checking batch reverts", "err", err)
continue
}
if reverted {
b.batchReverted.Store(true)
return
}
last = h.Number.Int64()
case <-ctx.Done():
return
}
Expand Down
10 changes: 5 additions & 5 deletions arbnode/execution/tx_pre_checker.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,11 +145,6 @@ func PreCheckTx(bc *core.BlockChain, chainConfig *params.ChainConfig, header *ty
if config.Strictness < TxPreCheckerStrictnessLikelyCompatible {
return nil
}
balance := statedb.GetBalance(sender)
cost := tx.Cost()
if arbmath.BigLessThan(balance, cost) {
return fmt.Errorf("%w: address %v have %v want %v", core.ErrInsufficientFunds, sender, balance, cost)
}
if options != nil {
if err := options.Check(extraInfo.L1BlockNumber, header.Time, statedb); err != nil {
conditionalTxRejectedByTxPreCheckerCurrentStateCounter.Inc(1)
Expand Down Expand Up @@ -185,6 +180,11 @@ func PreCheckTx(bc *core.BlockChain, chainConfig *params.ChainConfig, header *ty
conditionalTxAcceptedByTxPreCheckerOldStateCounter.Inc(1)
}
}
balance := statedb.GetBalance(sender)
cost := tx.Cost()
if arbmath.BigLessThan(balance, cost) {
return fmt.Errorf("%w: address %v have %v want %v", core.ErrInsufficientFunds, sender, balance, cost)
}
if config.Strictness >= TxPreCheckerStrictnessFullValidation && tx.Nonce() > stateNonce {
return MakeNonceError(sender, tx.Nonce(), stateNonce)
}
Expand Down
4 changes: 3 additions & 1 deletion arbnode/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ import (
"github.com/offchainlabs/nitro/solgen/go/bridgegen"
"github.com/offchainlabs/nitro/solgen/go/challengegen"
"github.com/offchainlabs/nitro/solgen/go/ospgen"
"github.com/offchainlabs/nitro/solgen/go/precompilesgen"
"github.com/offchainlabs/nitro/solgen/go/rollupgen"
"github.com/offchainlabs/nitro/staker"
"github.com/offchainlabs/nitro/util/contracts"
Expand Down Expand Up @@ -604,7 +605,8 @@ func createNodeImpl(

var l1Reader *headerreader.HeaderReader
if config.ParentChainReader.Enable {
l1Reader, err = headerreader.New(ctx, l1client, func() *headerreader.Config { return &configFetcher.Get().ParentChainReader })
arbSys, _ := precompilesgen.NewArbSys(types.ArbSysAddress, l1client)
l1Reader, err = headerreader.New(ctx, l1client, func() *headerreader.Config { return &configFetcher.Get().ParentChainReader }, arbSys)
if err != nil {
return nil, err
}
Expand Down
5 changes: 4 additions & 1 deletion cmd/daserver/daserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,15 @@ import (
flag "github.com/spf13/pflag"

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/log"
"github.com/ethereum/go-ethereum/metrics"
"github.com/ethereum/go-ethereum/metrics/exp"

"github.com/offchainlabs/nitro/cmd/genericconf"
"github.com/offchainlabs/nitro/cmd/util/confighelpers"
"github.com/offchainlabs/nitro/das"
"github.com/offchainlabs/nitro/solgen/go/precompilesgen"
"github.com/offchainlabs/nitro/util/headerreader"
)

Expand Down Expand Up @@ -196,7 +198,8 @@ func startup() error {
if err != nil {
return err
}
l1Reader, err = headerreader.New(ctx, l1Client, func() *headerreader.Config { return &headerreader.DefaultConfig }) // TODO: config
arbSys, _ := precompilesgen.NewArbSys(types.ArbSysAddress, l1Client)
l1Reader, err = headerreader.New(ctx, l1Client, func() *headerreader.Config { return &headerreader.DefaultConfig }, arbSys) // TODO: config
if err != nil {
return err
}
Expand Down
5 changes: 4 additions & 1 deletion cmd/deploy/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,12 @@ import (

"github.com/offchainlabs/nitro/cmd/chaininfo"
"github.com/offchainlabs/nitro/cmd/genericconf"
"github.com/offchainlabs/nitro/solgen/go/precompilesgen"
"github.com/offchainlabs/nitro/util/headerreader"
"github.com/offchainlabs/nitro/validator/server_common"

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/ethclient"
"github.com/ethereum/go-ethereum/log"
"github.com/ethereum/go-ethereum/params"
Expand Down Expand Up @@ -127,7 +129,8 @@ func main() {
panic(fmt.Errorf("failed to deserialize chain config: %w", err))
}

l1Reader, err := headerreader.New(ctx, l1client, func() *headerreader.Config { return &headerReaderConfig })
arbSys, _ := precompilesgen.NewArbSys(types.ArbSysAddress, l1client)
l1Reader, err := headerreader.New(ctx, l1client, func() *headerreader.Config { return &headerReaderConfig }, arbSys)
if err != nil {
panic(fmt.Errorf("failed to create header reader: %w", err))
}
Expand Down
5 changes: 4 additions & 1 deletion cmd/nitro/nitro.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
"github.com/ethereum/go-ethereum/accounts/keystore"
"github.com/ethereum/go-ethereum/arbitrum"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/crypto"
_ "github.com/ethereum/go-ethereum/eth/tracers/js"
_ "github.com/ethereum/go-ethereum/eth/tracers/native"
Expand All @@ -48,6 +49,7 @@ import (
"github.com/offchainlabs/nitro/cmd/util"
"github.com/offchainlabs/nitro/cmd/util/confighelpers"
_ "github.com/offchainlabs/nitro/nodeInterface"
"github.com/offchainlabs/nitro/solgen/go/precompilesgen"
"github.com/offchainlabs/nitro/staker"
"github.com/offchainlabs/nitro/util/colors"
"github.com/offchainlabs/nitro/util/headerreader"
Expand Down Expand Up @@ -354,7 +356,8 @@ func mainImpl() int {
flag.Usage()
log.Crit("--node.validator.only-create-wallet-contract requires --node.validator.use-smart-contract-wallet")
}
l1Reader, err := headerreader.New(ctx, l1Client, func() *headerreader.Config { return &liveNodeConfig.Get().Node.ParentChainReader })
arbSys, _ := precompilesgen.NewArbSys(types.ArbSysAddress, l1Client)
l1Reader, err := headerreader.New(ctx, l1Client, func() *headerreader.Config { return &liveNodeConfig.Get().Node.ParentChainReader }, arbSys)
if err != nil {
log.Crit("failed to get L1 headerreader", "error", err)
}
Expand Down
8 changes: 4 additions & 4 deletions staker/staker.go
Original file line number Diff line number Diff line change
Expand Up @@ -552,11 +552,11 @@ func (s *Staker) confirmDataPosterIsReady(ctx context.Context) error {
}

func (s *Staker) Act(ctx context.Context) (*types.Transaction, error) {
err := s.confirmDataPosterIsReady(ctx)
if err != nil {
return nil, err
}
if s.config.strategy != WatchtowerStrategy {
err := s.confirmDataPosterIsReady(ctx)
if err != nil {
return nil, err
}
whitelisted, err := s.IsWhitelisted(ctx)
if err != nil {
return nil, fmt.Errorf("error checking if whitelisted: %w", err)
Expand Down
3 changes: 2 additions & 1 deletion system_tests/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -476,7 +476,8 @@ func DeployOnTestL1(
serializedChainConfig, err := json.Marshal(chainConfig)
Require(t, err)

l1Reader, err := headerreader.New(ctx, l1client, func() *headerreader.Config { return &headerreader.TestConfig })
arbSys, _ := precompilesgen.NewArbSys(types.ArbSysAddress, l1client)
l1Reader, err := headerreader.New(ctx, l1client, func() *headerreader.Config { return &headerreader.TestConfig }, arbSys)
Require(t, err)
l1Reader.Start(ctx)
defer l1Reader.StopAndWait()
Expand Down
4 changes: 3 additions & 1 deletion system_tests/das_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
"github.com/offchainlabs/nitro/cmd/genericconf"
"github.com/offchainlabs/nitro/das"
"github.com/offchainlabs/nitro/solgen/go/bridgegen"
"github.com/offchainlabs/nitro/solgen/go/precompilesgen"
"github.com/offchainlabs/nitro/util/headerreader"
"github.com/offchainlabs/nitro/util/signature"
)
Expand Down Expand Up @@ -233,7 +234,8 @@ func TestDASComplexConfigAndRestMirror(t *testing.T) {
chainConfig := params.ArbitrumDevTestDASChainConfig()
l1info, l1client, _, l1stack := createTestL1BlockChain(t, nil)
defer requireClose(t, l1stack)
l1Reader, err := headerreader.New(ctx, l1client, func() *headerreader.Config { return &headerreader.TestConfig })
arbSys, _ := precompilesgen.NewArbSys(types.ArbSysAddress, l1client)
l1Reader, err := headerreader.New(ctx, l1client, func() *headerreader.Config { return &headerreader.TestConfig }, arbSys)
Require(t, err)
l1Reader.Start(ctx)
defer l1Reader.StopAndWait()
Expand Down
24 changes: 13 additions & 11 deletions util/headerreader/header_reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,20 @@ import (
"github.com/ethereum/go-ethereum/log"
"github.com/ethereum/go-ethereum/rpc"
"github.com/offchainlabs/nitro/arbutil"
"github.com/offchainlabs/nitro/solgen/go/precompilesgen"
"github.com/offchainlabs/nitro/util/stopwaiter"
flag "github.com/spf13/pflag"
)

type ArbSysInterface interface {
ArbBlockNumber(*bind.CallOpts) (*big.Int, error)
}

type HeaderReader struct {
stopwaiter.StopWaiter
config ConfigFetcher
client arbutil.L1Interface
isParentChainArbitrum bool
arbSys *precompilesgen.ArbSys
arbSys ArbSysInterface

chanMutex sync.RWMutex
// All fields below require the chanMutex
Expand Down Expand Up @@ -91,19 +94,18 @@ var TestConfig = Config{
UseFinalityData: false,
}

func New(ctx context.Context, client arbutil.L1Interface, config ConfigFetcher) (*HeaderReader, error) {
func New(ctx context.Context, client arbutil.L1Interface, config ConfigFetcher, arbSysPrecompile ArbSysInterface) (*HeaderReader, error) {
isParentChainArbitrum := false
var arbSys *precompilesgen.ArbSys
codeAt, err := client.CodeAt(ctx, types.ArbSysAddress, nil)
if err != nil {
return nil, err
}
if len(codeAt) != 0 {
isParentChainArbitrum = true
arbSys, err = precompilesgen.NewArbSys(types.ArbSysAddress, client)
var arbSys ArbSysInterface
if arbSysPrecompile != nil {
codeAt, err := client.CodeAt(ctx, types.ArbSysAddress, nil)
if err != nil {
return nil, err
}
if len(codeAt) != 0 {
isParentChainArbitrum = true
arbSys = arbSysPrecompile
}
}
return &HeaderReader{
client: client,
Expand Down

0 comments on commit 4d72f53

Please sign in to comment.