-
Notifications
You must be signed in to change notification settings - Fork 179
/
impl.go
409 lines (338 loc) · 9.56 KB
/
impl.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
package simulation
import (
"encoding/binary"
"github.com/okex/exchain/x/evm"
"sync"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
ethcrypto "github.com/ethereum/go-ethereum/crypto"
"github.com/okex/exchain/app/types"
"github.com/okex/exchain/libs/cosmos-sdk/codec"
store "github.com/okex/exchain/libs/cosmos-sdk/store/types"
sdk "github.com/okex/exchain/libs/cosmos-sdk/types"
"github.com/okex/exchain/libs/cosmos-sdk/x/auth"
authexported "github.com/okex/exchain/libs/cosmos-sdk/x/auth/exported"
"github.com/okex/exchain/libs/cosmos-sdk/x/mint"
"github.com/okex/exchain/libs/cosmos-sdk/x/params"
"github.com/okex/exchain/libs/cosmos-sdk/x/supply"
"github.com/okex/exchain/x/ammswap"
"github.com/okex/exchain/x/backend"
"github.com/okex/exchain/x/dex"
distr "github.com/okex/exchain/x/distribution"
evmtypes "github.com/okex/exchain/x/evm/types"
"github.com/okex/exchain/x/evm/watcher"
"github.com/okex/exchain/x/farm"
"github.com/okex/exchain/x/gov"
"github.com/okex/exchain/x/order"
"github.com/okex/exchain/x/staking"
"github.com/okex/exchain/x/token"
)
type QueryOnChainProxy interface {
GetAccount(address common.Address) (*types.EthAccount, error)
GetStorageAtInternal(address common.Address, key []byte) (hexutil.Bytes, error)
GetCodeByHash(hash common.Hash) (hexutil.Bytes, error)
}
// AccountKeeper defines the expected account keeper interface
type AccountKeeperProxy struct {
cachedAcc map[string]*types.EthAccount
queryOnChainProxy QueryOnChainProxy
q *watcher.Querier
}
func NewAccountKeeperProxy(qoc QueryOnChainProxy) AccountKeeperProxy {
return AccountKeeperProxy{
cachedAcc: make(map[string]*types.EthAccount, 0),
queryOnChainProxy: qoc,
q: watcher.NewQuerier(),
}
}
func (a AccountKeeperProxy) SetObserverKeeper(observer auth.ObserverI) {
}
func (a AccountKeeperProxy) NewAccountWithAddress(ctx sdk.Context, addr sdk.AccAddress) authexported.Account {
acc := types.EthAccount{
BaseAccount: &auth.BaseAccount{},
CodeHash: ethcrypto.Keccak256(nil),
}
acc.SetAddress(addr)
a.cachedAcc[addr.String()] = &acc
return &acc
}
func (a AccountKeeperProxy) GetAllAccounts(ctx sdk.Context) (accounts []authexported.Account) {
return nil
}
func (a AccountKeeperProxy) IterateAccounts(ctx sdk.Context, cb func(account authexported.Account) bool) {
return
}
func (a AccountKeeperProxy) GetAccount(ctx sdk.Context, addr sdk.AccAddress) authexported.Account {
acc, ok := a.cachedAcc[addr.String()]
if ok {
return acc
}
account, e := a.queryOnChainProxy.GetAccount(common.BytesToAddress(addr.Bytes()))
if e != nil {
//query account from chain
return nil
}
return account
}
func (a AccountKeeperProxy) SetAccount(ctx sdk.Context, account authexported.Account) {
acc, ok := account.(types.EthAccount)
if !ok {
return
}
a.cachedAcc[account.GetAddress().String()] = &acc
return
}
func (a AccountKeeperProxy) RemoveAccount(ctx sdk.Context, account authexported.Account) {
return
}
type SupplyKeeperProxy struct {
}
func (s SupplyKeeperProxy) SendCoinsFromModuleToAccount(ctx sdk.Context, senderModule string, recipientAddr sdk.AccAddress, amt sdk.Coins) error {
return nil
}
type SubspaceProxy struct {
q *watcher.Querier
}
func NewSubspaceProxy() SubspaceProxy {
return SubspaceProxy{
q: watcher.NewQuerier(),
}
}
func (p SubspaceProxy) GetParamSet(ctx sdk.Context, ps params.ParamSet) {
pr, err := p.q.GetParams()
if err == nil {
evmParam := ps.(*evmtypes.Params)
evmParam.MaxGasLimitPerTx = pr.MaxGasLimitPerTx
evmParam.EnableCall = pr.EnableCall
evmParam.EnableContractBlockedList = pr.EnableContractBlockedList
evmParam.EnableCreate = pr.EnableCreate
evmParam.ExtraEIPs = pr.ExtraEIPs
evmParam.EnableContractDeploymentWhitelist = pr.EnableContractDeploymentWhitelist
}
}
func (p SubspaceProxy) SetParamSet(ctx sdk.Context, ps params.ParamSet) {
}
type BankKeeperProxy struct {
blacklistedAddrs map[string]bool
}
func NewBankKeeperProxy() BankKeeperProxy {
modAccAddrs := make(map[string]bool)
maccPerms := map[string][]string{
auth.FeeCollectorName: nil,
distr.ModuleName: nil,
mint.ModuleName: {supply.Minter},
staking.BondedPoolName: {supply.Burner, supply.Staking},
staking.NotBondedPoolName: {supply.Burner, supply.Staking},
gov.ModuleName: nil,
token.ModuleName: {supply.Minter, supply.Burner},
dex.ModuleName: nil,
order.ModuleName: nil,
backend.ModuleName: nil,
ammswap.ModuleName: {supply.Minter, supply.Burner},
farm.ModuleName: nil,
farm.YieldFarmingAccount: nil,
farm.MintFarmingAccount: {supply.Burner},
}
for acc := range maccPerms {
modAccAddrs[supply.NewModuleAddress(acc).String()] = true
}
return BankKeeperProxy{blacklistedAddrs: modAccAddrs}
}
func (b BankKeeperProxy) BlacklistedAddr(addr sdk.AccAddress) bool {
return b.blacklistedAddrs[addr.String()]
}
type InternalDba struct {
dbPrefix []byte
ocProxy QueryOnChainProxy
}
var (
gSimulateCdc *codec.Codec
cdcOnce sync.Once
gSimulateChainConfig []byte
configOnce sync.Once
)
func instanceOfCdc() *codec.Codec {
cdcOnce.Do(func() {
module := evm.AppModuleBasic{}
cdc := codec.New()
module.RegisterCodec(cdc)
gSimulateCdc = cdc
})
return gSimulateCdc
}
func instanceOfChainConfig() []byte {
configOnce.Do(func() {
cdc := instanceOfCdc()
gSimulateChainConfig = cdc.MustMarshalBinaryBare(evmtypes.DefaultChainConfig())
})
return gSimulateChainConfig
}
func NewInternalDba(qoc QueryOnChainProxy) InternalDba {
return InternalDba{ocProxy: qoc}
}
func (i InternalDba) NewStore(parent store.KVStore, Prefix []byte) evmtypes.StoreProxy {
i.dbPrefix = Prefix
if Prefix == nil {
return nil
}
switch Prefix[0] {
case evmtypes.KeyPrefixChainConfig[0]:
return ConfigStore{defaultConfig: instanceOfChainConfig()}
case evmtypes.KeyPrefixBloom[0]:
return BloomStore{}
case evmtypes.KeyPrefixStorage[0]:
if len(Prefix) < 21 {
return nil
}
return StateStore{addr: common.BytesToAddress(Prefix[1:21]), ocProxy: i.ocProxy}
case evmtypes.KeyPrefixContractBlockedList[0]:
return ContractBlockedListStore{watcher.NewQuerier()}
case evmtypes.KeyPrefixContractDeploymentWhitelist[0]:
return ContractDeploymentWhitelist{watcher.NewQuerier()}
case evmtypes.KeyPrefixCode[0]:
return CodeStore{q: watcher.NewQuerier(), ocProxy: i.ocProxy}
case evmtypes.KeyPrefixHeightHash[0]:
return HeightHashStore{watcher.NewQuerier()}
case evmtypes.KeyPrefixBlockHash[0]:
return BlockHashStore{}
}
return nil
}
type HeightHashStore struct {
q *watcher.Querier
}
func (s HeightHashStore) Set(key, value []byte) {
//just ignore all set opt
}
func (s HeightHashStore) Get(key []byte) []byte {
h, _ := s.q.GetBlockHashByNumber(binary.BigEndian.Uint64(key))
return h.Bytes()
}
func (s HeightHashStore) Has(key []byte) bool {
return false
}
func (s HeightHashStore) Delete(key []byte) {
return
}
type BlockHashStore struct {
}
func (s BlockHashStore) Set(key, value []byte) {
//just ignore all set opt
}
func (s BlockHashStore) Get(key []byte) []byte {
return nil
}
func (s BlockHashStore) Has(key []byte) bool {
return false
}
func (s BlockHashStore) Delete(key []byte) {
return
}
type StateStore struct {
addr common.Address
ocProxy QueryOnChainProxy
}
func (s StateStore) Set(key, value []byte) {
//just ignore all set opt
}
func (s StateStore) Get(key []byte) []byte {
//include code and state
b, e := s.ocProxy.GetStorageAtInternal(s.addr, key)
if e != nil {
return nil
}
return b
}
func (s StateStore) Has(key []byte) bool {
return false
}
func (s StateStore) Delete(key []byte) {
return
}
type ConfigStore struct {
defaultConfig []byte
}
func (s ConfigStore) Set(key, value []byte) {
//just ignore all set opt
return
}
func (s ConfigStore) Get(key []byte) []byte {
return s.defaultConfig
}
func (s ConfigStore) Delete(key []byte) {
return
}
func (s ConfigStore) Has(key []byte) bool {
return false
}
type BloomStore struct {
}
func (s BloomStore) Set(key, value []byte) {
//just ignore all set opt
}
func (s BloomStore) Get(key []byte) []byte {
return nil
}
func (s BloomStore) Delete(key []byte) {
return
}
func (s BloomStore) Has(key []byte) bool {
return false
}
type CodeStore struct {
q *watcher.Querier
ocProxy QueryOnChainProxy
}
func (s CodeStore) Set(key, value []byte) {
//just ignore all set opt
}
func (s CodeStore) Get(key []byte) []byte {
//include code and state
b, e := s.ocProxy.GetCodeByHash(common.BytesToHash(key))
if e != nil {
return nil
}
return b
}
func (s CodeStore) Delete(key []byte) {
return
}
func (s CodeStore) Has(key []byte) bool {
return false
}
type ContractBlockedListStore struct {
q *watcher.Querier
}
func (s ContractBlockedListStore) Set(key, value []byte) {
//just ignore all set opt
}
func (s ContractBlockedListStore) Get(key []byte) []byte {
//include code and state
value, err := s.q.GetContractMethodBlockedList(key)
if err != nil {
return nil
}
return value
}
func (s ContractBlockedListStore) Delete(key []byte) {
return
}
func (s ContractBlockedListStore) Has(key []byte) bool {
return s.q.HasContractBlockedList(key)
}
type ContractDeploymentWhitelist struct {
q *watcher.Querier
}
func (s ContractDeploymentWhitelist) Set(key, value []byte) {
//just ignore all set opt
}
func (s ContractDeploymentWhitelist) Get(key []byte) []byte {
//include code and state
return nil
}
func (s ContractDeploymentWhitelist) Delete(key []byte) {
return
}
func (s ContractDeploymentWhitelist) Has(key []byte) bool {
return s.q.HasContractDeploymentWhitelist(key)
}