/
api_backend.go
265 lines (221 loc) · 9.22 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
// Modifications Copyright 2018 The klaytn Authors
// Copyright 2015 The go-ethereum Authors
// This file is part of go-ethereum.
//
// 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/>.
//
// This file is derived from eth/api_backend.go (2018/06/04).
// Modified and improved for the klaytn development.
package cn
import (
"context"
"fmt"
"math/big"
"github.com/klaytn/klaytn"
"github.com/klaytn/klaytn/accounts"
"github.com/klaytn/klaytn/blockchain"
"github.com/klaytn/klaytn/blockchain/bloombits"
"github.com/klaytn/klaytn/blockchain/state"
"github.com/klaytn/klaytn/blockchain/types"
"github.com/klaytn/klaytn/blockchain/vm"
"github.com/klaytn/klaytn/common"
"github.com/klaytn/klaytn/event"
"github.com/klaytn/klaytn/kerrors"
"github.com/klaytn/klaytn/networks/rpc"
"github.com/klaytn/klaytn/node/cn/gasprice"
"github.com/klaytn/klaytn/params"
"github.com/klaytn/klaytn/storage/database"
)
// CNAPIBackend implements api.Backend for full nodes
type CNAPIBackend struct {
cn *CN
gpo *gasprice.Oracle
}
// GetTxLookupInfoAndReceipt retrieves a tx and lookup info and receipt for a given transaction hash.
func (b *CNAPIBackend) GetTxLookupInfoAndReceipt(ctx context.Context, txHash common.Hash) (*types.Transaction, common.Hash, uint64, uint64, *types.Receipt) {
return b.cn.blockchain.GetTxLookupInfoAndReceipt(txHash)
}
// GetTxAndLookupInfoInCache retrieves a tx and lookup info for a given transaction hash in cache.
func (b *CNAPIBackend) GetTxAndLookupInfoInCache(txHash common.Hash) (*types.Transaction, common.Hash, uint64, uint64) {
return b.cn.blockchain.GetTxAndLookupInfoInCache(txHash)
}
// GetBlockReceiptsInCache retrieves receipts for a given block hash in cache.
func (b *CNAPIBackend) GetBlockReceiptsInCache(blockHash common.Hash) types.Receipts {
return b.cn.blockchain.GetBlockReceiptsInCache(blockHash)
}
// GetTxLookupInfoAndReceiptInCache retrieves a tx and lookup info and receipt for a given transaction hash in cache.
func (b *CNAPIBackend) GetTxLookupInfoAndReceiptInCache(txHash common.Hash) (*types.Transaction, common.Hash, uint64, uint64, *types.Receipt) {
return b.cn.blockchain.GetTxLookupInfoAndReceiptInCache(txHash)
}
func (b *CNAPIBackend) ChainConfig() *params.ChainConfig {
return b.cn.chainConfig
}
func (b *CNAPIBackend) CurrentBlock() *types.Block {
return b.cn.blockchain.CurrentBlock()
}
func (b *CNAPIBackend) SetHead(number uint64) {
//b.cn.protocolManager.downloader.Cancel()
b.cn.blockchain.SetHead(number)
}
func (b *CNAPIBackend) HeaderByNumber(ctx context.Context, blockNr rpc.BlockNumber) (*types.Header, error) {
// Pending block is only known by the miner
if blockNr == rpc.PendingBlockNumber {
return nil, kerrors.ErrPendingBlockNotSupported
}
// Otherwise resolve and return the block
if blockNr == rpc.LatestBlockNumber {
return b.cn.blockchain.CurrentBlock().Header(), nil
}
header := b.cn.blockchain.GetHeaderByNumber(uint64(blockNr))
if header == nil {
return nil, fmt.Errorf("the block does not exist (block number: %d)", blockNr)
}
return header, nil
}
func (b *CNAPIBackend) BlockByNumber(ctx context.Context, blockNr rpc.BlockNumber) (*types.Block, error) {
// Pending block is only known by the miner
if blockNr == rpc.PendingBlockNumber {
return nil, kerrors.ErrPendingBlockNotSupported
}
// Otherwise resolve and return the block
if blockNr == rpc.LatestBlockNumber {
return b.cn.blockchain.CurrentBlock(), nil
}
block := b.cn.blockchain.GetBlockByNumber(uint64(blockNr))
if block == nil {
return nil, fmt.Errorf("the block does not exist (block number: %d)", blockNr)
}
return block, nil
}
func (b *CNAPIBackend) StateAndHeaderByNumber(ctx context.Context, blockNr rpc.BlockNumber) (*state.StateDB, *types.Header, error) {
// Pending state is only known by the miner
if blockNr == rpc.PendingBlockNumber {
return nil, nil, kerrors.ErrPendingBlockNotSupported
}
// Otherwise resolve the block number and return its state
header, err := b.HeaderByNumber(ctx, blockNr)
if header == nil || err != nil {
return nil, nil, err
}
stateDb, err := b.cn.BlockChain().StateAt(header.Root)
return stateDb, header, err
}
func (b *CNAPIBackend) GetBlock(ctx context.Context, hash common.Hash) (*types.Block, error) {
block := b.cn.blockchain.GetBlockByHash(hash)
if block == nil {
return nil, fmt.Errorf("the block does not exist (block hash: %s)", hash.String())
}
return block, nil
}
// GetTxAndLookupInfo retrieves a tx and lookup info for a given transaction hash.
func (b *CNAPIBackend) GetTxAndLookupInfo(hash common.Hash) (*types.Transaction, common.Hash, uint64, uint64) {
return b.cn.blockchain.GetTxAndLookupInfo(hash)
}
// GetBlockReceipts retrieves the receipts for all transactions with given block hash.
func (b *CNAPIBackend) GetBlockReceipts(ctx context.Context, hash common.Hash) types.Receipts {
return b.cn.blockchain.GetReceiptsByBlockHash(hash)
}
func (b *CNAPIBackend) GetLogs(ctx context.Context, hash common.Hash) ([][]*types.Log, error) {
return b.cn.blockchain.GetLogsByHash(hash), nil
}
func (b *CNAPIBackend) GetTd(blockHash common.Hash) *big.Int {
return b.cn.blockchain.GetTdByHash(blockHash)
}
func (b *CNAPIBackend) GetEVM(ctx context.Context, msg blockchain.Message, state *state.StateDB, header *types.Header, vmCfg vm.Config) (*vm.EVM, func() error, error) {
// Add gas fee to sender for estimating gasLimit/computing cost or calling a function by insufficient balance sender.
state.AddBalance(msg.ValidatedSender(), new(big.Int).Mul(new(big.Int).SetUint64(msg.Gas()), msg.GasPrice()))
vmError := func() error { return nil }
context := blockchain.NewEVMContext(msg, header, b.cn.BlockChain(), nil)
return vm.NewEVM(context, state, b.cn.chainConfig, &vmCfg), vmError, nil
}
func (b *CNAPIBackend) SubscribeRemovedLogsEvent(ch chan<- blockchain.RemovedLogsEvent) event.Subscription {
return b.cn.BlockChain().SubscribeRemovedLogsEvent(ch)
}
func (b *CNAPIBackend) SubscribeChainEvent(ch chan<- blockchain.ChainEvent) event.Subscription {
return b.cn.BlockChain().SubscribeChainEvent(ch)
}
func (b *CNAPIBackend) SubscribeChainHeadEvent(ch chan<- blockchain.ChainHeadEvent) event.Subscription {
return b.cn.BlockChain().SubscribeChainHeadEvent(ch)
}
func (b *CNAPIBackend) SubscribeChainSideEvent(ch chan<- blockchain.ChainSideEvent) event.Subscription {
return b.cn.BlockChain().SubscribeChainSideEvent(ch)
}
func (b *CNAPIBackend) SubscribeLogsEvent(ch chan<- []*types.Log) event.Subscription {
return b.cn.BlockChain().SubscribeLogsEvent(ch)
}
func (b *CNAPIBackend) SendTx(ctx context.Context, signedTx *types.Transaction) error {
return b.cn.txPool.AddLocal(signedTx)
}
func (b *CNAPIBackend) GetPoolTransactions() (types.Transactions, error) {
pending, err := b.cn.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 *CNAPIBackend) GetPoolTransaction(hash common.Hash) *types.Transaction {
return b.cn.txPool.Get(hash)
}
func (b *CNAPIBackend) GetPoolNonce(ctx context.Context, addr common.Address) uint64 {
return b.cn.txPool.GetPendingNonce(addr)
}
func (b *CNAPIBackend) Stats() (pending int, queued int) {
return b.cn.txPool.Stats()
}
func (b *CNAPIBackend) TxPoolContent() (map[common.Address]types.Transactions, map[common.Address]types.Transactions) {
return b.cn.TxPool().Content()
}
func (b *CNAPIBackend) SubscribeNewTxsEvent(ch chan<- blockchain.NewTxsEvent) event.Subscription {
return b.cn.TxPool().SubscribeNewTxsEvent(ch)
}
func (b *CNAPIBackend) Progress() klaytn.SyncProgress {
return b.cn.Progress()
}
func (b *CNAPIBackend) ProtocolVersion() int {
return b.cn.ProtocolVersion()
}
func (b *CNAPIBackend) SuggestPrice(ctx context.Context) (*big.Int, error) {
return b.gpo.SuggestPrice(ctx)
}
func (b *CNAPIBackend) ChainDB() database.DBManager {
return b.cn.ChainDB()
}
func (b *CNAPIBackend) EventMux() *event.TypeMux {
return b.cn.EventMux()
}
func (b *CNAPIBackend) AccountManager() accounts.AccountManager {
return b.cn.AccountManager()
}
func (b *CNAPIBackend) BloomStatus() (uint64, uint64) {
sections, _, _ := b.cn.bloomIndexer.Sections()
return params.BloomBitsBlocks, sections
}
func (b *CNAPIBackend) ServiceFilter(ctx context.Context, session *bloombits.MatcherSession) {
for i := 0; i < bloomFilterThreads; i++ {
go session.Multiplex(bloomRetrievalBatch, bloomRetrievalWait, b.cn.bloomRequests)
}
}
func (b *CNAPIBackend) IsParallelDBWrite() bool {
return b.cn.BlockChain().IsParallelDBWrite()
}
func (b *CNAPIBackend) IsSenderTxHashIndexingEnabled() bool {
return b.cn.BlockChain().IsSenderTxHashIndexingEnabled()
}
func (b *CNAPIBackend) RPCGasCap() *big.Int {
return b.cn.config.RPCGasCap
}