Skip to content

Commit 124f216

Browse files
authored
Merge pull request #640 from NilFoundation/split-rollup-contract-methods
refactor(sc): split contract CommitBatch and UpdateState calls
2 parents 0e09f5e + 00c0fb1 commit 124f216

28 files changed

Lines changed: 1409 additions & 1493 deletions

nil/cmd/sync_committee/main.go

Lines changed: 13 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,17 +115,14 @@ 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-
}
118+
ctx := context.Background()
124119

125-
service, err := core.New(cfg.Config, database, ethClient)
120+
service, err := core.New(ctx, cfg.Config, database)
126121
if err != nil {
127122
return fmt.Errorf("can't create sync committee service: %w", err)
128123
}
129124

130-
err = service.Run(context.Background())
125+
err = service.Run(ctx)
131126
if err != nil {
132127
return fmt.Errorf("service exited with error: %w", err)
133128
}
@@ -142,13 +137,3 @@ func openDB(dbPath string) (db.DB, error) {
142137
}
143138
return badger, nil
144139
}
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: 43 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package core
22

33
import (
4+
"bytes"
45
"context"
56
"errors"
67
"fmt"
@@ -10,14 +11,16 @@ import (
1011
"github.com/NilFoundation/nil/nil/common/concurrent"
1112
"github.com/NilFoundation/nil/nil/common/logging"
1213
coreTypes "github.com/NilFoundation/nil/nil/internal/types"
13-
"github.com/NilFoundation/nil/nil/services/synccommittee/core/batches"
1414
"github.com/NilFoundation/nil/nil/services/synccommittee/core/batches/blob"
15+
"github.com/NilFoundation/nil/nil/services/synccommittee/core/batches/encode"
1516
v1 "github.com/NilFoundation/nil/nil/services/synccommittee/core/batches/encode/v1"
1617
"github.com/NilFoundation/nil/nil/services/synccommittee/core/reset"
1718
"github.com/NilFoundation/nil/nil/services/synccommittee/internal/metrics"
19+
"github.com/NilFoundation/nil/nil/services/synccommittee/internal/rollupcontract"
1820
"github.com/NilFoundation/nil/nil/services/synccommittee/internal/srv"
1921
"github.com/NilFoundation/nil/nil/services/synccommittee/internal/storage"
2022
"github.com/NilFoundation/nil/nil/services/synccommittee/internal/types"
23+
ethtypes "github.com/ethereum/go-ethereum/core/types"
2124
"github.com/jonboulle/clockwork"
2225
)
2326

@@ -41,11 +44,13 @@ type AggregatorBlockStorage interface {
4144

4245
type AggregatorConfig struct {
4346
RpcPollingInterval time.Duration
47+
MaxBlobsInTx uint
4448
}
4549

4650
func NewAggregatorConfig(rpcPollingInterval time.Duration) AggregatorConfig {
4751
return AggregatorConfig{
4852
RpcPollingInterval: rpcPollingInterval,
53+
MaxBlobsInTx: 6,
4954
}
5055
}
5156

@@ -58,10 +63,13 @@ type aggregator struct {
5863
blockStorage AggregatorBlockStorage
5964
taskStorage AggregatorTaskStorage
6065
subgraphFetcher *subgraphFetcher
61-
batchCommitter batches.BatchCommitter
66+
batchEncoder encode.BatchEncoder
67+
blobBuilder blob.Builder
68+
rollupContract rollupcontract.Wrapper
6269
resetter *reset.StateResetter
6370
clock clockwork.Clock
6471
metrics AggregatorMetrics
72+
config AggregatorConfig
6573
workerAction *concurrent.Suspendable
6674
logger logging.Logger
6775
}
@@ -71,6 +79,7 @@ func NewAggregator(
7179
blockStorage AggregatorBlockStorage,
7280
taskStorage AggregatorTaskStorage,
7381
resetter *reset.StateResetter,
82+
rollupContractWrapper rollupcontract.Wrapper,
7483
clock clockwork.Clock,
7584
logger logging.Logger,
7685
metrics AggregatorMetrics,
@@ -81,16 +90,13 @@ func NewAggregator(
8190
blockStorage: blockStorage,
8291
taskStorage: taskStorage,
8392
subgraphFetcher: newSubgraphFetcher(rpcClient, logger),
84-
batchCommitter: batches.NewBatchCommitter(
85-
v1.NewEncoder(logger),
86-
blob.NewBuilder(),
87-
nil, // TODO
88-
logger,
89-
batches.DefaultCommitOptions(),
90-
),
91-
resetter: resetter,
92-
clock: clock,
93-
metrics: metrics,
93+
batchEncoder: v1.NewEncoder(logger),
94+
blobBuilder: blob.NewBuilder(),
95+
rollupContract: rollupContractWrapper,
96+
resetter: resetter,
97+
clock: clock,
98+
metrics: metrics,
99+
config: config,
94100
}
95101

96102
agg.workerAction = concurrent.NewSuspendable(agg.runIteration, config.RpcPollingInterval)
@@ -357,13 +363,18 @@ func (agg *aggregator) handleBlockBatch(ctx context.Context, batch *types.BlockB
357363
return err
358364
}
359365

366+
sidecar, dataProofs, err := agg.prepareForBatchCommit(ctx, batch)
367+
if err != nil {
368+
return err
369+
}
370+
batch.SetDataProofs(dataProofs)
371+
360372
if err := agg.blockStorage.SetBlockBatch(ctx, batch); err != nil {
361373
return fmt.Errorf("error storing block batch, latestMainHash=%s: %w", batch.LatestMainBlock().Hash, err)
362374
}
363375

364-
prunedBatch := types.NewPrunedBatch(batch)
365-
if err := agg.batchCommitter.Commit(ctx, prunedBatch); err != nil {
366-
return err
376+
if err := agg.rollupContract.CommitBatch(ctx, sidecar, batch.Id.String()); err != nil {
377+
return fmt.Errorf("error committing batch, latestMainHash=%s: %w", batch.LatestMainBlock().Hash, err)
367378
}
368379

369380
if err := agg.createProofTasks(ctx, batch); err != nil {
@@ -392,3 +403,20 @@ func (agg *aggregator) createProofTasks(ctx context.Context, batch *types.BlockB
392403

393404
return nil
394405
}
406+
407+
func (agg *aggregator) prepareForBatchCommit(
408+
ctx context.Context, batch *types.BlockBatch,
409+
) (*ethtypes.BlobTxSidecar, types.DataProofs, error) {
410+
var binTransactions bytes.Buffer
411+
if err := agg.batchEncoder.Encode(types.NewPrunedBatch(batch), &binTransactions); err != nil {
412+
return nil, nil, err
413+
}
414+
agg.logger.Debug().Int("compressed_batch_len", binTransactions.Len()).Msg("encoded transaction")
415+
416+
blobs, err := agg.blobBuilder.MakeBlobs(&binTransactions, agg.config.MaxBlobsInTx)
417+
if err != nil {
418+
return nil, nil, err
419+
}
420+
421+
return agg.rollupContract.PrepareBlobs(ctx, blobs)
422+
}

nil/services/synccommittee/core/aggregator_test.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"github.com/NilFoundation/nil/nil/internal/db"
1010
"github.com/NilFoundation/nil/nil/services/synccommittee/core/reset"
1111
"github.com/NilFoundation/nil/nil/services/synccommittee/internal/metrics"
12+
"github.com/NilFoundation/nil/nil/services/synccommittee/internal/rollupcontract"
1213
"github.com/NilFoundation/nil/nil/services/synccommittee/internal/storage"
1314
"github.com/NilFoundation/nil/nil/services/synccommittee/internal/testaide"
1415
scTypes "github.com/NilFoundation/nil/nil/services/synccommittee/internal/types"
@@ -63,11 +64,18 @@ func (s *AggregatorTestSuite) newTestAggregator(
6364
stateResetter := reset.NewStateResetter(logger, s.blockStorage)
6465
clock := clockwork.NewRealClock()
6566

67+
contractWrapperConfig := rollupcontract.WrapperConfig{
68+
DisableL1: true,
69+
}
70+
contractWrapper, err := rollupcontract.NewWrapper(s.ctx, contractWrapperConfig, logger)
71+
s.Require().NoError(err)
72+
6673
return NewAggregator(
6774
s.rpcClientMock,
6875
blockStorage,
6976
s.taskStorage,
7077
stateResetter,
78+
contractWrapper,
7179
clock,
7280
logger,
7381
s.metrics,

nil/services/synccommittee/core/batches/blob/builder.go

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,19 @@ import (
1212

1313
const blobSize = len(kzg4844.Blob{})
1414

15-
type builder struct{}
15+
type Builder interface {
16+
MakeBlobs(in io.Reader, limit uint) ([]kzg4844.Blob, error)
17+
}
18+
19+
type builderImpl struct{}
20+
21+
var _ Builder = (*builderImpl)(nil)
1622

17-
func NewBuilder() *builder {
18-
return &builder{}
23+
func NewBuilder() *builderImpl {
24+
return &builderImpl{}
1925
}
2026

21-
func (bb *builder) MakeBlobs(rd io.Reader, blobLimit int) ([]kzg4844.Blob, error) {
27+
func (bb *builderImpl) MakeBlobs(rd io.Reader, blobLimit uint) ([]kzg4844.Blob, error) {
2228
const blobSize = len(kzg4844.Blob{})
2329

2430
var blobs []kzg4844.Blob
@@ -39,7 +45,7 @@ func (bb *builder) MakeBlobs(rd io.Reader, blobLimit int) ([]kzg4844.Blob, error
3945
}
4046

4147
align := 0
42-
for i := 0; !eof && i < blobLimit; i++ {
48+
for i := uint(0); !eof && i < blobLimit; i++ {
4349
blobBuf.Reset()
4450

4551
blobWriter := bitio.NewWriter(&blobBuf)
@@ -94,7 +100,7 @@ func (bb *builder) MakeBlobs(rd io.Reader, blobLimit int) ([]kzg4844.Blob, error
94100
if !eof {
95101
return nil, fmt.Errorf(
96102
"provided batch does not fit into %d blobs (%d bytes) [written = %d bits] [align = %d bits]",
97-
blobLimit, blobSize*blobLimit, writtenBits, align)
103+
blobLimit, uint(blobSize)*blobLimit, writtenBits, align)
98104
}
99105
return blobs, nil
100106
}

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

Lines changed: 0 additions & 81 deletions
This file was deleted.
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package encode
2+
3+
import (
4+
"io"
5+
6+
"github.com/NilFoundation/nil/nil/services/synccommittee/internal/types"
7+
)
8+
9+
type BatchEncoder interface {
10+
Encode(in *types.PrunedBatch, out io.Writer) error
11+
}

nil/services/synccommittee/core/batches/encode/v1/encoder.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ type batchEncoder struct {
2121
logger logging.Logger
2222
}
2323

24+
var _ encode.BatchEncoder = (*batchEncoder)(nil)
25+
2426
func NewEncoder(logger logging.Logger) *batchEncoder {
2527
return &batchEncoder{
2628
compressor: NewZstdCompressor(logger),

0 commit comments

Comments
 (0)