Skip to content

Commit c453f1f

Browse files
fjlGustav Simonsson
authored andcommitted
tests: hopefully improve test conversion helpers
(cherry picked from commit 035a30acbefb5eeadc1fc8dbd567775d5688f8a9)
1 parent 235ed7e commit c453f1f

2 files changed

Lines changed: 35 additions & 30 deletions

File tree

‎tests/blocktest.go‎

Lines changed: 30 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -211,13 +211,13 @@ func mustConvertHeader(in btHeader) *types.Header {
211211
UncleHash: mustConvertHash(in.UncleHash),
212212
ParentHash: mustConvertHash(in.ParentHash),
213213
Extra: mustConvertBytes(in.ExtraData),
214-
GasUsed: mustConvertBigInt(in.GasUsed),
215-
GasLimit: mustConvertBigInt(in.GasLimit),
216-
Difficulty: mustConvertBigInt(in.Difficulty),
217-
Time: mustConvertUint(in.Timestamp),
214+
GasUsed: mustConvertBigInt(in.GasUsed, 10),
215+
GasLimit: mustConvertBigInt(in.GasLimit, 10),
216+
Difficulty: mustConvertBigInt(in.Difficulty, 10),
217+
Time: mustConvertUint(in.Timestamp, 10),
218218
}
219219
// XXX cheats? :-)
220-
header.SetNonce(common.BytesToHash(mustConvertBytes(in.Nonce)).Big().Uint64())
220+
header.SetNonce(mustConvertUint(in.Nonce, 16))
221221
return header
222222
}
223223

@@ -238,7 +238,7 @@ func mustConvertBytes(in string) []byte {
238238
if in == "0x" {
239239
return []byte{}
240240
}
241-
h := strings.TrimPrefix(unfuckCPPHexInts(in), "0x")
241+
h := nibbleFix(strings.TrimPrefix(in, "0x"))
242242
out, err := hex.DecodeString(h)
243243
if err != nil {
244244
panic(fmt.Errorf("invalid hex: %q", h))
@@ -255,7 +255,7 @@ func mustConvertHash(in string) common.Hash {
255255
}
256256

257257
func mustConvertAddress(in string) common.Address {
258-
out, err := hex.DecodeString(strings.TrimPrefix(in, "0x"))
258+
out, err := hex.DecodeString(nibbleFix(strings.TrimPrefix(in, "0x")))
259259
if err != nil {
260260
panic(fmt.Errorf("invalid hex: %q", in))
261261
}
@@ -270,16 +270,18 @@ func mustConvertBloom(in string) types.Bloom {
270270
return types.BytesToBloom(out)
271271
}
272272

273-
func mustConvertBigInt(in string) *big.Int {
274-
out, ok := new(big.Int).SetString(unfuckCPPHexInts(in), 0)
273+
func mustConvertBigInt(in string, base int) *big.Int {
274+
in = prepInt(base, in)
275+
out, ok := new(big.Int).SetString(in, base)
275276
if !ok {
276277
panic(fmt.Errorf("invalid integer: %q", in))
277278
}
278279
return out
279280
}
280281

281-
func mustConvertUint(in string) uint64 {
282-
out, err := strconv.ParseUint(unfuckCPPHexInts(in), 0, 64)
282+
func mustConvertUint(in string, base int) uint64 {
283+
in = prepInt(base, in)
284+
out, err := strconv.ParseUint(in, base, 64)
283285
if err != nil {
284286
panic(fmt.Errorf("invalid integer: %q", in))
285287
}
@@ -316,19 +318,22 @@ func findLine(data []byte, offset int64) (line int) {
316318
return
317319
}
318320

319-
func unfuckCPPHexInts(s string) string {
320-
switch {
321-
case s == "0x":
322-
// no respect for the empty value :(
323-
return "0x00"
324-
case len(s) == 0:
325-
return "0x00"
326-
case len(s) == 1:
327-
return "0x0" + s[:1]
328-
case len(s)%2 != 0:
329-
// motherfucking nibbles
330-
return "0x0" + s[2:]
331-
default:
332-
return s
321+
func prepInt(base int, s string) string {
322+
if base == 16 {
323+
if strings.HasPrefix(s, "0x") {
324+
s = s[2:]
325+
}
326+
if len(s) == 0 {
327+
s = "00"
328+
}
329+
s = nibbleFix(s)
330+
}
331+
return s
332+
}
333+
334+
func nibbleFix(s string) string {
335+
if len(s)%2 != 0 {
336+
s = "0" + s
333337
}
338+
return s
334339
}

‎tests/transaction_test_util.go‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -127,15 +127,15 @@ func convertTestTypes(txTest TransactionTest) (sender, to common.Address,
127127
txInputData = mustConvertBytes(txTest.Transaction.Data)
128128
rlpBytes = mustConvertBytes(txTest.Rlp)
129129

130-
gasLimit = mustConvertBigInt(txTest.Transaction.GasLimit)
131-
gasPrice = mustConvertBigInt(txTest.Transaction.GasPrice)
132-
value = mustConvertBigInt(txTest.Transaction.Value)
130+
gasLimit = mustConvertBigInt(txTest.Transaction.GasLimit, 16)
131+
gasPrice = mustConvertBigInt(txTest.Transaction.GasPrice, 16)
132+
value = mustConvertBigInt(txTest.Transaction.Value, 16)
133133

134134
r = common.Bytes2Big(mustConvertBytes(txTest.Transaction.R))
135135
s = common.Bytes2Big(mustConvertBytes(txTest.Transaction.S))
136136

137-
nonce = mustConvertUint(txTest.Transaction.Nonce)
138-
v = mustConvertUint(txTest.Transaction.V)
137+
nonce = mustConvertUint(txTest.Transaction.Nonce, 16)
138+
v = mustConvertUint(txTest.Transaction.V, 16)
139139

140140
return sender, to, txInputData, rlpBytes, gasLimit, gasPrice, value, r, s, nonce, v, nil
141141
}

0 commit comments

Comments
 (0)