aboutsummaryrefslogtreecommitdiffstats
path: root/tests/blocktest.go
diff options
context:
space:
mode:
authorFelix Lange <fjl@twurst.com>2015-04-18 00:20:32 +0800
committerGustav Simonsson <gustav.simonsson@gmail.com>2015-04-19 06:07:09 +0800
commitc453f1f37093445ba1d9eba5a075169ef0566c19 (patch)
tree9bb62306bc9d40434c8ec51e26b9f62114a502b2 /tests/blocktest.go
parent235ed7ecb92527ce0029c2a6f5ace06707db9b28 (diff)
downloadgo-tangerine-c453f1f37093445ba1d9eba5a075169ef0566c19.tar
go-tangerine-c453f1f37093445ba1d9eba5a075169ef0566c19.tar.gz
go-tangerine-c453f1f37093445ba1d9eba5a075169ef0566c19.tar.bz2
go-tangerine-c453f1f37093445ba1d9eba5a075169ef0566c19.tar.lz
go-tangerine-c453f1f37093445ba1d9eba5a075169ef0566c19.tar.xz
go-tangerine-c453f1f37093445ba1d9eba5a075169ef0566c19.tar.zst
go-tangerine-c453f1f37093445ba1d9eba5a075169ef0566c19.zip
tests: hopefully improve test conversion helpers
(cherry picked from commit 035a30acbefb5eeadc1fc8dbd567775d5688f8a9)
Diffstat (limited to 'tests/blocktest.go')
-rw-r--r--tests/blocktest.go55
1 files changed, 30 insertions, 25 deletions
diff --git a/tests/blocktest.go b/tests/blocktest.go
index 37fd9e494..d0a910336 100644
--- a/tests/blocktest.go
+++ b/tests/blocktest.go
@@ -211,13 +211,13 @@ func mustConvertHeader(in btHeader) *types.Header {
UncleHash: mustConvertHash(in.UncleHash),
ParentHash: mustConvertHash(in.ParentHash),
Extra: mustConvertBytes(in.ExtraData),
- GasUsed: mustConvertBigInt(in.GasUsed),
- GasLimit: mustConvertBigInt(in.GasLimit),
- Difficulty: mustConvertBigInt(in.Difficulty),
- Time: mustConvertUint(in.Timestamp),
+ GasUsed: mustConvertBigInt(in.GasUsed, 10),
+ GasLimit: mustConvertBigInt(in.GasLimit, 10),
+ Difficulty: mustConvertBigInt(in.Difficulty, 10),
+ Time: mustConvertUint(in.Timestamp, 10),
}
// XXX cheats? :-)
- header.SetNonce(common.BytesToHash(mustConvertBytes(in.Nonce)).Big().Uint64())
+ header.SetNonce(mustConvertUint(in.Nonce, 16))
return header
}
@@ -238,7 +238,7 @@ func mustConvertBytes(in string) []byte {
if in == "0x" {
return []byte{}
}
- h := strings.TrimPrefix(unfuckCPPHexInts(in), "0x")
+ h := nibbleFix(strings.TrimPrefix(in, "0x"))
out, err := hex.DecodeString(h)
if err != nil {
panic(fmt.Errorf("invalid hex: %q", h))
@@ -255,7 +255,7 @@ func mustConvertHash(in string) common.Hash {
}
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 {
panic(fmt.Errorf("invalid hex: %q", in))
}
@@ -270,16 +270,18 @@ func mustConvertBloom(in string) types.Bloom {
return types.BytesToBloom(out)
}
-func mustConvertBigInt(in string) *big.Int {
- out, ok := new(big.Int).SetString(unfuckCPPHexInts(in), 0)
+func mustConvertBigInt(in string, base int) *big.Int {
+ in = prepInt(base, in)
+ out, ok := new(big.Int).SetString(in, base)
if !ok {
panic(fmt.Errorf("invalid integer: %q", in))
}
return out
}
-func mustConvertUint(in string) uint64 {
- out, err := strconv.ParseUint(unfuckCPPHexInts(in), 0, 64)
+func mustConvertUint(in string, base int) uint64 {
+ in = prepInt(base, in)
+ out, err := strconv.ParseUint(in, base, 64)
if err != nil {
panic(fmt.Errorf("invalid integer: %q", in))
}
@@ -316,19 +318,22 @@ func findLine(data []byte, offset int64) (line int) {
return
}
-func unfuckCPPHexInts(s string) string {
- switch {
- case s == "0x":
- // no respect for the empty value :(
- return "0x00"
- case len(s) == 0:
- return "0x00"
- case len(s) == 1:
- return "0x0" + s[:1]
- case len(s)%2 != 0:
- // motherfucking nibbles
- return "0x0" + s[2:]
- default:
- return s
+func prepInt(base int, s string) string {
+ if base == 16 {
+ if strings.HasPrefix(s, "0x") {
+ s = s[2:]
+ }
+ if len(s) == 0 {
+ s = "00"
+ }
+ s = nibbleFix(s)
+ }
+ return s
+}
+
+func nibbleFix(s string) string {
+ if len(s)%2 != 0 {
+ s = "0" + s
}
+ return s
}