Skip to content

Commit 121e215

Browse files
authored
fix(logpoller): minor patches (#584)
* fix: read locked replay status * fix: boc handling * fix: replay request block boundary * fix: validate BlockIDExt * fix: replay data model * fix: add RootHash/FileHash to test BlockIDExt * fix: block hashes in test * fix: error msg * fix: receiver type * fix: test util * chore: lint
1 parent e21088c commit 121e215

12 files changed

Lines changed: 463 additions & 189 deletions

File tree

integration-tests/logpoller/pg_logs_test.go

Lines changed: 17 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import (
1010

1111
"github.com/xssnick/tonutils-go/address"
1212
"github.com/xssnick/tonutils-go/tlb"
13-
"github.com/xssnick/tonutils-go/ton"
1413
"github.com/xssnick/tonutils-go/tvm/cell"
1514

1615
"github.com/smartcontractkit/chainlink-common/pkg/logger"
@@ -19,6 +18,7 @@ import (
1918

2019
"github.com/smartcontractkit/chainlink-ton/integration-tests/logpoller/testdata"
2120
pgtest "github.com/smartcontractkit/chainlink-ton/integration-tests/testutils/postgres"
21+
tontest "github.com/smartcontractkit/chainlink-ton/integration-tests/testutils/ton"
2222
"github.com/smartcontractkit/chainlink-ton/pkg/bindings/examples/counter"
2323
"github.com/smartcontractkit/chainlink-ton/pkg/logpoller"
2424
"github.com/smartcontractkit/chainlink-ton/pkg/logpoller/models"
@@ -46,21 +46,17 @@ func createTestLogs(t *testing.T, addr *address.Address, filterID int64) []model
4646
EndCell()
4747

4848
logs[i] = models.Log{
49-
FilterID: filterID,
50-
ChainID: "test-chain",
51-
Address: addr,
52-
EventSig: counter.TopicCountIncreased,
53-
Data: eventCell,
54-
TxHash: models.TxHash{byte(i + 1), 2, 3, 4, 5},
55-
TxLT: uint64(1000 + i), //nolint:gosec // test code with small values
56-
MsgLT: uint64(1000 + i), //nolint:gosec // test code with small values - same as TxLT for simplicity
57-
TxTimestamp: time.Now().Add(time.Duration(i) * time.Minute),
58-
Block: &ton.BlockIDExt{
59-
Workchain: 0,
60-
Shard: -1,
61-
SeqNo: uint32(100 + i), //nolint:gosec // test code with small values
62-
},
63-
MCBlockSeqno: uint32(200 + i), //nolint:gosec // test code with small values
49+
FilterID: filterID,
50+
ChainID: "test-chain",
51+
Address: addr,
52+
EventSig: counter.TopicCountIncreased,
53+
Data: eventCell,
54+
TxHash: models.TxHash{byte(i + 1), 2, 3, 4, 5},
55+
TxLT: uint64(1000 + i), //nolint:gosec // test code with small values
56+
MsgLT: uint64(1000 + i), //nolint:gosec // test code with small values - same as TxLT for simplicity
57+
TxTimestamp: time.Now().Add(time.Duration(i) * time.Minute),
58+
Block: tontest.TestBlockIDExt(uint32(100 + i)), //nolint:gosec // test code with small values
59+
MCBlockSeqno: uint32(200 + i), //nolint:gosec // test code with small values
6460
MsgIndex: int64(i),
6561
}
6662
}
@@ -197,8 +193,8 @@ func TestPgLogStore(t *testing.T) {
197193
TxLT: uint64(5000 - i), //nolint:gosec // test code with small values
198194
MsgLT: uint64(5000 - i), //nolint:gosec // test code with small values
199195
TxTimestamp: sameTimestamp,
200-
Block: &ton.BlockIDExt{Workchain: 0, Shard: -1, SeqNo: uint32(500 + i)}, //nolint:gosec // test code
201-
MCBlockSeqno: uint32(600 + i), //nolint:gosec // test code
196+
Block: tontest.TestBlockIDExt(uint32(500 + i)), //nolint:gosec // test code
197+
MCBlockSeqno: uint32(600 + i), //nolint:gosec // test code
202198
MsgIndex: int64(i),
203199
}
204200
}
@@ -354,7 +350,7 @@ func TestGetLatestBlock(t *testing.T) {
354350
TxLT: uint64(1000 + idx), //nolint:gosec // test code
355351
MsgLT: uint64(1000 + idx), //nolint:gosec // test code
356352
TxTimestamp: time.Now(),
357-
Block: &ton.BlockIDExt{Workchain: 0, Shard: -1, SeqNo: uint32(100 + idx)}, //nolint:gosec // test code
353+
Block: tontest.TestBlockIDExt(uint32(100 + idx)), //nolint:gosec // test code
358354
MCBlockSeqno: mcSeqno,
359355
MsgIndex: int64(idx),
360356
}
@@ -449,8 +445,8 @@ func TestMultiFilterDeduplication(t *testing.T) {
449445
TxLT: uint64(1000 + eventIdx), //nolint:gosec // test code
450446
MsgLT: uint64(1000 + eventIdx), //nolint:gosec // test code
451447
TxTimestamp: baseTime.Add(time.Duration(eventIdx) * time.Minute),
452-
Block: &ton.BlockIDExt{Workchain: 0, Shard: -1, SeqNo: uint32(100 + eventIdx)}, //nolint:gosec // test code
453-
MCBlockSeqno: uint32(200 + eventIdx), //nolint:gosec // test code
448+
Block: tontest.TestBlockIDExt(uint32(100 + eventIdx)), //nolint:gosec // test code
449+
MCBlockSeqno: uint32(200 + eventIdx), //nolint:gosec // test code
454450
MsgIndex: int64(eventIdx),
455451
}
456452
inserted, ierr := logStore.SaveLogs(ctx, []models.Log{log}, logpoller.DefaultConfigSet.BatchInsertSize, logpoller.DefaultConfigSet.MinBatchSize)

integration-tests/logpoller/pg_pruning_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,13 @@ import (
1111

1212
"github.com/xssnick/tonutils-go/address"
1313
"github.com/xssnick/tonutils-go/tlb"
14-
"github.com/xssnick/tonutils-go/ton"
1514
"github.com/xssnick/tonutils-go/tvm/cell"
1615

1716
"github.com/smartcontractkit/chainlink-common/pkg/logger"
1817

1918
"github.com/smartcontractkit/chainlink-ton/integration-tests/logpoller/testdata"
2019
pgtest "github.com/smartcontractkit/chainlink-ton/integration-tests/testutils/postgres"
20+
tontest "github.com/smartcontractkit/chainlink-ton/integration-tests/testutils/ton"
2121
"github.com/smartcontractkit/chainlink-ton/pkg/bindings/examples/counter"
2222
"github.com/smartcontractkit/chainlink-ton/pkg/logpoller"
2323
"github.com/smartcontractkit/chainlink-ton/pkg/logpoller/models"
@@ -58,8 +58,8 @@ func createTestLogsForPruning(t *testing.T, addr *address.Address, filterID int6
5858
TxLT: uint64(1000 + i), //nolint:gosec // test code
5959
MsgLT: uint64(1000 + i), //nolint:gosec // test code
6060
TxTimestamp: baseTime.Add(time.Duration(i) * time.Minute),
61-
Block: &ton.BlockIDExt{Workchain: 0, Shard: -1, SeqNo: uint32(100 + i)}, //nolint:gosec // test code
62-
MCBlockSeqno: uint32(200 + i), //nolint:gosec // test code
61+
Block: tontest.TestBlockIDExt(uint32(100 + i)), //nolint:gosec // test code
62+
MCBlockSeqno: uint32(200 + i), //nolint:gosec // test code
6363
MsgIndex: int64(i),
6464
}
6565

@@ -94,8 +94,8 @@ func createLogsWithTxLT(t *testing.T, addr *address.Address, filterID int64, txL
9494
TxLT: txLT,
9595
MsgLT: txLT,
9696
TxTimestamp: baseTime.Add(time.Duration(i) * time.Minute),
97-
Block: &ton.BlockIDExt{Workchain: 0, Shard: -1, SeqNo: uint32(100 + i)}, //nolint:gosec // test code
98-
MCBlockSeqno: uint32(200 + i), //nolint:gosec // test code
97+
Block: tontest.TestBlockIDExt(uint32(100 + i)), //nolint:gosec // test code
98+
MCBlockSeqno: uint32(200 + i), //nolint:gosec // test code
9999
MsgIndex: 0,
100100
}
101101
}

integration-tests/smoke/chainaccessor/accessor_test.go

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import (
3030

3131
logpoller_testdata "github.com/smartcontractkit/chainlink-ton/integration-tests/logpoller/testdata"
3232
pgtest "github.com/smartcontractkit/chainlink-ton/integration-tests/testutils/postgres"
33+
tontest "github.com/smartcontractkit/chainlink-ton/integration-tests/testutils/ton"
3334
)
3435

3536
const (
@@ -484,7 +485,7 @@ func testCommitReportsMixedHelper(t *testing.T, lp logpoller.Service, logStore l
484485
TxHash: lptypes.TxHash{1, 2, 3, 4, 5},
485486
TxLT: 1000,
486487
TxTimestamp: baseTimestamp.Add(1 * time.Second),
487-
Block: &ton.BlockIDExt{Workchain: 0, Shard: -1, SeqNo: 100},
488+
Block: tontest.TestBlockIDExt(100),
488489
MCBlockSeqno: 200,
489490
MsgIndex: 0,
490491
},
@@ -498,7 +499,7 @@ func testCommitReportsMixedHelper(t *testing.T, lp logpoller.Service, logStore l
498499
TxHash: lptypes.TxHash{2, 3, 4, 5, 6},
499500
TxLT: 1001,
500501
TxTimestamp: baseTimestamp.Add(2 * time.Second),
501-
Block: &ton.BlockIDExt{Workchain: 0, Shard: -1, SeqNo: 101},
502+
Block: tontest.TestBlockIDExt(101),
502503
MCBlockSeqno: 201,
503504
MsgIndex: 1,
504505
},
@@ -512,7 +513,7 @@ func testCommitReportsMixedHelper(t *testing.T, lp logpoller.Service, logStore l
512513
TxHash: lptypes.TxHash{3, 4, 5, 6, 7},
513514
TxLT: 1002,
514515
TxTimestamp: baseTimestamp.Add(3 * time.Second),
515-
Block: &ton.BlockIDExt{Workchain: 0, Shard: -1, SeqNo: 102},
516+
Block: tontest.TestBlockIDExt(102),
516517
MCBlockSeqno: 202,
517518
MsgIndex: 2,
518519
},
@@ -526,7 +527,7 @@ func testCommitReportsMixedHelper(t *testing.T, lp logpoller.Service, logStore l
526527
TxHash: lptypes.TxHash{4, 5, 6, 7, 8},
527528
TxLT: 1003,
528529
TxTimestamp: baseTimestamp.Add(4 * time.Second),
529-
Block: &ton.BlockIDExt{Workchain: 0, Shard: -1, SeqNo: 103},
530+
Block: tontest.TestBlockIDExt(103),
530531
MCBlockSeqno: 203,
531532
MsgIndex: 3,
532533
},
@@ -540,7 +541,7 @@ func testCommitReportsMixedHelper(t *testing.T, lp logpoller.Service, logStore l
540541
TxHash: lptypes.TxHash{5, 6, 7, 8, 9},
541542
TxLT: 1004,
542543
TxTimestamp: baseTimestamp.Add(5 * time.Second),
543-
Block: &ton.BlockIDExt{Workchain: 0, Shard: -1, SeqNo: 104},
544+
Block: tontest.TestBlockIDExt(104),
544545
MCBlockSeqno: 204,
545546
MsgIndex: 4,
546547
},
@@ -609,7 +610,7 @@ func testCommitReportsBasicHelper(t *testing.T, lp logpoller.Service, logStore l
609610
TxHash: lptypes.TxHash{1, 2, 3, 4, 5},
610611
TxLT: 1000,
611612
TxTimestamp: logTimestamp,
612-
Block: &ton.BlockIDExt{Workchain: 0, Shard: -1, SeqNo: 100},
613+
Block: tontest.TestBlockIDExt(100),
613614
MCBlockSeqno: 200,
614615
MsgIndex: 0,
615616
}}, logpoller.DefaultConfigSet.BatchInsertSize, logpoller.DefaultConfigSet.MinBatchSize)
@@ -870,7 +871,7 @@ func testExecutedMessagesHelper(t *testing.T, lp logpoller.Service, logStore log
870871
TxHash: lptypes.TxHash{1, 2, 3, 4, 5},
871872
TxLT: 1000,
872873
TxTimestamp: baseTimestamp.Add(1 * time.Second),
873-
Block: &ton.BlockIDExt{Workchain: 0, Shard: -1, SeqNo: 100},
874+
Block: tontest.TestBlockIDExt(100),
874875
MCBlockSeqno: 200,
875876
MsgLT: 1000,
876877
MsgIndex: 0,
@@ -884,7 +885,7 @@ func testExecutedMessagesHelper(t *testing.T, lp logpoller.Service, logStore log
884885
TxHash: lptypes.TxHash{2, 3, 4, 5, 6},
885886
TxLT: 1001,
886887
TxTimestamp: baseTimestamp.Add(2 * time.Second),
887-
Block: &ton.BlockIDExt{Workchain: 0, Shard: -1, SeqNo: 101},
888+
Block: tontest.TestBlockIDExt(101),
888889
MCBlockSeqno: 201,
889890
MsgLT: 1001,
890891
MsgIndex: 1,
@@ -898,7 +899,7 @@ func testExecutedMessagesHelper(t *testing.T, lp logpoller.Service, logStore log
898899
TxHash: lptypes.TxHash{3, 4, 5, 6, 7},
899900
TxLT: 1002,
900901
TxTimestamp: baseTimestamp.Add(3 * time.Second),
901-
Block: &ton.BlockIDExt{Workchain: 0, Shard: -1, SeqNo: 102},
902+
Block: tontest.TestBlockIDExt(102),
902903
MCBlockSeqno: 202,
903904
MsgLT: 1002,
904905
MsgIndex: 2,
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package ton
2+
3+
import (
4+
tonSDK "github.com/xssnick/tonutils-go/ton"
5+
)
6+
7+
// TestBlockIDExt creates a valid BlockIDExt for testing with required hash fields.
8+
func TestBlockIDExt(seqNo uint32) *tonSDK.BlockIDExt {
9+
return &tonSDK.BlockIDExt{
10+
Workchain: 0,
11+
Shard: -1,
12+
SeqNo: seqNo,
13+
RootHash: make([]byte, 32),
14+
FileHash: make([]byte, 32),
15+
}
16+
}

pkg/logpoller/loader/loader.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,11 @@ func (l *rawTxLoader) listTransactionsWithBlock(ctx context.Context, addr *addre
282282
// update txHash for next iteration's validation
283283
txHash = tx.PrevTxHash
284284

285+
// validate block hash lengths
286+
if err = models.ValidateBlockIDExt(t.IDs[i]); err != nil {
287+
return nil, nil, fmt.Errorf("invalid block ID at index %d: %w", i, err)
288+
}
289+
285290
reversedIdx := (len(txList) - 1) - i
286291
resTxs[reversedIdx] = &tx
287292
resBlocks[reversedIdx] = t.IDs[i]

pkg/logpoller/models/models.go

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

33
import (
44
"encoding/hex"
5+
"errors"
56
"fmt"
67
"strings"
78
"time"
@@ -12,6 +13,26 @@ import (
1213
"github.com/xssnick/tonutils-go/tvm/cell"
1314
)
1415

16+
const (
17+
// int256Len is the byte length of int256 fields in TON TL schema (256 bits = 32 bytes).
18+
// Used for RootHash and FileHash validation in BlockIDExt.
19+
int256Len = 32
20+
)
21+
22+
// ValidateBlockIDExt validates that BlockIDExt hashes have the expected length.
23+
func ValidateBlockIDExt(block *ton.BlockIDExt) error {
24+
if block == nil {
25+
return errors.New("block is nil")
26+
}
27+
if len(block.RootHash) != int256Len {
28+
return fmt.Errorf("invalid RootHash length: expected %d bytes, got %d", int256Len, len(block.RootHash))
29+
}
30+
if len(block.FileHash) != int256Len {
31+
return fmt.Errorf("invalid FileHash length: expected %d bytes, got %d", int256Len, len(block.FileHash))
32+
}
33+
return nil
34+
}
35+
1536
// ReplayStatus represents the current state of a replay operation
1637
type ReplayStatus int
1738

pkg/logpoller/models/models_test.go

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,90 @@ import (
55

66
"github.com/stretchr/testify/require"
77
"github.com/xssnick/tonutils-go/address"
8+
"github.com/xssnick/tonutils-go/ton"
89
)
910

11+
func TestValidateBlockIDExt(t *testing.T) {
12+
tests := []struct {
13+
name string
14+
block *ton.BlockIDExt
15+
wantErr string
16+
}{
17+
{
18+
name: "nil block",
19+
block: nil,
20+
wantErr: "block is nil",
21+
},
22+
{
23+
name: "valid block",
24+
block: &ton.BlockIDExt{
25+
Workchain: 0,
26+
Shard: -1,
27+
SeqNo: 100,
28+
RootHash: make([]byte, 32),
29+
FileHash: make([]byte, 32),
30+
},
31+
wantErr: "",
32+
},
33+
{
34+
name: "empty RootHash",
35+
block: &ton.BlockIDExt{
36+
Workchain: 0,
37+
Shard: -1,
38+
SeqNo: 100,
39+
RootHash: nil,
40+
FileHash: make([]byte, 32),
41+
},
42+
wantErr: "invalid RootHash length: expected 32 bytes, got 0",
43+
},
44+
{
45+
name: "empty FileHash",
46+
block: &ton.BlockIDExt{
47+
Workchain: 0,
48+
Shard: -1,
49+
SeqNo: 100,
50+
RootHash: make([]byte, 32),
51+
FileHash: nil,
52+
},
53+
wantErr: "invalid FileHash length: expected 32 bytes, got 0",
54+
},
55+
{
56+
name: "short RootHash",
57+
block: &ton.BlockIDExt{
58+
Workchain: 0,
59+
Shard: -1,
60+
SeqNo: 100,
61+
RootHash: make([]byte, 16),
62+
FileHash: make([]byte, 32),
63+
},
64+
wantErr: "invalid RootHash length: expected 32 bytes, got 16",
65+
},
66+
{
67+
name: "long FileHash",
68+
block: &ton.BlockIDExt{
69+
Workchain: 0,
70+
Shard: -1,
71+
SeqNo: 100,
72+
RootHash: make([]byte, 32),
73+
FileHash: make([]byte, 64),
74+
},
75+
wantErr: "invalid FileHash length: expected 32 bytes, got 64",
76+
},
77+
}
78+
79+
for _, tt := range tests {
80+
t.Run(tt.name, func(t *testing.T) {
81+
err := ValidateBlockIDExt(tt.block)
82+
if tt.wantErr == "" {
83+
require.NoError(t, err)
84+
} else {
85+
require.Error(t, err)
86+
require.Contains(t, err.Error(), tt.wantErr)
87+
}
88+
})
89+
}
90+
}
91+
1092
func TestLog_Validate(t *testing.T) {
1193
testAddr, err := address.ParseAddr("EQDKbjIcfM6ezt8KjKJJLshZJJSqX7XOA4ff-W72r5gqPrHF")
1294
require.NoError(t, err)

0 commit comments

Comments
 (0)