Repository navigation
Expand file tree
/
Copy pathmessage_builder.go
More file actions
389 lines (358 loc) · 12.1 KB
/
Copy pathmessage_builder.go
File metadata and controls
389 lines (358 loc) · 12.1 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
package solana
import (
"fmt"
"sort"
)
// LoadedAddressLookupTable holds a resolved Address Lookup Table: its
// on-chain key and the full ordered list of addresses stored in the table
// (slot 0, 1, 2, …). Obtain this data by calling the RPC method
// getAddressLookupTable (or getAccountInfo on the ALT account and parsing
// the state). Pass a slice of LoadedAddressLookupTable to NewMessageV0 or
// Builder.BuildV0 so the builder can map instruction accounts to their
// table slot indices.
type LoadedAddressLookupTable struct {
AccountKey PublicKey // On-chain ALT address
Addresses []PublicKey // All addresses in the table, ordered by slot index
}
// staticEntry tracks one static account's cumulative signer/writable
// roles, plus the order it first appeared in the instruction stream.
// firstSeen preserves the solana-web3.js-compatible bucket ordering.
type staticEntry struct {
pubkey PublicKey
signer bool
writable bool
firstSeen int
}
// staticEntries is a deduplicating collector for static account keys.
// addOrUpdate is OR-semantics: the same key seen as signer in one
// instruction and non-signer in another ends up signer.
type staticEntries struct {
m map[PublicKey]*staticEntry
counter int
}
func newStaticEntries() *staticEntries {
return &staticEntries{m: make(map[PublicKey]*staticEntry)}
}
func (s *staticEntries) addOrUpdate(pk PublicKey, signer, writable bool) {
e, ok := s.m[pk]
if !ok {
e = &staticEntry{pubkey: pk, firstSeen: s.counter}
s.counter++
s.m[pk] = e
}
e.signer = e.signer || signer
e.writable = e.writable || writable
}
// bucketAndOrder returns the static account list sorted into the four
// Solana role buckets — signer-writable, signer-readonly,
// non-signer-writable, non-signer-readonly — plus the matching
// MessageHeader counters. Within each bucket entries are ordered by
// firstSeen so output is stable and matches solana-web3.js.
func (s *staticEntries) bucketAndOrder() ([]PublicKey, MessageHeader) {
var bucketCounts [4]int
for _, e := range s.m {
bucketCounts[bucketOf(e)]++
}
var buckets [4][]*staticEntry
for i := range buckets {
buckets[i] = make([]*staticEntry, 0, bucketCounts[i])
}
for _, e := range s.m {
b := bucketOf(e)
buckets[b] = append(buckets[b], e)
}
for i := range buckets {
sort.SliceStable(buckets[i], func(a, b int) bool {
return buckets[i][a].firstSeen < buckets[i][b].firstSeen
})
}
keys := make([]PublicKey, 0, len(s.m))
for _, b := range buckets {
for _, e := range b {
keys = append(keys, e.pubkey)
}
}
header := MessageHeader{
NumRequiredSignatures: uint8(len(buckets[0]) + len(buckets[1])),
NumReadonlySignedAccounts: uint8(len(buckets[1])),
NumReadonlyUnsignedAccounts: uint8(len(buckets[3])),
}
return keys, header
}
// bucketOf maps a staticEntry to its role bucket index:
// 0=signer-writable, 1=signer-readonly, 2=non-signer-writable,
// 3=non-signer-readonly.
func bucketOf(e *staticEntry) int {
switch {
case e.signer && e.writable:
return 0
case e.signer && !e.writable:
return 1
case !e.signer && e.writable:
return 2
default:
return 3
}
}
// compileInstructions resolves each Instruction's program id and
// AccountMetas to indices into keyIndex, producing the compiled form
// stored in the message. fnName names the calling builder for error
// messages.
func compileInstructions(instructions []Instruction, keyIndex map[PublicKey]int, fnName string) ([]CompiledInstruction, error) {
compiled := make([]CompiledInstruction, 0, len(instructions))
for _, ix := range instructions {
programIdx, ok := keyIndex[ix.ProgramID()]
if !ok {
return nil, fmt.Errorf("solana: %s: program id %s missing from key index", fnName, ix.ProgramID())
}
metas := ix.Accounts()
accIdxs := make(Uint8Slice, len(metas))
for i, m := range metas {
idx, ok := keyIndex[m.PublicKey]
if !ok {
return nil, fmt.Errorf("solana: %s: account %s missing from key index", fnName, m.PublicKey)
}
accIdxs[i] = uint8(idx)
}
data, err := ix.Data()
if err != nil {
return nil, fmt.Errorf("solana: %s: instruction data: %w", fnName, err)
}
compiled = append(compiled, CompiledInstruction{
ProgramIDIndex: uint8(programIdx),
Accounts: accIdxs,
Data: data,
})
}
return compiled, nil
}
// NewMessage compiles a slice of typed Instructions into a legacy
// Message ready to be signed by Transaction.Sign.
//
// It deduplicates account keys across all instructions, orders them
// by role according to the Solana protocol, computes the header
// counters, and resolves each instruction's account list to
// positional indices into the deduplicated key array.
//
// Account ordering within the resulting Message follows the Solana
// convention:
//
// 1. payer (always first, signer + writable)
// 2. other signer-writable accounts
// 3. signer-readonly accounts
// 4. non-signer-writable accounts
// 5. non-signer-readonly accounts (including program ids)
//
// Within each bucket, accounts are ordered by the order they first
// appear in the instructions slice, which matches solana-web3.js
// and produces stable, review-friendly output.
//
// The payer is always placed first in the AccountKeys slice and is
// guaranteed to be signer + writable regardless of how individual
// instructions reference it. Roles elsewhere are cumulative: if
// the same account appears as signer in one instruction and
// non-signer in another, it ends up in the signer bucket.
func NewMessage(payer PublicKey, instructions []Instruction, recentBlockhash Hash) (*Message, error) {
if len(instructions) == 0 {
return nil, fmt.Errorf("solana: NewMessage: no instructions")
}
entries := newStaticEntries()
entries.addOrUpdate(payer, true, true) // payer always signer+writable
for _, ix := range instructions {
if ix == nil {
return nil, fmt.Errorf("solana: NewMessage: nil instruction")
}
for _, meta := range ix.Accounts() {
if meta == nil {
return nil, fmt.Errorf("solana: NewMessage: nil AccountMeta")
}
entries.addOrUpdate(meta.PublicKey, meta.IsSigner, meta.IsWritable)
}
entries.addOrUpdate(ix.ProgramID(), false, false)
}
accountKeys, header := entries.bucketAndOrder()
if len(accountKeys) > 256 {
return nil, fmt.Errorf("solana: NewMessage: %d account keys exceeds Solana maximum of 256", len(accountKeys))
}
keyIndex := make(map[PublicKey]int, len(accountKeys))
for i, k := range accountKeys {
keyIndex[k] = i
}
compiled, err := compileInstructions(instructions, keyIndex, "NewMessage")
if err != nil {
return nil, err
}
return &Message{
Version: MessageVersionLegacy,
Header: header,
AccountKeys: accountKeys,
RecentBlockhash: recentBlockhash,
Instructions: compiled,
}, nil
}
// NewMessageV0 compiles a v0 versioned Message that uses Address Lookup
// Tables to extend the account limit beyond the legacy 35-account cap.
//
// tables contains resolved ALTs: each entry holds the table's on-chain
// address and the full ordered list of addresses stored in it. The builder
// inspects every instruction account and, when the account appears in a
// table AND is not required to sign (signer accounts must always be static),
// routes it through that table rather than the static account list.
//
// Account index layout in the compiled message:
//
// [0 .. staticLen-1] — static accounts (same role ordering as NewMessage)
// [staticLen .. +Σwritable] — writable table accounts, table by table
// [above .. +Σreadonly] — readonly table accounts, table by table
//
// If an account appears in multiple tables the first matching table wins.
func NewMessageV0(payer PublicKey, instructions []Instruction, recentBlockhash Hash, tables []LoadedAddressLookupTable) (*Message, error) {
if len(instructions) == 0 {
return nil, fmt.Errorf("solana: NewMessageV0: no instructions")
}
// Reverse map pubkey → (tableIdx, slotIdx) from all tables.
type tableSlot struct {
tableIdx int
slotIdx int
}
tableIndex := make(map[PublicKey]tableSlot)
for ti, t := range tables {
for si, addr := range t.Addresses {
if _, exists := tableIndex[addr]; !exists {
tableIndex[addr] = tableSlot{tableIdx: ti, slotIdx: si}
}
}
}
// Signers must always be static, even if present in a table.
signerSet := make(map[PublicKey]bool)
signerSet[payer] = true
for _, ix := range instructions {
if ix == nil {
return nil, fmt.Errorf("solana: NewMessageV0: nil instruction")
}
for _, meta := range ix.Accounts() {
if meta == nil {
return nil, fmt.Errorf("solana: NewMessageV0: nil AccountMeta")
}
if meta.IsSigner {
signerSet[meta.PublicKey] = true
}
}
}
entries := newStaticEntries()
entries.addOrUpdate(payer, true, true)
tableAccounts := make([]map[int]bool, len(tables)) // tableIdx → slotIdx → writable
for i := range tableAccounts {
tableAccounts[i] = make(map[int]bool)
}
addOrUpdateTable := func(pk PublicKey, writable bool) {
slot := tableIndex[pk]
prev := tableAccounts[slot.tableIdx][slot.slotIdx]
tableAccounts[slot.tableIdx][slot.slotIdx] = prev || writable
}
for _, ix := range instructions {
for _, meta := range ix.Accounts() {
_, inTable := tableIndex[meta.PublicKey]
if inTable && !signerSet[meta.PublicKey] {
addOrUpdateTable(meta.PublicKey, meta.IsWritable)
} else {
entries.addOrUpdate(meta.PublicKey, meta.IsSigner, meta.IsWritable)
}
}
// Program IDs are always static.
entries.addOrUpdate(ix.ProgramID(), false, false)
}
staticKeys, header := entries.bucketAndOrder()
if len(staticKeys) > 256 {
return nil, fmt.Errorf("solana: NewMessageV0: %d static account keys exceeds maximum of 256", len(staticKeys))
}
// Build full index map matching the Solana resolution order: static
// keys, then ALL writable table accounts across every table, then ALL
// readonly table accounts across every table. This must match
// Message.ResolvedAccountKeys; per-table interleaving would silently
// corrupt compiled instruction account indices.
keyIndex := make(map[PublicKey]int, len(staticKeys))
for i, k := range staticKeys {
keyIndex[k] = i
}
type tableSlots struct {
writable, readonly []int
}
perTable := make([]tableSlots, len(tables))
emit := make([]bool, len(tables))
for ti := range tables {
slots := tableAccounts[ti]
if len(slots) == 0 {
continue
}
emit[ti] = true
ts := tableSlots{}
for slotIdx, writable := range slots {
if writable {
ts.writable = append(ts.writable, slotIdx)
} else {
ts.readonly = append(ts.readonly, slotIdx)
}
}
sort.Ints(ts.writable)
sort.Ints(ts.readonly)
perTable[ti] = ts
}
next := len(staticKeys)
for ti, t := range tables {
if !emit[ti] {
continue
}
for _, s := range perTable[ti].writable {
keyIndex[t.Addresses[s]] = next
next++
}
}
for ti, t := range tables {
if !emit[ti] {
continue
}
for _, s := range perTable[ti].readonly {
keyIndex[t.Addresses[s]] = next
next++
}
}
// Instruction account indices are u8, so the total account count
// (static + all ALT-resolved) must fit in 0..255. Without this check
// compileInstructions' uint8(idx) silently truncates indices >=256,
// producing a message that references the wrong accounts.
if next > 256 {
return nil, fmt.Errorf("solana: NewMessageV0: %d total accounts (static + lookup) exceeds maximum of 256", next)
}
var lookups []MessageAddressTableLookup
for ti, t := range tables {
if !emit[ti] {
continue
}
writableIdxs := make([]uint8, len(perTable[ti].writable))
for i, s := range perTable[ti].writable {
writableIdxs[i] = uint8(s)
}
readonlyIdxs := make([]uint8, len(perTable[ti].readonly))
for i, s := range perTable[ti].readonly {
readonlyIdxs[i] = uint8(s)
}
lookups = append(lookups, MessageAddressTableLookup{
AccountKey: t.AccountKey,
WritableIndexes: writableIdxs,
ReadonlyIndexes: readonlyIdxs,
})
}
compiled, err := compileInstructions(instructions, keyIndex, "NewMessageV0")
if err != nil {
return nil, err
}
return &Message{
Version: MessageVersion0,
Header: header,
AccountKeys: staticKeys,
RecentBlockhash: recentBlockhash,
Instructions: compiled,
AddressTableLookups: lookups,
}, nil
}