forked from ava-labs/hypersdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontroller.go
173 lines (149 loc) · 4.59 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
// Copyright (C) 2023, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package controller
import (
"context"
"fmt"
"net/http"
"github.com/ava-labs/avalanchego/database"
"github.com/ava-labs/avalanchego/snow"
"go.uber.org/zap"
"github.com/ava-labs/hypersdk/auth"
"github.com/ava-labs/hypersdk/builder"
"github.com/ava-labs/hypersdk/chain"
"github.com/ava-labs/hypersdk/examples/morpheusvm/config"
"github.com/ava-labs/hypersdk/examples/morpheusvm/consts"
"github.com/ava-labs/hypersdk/examples/morpheusvm/genesis"
"github.com/ava-labs/hypersdk/examples/morpheusvm/rpc"
"github.com/ava-labs/hypersdk/examples/morpheusvm/storage"
"github.com/ava-labs/hypersdk/examples/morpheusvm/version"
"github.com/ava-labs/hypersdk/extension/indexer"
"github.com/ava-labs/hypersdk/gossiper"
"github.com/ava-labs/hypersdk/pebble"
"github.com/ava-labs/hypersdk/vm"
ametrics "github.com/ava-labs/avalanchego/api/metrics"
hrpc "github.com/ava-labs/hypersdk/rpc"
hstorage "github.com/ava-labs/hypersdk/storage"
)
var (
_ vm.Controller = (*Controller)(nil)
_ vm.ControllerFactory = (*factory)(nil)
)
func New() *vm.VM {
return vm.New(&factory{}, version.Version)
}
type factory struct{}
func (*factory) New(
inner *vm.VM,
snowCtx *snow.Context,
gatherer ametrics.MultiGatherer,
genesisBytes []byte,
upgradeBytes []byte, // subnets to allow for AWM
configBytes []byte,
) (
vm.Controller,
vm.Genesis,
builder.Builder,
gossiper.Gossiper,
vm.Handlers,
chain.ActionRegistry,
chain.AuthRegistry,
map[uint8]vm.AuthEngine,
error,
) {
c := &Controller{}
c.inner = inner
c.snowCtx = snowCtx
c.stateManager = &storage.StateManager{}
// Instantiate metrics
var err error
c.metrics, err = newMetrics(gatherer)
if err != nil {
return nil, nil, nil, nil, nil, nil, nil, nil, err
}
// Load config and genesis
c.config, err = config.New(configBytes)
if err != nil {
return nil, nil, nil, nil, nil, nil, nil, nil, err
}
c.snowCtx.Log.SetLevel(c.config.LogLevel)
snowCtx.Log.Info("initialized config", zap.Any("contents", c.config))
c.genesis, err = genesis.New(genesisBytes, upgradeBytes)
if err != nil {
return 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))
c.txDB, err = hstorage.New(pebble.NewDefaultConfig(), snowCtx.ChainDataDir, "db", gatherer)
if err != nil {
return nil, nil, nil, nil, nil, nil, nil, nil, err
}
acceptedSubscribers := []indexer.AcceptedSubscriber{
indexer.NewSuccessfulTxSubscriber(&actionHandler{c: c}),
}
if c.config.StoreTransactions {
c.txIndexer = indexer.NewTxDBIndexer(c.txDB)
acceptedSubscribers = append(acceptedSubscribers, c.txIndexer)
} else {
c.txIndexer = indexer.NewNoOpTxIndexer()
}
c.acceptedSubscriber = indexer.NewAcceptedSubscribers(acceptedSubscribers...)
// 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, 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()
gossip, err = gossiper.NewProposer(inner, gcfg)
if err != nil {
return nil, nil, nil, nil, nil, nil, nil, nil, err
}
}
return c, c.genesis, build, gossip, apis, consts.ActionRegistry, consts.AuthRegistry, auth.Engines(), nil
}
type Controller struct {
inner *vm.VM
snowCtx *snow.Context
genesis *genesis.Genesis
config *config.Config
stateManager *storage.StateManager
metrics *metrics
txDB database.Database
txIndexer indexer.TxIndexer
acceptedSubscriber indexer.AcceptedSubscriber
}
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 {
return c.acceptedSubscriber.Accepted(ctx, blk)
}
func (c *Controller) Shutdown(context.Context) error {
// Close any databases created during initialization
return c.txDB.Close()
}