summaryrefslogtreecommitdiff
path: root/txnsync/encodedgroupstypes.go
blob: 727d67ee877e7d821186ab852de59fa269b9554a (plain)
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
// Copyright (C) 2019-2021 Algorand, Inc.
// This file is part of go-algorand
//
// go-algorand is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// go-algorand is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with go-algorand.  If not, see <https://www.gnu.org/licenses/>.

package txnsync

import (
	"errors"

	"github.com/algorand/go-algorand/crypto"
	"github.com/algorand/go-algorand/crypto/compactcert"
	"github.com/algorand/go-algorand/data/basics"
	"github.com/algorand/go-algorand/data/pooldata"
	"github.com/algorand/go-algorand/protocol"
)

const maxEncodedTransactionGroups = 30000
const maxEncodedTransactionGroupEntries = 30000
const maxBitmaskSize = (maxEncodedTransactionGroupEntries+7)/8 + 1
const maxSignatureBytes = maxEncodedTransactionGroupEntries * len(crypto.Signature{})
const maxAddressBytes = maxEncodedTransactionGroupEntries * crypto.DigestSize

var errInvalidTxType = errors.New("invalid txtype")

//msgp:allocbound txnGroups maxEncodedTransactionGroupEntries
type txnGroups pooldata.SignedTxnSlice //nolint:unused

// old data structure for encoding (only used for testing)
type txGroupsEncodingStubOld struct { //nolint:unused
	_struct struct{} `codec:",omitempty,omitemptyarray"` //nolint:structcheck,unused

	TxnGroups []txnGroups `codec:"t,allocbound=maxEncodedTransactionGroups"`
}

type txGroupsEncodingStub struct {
	_struct struct{} `codec:",omitempty,omitemptyarray"` //nolint:structcheck,unused

	TotalTransactionsCount uint64 `codec:"ttc"`
	TransactionGroupCount  uint64 `codec:"tgc"`
	TransactionGroupSizes  []byte `codec:"tgs,allocbound=maxEncodedTransactionGroups"`

	encodedSignedTxns
}

type encodedSignedTxns struct {
	_struct struct{} `codec:",omitempty,omitemptyarray"` //nolint:structcheck,unused

	Sig        []byte  `codec:"sig,allocbound=maxSignatureBytes"`
	BitmaskSig bitmask `codec:"sigbm"`

	encodedMsigs
	encodedLsigs

	AuthAddr        []byte  `codec:"sgnr,allocbound=maxAddressBytes"`
	BitmaskAuthAddr bitmask `codec:"sgnrbm"`

	encodedTxns
}

type encodedMsigs struct {
	_struct struct{} `codec:",omitempty,omitemptyarray"` //nolint:structcheck,unused

	Version          []byte  `codec:"msigv,allocbound=maxEncodedTransactionGroups"`
	BitmaskVersion   bitmask `codec:"msigvbm"`
	Threshold        []byte  `codec:"msigthr,allocbound=maxEncodedTransactionGroups"`
	BitmaskThreshold bitmask `codec:"msigthrbm"`
	// splitting subsigs further make the code much more complicated / does not give gains
	Subsigs        [][]crypto.MultisigSubsig `codec:"subsig,allocbound=maxEncodedTransactionGroups,allocbound=crypto.MaxMultisig"`
	BitmaskSubsigs bitmask                   `codec:"subsigsbm"`
}

type encodedLsigs struct {
	_struct struct{} `codec:",omitempty,omitemptyarray"` //nolint:structcheck,unused

	Logic            [][]byte   `codec:"lsigl,allocbound=maxEncodedTransactionGroups,allocbound=config.MaxLogicSigMaxSize"`
	BitmaskLogic     bitmask    `codec:"lsiglbm"`
	LogicArgs        [][][]byte `codec:"lsigarg,allocbound=maxEncodedTransactionGroups,allocbound=transactions.EvalMaxArgs,allocbound=config.MaxLogicSigMaxSize"`
	BitmaskLogicArgs bitmask    `codec:"lsigargbm"`
}

type encodedTxns struct {
	_struct struct{} `codec:",omitempty,omitemptyarray"` //nolint:structcheck,unused

	TxType        []byte  `codec:"type,allocbound=maxEncodedTransactionGroups"`
	BitmaskTxType bitmask `codec:"typebm"`
	TxTypeOffset  byte    `codec:"typeo"`

	encodedTxnHeaders
	encodedKeyregTxnFields
	encodedPaymentTxnFields
	encodedAssetConfigTxnFields
	encodedAssetTransferTxnFields
	encodedAssetFreezeTxnFields
	encodedApplicationCallTxnFields
	encodedCompactCertTxnFields
}

