/
service.go
335 lines (275 loc) · 10 KB
/
service.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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
package gossip
import (
"fmt"
"math/rand"
"sync"
"sync/atomic"
"time"
"github.com/ethereum/go-ethereum/accounts"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core"
"github.com/ethereum/go-ethereum/core/types"
notify "github.com/ethereum/go-ethereum/event"
"github.com/ethereum/go-ethereum/node"
"github.com/ethereum/go-ethereum/p2p"
"github.com/ethereum/go-ethereum/p2p/discv5"
"github.com/ethereum/go-ethereum/p2p/enr"
"github.com/ethereum/go-ethereum/rpc"
"github.com/Fantom-foundation/lachesis-ex/app"
"github.com/Fantom-foundation/lachesis-ex/ethapi"
"github.com/Fantom-foundation/lachesis-ex/eventcheck"
"github.com/Fantom-foundation/lachesis-ex/eventcheck/basiccheck"
"github.com/Fantom-foundation/lachesis-ex/eventcheck/epochcheck"
"github.com/Fantom-foundation/lachesis-ex/eventcheck/gaspowercheck"
"github.com/Fantom-foundation/lachesis-ex/eventcheck/heavycheck"
"github.com/Fantom-foundation/lachesis-ex/eventcheck/parentscheck"
"github.com/Fantom-foundation/lachesis-ex/evmcore"
"github.com/Fantom-foundation/lachesis-ex/gossip/filters"
"github.com/Fantom-foundation/lachesis-ex/gossip/gasprice"
"github.com/Fantom-foundation/lachesis-ex/gossip/occuredtxs"
"github.com/Fantom-foundation/lachesis-ex/hash"
"github.com/Fantom-foundation/lachesis-ex/inter"
"github.com/Fantom-foundation/lachesis-ex/inter/idx"
"github.com/Fantom-foundation/lachesis-ex/lachesis"
"github.com/Fantom-foundation/lachesis-ex/lachesis/params"
"github.com/Fantom-foundation/lachesis-ex/logger"
)
const (
txsRingBufferSize = 20000 // Maximum number of stored hashes of included but not confirmed txs
)
type ServiceFeed struct {
scope notify.SubscriptionScope
newEpoch notify.Feed
newPack notify.Feed
newEmittedEvent notify.Feed
newBlock notify.Feed
newTxs notify.Feed
newLogs notify.Feed
}
func (f *ServiceFeed) SubscribeNewEpoch(ch chan<- idx.Epoch) notify.Subscription {
return f.scope.Track(f.newEpoch.Subscribe(ch))
}
func (f *ServiceFeed) SubscribeNewPack(ch chan<- idx.Pack) notify.Subscription {
return f.scope.Track(f.newPack.Subscribe(ch))
}
func (f *ServiceFeed) SubscribeNewEmitted(ch chan<- *inter.Event) notify.Subscription {
return f.scope.Track(f.newEmittedEvent.Subscribe(ch))
}
func (f *ServiceFeed) SubscribeNewBlock(ch chan<- evmcore.ChainHeadNotify) notify.Subscription {
return f.scope.Track(f.newBlock.Subscribe(ch))
}
func (f *ServiceFeed) SubscribeNewTxs(ch chan<- core.NewTxsEvent) notify.Subscription {
return f.scope.Track(f.newTxs.Subscribe(ch))
}
func (f *ServiceFeed) SubscribeNewLogs(ch chan<- []*types.Log) notify.Subscription {
return f.scope.Track(f.newLogs.Subscribe(ch))
}
// Service implements go-ethereum/node.Service interface.
type Service struct {
config *Config
wg sync.WaitGroup
done chan struct{}
// server
Name string
Topic discv5.Topic
serverPool *serverPool
// application
node *node.ServiceContext
store *Store
abciApp *app.App
engine Consensus
engineMu *sync.RWMutex
emitter *Emitter
txpool *evmcore.TxPool
occurredTxs *occuredtxs.Buffer
heavyCheckReader HeavyCheckReader
gasPowerCheckReader GasPowerCheckReader
checkers *eventcheck.Checkers
// global variables. TODO refactor to pass them as arguments if possible
blockParticipated map[idx.StakerID]bool // validators who participated in last block
currentEvent hash.Event // current event which is being processed
feed ServiceFeed
// application protocol
pm *ProtocolManager
EthAPI *EthAPIBackend
netRPCService *ethapi.PublicNetAPI
logger.Instance
}
func NewService(ctx *node.ServiceContext, config *Config, store *Store, engine Consensus, abci *app.App) (*Service, error) {
svc := &Service{
config: config,
done: make(chan struct{}),
Name: fmt.Sprintf("Node-%d", rand.Int()),
node: ctx,
store: store,
abciApp: abci,
engineMu: new(sync.RWMutex),
occurredTxs: occuredtxs.New(txsRingBufferSize, types.NewEIP155Signer(config.Net.EvmChainConfig().ChainID)),
blockParticipated: make(map[idx.StakerID]bool),
Instance: logger.MakeInstance(),
}
// wrap engine
svc.engine = &HookedEngine{
engine: engine,
processEvent: svc.processEvent,
}
svc.engine.Bootstrap(inter.ConsensusCallbacks{
ApplyBlock: svc.applyBlock,
SelectValidatorsGroup: svc.selectValidatorsGroup,
OnEventConfirmed: svc.onEventConfirmed,
IsEventAllowedIntoBlock: svc.isEventAllowedIntoBlock,
})
svc.abciApp.InitChain(
svc.engine.GetEpoch())
// create server pool
trustedNodes := []string{}
svc.serverPool = newServerPool(store.service.Peers, svc.done, &svc.wg, trustedNodes)
// create tx pool
stateReader := svc.GetEvmStateReader()
if config.TxPool.Journal != "" {
config.TxPool.Journal = ctx.ResolvePath(config.TxPool.Journal)
}
svc.txpool = evmcore.NewTxPool(config.TxPool, config.Net.EvmChainConfig(), stateReader)
// create checkers
svc.heavyCheckReader.Addrs.Store(ReadEpochPubKeys(svc.abciApp, svc.engine.GetEpoch())) // read pub keys of current epoch from disk
svc.gasPowerCheckReader.Ctx.Store(ReadGasPowerContext(svc.store, svc.abciApp, svc.engine.GetValidators(), svc.engine.GetEpoch(), &svc.config.Net.Economy)) // read gaspower check data from disk
svc.checkers = makeCheckers(&svc.config.Net, &svc.heavyCheckReader, &svc.gasPowerCheckReader, svc.engine, svc.store)
// create protocol manager
var err error
svc.pm, err = NewProtocolManager(config, &svc.feed, svc.txpool, svc.engineMu, svc.checkers, store, svc.engine, svc.serverPool)
// create API backend
svc.EthAPI = &EthAPIBackend{svc.abciApp.EthAPIBackend(), svc, stateReader, nil, config.ExtRPCEnabled}
svc.EthAPI.gpo = gasprice.NewOracle(svc.EthAPI, svc.config.GPO)
return svc, err
}
// makeCheckers builds event checkers
func makeCheckers(net *lachesis.Config, heavyCheckReader *HeavyCheckReader, gasPowerCheckReader *GasPowerCheckReader, engine Consensus, store *Store) *eventcheck.Checkers {
// create signatures checker
ledgerID := net.EvmChainConfig().ChainID
heavyCheck := heavycheck.NewDefault(&net.Dag, heavyCheckReader, types.NewEIP155Signer(ledgerID))
// create gaspower checker
gaspowerCheck := gaspowercheck.New(gasPowerCheckReader)
return &eventcheck.Checkers{
Basiccheck: basiccheck.New(&net.Dag),
Epochcheck: epochcheck.New(&net.Dag, engine),
Parentscheck: parentscheck.New(&net.Dag),
Heavycheck: heavyCheck,
Gaspowercheck: gaspowerCheck,
}
}
func (s *Service) makeEmitter() *Emitter {
// randomize event time to decrease peak load, and increase chance of catching double instances of validator
r := rand.New(rand.NewSource(time.Now().UnixNano()))
emitterCfg := s.config.Emitter // copy data
emitterCfg.EmitIntervals = *emitterCfg.EmitIntervals.RandomizeEmitTime(r)
return NewEmitter(&s.config.Net, &emitterCfg,
EmitterWorld{
Am: s.AccountManager(),
Engine: s.engine,
EngineMu: s.engineMu,
Store: s.store,
App: s.abciApp,
Txpool: s.txpool,
OccurredTxs: s.occurredTxs,
OnEmitted: func(emitted *inter.Event) {
// s.engineMu is locked here
err := s.engine.ProcessEvent(emitted)
if err != nil {
s.Log.Crit("Self-event connection failed", "err", err.Error())
}
s.feed.newEmittedEvent.Send(emitted) // PM listens and will broadcast it
if err != nil {
s.Log.Crit("Failed to post self-event", "err", err.Error())
}
},
IsSynced: func() bool {
return atomic.LoadUint32(&s.pm.synced) != 0
},
PeersNum: func() int {
return s.pm.peers.Len()
},
AddVersion: func(e *inter.Event) *inter.Event {
// serialization version
e.Version = 0
// node version
if e.Seq <= 1 && len(s.config.Emitter.VersionToPublish) > 0 {
version := []byte("v-" + s.config.Emitter.VersionToPublish)
if len(version) <= params.MaxExtraData {
e.Extra = version
}
}
return e
},
Checkers: s.checkers,
},
)
}
// Protocols returns protocols the service can communicate on.
func (s *Service) Protocols() []p2p.Protocol {
protos := make([]p2p.Protocol, len(ProtocolVersions))
for i, vsn := range ProtocolVersions {
protos[i] = s.pm.makeProtocol(vsn)
protos[i].Attributes = []enr.Entry{s.currentEnr()}
}
return protos
}
// APIs returns api methods the service wants to expose on rpc channels.
func (s *Service) APIs() []rpc.API {
apis := ethapi.GetAPIs(s.EthAPI)
apis = append(apis, []rpc.API{
{
Namespace: "eth",
Version: "1.0",
Service: NewPublicEthereumAPI(s),
Public: true,
}, {
Namespace: "eth",
Version: "1.0",
Service: filters.NewPublicFilterAPI(s.EthAPI),
Public: true,
}, {
Namespace: "net",
Version: "1.0",
Service: s.netRPCService,
Public: true,
},
}...)
return apis
}
// Start method invoked when the node is ready to start the service.
func (s *Service) Start(srv *p2p.Server) error {
// Start the RPC service
s.netRPCService = ethapi.NewPublicNetAPI(srv, s.config.Net.NetworkID)
var genesis common.Hash
genesis = s.engine.GetGenesisHash()
s.Topic = discv5.Topic("lachesis@" + genesis.Hex())
if srv.DiscV5 != nil {
go func(topic discv5.Topic) {
s.Log.Info("Starting topic registration")
defer s.Log.Info("Terminated topic registration")
srv.DiscV5.RegisterTopic(topic, s.done)
}(s.Topic)
}
s.pm.Start(srv.MaxPeers)
s.serverPool.start(srv, s.Topic)
s.emitter = s.makeEmitter()
s.emitter.SetValidator(s.config.Emitter.Validator)
s.emitter.StartEventEmission()
return nil
}
// Stop method invoked when the node terminates the service.
func (s *Service) Stop() error {
close(s.done)
s.emitter.StopEventEmission()
s.pm.Stop()
s.wg.Wait()
s.feed.scope.Close()
// flush the state at exit, after all the routines stopped
s.engineMu.Lock()
defer s.engineMu.Unlock()
return s.store.Commit(nil, true)
}
// AccountManager return node's account manager
func (s *Service) AccountManager() *accounts.Manager {
return s.node.AccountManager
}