-
Notifications
You must be signed in to change notification settings - Fork 4
/
message.go
594 lines (528 loc) · 18.5 KB
/
message.go
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
// Copyright 2021 github.com/gagliardetto
// This file has been modified by github.com/gagliardetto
//
// Copyright 2020 dfuse Platform Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package solana
import (
"encoding/base64"
"fmt"
bin "github.com/gagliardetto/binary"
"github.com/gagliardetto/solana-go/text"
"github.com/gagliardetto/treeout"
)
type MessageAddressTableLookupSlice []MessageAddressTableLookup
// NumLookups returns the number of accounts in all the MessageAddressTableLookupSlice
func (lookups MessageAddressTableLookupSlice) NumLookups() int {
count := 0
for _, lookup := range lookups {
// TODO: check if this is correct.
count += len(lookup.ReadonlyIndexes)
count += len(lookup.WritableIndexes)
}
return count
}
// GetTableIDs returns the list of all address table IDs.
func (lookups MessageAddressTableLookupSlice) GetTableIDs() PublicKeySlice {
if lookups == nil {
return nil
}
ids := make(PublicKeySlice, 0)
for _, lookup := range lookups {
ids.UniqueAppend(lookup.AccountKey)
}
return ids
}
type MessageAddressTableLookup struct {
AccountKey PublicKey // The account key of the address table.
WritableIndexes []uint8
ReadonlyIndexes []uint8
}
type MessageVersion int
const (
MessageVersionLegacy MessageVersion = 0 // default
MessageVersionV0 MessageVersion = 1 // v0
)
type Message struct {
version MessageVersion
// List of base-58 encoded public keys used by the transaction,
// including by the instructions and for signatures.
// The first `message.header.numRequiredSignatures` public keys must sign the transaction.
AccountKeys []PublicKey `json:"accountKeys"`
// Details the account types and signatures required by the transaction.
Header MessageHeader `json:"header"`
// A base-58 encoded hash of a recent block in the ledger used to
// prevent transaction duplication and to give transactions lifetimes.
RecentBlockhash Hash `json:"recentBlockhash"`
// List of program instructions that will be executed in sequence
// and committed in one atomic transaction if all succeed.
Instructions []CompiledInstruction `json:"instructions"`
// List of address table lookups used to load additional accounts for this transaction.
addressTableLookups MessageAddressTableLookupSlice
// The actual tables that contain the list of account pubkeys.
// NOTE: you need to fetch these from the chain, and then call `SetAddressTables`
// before you use this transaction -- otherwise, you will get a panic.
addressTables map[PublicKey][]PublicKey
}
func (mx *Message) SetAddressTables(tables map[PublicKey][]PublicKey) error {
if mx.addressTables != nil {
return fmt.Errorf("address tables already set")
}
mx.addressTables = tables
return mx.resolveLookups(tables)
}
// GetAddressTables returns the address tables used by this message.
// NOTE: you must have called `SetAddressTable` one or more times before being able to use this method.
func (mx *Message) GetAddressTables() map[PublicKey][]PublicKey {
return mx.addressTables
}
var _ bin.EncoderDecoder = &Message{}
// SetVersion sets the message version.
// This method forces the message to be encoded in the specified version.
// NOTE: if you set lookups, the version will default to V0.
func (m *Message) SetVersion(version MessageVersion) *Message {
// check if the version is valid
switch version {
case MessageVersionV0, MessageVersionLegacy:
default:
panic(fmt.Errorf("invalid message version: %d", version))
}
m.version = version
return m
}
// GetVersion returns the message version.
func (m *Message) GetVersion() MessageVersion {
return m.version
}
func (mx *Message) SetAddressTableLookups(lookups []MessageAddressTableLookup) *Message {
mx.addressTableLookups = lookups
mx.version = MessageVersionV0
return mx
}
func (mx *Message) AddAddressTableLookup(lookup MessageAddressTableLookup) *Message {
mx.addressTableLookups = append(mx.addressTableLookups, lookup)
mx.version = MessageVersionV0
return mx
}
func (mx *Message) GetAddressTableLookups() MessageAddressTableLookupSlice {
return mx.addressTableLookups
}
func (mx *Message) EncodeToTree(txTree treeout.Branches) {
switch mx.version {
case MessageVersionV0:
txTree.Child("Version: v0")
case MessageVersionLegacy:
txTree.Child("Version: legacy")
default:
txTree.Child(text.Sf("Version (unknown): %v", mx.version))
}
txTree.Child(text.Sf("RecentBlockhash: %s", mx.RecentBlockhash))
txTree.Child(fmt.Sprintf("AccountKeys[len=%v]", len(mx.AccountKeys))).ParentFunc(func(accountKeysBranch treeout.Branches) {
for keyIndex, key := range mx.AccountKeys {
isFromTable := mx.IsVersioned() && keyIndex >= len(mx.AccountKeys)-mx.addressTableLookups.NumLookups()
if isFromTable {
accountKeysBranch.Child(text.Sf("%s (from Address Table Lookup)", text.ColorizeBG(key.String())))
} else {
accountKeysBranch.Child(text.ColorizeBG(key.String()))
}
}
})
if mx.IsVersioned() {
txTree.Child(fmt.Sprintf("AddressTableLookups[len=%v]", len(mx.addressTableLookups))).ParentFunc(func(lookupsBranch treeout.Branches) {
for _, lookup := range mx.addressTableLookups {
lookupsBranch.Child(text.Sf("%s", text.ColorizeBG(lookup.AccountKey.String()))).ParentFunc(func(lookupBranch treeout.Branches) {
lookupBranch.Child(text.Sf("WritableIndexes: %v", lookup.WritableIndexes))
lookupBranch.Child(text.Sf("ReadonlyIndexes: %v", lookup.ReadonlyIndexes))
})
}
})
}
txTree.Child("Header").ParentFunc(func(message treeout.Branches) {
mx.Header.EncodeToTree(message)
})
}
func (header *MessageHeader) EncodeToTree(mxBranch treeout.Branches) {
mxBranch.Child(text.Sf("NumRequiredSignatures: %v", header.NumRequiredSignatures))
mxBranch.Child(text.Sf("NumReadonlySignedAccounts: %v", header.NumReadonlySignedAccounts))
mxBranch.Child(text.Sf("NumReadonlyUnsignedAccounts: %v", header.NumReadonlyUnsignedAccounts))
}
func (mx *Message) MarshalBinary() ([]byte, error) {
switch mx.version {
case MessageVersionV0:
return mx.MarshalV0()
case MessageVersionLegacy:
return mx.MarshalLegacy()
default:
return nil, fmt.Errorf("invalid message version: %d", mx.version)
}
}
func (mx *Message) MarshalLegacy() ([]byte, error) {
buf := []byte{
mx.Header.NumRequiredSignatures,
mx.Header.NumReadonlySignedAccounts,
mx.Header.NumReadonlyUnsignedAccounts,
}
bin.EncodeCompactU16Length(&buf, len(mx.AccountKeys))
for _, key := range mx.AccountKeys {
buf = append(buf, key[:]...)
}
buf = append(buf, mx.RecentBlockhash[:]...)
bin.EncodeCompactU16Length(&buf, len(mx.Instructions))
for _, instruction := range mx.Instructions {
buf = append(buf, byte(instruction.ProgramIDIndex))
bin.EncodeCompactU16Length(&buf, len(instruction.Accounts))
for _, accountIdx := range instruction.Accounts {
buf = append(buf, byte(accountIdx))
}
bin.EncodeCompactU16Length(&buf, len(instruction.Data))
buf = append(buf, instruction.Data...)
}
return buf, nil
}
func (mx *Message) MarshalV0() ([]byte, error) {
buf := []byte{
mx.Header.NumRequiredSignatures,
mx.Header.NumReadonlySignedAccounts,
mx.Header.NumReadonlyUnsignedAccounts,
}
{
numHardcodedKeys := len(mx.AccountKeys) - mx.addressTableLookups.NumLookups()
bin.EncodeCompactU16Length(&buf, numHardcodedKeys)
for _, key := range mx.AccountKeys[:numHardcodedKeys] {
buf = append(buf, key[:]...)
}
buf = append(buf, mx.RecentBlockhash[:]...)
bin.EncodeCompactU16Length(&buf, len(mx.Instructions))
for _, instruction := range mx.Instructions {
buf = append(buf, byte(instruction.ProgramIDIndex))
bin.EncodeCompactU16Length(&buf, len(instruction.Accounts))
for _, accountIdx := range instruction.Accounts {
buf = append(buf, byte(accountIdx))
}
bin.EncodeCompactU16Length(&buf, len(instruction.Data))
buf = append(buf, instruction.Data...)
}
}
versionNum := byte(mx.version) // TODO: what number is this?
if versionNum > 127 {
return nil, fmt.Errorf("invalid message version: %d", mx.version)
}
buf = append([]byte{byte(versionNum + 127)}, buf...)
if mx.addressTableLookups != nil && len(mx.addressTableLookups) > 0 {
// wite length of address table lookups as u8
buf = append(buf, byte(len(mx.addressTableLookups)))
for _, lookup := range mx.addressTableLookups {
// write account pubkey
buf = append(buf, lookup.AccountKey[:]...)
// write writable indexes
bin.EncodeCompactU16Length(&buf, len(lookup.WritableIndexes))
buf = append(buf, lookup.WritableIndexes...)
// write readonly indexes
bin.EncodeCompactU16Length(&buf, len(lookup.ReadonlyIndexes))
buf = append(buf, lookup.ReadonlyIndexes...)
}
} else {
buf = append(buf, 0)
}
return buf, nil
}
func (mx Message) MarshalWithEncoder(encoder *bin.Encoder) error {
out, err := mx.MarshalBinary()
if err != nil {
return err
}
return encoder.WriteBytes(out, false)
}
func (mx Message) ToBase64() string {
out, _ := mx.MarshalBinary()
return base64.StdEncoding.EncodeToString(out)
}
func (mx *Message) UnmarshalWithDecoder(decoder *bin.Decoder) (err error) {
// peek first byte to determine if this is a legacy or v0 message
versionNum, err := decoder.Peek(1)
if err != nil {
return err
}
// TODO: is this the right way to determine if this is a legacy or v0 message?
if versionNum[0] < 127 {
mx.version = MessageVersionLegacy
} else {
mx.version = MessageVersionV0
}
switch mx.version {
case MessageVersionV0:
return mx.UnmarshalV0(decoder)
case MessageVersionLegacy:
return mx.UnmarshalLegacy(decoder)
default:
return fmt.Errorf("invalid message version: %d", mx.version)
}
}
func (mx *Message) UnmarshalBase64(b64 string) error {
b, err := base64.StdEncoding.DecodeString(b64)
if err != nil {
return err
}
return mx.UnmarshalWithDecoder(bin.NewBinDecoder(b))
}
func (mx *Message) resolveLookups(tables map[PublicKey][]PublicKey) (err error) {
// add accounts from the address table lookups
for _, lookup := range mx.addressTableLookups {
table, ok := tables[lookup.AccountKey]
if !ok {
return fmt.Errorf("address table lookup not found for account: %v", lookup.AccountKey)
}
for _, idx := range lookup.WritableIndexes {
if int(idx) >= len(table) {
return fmt.Errorf("address table lookup index out of range: %v", idx)
}
mx.AccountKeys = append(mx.AccountKeys, table[idx])
}
for _, idx := range lookup.ReadonlyIndexes {
if int(idx) >= len(table) {
return fmt.Errorf("address table lookup index out of range: %v", idx)
}
mx.AccountKeys = append(mx.AccountKeys, table[idx])
}
}
return nil
}
func (mx *Message) UnmarshalV0(decoder *bin.Decoder) (err error) {
version, err := decoder.ReadByte()
if err != nil {
return fmt.Errorf("failed to read message version: %w", err)
}
// TODO: check version
mx.version = MessageVersion(version - 127)
// The middle of the message is the same as the legacy message:
err = mx.UnmarshalLegacy(decoder)
if err != nil {
return err
}
// Read address table lookups length:
addressTableLookupsLen, err := decoder.ReadByte()
if err != nil {
return fmt.Errorf("failed to read address table lookups length: %w", err)
}
if addressTableLookupsLen > 0 {
mx.addressTableLookups = make([]MessageAddressTableLookup, addressTableLookupsLen)
for i := 0; i < int(addressTableLookupsLen); i++ {
// read account pubkey
_, err = decoder.Read(mx.addressTableLookups[i].AccountKey[:])
if err != nil {
return fmt.Errorf("failed to read account pubkey: %w", err)
}
// read writable indexes
writableIndexesLen, err := decoder.ReadCompactU16Length()
if err != nil {
return fmt.Errorf("failed to read writable indexes length: %w", err)
}
mx.addressTableLookups[i].WritableIndexes = make([]byte, writableIndexesLen)
_, err = decoder.Read(mx.addressTableLookups[i].WritableIndexes)
if err != nil {
return fmt.Errorf("failed to read writable indexes: %w", err)
}
// read readonly indexes
readonlyIndexesLen, err := decoder.ReadCompactU16Length()
if err != nil {
return fmt.Errorf("failed to read readonly indexes length: %w", err)
}
mx.addressTableLookups[i].ReadonlyIndexes = make([]byte, readonlyIndexesLen)
_, err = decoder.Read(mx.addressTableLookups[i].ReadonlyIndexes)
if err != nil {
return fmt.Errorf("failed to read readonly indexes: %w", err)
}
}
}
return nil
}
func (mx *Message) UnmarshalLegacy(decoder *bin.Decoder) (err error) {
{
mx.Header.NumRequiredSignatures, err = decoder.ReadUint8()
if err != nil {
return fmt.Errorf("unable to decode mx.Header.NumRequiredSignatures: %w", err)
}
mx.Header.NumReadonlySignedAccounts, err = decoder.ReadUint8()
if err != nil {
return fmt.Errorf("unable to decode mx.Header.NumReadonlySignedAccounts: %w", err)
}
mx.Header.NumReadonlyUnsignedAccounts, err = decoder.ReadUint8()
if err != nil {
return fmt.Errorf("unable to decode mx.Header.NumReadonlyUnsignedAccounts: %w", err)
}
}
{
numAccountKeys, err := decoder.ReadCompactU16()
if err != nil {
return fmt.Errorf("unable to decode numAccountKeys: %w", err)
}
mx.AccountKeys = make([]PublicKey, numAccountKeys)
for i := 0; i < numAccountKeys; i++ {
_, err := decoder.Read(mx.AccountKeys[i][:])
if err != nil {
return fmt.Errorf("unable to decode mx.AccountKeys[%d]: %w", i, err)
}
}
}
{
_, err := decoder.Read(mx.RecentBlockhash[:])
if err != nil {
return fmt.Errorf("unable to decode mx.RecentBlockhash: %w", err)
}
}
{
numInstructions, err := decoder.ReadCompactU16()
if err != nil {
return fmt.Errorf("unable to decode numInstructions: %w", err)
}
mx.Instructions = make([]CompiledInstruction, numInstructions)
for instructionIndex := 0; instructionIndex < numInstructions; instructionIndex++ {
programIDIndex, err := decoder.ReadUint8()
if err != nil {
return fmt.Errorf("unable to decode mx.Instructions[%d].ProgramIDIndex: %w", instructionIndex, err)
}
mx.Instructions[instructionIndex].ProgramIDIndex = uint16(programIDIndex)
{
numAccounts, err := decoder.ReadCompactU16()
if err != nil {
return fmt.Errorf("unable to decode numAccounts for ix[%d]: %w", instructionIndex, err)
}
mx.Instructions[instructionIndex].Accounts = make([]uint16, numAccounts)
for i := 0; i < numAccounts; i++ {
accountIndex, err := decoder.ReadUint8()
if err != nil {
return fmt.Errorf("unable to decode accountIndex for ix[%d].Accounts[%d]: %w", instructionIndex, i, err)
}
mx.Instructions[instructionIndex].Accounts[i] = uint16(accountIndex)
}
}
{
dataLen, err := decoder.ReadCompactU16()
if err != nil {
return fmt.Errorf("unable to decode dataLen for ix[%d]: %w", instructionIndex, err)
}
dataBytes, err := decoder.ReadNBytes(dataLen)
if err != nil {
return fmt.Errorf("unable to decode dataBytes for ix[%d]: %w", instructionIndex, err)
}
mx.Instructions[instructionIndex].Data = (Base58)(dataBytes)
}
}
}
return nil
}
func (m Message) checkPreconditions() {
// if this is versioned,
// and there are > 0 lookups,
// but the address table is empty,
// then we can't build the account meta list:
if m.IsVersioned() && m.addressTableLookups.NumLookups() > 0 && (m.addressTables == nil || len(m.addressTables) == 0) {
panic("cannot build account meta list without address tables")
}
}
func (m Message) AccountMetaList() AccountMetaSlice {
m.checkPreconditions()
out := make(AccountMetaSlice, len(m.AccountKeys))
for i, a := range m.AccountKeys {
out[i] = &AccountMeta{
PublicKey: a,
IsSigner: m.IsSigner(a),
IsWritable: m.IsWritable(a),
}
}
// TODO: include accounts from lookup tables
return out
}
func (m Message) IsVersioned() bool {
return m.version != MessageVersionLegacy
}
// Signers returns the pubkeys of all accounts that are signers.
func (m Message) Signers() PublicKeySlice {
m.checkPreconditions()
out := make(PublicKeySlice, 0, len(m.AccountKeys))
for _, a := range m.AccountKeys {
if m.IsSigner(a) {
out = append(out, a)
}
}
return out
}
// Writable returns the pubkeys of all accounts that are writable.
func (m Message) Writable() (out PublicKeySlice) {
m.checkPreconditions()
for _, a := range m.AccountKeys {
if m.IsWritable(a) {
out = append(out, a)
}
}
return out
}
func (m Message) ResolveProgramIDIndex(programIDIndex uint16) (PublicKey, error) {
m.checkPreconditions()
if int(programIDIndex) < len(m.AccountKeys) {
return m.AccountKeys[programIDIndex], nil
}
return PublicKey{}, fmt.Errorf("programID index not found %d", programIDIndex)
}
func (m Message) HasAccount(account PublicKey) bool {
m.checkPreconditions()
for _, a := range m.AccountKeys {
if a.Equals(account) {
return true
}
}
return false
}
func (m Message) IsSigner(account PublicKey) bool {
m.checkPreconditions()
for idx, acc := range m.AccountKeys {
if acc.Equals(account) {
return idx < int(m.Header.NumRequiredSignatures)
}
}
return false
}
func (m Message) IsWritable(account PublicKey) bool {
m.checkPreconditions()
index := 0
found := false
for idx, acc := range m.AccountKeys {
if acc.Equals(account) {
found = true
index = idx
}
}
if !found {
return false
}
h := m.Header
return (index < int(h.NumRequiredSignatures-h.NumReadonlySignedAccounts)) ||
((index >= int(h.NumRequiredSignatures)) && (index < len(m.AccountKeys)-int(h.NumReadonlyUnsignedAccounts)))
}
func (m Message) signerKeys() []PublicKey {
return m.AccountKeys[0:m.Header.NumRequiredSignatures]
}
type MessageHeader struct {
// The total number of signatures required to make the transaction valid.
// The signatures must match the first `numRequiredSignatures` of `message.account_keys`.
NumRequiredSignatures uint8 `json:"numRequiredSignatures"`
// The last numReadonlySignedAccounts of the signed keys are read-only accounts.
// Programs may process multiple transactions that load read-only accounts within
// a single PoH entry, but are not permitted to credit or debit lamports or modify
// account data.
// Transactions targeting the same read-write account are evaluated sequentially.
NumReadonlySignedAccounts uint8 `json:"numReadonlySignedAccounts"`
// The last `numReadonlyUnsignedAccounts` of the unsigned keys are read-only accounts.
NumReadonlyUnsignedAccounts uint8 `json:"numReadonlyUnsignedAccounts"`
}