type encodedTxnHeaders struct {
	_struct struct{} `codec:",omitempty,omitemptyarray"` //nolint:structcheck,unused

	Sender            []byte              `codec:"snd,allocbound=maxAddressBytes"`
	BitmaskSender     bitmask             `codec:"sndbm"`
	Fee               []basics.MicroAlgos `codec:"fee,allocbound=maxEncodedTransactionGroups"`
	BitmaskFee        bitmask             `codec:"feebm"`
	FirstValid        []basics.Round      `codec:"fv,allocbound=maxEncodedTransactionGroups"`
	BitmaskFirstValid bitmask             `codec:"fvbm"`
	LastValid         []basics.Round      `codec:"lv,allocbound=maxEncodedTransactionGroups"`
	BitmaskLastValid  bitmask             `codec:"lvbm"`
	Note              [][]byte            `codec:"note,allocbound=maxEncodedTransactionGroups,allocbound=config.MaxTxnNoteBytes"`
	BitmaskNote       bitmask             `codec:"notebm"`
	BitmaskGenesisID  bitmask             `codec:"genbm"`

	BitmaskGroup bitmask `codec:"grpbm"`

	Lease        []byte  `codec:"lx,allocbound=maxAddressBytes"`
	BitmaskLease bitmask `codec:"lxbm"`

	RekeyTo        []byte  `codec:"rekey,allocbound=maxAddressBytes"`
	BitmaskRekeyTo bitmask `codec:"rekeybm"`
}

type encodedKeyregTxnFields struct {
	_struct struct{} `codec:",omitempty,omitemptyarray"` //nolint:structcheck,unused

	VotePK                  []byte         `codec:"votekey,allocbound=maxAddressBytes"`
	SelectionPK             []byte         `codec:"selkey,allocbound=maxAddressBytes"`
	VoteFirst               []basics.Round `codec:"votefst,allocbound=maxEncodedTransactionGroups"`
	BitmaskVoteFirst        bitmask        `codec:"votefstbm"`
	VoteLast                []basics.Round `codec:"votelst,allocbound=maxEncodedTransactionGroups"`
	BitmaskVoteLast         bitmask        `codec:"votelstbm"`
	VoteKeyDilution         []uint64       `codec:"votekd,allocbound=maxEncodedTransactionGroups"`
	BitmaskKeys             bitmask        `codec:"votekbm"`
	BitmaskNonparticipation bitmask        `codec:"nonpartbm"`
}

type encodedPaymentTxnFields struct {
	_struct struct{} `codec:",omitempty,omitemptyarray"` //nolint:structcheck,unused

	Receiver        []byte              `codec:"rcv,allocbound=maxAddressBytes"`
	BitmaskReceiver bitmask             `codec:"rcvbm"`
	Amount          []basics.MicroAlgos `codec:"amt,allocbound=maxEncodedTransactionGroups"`
	BitmaskAmount   bitmask             `codec:"amtbm"`

	CloseRemainderTo        []byte  `codec:"close,allocbound=maxAddressBytes"`
	BitmaskCloseRemainderTo bitmask `codec:"closebm"`
}

type encodedAssetConfigTxnFields struct {
	_struct struct{} `codec:",omitempty,omitemptyarray"` //nolint:structcheck,unused

	ConfigAsset        []basics.AssetIndex `codec:"caid,allocbound=maxEncodedTransactionGroups"`
	BitmaskConfigAsset bitmask             `codec:"caidbm"`

	encodedAssetParams
}

