forked from ava-labs/hypersdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
controller.go
227 lines (202 loc) · 6.11 KB
/
controller.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
// Copyright (C) 2023, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package controller
import (
"context"
"fmt"
"net/http"
ametrics "github.com/ava-labs/avalanchego/api/metrics"
"github.com/ava-labs/avalanchego/database"
"github.com/ava-labs/avalanchego/snow"
"github.com/ava-labs/hypersdk/builder"
"github.com/ava-labs/hypersdk/chain"
"github.com/ava-labs/hypersdk/gossiper"
hrpc "github.com/ava-labs/hypersdk/rpc"
hstorage "github.com/ava-labs/hypersdk/storage"
"github.com/ava-labs/hypersdk/vm"
"go.uber.org/zap"
"github.com/ava-labs/hypersdk/examples/tokenvm/actions"
"github.com/ava-labs/hypersdk/examples/tokenvm/auth"
"github.com/ava-labs/hypersdk/examples/tokenvm/config"
"github.com/ava-labs/hypersdk/examples/tokenvm/consts"
"github.com/ava-labs/hypersdk/examples/tokenvm/genesis"
"github.com/ava-labs/hypersdk/examples/tokenvm/orderbook"
"github.com/ava-labs/hypersdk/examples/tokenvm/rpc"
"github.com/ava-labs/hypersdk/examples/tokenvm/storage"
"github.com/ava-labs/hypersdk/examples/tokenvm/version"
)
var _ vm.Controller = (*Controller)(nil)
type Controller struct {
inner *vm.VM
snowCtx *snow.Context
genesis *genesis.Genesis
config *config.Config
stateManager *StateManager
metrics *metrics
metaDB database.Database
orderBook *orderbook.OrderBook
}
func New() *vm.VM {
return vm.New(&Controller{}, version.Version)
}
func (c *Controller) Initialize(
inner *vm.VM,
snowCtx *snow.Context,
gatherer ametrics.MultiGatherer,
genesisBytes []byte,
upgradeBytes []byte, // subnets to allow for AWM
configBytes []byte,
) (
vm.Config,
vm.Genesis,
builder.Builder,
gossiper.Gossiper,
database.Database,
database.Database,
vm.Handlers,
chain.ActionRegistry,
chain.AuthRegistry,
map[uint8]vm.AuthEngine,
error,
) {
c.inner = inner
c.snowCtx = snowCtx
c.stateManager = &StateManager{}
// Instantiate metrics
var err error
c.metrics, err = newMetrics(gatherer)
if err != nil {
return nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, err
}
// Load config and genesis
c.config, err = config.New(c.snowCtx.NodeID, configBytes)
if err != nil {
return nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, err
}
c.snowCtx.Log.SetLevel(c.config.GetLogLevel())
snowCtx.Log.Info("initialized config", zap.Bool("loaded", c.config.Loaded()), zap.Any("contents", c.config))
c.genesis, err = genesis.New(genesisBytes, upgradeBytes)
if err != nil {
return nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, fmt.Errorf(
"unable to read genesis: %w",
err,
)
}
snowCtx.Log.Info("loaded genesis", zap.Any("genesis", c.genesis))
// Create DBs
blockDB, stateDB, metaDB, err := hstorage.New(snowCtx.ChainDataDir, gatherer)
if err != nil {
return nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, err
}
c.metaDB = metaDB
// Create handlers
//
// hypersdk handler are initiatlized automatically, you just need to
// initialize custom handlers here.
apis := map[string]http.Handler{}
jsonRPCHandler, err := hrpc.NewJSONRPCHandler(
consts.Name,
rpc.NewJSONRPCServer(c),
)
if err != nil {
return nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, err
}
apis[rpc.JSONRPCEndpoint] = jsonRPCHandler
// Create builder and gossiper
var (
build builder.Builder
gossip gossiper.Gossiper
)
if c.config.TestMode {
c.inner.Logger().Info("running build and gossip in test mode")
build = builder.NewManual(inner)
gossip = gossiper.NewManual(inner)
} else {
build = builder.NewTime(inner)
gcfg := gossiper.DefaultProposerConfig()
gcfg.GossipMaxSize = c.config.GossipMaxSize
gcfg.GossipProposerDiff = c.config.GossipProposerDiff
gcfg.GossipProposerDepth = c.config.GossipProposerDepth
gcfg.NoGossipBuilderDiff = c.config.NoGossipBuilderDiff
gcfg.VerifyTimeout = c.config.VerifyTimeout
gossip, err = gossiper.NewProposer(inner, gcfg)
if err != nil {
return nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, err
}
}
// Initialize order book used to track all open orders
c.orderBook = orderbook.New(c, c.config.TrackedPairs, c.config.MaxOrdersPerPair)
return c.config, c.genesis, build, gossip, blockDB, stateDB, apis, consts.ActionRegistry, consts.AuthRegistry, auth.Engines(), nil
}
func (c *Controller) Rules(t int64) chain.Rules {
// TODO: extend with [UpgradeBytes]
return c.genesis.Rules(t, c.snowCtx.NetworkID, c.snowCtx.ChainID)
}
func (c *Controller) StateManager() chain.StateManager {
return c.stateManager
}
func (c *Controller) Accepted(ctx context.Context, blk *chain.StatelessBlock) error {
batch := c.metaDB.NewBatch()
defer batch.Reset()
results := blk.Results()
for i, tx := range blk.Txs {
result := results[i]
if c.config.GetStoreTransactions() {
err := storage.StoreTransaction(
ctx,
batch,
tx.ID(),
blk.GetTimestamp(),
result.Success,
result.Consumed,
result.Fee,
)
if err != nil {
return err
}
}
if result.Success {
switch action := tx.Action.(type) {
case *actions.CreateAsset:
c.metrics.createAsset.Inc()
case *actions.MintAsset:
c.metrics.mintAsset.Inc()
case *actions.BurnAsset:
c.metrics.burnAsset.Inc()
case *actions.Transfer:
c.metrics.transfer.Inc()
case *actions.CreateOrder:
c.metrics.createOrder.Inc()
c.orderBook.Add(tx.ID(), tx.Auth.Actor(), action)
case *actions.FillOrder:
c.metrics.fillOrder.Inc()
orderResult, err := actions.UnmarshalOrderResult(result.Output)
if err != nil {
// This should never happen
return err
}
if orderResult.Remaining == 0 {
c.orderBook.Remove(action.Order)
continue
}
c.orderBook.UpdateRemaining(action.Order, orderResult.Remaining)
case *actions.CloseOrder:
c.metrics.closeOrder.Inc()
c.orderBook.Remove(action.Order)
case *actions.ImportAsset:
c.metrics.importAsset.Inc()
case *actions.ExportAsset:
c.metrics.exportAsset.Inc()
}
}
}
return batch.Write()
}
func (*Controller) Rejected(context.Context, *chain.StatelessBlock) error {
return nil
}
func (*Controller) Shutdown(context.Context) error {
// Do not close any databases provided during initialization. The VM will
// close any databases your provided.
return nil
}