eth/filters: reuse genesisAlloc

Signed-off-by: jsvisa <delweng@gmail.com>
This commit is contained in:
jsvisa 2023-07-25 21:56:00 +08:00
parent 3e9bc42f4c
commit b187581ee8

View file

@ -900,6 +900,20 @@ var (
contract = common.HexToAddress("0000000000000000000000000000000000031ec7")
// Transfer(address indexed from, address indexed to, uint256 value);
topic = common.HexToHash("0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef")
genesisAlloc = core.GenesisAlloc{
// // SPDX-License-Identifier: GPL-3.0
// pragma solidity >=0.7.0 <0.9.0;
//
// contract Token {
// event Transfer(address indexed from, address indexed to, uint256 value);
// function transfer(address to, uint256 value) public returns (bool) {
// emit Transfer(msg.sender, to, value);
// return true;
// }
// }
contract: {Balance: big.NewInt(params.Ether), Code: common.FromHex("0x608060405234801561001057600080fd5b506004361061002b5760003560e01c8063a9059cbb14610030575b600080fd5b61004a6004803603810190610045919061016a565b610060565b60405161005791906101c5565b60405180910390f35b60008273ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff167fddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef846040516100bf91906101ef565b60405180910390a36001905092915050565b600080fd5b600073ffffffffffffffffffffffffffffffffffffffff82169050919050565b6000610101826100d6565b9050919050565b610111816100f6565b811461011c57600080fd5b50565b60008135905061012e81610108565b92915050565b6000819050919050565b61014781610134565b811461015257600080fd5b50565b6000813590506101648161013e565b92915050565b60008060408385031215610181576101806100d1565b5b600061018f8582860161011f565b92505060206101a085828601610155565b9150509250929050565b60008115159050919050565b6101bf816101aa565b82525050565b60006020820190506101da60008301846101b6565b92915050565b6101e981610134565b82525050565b600060208201905061020460008301846101e0565b9291505056fea2646970667358221220b469033f4b77b9565ee84e0a2f04d496b18160d26034d54f9487e57788fd36d564736f6c63430008120033")},
addr: {Balance: big.NewInt(params.Ether)},
}
)
func makeTx(to int, value int, b *core.BlockGen) *types.Transaction {
@ -912,6 +926,13 @@ func makeTx(to int, value int, b *core.BlockGen) *types.Transaction {
func i2h(i int) common.Hash { return common.BigToHash(big.NewInt(int64(i))) }
func a2h(a common.Address) common.Hash { return common.HexToHash(a.Hex()) }
func parseTransferLog(log *types.Log) (from, to common.Address, amount uint64) {
from = common.BytesToAddress(log.Topics[1].Bytes())
to = common.BytesToAddress(log.Topics[2].Bytes())
amount = common.BytesToHash(log.Data).Big().Uint64()
return
}
// calculateBalance calculates the address balances of all Transfer events
// We check with the balance instead of logs because there is a gap between the ChainReorg occurred and detected,
// so we can't fully control how many logs we'll receive.
@ -920,9 +941,7 @@ func calculateBalance(logs []*types.Log) map[common.Address]uint64 {
balances := make(map[common.Address]uint64)
for _, log := range logs {
log := log
from := common.BytesToAddress(log.Topics[1].Bytes())
to := common.BytesToAddress(log.Topics[2].Bytes())
amount := common.BytesToHash(log.Data).Big().Uint64()
from, to, amount := parseTransferLog(log)
if _, ok := balances[from]; !ok {
balances[from] = 0
@ -954,24 +973,7 @@ func testLogsSubscriptionReorg(t *testing.T, oldChainMaker, newChainMaker, pendi
db = rawdb.NewMemoryDatabase()
backend, sys = newTestFilterSystem(t, db, Config{})
api = NewFilterAPI(sys, false)
genesis = &core.Genesis{
Config: params.TestChainConfig,
GasLimit: 10000000000000,
Alloc: core.GenesisAlloc{
// // SPDX-License-Identifier: GPL-3.0
// pragma solidity >=0.7.0 <0.9.0;
//
// contract Token {
// event Transfer(address indexed from, address indexed to, uint256 value);
// function transfer(address to, uint256 value) public returns (bool) {
// emit Transfer(msg.sender, to, value);
// return true;
// }
// }
contract: {Balance: big.NewInt(params.Ether), Code: common.FromHex("0x608060405234801561001057600080fd5b506004361061002b5760003560e01c8063a9059cbb14610030575b600080fd5b61004a6004803603810190610045919061016a565b610060565b60405161005791906101c5565b60405180910390f35b60008273ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff167fddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef846040516100bf91906101ef565b60405180910390a36001905092915050565b600080fd5b600073ffffffffffffffffffffffffffffffffffffffff82169050919050565b6000610101826100d6565b9050919050565b610111816100f6565b811461011c57600080fd5b50565b60008135905061012e81610108565b92915050565b6000819050919050565b61014781610134565b811461015257600080fd5b50565b6000813590506101648161013e565b92915050565b60008060408385031215610181576101806100d1565b5b600061018f8582860161011f565b92505060206101a085828601610155565b9150509250929050565b60008115159050919050565b6101bf816101aa565b82525050565b60006020820190506101da60008301846101b6565b92915050565b6101e981610134565b82525050565b600060208201905061020460008301846101e0565b9291505056fea2646970667358221220b469033f4b77b9565ee84e0a2f04d496b18160d26034d54f9487e57788fd36d564736f6c63430008120033")},
addr: {Balance: big.NewInt(params.Ether)},
},
}
genesis = &core.Genesis{Config: params.TestChainConfig, Alloc: genesisAlloc, GasLimit: 10000000000000}
)
// Hack: GenerateChainWithGenesis creates a new db.