-
Notifications
You must be signed in to change notification settings - Fork 0
/
asset_transfer.go
338 lines (297 loc) · 7.61 KB
/
asset_transfer.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
/*
github.com/tcrain/cons - Experimental project for testing and scaling consensus algorithms.
Copyright (C) 2020 The project authors - tcrain
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program 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 General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package asset
import (
"bytes"
"fmt"
"github.com/tcrain/cons/config"
"github.com/tcrain/cons/consensus/auth/sig"
"github.com/tcrain/cons/consensus/types"
"github.com/tcrain/cons/consensus/utils"
"io"
"sort"
)
type AssetTransfer struct {
Sender sig.Pub // Who is sending the assets.
Inputs []types.HashBytes // The input assets.
Outputs []AssetInterface // The output assets.
Receivers []sig.Pub // The receivers of the output assets.
Signature sig.Sig // The signature of the sender.
// For encoding.
NewAssetFunc func() AssetInterface
NewSigFunc func() sig.Sig
NewPubFunc func() sig.Pub
SignedBytes []byte // The bytes to sign.
}
func NewEmptyAssetTransfer(newAssetFunc func() AssetInterface, newSigFunc func() sig.Sig,
newPubFunc func() sig.Pub) *AssetTransfer {
return &AssetTransfer{
NewAssetFunc: newAssetFunc,
NewSigFunc: newSigFunc,
NewPubFunc: newPubFunc,
}
}
func NewAssetTransfer(sender sig.Pub, inputs []types.HashBytes, outputs []AssetInterface,
receivers []sig.Pub) *AssetTransfer {
return &AssetTransfer{
Sender: sender,
Inputs: inputs,
Outputs: outputs,
Receivers: receivers,
}
}
// New creates an empty asset transfer object.
func (at *AssetTransfer) New() types.EncodeInterface {
return &AssetTransfer{
Sender: at.Sender.New(),
NewSigFunc: at.NewSigFunc,
NewAssetFunc: at.NewAssetFunc,
}
}
func CheckUnique(assets []AssetInterface) error {
sorted := make(SortedAssets, len(assets))
copy(sorted, assets)
sort.Sort(sorted)
for i := 1; i < len(sorted); i++ {
if bytes.Equal(sorted[i-1].GetID(), sorted[i].GetID()) {
return errNonUniqueAssets
}
}
return nil
}
func (at *AssetTransfer) CheckFormat() error {
if at.Sender == nil {
return errNoSender
}
if _, err := at.Sender.GetPubString(); err != nil {
return err
}
if len(at.Inputs) > config.MaxAdditionalIndices || len(at.Inputs) == 0 {
return errInvalidAssetCount
}
if len(at.Outputs) > config.MaxAdditionalIndices {
return errInvalidAssetCount
}
if config.SignCausalAssets {
if at.Signature == nil {
return errNoSignature
}
}
if len(at.Outputs) != len(at.Receivers) {
return errInvalidAssetCount
}
if err := CheckUnique(at.Outputs); err != nil {
return err
}
// The inputs are checked for unique during validation
return nil
}
// GetSignedBytes returns the bytes that should be signed by the senders.
func (tx *AssetTransfer) GetSignedMessage() []byte {
if len(tx.SignedBytes) == 0 {
panic(len(tx.SignedBytes))
}
return tx.SignedBytes
}
// GetSignedHash returns messages.GetHash(tx.GetSignedMessage())
func (tx *AssetTransfer) GetSignedHash() types.HashBytes {
return types.GetHash(tx.SignedBytes)
}
//////////////////////////////////////// Encoding ///////////////////////////////////////////////////////
// encodeSignedBytes generates the bytes that will be signed
func (at *AssetTransfer) encodeSignedBytes() (err error) {
var n int
writer := bytes.NewBuffer(nil)
// The sender key
if n, err = at.Sender.Encode(writer); err != nil {
return
}
if len(at.Inputs) > config.MaxAdditionalIndices || len(at.Inputs) == 0 {
panic("encoding invalid asset transfer")
}
// The number of inputs
var n1 int
n1, err = utils.EncodeUvarint(uint64(len(at.Inputs)), writer)
n += n1
if err != nil {
return
}
// The inputs
for _, aid := range at.Inputs {
n1, err = writer.Write(aid)
n += n1
if err != nil {
return
}
}
if len(at.Outputs) > config.MaxAdditionalIndices || len(at.Outputs) != len(at.Receivers) {
panic("encoding invalid asset transfer")
}
// The number of outputs
n1, err = utils.EncodeUvarint(uint64(len(at.Outputs)), writer)
n += n1
if err != nil {
return
}
// The outputs
for _, asset := range at.Outputs {
n1, err = asset.Encode(writer)
n += n1
if err != nil {
return
}
}
for _, pub := range at.Receivers {
n1, err = pub.Encode(writer)
n += n1
if err != nil {
return
}
}
at.SignedBytes = writer.Bytes()
return
}
var ErrMustSignFirst = fmt.Errorf("must sign tx before encoding")
// Encode the asset transfer to the writer.
func (at *AssetTransfer) Encode(writer io.Writer) (n int, err error) {
if config.SignCausalAssets {
if at.SignedBytes == nil {
err = ErrMustSignFirst
return
}
}
// be sure we have encoded the signed part
if at.SignedBytes == nil {
if err = at.encodeSignedBytes(); err != nil {
return
}
}
var nxtN int
// First the size of the signed part
nxtN, err = utils.EncodeUvarint(uint64(len(at.SignedBytes)), writer)
n += nxtN
if err != nil {
return
}
// The main message
nxtN, err = writer.Write(at.SignedBytes)
n += nxtN
if err != nil {
return
}
if config.SignCausalAssets {
// The signature
nxtN, err = at.Signature.Encode(writer)
n += nxtN
}
if n > MaxAssetSize {
panic("fix me")
}
return
}
// Decode the asset transfer from the writer.
func (at *AssetTransfer) Decode(r io.Reader) (n int, err error) {
var nxtN int
var signedByteCount uint64
signedByteCount, nxtN, err = utils.ReadUvarint(r)
n += nxtN
if err != nil {
return
}
if signedByteCount > MaxAssetSize { // TODO
err = errAssetTooLarge
return
}
at.SignedBytes = make([]byte, signedByteCount)
nxtN, err = r.Read(at.SignedBytes)
n += nxtN
if err != nil {
return
}
reader := bytes.NewReader(at.SignedBytes)
// First the sender pub
at.Sender = at.NewPubFunc()
if n, err = at.Sender.Decode(reader); err != nil {
return
}
// The number of inputs
v, n1, err := utils.ReadUvarint(reader)
n += n1
if err != nil {
return
}
inputCount := int(v)
switch {
case inputCount <= 0, inputCount > config.MaxAdditionalIndices:
err = errInvalidAssetCount
return
}
// The inputs
at.Inputs = make([]types.HashBytes, inputCount)
for i := range at.Inputs {
at.Inputs[i], n1, err = types.DecodeHash(reader)
n += n1
if err != nil {
return
}
}
// The number of outputs.
v, n1, err = utils.ReadUvarint(reader)
n += n1
if err != nil {
return
}
assetCount := int(v)
switch {
case assetCount <= 0, assetCount > config.MaxAdditionalIndices:
err = errInvalidAssetCount
return
}
// The outputs.
at.Outputs = make([]AssetInterface, assetCount)
for i := range at.Outputs {
at.Outputs[i] = at.NewAssetFunc()
n1, err = at.Outputs[i].Decode(reader)
n += n1
if err != nil {
return
}
}
// The recipiants
at.Receivers = make([]sig.Pub, assetCount)
for i := range at.Receivers {
at.Receivers[i] = at.NewPubFunc()
n1, err = at.Receivers[i].Decode(reader)
n += n1
if err != nil {
return
}
}
if config.SignCausalAssets {
// The signature
at.Signature = at.NewSigFunc()
n1, err = at.Signature.Decode(r)
n += n1
}
return
}
// Marshal the asset transfer.
func (ba *AssetTransfer) MarshalBinary() (data []byte, err error) {
return types.MarshalBinaryHelper(ba)
}
// Unmarshal the asset transfer.
func (ba *AssetTransfer) UnmarshalBinary(data []byte) error {
return types.UnmarshalBinaryHelper(ba, data)
}