Skip to content

Commit 875feae

Browse files
committed
refactor(sc): split contract CommitBatch and UpdateState calls
- Refactored CommitBatch and UpdateState methods: - CommitBatch now called in the aggregator service just before creating proof tasks. Transaction creation is skipped if a batch with the same ID has already been submitted. - UpdateState is still called in the proposer service after it mentions there is a new state to be proposed. - UpdateState now retrieves the CommitBatch transaction for the specified batch ID from L1 and extracts blob data. - Instead of returning etherium transaction methods wait for transaction for being included into block. - Improved error handling and configuration: - Proposer now handles `ErrBatchAlreadyFinalized` by refreshing the last proved state root from L1 if local tasks state is out of sync. - Introduced a dummy structure mode for the EthClient when the DisableL1 config option is set, which logs calls without returning errors. - Optimized contract and client interactions: - Shared the contract wrapper between Aggregator and Proposer to streamline interactions. - Encapsulated retry logic within the EthClient, which now acts as a wrapper around go-ethereum/ethclient. - Extended testing and utilities: - Migrated contract mocker to the testaide package for broader reuse in tests. - Added new tests for both the proposer and the wrapper to ensure robustness.
1 parent f87f618 commit 875feae

16 files changed

Lines changed: 1199 additions & 554 deletions

File tree

nil/cmd/sync_committee/main.go

Lines changed: 11 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,12 @@ import (
44
"context"
55
"fmt"
66
"os"
7-
"time"
87

98
"github.com/NilFoundation/nil/nil/common/check"
109
"github.com/NilFoundation/nil/nil/common/logging"
1110
"github.com/NilFoundation/nil/nil/internal/db"
1211
"github.com/NilFoundation/nil/nil/internal/profiling"
1312
"github.com/NilFoundation/nil/nil/services/synccommittee/core"
14-
"github.com/ethereum/go-ethereum/ethclient"
1513
"github.com/spf13/cobra"
1614
)
1715

@@ -71,29 +69,29 @@ func addFlags(cmd *cobra.Command, cfg *cmdConfig) {
7169
"sync_committee.db",
7270
"path to database")
7371
cmd.Flags().StringVar(
74-
&cfg.ProposerParams.Endpoint,
72+
&cfg.ContractWrapperConfig.Endpoint,
7573
"l1-endpoint",
76-
cfg.ProposerParams.Endpoint,
74+
cfg.ContractWrapperConfig.Endpoint,
7775
"L1 endpoint")
7876
cmd.Flags().StringVar(
79-
&cfg.ProposerParams.PrivateKey,
77+
&cfg.ContractWrapperConfig.PrivateKeyHex,
8078
"l1-private-key",
81-
cfg.ProposerParams.PrivateKey,
79+
cfg.ContractWrapperConfig.PrivateKeyHex,
8280
"L1 account private key")
8381
cmd.Flags().StringVar(
84-
&cfg.ProposerParams.ContractAddress,
82+
&cfg.ContractWrapperConfig.ContractAddressHex,
8583
"l1-contract-address",
86-
cfg.ProposerParams.ContractAddress,
84+
cfg.ContractWrapperConfig.ContractAddressHex,
8785
"L1 update state contract address")
8886
cmd.Flags().DurationVar(
89-
&cfg.ProposerParams.EthClientTimeout,
87+
&cfg.ContractWrapperConfig.RequestsTimeout,
9088
"l1-client-timeout",
91-
cfg.ProposerParams.EthClientTimeout,
89+
cfg.ContractWrapperConfig.RequestsTimeout,
9290
"L1 client timeout")
9391
cmd.Flags().BoolVar(
94-
&cfg.ProposerParams.DisableL1,
92+
&cfg.ContractWrapperConfig.DisableL1,
9593
"disable-l1",
96-
cfg.ProposerParams.DisableL1,
94+
cfg.ContractWrapperConfig.DisableL1,
9795
"Disable send trancations to L1")
9896
logLevel := cmd.Flags().String(
9997
"log-level",
@@ -117,12 +115,7 @@ func run(cfg *cmdConfig) error {
117115
}
118116
defer database.Close()
119117

120-
ethClient, err := connectToEthClient(cfg.ProposerParams.Endpoint, cfg.ProposerParams.EthClientTimeout)
121-
if err != nil {
122-
return err
123-
}
124-
125-
service, err := core.New(cfg.Config, database, ethClient)
118+
service, err := core.New(cfg.Config, database)
126119
if err != nil {
127120
return fmt.Errorf("can't create sync committee service: %w", err)
128121
}
@@ -142,13 +135,3 @@ func openDB(dbPath string) (db.DB, error) {
142135
}
143136
return badger, nil
144137
}
145-
146-
func connectToEthClient(url string, timeout time.Duration) (*ethclient.Client, error) {
147-
ctx, cancel := context.WithTimeout(context.Background(), timeout)
148-
defer cancel()
149-
ethClient, err := ethclient.DialContext(ctx, url)
150-
if err != nil {
151-
return nil, fmt.Errorf("connecting to ETH RPC node: %w", err)
152-
}
153-
return ethClient, nil
154-
}

