tests: hopefully improve test conversion helpers

(cherry picked from commit 035a30acbefb5eeadc1fc8dbd567775d5688f8a9)
This commit is contained in:
Felix Lange 2015-04-17 18:20:32 +02:00 committed by Gustav Simonsson
parent 235ed7ecb9
commit c453f1f370
2 changed files with 35 additions and 30 deletions

@ -211,13 +211,13 @@ func mustConvertHeader(in btHeader) *types.Header {
UncleHash: mustConvertHash(in.UncleHash), UncleHash: mustConvertHash(in.UncleHash),
ParentHash: mustConvertHash(in.ParentHash), ParentHash: mustConvertHash(in.ParentHash),
Extra: mustConvertBytes(in.ExtraData), Extra: mustConvertBytes(in.ExtraData),
GasUsed: mustConvertBigInt(in.GasUsed), GasUsed: mustConvertBigInt(in.GasUsed, 10),
GasLimit: mustConvertBigInt(in.GasLimit), GasLimit: mustConvertBigInt(in.GasLimit, 10),
Difficulty: mustConvertBigInt(in.Difficulty), Difficulty: mustConvertBigInt(in.Difficulty, 10),
Time: mustConvertUint(in.Timestamp), Time: mustConvertUint(in.Timestamp, 10),
} }
// XXX cheats? :-) // XXX cheats? :-)
header.SetNonce(common.BytesToHash(mustConvertBytes(in.Nonce)).Big().Uint64()) header.SetNonce(mustConvertUint(in.Nonce, 16))
return header return header
} }
@ -238,7 +238,7 @@ func mustConvertBytes(in string) []byte {
if in == "0x" { if in == "0x" {
return []byte{} return []byte{}
} }
h := strings.TrimPrefix(unfuckCPPHexInts(in), "0x") h := nibbleFix(strings.TrimPrefix(in, "0x"))
out, err := hex.DecodeString(h) out, err := hex.DecodeString(h)
if err != nil { if err != nil {
panic(fmt.Errorf("invalid hex: %q", h)) panic(fmt.Errorf("invalid hex: %q", h))
@ -255,7 +255,7 @@ func mustConvertHash(in string) common.Hash {
} }
func mustConvertAddress(in string) common.Address { func mustConvertAddress(in string) common.Address {
out, err := hex.DecodeString(strings.TrimPrefix(in, "0x")) out, err := hex.DecodeString(nibbleFix(strings.TrimPrefix(in, "0x")))
if err != nil { if err != nil {
panic(fmt.Errorf("invalid hex: %q", in)) panic(fmt.Errorf("invalid hex: %q", in))
} }
@ -270,16 +270,18 @@ func mustConvertBloom(in string) types.Bloom {
return types.BytesToBloom(out) return types.BytesToBloom(out)
} }
func mustConvertBigInt(in string) *big.Int { func mustConvertBigInt(in string, base int) *big.Int {
out, ok := new(big.Int).SetString(unfuckCPPHexInts(in), 0) in = prepInt(base, in)
out, ok := new(big.Int).SetString(in, base)
if !ok { if !ok {
panic(fmt.Errorf("invalid integer: %q", in)) panic(fmt.Errorf("invalid integer: %q", in))
} }
return out return out
} }
func mustConvertUint(in string) uint64 { func mustConvertUint(in string, base int) uint64 {
out, err := strconv.ParseUint(unfuckCPPHexInts(in), 0, 64) in = prepInt(base, in)
out, err := strconv.ParseUint(in, base, 64)
if err != nil { if err != nil {
panic(fmt.Errorf("invalid integer: %q", in)) panic(fmt.Errorf("invalid integer: %q", in))
} }
@ -316,19 +318,22 @@ func findLine(data []byte, offset int64) (line int) {
return return
} }
func unfuckCPPHexInts(s string) string { func prepInt(base int, s string) string {
switch { if base == 16 {
case s == "0x": if strings.HasPrefix(s, "0x") {
// no respect for the empty value :( s = s[2:]
return "0x00" }
case len(s) == 0: if len(s) == 0 {
return "0x00" s = "00"
case len(s) == 1: }
return "0x0" + s[:1] s = nibbleFix(s)
case len(s)%2 != 0:
// motherfucking nibbles
return "0x0" + s[2:]
default:
return s
} }
return s
}
func nibbleFix(s string) string {
if len(s)%2 != 0 {
s = "0" + s
}
return s
} }

@ -127,15 +127,15 @@ func convertTestTypes(txTest TransactionTest) (sender, to common.Address,
txInputData = mustConvertBytes(txTest.Transaction.Data) txInputData = mustConvertBytes(txTest.Transaction.Data)
rlpBytes = mustConvertBytes(txTest.Rlp) rlpBytes = mustConvertBytes(txTest.Rlp)
gasLimit = mustConvertBigInt(txTest.Transaction.GasLimit) gasLimit = mustConvertBigInt(txTest.Transaction.GasLimit, 16)
gasPrice = mustConvertBigInt(txTest.Transaction.GasPrice) gasPrice = mustConvertBigInt(txTest.Transaction.GasPrice, 16)
value = mustConvertBigInt(txTest.Transaction.Value) value = mustConvertBigInt(txTest.Transaction.Value, 16)
r = common.Bytes2Big(mustConvertBytes(txTest.Transaction.R)) r = common.Bytes2Big(mustConvertBytes(txTest.Transaction.R))
s = common.Bytes2Big(mustConvertBytes(txTest.Transaction.S)) s = common.Bytes2Big(mustConvertBytes(txTest.Transaction.S))
nonce = mustConvertUint(txTest.Transaction.Nonce) nonce = mustConvertUint(txTest.Transaction.Nonce, 16)
v = mustConvertUint(txTest.Transaction.V) v = mustConvertUint(txTest.Transaction.V, 16)
return sender, to, txInputData, rlpBytes, gasLimit, gasPrice, value, r, s, nonce, v, nil return sender, to, txInputData, rlpBytes, gasLimit, gasPrice, value, r, s, nonce, v, nil
} }