Skip to content

Commit

Permalink
Fix unit tests cont.
Browse files Browse the repository at this point in the history
  • Loading branch information
trinhdn2 committed Jul 7, 2023
1 parent 281251e commit 4e9ac23
Show file tree
Hide file tree
Showing 6 changed files with 101 additions and 47 deletions.
5 changes: 3 additions & 2 deletions accounts/abi/bind/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,12 @@ var waitDeployedTests = map[string]struct {
func TestWaitDeployed(t *testing.T) {
for name, test := range waitDeployedTests {
backend := backends.NewSimulatedBackend(core.GenesisAlloc{
crypto.PubkeyToAddress(testKey.PublicKey): {Balance: big.NewInt(10000000000)},
crypto.PubkeyToAddress(testKey.PublicKey): {Balance: big.NewInt(10000000000000000)},
})

// Create the transaction.
tx := types.NewContractCreation(0, big.NewInt(0), test.gas, big.NewInt(1), common.FromHex(test.code))
head, _ := backend.HeaderByNumber(context.Background(), nil) // Should be child's, good enough
tx := types.NewContractCreation(0, big.NewInt(0), test.gas, new(big.Int).Add(head.BaseFee, big.NewInt(1)), common.FromHex(test.code))
tx, _ = types.SignTx(tx, types.HomesteadSigner{}, testKey)

// Wait for it to get mined in the background.
Expand Down
2 changes: 1 addition & 1 deletion accounts/keystore/keystore.go
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,7 @@ func (ks *KeyStore) SignTxWithPassphrase(a accounts.Account, passphrase string,
if chainID != nil {
return types.SignTx(tx, types.NewEIP155Signer(chainID), key.PrivateKey)
}
return types.SignTx(tx, types.HomesteadSigner{}, key.PrivateKey)
return types.SignTx(tx, types.LatestSignerForChainID(chainID), key.PrivateKey)
}

// Unlock unlocks the given account indefinitely.
Expand Down
22 changes: 13 additions & 9 deletions eth/filters/filter_system_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,15 +145,19 @@ func TestBlockSubscription(t *testing.T) {
t.Parallel()

var (
mux = new(event.TypeMux)
db = rawdb.NewMemoryDatabase()
txFeed = new(event.Feed)
rmLogsFeed = new(event.Feed)
logsFeed = new(event.Feed)
chainFeed = new(event.Feed)
backend = &testBackend{mux, db, 0, txFeed, rmLogsFeed, logsFeed, chainFeed}
api = NewPublicFilterAPI(backend, false)
genesis = new(core.Genesis).MustCommit(db)
mux = new(event.TypeMux)
db = rawdb.NewMemoryDatabase()
txFeed = new(event.Feed)
rmLogsFeed = new(event.Feed)
logsFeed = new(event.Feed)
chainFeed = new(event.Feed)
backend = &testBackend{mux, db, 0, txFeed, rmLogsFeed, logsFeed, chainFeed}
api = NewPublicFilterAPI(backend, false)
g = &core.Genesis{
Config: params.TestChainConfig,
BaseFee: big.NewInt(params.InitialBaseFee),
}
genesis = g.MustCommit(db)
chain, _ = core.GenerateChain(params.TestChainConfig, genesis, ethash.NewFaker(), db, 10, func(i int, gen *core.BlockGen) {})
chainEvents = []core.ChainEvent{}
)
Expand Down
114 changes: 81 additions & 33 deletions eth/filters/filter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,17 @@ package filters

import (
"context"
"github.com/tomochain/tomochain/core/rawdb"
"io/ioutil"
"math/big"
"os"
"strings"
"testing"

"github.com/tomochain/tomochain/accounts/abi"
"github.com/tomochain/tomochain/common"
"github.com/tomochain/tomochain/consensus/ethash"
"github.com/tomochain/tomochain/core"
"github.com/tomochain/tomochain/core/rawdb"
"github.com/tomochain/tomochain/core/types"
"github.com/tomochain/tomochain/crypto"
"github.com/tomochain/tomochain/event"
Expand All @@ -50,7 +52,7 @@ func BenchmarkFilters(b *testing.B) {
defer os.RemoveAll(dir)

var (
db, _ = rawdb.NewLevelDBDatabase(dir, 0, 0,"")
db, _ = rawdb.NewLevelDBDatabase(dir, 0, 0, "")
mux = new(event.TypeMux)
txFeed = new(event.Feed)
rmLogsFeed = new(event.Feed)
Expand Down Expand Up @@ -115,7 +117,7 @@ func TestFilters(t *testing.T) {
defer os.RemoveAll(dir)

var (
db, _ = rawdb.NewLevelDBDatabase(dir, 0, 0,"")
db, _ = rawdb.NewLevelDBDatabase(dir, 0, 0, "")
mux = new(event.TypeMux)
txFeed = new(event.Feed)
rmLogsFeed = new(event.Feed)
Expand All @@ -124,6 +126,12 @@ func TestFilters(t *testing.T) {
backend = &testBackend{mux, db, 0, txFeed, rmLogsFeed, logsFeed, chainFeed}
key1, _ = crypto.HexToECDSA("b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291")
addr = crypto.PubkeyToAddress(key1.PublicKey)
signer = types.NewLondonSigner(big.NewInt(1))

// Logging contract
contract = common.Address{0xfe}
contract2 = common.Address{0xff}
abiStr = `[{"inputs":[],"name":"log0","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint256","name":"t1","type":"uint256"}],"name":"log1","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint256","name":"t1","type":"uint256"},{"internalType":"uint256","name":"t2","type":"uint256"}],"name":"log2","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint256","name":"t1","type":"uint256"},{"internalType":"uint256","name":"t2","type":"uint256"},{"internalType":"uint256","name":"t3","type":"uint256"}],"name":"log3","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint256","name":"t1","type":"uint256"},{"internalType":"uint256","name":"t2","type":"uint256"},{"internalType":"uint256","name":"t3","type":"uint256"},{"internalType":"uint256","name":"t4","type":"uint256"}],"name":"log4","outputs":[],"stateMutability":"nonpayable","type":"function"}]`

hash1 = common.BytesToHash([]byte("topic1"))
hash2 = common.BytesToHash([]byte("topic2"))
Expand All @@ -132,45 +140,85 @@ func TestFilters(t *testing.T) {
)
defer db.Close()

genesis := core.GenesisBlockForTesting(db, addr, big.NewInt(1000000))
chain, receipts := core.GenerateChain(params.TestChainConfig, genesis, ethash.NewFaker(), db, 1000, func(i int, gen *core.BlockGen) {
g := &core.Genesis{
Config: params.TestChainConfig,
BaseFee: big.NewInt(params.InitialBaseFee),
Alloc: core.GenesisAlloc{addr: {Balance: big.NewInt(0).Mul(big.NewInt(100), big.NewInt(params.Ether))}},
}

contractABI, err := abi.JSON(strings.NewReader(abiStr))
if err != nil {
t.Fatal(err)
}

// Hack: GenerateChainWithGenesis creates a new db.
// Commit the genesis manually and use GenerateChain.
_, err = g.Commit(db)
if err != nil {
t.Fatal(err)
}
chain, receipts := core.GenerateChain(g.Config, g.ToBlock(db), ethash.NewFaker(), db, 1000, func(i int, gen *core.BlockGen) {
switch i {
case 1:
receipt := types.NewReceipt(nil, false, 0)
receipt.Logs = []*types.Log{
{
Address: addr,
Topics: []common.Hash{hash1},
},
data, err := contractABI.Pack("log1", hash1.Big())
if err != nil {
t.Fatal(err)
}
gen.AddUncheckedReceipt(receipt)
tx, _ := types.SignTx(types.NewTx(&types.LegacyTx{
Nonce: 0,
GasPrice: gen.BaseFee(),
Gas: 30000,
To: &contract,
Data: data,
}), signer, key1)
gen.AddTx(tx)
tx2, _ := types.SignTx(types.NewTx(&types.LegacyTx{
Nonce: 1,
GasPrice: gen.BaseFee(),
Gas: 30000,
To: &contract2,
Data: data,
}), signer, key1)
gen.AddTx(tx2)
case 2:
receipt := types.NewReceipt(nil, false, 0)
receipt.Logs = []*types.Log{
{
Address: addr,
Topics: []common.Hash{hash2},
},
data, err := contractABI.Pack("log2", hash2.Big(), hash1.Big())
if err != nil {
t.Fatal(err)
}
gen.AddUncheckedReceipt(receipt)
tx, _ := types.SignTx(types.NewTx(&types.LegacyTx{
Nonce: 2,
GasPrice: gen.BaseFee(),
Gas: 30000,
To: &contract,
Data: data,
}), signer, key1)
gen.AddTx(tx)
case 998:
receipt := types.NewReceipt(nil, false, 0)
receipt.Logs = []*types.Log{
{
Address: addr,
Topics: []common.Hash{hash3},
},
data, err := contractABI.Pack("log1", hash3.Big())
if err != nil {
t.Fatal(err)
}
gen.AddUncheckedReceipt(receipt)
tx, _ := types.SignTx(types.NewTx(&types.LegacyTx{
Nonce: 3,
GasPrice: gen.BaseFee(),
Gas: 30000,
To: &contract2,
Data: data,
}), signer, key1)
gen.AddTx(tx)
case 999:
receipt := types.NewReceipt(nil, false, 0)
receipt.Logs = []*types.Log{
{
Address: addr,
Topics: []common.Hash{hash4},
},
data, err := contractABI.Pack("log1", hash4.Big())
if err != nil {
t.Fatal(err)
}
gen.AddUncheckedReceipt(receipt)
tx, _ := types.SignTx(types.NewTx(&types.LegacyTx{
Nonce: 4,
GasPrice: gen.BaseFee(),
Gas: 30000,
To: &contract,
Data: data,
}), signer, key1)
gen.AddTx(tx)
}
})
for i, block := range chain {
Expand Down
3 changes: 2 additions & 1 deletion internal/guide/guide_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import (
"time"

"github.com/tomochain/tomochain/accounts/keystore"
"github.com/tomochain/tomochain/common"
"github.com/tomochain/tomochain/core/types"
)

Expand Down Expand Up @@ -75,7 +76,7 @@ func TestAccountManagement(t *testing.T) {
if err != nil {
t.Fatalf("Failed to create signer account: %v", err)
}
tx, chain := new(types.Transaction), big.NewInt(1)
tx, chain := types.NewTransaction(0, common.Address{}, big.NewInt(0), 0, big.NewInt(0), nil), big.NewInt(1)

// Sign a transaction with a single authorization
if _, err := ks.SignTxWithPassphrase(signer, "Signer password", tx, chain); err != nil {
Expand Down
2 changes: 1 addition & 1 deletion p2p/discv5/node_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ var parseNodeTests = []struct {
{
// This test checks that errors from url.Parse are handled.
rawurl: "://foo",
wantError: `parse ://foo: missing protocol scheme`,
wantError: `parse "://foo": missing protocol scheme`,
},
}

Expand Down

0 comments on commit 4e9ac23

Please sign in to comment.