-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathaccounts_pool.go
230 lines (183 loc) · 5.32 KB
/
accounts_pool.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
package splitter_token
import (
"bytes"
"crypto/ed25519"
"fmt"
"strconv"
"github.com/mr-tron/base58/base58"
)
const (
MaxNameLength = 32
MaxHistory = 32
)
// Arguments used to create {@link PoolAccount}
type PoolAccount struct {
DataVersion DataVersion
Authority ed25519.PublicKey
Mint ed25519.PublicKey
Vault ed25519.PublicKey
VaultBump uint8
Name string
HistoryList []Hash
CurrentIndex uint8
MerkleTree *MerkleTree
}
const PoolAccountSize = (8 + // discriminator
1 + // data_version
32 + // authority
32 + // mint
32 + // vault
1 + // vault_bump
+MaxNameLength + // name
4 + // history_list
1 + // current_index
1) // merkletree_levels
// getPoolAccountSize is the size of a {@link PoolAccount} in bytes
func getPoolAccountSize(levels uint8, num_recent_roots uint8) int {
return (PoolAccountSize) +
getHistorySize(num_recent_roots) - 1 +
getMerkleTreeSize(levels) - 1
}
func getHistorySize(num_recent_roots uint8) int {
return 32 * int(num_recent_roots)
}
var poolAccountDiscriminator = []byte{241, 154, 109, 4, 17, 177, 109, 188}
// Holds the data for the {@link PoolAccount} Account and provides de/serialization
// functionality for that data
func NewPoolAccount(
dataVersion DataVersion,
authority ed25519.PublicKey,
mint ed25519.PublicKey,
vault ed25519.PublicKey,
vaultBump uint8,
name string,
historyList []Hash,
currentIndex uint8,
merkleTree *MerkleTree,
) *PoolAccount {
return &PoolAccount{
DataVersion: dataVersion,
Authority: authority,
Mint: mint,
Vault: vault,
VaultBump: vaultBump,
Name: name,
HistoryList: historyList,
CurrentIndex: currentIndex,
MerkleTree: merkleTree,
}
}
// Clones a {@link PoolAccount} instance.
func (obj *PoolAccount) Clone() *PoolAccount {
return NewPoolAccount(
obj.DataVersion,
obj.Authority,
obj.Mint,
obj.Vault,
obj.VaultBump,
obj.Name,
obj.HistoryList,
obj.CurrentIndex,
obj.MerkleTree.Clone(),
)
}
func (obj *PoolAccount) String() string {
var authority, mint, vault string
if obj.Authority != nil {
authority = base58.Encode(obj.Authority)
}
if obj.Mint != nil {
mint = base58.Encode(obj.Mint)
}
if obj.Vault != nil {
vault = base58.Encode(obj.Vault)
}
historyListStr := "["
for _, hash := range obj.HistoryList {
historyListStr += fmt.Sprintf("'%s', ", hash.ToString())
}
historyListStr += "]"
return "PoolAccount {" +
" data_version='" + strconv.Itoa(int(obj.DataVersion)) + "'" +
", authority='" + authority + "'" +
", mint='" + mint + "'" +
", vault='" + vault + "'" +
", vault_bump='" + strconv.Itoa(int(obj.VaultBump)) + "'" +
", name='" + obj.Name + "'" +
", current_index='" + strconv.Itoa(int(obj.CurrentIndex)) + "'" +
", history_list=" + historyListStr + "" +
", merkle_tree='" + obj.MerkleTree.ToString() + "'" +
"}"
}
// Serializes the {@link PoolAccount} into a Buffer.
// @returns the created []byte buffer
func (obj *PoolAccount) Marshal() []byte {
data := make([]byte, getPoolAccountSize(obj.MerkleTree.Levels, MaxHistory))
var offset int
putDiscriminator(data, poolAccountDiscriminator, &offset)
putDataVersion(data, obj.DataVersion, &offset)
putKey(data, obj.Authority, &offset)
putKey(data, obj.Mint, &offset)
putKey(data, obj.Vault, &offset)
putUint8(data, obj.VaultBump, &offset)
putName(data, obj.Name, &offset)
putUint32(data, MaxHistory, &offset)
for _, recentRoot := range obj.HistoryList {
putHash(data, recentRoot[:], &offset)
}
putUint8(data, obj.CurrentIndex, &offset)
merkleTree := obj.MerkleTree.Marshal()
copy(data[offset:], merkleTree)
return data
}
// Deserializes the {@link PoolAccount} from the provided data Buffer.
// @returns an error if the deserialize operation was unsuccessful.
func (obj *PoolAccount) Unmarshal(data []byte) error {
if len(data) < PoolAccountSize {
return ErrInvalidInstructionData
}
var offset int
var discriminator []byte
getDiscriminator(data, &discriminator, &offset)
if !bytes.Equal(discriminator, poolAccountDiscriminator) {
return ErrInvalidInstructionData
}
getDataVersion(data, &obj.DataVersion, &offset)
if obj.DataVersion != DataVersion1 {
return ErrInvalidInstructionData
}
getKey(data, &obj.Authority, &offset)
getKey(data, &obj.Mint, &offset)
getKey(data, &obj.Vault, &offset)
getUint8(data, &obj.VaultBump, &offset)
getName(data, &obj.Name, &offset)
var historyListSize uint32
getUint32(data, &historyListSize, &offset)
if historyListSize != MaxHistory {
return ErrInvalidInstructionData
}
obj.HistoryList = make([]Hash, MaxHistory)
for i := uint8(0); i < MaxHistory; i++ {
getHash(data, &obj.HistoryList[i], &offset)
}
getUint8(data, &obj.CurrentIndex, &offset)
obj.MerkleTree = &MerkleTree{}
var merkleOffset = offset
getUint8(data, &obj.MerkleTree.Levels, &offset)
// Check if the dynamic length of the merkle tree is correct
if len(data) < getPoolAccountSize(obj.MerkleTree.Levels, MaxHistory) {
return ErrInvalidInstructionData
}
return obj.MerkleTree.Unmarshal(data[merkleOffset:])
}
func putName(dst []byte, src string, offset *int) {
putUint32(dst, uint32(len(src)), offset)
copy(dst[*offset:*offset+int(len(src))], src)
*offset += len(src)
}
func getName(data []byte, dst *string, offset *int) {
var length uint32
getUint32(data, &length, offset)
*dst = string(data[*offset : *offset+int(length)])
*offset += int(length)
}