-
Notifications
You must be signed in to change notification settings - Fork 132
/
codec.go
99 lines (86 loc) · 3.1 KB
/
codec.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
// (c) 2019-2021, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package evm
import (
"fmt"
"github.com/ava-labs/avalanchego/codec"
"github.com/ava-labs/avalanchego/codec/linearcodec"
"github.com/ava-labs/avalanchego/utils/wrappers"
"github.com/ava-labs/avalanchego/vms/secp256k1fx"
)
// Codec does serialization and deserialization
var Codec codec.Manager
func init() {
Codec = codec.NewDefaultManager()
c := linearcodec.NewDefault()
errs := wrappers.Errs{}
errs.Add(
c.RegisterType(&UnsignedImportTx{}),
c.RegisterType(&UnsignedExportTx{}),
)
c.SkipRegistrations(3)
errs.Add(
c.RegisterType(&secp256k1fx.TransferInput{}),
c.RegisterType(&secp256k1fx.MintOutput{}),
c.RegisterType(&secp256k1fx.TransferOutput{}),
c.RegisterType(&secp256k1fx.MintOperation{}),
c.RegisterType(&secp256k1fx.Credential{}),
c.RegisterType(&secp256k1fx.Input{}),
c.RegisterType(&secp256k1fx.OutputOwners{}),
Codec.RegisterCodec(codecVersion, c),
)
if errs.Errored() {
panic(errs.Err)
}
}
// extractAtomicTxs returns the atomic transactions in [atomicTxBytes] if
// they exist.
// if [batch] is true, it attempts to unmarshal [atomicTxBytes] as a slice of
// transactions (post-ApricotPhase5), and if it is false, then it unmarshals
// it as a single atomic transaction.
func ExtractAtomicTxs(atomicTxBytes []byte, batch bool, codec codec.Manager) ([]*Tx, error) {
if len(atomicTxBytes) == 0 {
return nil, nil
}
if !batch {
tx, err := ExtractAtomicTx(atomicTxBytes, codec)
if err != nil {
return nil, err
}
return []*Tx{tx}, err
}
return ExtractAtomicTxsBatch(atomicTxBytes, codec)
}
// [ExtractAtomicTx] extracts a singular atomic transaction from [atomicTxBytes]
// and returns a slice of atomic transactions for compatibility with the type returned post
// ApricotPhase5.
// Note: this function assumes [atomicTxBytes] is non-empty.
func ExtractAtomicTx(atomicTxBytes []byte, codec codec.Manager) (*Tx, error) {
atomicTx := new(Tx)
if _, err := codec.Unmarshal(atomicTxBytes, atomicTx); err != nil {
return nil, fmt.Errorf("failed to unmarshal atomic transaction (pre-AP5): %w", err)
}
if err := atomicTx.Sign(codec, nil); err != nil {
return nil, fmt.Errorf("failed to initialize singleton atomic tx due to: %w", err)
}
return atomicTx, nil
}
// [ExtractAtomicTxsBatch] extracts a slice of atomic transactions from [atomicTxBytes].
// Note: this function assumes [atomicTxBytes] is non-empty.
func ExtractAtomicTxsBatch(atomicTxBytes []byte, codec codec.Manager) ([]*Tx, error) {
var atomicTxs []*Tx
if _, err := codec.Unmarshal(atomicTxBytes, &atomicTxs); err != nil {
return nil, fmt.Errorf("failed to unmarshal atomic tx (AP5) due to %w", err)
}
// Do not allow non-empty extra data field to contain zero atomic transactions. This would allow
// people to construct a block that contains useless data.
if len(atomicTxs) == 0 {
return nil, errMissingAtomicTxs
}
for index, atx := range atomicTxs {
if err := atx.Sign(codec, nil); err != nil {
return nil, fmt.Errorf("failed to initialize atomic tx at index %d: %w", index, err)
}
}
return atomicTxs, nil
}