forked from solana-foundation/solana-go
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmessage_test.go
More file actions
709 lines (626 loc) · 21.3 KB
/
Copy pathmessage_test.go
File metadata and controls
709 lines (626 loc) · 21.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
package solana
import (
"testing"
bin "github.com/gagliardetto/binary"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
// Ported from solana-sdk/message/src/versions/mod.rs: test_legacy_message_serialization
func TestLegacyMessageSerializationRoundtrip(t *testing.T) {
key0 := newUniqueKey()
key1 := newUniqueKey()
key2 := newUniqueKey()
blockhash := Hash{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32}
msg := Message{
Header: MessageHeader{
NumRequiredSignatures: 2,
NumReadonlySignedAccounts: 1,
NumReadonlyUnsignedAccounts: 1,
},
AccountKeys: PublicKeySlice{key0, key1, key2},
RecentBlockhash: blockhash,
Instructions: []CompiledInstruction{
{
ProgramIDIndex: 2,
Accounts: []uint16{0, 1},
Data: []byte{0xAA, 0xBB},
},
},
}
msg.version = MessageVersionLegacy
data, err := msg.MarshalBinary()
require.NoError(t, err)
// First byte should be numRequiredSignatures for legacy messages.
assert.Equal(t, byte(2), data[0], "first byte should be numRequiredSignatures")
var decoded Message
err = decoded.UnmarshalWithDecoder(bin.NewBinDecoder(data))
require.NoError(t, err)
assert.Equal(t, MessageVersionLegacy, decoded.GetVersion())
assert.Equal(t, msg.Header, decoded.Header)
assert.Equal(t, msg.AccountKeys, decoded.AccountKeys)
assert.Equal(t, msg.RecentBlockhash, decoded.RecentBlockhash)
require.Equal(t, len(msg.Instructions), len(decoded.Instructions))
assert.Equal(t, msg.Instructions[0].ProgramIDIndex, decoded.Instructions[0].ProgramIDIndex)
assert.Equal(t, msg.Instructions[0].Accounts, decoded.Instructions[0].Accounts)
assert.Equal(t, Base58(msg.Instructions[0].Data), decoded.Instructions[0].Data)
}
// Ported from solana-sdk/message/src/versions/mod.rs: test_versioned_message_serialization
func TestV0MessageSerializationRoundtrip(t *testing.T) {
key0 := newUniqueKey()
tableKey0 := newUniqueKey()
tableKey1 := newUniqueKey()
blockhash := Hash{9, 8, 7, 6, 5, 4, 3, 2, 1, 0, 1, 2, 3, 4, 5, 6,
7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22}
msg := Message{
Header: MessageHeader{
NumRequiredSignatures: 1,
NumReadonlySignedAccounts: 0,
NumReadonlyUnsignedAccounts: 0,
},
AccountKeys: PublicKeySlice{key0},
RecentBlockhash: blockhash,
Instructions: []CompiledInstruction{
{
ProgramIDIndex: 1,
Accounts: []uint16{0, 2, 3, 4},
Data: []byte{},
},
},
AddressTableLookups: []MessageAddressTableLookup{
{
AccountKey: tableKey0,
WritableIndexes: []uint8{1},
ReadonlyIndexes: []uint8{0},
},
{
AccountKey: tableKey1,
WritableIndexes: []uint8{0},
ReadonlyIndexes: []uint8{1},
},
},
}
msg.version = MessageVersionV0
data, err := msg.MarshalBinary()
require.NoError(t, err)
// First byte must have high bit set for versioned messages (0x80).
assert.Equal(t, byte(0x80), data[0], "first byte should be version prefix 0x80 for V0")
var decoded Message
err = decoded.UnmarshalWithDecoder(bin.NewBinDecoder(data))
require.NoError(t, err)
assert.Equal(t, MessageVersionV0, decoded.GetVersion())
assert.Equal(t, msg.Header, decoded.Header)
assert.Equal(t, msg.AccountKeys, decoded.AccountKeys)
assert.Equal(t, msg.RecentBlockhash, decoded.RecentBlockhash)
require.Equal(t, len(msg.Instructions), len(decoded.Instructions))
assert.Equal(t, msg.Instructions[0].ProgramIDIndex, decoded.Instructions[0].ProgramIDIndex)
assert.Equal(t, msg.Instructions[0].Accounts, decoded.Instructions[0].Accounts)
require.Equal(t, 2, len(decoded.AddressTableLookups))
assert.Equal(t, tableKey0, decoded.AddressTableLookups[0].AccountKey)
assert.Equal(t, Uint8SliceAsNum{1}, decoded.AddressTableLookups[0].WritableIndexes)
assert.Equal(t, Uint8SliceAsNum{0}, decoded.AddressTableLookups[0].ReadonlyIndexes)
assert.Equal(t, tableKey1, decoded.AddressTableLookups[1].AccountKey)
assert.Equal(t, Uint8SliceAsNum{0}, decoded.AddressTableLookups[1].WritableIndexes)
assert.Equal(t, Uint8SliceAsNum{1}, decoded.AddressTableLookups[1].ReadonlyIndexes)
}
// Tests the version prefix detection logic ported from solana-sdk/message/src/versions/mod.rs.
// In Rust: MESSAGE_VERSION_PREFIX = 0x80; if first_byte & 0x80 != 0 → versioned.
// This specifically tests the bug fix where byte value 127 (0x7F) was incorrectly
// classified as versioned — it should be legacy (numRequiredSignatures = 127).
func TestVersionDetection_PrefixByte(t *testing.T) {
tests := []struct {
name string
firstByte byte
expectedVersion MessageVersion
}{
{"numRequiredSignatures=1 is legacy", 1, MessageVersionLegacy},
{"numRequiredSignatures=64 is legacy", 64, MessageVersionLegacy},
{"numRequiredSignatures=126 is legacy", 126, MessageVersionLegacy},
{"numRequiredSignatures=127 is legacy", 127, MessageVersionLegacy}, // was buggy before fix
{"0x80 is V0", 0x80, MessageVersionV0},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// Build a minimal valid message with the given first byte.
// For legacy: first byte is numRequiredSignatures.
// For V0: first byte is 0x80 | version.
var buf []byte
if tt.expectedVersion == MessageVersionLegacy {
// legacy: header(3) + compact_len(1) + key(32) + blockhash(32) + compact_len(1)=0 instructions
buf = make([]byte, 0, 69)
buf = append(buf, tt.firstByte, 0, 0) // header
buf = append(buf, 1) // 1 account key
buf = append(buf, make([]byte, 32)...) // account key
buf = append(buf, make([]byte, 32)...) // blockhash
buf = append(buf, 0) // 0 instructions
} else {
// v0: prefix(1) + header(3) + compact_len(1) + key(32) + blockhash(32) + compact_len(1) + compact_len(1) lookups
buf = make([]byte, 0, 71)
buf = append(buf, tt.firstByte) // version prefix
buf = append(buf, 1, 0, 0) // header
buf = append(buf, 1) // 1 account key
buf = append(buf, make([]byte, 32)...) // account key
buf = append(buf, make([]byte, 32)...) // blockhash
buf = append(buf, 0) // 0 instructions
buf = append(buf, 0) // 0 address table lookups
}
var msg Message
err := msg.UnmarshalWithDecoder(bin.NewBinDecoder(buf))
require.NoError(t, err)
assert.Equal(t, tt.expectedVersion, msg.GetVersion())
})
}
}
// Tests that unsupported version numbers (> 0) in versioned messages are rejected.
// Ported from solana-sdk/message/src/versions/v0/mod.rs version validation.
func TestVersionDetection_UnsupportedVersion(t *testing.T) {
// 0x81 = messageVersionPrefix | 1 → version 1 (unsupported)
buf := []byte{0x81, 1, 0, 0}
buf = append(buf, 1) // 1 account key
buf = append(buf, make([]byte, 32)...) // account key
buf = append(buf, make([]byte, 32)...) // blockhash
buf = append(buf, 0) // 0 instructions
buf = append(buf, 0) // 0 lookups
var msg Message
err := msg.UnmarshalWithDecoder(bin.NewBinDecoder(buf))
require.Error(t, err)
assert.Contains(t, err.Error(), "unsupported message version")
}
// Ported from solana-sdk/message/src/legacy.rs: test_is_writable_index_saturating_behavior.
// Tests edge cases where header values exceed the number of account keys.
func TestIsWritable_SaturatingBehavior(t *testing.T) {
// Case 1: num_readonly_signed (2) > num_required_signatures (1)
// Index 0 is signed but readonly count exceeds signature count → not writable.
key0 := newUniqueKey()
msg1 := Message{
Header: MessageHeader{
NumRequiredSignatures: 1,
NumReadonlySignedAccounts: 2,
NumReadonlyUnsignedAccounts: 0,
},
AccountKeys: PublicKeySlice{key0},
}
w, err := msg1.IsWritable(key0)
require.NoError(t, err)
assert.False(t, w, "case 1: readonly signed exceeds required signatures")
// Case 2: num_readonly_unsigned (2) > num unsigned accounts (1)
// Only 1 account, 0 signers, all are unsigned but readonly count exceeds → not writable.
key1 := newUniqueKey()
msg2 := Message{
Header: MessageHeader{
NumRequiredSignatures: 0,
NumReadonlySignedAccounts: 0,
NumReadonlyUnsignedAccounts: 2,
},
AccountKeys: PublicKeySlice{key1},
}
w, err = msg2.IsWritable(key1)
require.NoError(t, err)
assert.False(t, w, "case 2: readonly unsigned exceeds unsigned accounts")
// Case 3: 1 signer, 0 readonly signed, 2 readonly unsigned but only 1 account.
// Index 0 is a writable signer.
msg3 := Message{
Header: MessageHeader{
NumRequiredSignatures: 1,
NumReadonlySignedAccounts: 0,
NumReadonlyUnsignedAccounts: 2,
},
AccountKeys: PublicKeySlice{key0},
}
w, err = msg3.IsWritable(key0)
require.NoError(t, err)
assert.True(t, w, "case 3: signer with no readonly signed is writable")
// Case 4: 2 accounts, 1 signer, 0 readonly signed, 3 readonly unsigned.
// Index 0: writable signer; index 1: unsigned but readonly exceeds → not writable.
key2 := newUniqueKey()
msg4 := Message{
Header: MessageHeader{
NumRequiredSignatures: 1,
NumReadonlySignedAccounts: 0,
NumReadonlyUnsignedAccounts: 3,
},
AccountKeys: PublicKeySlice{key0, key2},
}
w, err = msg4.IsWritable(key0)
require.NoError(t, err)
assert.True(t, w, "case 4: key0 writable signer")
w, err = msg4.IsWritable(key2)
require.NoError(t, err)
assert.False(t, w, "case 4: key1 readonly unsigned")
// Case 5: 2 accounts, 1 signer with 2 readonly signed, 3 readonly unsigned.
// Both accounts should be readonly.
msg5 := Message{
Header: MessageHeader{
NumRequiredSignatures: 1,
NumReadonlySignedAccounts: 2,
NumReadonlyUnsignedAccounts: 3,
},
AccountKeys: PublicKeySlice{key0, key2},
}
w, err = msg5.IsWritable(key0)
require.NoError(t, err)
assert.False(t, w, "case 5: key0 readonly (readonly_signed exceeds required)")
w, err = msg5.IsWritable(key2)
require.NoError(t, err)
assert.False(t, w, "case 5: key1 readonly unsigned")
}
// Ported from solana-sdk/message/src/legacy.rs: test_is_maybe_writable.
// Tests the standard writability layout:
//
// Header: 3 signers (2 readonly), 1 readonly unsigned → 6 accounts total.
// idx 0: writable signer
// idx 1: readonly signer
// idx 2: readonly signer
// idx 3: writable unsigned
// idx 4: writable unsigned
// idx 5: readonly unsigned
func TestIsWritable_StandardLayout(t *testing.T) {
keys := [6]PublicKey{}
for i := range keys {
keys[i] = newUniqueKey()
}
msg := Message{
Header: MessageHeader{
NumRequiredSignatures: 3,
NumReadonlySignedAccounts: 2,
NumReadonlyUnsignedAccounts: 1,
},
AccountKeys: PublicKeySlice{keys[0], keys[1], keys[2], keys[3], keys[4], keys[5]},
}
expected := []bool{true, false, false, true, true, false}
for i, key := range keys {
w, err := msg.IsWritable(key)
require.NoError(t, err)
assert.Equal(t, expected[i], w, "index %d", i)
}
}
// Ported from solana-sdk/message/src/legacy.rs: test_message_signed_keys_len.
func TestIsSigner(t *testing.T) {
key0 := newUniqueKey()
key1 := newUniqueKey()
programID := newUniqueKey()
// No signers.
msg := Message{
Header: MessageHeader{
NumRequiredSignatures: 0,
},
AccountKeys: PublicKeySlice{key0, programID},
}
assert.False(t, msg.IsSigner(key0))
// One signer.
msg = Message{
Header: MessageHeader{
NumRequiredSignatures: 1,
},
AccountKeys: PublicKeySlice{key0, programID},
}
assert.True(t, msg.IsSigner(key0))
assert.False(t, msg.IsSigner(programID))
// Two signers.
msg = Message{
Header: MessageHeader{
NumRequiredSignatures: 2,
},
AccountKeys: PublicKeySlice{key0, key1, programID},
}
assert.True(t, msg.IsSigner(key0))
assert.True(t, msg.IsSigner(key1))
assert.False(t, msg.IsSigner(programID))
// Unknown key is not a signer.
assert.False(t, msg.IsSigner(newUniqueKey()))
}
// Ported from solana-sdk/message/src/legacy.rs: test_program_ids.
func TestProgramIDs(t *testing.T) {
key0 := newUniqueKey()
key1 := newUniqueKey()
programID := newUniqueKey()
msg := Message{
Header: MessageHeader{
NumRequiredSignatures: 1,
NumReadonlySignedAccounts: 0,
NumReadonlyUnsignedAccounts: 1, // programID at index 2 is readonly unsigned
},
AccountKeys: PublicKeySlice{key0, key1, programID},
Instructions: []CompiledInstruction{
{
ProgramIDIndex: 2,
Accounts: []uint16{0, 1},
Data: []byte{},
},
},
}
resolved, err := msg.Program(2)
require.NoError(t, err)
assert.Equal(t, programID, resolved)
_, err = msg.Program(3) // out of range
require.Error(t, err)
}
// Tests that IsWritableStatic only considers static accounts, ignoring lookups.
func TestIsWritableStatic_IgnoresLookups(t *testing.T) {
keys := [4]PublicKey{}
for i := range keys {
keys[i] = newUniqueKey()
}
msg := Message{
Header: MessageHeader{
NumRequiredSignatures: 2,
NumReadonlySignedAccounts: 1,
NumReadonlyUnsignedAccounts: 1,
},
AccountKeys: PublicKeySlice{keys[0], keys[1], keys[2], keys[3]},
}
msg.version = MessageVersionV0
assert.True(t, msg.IsWritableStatic(keys[0]), "writable signer")
assert.False(t, msg.IsWritableStatic(keys[1]), "readonly signer")
assert.True(t, msg.IsWritableStatic(keys[2]), "writable unsigned")
assert.False(t, msg.IsWritableStatic(keys[3]), "readonly unsigned")
assert.False(t, msg.IsWritableStatic(newUniqueKey()), "unknown key")
}
// Tests JSON serialization roundtrip for both legacy and V0 messages.
func TestMessageJSONRoundtrip(t *testing.T) {
t.Run("legacy", func(t *testing.T) {
msg := Message{
Header: MessageHeader{
NumRequiredSignatures: 1,
NumReadonlySignedAccounts: 0,
NumReadonlyUnsignedAccounts: 1,
},
AccountKeys: PublicKeySlice{newUniqueKey(), newUniqueKey()},
RecentBlockhash: Hash{1, 2, 3},
Instructions: []CompiledInstruction{
{ProgramIDIndex: 1, Accounts: []uint16{0}, Data: []byte{0xFF}},
},
}
msg.version = MessageVersionLegacy
data, err := msg.MarshalJSON()
require.NoError(t, err)
require.NotEmpty(t, data)
// Should not contain addressTableLookups for legacy.
assert.NotContains(t, string(data), "addressTableLookups")
})
t.Run("v0", func(t *testing.T) {
msg := Message{
Header: MessageHeader{
NumRequiredSignatures: 1,
NumReadonlySignedAccounts: 0,
NumReadonlyUnsignedAccounts: 0,
},
AccountKeys: PublicKeySlice{newUniqueKey()},
RecentBlockhash: Hash{4, 5, 6},
Instructions: []CompiledInstruction{
{ProgramIDIndex: 0, Accounts: []uint16{0}, Data: []byte{0x01}},
},
AddressTableLookups: []MessageAddressTableLookup{
{
AccountKey: newUniqueKey(),
WritableIndexes: []uint8{0},
ReadonlyIndexes: []uint8{1},
},
},
}
msg.version = MessageVersionV0
data, err := msg.MarshalJSON()
require.NoError(t, err)
assert.Contains(t, string(data), "addressTableLookups")
})
}
// Tests that the V0 prefix byte is exactly 0x80 for version 0,
// matching Rust's MESSAGE_VERSION_PREFIX | 0 = 0x80.
func TestMarshalV0_PrefixByte(t *testing.T) {
msg := Message{
Header: MessageHeader{
NumRequiredSignatures: 1,
NumReadonlySignedAccounts: 0,
NumReadonlyUnsignedAccounts: 0,
},
AccountKeys: PublicKeySlice{newUniqueKey()},
RecentBlockhash: Hash{},
Instructions: []CompiledInstruction{},
}
msg.version = MessageVersionV0
data, err := msg.MarshalBinary()
require.NoError(t, err)
assert.Equal(t, byte(0x80), data[0])
// Second byte should be numRequiredSignatures.
assert.Equal(t, byte(1), data[1])
}
// Tests that legacy message does NOT have the 0x80 prefix.
func TestMarshalLegacy_NoPrefixByte(t *testing.T) {
msg := Message{
Header: MessageHeader{
NumRequiredSignatures: 3,
NumReadonlySignedAccounts: 1,
NumReadonlyUnsignedAccounts: 1,
},
AccountKeys: PublicKeySlice{newUniqueKey(), newUniqueKey(), newUniqueKey()},
RecentBlockhash: Hash{},
Instructions: []CompiledInstruction{},
}
msg.version = MessageVersionLegacy
data, err := msg.MarshalBinary()
require.NoError(t, err)
// First byte is numRequiredSignatures directly, not a version prefix.
assert.Equal(t, byte(3), data[0])
assert.Equal(t, byte(0), data[0]&0x80, "high bit should not be set for legacy")
}
// Tests Account() method for both static and resolved lookup accounts.
func TestAccount_StaticAndLookup(t *testing.T) {
keys := [4]PublicKey{}
for i := range keys {
keys[i] = newUniqueKey()
}
tableKey := newUniqueKey()
lookupKey := newUniqueKey()
msg := Message{
Header: MessageHeader{
NumRequiredSignatures: 1,
NumReadonlySignedAccounts: 0,
NumReadonlyUnsignedAccounts: 0,
},
AccountKeys: PublicKeySlice{keys[0], keys[1]},
AddressTableLookups: []MessageAddressTableLookup{
{
AccountKey: tableKey,
WritableIndexes: []uint8{0},
ReadonlyIndexes: []uint8{},
},
},
}
msg.version = MessageVersionV0
err := msg.SetAddressTables(map[PublicKey]PublicKeySlice{
tableKey: {lookupKey},
})
require.NoError(t, err)
// Static account.
acct, err := msg.Account(0)
require.NoError(t, err)
assert.Equal(t, keys[0], acct)
acct, err = msg.Account(1)
require.NoError(t, err)
assert.Equal(t, keys[1], acct)
// Lookup account (index 2 = first lookup).
acct, err = msg.Account(2)
require.NoError(t, err)
assert.Equal(t, lookupKey, acct)
// Out of range.
_, err = msg.Account(3)
require.Error(t, err)
}
// Tests HasAccount and GetAccountIndex.
func TestHasAccountAndGetIndex(t *testing.T) {
keys := [3]PublicKey{}
for i := range keys {
keys[i] = newUniqueKey()
}
msg := Message{
Header: MessageHeader{
NumRequiredSignatures: 1,
},
AccountKeys: PublicKeySlice{keys[0], keys[1], keys[2]},
}
for i, key := range keys {
has, err := msg.HasAccount(key)
require.NoError(t, err)
assert.True(t, has)
idx, err := msg.GetAccountIndex(key)
require.NoError(t, err)
assert.Equal(t, uint16(i), idx)
}
unknown := newUniqueKey()
has, err := msg.HasAccount(unknown)
require.NoError(t, err)
assert.False(t, has)
_, err = msg.GetAccountIndex(unknown)
require.Error(t, err)
}
// Tests Signers() returns only the first numRequiredSignatures accounts.
func TestSigners(t *testing.T) {
keys := [5]PublicKey{}
for i := range keys {
keys[i] = newUniqueKey()
}
msg := Message{
Header: MessageHeader{
NumRequiredSignatures: 3,
NumReadonlySignedAccounts: 1,
NumReadonlyUnsignedAccounts: 0,
},
AccountKeys: PublicKeySlice{keys[0], keys[1], keys[2], keys[3], keys[4]},
}
signers := msg.Signers()
require.Equal(t, 3, len(signers))
assert.Equal(t, keys[0], signers[0])
assert.Equal(t, keys[1], signers[1])
assert.Equal(t, keys[2], signers[2])
}
// Tests Writable() returns only writable accounts across static and lookup.
func TestWritable(t *testing.T) {
keys := [4]PublicKey{}
for i := range keys {
keys[i] = newUniqueKey()
}
msg := Message{
Header: MessageHeader{
NumRequiredSignatures: 2,
NumReadonlySignedAccounts: 1,
NumReadonlyUnsignedAccounts: 1,
},
AccountKeys: PublicKeySlice{keys[0], keys[1], keys[2], keys[3]},
}
writable, err := msg.Writable()
require.NoError(t, err)
// idx 0: writable signer, idx 1: readonly signer, idx 2: writable unsigned, idx 3: readonly unsigned
require.Equal(t, 2, len(writable))
assert.Equal(t, keys[0], writable[0])
assert.Equal(t, keys[2], writable[1])
}
// Tests that SetVersion validates input.
func TestSetVersion_Validation(t *testing.T) {
msg := &Message{}
_, err := msg.SetVersion(MessageVersionLegacy)
require.NoError(t, err)
assert.Equal(t, MessageVersionLegacy, msg.GetVersion())
_, err = msg.SetVersion(MessageVersionV0)
require.NoError(t, err)
assert.Equal(t, MessageVersionV0, msg.GetVersion())
_, err = msg.SetVersion(MessageVersion(99))
require.Error(t, err)
}
// Tests IsVersioned().
func TestIsVersioned(t *testing.T) {
msg := Message{}
msg.version = MessageVersionLegacy
assert.False(t, msg.IsVersioned())
msg.version = MessageVersionV0
assert.True(t, msg.IsVersioned())
}
// Tests that SetAddressTables can only be called once.
func TestSetAddressTables_OnlyOnce(t *testing.T) {
msg := &Message{}
msg.version = MessageVersionV0
err := msg.SetAddressTables(map[PublicKey]PublicKeySlice{})
require.NoError(t, err)
err = msg.SetAddressTables(map[PublicKey]PublicKeySlice{})
require.Error(t, err)
assert.Contains(t, err.Error(), "already set")
}
// Tests that checkPreconditions fails when address tables are needed but not set.
func TestCheckPreconditions_MissingTables(t *testing.T) {
msg := Message{
AddressTableLookups: []MessageAddressTableLookup{
{
AccountKey: newUniqueKey(),
WritableIndexes: []uint8{0},
ReadonlyIndexes: []uint8{},
},
},
}
msg.version = MessageVersionV0
_, err := msg.AccountMetaList()
require.Error(t, err)
assert.Contains(t, err.Error(), "without address tables")
}
// Tests base64 roundtrip.
func TestMarshalUnmarshalBase64(t *testing.T) {
msg := Message{
Header: MessageHeader{
NumRequiredSignatures: 1,
NumReadonlySignedAccounts: 0,
NumReadonlyUnsignedAccounts: 1,
},
AccountKeys: PublicKeySlice{newUniqueKey(), newUniqueKey()},
RecentBlockhash: Hash{42},
Instructions: []CompiledInstruction{
{ProgramIDIndex: 1, Accounts: []uint16{0}, Data: []byte{0xDE, 0xAD}},
},
}
msg.version = MessageVersionLegacy
b64 := msg.ToBase64()
require.NotEmpty(t, b64)
var decoded Message
err := decoded.UnmarshalBase64(b64)
require.NoError(t, err)
assert.Equal(t, msg.Header, decoded.Header)
assert.Equal(t, msg.AccountKeys, decoded.AccountKeys)
assert.Equal(t, msg.RecentBlockhash, decoded.RecentBlockhash)
}