forked from desperatee/solana-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
address-lookup.go
159 lines (145 loc) · 4.48 KB
/
address-lookup.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
package addresslookuptable
import (
"fmt"
"math"
"github.com/Rabbitcoccus/solana-go"
bin "github.com/gagliardetto/binary"
)
// The serialized size of lookup table metadata.
const (
LOOKUP_TABLE_META_SIZE = 56
LOOKUP_TABLE_MAX_ADDRESSES = 256
)
type AddressLookupTableState struct {
TypeIndex uint32
DeactivationSlot uint64
LastExtendedSlot uint64
LastExtendedSlotStartIndex uint8
Authority *solana.PublicKey
Addresses solana.PublicKeySlice
}
func (a *AddressLookupTableState) UnmarshalWithDecoder(decoder *bin.Decoder) (err error) {
if a.TypeIndex, err = decoder.ReadUint32(bin.LE); err != nil {
return fmt.Errorf("failed to decode TypeIndex: %w", err)
}
if a.DeactivationSlot, err = decoder.ReadUint64(bin.LE); err != nil {
return fmt.Errorf("failed to decode DeactivationSlot: %w", err)
}
if a.LastExtendedSlot, err = decoder.ReadUint64(bin.LE); err != nil {
return fmt.Errorf("failed to decode LastExtendedSlot: %w", err)
}
if a.LastExtendedSlotStartIndex, err = decoder.ReadUint8(); err != nil {
return fmt.Errorf("failed to decode LastExtendedSlotStartIndex: %w", err)
}
has, err := decoder.ReadOption()
if err != nil {
return fmt.Errorf("failed to decode Authority option: %w", err)
}
if has {
var auth solana.PublicKey
if _, err := decoder.Read(auth[:]); err != nil {
return fmt.Errorf("failed to decode Authority: %w", err)
}
a.Authority = &auth
} else {
err = decoder.Discard(32)
if err != nil {
return fmt.Errorf("failed to decode Authority: %w", err)
}
}
serializedAddressesNumBytes := decoder.Len() - LOOKUP_TABLE_META_SIZE
if serializedAddressesNumBytes == 0 {
// No addresses (the lookup table is empty).
a.Addresses = make(solana.PublicKeySlice, 0)
return nil
}
numSerializedAddresses := serializedAddressesNumBytes / 32
if serializedAddressesNumBytes%32 != 0 {
return fmt.Errorf("lookup table is invalid")
}
if numSerializedAddresses > LOOKUP_TABLE_MAX_ADDRESSES {
return fmt.Errorf("lookup table is invalid: max addresses exceeded (%d > %d)", numSerializedAddresses, LOOKUP_TABLE_MAX_ADDRESSES)
}
// Set the position to the start of the serialized addresses.
if err := decoder.SetPosition(LOOKUP_TABLE_META_SIZE); err != nil {
return fmt.Errorf("failed to set position: %w", err)
}
a.Addresses = make(solana.PublicKeySlice, numSerializedAddresses)
for i := 0; i < numSerializedAddresses; i++ {
var address solana.PublicKey
numRead, err := decoder.Read(address[:])
if err != nil {
return fmt.Errorf("failed to read addresses[%d]: %w", i, err)
}
if numRead != 32 {
return fmt.Errorf("failed to read addresses[%d]: expected to read 32, but read %d", i, numRead)
}
a.Addresses[i] = address
}
if decoder.Remaining() != 0 {
return fmt.Errorf("failed to read all addresses: remaining %d bytes", decoder.Remaining())
}
return nil
}
func (a AddressLookupTableState) MarshalWithEncoder(encoder *bin.Encoder) error {
if err := encoder.WriteUint32(a.TypeIndex, bin.LE); err != nil {
return err
}
if err := encoder.WriteUint64(a.DeactivationSlot, bin.LE); err != nil {
return err
}
if err := encoder.WriteUint64(a.LastExtendedSlot, bin.LE); err != nil {
return err
}
if err := encoder.WriteUint8(a.LastExtendedSlotStartIndex); err != nil {
return err
}
if a.Authority != nil {
if err := encoder.WriteOption(true); err != nil {
return err
}
if _, err := encoder.Write(a.Authority[:]); err != nil {
return err
}
} else {
if err := encoder.WriteOption(false); err != nil {
return err
}
if _, err := encoder.Write(make([]byte, 32)); err != nil {
return err
}
}
if _, err := encoder.Write(make([]byte, 2)); err != nil {
return err
}
for _, address := range a.Addresses {
if _, err := encoder.Write(address[:]); err != nil {
return err
}
}
return nil
}
func (a AddressLookupTableState) IsActive() bool {
return a.DeactivationSlot == math.MaxUint64
}
type KeyedAddressLookupTable struct {
Key solana.PublicKey
State AddressLookupTableState
}
func NewKeyedAddressLookupTable(key solana.PublicKey) *KeyedAddressLookupTable {
return &KeyedAddressLookupTable{
Key: key,
}
}
func (a *KeyedAddressLookupTable) UnmarshalWithDecoder(decoder *bin.Decoder) error {
if err := a.State.UnmarshalWithDecoder(decoder); err != nil {
return err
}
return nil
}
func (a KeyedAddressLookupTable) MarshalWithEncoder(encoder *bin.Encoder) error {
if err := a.State.MarshalWithEncoder(encoder); err != nil {
return err
}
return nil
}