-
Notifications
You must be signed in to change notification settings - Fork 321
/
chainservice.go
175 lines (151 loc) · 5.57 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
// Copyright (c) 2019 IoTeX Foundation
// This is an alpha (internal) release and is not suitable for production. This source code is provided 'as is' and no
// warranties are given as to title or non-infringement, merchantability or fitness for purpose and, to the extent
// permitted by law, all liability for your use of the code is disclaimed. This source code is governed by Apache
// License 2.0 that can be found in the LICENSE file.
package chainservice
import (
"context"
"github.com/libp2p/go-libp2p-core/peer"
"github.com/pkg/errors"
"google.golang.org/protobuf/proto"
"github.com/iotexproject/iotex-election/committee"
"github.com/iotexproject/iotex-proto/golang/iotexrpc"
"github.com/iotexproject/iotex-proto/golang/iotextypes"
"github.com/iotexproject/iotex-core/action"
"github.com/iotexproject/iotex-core/action/protocol"
"github.com/iotexproject/iotex-core/action/protocol/poll"
"github.com/iotexproject/iotex-core/action/protocol/staking"
"github.com/iotexproject/iotex-core/actpool"
"github.com/iotexproject/iotex-core/api"
"github.com/iotexproject/iotex-core/blockchain"
"github.com/iotexproject/iotex-core/blockchain/block"
"github.com/iotexproject/iotex-core/blockchain/blockdao"
"github.com/iotexproject/iotex-core/blockindex"
"github.com/iotexproject/iotex-core/blocksync"
"github.com/iotexproject/iotex-core/config"
"github.com/iotexproject/iotex-core/consensus"
"github.com/iotexproject/iotex-core/p2p"
"github.com/iotexproject/iotex-core/pkg/lifecycle"
"github.com/iotexproject/iotex-core/pkg/log"
"github.com/iotexproject/iotex-core/state/factory"
)
// ChainService is a blockchain service with all blockchain components.
type ChainService struct {
lifecycle lifecycle.Lifecycle
actpool actpool.ActPool
blocksync blocksync.BlockSync
consensus consensus.Consensus
chain blockchain.Blockchain
factory factory.Factory
blockdao blockdao.BlockDAO
p2pAgent p2p.Agent
electionCommittee committee.Committee
// TODO: explorer dependency deleted at #1085, need to api related params
indexer blockindex.Indexer
bfIndexer blockindex.BloomFilterIndexer
candidateIndexer *poll.CandidateIndexer
candBucketsIndexer *staking.CandidatesBucketsIndexer
registry *protocol.Registry
}
// Start starts the server
func (cs *ChainService) Start(ctx context.Context) error {
return cs.lifecycle.OnStartSequentially(ctx)
}
// Stop stops the server
func (cs *ChainService) Stop(ctx context.Context) error {
return cs.lifecycle.OnStopSequentially(ctx)
}
// ReportFullness switch on or off block sync
func (cs *ChainService) ReportFullness(_ context.Context, _ iotexrpc.MessageType, fullness float32) {
}
// HandleAction handles incoming action request.
func (cs *ChainService) HandleAction(ctx context.Context, actPb *iotextypes.Action) error {
act, err := (&action.Deserializer{}).SetEvmNetworkID(cs.chain.EvmNetworkID()).ActionToSealedEnvelope(actPb)
if err != nil {
return err
}
ctx = protocol.WithRegistry(ctx, cs.registry)
err = cs.actpool.Add(ctx, act)
if err != nil {
log.L().Debug(err.Error())
}
return err
}
// HandleBlock handles incoming block request.
func (cs *ChainService) HandleBlock(ctx context.Context, peer string, pbBlock *iotextypes.Block) error {
blk, err := block.NewDeserializer(cs.chain.EvmNetworkID()).FromBlockProto(pbBlock)
if err != nil {
return err
}
ctx, err = cs.chain.Context(ctx)
if err != nil {
return err
}
return cs.blocksync.ProcessBlock(ctx, peer, blk)
}
// HandleSyncRequest handles incoming sync request.
func (cs *ChainService) HandleSyncRequest(ctx context.Context, peer peer.AddrInfo, sync *iotexrpc.BlockSync) error {
return cs.blocksync.ProcessSyncRequest(ctx, peer, sync.Start, sync.End)
}
// HandleConsensusMsg handles incoming consensus message.
func (cs *ChainService) HandleConsensusMsg(msg *iotextypes.ConsensusMessage) error {
return cs.consensus.HandleConsensusMsg(msg)
}
// ChainID returns ChainID.
func (cs *ChainService) ChainID() uint32 { return cs.chain.ChainID() }
// Blockchain returns the Blockchain
func (cs *ChainService) Blockchain() blockchain.Blockchain {
return cs.chain
}
// StateFactory returns the state factory
func (cs *ChainService) StateFactory() factory.Factory {
return cs.factory
}
// BlockDAO returns the blockdao
func (cs *ChainService) BlockDAO() blockdao.BlockDAO {
return cs.blockdao
}
// ActionPool returns the Action pool
func (cs *ChainService) ActionPool() actpool.ActPool {
return cs.actpool
}
// Consensus returns the consensus instance
func (cs *ChainService) Consensus() consensus.Consensus {
return cs.consensus
}
// BlockSync returns the block syncer
func (cs *ChainService) BlockSync() blocksync.BlockSync {
return cs.blocksync
}
// Registry returns a pointer to the registry
func (cs *ChainService) Registry() *protocol.Registry { return cs.registry }
// NewAPIServer creates a new api server
func (cs *ChainService) NewAPIServer(cfg config.API, plugins map[int]interface{}) (*api.ServerV2, error) {
if cfg.GRPCPort == 0 && cfg.HTTPPort == 0 {
return nil, nil
}
p2pAgent := cs.p2pAgent
apiServerOptions := []api.Option{
api.WithBroadcastOutbound(func(ctx context.Context, chainID uint32, msg proto.Message) error {
return p2pAgent.BroadcastOutbound(ctx, msg)
}),
api.WithNativeElection(cs.electionCommittee),
}
svr, err := api.NewServerV2(
cfg,
cs.chain,
cs.blocksync,
cs.factory,
cs.blockdao,
cs.indexer,
cs.bfIndexer,
cs.actpool,
cs.registry,
apiServerOptions...,
)
if err != nil {
return nil, errors.Wrap(err, "failed to create API server")
}
return svr, nil
}