Skip to content

Commit e93e051

Browse files
fix: use new interfaces
1 parent 9501d1e commit e93e051

22 files changed

+1004
-2734
lines changed

aggsender/aggsender_test.go

Lines changed: 3 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ import (
2626
mocksdb "github.com/agglayer/aggkit/db/compatibility/mocks"
2727
"github.com/agglayer/aggkit/grpc"
2828
"github.com/agglayer/aggkit/log"
29-
treetypes "github.com/agglayer/aggkit/tree/types"
3029
"github.com/agglayer/go_signer/signer"
3130
signertypes "github.com/agglayer/go_signer/signer/types"
3231
"github.com/ethereum/go-ethereum/common"
@@ -196,73 +195,6 @@ func TestExploratoryGenerateCert(t *testing.T) {
196195
require.NoError(t, encoder.Encode(certificate))
197196
}
198197

199-
func TestSendCertificate_NoClaims(t *testing.T) {
200-
privateKey, err := crypto.GenerateKey()
201-
require.NoError(t, err)
202-
203-
ctx := context.Background()
204-
mockStorage := mocks.NewAggSenderStorage(t)
205-
mockL2BridgeQuerier := mocks.NewBridgeQuerier(t)
206-
mockL1Querier := mocks.NewL1InfoTreeDataQuerier(t)
207-
mockAggLayerClient := agglayer.NewAgglayerClientMock(t)
208-
mockEpochNotifier := mocks.NewEpochNotifier(t)
209-
mockLERQuerier := mocks.NewLERQuerier(t)
210-
logger := log.WithFields("aggsender-test", "no claims test")
211-
signer := signer.NewLocalSignFromPrivateKey("ut", log.WithFields("aggsender", 1), privateKey, 0)
212-
aggSender := &AggSender{
213-
log: logger,
214-
storage: mockStorage,
215-
l2OriginNetwork: 1,
216-
aggLayerClient: mockAggLayerClient,
217-
epochNotifier: mockEpochNotifier,
218-
cfg: config.Config{},
219-
flow: flows.NewPPFlow(logger,
220-
flows.NewBaseFlow(logger, mockL2BridgeQuerier, mockStorage,
221-
mockL1Querier, mockLERQuerier, flows.NewBaseFlowConfigDefault()),
222-
mockStorage, mockL1Querier, mockL2BridgeQuerier, signer, true, 0),
223-
rateLimiter: aggkitcommon.NewRateLimit(aggkitcommon.RateLimitConfig{}),
224-
}
225-
226-
mockStorage.EXPECT().GetLastSentCertificateHeader().Return(&aggsendertypes.CertificateHeader{
227-
NewLocalExitRoot: common.HexToHash("0x123"),
228-
Height: 1,
229-
FromBlock: 0,
230-
ToBlock: 10,
231-
Status: agglayertypes.Settled,
232-
}, nil).Once()
233-
mockStorage.EXPECT().SaveLastSentCertificate(mock.Anything, mock.Anything).Return(nil).Once()
234-
mockL2BridgeQuerier.EXPECT().GetLastProcessedBlock(mock.Anything).Return(uint64(50), nil)
235-
mockL2BridgeQuerier.EXPECT().GetBridgesAndClaims(mock.Anything, uint64(11), uint64(50)).Return([]bridgesync.Bridge{
236-
{
237-
BlockNum: 30,
238-
BlockPos: 0,
239-
LeafType: agglayertypes.LeafTypeAsset.Uint8(),
240-
OriginNetwork: 1,
241-
OriginAddress: common.HexToAddress("0x1"),
242-
DestinationNetwork: 2,
243-
DestinationAddress: common.HexToAddress("0x2"),
244-
Amount: big.NewInt(100),
245-
Metadata: []byte("metadata"),
246-
DepositCount: 1,
247-
},
248-
}, []bridgesync.Claim{}, nil).Once()
249-
mockL1Querier.EXPECT().GetLatestFinalizedL1InfoRoot(ctx).Return(&treetypes.Root{}, nil, nil).Once()
250-
mockL2BridgeQuerier.EXPECT().GetExitRootByIndex(mock.Anything, uint32(1)).Return(common.Hash{}, nil).Once()
251-
mockL2BridgeQuerier.EXPECT().OriginNetwork().Return(uint32(1)).Once()
252-
mockAggLayerClient.EXPECT().SendCertificate(mock.Anything, mock.Anything, mock.Anything).Return(common.Hash{}, nil).Once()
253-
mockEpochNotifier.EXPECT().GetEpochStatus().Return(aggsendertypes.EpochStatus{})
254-
signedCertificate, err := aggSender.sendCertificate(ctx)
255-
require.NoError(t, err)
256-
require.NotNil(t, signedCertificate)
257-
require.NotNil(t, signedCertificate.AggchainData)
258-
require.NotNil(t, signedCertificate.ImportedBridgeExits)
259-
require.Len(t, signedCertificate.BridgeExits, 1)
260-
261-
mockStorage.AssertExpectations(t)
262-
mockL2BridgeQuerier.AssertExpectations(t)
263-
mockAggLayerClient.AssertExpectations(t)
264-
}
265-
266198
func TestExtractFromCertificateMetadataToBlock(t *testing.T) {
267199
t.Parallel()
268200

@@ -747,8 +679,9 @@ func newAggsenderTestData(t *testing.T, creationFlags testDataFlags) *aggsenderT
747679
l2BridgeQuerier := mocks.NewBridgeQuerier(t)
748680
agglayerClientMock := agglayer.NewAgglayerClientMock(t)
749681
l1InfoTreeQuerierMock := mocks.NewL1InfoTreeDataQuerier(t)
750-
lerQuerier := mocks.NewLERQuerier(t)
751682
epochNotifierMock := mocks.NewEpochNotifier(t)
683+
paramsBuilderMock := mocks.NewCommonCertParamsBuilder(t)
684+
paramsVerifierMock := mocks.NewCommonCertParamsVerifier(t)
752685
logger := log.WithFields("aggsender-test", "checkLastCertificateFromAgglayer")
753686
var storageMock *mocks.AggSenderStorage
754687
var storage db.AggSenderStorage
@@ -783,8 +716,7 @@ func newAggsenderTestData(t *testing.T, creationFlags testDataFlags) *aggsenderT
783716
rateLimiter: aggkitcommon.NewRateLimit(aggkitcommon.RateLimitConfig{}),
784717
epochNotifier: epochNotifierMock,
785718
flow: flows.NewPPFlow(logger,
786-
flows.NewBaseFlow(logger, l2BridgeQuerier, storage,
787-
l1InfoTreeQuerierMock, lerQuerier, flows.NewBaseFlowConfigDefault()),
719+
paramsBuilderMock, paramsVerifierMock,
788720
storage, l1InfoTreeQuerierMock, l2BridgeQuerier, signer, true, 0),
789721
}
790722
var flowMock *mocks.AggsenderFlow

aggsender/certificatebuild/common_params_builder.go

Lines changed: 12 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"context"
55
"errors"
66
"fmt"
7-
"sync/atomic"
87
"time"
98

109
agglayertypes "github.com/agglayer/aggkit/agglayer/types"
@@ -17,28 +16,13 @@ import (
1716

1817
var (
1918
errNoBridgesAndClaims = errors.New("no bridges and claims to build certificate")
20-
errNoNewBlocks = errors.New("no new blocks to send a certificate")
19+
ErrNoNewBlocks = errors.New("no new blocks to send a certificate")
2120

22-
emptyLER = common.HexToHash("0x27ae5ba08d7291c96c8cbddcc148bf48a6d68c7974b94356f53754ef6171d757")
21+
EmptyLER = common.HexToHash("0x27ae5ba08d7291c96c8cbddcc148bf48a6d68c7974b94356f53754ef6171d757")
2322
)
2423

25-
var timeNowFunc atomic.Value
26-
27-
func init() {
28-
timeNowFunc.Store(TimeNowUTC)
29-
}
30-
31-
// TimeNow returns the current time as a uint32 timestamp (thread-safe).
32-
func TimeNow() uint32 {
33-
return timeNowFunc.Load().(func() uint32)() //nolint:forcetypeassert
34-
}
35-
36-
func SetTimeNowFunc(f func() uint32) {
37-
timeNowFunc.Store(f)
38-
}
39-
40-
// TimeNowUTC returns the current time in UTC as a uint32 timestamp.
41-
func TimeNowUTC() uint32 {
24+
// timeNowUTC returns the current time in UTC as a uint32 timestamp.
25+
func timeNowUTC() uint32 {
4226
// Use a more precise time function to avoid collisions in tests
4327
// and ensure that the time is always in UTC.
4428
return uint32(time.Now().UTC().Unix())
@@ -93,6 +77,8 @@ type commonParamsBuilder struct {
9377
lerQuerier types.LERQuerier
9478

9579
cfg CommonBuildConfig
80+
// TimeNowFunc is a function that returns the current time as a uint32 timestamp.
81+
timeNowFunc func() uint32
9682
}
9783

9884
// NewCommonParamsBuilder creates a new instance of CommonParamsBuilder.
@@ -109,6 +95,7 @@ func NewCommonParamsBuilder(
10995
cfg: cfg,
11096
storage: storage,
11197
lerQuerier: lerQuerier,
98+
timeNowFunc: timeNowUTC,
11299
l2BridgeQuerier: l2BridgeQuerier,
113100
l1InfoTreeQuerier: l1InfoTreeDataQuerier,
114101
}
@@ -151,7 +138,7 @@ func (c *commonParamsBuilder) GeneratePreBuildParams(ctx context.Context,
151138
L1InfoTreeRootToProve: l1InfoRoot.Hash,
152139
L1InfoTreeLeafCount: l1InfoRoot.Index + 1,
153140
},
154-
CreatedAt: TimeNow(),
141+
CreatedAt: c.timeNowFunc(),
155142
}, nil
156143
}
157144

@@ -319,7 +306,7 @@ func (c *commonParamsBuilder) nextCertificateBlockRange(ctx context.Context,
319306
if previousToBlock >= lastL2BlockSynced {
320307
c.log.Warnf("no new blocks to send a certificate, last certificate block: %d, last L2 block: %d",
321308
previousToBlock, lastL2BlockSynced)
322-
return types.BlockRangeZero, 0, errNoNewBlocks
309+
return types.BlockRangeZero, 0, ErrNoNewBlocks
323310
}
324311

325312
fromBlock := previousToBlock + 1
@@ -352,8 +339,8 @@ func (c *commonParamsBuilder) getLastSentBlockAndRetryCount(
352339
return lastSentBlock, retryCount
353340
}
354341

355-
// GetNewLocalExitRootForCert gets the new local exit root for the new certificate
356-
func (c *commonParamsBuilder) GetNewLocalExitRootForCert(ctx context.Context,
342+
// GetNewLocalExitRoot gets the new local exit root for the new certificate
343+
func (c *commonParamsBuilder) GetNewLocalExitRoot(ctx context.Context,
357344
certParams *types.CertificateBuildParams) (common.Hash, error) {
358345
if certParams == nil {
359346
return common.Hash{},
@@ -450,7 +437,7 @@ func (c *commonParamsBuilder) getStartLER() (common.Hash, error) {
450437
}
451438

452439
if ler == aggkitcommon.ZeroHash {
453-
return emptyLER, nil
440+
return EmptyLER, nil
454441
}
455442

456443
return ler, nil

aggsender/certificatebuild/common_params_builder_test.go

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,6 @@ func timeNowUTCForTest() uint32 {
2525
func Test_GeneratePreBuildParams(t *testing.T) {
2626
t.Parallel()
2727

28-
SetTimeNowFunc(timeNowUTCForTest)
29-
t.Cleanup(func() {
30-
SetTimeNowFunc(TimeNowUTC)
31-
})
32-
3328
testCases := []struct {
3429
name string
3530
certType types.CertificateType
@@ -118,6 +113,9 @@ func Test_GeneratePreBuildParams(t *testing.T) {
118113
nil, // lerQuerier
119114
NewCommonBuildConfig(0, 0, false),
120115
)
116+
bl, ok := builder.(*commonParamsBuilder)
117+
require.True(t, ok, "builder should be of type *commonParamsBuilder")
118+
bl.timeNowFunc = timeNowUTCForTest
121119

122120
result, err := builder.GeneratePreBuildParams(t.Context(), tc.certType)
123121

@@ -138,11 +136,6 @@ func Test_GeneratePreBuildParams(t *testing.T) {
138136
func Test_GenerateBuildParams(t *testing.T) {
139137
t.Parallel()
140138

141-
SetTimeNowFunc(timeNowUTCForTest)
142-
t.Cleanup(func() {
143-
SetTimeNowFunc(TimeNowUTC)
144-
})
145-
146139
testCases := []struct {
147140
name string
148141
preParams types.CertificatePreBuildParams
@@ -237,6 +230,9 @@ func Test_GenerateBuildParams(t *testing.T) {
237230
nil, // lerQuerier
238231
NewCommonBuildConfig(0, 0, false),
239232
)
233+
bl, ok := builder.(*commonParamsBuilder)
234+
require.True(t, ok, "builder should be of type *commonParamsBuilder")
235+
bl.timeNowFunc = timeNowUTCForTest
240236

241237
result, err := builder.GenerateBuildParams(t.Context(), tc.preParams)
242238

@@ -378,7 +374,7 @@ func Test_LimitCertSize(t *testing.T) {
378374
}
379375
}
380376

381-
func Test_GetNewLocalExitRootForCert(t *testing.T) {
377+
func Test_GetNewLocalExitRoot(t *testing.T) {
382378
t.Parallel()
383379

384380
tests := []struct {
@@ -453,7 +449,7 @@ func Test_GetNewLocalExitRootForCert(t *testing.T) {
453449
}
454450

455451
ctx := context.TODO()
456-
result, err := builder.GetNewLocalExitRootForCert(ctx, tt.certParams)
452+
result, err := builder.GetNewLocalExitRoot(ctx, tt.certParams)
457453

458454
if tt.expectedError != "" {
459455
require.Error(t, err)
@@ -469,11 +465,6 @@ func Test_GetNewLocalExitRootForCert(t *testing.T) {
469465
func Test_GetCommonCertificateBuildParams(t *testing.T) {
470466
t.Parallel()
471467

472-
SetTimeNowFunc(timeNowUTCForTest)
473-
t.Cleanup(func() {
474-
SetTimeNowFunc(TimeNowUTC)
475-
})
476-
477468
type mocksSetup struct {
478469
storage *mocks.AggSenderStorage
479470
l2BridgeQuerier *mocks.BridgeQuerier
@@ -571,6 +562,10 @@ func Test_GetCommonCertificateBuildParams(t *testing.T) {
571562
m.lerQuerier,
572563
cfg,
573564
)
565+
bl, ok := builder.(*commonParamsBuilder)
566+
require.True(t, ok, "builder should be of type *commonParamsBuilder")
567+
bl.timeNowFunc = timeNowUTCForTest
568+
574569
result, err := builder.GetCommonCertificateBuildParams(context.Background(), tt.certType)
575570
if tt.expectedError != "" {
576571
require.Error(t, err)
@@ -697,7 +692,7 @@ func Test_getNextHeightAndPreviousLER(t *testing.T) {
697692
name: "no last sent certificate - zero start LER",
698693
lastSentCert: nil,
699694
expectedHeight: 0,
700-
expectedLER: emptyLER,
695+
expectedLER: EmptyLER,
701696
mockFn: func(mockLERQuerier *mocks.LERQuerier, mockStorage *mocks.AggSenderStorage) {
702697
mockLERQuerier.EXPECT().GetLastLocalExitRoot().Return(aggkitcommon.ZeroHash, nil)
703698
},
@@ -760,9 +755,9 @@ func Test_getNextHeightAndPreviousLER(t *testing.T) {
760755
NewLocalExitRoot: common.HexToHash("0x789"),
761756
},
762757
expectedHeight: 0,
763-
expectedLER: emptyLER,
758+
expectedLER: EmptyLER,
764759
mockFn: func(mockLERQuerier *mocks.LERQuerier, mockStorage *mocks.AggSenderStorage) {
765-
mockLERQuerier.EXPECT().GetLastLocalExitRoot().Return(emptyLER, nil)
760+
mockLERQuerier.EXPECT().GetLastLocalExitRoot().Return(EmptyLER, nil)
766761
},
767762
},
768763
{

0 commit comments

Comments
 (0)