Skip to content

Commit

Permalink
Fixes for TransactionTests
Browse files Browse the repository at this point in the history
* Include tests which now has consistent HEX encodings
* Comment out two failing tests: "
  "TransactionWithHihghNonce" due to wrong nonce size
  "TransactionWithSvalueHigh" due to wrong ECDSA s range
* Cleanup conversion functions and fix expected encodings for
  tests validation fields
  • Loading branch information
Gustav Simonsson committed Apr 15, 2015
1 parent 2d8a2d0 commit c617a6e
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 41 deletions.
46 changes: 22 additions & 24 deletions tests/blocktest.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,9 +187,9 @@ func mustConvertHeader(in btHeader) *types.Header {
UncleHash: mustConvertHash(in.UncleHash),
ParentHash: mustConvertHash(in.ParentHash),
Extra: mustConvertBytes(in.ExtraData),
GasUsed: mustConvertBigInt10(in.GasUsed),
GasLimit: mustConvertBigInt10(in.GasLimit),
Difficulty: mustConvertBigInt10(in.Difficulty),
GasUsed: mustConvertBigInt(in.GasUsed),
GasLimit: mustConvertBigInt(in.GasLimit),
Difficulty: mustConvertBigInt(in.Difficulty),
Time: mustConvertUint(in.Timestamp),
}
// XXX cheats? :-)
Expand All @@ -211,9 +211,13 @@ func mustConvertBlocks(testBlocks []btBlock) []*types.Block {
}

func mustConvertBytes(in string) []byte {
out, err := hex.DecodeString(strings.TrimPrefix(in, "0x"))
if in == "0x" {
return []byte{}
}
h := strings.TrimPrefix(unfuckCPPHexInts(in), "0x")
out, err := hex.DecodeString(h)
if err != nil {
panic(fmt.Errorf("invalid hex: %q", in))
panic(fmt.Errorf("invalid hex: %q", h))
}
return out
}
Expand Down Expand Up @@ -242,32 +246,16 @@ func mustConvertBloom(in string) types.Bloom {
return types.BytesToBloom(out)
}

func mustConvertBigInt10(in string) *big.Int {
out, ok := new(big.Int).SetString(in, 10)
if !ok {
panic(fmt.Errorf("invalid integer: %q", in))
}
return out
}

func mustConvertBigIntHex(in string) *big.Int {
out, ok := new(big.Int).SetString(in, 16)
func mustConvertBigInt(in string) *big.Int {
out, ok := new(big.Int).SetString(unfuckCPPHexInts(in), 0)
if !ok {
panic(fmt.Errorf("invalid integer: %q", in))
}
return out
}

func mustConvertUint(in string) uint64 {
out, err := strconv.ParseUint(in, 0, 64)
if err != nil {
panic(fmt.Errorf("invalid integer: %q", in))
}
return out
}

func mustConvertUintHex(in string) uint64 {
out, err := strconv.ParseUint(in, 16, 64)
out, err := strconv.ParseUint(unfuckCPPHexInts(in), 0, 64)
if err != nil {
panic(fmt.Errorf("invalid integer: %q", in))
}
Expand Down Expand Up @@ -303,3 +291,13 @@ func findLine(data []byte, offset int64) (line int) {
}
return
}

func unfuckCPPHexInts(s string) string {
if s == "0x" { // no respect for the empty value :(
return "0x00"
}
if (len(s) % 2) != 0 { // motherfucking nibbles
return "0x0" + s[2:]
}
return s
}
29 changes: 18 additions & 11 deletions tests/transaction_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,11 @@ import (

func TestTransactions(t *testing.T) {
notWorking := make(map[string]bool, 100)
// TODO: all commented out tests should work!

// TODO: all these tests should work! remove them from the array when they work
snafus := []string{
"EmptyTransaction",
"TransactionWithHihghNonce",
"TransactionWithRvalueWrongSize",
"TransactionWithSvalueHigh",
"TransactionWithSvalueTooHigh",
"TransactionWithSvalueWrongSize",
"ValuesAsDec",
"ValuesAsHex",
"libsecp256k1test",
"unpadedRValue",
"TransactionWithHihghNonce", // fails due to testing upper bound of 256 bit nonce
"TransactionWithSvalueHigh", // fails due to C++ wrong ECDSA r,s ranges. see https://github.com/ethereum/yellowpaper/pull/112
}

for _, name := range snafus {
Expand All @@ -42,3 +34,18 @@ func TestWrongRLPTransactions(t *testing.T) {
t.Fatal(err)
}
}

/*
Not working until it's fields are in HEX
func Test10MBtx(t *testing.T) {
notWorking := make(map[string]bool, 100)
var err error
err = RunTransactionTests("./files/TransactionTests/tt10mbDataField.json",
notWorking)
if err != nil {
t.Fatal(err)
}
}
*/
19 changes: 13 additions & 6 deletions tests/transaction_test_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,16 @@ func runTest(txTest TransactionTest) (err error) {
if expectedV != uint64(tx.V) {
return fmt.Errorf("V mismatch: %v %v", expectedV, uint64(tx.V))
}
if expectedTo != *tx.Recipient {
return fmt.Errorf("To mismatch: %v %v", expectedTo, *tx.Recipient)
if tx.Recipient == nil {
if expectedTo != common.BytesToAddress([]byte{}) { // "empty" or "zero" address
return fmt.Errorf("To mismatch when recipient is nil (contract creation): %v", expectedTo)
}
} else {
if expectedTo != *tx.Recipient {
return fmt.Errorf("To mismatch: %v %v", expectedTo, *tx.Recipient)
}
}

if expectedValue.Cmp(tx.Amount) != 0 {
return fmt.Errorf("Value mismatch: %v %v", expectedValue, tx.Amount)
}
Expand Down Expand Up @@ -120,14 +127,14 @@ func convertTestTypes(txTest TransactionTest) (sender, to common.Address,
txInputData = mustConvertBytes(txTest.Transaction.Data)
rlpBytes = mustConvertBytes(txTest.Rlp)

gasLimit = mustConvertBigInt10(txTest.Transaction.GasLimit)
gasPrice = mustConvertBigInt10(txTest.Transaction.GasPrice)
value = mustConvertBigInt10(txTest.Transaction.Value)
gasLimit = mustConvertBigInt(txTest.Transaction.GasLimit)
gasPrice = mustConvertBigInt(txTest.Transaction.GasPrice)
value = mustConvertBigInt(txTest.Transaction.Value)

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

nonce = mustConvertUintHex(txTest.Transaction.Nonce)
nonce = mustConvertUint(txTest.Transaction.Nonce)
v = mustConvertUint(txTest.Transaction.V)

return sender, to, txInputData, rlpBytes, gasLimit, gasPrice, value, r, s, nonce, v, nil
Expand Down

0 comments on commit c617a6e

Please sign in to comment.