github.com/fibonacci-chain/fbc@v0.0.0-20231124064014-c7636198c1e9/app/rpc/namespaces/eth/simulation/impl.go (about) 1 package simulation 2 3 import ( 4 "encoding/binary" 5 "sync" 6 7 "github.com/ethereum/go-ethereum/common" 8 "github.com/ethereum/go-ethereum/common/hexutil" 9 ethcrypto "github.com/ethereum/go-ethereum/crypto" 10 11 "github.com/fibonacci-chain/fbc/app/types" 12 "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/codec" 13 store "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/store/types" 14 sdk "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/types" 15 "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/x/auth" 16 authexported "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/x/auth/exported" 17 "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/x/mint" 18 "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/x/params" 19 "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/x/supply" 20 "github.com/fibonacci-chain/fbc/x/ammswap" 21 "github.com/fibonacci-chain/fbc/x/dex" 22 distr "github.com/fibonacci-chain/fbc/x/distribution" 23 "github.com/fibonacci-chain/fbc/x/evm" 24 evmtypes "github.com/fibonacci-chain/fbc/x/evm/types" 25 "github.com/fibonacci-chain/fbc/x/evm/watcher" 26 "github.com/fibonacci-chain/fbc/x/farm" 27 "github.com/fibonacci-chain/fbc/x/gov" 28 "github.com/fibonacci-chain/fbc/x/order" 29 "github.com/fibonacci-chain/fbc/x/staking" 30 "github.com/fibonacci-chain/fbc/x/token" 31 ) 32 33 type QueryOnChainProxy interface { 34 GetAccount(address common.Address) (*types.EthAccount, error) 35 GetStorageAtInternal(address common.Address, key []byte) (hexutil.Bytes, error) 36 GetCodeByHash(hash common.Hash) (hexutil.Bytes, error) 37 GetCodec() *codec.Codec 38 } 39 40 // AccountKeeper defines the expected account keeper interface 41 type AccountKeeperProxy struct { 42 cachedAcc map[string]*types.EthAccount 43 queryOnChainProxy QueryOnChainProxy 44 q *watcher.Querier 45 } 46 47 func NewAccountKeeperProxy(qoc QueryOnChainProxy) AccountKeeperProxy { 48 return AccountKeeperProxy{ 49 cachedAcc: make(map[string]*types.EthAccount, 0), 50 queryOnChainProxy: qoc, 51 q: watcher.NewQuerier(), 52 } 53 } 54 55 func (a AccountKeeperProxy) SetObserverKeeper(observer auth.ObserverI) { 56 } 57 58 func (a AccountKeeperProxy) NewAccountWithAddress(ctx sdk.Context, addr sdk.AccAddress) authexported.Account { 59 acc := types.EthAccount{ 60 BaseAccount: &auth.BaseAccount{}, 61 CodeHash: ethcrypto.Keccak256(nil), 62 } 63 acc.SetAddress(addr) 64 a.cachedAcc[addr.String()] = &acc 65 return &acc 66 } 67 68 func (a AccountKeeperProxy) GetAllAccounts(ctx sdk.Context) (accounts []authexported.Account) { 69 return nil 70 } 71 72 func (a AccountKeeperProxy) IterateAccounts(ctx sdk.Context, cb func(account authexported.Account) bool) { 73 return 74 } 75 76 func (a AccountKeeperProxy) GetAccount(ctx sdk.Context, addr sdk.AccAddress) authexported.Account { 77 acc, ok := a.cachedAcc[addr.String()] 78 if ok { 79 return acc 80 } 81 account, e := a.queryOnChainProxy.GetAccount(common.BytesToAddress(addr.Bytes())) 82 if e != nil { 83 //query account from chain 84 return nil 85 } 86 return account 87 } 88 89 func (a AccountKeeperProxy) SetAccount(ctx sdk.Context, account authexported.Account) { 90 acc, ok := account.(types.EthAccount) 91 if !ok { 92 return 93 } 94 a.cachedAcc[account.GetAddress().String()] = &acc 95 return 96 } 97 98 func (a AccountKeeperProxy) RemoveAccount(ctx sdk.Context, account authexported.Account) { 99 return 100 } 101 102 type SupplyKeeperProxy struct { 103 } 104 105 func (s SupplyKeeperProxy) SendCoinsFromModuleToAccount(ctx sdk.Context, senderModule string, recipientAddr sdk.AccAddress, amt sdk.Coins) error { 106 return nil 107 } 108 109 type SubspaceProxy struct { 110 q *watcher.Querier 111 } 112 113 func (p SubspaceProxy) CustomKVStore(ctx sdk.Context) sdk.KVStore { 114 panic("implement me") 115 } 116 117 func NewSubspaceProxy() SubspaceProxy { 118 return SubspaceProxy{ 119 q: watcher.NewQuerier(), 120 } 121 } 122 123 func (p SubspaceProxy) GetParamSet(ctx sdk.Context, ps params.ParamSet) { 124 pr, err := p.q.GetParams() 125 if err == nil { 126 evmParam := ps.(*evmtypes.Params) 127 evmParam.MaxGasLimitPerTx = pr.MaxGasLimitPerTx 128 evmParam.EnableCall = pr.EnableCall 129 evmParam.EnableContractBlockedList = pr.EnableContractBlockedList 130 evmParam.EnableCreate = pr.EnableCreate 131 evmParam.ExtraEIPs = pr.ExtraEIPs 132 evmParam.EnableContractDeploymentWhitelist = pr.EnableContractDeploymentWhitelist 133 } 134 135 } 136 137 func (p SubspaceProxy) RegisterSignal(handler func()) { 138 139 } 140 141 func (p SubspaceProxy) SetParamSet(ctx sdk.Context, ps params.ParamSet) { 142 143 } 144 145 type BankKeeperProxy struct { 146 blacklistedAddrs map[string]bool 147 } 148 149 func NewBankKeeperProxy() BankKeeperProxy { 150 modAccAddrs := make(map[string]bool) 151 maccPerms := map[string][]string{ 152 auth.FeeCollectorName: nil, 153 distr.ModuleName: nil, 154 mint.ModuleName: {supply.Minter}, 155 staking.BondedPoolName: {supply.Burner, supply.Staking}, 156 staking.NotBondedPoolName: {supply.Burner, supply.Staking}, 157 gov.ModuleName: nil, 158 token.ModuleName: {supply.Minter, supply.Burner}, 159 dex.ModuleName: nil, 160 order.ModuleName: nil, 161 ammswap.ModuleName: {supply.Minter, supply.Burner}, 162 farm.ModuleName: nil, 163 farm.YieldFarmingAccount: nil, 164 farm.MintFarmingAccount: {supply.Burner}, 165 } 166 167 for acc := range maccPerms { 168 modAccAddrs[supply.NewModuleAddress(acc).String()] = true 169 } 170 return BankKeeperProxy{blacklistedAddrs: modAccAddrs} 171 } 172 173 func (b BankKeeperProxy) BlacklistedAddr(addr sdk.AccAddress) bool { 174 return b.blacklistedAddrs[addr.String()] 175 } 176 177 type StakingKeeperProxy struct { 178 } 179 180 func (s StakingKeeperProxy) IsValidator(ctx sdk.Context, addr sdk.AccAddress) bool { 181 return true 182 } 183 184 type InternalDba struct { 185 dbPrefix []byte 186 ocProxy QueryOnChainProxy 187 } 188 189 var ( 190 gSimulateCdc *codec.Codec 191 cdcOnce sync.Once 192 gSimulateChainConfig []byte 193 configOnce sync.Once 194 ) 195 196 func instanceOfCdc() *codec.Codec { 197 cdcOnce.Do(func() { 198 module := evm.AppModuleBasic{} 199 cdc := codec.New() 200 module.RegisterCodec(cdc) 201 gSimulateCdc = cdc 202 }) 203 return gSimulateCdc 204 } 205 206 func instanceOfChainConfig() []byte { 207 configOnce.Do(func() { 208 cdc := instanceOfCdc() 209 gSimulateChainConfig = cdc.MustMarshalBinaryBare(evmtypes.DefaultChainConfig()) 210 }) 211 return gSimulateChainConfig 212 } 213 214 func NewInternalDba(qoc QueryOnChainProxy) InternalDba { 215 return InternalDba{ocProxy: qoc} 216 } 217 218 func (i InternalDba) NewStore(parent store.KVStore, Prefix []byte) evmtypes.StoreProxy { 219 i.dbPrefix = Prefix 220 if Prefix == nil { 221 return nil 222 } 223 224 switch Prefix[0] { 225 case evmtypes.KeyPrefixChainConfig[0]: 226 return ConfigStore{defaultConfig: instanceOfChainConfig()} 227 case evmtypes.KeyPrefixBloom[0]: 228 return BloomStore{} 229 case evmtypes.KeyPrefixStorage[0]: 230 if len(Prefix) < 21 { 231 return nil 232 } 233 return StateStore{addr: common.BytesToAddress(Prefix[1:21]), ocProxy: i.ocProxy} 234 case evmtypes.KeyPrefixContractBlockedList[0]: 235 return ContractBlockedListStore{watcher.NewQuerier()} 236 case evmtypes.KeyPrefixContractDeploymentWhitelist[0]: 237 return ContractDeploymentWhitelist{watcher.NewQuerier()} 238 case evmtypes.KeyPrefixCode[0]: 239 return CodeStore{q: watcher.NewQuerier(), ocProxy: i.ocProxy} 240 case evmtypes.KeyPrefixHeightHash[0]: 241 return HeightHashStore{watcher.NewQuerier()} 242 case evmtypes.KeyPrefixBlockHash[0]: 243 return BlockHashStore{} 244 } 245 return nil 246 } 247 248 type HeightHashStore struct { 249 q *watcher.Querier 250 } 251 252 func (s HeightHashStore) Set(key, value []byte) { 253 //just ignore all set opt 254 } 255 256 func (s HeightHashStore) Get(key []byte) []byte { 257 h, _ := s.q.GetBlockHashByNumber(binary.BigEndian.Uint64(key)) 258 return h.Bytes() 259 } 260 261 func (s HeightHashStore) Has(key []byte) bool { 262 return false 263 } 264 265 func (s HeightHashStore) Delete(key []byte) { 266 return 267 } 268 269 type BlockHashStore struct { 270 } 271 272 func (s BlockHashStore) Set(key, value []byte) { 273 //just ignore all set opt 274 } 275 276 func (s BlockHashStore) Get(key []byte) []byte { 277 278 return nil 279 } 280 281 func (s BlockHashStore) Has(key []byte) bool { 282 return false 283 } 284 285 func (s BlockHashStore) Delete(key []byte) { 286 return 287 } 288 289 type StateStore struct { 290 addr common.Address 291 ocProxy QueryOnChainProxy 292 } 293 294 func (s StateStore) Set(key, value []byte) { 295 //just ignore all set opt 296 } 297 298 func (s StateStore) Get(key []byte) []byte { 299 //include code and state 300 b, e := s.ocProxy.GetStorageAtInternal(s.addr, key) 301 if e != nil { 302 return nil 303 } 304 return b 305 } 306 307 func (s StateStore) Has(key []byte) bool { 308 return false 309 } 310 311 func (s StateStore) Delete(key []byte) { 312 return 313 } 314 315 type ConfigStore struct { 316 defaultConfig []byte 317 } 318 319 func (s ConfigStore) Set(key, value []byte) { 320 //just ignore all set opt 321 return 322 } 323 324 func (s ConfigStore) Get(key []byte) []byte { 325 return s.defaultConfig 326 } 327 328 func (s ConfigStore) Delete(key []byte) { 329 return 330 } 331 332 func (s ConfigStore) Has(key []byte) bool { 333 return false 334 } 335 336 type BloomStore struct { 337 } 338 339 func (s BloomStore) Set(key, value []byte) { 340 //just ignore all set opt 341 } 342 343 func (s BloomStore) Get(key []byte) []byte { 344 return nil 345 } 346 347 func (s BloomStore) Delete(key []byte) { 348 return 349 } 350 351 func (s BloomStore) Has(key []byte) bool { 352 return false 353 } 354 355 type CodeStore struct { 356 q *watcher.Querier 357 ocProxy QueryOnChainProxy 358 } 359 360 func (s CodeStore) Set(key, value []byte) { 361 //just ignore all set opt 362 } 363 364 func (s CodeStore) Get(key []byte) []byte { 365 //include code and state 366 b, e := s.ocProxy.GetCodeByHash(common.BytesToHash(key)) 367 if e != nil { 368 return nil 369 } 370 return b 371 } 372 373 func (s CodeStore) Delete(key []byte) { 374 return 375 } 376 377 func (s CodeStore) Has(key []byte) bool { 378 return false 379 } 380 381 type ContractBlockedListStore struct { 382 q *watcher.Querier 383 } 384 385 func (s ContractBlockedListStore) Set(key, value []byte) { 386 //just ignore all set opt 387 } 388 389 func (s ContractBlockedListStore) Get(key []byte) []byte { 390 //include code and state 391 value, err := s.q.GetContractMethodBlockedList(key) 392 if err != nil { 393 return nil 394 } 395 return value 396 } 397 398 func (s ContractBlockedListStore) Delete(key []byte) { 399 return 400 } 401 402 func (s ContractBlockedListStore) Has(key []byte) bool { 403 return s.q.HasContractBlockedList(key) 404 } 405 406 type ContractDeploymentWhitelist struct { 407 q *watcher.Querier 408 } 409 410 func (s ContractDeploymentWhitelist) Set(key, value []byte) { 411 //just ignore all set opt 412 } 413 414 func (s ContractDeploymentWhitelist) Get(key []byte) []byte { 415 //include code and state 416 return nil 417 } 418 419 func (s ContractDeploymentWhitelist) Delete(key []byte) { 420 return 421 } 422 423 func (s ContractDeploymentWhitelist) Has(key []byte) bool { 424 return s.q.HasContractDeploymentWhitelist(key) 425 }