/
local_backend.go
340 lines (293 loc) · 11.3 KB
/
local_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
// Modifications Copyright 2019 The klaytn Authors
// 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/>.
//
// This file is derived from accounts/abi/bind/backends/simulated.go (2018/06/04).
// Modified and improved for the klaytn development.
package sc
import (
"context"
"errors"
"fmt"
"math/big"
"time"
"github.com/klaytn/klaytn"
"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/common/math"
"github.com/klaytn/klaytn/event"
"github.com/klaytn/klaytn/networks/rpc"
"github.com/klaytn/klaytn/node/cn/filters"
"github.com/klaytn/klaytn/params"
"github.com/klaytn/klaytn/storage/database"
)
const defaultGasPrice = 50 * params.Ston
var errBlockNumberUnsupported = errors.New("LocalBackend cannot access blocks other than the latest block")
var errGasEstimationFailed = errors.New("gas required exceeds allowance or always failing transaction")
// TODO-Klaytn currently LocalBackend is only for ServiceChain, especially Bridge SmartContract
type LocalBackend struct {
subbrige *SubBridge
events *filters.EventSystem // Event system for filtering log events live
config *params.ChainConfig
}
func NewLocalBackend(main *SubBridge) (*LocalBackend, error) {
return &LocalBackend{
subbrige: main,
config: main.blockchain.Config(),
events: filters.NewEventSystem(main.EventMux(), &filterLocalBackend{main}, false),
}, nil
}
func (lb *LocalBackend) CodeAt(ctx context.Context, contract common.Address, blockNumber *big.Int) ([]byte, error) {
if blockNumber != nil && blockNumber.Cmp(lb.subbrige.blockchain.CurrentBlock().Number()) != 0 {
return nil, errBlockNumberUnsupported
}
statedb, _ := lb.subbrige.blockchain.State()
return statedb.GetCode(contract), nil
}
func (lb *LocalBackend) BalanceAt(ctx context.Context, account common.Address, blockNumber *big.Int) (*big.Int, error) {
if blockNumber != nil && blockNumber.Cmp(lb.subbrige.blockchain.CurrentBlock().Number()) != 0 {
return nil, errBlockNumberUnsupported
}
statedb, _ := lb.subbrige.blockchain.State()
return statedb.GetBalance(account), nil
}
func (lb *LocalBackend) CallContract(ctx context.Context, call klaytn.CallMsg, blockNumber *big.Int) ([]byte, error) {
if blockNumber != nil && blockNumber.Cmp(lb.subbrige.blockchain.CurrentBlock().Number()) != 0 {
return nil, errBlockNumberUnsupported
}
currentState, err := lb.subbrige.blockchain.State()
if err != nil {
return nil, err
}
rval, _, _, err := lb.callContract(ctx, call, lb.subbrige.blockchain.CurrentBlock(), currentState)
return rval, err
}
func (b *LocalBackend) callContract(ctx context.Context, call klaytn.CallMsg, block *types.Block, statedb *state.StateDB) ([]byte, uint64, bool, error) {
// Set default gas & gas price if none were set
gas, gasPrice := uint64(call.Gas), call.GasPrice
if gas == 0 {
gas = math.MaxUint64 / 2
}
if gasPrice == nil || gasPrice.Sign() == 0 {
gasPrice = new(big.Int).SetUint64(defaultGasPrice)
}
intrinsicGas, err := types.IntrinsicGas(call.Data, call.To == nil, b.config.Rules(block.Number()))
if err != nil {
return nil, 0, false, err
}
// Create new call message
msg := types.NewMessage(call.From, call.To, 0, call.Value, gas, gasPrice, call.Data, false, intrinsicGas)
// Setup context so it may be cancelled the call has completed
// or, in case of unmetered gas, setup a context with a timeout.
ctx, cancel := context.WithTimeout(ctx, 5*time.Second)
// Make sure the context is cancelled when the call has completed
// this makes sure resources are cleaned up.
defer cancel()
statedb.SetBalance(msg.ValidatedSender(), math.MaxBig256)
vmError := func() error { return nil }
context := blockchain.NewEVMContext(msg, block.Header(), b.subbrige.blockchain, nil)
evm := vm.NewEVM(context, statedb, b.config, &vm.Config{})
// Wait for the context to be done and cancel the evm. Even if the
// EVM has finished, cancelling may be done (repeatedly)
go func() {
<-ctx.Done()
evm.Cancel(vm.CancelByCtxDone)
}()
res, gas, kerr := blockchain.ApplyMessage(evm, msg)
err = kerr.ErrTxInvalid
if err := vmError(); err != nil {
return nil, 0, false, err
}
// Propagate error of Receipt as JSON RPC error
if err == nil {
err = blockchain.GetVMerrFromReceiptStatus(kerr.Status)
}
return res, gas, kerr.Status != types.ReceiptStatusSuccessful, err
}
func (lb *LocalBackend) PendingCodeAt(ctx context.Context, contract common.Address) ([]byte, error) {
// TODO-Klaytn this is not pending code but latest code
return lb.CodeAt(ctx, contract, lb.subbrige.blockchain.CurrentBlock().Number())
}
func (lb *LocalBackend) PendingNonceAt(ctx context.Context, account common.Address) (uint64, error) {
return lb.subbrige.txPool.GetPendingNonce(account), nil
}
func (lb *LocalBackend) SuggestGasPrice(ctx context.Context) (*big.Int, error) {
return new(big.Int).SetUint64(lb.config.UnitPrice), nil
}
func (lb *LocalBackend) EstimateGas(ctx context.Context, call klaytn.CallMsg) (gas uint64, err error) {
// Binary search the gas requirement, as it may be higher than the amount used
var (
lo uint64 = params.TxGas - 1
hi uint64
cap uint64
)
if uint64(call.Gas) >= params.TxGas {
hi = uint64(call.Gas)
} else {
hi = params.UpperGasLimit
}
cap = hi
// Create a helper to check if a gas allowance results in an executable transaction
executable := func(gas uint64) bool {
call.Gas = gas
currentState, err := lb.subbrige.blockchain.State()
if err != nil {
return false
}
_, _, failed, err := lb.callContract(ctx, call, lb.subbrige.blockchain.CurrentBlock(), currentState)
if err != nil || failed {
return false
}
return true
}
// Execute the binary search and hone in on an executable gas limit
for lo+1 < hi {
mid := (hi + lo) / 2
if !executable(mid) {
lo = mid
} else {
hi = mid
}
}
// Reject the transaction as invalid if it still fails at the highest allowance
if hi == cap {
if !executable(hi) {
return 0, fmt.Errorf("gas required exceeds allowance or always failing transaction")
}
}
return hi, nil
}
func (lb *LocalBackend) SendTransaction(ctx context.Context, tx *types.Transaction) error {
return lb.subbrige.txPool.AddLocal(tx)
}
// ChainID can return the chain ID of the chain.
func (lb *LocalBackend) ChainID(ctx context.Context) (*big.Int, error) {
return lb.config.ChainID, nil
}
func (lb *LocalBackend) TransactionReceipt(ctx context.Context, txHash common.Hash) (*types.Receipt, error) {
receipt := lb.subbrige.blockchain.GetReceiptByTxHash(txHash)
if receipt != nil {
return receipt, nil
}
return nil, errors.New("receipt is not exist")
}
func (lb *LocalBackend) FilterLogs(ctx context.Context, query klaytn.FilterQuery) ([]types.Log, error) {
// Convert the RPC block numbers into internal representations
if query.FromBlock == nil {
query.FromBlock = big.NewInt(rpc.LatestBlockNumber.Int64())
}
if query.ToBlock == nil {
query.ToBlock = big.NewInt(rpc.LatestBlockNumber.Int64())
}
from := query.FromBlock.Int64()
to := query.ToBlock.Int64()
// Construct and execute the filter
filter := filters.NewRangeFilter(&filterLocalBackend{lb.subbrige}, from, to, query.Addresses, query.Topics)
logs, err := filter.Logs(ctx)
if err != nil {
return nil, err
}
res := make([]types.Log, len(logs))
for i, log := range logs {
res[i] = *log
}
return res, nil
}
func (lb *LocalBackend) SubscribeFilterLogs(ctx context.Context, query klaytn.FilterQuery, ch chan<- types.Log) (klaytn.Subscription, error) {
// Subscribe to contract events
sink := make(chan []*types.Log)
sub, err := lb.events.SubscribeLogs(query, sink)
if err != nil {
return nil, err
}
// Since we're getting logs in batches, we need to flatten them into a plain stream
return event.NewSubscription(func(quit <-chan struct{}) error {
defer sub.Unsubscribe()
for {
select {
case logs := <-sink:
for _, log := range logs {
select {
case ch <- *log:
case err := <-sub.Err():
return err
case <-quit:
return nil
}
}
case err := <-sub.Err():
return err
case <-quit:
return nil
}
}
}), nil
}
// CurrentBlockNumber returns a current block number.
func (lb *LocalBackend) CurrentBlockNumber(ctx context.Context) (uint64, error) {
return lb.subbrige.blockchain.CurrentBlock().NumberU64(), nil
}
type filterLocalBackend struct {
subbridge *SubBridge
}
func (fb *filterLocalBackend) ChainDB() database.DBManager {
// TODO-Klaytn consider chain's chainDB instead of bridge's chainDB currently.
return fb.subbridge.chainDB
}
func (fb *filterLocalBackend) EventMux() *event.TypeMux {
// TODO-Klaytn consider chain's eventMux instead of bridge's eventMux currently.
return fb.subbridge.EventMux()
}
func (fb *filterLocalBackend) HeaderByNumber(ctx context.Context, block rpc.BlockNumber) (*types.Header, error) {
// TODO-Klaytn consider pendingblock instead of latest block
if block == rpc.LatestBlockNumber {
return fb.subbridge.blockchain.CurrentHeader(), nil
}
return fb.subbridge.blockchain.GetHeaderByNumber(uint64(block.Int64())), nil
}
func (fb *filterLocalBackend) GetBlockReceipts(ctx context.Context, hash common.Hash) types.Receipts {
return fb.subbridge.blockchain.GetReceiptsByBlockHash(hash)
}
func (fb *filterLocalBackend) GetLogs(ctx context.Context, hash common.Hash) ([][]*types.Log, error) {
return fb.subbridge.blockchain.GetLogsByHash(hash), nil
}
func (fb *filterLocalBackend) SubscribeNewTxsEvent(ch chan<- blockchain.NewTxsEvent) event.Subscription {
return fb.subbridge.txPool.SubscribeNewTxsEvent(ch)
}
func (fb *filterLocalBackend) SubscribeChainEvent(ch chan<- blockchain.ChainEvent) event.Subscription {
return fb.subbridge.blockchain.SubscribeChainEvent(ch)
}
func (fb *filterLocalBackend) SubscribeRemovedLogsEvent(ch chan<- blockchain.RemovedLogsEvent) event.Subscription {
return fb.subbridge.blockchain.SubscribeRemovedLogsEvent(ch)
}
func (fb *filterLocalBackend) SubscribeLogsEvent(ch chan<- []*types.Log) event.Subscription {
return fb.subbridge.blockchain.SubscribeLogsEvent(ch)
}
func (fb *filterLocalBackend) BloomStatus() (uint64, uint64) {
// TODO-Klaytn consider this number of sections.
// BloomBitsBlocks (const : 4096), the number of processed sections maintained by the chain indexer
return 4096, 0
}
func (fb *filterLocalBackend) ServiceFilter(ctx context.Context, session *bloombits.MatcherSession) {
// TODO-Klaytn this method should implmentation to support indexed tag in solidity
//for i := 0; i < bloomFilterThreads; i++ {
// go session.Multiplex(bloomRetrievalBatch, bloomRetrievalWait, backend.bloomRequests)
//}
}