forked from ava-labs/ortelius
-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser.go
108 lines (94 loc) · 2.78 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
// (c) 2020, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package avm
import (
"github.com/corpetty/avalanchego/codec"
"github.com/corpetty/avalanchego/database"
"github.com/corpetty/avalanchego/genesis"
"github.com/corpetty/avalanchego/ids"
"github.com/corpetty/avalanchego/snow"
"github.com/corpetty/avalanchego/snow/engine/common"
"github.com/corpetty/avalanchego/utils/logging"
"github.com/corpetty/avalanchego/vms/avm"
"github.com/corpetty/avalanchego/vms/nftfx"
"github.com/corpetty/avalanchego/vms/platformvm"
"github.com/corpetty/avalanchego/vms/secp256k1fx"
"github.com/corpetty/ortelius/utils"
"github.com/prometheus/client_golang/prometheus"
)
const MaxCodecSize = 100_000_000
func parseTx(c codec.Manager, bytes []byte) (*avm.Tx, error) {
tx := &avm.Tx{}
ver, err := c.Unmarshal(bytes, tx)
if err != nil {
return nil, err
}
unsignedBytes, err := c.Marshal(ver, &tx.UnsignedTx)
if err != nil {
return nil, err
}
tx.Initialize(unsignedBytes, bytes)
return tx, nil
}
// newAVMCodec creates codec that can parse avm objects
func newAVMCodec(networkID uint32, chainID string) (*avm.VM, *snow.Context, codec.Manager, database.Database, error) {
genesisBytes, _, err := genesis.Genesis(networkID, "")
if err != nil {
return nil, nil, nil, nil, err
}
g, err := genesis.VMGenesis(genesisBytes, avm.ID)
if err != nil {
return nil, nil, nil, nil, err
}
createChainTx, ok := g.UnsignedTx.(*platformvm.UnsignedCreateChainTx)
if !ok {
return nil, nil, nil, nil, ErrIncorrectGenesisChainTxType
}
bcLookup := &ids.Aliaser{}
bcLookup.Initialize()
id, err := ids.FromString(chainID)
if err != nil {
return nil, nil, nil, nil, err
}
if err = bcLookup.Alias(id, "X"); err != nil {
return nil, nil, nil, nil, err
}
var (
fxIDs = createChainTx.FxIDs
fxs = make([]*common.Fx, 0, len(fxIDs))
ctx = &snow.Context{
NetworkID: networkID,
ChainID: id,
Log: logging.NoLog{},
Metrics: prometheus.NewRegistry(),
BCLookup: bcLookup,
}
)
for _, fxID := range fxIDs {
switch {
case fxID == secp256k1fx.ID:
fxs = append(fxs, &common.Fx{
Fx: &secp256k1fx.Fx{},
ID: fxID,
})
case fxID == nftfx.ID:
fxs = append(fxs, &common.Fx{
Fx: &nftfx.Fx{},
ID: fxID,
})
default:
// return nil, fmt.Errorf("Unknown FxID: %s", fxID)
}
}
db := &utils.NoopDatabase{}
// Initialize an producer to use for tx parsing
// An error is returned about the DB being closed but this is expected because
// we're not using a real DB here.
vm := &avm.VM{}
err = vm.Initialize(ctx, db, createChainTx.GenesisData, make(chan common.Message, 1), fxs)
if err != nil && err != database.ErrClosed {
return nil, nil, nil, nil, err
}
vm.Codec().SetMaxSize(MaxCodecSize)
return vm, ctx, vm.Codec(), db, nil
}