nil/services/synccommittee/core/aggregator.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import (
1717
v1 "github.com/NilFoundation/nil/nil/services/synccommittee/core/batches/encode/v1"
1818
"github.com/NilFoundation/nil/nil/services/synccommittee/core/reset"
1919
"github.com/NilFoundation/nil/nil/services/synccommittee/internal/metrics"
20+
"github.com/NilFoundation/nil/nil/services/synccommittee/internal/rollupcontract"
2021
"github.com/NilFoundation/nil/nil/services/synccommittee/internal/srv"
2122
"github.com/NilFoundation/nil/nil/services/synccommittee/internal/storage"
2223
"github.com/NilFoundation/nil/nil/services/synccommittee/internal/types"
@@ -72,6 +73,7 @@ func NewAggregator(
7273
blockStorage AggregatorBlockStorage,
7374
taskStorage AggregatorTaskStorage,
7475
resetter *reset.StateResetter,
76+
rollupContractWrapper rollupcontract.Wrapper,
7577
clock clockwork.Clock,
7678
logger logging.Logger,
7779
metrics AggregatorMetrics,
@@ -84,7 +86,7 @@ func NewAggregator(
8486
batchCommitter: batches.NewBatchCommitter(
8587
v1.NewEncoder(logger),
8688
blob.NewBuilder(),
87-
nil, // TODO
89+
rollupContractWrapper,
8890
logger,
8991
batches.DefaultCommitOptions(),
9092
),

nil/services/synccommittee/core/aggregator_test.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"github.com/NilFoundation/nil/nil/services/rpc/jsonrpc"
1414
"github.com/NilFoundation/nil/nil/services/synccommittee/core/reset"
1515
"github.com/NilFoundation/nil/nil/services/synccommittee/internal/metrics"
16+
"github.com/NilFoundation/nil/nil/services/synccommittee/internal/rollupcontract"
1617
"github.com/NilFoundation/nil/nil/services/synccommittee/internal/storage"
1718
"github.com/NilFoundation/nil/nil/services/synccommittee/internal/testaide"
1819
scTypes "github.com/NilFoundation/nil/nil/services/synccommittee/internal/types"
@@ -67,11 +68,18 @@ func (s *AggregatorTestSuite) newTestAggregator(
6768
stateResetter := reset.NewStateResetter(logger, s.blockStorage)
6869
clock := clockwork.NewRealClock()
6970

71+
contractWrapperConfig := rollupcontract.WrapperConfig{
72+
DisableL1: true,
73+
}
74+
contractWrapper, err := rollupcontract.NewWrapper(s.ctx, contractWrapperConfig, logger)
75+
s.Require().NoError(err)
76+
7077
return NewAggregator(
7178
s.rpcClientMock,
7279
blockStorage,
7380
s.taskStorage,
7481
stateResetter,
82+
contractWrapper,
7583
clock,
7684
logger,
7785
s.metrics,

nil/services/synccommittee/core/batches/commiter.go

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,13 @@ package batches
33
import (
44
"bytes"
55
"context"
6+
"errors"
7+
"fmt"
68
"io"
79

810
"github.com/NilFoundation/nil/nil/common/logging"
11+
"github.com/NilFoundation/nil/nil/services/synccommittee/internal/rollupcontract"
912
"github.com/NilFoundation/nil/nil/services/synccommittee/internal/types"
10-
ethtypes "github.com/ethereum/go-ethereum/core/types"
1113
"github.com/ethereum/go-ethereum/crypto/kzg4844"
1214
)
1315

@@ -24,7 +26,7 @@ type blobBuilder interface {
2426
}
2527

2628
type ethCommitter interface {
27-
CommitBatch(ctx context.Context, blobs []kzg4844.Blob, batchIndex string) (*ethtypes.Transaction, error)
29+
CommitBatch(ctx context.Context, blobs []kzg4844.Blob, batchIndex string) error
2830
}
2931

3032
type batchCommitter struct {
@@ -72,10 +74,15 @@ func (bc *batchCommitter) Commit(ctx context.Context, batch *types.PrunedBatch)
7274
if err != nil {
7375
return err
7476
}
75-
bc.logger.Debug().Int("batch_blob_count", len(blobs)).Msg("packed batch blobs")
7677

77-
// TODO add ethCommiter.CommitBatch() call
78-
bc.logger.Info().Int("blob_count", len(blobs)).Msg("committed batch")
78+
if err := bc.ethCommitter.CommitBatch(ctx, blobs, batch.BatchId.String()); err != nil {
79+
if errors.Is(err, rollupcontract.ErrBatchAlreadyCommitted) {
80+
bc.logger.Warn().Msg("batch is already committed, skipping CommitBatch tx")
81+
return nil
82+
} else {
83+
return fmt.Errorf("failed to CommitBatch: %w", err)
84+
}
85+
}
7986

8087
return nil
8188
}

nil/services/synccommittee/core/config.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package core
22

33
import (
44
"github.com/NilFoundation/nil/nil/internal/telemetry"
5+
"github.com/NilFoundation/nil/nil/services/synccommittee/internal/rollupcontract"
56
)
67

78
const (
@@ -12,7 +13,8 @@ type Config struct {
1213
RpcEndpoint string
1314
TaskListenerRpcEndpoint string
1415
AggregatorConfig AggregatorConfig
15-
ProposerParams ProposerParams
16+
ProposerParams ProposerConfig
17+
ContractWrapperConfig rollupcontract.WrapperConfig
1618
Telemetry *telemetry.Config
1719
}
1820

@@ -21,7 +23,8 @@ func NewDefaultConfig() *Config {
2123
RpcEndpoint: "tcp://127.0.0.1:8529",
2224
TaskListenerRpcEndpoint: DefaultTaskRpcEndpoint,
2325
AggregatorConfig: NewDefaultAggregatorConfig(),
24-
ProposerParams: NewDefaultProposerParams(),
26+
ProposerParams: NewDefaultProposerConfig(),
27+
ContractWrapperConfig: rollupcontract.NewDefaultWrapperConfig(),
2528
Telemetry: &telemetry.Config{
2629
ServiceName: "sync_committee",
2730
},

0 commit comments

Comments
 (0)