-
Notifications
You must be signed in to change notification settings - Fork 659
/
parser.go
138 lines (119 loc) · 3.37 KB
/
parser.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
// Copyright (C) 2019-2021, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package txs
import (
"fmt"
"math"
"reflect"
"github.com/ava-labs/avalanchego/codec"
"github.com/ava-labs/avalanchego/codec/linearcodec"
"github.com/ava-labs/avalanchego/codec/reflectcodec"
"github.com/ava-labs/avalanchego/utils/logging"
"github.com/ava-labs/avalanchego/utils/timer/mockable"
"github.com/ava-labs/avalanchego/utils/wrappers"
"github.com/ava-labs/avalanchego/vms/avm/fxs"
)
const CodecVersion = 0
var _ Parser = &parser{}
type Parser interface {
Codec() codec.Manager
GenesisCodec() codec.Manager
Parse(bytes []byte) (*Tx, error)
ParseGenesis(bytes []byte) (*Tx, error)
InitializeTx(tx *Tx) error
InitializeGenesisTx(tx *Tx) error
}
type parser struct {
cm codec.Manager
gcm codec.Manager
}
func NewParser(fxs []fxs.Fx) (Parser, error) {
return NewCustomParser(
make(map[reflect.Type]int),
&mockable.Clock{},
logging.NoLog{},
fxs,
)
}
func NewCustomParser(
typeToFxIndex map[reflect.Type]int,
clock *mockable.Clock,
log logging.Logger,
fxs []fxs.Fx,
) (Parser, error) {
gc := linearcodec.New([]string{reflectcodec.DefaultTagName}, 1<<20)
c := linearcodec.NewDefault()
gcm := codec.NewManager(math.MaxInt32)
cm := codec.NewDefaultManager()
errs := wrappers.Errs{}
errs.Add(
c.RegisterType(&BaseTx{}),
c.RegisterType(&CreateAssetTx{}),
c.RegisterType(&OperationTx{}),
c.RegisterType(&ImportTx{}),
c.RegisterType(&ExportTx{}),
cm.RegisterCodec(CodecVersion, c),
gc.RegisterType(&BaseTx{}),
gc.RegisterType(&CreateAssetTx{}),
gc.RegisterType(&OperationTx{}),
gc.RegisterType(&ImportTx{}),
gc.RegisterType(&ExportTx{}),
gcm.RegisterCodec(CodecVersion, gc),
)
if errs.Errored() {
return nil, errs.Err
}
vm := &fxVM{
typeToFxIndex: typeToFxIndex,
clock: clock,
log: log,
}
for i, fx := range fxs {
vm.codecRegistry = &codecRegistry{
codecs: []codec.Registry{gc, c},
index: i,
typeToIndex: vm.typeToFxIndex,
}
if err := fx.Initialize(vm); err != nil {
return nil, err
}
}
return &parser{
cm: cm,
gcm: gcm,
}, nil
}
func (p *parser) Codec() codec.Manager { return p.cm }
func (p *parser) GenesisCodec() codec.Manager { return p.gcm }
func (p *parser) Parse(bytes []byte) (*Tx, error) { return parse(p.cm, bytes) }
func (p *parser) ParseGenesis(bytes []byte) (*Tx, error) { return parse(p.gcm, bytes) }
func (p *parser) InitializeTx(tx *Tx) error { return initializeTx(p.cm, tx) }
func (p *parser) InitializeGenesisTx(tx *Tx) error { return initializeTx(p.gcm, tx) }
func parse(cm codec.Manager, bytes []byte) (*Tx, error) {
tx := &Tx{}
parsedVersion, err := cm.Unmarshal(bytes, tx)
if err != nil {
return nil, err
}
if parsedVersion != CodecVersion {
return nil, fmt.Errorf("expected codec version %d but got %d", CodecVersion, parsedVersion)
}
unsignedBytes, err := cm.Marshal(CodecVersion, &tx.UnsignedTx)
if err != nil {
return nil, err
}
tx.UnsignedTx.Initialize(unsignedBytes, bytes)
return tx, nil
}
func initializeTx(cm codec.Manager, tx *Tx) error {
unsignedBytes, err := cm.Marshal(CodecVersion, tx.UnsignedTx)
if err != nil {
return err
}
signedBytes, err := cm.Marshal(CodecVersion, &tx)
if err != nil {
return err
}
tx.UnsignedTx.Initialize(unsignedBytes, signedBytes)
return nil
}