forked from aergoio/aergo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
chainservice.go
607 lines (537 loc) · 16.7 KB
/
chainservice.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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
/**
* @file
* @copyright defined in aergo/LICENSE.txt
*/
package chain
import (
"bytes"
"errors"
"fmt"
"math/big"
"reflect"
"runtime"
"github.com/aergoio/aergo-actor/actor"
"github.com/aergoio/aergo-lib/log"
cfg "github.com/aergoio/aergo/config"
"github.com/aergoio/aergo/consensus"
"github.com/aergoio/aergo/contract"
"github.com/aergoio/aergo/contract/system"
"github.com/aergoio/aergo/internal/common"
"github.com/aergoio/aergo/internal/enc"
"github.com/aergoio/aergo/message"
"github.com/aergoio/aergo/pkg/component"
"github.com/aergoio/aergo/state"
"github.com/aergoio/aergo/types"
"github.com/libp2p/go-libp2p-peer"
)
var (
logger = log.NewLogger("chain")
ErrBlockExist = errors.New("block already exist")
)
// Core represents a storage layer of a blockchain (chain & state DB).
type Core struct {
cdb *ChainDB
sdb *state.ChainStateDB
}
// NewCore returns an instance of Core.
func NewCore(dbType string, dataDir string, testModeOn bool) (*Core, error) {
core := &Core{
cdb: NewChainDB(),
sdb: state.NewChainStateDB(),
}
err := core.init(dbType, dataDir, testModeOn)
if err != nil {
return nil, err
}
return core, nil
}
// Init prepares Core (chain & state DB).
func (core *Core) init(dbType string, dataDir string, testModeOn bool) error {
// init chaindb
if err := core.cdb.Init(dbType, dataDir); err != nil {
logger.Fatal().Err(err).Msg("failed to initialize chaindb")
return err
}
// init statedb
bestBlock, err := core.cdb.GetBestBlock()
if err != nil {
return err
}
if err := core.sdb.Init(dbType, dataDir, bestBlock, testModeOn); err != nil {
logger.Fatal().Err(err).Msg("failed to initialize statedb")
return err
}
contract.LoadDatabase(dataDir)
return nil
}
func (core *Core) initGenesis(genesis *types.Genesis) (*types.Block, error) {
gh, _ := core.cdb.getHashByNo(0)
if len(gh) == 0 {
latest := core.cdb.getBestBlockNo()
logger.Info().Uint64("nom", latest).Msg("current latest")
if latest == 0 {
if genesis == nil {
genesis = types.GetDefaultGenesis()
}
err := core.sdb.SetGenesis(genesis, InitGenesisBPs)
if err != nil {
logger.Fatal().Err(err).Msg("cannot set statedb of genesisblock")
return nil, err
}
err = core.cdb.addGenesisBlock(genesis)
if err != nil {
logger.Fatal().Err(err).Msg("cannot add genesisblock")
return nil, err
}
logger.Info().Msg("genesis block is generated")
}
}
genesisBlock, _ := core.cdb.GetBlockByNo(0)
logger.Info().Str("genesis", enc.ToString(genesisBlock.Hash)).
Str("stateroot", enc.ToString(genesisBlock.GetHeader().GetBlocksRootHash())).Msg("chain initialized")
return genesisBlock, nil
}
// Close closes chain & state DB.
func (core *Core) Close() {
if core.sdb != nil {
core.sdb.Close()
}
if core.cdb != nil {
core.cdb.Close()
}
contract.CloseDatabase()
}
// InitGenesisBlock initialize chain database and generate specified genesis block if necessary
func (core *Core) InitGenesisBlock(gb *types.Genesis) error {
_, err := core.initGenesis(gb)
if err != nil {
logger.Fatal().Err(err).Msg("cannot initialize genesis block")
return err
}
return nil
}
type IChainHandler interface {
getBlock(blockHash []byte) (*types.Block, error)
getBlockByNo(blockNo types.BlockNo) (*types.Block, error)
getTx(txHash []byte) (*types.Tx, *types.TxIdx, error)
getReceipt(txHash []byte) (*types.Receipt, error)
getVote(addr []byte) (*types.VoteList, error)
getVotes(n int) (*types.VoteList, error)
getStaking(addr []byte) (*types.Staking, error)
addBlock(newBlock *types.Block, usedBstate *state.BlockState, peerID peer.ID) error
handleMissing(stopHash []byte, Hashes [][]byte) (message.BlockHash, types.BlockNo, types.BlockNo)
getAnchorsNew() (ChainAnchor, types.BlockNo, error)
findAncestor(Hashes [][]byte) (*types.BlockInfo, error)
checkBlockHandshake(peerID peer.ID, remoteBestHeight uint64, remoteBestHash []byte)
}
// ChainService manage connectivity of blocks
type ChainService struct {
*component.BaseComponent
consensus.ChainConsensus
*Core
cfg *cfg.Config
op *OrphanPool
validator *BlockValidator
chainWorker *ChainWorker
chainManager *ChainManager
}
// NewChainService creates an instance of ChainService.
func NewChainService(cfg *cfg.Config) *ChainService {
cs := &ChainService{
cfg: cfg,
op: NewOrphanPool(),
}
var err error
if cs.Core, err = NewCore(cfg.DbType, cfg.DataDir, cfg.EnableTestmode); err != nil {
logger.Fatal().Err(err).Msg("failed to initialize DB")
panic(err)
}
if err = Init(cfg.Blockchain.MaxBlockSize,
cfg.Blockchain.CoinbaseAccount,
types.DefaultCoinbaseFee,
cfg.Consensus.EnableBp,
cfg.Blockchain.MaxAnchorCount,
cfg.Blockchain.UseFastSyncer); err != nil {
logger.Error().Err(err).Msg("failed to init chainservice")
panic("invalid config: blockchain")
}
cs.validator = NewBlockValidator(cs.sdb)
cs.BaseComponent = component.NewBaseComponent(message.ChainSvc, cs, logger)
cs.chainManager = newChainManager(cs, cs.Core)
cs.chainWorker = newChainWorker(cs, defaultChainWorkerCount, cs.Core)
// init genesis block
if _, err := cs.initGenesis(nil); err != nil {
logger.Fatal().Err(err).Msg("failed to create a genesis block")
}
top, err := cs.getVotes(1)
if err != nil {
logger.Debug().Err(err).Msg("failed to get elected BPs")
} else {
for _, res := range top.Votes {
logger.Debug().Str("BP", enc.ToString(res.Candidate)).
Str("votes", new(big.Int).SetBytes(res.Amount).String()).Msgf("BP vote stat")
}
}
return cs
}
// SDB returns cs.sdb.
func (cs *ChainService) SDB() *state.ChainStateDB {
return cs.sdb
}
// CDBReader returns cs.sdb as a consensus.ChainDbReader.
func (cs *ChainService) CDBReader() consensus.ChainDbReader {
return cs.cdb
}
// SetChainConsensus sets cs.cc to cc.
func (cs *ChainService) SetChainConsensus(cc consensus.ChainConsensus) {
cs.ChainConsensus = cc
cs.cdb.cc = cc
}
// BeforeStart initialize chain database and generate empty genesis block if necessary
func (cs *ChainService) BeforeStart() {
}
// AfterStart ... do nothing
func (cs *ChainService) AfterStart() {
cs.chainManager.Start()
cs.chainWorker.Start()
}
// ChainSync synchronize with peer
func (cs *ChainService) ChainSync(peerID peer.ID, remoteBestHash []byte) {
// handlt it like normal block (orphan)
logger.Debug().Msg("Best Block Request")
anchors := cs.getAnchorsFromHash(remoteBestHash)
hashes := make([]message.BlockHash, 0)
for _, a := range anchors {
hashes = append(hashes, message.BlockHash(a))
logger.Debug().Str("hash", enc.ToString(a)).Msg("request blocks for sync")
}
cs.RequestTo(message.P2PSvc, &message.GetMissingBlocks{ToWhom: peerID, Hashes: hashes})
}
// BeforeStop close chain database and stop BlockValidator
func (cs *ChainService) BeforeStop() {
cs.Close()
cs.chainManager.Stop()
cs.chainWorker.Stop()
cs.validator.Stop()
}
func (cs *ChainService) notifyBlock(block *types.Block) {
cs.BaseComponent.RequestTo(message.P2PSvc,
&message.NotifyNewBlock{
BlockNo: block.Header.BlockNo,
Block: block,
})
}
// Receive actor message
func (cs *ChainService) Receive(context actor.Context) {
switch msg := context.Message().(type) {
case *message.AddBlock,
*message.GetAnchors, //TODO move to ChainWorker (need chain lock)
*message.GetMissing,
*message.GetAncestor,
*message.GetQuery:
cs.chainManager.Request(msg, context.Sender())
//pass to chainWorker
case *message.GetBlock,
*message.GetBlockByNo,
*message.GetState,
*message.GetStateAndProof,
*message.GetTx,
*message.GetReceipt,
*message.GetABI,
*message.GetStateQuery,
*message.SyncBlockState,
*message.GetElected,
*message.GetVote,
*message.GetStaking:
cs.chainWorker.Request(msg, context.Sender())
//handle directly
case *message.GetBestBlockNo:
context.Respond(message.GetBestBlockNoRsp{
BlockNo: cs.getBestBlockNo(),
})
case *message.GetBestBlock:
block, err := cs.GetBestBlock()
if err != nil {
logger.Error().Err(err).Msg("failed to get best block")
}
context.Respond(message.GetBestBlockRsp{
Block: block,
Err: err,
})
case *message.MemPoolDelRsp:
err := msg.Err
if err != nil {
logger.Error().Err(err).Msg("failed to remove txs from mempool")
}
case actor.SystemMessage,
actor.AutoReceiveMessage,
actor.NotInfluenceReceiveTimeout:
//logger.Debugf("Received message. (%v) %s", reflect.TypeOf(msg), msg)
default:
//logger.Debugf("Missed message. (%v) %s", reflect.TypeOf(msg), msg)
}
}
func (cs *ChainService) Statistics() *map[string]interface{} {
return &map[string]interface{}{
"orphan": cs.op.curCnt,
}
}
func (cs *ChainService) GetChainTree() ([]byte, error) {
return cs.cdb.GetChainTree()
}
func (cs *ChainService) getVotes(n int) (*types.VoteList, error) {
scs, err := cs.sdb.GetStateDB().OpenContractStateAccount(types.ToAccountID([]byte(types.AergoSystem)))
if err != nil {
return nil, err
}
return system.GetVoteResult(scs)
}
func (cs *ChainService) getVote(addr []byte) (*types.VoteList, error) {
scs, err := cs.sdb.GetStateDB().OpenContractStateAccount(types.ToAccountID([]byte(types.AergoSystem)))
if err != nil {
return nil, err
}
var voteList types.VoteList
var tmp []*types.Vote
voteList.Votes = tmp
vote, err := system.GetVote(scs, addr)
if err != nil {
return nil, err
}
to := vote.GetCandidate()
for offset := 0; offset < len(to); offset += system.PeerIDLength {
vote := &types.Vote{
Candidate: to[offset : offset+system.PeerIDLength],
Amount: vote.GetAmount(),
}
voteList.Votes = append(voteList.Votes, vote)
}
return &voteList, nil
}
func (cs *ChainService) getStaking(addr []byte) (*types.Staking, error) {
scs, err := cs.sdb.GetStateDB().OpenContractStateAccount(types.ToAccountID([]byte(types.AergoSystem)))
if err != nil {
return nil, err
}
staking, err := system.GetStaking(scs, addr)
if err != nil {
return nil, err
}
return staking, nil
}
type ChainManager struct {
*SubComponent
IChainHandler //to use chain APIs
*Core // TODO remove after moving GetQuery to ChainWorker
}
type ChainWorker struct {
*SubComponent
IChainHandler //to use chain APIs
*Core
}
var (
chainManagerName = "Chain Manager"
chainWorkerName = "Chain Worker"
)
func newChainManager(cs *ChainService, core *Core) *ChainManager {
chainManager := &ChainManager{IChainHandler: cs, Core: core}
chainManager.SubComponent = NewSubComponent(chainManager, cs.BaseComponent, chainManagerName, 1)
return chainManager
}
func newChainWorker(cs *ChainService, cntWorker int, core *Core) *ChainWorker {
chainWorker := &ChainWorker{IChainHandler: cs, Core: core}
chainWorker.SubComponent = NewSubComponent(chainWorker, cs.BaseComponent, chainWorkerName, cntWorker)
return chainWorker
}
func (cm *ChainManager) Receive(context actor.Context) {
logger.Debug().Msg("chain manager")
switch msg := context.Message().(type) {
case *message.AddBlock:
runtime.LockOSThread()
defer runtime.UnlockOSThread()
bid := msg.Block.BlockID()
block := msg.Block
logger.Debug().Str("hash", msg.Block.ID()).
Uint64("blockNo", msg.Block.GetHeader().GetBlockNo()).Bool("syncer", msg.IsSync).Msg("add block chainservice")
_, err := cm.getBlock(bid[:])
if err == nil {
logger.Debug().Str("hash", msg.Block.ID()).Msg("already exist")
err = ErrBlockExist
} else {
var bstate *state.BlockState
if msg.Bstate != nil {
bstate = msg.Bstate.(*state.BlockState)
}
err = cm.addBlock(block, bstate, msg.PeerID)
if err != nil && err != ErrBlockOrphan {
logger.Error().Err(err).Str("hash", msg.Block.ID()).Msg("failed add block")
}
}
rsp := message.AddBlockRsp{
BlockNo: block.GetHeader().GetBlockNo(),
BlockHash: block.BlockHash(),
Err: err,
}
context.Respond(&rsp)
cm.TellTo(message.RPCSvc, block)
case *message.GetMissing:
stopHash := msg.StopHash
hashes := msg.Hashes
topHash, topNo, stopNo := cm.handleMissing(stopHash, hashes)
context.Respond(message.GetMissingRsp{
TopMatched: topHash,
TopNumber: topNo,
StopNumber: stopNo,
})
case *message.GetAnchors:
anchor, lastNo, err := cm.getAnchorsNew()
context.Respond(message.GetAnchorsRsp{
Hashes: anchor,
LastNo: lastNo,
Err: err,
})
case *message.GetAncestor:
hashes := msg.Hashes
ancestor, err := cm.findAncestor(hashes)
context.Respond(message.GetAncestorRsp{
Ancestor: ancestor,
Err: err,
})
case *message.GetQuery: //TODO move to ChainWorker (Currently, contract doesn't support parallel execution)
runtime.LockOSThread()
defer runtime.UnlockOSThread()
ctrState, err := cm.sdb.GetStateDB().OpenContractStateAccount(types.ToAccountID(msg.Contract))
if err != nil {
logger.Error().Str("hash", enc.ToString(msg.Contract)).Err(err).Msg("failed to get state for contract")
context.Respond(message.GetQueryRsp{Result: nil, Err: err})
} else {
bs := state.NewBlockState(cm.sdb.OpenNewStateDB(cm.sdb.GetRoot()))
ret, err := contract.Query(msg.Contract, bs, ctrState, msg.Queryinfo)
context.Respond(message.GetQueryRsp{Result: ret, Err: err})
}
default:
debug := fmt.Sprintf("[%s] Missed message. (%v) %s", cm.name, reflect.TypeOf(msg), msg)
logger.Debug().Msg(debug)
}
}
func (cw *ChainWorker) Receive(context actor.Context) {
logger.Debug().Msg("chain worker")
switch msg := context.Message().(type) {
case *message.GetBlock:
bid := types.ToBlockID(msg.BlockHash)
block, err := cw.getBlock(bid[:])
if err != nil {
logger.Debug().Err(err).Str("hash", enc.ToString(msg.BlockHash)).Msg("block not found")
}
context.Respond(message.GetBlockRsp{
Block: block,
Err: err,
})
case *message.GetBlockByNo:
block, err := cw.getBlockByNo(msg.BlockNo)
if err != nil {
logger.Error().Err(err).Uint64("blockNo", msg.BlockNo).Msg("failed to get block by no")
}
context.Respond(message.GetBlockByNoRsp{
Block: block,
Err: err,
})
case *message.GetState:
id := types.ToAccountID(msg.Account)
accState, err := cw.sdb.GetStateDB().GetAccountState(id)
if err != nil {
logger.Error().Str("hash", enc.ToString(msg.Account)).Err(err).Msg("failed to get state for account")
}
context.Respond(message.GetStateRsp{
State: accState,
Err: err,
})
case *message.GetStateAndProof:
id := types.ToAccountID(msg.Account)
stateProof, err := cw.sdb.GetStateDB().GetStateAndProof(id[:], msg.Root, msg.Compressed)
if err != nil {
logger.Error().Str("hash", enc.ToString(msg.Account)).Err(err).Msg("failed to get state for account")
}
context.Respond(message.GetStateAndProofRsp{
StateProof: stateProof,
Err: err,
})
case *message.GetTx:
tx, txIdx, err := cw.getTx(msg.TxHash)
context.Respond(message.GetTxRsp{
Tx: tx,
TxIds: txIdx,
Err: err,
})
case *message.GetReceipt:
receipt, err := cw.getReceipt(msg.TxHash)
context.Respond(message.GetReceiptRsp{
Receipt: receipt,
Err: err,
})
case *message.GetABI:
contractState, err := cw.sdb.GetStateDB().OpenContractStateAccount(types.ToAccountID(msg.Contract))
if err == nil {
abi, err := contract.GetABI(contractState)
context.Respond(message.GetABIRsp{
ABI: abi,
Err: err,
})
} else {
context.Respond(message.GetABIRsp{
ABI: nil,
Err: err,
})
}
case *message.GetStateQuery:
var varProof *types.ContractVarProof
var contractProof *types.StateProof
var err error
id := types.ToAccountID(msg.ContractAddress)
contractProof, err = cw.sdb.GetStateDB().GetStateAndProof(id[:], msg.Root, msg.Compressed)
if err != nil {
logger.Error().Str("hash", enc.ToString(msg.ContractAddress)).Err(err).Msg("failed to get state for account")
} else if contractProof.Inclusion {
contractTrieRoot := contractProof.State.StorageRoot
varId := bytes.NewBufferString("_")
varId.WriteString(msg.VarName)
varId.WriteString(msg.VarIndex)
varTrieKey := common.Hasher(varId.Bytes())
varProof, err = cw.sdb.GetStateDB().GetVarAndProof(varTrieKey, contractTrieRoot, msg.Compressed)
if err != nil {
logger.Error().Str("hash", enc.ToString(msg.ContractAddress)).Err(err).Msg("failed to get state variable in contract")
}
}
stateQuery := &types.StateQueryProof{
ContractProof: contractProof,
VarProof: varProof,
}
context.Respond(message.GetStateQueryRsp{
Result: stateQuery,
Err: err,
})
case *message.SyncBlockState:
cw.checkBlockHandshake(msg.PeerID, msg.BlockNo, msg.BlockHash)
case *message.GetElected:
top, err := cw.getVotes(msg.N)
context.Respond(&message.GetVoteRsp{
Top: top,
Err: err,
})
case *message.GetVote:
top, err := cw.getVote(msg.Addr)
context.Respond(&message.GetVoteRsp{
Top: top,
Err: err,
})
case *message.GetStaking:
staking, err := cw.getStaking(msg.Addr)
context.Respond(&message.GetStakingRsp{
Staking: staking,
Err: err,
})
default:
debug := fmt.Sprintf("[%s] Missed message. (%v) %s", cw.name, reflect.TypeOf(msg), msg)
logger.Debug().Msg(debug)
}
}