Skip to content

Commit bf95c12

Browse files
increase code coverage, ignore double insertions to bridge_archive table
1 parent 44774fa commit bf95c12

File tree

4 files changed

+55
-26
lines changed

4 files changed

+55
-26
lines changed

bridgesync/downloader_test.go

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ func TestBuildAppender(t *testing.T) {
5555
eventSignature common.Hash
5656
deploymentKind BridgeDeployment
5757
logBuilder func() (types.Log, error)
58+
expectedErr string
5859
}{
5960
{
6061
name: "bridgeEventSignature appender",
@@ -372,6 +373,12 @@ func TestBuildAppender(t *testing.T) {
372373
return l, nil
373374
},
374375
},
376+
{
377+
name: "unknown deployment kind",
378+
deploymentKind: 100,
379+
logBuilder: func() (types.Log, error) { return types.Log{}, nil },
380+
expectedErr: "unsupported bridge deployment kind: 100",
381+
},
375382
}
376383

377384
for _, tt := range tests {
@@ -382,17 +389,21 @@ func TestBuildAppender(t *testing.T) {
382389
logger := logger.WithFields("module", "test")
383390
bridgeDeployment.kind = tt.deploymentKind
384391
appenderMap, err := buildAppender(ethClient, bridgeAddr, false, bridgeDeployment, logger)
385-
require.NoError(t, err)
386-
require.NotNil(t, appenderMap)
392+
if tt.expectedErr == "" {
393+
require.NoError(t, err)
394+
require.NotNil(t, appenderMap)
387395

388-
block := &sync.EVMBlock{EVMBlockHeader: sync.EVMBlockHeader{Num: blockNum}}
396+
block := &sync.EVMBlock{EVMBlockHeader: sync.EVMBlockHeader{Num: blockNum}}
389397

390-
appenderFunc, exists := appenderMap[tt.eventSignature]
391-
require.True(t, exists)
398+
appenderFunc, exists := appenderMap[tt.eventSignature]
399+
require.True(t, exists)
392400

393-
err = appenderFunc(block, log)
394-
require.NoError(t, err)
395-
require.Len(t, block.Events, 1)
401+
err = appenderFunc(block, log)
402+
require.NoError(t, err)
403+
require.Len(t, block.Events, 1)
404+
} else {
405+
require.ErrorContains(t, err, tt.expectedErr)
406+
}
396407
})
397408
}
398409
}