type encodedAssetParams struct {
	_struct struct{} `codec:",omitempty,omitemptyarray"` //nolint:structcheck,unused

	Total        []uint64 `codec:"t,allocbound=maxEncodedTransactionGroups"`
	BitmaskTotal bitmask  `codec:"tbm"`

	Decimals        []uint32 `codec:"dc,allocbound=maxEncodedTransactionGroups"`
	BitmaskDecimals bitmask  `codec:"dcbm"`

	BitmaskDefaultFrozen bitmask `codec:"dfbm"`

	UnitName        []string `codec:"un,allocbound=maxEncodedTransactionGroups"`
	BitmaskUnitName bitmask  `codec:"unbm"`

	AssetName        []string `codec:"an,allocbound=maxEncodedTransactionGroups"`
	BitmaskAssetName bitmask  `codec:"anbm"`

	URL        []string `codec:"au,allocbound=maxEncodedTransactionGroups"`
	BitmaskURL bitmask  `codec:"aubm"`

	MetadataHash        []byte  `codec:"am,allocbound=maxAddressBytes"`
	BitmaskMetadataHash bitmask `codec:"ambm"`

	Manager        []byte  `codec:"m,allocbound=maxAddressBytes"`
	BitmaskManager bitmask `codec:"mbm"`

	Reserve        []byte  `codec:"r,allocbound=maxAddressBytes"`
	BitmaskReserve bitmask `codec:"rbm"`

	Freeze        []byte  `codec:"f,allocbound=maxAddressBytes"`
	BitmaskFreeze bitmask `codec:"fbm"`

	Clawback        []byte  `codec:"c,allocbound=maxAddressBytes"`
	BitmaskClawback bitmask `codec:"cbm"`
}

type encodedAssetTransferTxnFields struct {
	_struct struct{} `codec:",omitempty,omitemptyarray"` //nolint:structcheck,unused

	XferAsset        []basics.AssetIndex `codec:"xaid,allocbound=maxEncodedTransactionGroups"`
	BitmaskXferAsset bitmask             `codec:"xaidbm"`

	AssetAmount        []uint64 `codec:"aamt,allocbound=maxEncodedTransactionGroups"`
	BitmaskAssetAmount bitmask  `codec:"aamtbm"`

	AssetSender        []byte  `codec:"asnd,allocbound=maxAddressBytes"`
	BitmaskAssetSender bitmask `codec:"asndbm"`

	AssetReceiver        []byte  `codec:"arcv,allocbound=maxAddressBytes"`
	BitmaskAssetReceiver bitmask `codec:"arcvbm"`

	AssetCloseTo        []byte  `codec:"aclose,allocbound=maxAddressBytes"`
	BitmaskAssetCloseTo bitmask `codec:"aclosebm"`
}

type encodedAssetFreezeTxnFields struct {
	_struct struct{} `codec:",omitempty,omitemptyarray"` //nolint:structcheck,unused

	FreezeAccount        []byte  `codec:"fadd,allocbound=maxAddressBytes"`
	BitmaskFreezeAccount bitmask `codec:"faddbm"`

	FreezeAsset        []basics.AssetIndex `codec:"faid,allocbound=maxEncodedTransactionGroups"`
	BitmaskFreezeAsset bitmask             `codec:"faidbm"`

	BitmaskAssetFrozen bitmask `codec:"afrzbm"`
}

//msgp:allocbound applicationArgs transactions.EncodedMaxApplicationArgs
type applicationArgs [][]byte

//msgp:allocbound addresses transactions.EncodedMaxAccounts
type addresses []basics.Address

//msgp:allocbound appIndices transactions.EncodedMaxForeignApps
type appIndices []basics.AppIndex

//msgp:allocbound assetIndices transactions.EncodedMaxForeignAssets
type assetIndices []basics.AssetIndex

//msgp:allocbound program config.MaxAvailableAppProgramLen
type program []byte

