Skip to content

Commit 4dcfe11

Browse files
authored
Merge pull request #24 from qubic/bugfix/tx-status-validation
Skip tx status validation in case tx addon is disabled
2 parents 41df2b4 + e6e4f26 commit 4dcfe11

3 files changed

Lines changed: 44 additions & 40 deletions

File tree

validator/txstatus/validator.go

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,21 @@ import (
1111
"github.com/qubic/go-node-connector/types"
1212
)
1313

14-
func Validate(_ context.Context, tickTxStatus types.TransactionStatus, tickTxs types.Transactions) (*protobuf.TickTransactionsStatus, error) {
15-
if tickTxStatus.TxCount != uint32(len(tickTxs)) {
16-
return nil, fmt.Errorf("mismatched tx count: node reported %d, have %d transactions", tickTxStatus.TxCount, len(tickTxs))
17-
}
14+
func ValidateAndConvert(_ context.Context, tickTxStatus types.TransactionStatus, tickTxs types.Transactions, validate bool) (*protobuf.TickTransactionsStatus, error) {
1815

19-
tickTxDigests, err := getTickTxDigests(tickTxs)
20-
if err != nil {
21-
return nil, fmt.Errorf("getting tick tx digests: %w", err)
22-
}
16+
if validate { // needs tx status addon to be enabled
17+
if tickTxStatus.TxCount != uint32(len(tickTxs)) {
18+
return nil, fmt.Errorf("mismatched tx count: node reported %d, have %d transactions", tickTxStatus.TxCount, len(tickTxs))
19+
}
2320

24-
if !equalDigests(tickTxDigests, tickTxStatus.TransactionDigests) {
25-
return nil, fmt.Errorf("transaction digests do not match")
21+
tickTxDigests, err := getTickTxDigests(tickTxs)
22+
if err != nil {
23+
return nil, fmt.Errorf("getting tick tx digests: %w", err)
24+
}
25+
26+
if !equalDigests(tickTxDigests, tickTxStatus.TransactionDigests) {
27+
return nil, fmt.Errorf("transaction digests do not match")
28+
}
2629
}
2730

2831
proto, err := qubicToProto(tickTxs, tickTxStatus)

validator/txstatus/validator_test.go

Lines changed: 30 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"testing"
66

77
"github.com/qubic/go-node-connector/types"
8+
"github.com/stretchr/testify/require"
89
)
910

1011
// helper to build a minimal transaction with known source/dest public keys
@@ -22,9 +23,7 @@ func TestValidate_HappyPath(t *testing.T) {
2223
txs := types.Transactions{tx}
2324

2425
digest, err := tx.Digest()
25-
if err != nil {
26-
t.Fatalf("computing digest: %v", err)
27-
}
26+
require.NoError(t, err)
2827

2928
txStatus := types.TransactionStatus{
3029
CurrentTickOfNode: 1000,
@@ -34,18 +33,10 @@ func TestValidate_HappyPath(t *testing.T) {
3433
TransactionDigests: [][32]byte{digest},
3534
}
3635

37-
result, err := Validate(context.Background(), txStatus, txs)
38-
if err != nil {
39-
t.Fatalf("unexpected error: %v", err)
40-
}
41-
42-
if len(result.Transactions) != 1 {
43-
t.Fatalf("expected 1 transaction status, got %d", len(result.Transactions))
44-
}
45-
46-
if !result.Transactions[0].MoneyFlew {
47-
t.Error("expected moneyFlew to be true")
48-
}
36+
result, err := ValidateAndConvert(context.Background(), txStatus, txs, false)
37+
require.NoError(t, err)
38+
require.Len(t, result.Transactions, 1)
39+
require.True(t, result.Transactions[0].MoneyFlew)
4940
}
5041

5142
func TestValidate_TxCountMismatch(t *testing.T) {
@@ -61,10 +52,8 @@ func TestValidate_TxCountMismatch(t *testing.T) {
6152
TransactionDigests: nil,
6253
}
6354

64-
_, err := Validate(context.Background(), txStatus, txs)
65-
if err == nil {
66-
t.Fatal("expected error for TxCount mismatch, got nil")
67-
}
55+
_, err := ValidateAndConvert(context.Background(), txStatus, txs, true)
56+
require.Error(t, err)
6857
}
6958

7059
func TestValidate_DigestMismatch(t *testing.T) {
@@ -80,10 +69,8 @@ func TestValidate_DigestMismatch(t *testing.T) {
8069
TransactionDigests: [][32]byte{{99, 99, 99}}, // wrong digest
8170
}
8271

83-
_, err := Validate(context.Background(), txStatus, txs)
84-
if err == nil {
85-
t.Fatal("expected error for digest mismatch, got nil")
86-
}
72+
_, err := ValidateAndConvert(context.Background(), txStatus, txs, true)
73+
require.Error(t, err)
8774
}
8875

8976
func TestValidate_EmptyTick(t *testing.T) {
@@ -97,12 +84,26 @@ func TestValidate_EmptyTick(t *testing.T) {
9784
TransactionDigests: nil,
9885
}
9986

100-
result, err := Validate(context.Background(), txStatus, txs)
101-
if err != nil {
102-
t.Fatalf("unexpected error for empty tick: %v", err)
103-
}
87+
result, err := ValidateAndConvert(context.Background(), txStatus, txs, false)
88+
require.NoError(t, err)
89+
require.Empty(t, result.Transactions)
90+
}
10491

105-
if len(result.Transactions) != 0 {
106-
t.Fatalf("expected 0 transaction statuses, got %d", len(result.Transactions))
92+
func TestValidateAndConvert_NoValidation(t *testing.T) {
93+
tx := makeTransaction([32]byte{1}, [32]byte{2}, 100, 1000)
94+
txs := types.Transactions{tx}
95+
96+
// data that would fail validation
97+
txStatus := types.TransactionStatus{
98+
CurrentTickOfNode: 12345,
99+
Tick: 12345,
100+
TxCount: 1, // any count
101+
MoneyFlew: [128]byte{}, // no tx status data
102+
TransactionDigests: nil, // no tx status digests
107103
}
104+
105+
// should pass because validate=false
106+
result, err := ValidateAndConvert(t.Context(), txStatus, txs, false)
107+
require.NoError(t, err)
108+
require.Empty(t, result.Transactions)
108109
}

validator/validator.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ func (v *Validator) validateTransactions(ctx context.Context, client network.Qub
224224
}
225225

226226
// combine valid transactions with money flew status
227-
transactionsWithTxStatus, err := txstatus.Validate(ctx, tickTxStatus, validTxs)
227+
transactionsWithTxStatus, err := txstatus.ValidateAndConvert(ctx, tickTxStatus, validTxs, v.statusAddonEnabled)
228228
if err != nil {
229229
return nil, nil, fmt.Errorf("validating tx status: %w", err)
230230
}

0 commit comments

Comments
 (0)