DRY log check

This commit is contained in:
Taylor Gerring 2015-06-10 15:02:16 -04:00
parent 7c6ef0ddac
commit 24554629b1
2 changed files with 47 additions and 62 deletions

View File

@ -75,22 +75,20 @@ func RunStateTest(p string, t *testing.T) {
} }
} }
// check post state
for addr, account := range test.Post { for addr, account := range test.Post {
obj := statedb.GetStateObject(common.HexToAddress(addr)) obj := statedb.GetStateObject(common.HexToAddress(addr))
if obj == nil { if obj == nil {
continue continue
} }
// if len(test.Exec) == 0 {
if obj.Balance().Cmp(common.Big(account.Balance)) != 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())) 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() { 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()) 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 { for addr, value := range account.Storage {
v := obj.GetState(common.HexToHash(addr)).Bytes() v := obj.GetState(common.HexToHash(addr)).Bytes()
@ -108,34 +106,11 @@ func RunStateTest(p string, t *testing.T) {
t.Errorf("%s's : Post state root error. Expected %s, got %x", name, test.PostStateRoot, statedb.Root()) 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) > 0 {
if len(test.Logs) != len(logs) { lerr := CheckLogs(test.Logs, logs, t)
t.Errorf("log length mismatch. Expected %d, got %d", len(test.Logs), len(logs)) if lerr != nil {
} else { t.Errorf("'%s' ", name, lerr.Error())
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)
}
}
} }
} }
@ -145,6 +120,39 @@ func RunStateTest(p string, t *testing.T) {
// logger.Flush() // logger.Flush()
} }
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(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)
if !bytes.Equal(genBloom, common.Hex2Bytes(log.BloomF)) {
return fmt.Errorf("bloom mismatch")
}
}
}
return nil
}
func RunState(statedb *state.StateDB, env, tx map[string]string) ([]byte, state.Logs, *big.Int, error) { func RunState(statedb *state.StateDB, env, tx map[string]string) ([]byte, state.Logs, *big.Int, error) {
var ( var (
keyPair, _ = crypto.NewKeyPairFromSec([]byte(common.Hex2Bytes(tx["secretKey"]))) keyPair, _ = crypto.NewKeyPairFromSec([]byte(common.Hex2Bytes(tx["secretKey"])))

View File

@ -11,7 +11,7 @@ import (
"github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core" "github.com/ethereum/go-ethereum/core"
"github.com/ethereum/go-ethereum/core/state" "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/core/vm"
"github.com/ethereum/go-ethereum/crypto" "github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/ethdb" "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) ret, logs, gas, err = RunVm(statedb, env, test.Exec)
// Compare expectedand actual return
rexp := common.FromHex(test.Out) rexp := common.FromHex(test.Out)
if bytes.Compare(rexp, ret) != 0 { if bytes.Compare(rexp, ret) != 0 {
t.Errorf("%s's return failed. Expected %x, got %x\n", name, rexp, ret) t.Errorf("%s's return failed. Expected %x, got %x\n", name, rexp, ret)
} }
// Check gas usage
if len(test.Gas) == 0 && err == nil { if len(test.Gas) == 0 && err == nil {
t.Errorf("%s's gas unspecified, indicating an error. VM returned (incorrectly) successfull", name) t.Errorf("%s's gas unspecified, indicating an error. VM returned (incorrectly) successfull", name)
} else { } else {
@ -139,6 +141,7 @@ func RunVmTest(p string, t *testing.T) {
} }
} }
// check post state
for addr, account := range test.Post { for addr, account := range test.Post {
obj := statedb.GetStateObject(common.HexToAddress(addr)) obj := statedb.GetStateObject(common.HexToAddress(addr))
if obj == nil { 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) > 0 {
if len(test.Logs) != len(logs) { CheckLogs(test.Logs, logs, t)
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)
}
}
}
} }
fmt.Println("VM test passed: ", name) fmt.Println("VM test passed: ", name)