aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTaylor Gerring <taylor.gerring@gmail.com>2015-06-11 03:02:16 +0800
committerTaylor Gerring <taylor.gerring@gmail.com>2015-06-19 04:15:08 +0800
commit24554629b162d20a1f945386a45e3221c58adc2b (patch)
tree6fa08ffc20d2d8f7cec6417e444576c779a73f39
parent7c6ef0ddac91564f31ef852fd4ef1e821db17c2e (diff)
downloadgo-tangerine-24554629b162d20a1f945386a45e3221c58adc2b.tar
go-tangerine-24554629b162d20a1f945386a45e3221c58adc2b.tar.gz
go-tangerine-24554629b162d20a1f945386a45e3221c58adc2b.tar.bz2
go-tangerine-24554629b162d20a1f945386a45e3221c58adc2b.tar.lz
go-tangerine-24554629b162d20a1f945386a45e3221c58adc2b.tar.xz
go-tangerine-24554629b162d20a1f945386a45e3221c58adc2b.tar.zst
go-tangerine-24554629b162d20a1f945386a45e3221c58adc2b.zip
DRY log check
-rw-r--r--tests/state_test_util.go72
-rw-r--r--tests/vm_test_util.go35
2 files changed, 46 insertions, 61 deletions
diff --git a/tests/state_test_util.go b/tests/state_test_util.go
index 1f481147e..91a8367e7 100644
--- a/tests/state_test_util.go
+++ b/tests/state_test_util.go
@@ -75,22 +75,20 @@ func RunStateTest(p string, t *testing.T) {
}
}
+ // check post state
for addr, account := range test.Post {
obj := statedb.GetStateObject(common.HexToAddress(addr))
if obj == nil {
continue
}
- // if len(test.Exec) == 0 {
if obj.Balance().Cmp(common.Big(account.Balance)) != 0 {
t.Errorf("%s's : (%x) balance failed. Expected %v, got %v => %v\n", name, obj.Address().Bytes()[:4], account.Balance, obj.Balance(), new(big.Int).Sub(common.Big(account.Balance), obj.Balance()))
}
- // if obj.Nonce() != common.String2Big(account.Nonce).Uint64() {
- // t.Errorf("%s's : (%x) nonce failed. Expected %v, got %v\n", name, obj.Address().Bytes()[:4], account.Nonce, obj.Nonce())
- // }
-
- // }
+ if obj.Nonce() != common.String2Big(account.Nonce).Uint64() {
+ t.Errorf("%s's : (%x) nonce failed. Expected %v, got %v\n", name, obj.Address().Bytes()[:4], account.Nonce, obj.Nonce())
+ }
for addr, value := range account.Storage {
v := obj.GetState(common.HexToHash(addr)).Bytes()
@@ -108,41 +106,51 @@ func RunStateTest(p string, t *testing.T) {
t.Errorf("%s's : Post state root error. Expected %s, got %x", name, test.PostStateRoot, statedb.Root())
}
+ // check logs
if len(test.Logs) > 0 {
- if len(test.Logs) != len(logs) {
- t.Errorf("log length mismatch. Expected %d, got %d", len(test.Logs), len(logs))
- } else {
- for i, log := range test.Logs {
- if common.HexToAddress(log.AddressF) != logs[i].Address {
- t.Errorf("'%s' log address expected %v got %x", name, log.AddressF, logs[i].Address)
- }
+ lerr := CheckLogs(test.Logs, logs, t)
+ if lerr != nil {
+ t.Errorf("'%s' ", name, lerr.Error())
+ }
+ }
- if !bytes.Equal(logs[i].Data, common.FromHex(log.DataF)) {
- t.Errorf("'%s' log data expected %v got %x", name, log.DataF, logs[i].Data)
- }
+ fmt.Println("State test passed: ", name)
+ //fmt.Println(string(statedb.Dump()))
+ }
+ // logger.Flush()
+}
- if len(log.TopicsF) != len(logs[i].Topics) {
- t.Errorf("'%s' log topics length expected %d got %d", name, len(log.TopicsF), logs[i].Topics)
- } else {
- for j, topic := range log.TopicsF {
- if common.HexToHash(topic) != logs[i].Topics[j] {
- t.Errorf("'%s' log topic[%d] expected %v got %x", name, j, topic, logs[i].Topics[j])
- }
- }
- }
- genBloom := common.LeftPadBytes(types.LogsBloom(state.Logs{logs[i]}).Bytes(), 256)
+func CheckLogs(tlog []Log, logs state.Logs, t *testing.T) error {
+
+ if len(tlog) != len(logs) {
+ return fmt.Errorf("log length mismatch. Expected %d, got %d", len(tlog), len(logs))
+ } else {
+ for i, log := range tlog {
+ if common.HexToAddress(log.AddressF) != logs[i].Address {
+ return fmt.Errorf("log address expected %v got %x", log.AddressF, logs[i].Address)
+ }
- if !bytes.Equal(genBloom, common.Hex2Bytes(log.BloomF)) {
- t.Errorf("'%s' bloom mismatch", name)
+ if !bytes.Equal(logs[i].Data, common.FromHex(log.DataF)) {
+ return fmt.Errorf("log data expected %v got %x", log.DataF, logs[i].Data)
+ }
+
+ if len(log.TopicsF) != len(logs[i].Topics) {
+ return fmt.Errorf("log topics length expected %d got %d", len(log.TopicsF), logs[i].Topics)
+ } else {
+ for j, topic := range log.TopicsF {
+ if common.HexToHash(topic) != logs[i].Topics[j] {
+ return fmt.Errorf("log topic[%d] expected %v got %x", j, topic, logs[i].Topics[j])
}
}
}
- }
+ genBloom := common.LeftPadBytes(types.LogsBloom(state.Logs{logs[i]}).Bytes(), 256)
- fmt.Println("State test passed: ", name)
- //fmt.Println(string(statedb.Dump()))
+ if !bytes.Equal(genBloom, common.Hex2Bytes(log.BloomF)) {
+ return fmt.Errorf("bloom mismatch")
+ }
+ }
}
- // logger.Flush()
+ return nil
}
func RunState(statedb *state.StateDB, env, tx map[string]string) ([]byte, state.Logs, *big.Int, error) {
diff --git a/tests/vm_test_util.go b/tests/vm_test_util.go
index 3852ba4bf..6743a0872 100644
--- a/tests/vm_test_util.go
+++ b/tests/vm_test_util.go
@@ -11,7 +11,7 @@ import (
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core"
"github.com/ethereum/go-ethereum/core/state"
- "github.com/ethereum/go-ethereum/core/types"
+ // "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/ethdb"
@@ -125,11 +125,13 @@ func RunVmTest(p string, t *testing.T) {
ret, logs, gas, err = RunVm(statedb, env, test.Exec)
+ // Compare expectedand actual return
rexp := common.FromHex(test.Out)
if bytes.Compare(rexp, ret) != 0 {
t.Errorf("%s's return failed. Expected %x, got %x\n", name, rexp, ret)
}
+ // Check gas usage
if len(test.Gas) == 0 && err == nil {
t.Errorf("%s's gas unspecified, indicating an error. VM returned (incorrectly) successfull", name)
} else {
@@ -139,6 +141,7 @@ func RunVmTest(p string, t *testing.T) {
}
}
+ // check post state
for addr, account := range test.Post {
obj := statedb.GetStateObject(common.HexToAddress(addr))
if obj == nil {
@@ -155,35 +158,9 @@ func RunVmTest(p string, t *testing.T) {
}
}
+ // check logs
if len(test.Logs) > 0 {
- if len(test.Logs) != len(logs) {
- t.Errorf("log length mismatch. Expected %d, got %d", len(test.Logs), len(logs))
- } else {
- for i, log := range test.Logs {
- if common.HexToAddress(log.AddressF) != logs[i].Address {
- t.Errorf("'%s' log address expected %v got %x", name, log.AddressF, logs[i].Address)
- }
-
- if !bytes.Equal(logs[i].Data, common.FromHex(log.DataF)) {
- t.Errorf("'%s' log data expected %v got %x", name, log.DataF, logs[i].Data)
- }
-
- if len(log.TopicsF) != len(logs[i].Topics) {
- t.Errorf("'%s' log topics length expected %d got %d", name, len(log.TopicsF), logs[i].Topics)
- } else {
- for j, topic := range log.TopicsF {
- if common.HexToHash(topic) != logs[i].Topics[j] {
- t.Errorf("'%s' log topic[%d] expected %v got %x", name, j, topic, logs[i].Topics[j])
- }
- }
- }
- genBloom := common.LeftPadBytes(types.LogsBloom(state.Logs{logs[i]}).Bytes(), 256)
-
- if !bytes.Equal(genBloom, common.Hex2Bytes(log.BloomF)) {
- t.Errorf("'%s' bloom mismatch", name)
- }
- }
- }
+ CheckLogs(test.Logs, logs, t)
}
fmt.Println("VM test passed: ", name)