Skip to content

Commit

Permalink
add state test for w3q (ethereum#50)
Browse files Browse the repository at this point in the history
* add state test for w3q

* fix go_import format
  • Loading branch information
cyl19970726 authored Mar 15, 2022
1 parent c621b5c commit 0381b48
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 4 deletions.
2 changes: 1 addition & 1 deletion .gitmodules
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[submodule "tests"]
path = tests/testdata
url = https://github.com/ethereum/tests
url = https://github.com/QuarkChain/tests
shallow = true
[submodule "evm-benchmarks"]
path = tests/evm-benchmarks
Expand Down
54 changes: 54 additions & 0 deletions tests/state_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,13 @@ import (
"strings"
"testing"

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core"
"github.com/ethereum/go-ethereum/core/rawdb"
"github.com/ethereum/go-ethereum/core/state"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/core/vm"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/eth/tracers/logger"
)

Expand Down Expand Up @@ -252,3 +255,54 @@ func runBenchmark(b *testing.B, t *StateTest) {
})
}
}

var web3QStateTestDir = filepath.Join(baseDir, "Web3QTest")

func TestWeb3QState(t *testing.T) {
t.Parallel()
st := new(testMatcher)

//st.fails("TestWeb3QState/Stake/StakeFor25kCode.json/London0/trie", "insufficient staking for code")
for _, dir := range []string{
web3QStateTestDir,
} {
st.walk(t, dir, func(t *testing.T, name string, test *StateTest) {
for _, subtest := range test.Subtests() {
subtest := subtest
key := fmt.Sprintf("%s%d", subtest.Fork, subtest.Index)
t.Run(key+"/trie", func(t *testing.T) {
config := vm.Config{}
_, db, err := test.Run(subtest, config, false)
err = st.checkFailure(t, err)
if err != nil {
printStateTrie(db, test, t)
t.Error(err)
}
})
}
})
}
}

func printStateTrie(db *state.StateDB, test *StateTest, t *testing.T) {
noContractCreation := test.json.Tx.To != ""

t.Log("--------------------StateInfo---------------------")

coinbase := test.json.Env.Coinbase
t.Logf("--------------------CoinBase---------------------- \naddress: %s \nbalance: %d \nnonce: %d \n", coinbase.Hex(), db.GetBalance(coinbase).Int64(), db.GetNonce(coinbase))
for addr, acc := range test.json.Pre {
t.Logf("--------------------Account---------------------- \naddress: %s \npre balance: %d \n balance: %d \nnonce: %d \ncode len: %d \n", addr.Hex(), acc.Balance.Int64(), db.GetBalance(addr).Int64(), db.GetNonce(addr), len(db.GetCode(addr)))
}

if !noContractCreation {
caller := common.HexToAddress("0xa94f5374fce5edbc8e2a8697c15331677e6ebf0b")
contract := getCreateContractAddr(caller, test.json.Tx.Nonce)
t.Logf("--------------------Account---------------------- \naddress: %s \nbalance: %d \nnonce: %d \ncode len: %d \n", contract.Hex(), db.GetBalance(contract).Int64(), db.GetNonce(contract), len(db.GetCode(contract)))
}
t.Log("-------------------END-------------------------")
}

func getCreateContractAddr(caller common.Address, nonce uint64) common.Address {
return crypto.CreateAddress(caller, nonce)
}
4 changes: 2 additions & 2 deletions tests/state_test_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -230,10 +230,10 @@ func (t *StateTest) RunNoVerify(subtest StateSubtest, vmconfig vm.Config, snapsh
snapshot := statedb.Snapshot()
gaspool := new(core.GasPool)
gaspool.AddGas(block.GasLimit())
if _, err := core.ApplyMessage(evm, msg, gaspool); err != nil {
if res, err := core.ApplyMessage(evm, msg, gaspool); err != nil {
statedb.RevertToSnapshot(snapshot)
fmt.Println("evm result:", res)
}

// Commit block
statedb.Commit(config.IsEIP158(block.Number()))
// Add 0-value mining reward. This only makes a difference in the cases
Expand Down

0 comments on commit 0381b48

Please sign in to comment.