bridgesync/migrations/bridgesync0011.sql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ CREATE TRIGGER IF NOT EXISTS archive_bridge_before_delete
4242
BEFORE DELETE ON bridge
4343
FOR EACH ROW
4444
BEGIN
45-
INSERT INTO bridge_archive (
45+
INSERT OR IGNORE INTO bridge_archive (
4646
deposit_count,
4747
block_num,
4848
block_pos,

bridgesync/processor.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1369,10 +1369,9 @@ func (p *processor) ProcessBlock(ctx context.Context, block sync.Block) error {
13691369

13701370
// remove all the bridges whose deposit_count is greater than the one captured by the BackwardLET event
13711371
deleteBridges := fmt.Sprintf("DELETE from %s WHERE deposit_count > $1", bridgeTableName)
1372-
_, err := tx.Exec(deleteBridges, newDepositCount)
1372+
_, err := tx.Exec(deleteBridges, newDepositCountU64)
13731373
if err != nil {
1374-
p.log.Errorf("failed to remove bridges whose deposit count is greater than or equal to %d",
1375-
event.BackwardLET.NewDepositCount)
1374+
p.log.Errorf("failed to remove bridges whose deposit count is greater than %d", newDepositCountU64)
13761375
return err
13771376
}
13781377

bridgesync/processor_test.go

Lines changed: 33 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"github.com/0xPolygon/cdk-contracts-tooling/contracts/aggchain-multisig/polygonzkevmbridge"
2121
bridgetypes "github.com/agglayer/aggkit/bridgeservice/types"
2222
"github.com/agglayer/aggkit/bridgesync/migrations"
23+
bridgesynctypes "github.com/agglayer/aggkit/bridgesync/types"
2324
"github.com/agglayer/aggkit/db"
2425
"github.com/agglayer/aggkit/log"
2526
"github.com/agglayer/aggkit/sync"
@@ -339,22 +340,22 @@ var (
339340
Event{Bridge: &Bridge{
340341
BlockNum: 1,
341342
BlockPos: 0,
342-
LeafType: 1,
343+
LeafType: bridgesynctypes.LeafTypeAsset.Uint8(),
343344
OriginNetwork: 1,
344-
OriginAddress: common.HexToAddress("01"),
345+
OriginAddress: common.HexToAddress("1"),
345346
DestinationNetwork: 1,
346-
DestinationAddress: common.HexToAddress("01"),
347+
DestinationAddress: common.HexToAddress("1"),
347348
Amount: big.NewInt(1),
348-
Metadata: common.Hex2Bytes("01"),
349+
Metadata: common.Hex2Bytes("1"),
349350
DepositCount: 0,
350351
}},
351352
Event{Claim: &Claim{
352353
BlockNum: 1,
353354
BlockPos: 1,
354355
GlobalIndex: big.NewInt(1),
355356
OriginNetwork: 1,
356-
OriginAddress: common.HexToAddress("01"),
357-
DestinationAddress: common.HexToAddress("01"),
357+
OriginAddress: common.HexToAddress("1"),
358+
DestinationAddress: common.HexToAddress("1"),
358359
Amount: big.NewInt(1),
359360
MainnetExitRoot: common.Hash{},
360361
}},
@@ -390,27 +391,39 @@ var (
390391
Event{Bridge: &Bridge{
391392
BlockNum: 3,
392393
BlockPos: 0,
393-
LeafType: 2,
394+
LeafType: bridgesynctypes.LeafTypeAsset.Uint8(),
394395
OriginNetwork: 2,
395-
OriginAddress: common.HexToAddress("02"),
396+
OriginAddress: common.HexToAddress("2"),
396397
DestinationNetwork: 2,
397-
DestinationAddress: common.HexToAddress("02"),
398+
DestinationAddress: common.HexToAddress("2"),
398399
Amount: big.NewInt(2),
399-
Metadata: common.Hex2Bytes("02"),
400+
Metadata: common.Hex2Bytes("2"),
400401
DepositCount: 1,
401402
}},
402403
Event{Bridge: &Bridge{
403404
BlockNum: 3,
404405
BlockPos: 1,
405-
LeafType: 3,
406+
LeafType: bridgesynctypes.LeafTypeAsset.Uint8(),
406407
OriginNetwork: 3,
407-
OriginAddress: common.HexToAddress("03"),
408+
OriginAddress: common.HexToAddress("3"),
408409
DestinationNetwork: 3,
409-
DestinationAddress: common.HexToAddress("03"),
410+
DestinationAddress: common.HexToAddress("3"),
410411
Amount: big.NewInt(0),
411-
Metadata: common.Hex2Bytes("03"),
412+
Metadata: common.Hex2Bytes("3"),
412413
DepositCount: 2,
413414
}},
415+
Event{Bridge: &Bridge{
416+
BlockNum: 3,
417+
BlockPos: 2,
418+
LeafType: bridgesynctypes.LeafTypeAsset.Uint8(),
419+
OriginNetwork: 3,
420+
OriginAddress: common.HexToAddress("4"),
421+
DestinationNetwork: 3,
422+
DestinationAddress: common.HexToAddress("4"),
423+
Amount: big.NewInt(0),
424+
Metadata: common.Hex2Bytes("4"),
425+
DepositCount: 3,
426+
}},
414427
},
415428
}
416429
block4 = sync.Block{
@@ -453,6 +466,12 @@ var (
453466
BlockPos: 3,
454467
LegacyTokenAddress: common.HexToAddress("0x11"),
455468
}},
469+
Event{BackwardLET: &BackwardLET{
470+
BlockNum: 5,
471+
BlockPos: 4,
472+
PreviousDepositCount: big.NewInt(3),
473+
NewDepositCount: big.NewInt(2),
474+
}},
456475
},
457476
}
458477
)

0 commit comments

Comments
 (0)