-
Notifications
You must be signed in to change notification settings - Fork 237
/
Copy pathblockchain_log_test.go
100 lines (85 loc) · 3.39 KB
/
blockchain_log_test.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
// (c) 2023, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package core
import (
"math/big"
"strings"
"testing"
"github.com/ava-labs/subnet-evm/accounts/abi"
"github.com/ava-labs/subnet-evm/consensus/dummy"
"github.com/ava-labs/subnet-evm/core/rawdb"
"github.com/ava-labs/subnet-evm/core/types"
"github.com/ava-labs/subnet-evm/core/vm"
"github.com/ava-labs/subnet-evm/params"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/crypto"
"github.com/stretchr/testify/require"
)
func TestAcceptedLogsSubscription(t *testing.T) {
/*
Example contract to test event emission:
pragma solidity >=0.7.0 <0.9.0;
contract Callable {
event Called();
function Call() public { emit Called(); }
}
*/
const (
callableABI = "[{\"anonymous\":false,\"inputs\":[],\"name\":\"Called\",\"type\":\"event\"},{\"inputs\":[],\"name\":\"Call\",\"outputs\":[],\"stateMutability\":\"nonpayable\",\"type\":\"function\"}]"
callableBin = "6080604052348015600f57600080fd5b5060998061001e6000396000f3fe6080604052348015600f57600080fd5b506004361060285760003560e01c806334e2292114602d575b600080fd5b60336035565b005b7f81fab7a4a0aa961db47eefc81f143a5220e8c8495260dd65b1356f1d19d3c7b860405160405180910390a156fea2646970667358221220029436d24f3ac598ceca41d4d712e13ced6d70727f4cdc580667de66d2f51d8b64736f6c63430008010033"
)
var (
require = require.New(t)
engine = dummy.NewCoinbaseFaker()
key1, _ = crypto.HexToECDSA("b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291")
addr1 = crypto.PubkeyToAddress(key1.PublicKey)
funds = new(big.Int).Mul(big.NewInt(100), big.NewInt(params.Ether))
gspec = &Genesis{
Config: params.TestChainConfig,
Alloc: GenesisAlloc{addr1: {Balance: funds}},
BaseFee: big.NewInt(params.TestInitialBaseFee),
}
contractAddress = crypto.CreateAddress(addr1, 0)
signer = types.LatestSigner(gspec.Config)
)
parsed, err := abi.JSON(strings.NewReader(callableABI))
require.NoError(err)
packedFunction, err := parsed.Pack("Call")
require.NoError(err)
_, blocks, _, err := GenerateChainWithGenesis(gspec, engine, 2, 10, func(i int, b *BlockGen) {
switch i {
case 0:
// First, we deploy the contract
contractTx := types.NewContractCreation(0, common.Big0, 200000, big.NewInt(params.TestInitialBaseFee), common.FromHex(callableBin))
contractSignedTx, err := types.SignTx(contractTx, signer, key1)
require.NoError(err)
b.AddTx(contractSignedTx)
case 1:
// In the next block, we call the contract function
tx := types.NewTransaction(1, contractAddress, common.Big0, 23000, big.NewInt(params.TestInitialBaseFee), packedFunction)
tx, err := types.SignTx(tx, signer, key1)
require.NoError(err)
b.AddTx(tx)
}
})
require.NoError(err)
chain, err := NewBlockChain(rawdb.NewMemoryDatabase(), DefaultCacheConfig, gspec, engine, vm.Config{}, common.Hash{}, false)
require.NoError(err)
defer chain.Stop()
// Create Log Subscriber
logsCh := make(chan []*types.Log, 10)
defer close(logsCh)
sub := chain.SubscribeAcceptedLogsEvent(logsCh)
defer sub.Unsubscribe()
_, err = chain.InsertChain(blocks)
require.NoError(err)
for _, block := range blocks {
err := chain.Accept(block)
require.NoError(err)
}
chain.DrainAcceptorQueue()
logs := <-logsCh
require.Len(logs, 1)
require.Equal(blocks[1].Hash(), logs[0].BlockHash)
require.Equal(blocks[1].Number().Uint64(), logs[0].BlockNumber)
}