-
Notifications
You must be signed in to change notification settings - Fork 10
/
api_backend.go
346 lines (293 loc) · 10.7 KB
/
api_backend.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
// Copyright 2015 The go-ethereum Authors
// This file is part of the go-ethereum library.
//
// The go-ethereum library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The go-ethereum library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
package cpc
import (
"context"
"errors"
"math/big"
"bitbucket.org/cpchain/chain/accounts"
"bitbucket.org/cpchain/chain/api/rpc"
"bitbucket.org/cpchain/chain/configs"
"bitbucket.org/cpchain/chain/consensus/dpor"
"bitbucket.org/cpchain/chain/core"
"bitbucket.org/cpchain/chain/core/bloombits"
"bitbucket.org/cpchain/chain/core/rawdb"
"bitbucket.org/cpchain/chain/core/state"
"bitbucket.org/cpchain/chain/core/vm"
"bitbucket.org/cpchain/chain/database"
"bitbucket.org/cpchain/chain/protocols/cpc/gasprice"
"bitbucket.org/cpchain/chain/protocols/cpc/syncer"
"bitbucket.org/cpchain/chain/types"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/math"
"github.com/ethereum/go-ethereum/event"
)
var (
errNilBlock = errors.New("nil block")
errInvalidProposersList = errors.New("invalid proposers list")
)
// APIBackend implements cpcapi.Backend for full nodes
type APIBackend struct {
cpc *CpchainService
gpo *gasprice.Oracle
}
// ChainConfig returns the active chain configuration.
func (b *APIBackend) ChainConfig() *configs.ChainConfig {
return b.cpc.chainConfig
}
func (b *APIBackend) CurrentBlock() *types.Block {
return b.cpc.blockchain.CurrentBlock()
}
func (b *APIBackend) SetHead(number uint64) {
b.cpc.blockchain.SetHead(number)
}
func (b *APIBackend) HeaderByNumber(ctx context.Context, blockNr rpc.BlockNumber) (*types.Header, error) {
// Pending block is only known by the miner
if blockNr == rpc.PendingBlockNumber {
block := b.cpc.miner.PendingBlock()
return block.Header(), nil
}
// Otherwise resolve and return the block
if blockNr == rpc.LatestBlockNumber {
return b.cpc.blockchain.CurrentBlock().Header(), nil
}
return b.cpc.blockchain.GetHeaderByNumber(uint64(blockNr)), nil
}
func (b *APIBackend) BlockByNumber(ctx context.Context, blockNr rpc.BlockNumber) (*types.Block, error) {
// Pending block is only known by the miner
if blockNr == rpc.PendingBlockNumber {
block := b.cpc.miner.PendingBlock()
return block, nil
}
// Otherwise resolve and return the block
if blockNr == rpc.LatestBlockNumber {
return b.cpc.blockchain.CurrentBlock(), nil
}
return b.cpc.blockchain.GetBlockByNumber(uint64(blockNr)), nil
}
func (b *APIBackend) StateAndHeaderByNumber(ctx context.Context, blockNr rpc.BlockNumber, isPrivate bool) (*state.StateDB, *types.Header, error) {
// Pending state is only known by the miner
if blockNr == rpc.PendingBlockNumber {
block, state := b.cpc.miner.Pending()
return state, block.Header(), nil
}
// Otherwise resolve the block number and return its state
header, err := b.HeaderByNumber(ctx, blockNr)
if header == nil || err != nil {
return nil, nil, err
}
var stateDb *state.StateDB
if isPrivate {
stateDb, err = b.cpc.BlockChain().StatePrivAt(header.StateRoot)
} else {
stateDb, err = b.cpc.BlockChain().StateAt(header.StateRoot)
}
return stateDb, header, err
}
func (b *APIBackend) GetBlock(ctx context.Context, hash common.Hash) (*types.Block, error) {
return b.cpc.blockchain.GetBlockByHash(hash), nil
}
func (b *APIBackend) GetReceipts(ctx context.Context, hash common.Hash) (types.Receipts, error) {
if number := rawdb.ReadHeaderNumber(b.cpc.chainDb, hash); number != nil {
return rawdb.ReadReceipts(b.cpc.chainDb, hash, *number), nil
}
return nil, nil
}
func (b *APIBackend) GetPrivateReceipt(ctx context.Context, txHash common.Hash) (*types.Receipt, error) {
receipt, err := core.ReadPrivateReceipt(txHash, b.ChainDb())
if err != nil {
return nil, err
}
return receipt, nil
}
func (b *APIBackend) GetLogs(ctx context.Context, hash common.Hash) ([][]*types.Log, error) {
number := rawdb.ReadHeaderNumber(b.cpc.chainDb, hash)
if number == nil {
return nil, nil
}
receipts := rawdb.ReadReceipts(b.cpc.chainDb, hash, *number)
if receipts == nil {
return nil, nil
}
logs := make([][]*types.Log, len(receipts))
for i, receipt := range receipts {
logs[i] = receipt.Logs
}
return logs, nil
}
func (b *APIBackend) GetEVM(ctx context.Context, msg core.Message, state *state.StateDB, header *types.Header, vmCfg vm.Config) (*vm.EVM, func() error, error) {
state.SetBalance(msg.From(), math.MaxBig256)
vmError := func() error { return nil }
context := core.NewEVMContext(msg, header, b.cpc.BlockChain(), nil)
return vm.NewEVM(context, state, b.cpc.chainConfig, vmCfg), vmError, nil
}
func (b *APIBackend) SubscribeRemovedLogsEvent(ch chan<- core.RemovedLogsEvent) event.Subscription {
return b.cpc.BlockChain().SubscribeRemovedLogsEvent(ch)
}
func (b *APIBackend) SubscribeChainEvent(ch chan<- core.ChainEvent) event.Subscription {
return b.cpc.BlockChain().SubscribeChainEvent(ch)
}
func (b *APIBackend) SubscribeChainHeadEvent(ch chan<- core.ChainHeadEvent) event.Subscription {
return b.cpc.BlockChain().SubscribeChainHeadEvent(ch)
}
func (b *APIBackend) SubscribeChainSideEvent(ch chan<- core.ChainSideEvent) event.Subscription {
return b.cpc.BlockChain().SubscribeChainSideEvent(ch)
}
func (b *APIBackend) SubscribeLogsEvent(ch chan<- []*types.Log) event.Subscription {
return b.cpc.BlockChain().SubscribeLogsEvent(ch)
}
func (b *APIBackend) SendTx(ctx context.Context, signedTx *types.Transaction) error {
return b.cpc.txPool.AddLocal(signedTx)
}
func (b *APIBackend) GetPoolTransactions() (types.Transactions, error) {
pending, err := b.cpc.txPool.Pending()
if err != nil {
return nil, err
}
var txs types.Transactions
for _, batch := range pending {
txs = append(txs, batch...)
}
return txs, nil
}
func (b *APIBackend) GetPoolTransaction(hash common.Hash) *types.Transaction {
return b.cpc.txPool.Get(hash)
}
func (b *APIBackend) GetPoolNonce(ctx context.Context, addr common.Address) (uint64, error) {
return b.cpc.txPool.State().GetNonce(addr), nil
}
func (b *APIBackend) Stats() (pending int, queued int) {
return b.cpc.txPool.Stats()
}
func (b *APIBackend) TxPoolContent() (map[common.Address]types.Transactions, map[common.Address]types.Transactions) {
return b.cpc.TxPool().Content()
}
func (b *APIBackend) SubscribeNewTxsEvent(ch chan<- core.NewTxsEvent) event.Subscription {
return b.cpc.TxPool().SubscribeNewTxsEvent(ch)
}
func (b *APIBackend) Downloader() syncer.Syncer {
return b.cpc.Downloader()
}
func (b *APIBackend) ProtocolVersion() int {
return b.cpc.CpcVersion()
}
func (b *APIBackend) SuggestPrice(ctx context.Context) (*big.Int, error) {
return b.gpo.SuggestPrice(ctx)
}
func (b *APIBackend) ChainDb() database.Database {
return b.cpc.ChainDb()
}
func (b *APIBackend) EventMux() *event.TypeMux {
return b.cpc.EventMux()
}
func (b *APIBackend) AccountManager() *accounts.Manager {
return b.cpc.AccountManager()
}
func (b *APIBackend) BloomStatus() (uint64, uint64) {
sections, _, _ := b.cpc.bloomIndexer.Sections()
return configs.BloomBitsBlocks, sections
}
func (b *APIBackend) ServiceFilter(ctx context.Context, session *bloombits.MatcherSession) {
for i := 0; i < bloomFilterThreads; i++ {
go session.Multiplex(bloomRetrievalBatch, bloomRetrievalWait, b.cpc.bloomRequests)
}
}
// RemoteDB returns remote database instance.
func (b *APIBackend) RemoteDB() database.RemoteDatabase {
return b.cpc.RemoteDB()
}
// RNode returns current RNode information
func (b *APIBackend) RNode() ([]common.Address, uint64) {
block := b.cpc.blockchain.CurrentBlock()
bn := block.Number()
api := b.cpc.engine.(*dpor.Dpor).APIs(b.cpc.blockchain)
rNodes, _ := api[0].Service.(*dpor.API).GetRNodes()
return rNodes, bn.Uint64()
}
// CurrentProposerIndex return current proposer index, (0,1,...,11)
func (b *APIBackend) CurrentProposerIndex() uint64 {
block := b.cpc.blockchain.CurrentBlock()
bn := block.Number()
vl, tl := b.ViewLen(), b.TermLen()
// be cautious vl*tl does not overflow
proposerIndex := ((bn.Uint64() - 1) % (vl * tl)) % tl
return proposerIndex
}
// CurrentView return current view, (0,1,2)
func (b *APIBackend) CurrentView() uint64 {
block := b.cpc.blockchain.CurrentBlock()
bn := block.Number()
vl, tl := b.ViewLen(), b.TermLen()
span := ((bn.Uint64() - 1) % (vl * tl)) / tl
return span
}
// CurrentTerm return current term
func (b *APIBackend) CurrentTerm() uint64 {
block := b.cpc.blockchain.CurrentBlock()
bn := block.Number()
vl, tl := b.ViewLen(), b.TermLen()
term := (bn.Uint64() - 1) / (vl * tl)
return term
}
// ViewLen return current ViewLen
func (b *APIBackend) ViewLen() uint64 {
return b.cpc.chainConfig.Dpor.ViewLen
}
// TermLen return current TermLen
func (b *APIBackend) TermLen() uint64 {
return b.cpc.chainConfig.Dpor.TermLen
}
// CommitteMember return current committe
func (b *APIBackend) CommitteMember() []common.Address {
block := b.cpc.blockchain.CurrentBlock()
return block.Header().Dpor.Proposers
}
func (b *APIBackend) CalcRptInfo(address common.Address, addresses []common.Address, blockNum uint64) int64 {
return b.cpc.engine.(*dpor.Dpor).GetCalcRptInfo(address, addresses, blockNum)
}
func (b *APIBackend) BlockReward(blockNum rpc.BlockNumber) *big.Int {
return b.cpc.engine.(*dpor.Dpor).GetBlockReward(uint64(blockNum))
}
func (b *APIBackend) ProposerOf(blockNum rpc.BlockNumber) (common.Address, error) {
proposers, err := b.Proposers(blockNum)
if err != nil {
return common.Address{}, err
}
vl, tl := b.ViewLen(), b.TermLen()
// be cautious vl*tl does not overflow
view := ((uint64(blockNum) - 1) % (vl * tl)) % tl
if len(proposers) > int(view) {
return proposers[int(view)], nil
}
return common.Address{}, errInvalidProposersList
}
// Proposers returns block Proposers information
func (b *APIBackend) Proposers(blockNum rpc.BlockNumber) ([]common.Address, error) {
block := b.cpc.BlockChain().GetBlockByNumber(uint64(blockNum))
if block == nil {
return []common.Address{}, errNilBlock
}
proposers := block.Dpor().Proposers
return proposers, nil
}
// Validators returns current block Validators information
func (b *APIBackend) Validators(blockNr rpc.BlockNumber) ([]common.Address, error) {
api := b.cpc.engine.(*dpor.Dpor).APIs(b.cpc.blockchain)
return api[0].Service.(*dpor.API).GetValidators(blockNr)
}
func (b *APIBackend) SupportPrivateTx(ctx context.Context) (bool, error) {
return types.SupportTxType(types.PrivateTx), nil
}