type encodedApplicationCallTxnFields struct {
	_struct struct{} `codec:",omitempty,omitemptyarray"` //nolint:structcheck,unused

	ApplicationID        []basics.AppIndex `codec:"apid,allocbound=maxEncodedTransactionGroups"`
	BitmaskApplicationID bitmask           `codec:"apidbm"`

	OnCompletion        []byte  `codec:"apan,allocbound=maxEncodedTransactionGroups"`
	BitmaskOnCompletion bitmask `codec:"apanbm"`

	ApplicationArgs        []applicationArgs `codec:"apaa,allocbound=maxEncodedTransactionGroups"`
	BitmaskApplicationArgs bitmask           `codec:"apaabm"`

	Accounts        []addresses `codec:"apat,allocbound=maxEncodedTransactionGroups"`
	BitmaskAccounts bitmask     `codec:"apatbm"`

	ForeignApps        []appIndices `codec:"apfa,allocbound=maxEncodedTransactionGroups"`
	BitmaskForeignApps bitmask      `codec:"apfabm"`

	ForeignAssets        []assetIndices `codec:"apas,allocbound=maxEncodedTransactionGroups"`
	BitmaskForeignAssets bitmask        `codec:"apasbm"`

	LocalNumUint             []uint64 `codec:"lnui,allocbound=maxEncodedTransactionGroups"`
	BitmaskLocalNumUint      bitmask  `codec:"lnuibm"`
	LocalNumByteSlice        []uint64 `codec:"lnbs,allocbound=maxEncodedTransactionGroups"`
	BitmaskLocalNumByteSlice bitmask  `codec:"lnbsbm"`

	GlobalNumUint             []uint64 `codec:"gnui,allocbound=maxEncodedTransactionGroups"`
	BitmaskGlobalNumUint      bitmask  `codec:"gnuibm"`
	GlobalNumByteSlice        []uint64 `codec:"gnbs,allocbound=maxEncodedTransactionGroups"`
	BitmaskGlobalNumByteSlice bitmask  `codec:"gnbsbm"`

	ApprovalProgram        []program `codec:"apap,allocbound=maxEncodedTransactionGroups"`
	BitmaskApprovalProgram bitmask   `codec:"apapbm"`

	ClearStateProgram        []program `codec:"apsu,allocbound=maxEncodedTransactionGroups"`
	BitmaskClearStateProgram bitmask   `codec:"apsubm"`

	ExtraProgramPages        []uint32 `codec:"apep,allocbound=maxEncodedTransactionGroups"`
	BitmaskExtraProgramPages bitmask  `codec:"apepbm"`
}

type encodedCompactCertTxnFields struct {
	_struct struct{} `codec:",omitempty,omitemptyarray"` //nolint:structcheck,unused

	CertRound        []basics.Round `codec:"certrnd,allocbound=maxEncodedTransactionGroups"`
	BitmaskCertRound bitmask        `codec:"certrndbm"`

	CertType        []protocol.CompactCertType `codec:"certtype,allocbound=maxEncodedTransactionGroups"`
	BitmaskCertType bitmask                    `codec:"certtypebm"`

	encodedCert
}

//msgp:allocbound certProofs compactcert.MaxProofDigests
type certProofs []crypto.Digest

//msgp:allocbound revealMap compactcert.MaxReveals
type revealMap map[uint64]compactcert.Reveal

// SortUint64 implements sorting by uint64 keys for
// canonical encoding of maps in msgpack format.
type SortUint64 = compactcert.SortUint64

type encodedCert struct {
	_struct struct{} `codec:",omitempty,omitemptyarray"` //nolint:structcheck,unused

	SigCommit        []byte  `codec:"certc,allocbound=maxAddressBytes"`
	BitmaskSigCommit bitmask `codec:"certcbm"`

	SignedWeight        []uint64 `codec:"certw,allocbound=maxEncodedTransactionGroups"`
	BitmaskSignedWeight bitmask  `codec:"certwbm"`

	SigProofs        []certProofs `codec:"certS,allocbound=maxEncodedTransactionGroups"`
	BitmaskSigProofs bitmask      `codec:"certSbm"`

	PartProofs        []certProofs `codec:"certP,allocbound=maxEncodedTransactionGroups"`
	BitmaskPartProofs bitmask      `codec:"certPbm"`

	Reveals        []revealMap `codec:"certr,allocbound=maxEncodedTransactionGroups"`
	BitmaskReveals bitmask     `codec:"certrbm"`
}

const (
	paymentTx = iota
	keyRegistrationTx
	assetConfigTx
	assetTransferTx
	assetFreezeTx
	applicationCallTx
	compactCertTx
	unknownTx
)

// TxTypeToByte converts a TxType to byte encoding
func TxTypeToByte(t protocol.TxType) (byte, error) {
	switch t {
	case protocol.PaymentTx:
		return paymentTx, nil
	case protocol.KeyRegistrationTx:
		return keyRegistrationTx, nil
	case protocol.AssetConfigTx:
		return assetConfigTx, nil
	case protocol.AssetTransferTx:
		return assetTransferTx, nil
	case protocol.AssetFreezeTx:
		return assetFreezeTx, nil
	case protocol.ApplicationCallTx:
		return applicationCallTx, nil
	case protocol.CompactCertTx:
		return compactCertTx, nil
	default:
		return unknownTx, errInvalidTxType
	}
}

// ByteToTxType converts a byte encoding to TxType
func ByteToTxType(b byte) protocol.TxType {
	if int(b) >= len(protocol.TxnTypes) {
		return protocol.UnknownTx
	}
	return protocol.TxnTypes[b]
}