2015-04-10 09:55:31 +00:00
|
|
|
package tests
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2015-04-18 22:35:48 +00:00
|
|
|
"errors"
|
2015-04-10 09:55:31 +00:00
|
|
|
"fmt"
|
2015-06-14 21:55:03 +00:00
|
|
|
"io"
|
2015-04-10 09:55:31 +00:00
|
|
|
"runtime"
|
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/common"
|
|
|
|
"github.com/ethereum/go-ethereum/core/types"
|
2015-06-11 17:06:56 +00:00
|
|
|
"github.com/ethereum/go-ethereum/logger/glog"
|
2015-04-10 09:55:31 +00:00
|
|
|
"github.com/ethereum/go-ethereum/rlp"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Transaction Test JSON Format
|
|
|
|
type TtTransaction struct {
|
|
|
|
Data string
|
|
|
|
GasLimit string
|
|
|
|
GasPrice string
|
|
|
|
Nonce string
|
|
|
|
R string
|
|
|
|
S string
|
|
|
|
To string
|
|
|
|
V string
|
|
|
|
Value string
|
|
|
|
}
|
|
|
|
|
|
|
|
type TransactionTest struct {
|
|
|
|
Rlp string
|
|
|
|
Sender string
|
|
|
|
Transaction TtTransaction
|
|
|
|
}
|
|
|
|
|
2015-06-14 21:55:03 +00:00
|
|
|
func RunTransactionTestsWithReader(r io.Reader) error {
|
|
|
|
skipTest := make(map[string]bool, len(TransSkipTests))
|
|
|
|
for _, name := range TransSkipTests {
|
|
|
|
skipTest[name] = true
|
|
|
|
}
|
|
|
|
|
|
|
|
bt := make(map[string]TransactionTest)
|
|
|
|
if err := readJson(r, &bt); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for name, test := range bt {
|
|
|
|
// if the test should be skipped, return
|
|
|
|
if skipTest[name] {
|
|
|
|
glog.Infoln("Skipping transaction test", name)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
// test the block
|
|
|
|
if err := runTransactionTest(test); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
glog.Infoln("Transaction test passed: ", name)
|
|
|
|
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-06-10 21:04:06 +00:00
|
|
|
func RunTransactionTests(file string) error {
|
2015-06-14 21:55:03 +00:00
|
|
|
skipTest := make(map[string]bool, len(TransSkipTests))
|
|
|
|
for _, name := range TransSkipTests {
|
2015-06-10 21:04:06 +00:00
|
|
|
skipTest[name] = true
|
|
|
|
}
|
|
|
|
|
2015-04-10 09:55:31 +00:00
|
|
|
bt := make(map[string]TransactionTest)
|
2015-06-14 21:55:03 +00:00
|
|
|
if err := readJsonFile(file, &bt); err != nil {
|
2015-04-10 09:55:31 +00:00
|
|
|
return err
|
|
|
|
}
|
2015-06-10 21:04:06 +00:00
|
|
|
|
|
|
|
for name, test := range bt {
|
|
|
|
// if the test should be skipped, return
|
|
|
|
if skipTest[name] {
|
2015-06-11 17:06:56 +00:00
|
|
|
glog.Infoln("Skipping transaction test", name)
|
2015-06-10 21:04:06 +00:00
|
|
|
return nil
|
|
|
|
}
|
2015-06-14 21:55:03 +00:00
|
|
|
|
2015-06-10 21:04:06 +00:00
|
|
|
// test the block
|
2015-06-14 21:55:03 +00:00
|
|
|
if err := runTransactionTest(test); err != nil {
|
2015-06-10 21:04:06 +00:00
|
|
|
return err
|
2015-04-10 09:55:31 +00:00
|
|
|
}
|
2015-06-11 17:06:56 +00:00
|
|
|
glog.Infoln("Transaction test passed: ", name)
|
2015-06-10 21:04:06 +00:00
|
|
|
|
2015-04-10 09:55:31 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-06-14 21:55:03 +00:00
|
|
|
func runTransactionTest(txTest TransactionTest) (err error) {
|
2015-04-18 22:35:48 +00:00
|
|
|
tx := new(types.Transaction)
|
|
|
|
err = rlp.DecodeBytes(mustConvertBytes(txTest.Rlp), tx)
|
2015-04-10 09:55:31 +00:00
|
|
|
|
|
|
|
if err != nil {
|
2015-04-18 22:35:48 +00:00
|
|
|
if txTest.Sender == "" {
|
|
|
|
// RLP decoding failed and this is expected (test OK)
|
2015-04-10 09:55:31 +00:00
|
|
|
return nil
|
|
|
|
} else {
|
2015-04-18 22:35:48 +00:00
|
|
|
// RLP decoding failed but is expected to succeed (test FAIL)
|
2015-05-07 11:03:22 +00:00
|
|
|
return fmt.Errorf("RLP decoding failed when expected to succeed: ", err)
|
2015-04-10 09:55:31 +00:00
|
|
|
}
|
|
|
|
}
|
2015-04-18 22:35:48 +00:00
|
|
|
|
|
|
|
validationError := verifyTxFields(txTest, tx)
|
|
|
|
if txTest.Sender == "" {
|
|
|
|
if validationError != nil {
|
|
|
|
// RLP decoding works but validation should fail (test OK)
|
|
|
|
return nil
|
|
|
|
} else {
|
|
|
|
// RLP decoding works but validation should fail (test FAIL)
|
|
|
|
// (this should not be possible but added here for completeness)
|
|
|
|
return errors.New("Field validations succeeded but should fail")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if txTest.Sender != "" {
|
|
|
|
if validationError == nil {
|
|
|
|
// RLP decoding works and validations pass (test OK)
|
|
|
|
return nil
|
|
|
|
} else {
|
|
|
|
// RLP decoding works and validations pass (test FAIL)
|
2015-04-22 10:09:44 +00:00
|
|
|
return fmt.Errorf("Field validations failed after RLP decoding: ", validationError)
|
2015-04-18 22:35:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return errors.New("Should not happen: verify RLP decoding and field validation")
|
|
|
|
}
|
|
|
|
|
|
|
|
func verifyTxFields(txTest TransactionTest, decodedTx *types.Transaction) (err error) {
|
|
|
|
defer func() {
|
|
|
|
if recovered := recover(); recovered != nil {
|
|
|
|
buf := make([]byte, 64<<10)
|
|
|
|
buf = buf[:runtime.Stack(buf, false)]
|
|
|
|
err = fmt.Errorf("%v\n%s", recovered, buf)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
decodedSender, err := decodedTx.From()
|
2015-04-10 09:55:31 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-04-18 22:35:48 +00:00
|
|
|
expectedSender := mustConvertAddress(txTest.Sender)
|
|
|
|
if expectedSender != decodedSender {
|
|
|
|
return fmt.Errorf("Sender mismatch: %v %v", expectedSender, decodedSender)
|
2015-04-10 09:55:31 +00:00
|
|
|
}
|
2015-04-18 22:35:48 +00:00
|
|
|
|
|
|
|
expectedData := mustConvertBytes(txTest.Transaction.Data)
|
|
|
|
if !bytes.Equal(expectedData, decodedTx.Payload) {
|
|
|
|
return fmt.Errorf("Tx input data mismatch: %#v %#v", expectedData, decodedTx.Payload)
|
2015-04-10 09:55:31 +00:00
|
|
|
}
|
2015-04-18 22:35:48 +00:00
|
|
|
|
|
|
|
expectedGasLimit := mustConvertBigInt(txTest.Transaction.GasLimit, 16)
|
|
|
|
if expectedGasLimit.Cmp(decodedTx.GasLimit) != 0 {
|
|
|
|
return fmt.Errorf("GasLimit mismatch: %v %v", expectedGasLimit, decodedTx.GasLimit)
|
2015-04-10 09:55:31 +00:00
|
|
|
}
|
2015-04-18 22:35:48 +00:00
|
|
|
|
|
|
|
expectedGasPrice := mustConvertBigInt(txTest.Transaction.GasPrice, 16)
|
|
|
|
if expectedGasPrice.Cmp(decodedTx.Price) != 0 {
|
|
|
|
return fmt.Errorf("GasPrice mismatch: %v %v", expectedGasPrice, decodedTx.Price)
|
2015-04-10 09:55:31 +00:00
|
|
|
}
|
2015-04-18 22:35:48 +00:00
|
|
|
|
|
|
|
expectedNonce := mustConvertUint(txTest.Transaction.Nonce, 16)
|
|
|
|
if expectedNonce != decodedTx.AccountNonce {
|
|
|
|
return fmt.Errorf("Nonce mismatch: %v %v", expectedNonce, decodedTx.AccountNonce)
|
2015-04-10 09:55:31 +00:00
|
|
|
}
|
2015-04-18 22:35:48 +00:00
|
|
|
|
|
|
|
expectedR := common.Bytes2Big(mustConvertBytes(txTest.Transaction.R))
|
|
|
|
if expectedR.Cmp(decodedTx.R) != 0 {
|
|
|
|
return fmt.Errorf("R mismatch: %v %v", expectedR, decodedTx.R)
|
2015-04-10 09:55:31 +00:00
|
|
|
}
|
2015-04-18 22:35:48 +00:00
|
|
|
|
|
|
|
expectedS := common.Bytes2Big(mustConvertBytes(txTest.Transaction.S))
|
|
|
|
if expectedS.Cmp(decodedTx.S) != 0 {
|
|
|
|
return fmt.Errorf("S mismatch: %v %v", expectedS, decodedTx.S)
|
2015-04-10 09:55:31 +00:00
|
|
|
}
|
2015-04-18 22:35:48 +00:00
|
|
|
|
|
|
|
expectedV := mustConvertUint(txTest.Transaction.V, 16)
|
|
|
|
if expectedV != uint64(decodedTx.V) {
|
|
|
|
return fmt.Errorf("V mismatch: %v %v", expectedV, uint64(decodedTx.V))
|
2015-04-10 09:55:31 +00:00
|
|
|
}
|
2015-04-18 22:35:48 +00:00
|
|
|
|
|
|
|
expectedTo := mustConvertAddress(txTest.Transaction.To)
|
|
|
|
if decodedTx.Recipient == nil {
|
2015-04-15 20:37:16 +00:00
|
|
|
if expectedTo != common.BytesToAddress([]byte{}) { // "empty" or "zero" address
|
|
|
|
return fmt.Errorf("To mismatch when recipient is nil (contract creation): %v", expectedTo)
|
|
|
|
}
|
|
|
|
} else {
|
2015-04-18 22:35:48 +00:00
|
|
|
if expectedTo != *decodedTx.Recipient {
|
|
|
|
return fmt.Errorf("To mismatch: %v %v", expectedTo, *decodedTx.Recipient)
|
2015-04-15 20:37:16 +00:00
|
|
|
}
|
2015-04-10 09:55:31 +00:00
|
|
|
}
|
2015-04-15 20:37:16 +00:00
|
|
|
|
2015-04-18 22:35:48 +00:00
|
|
|
expectedValue := mustConvertBigInt(txTest.Transaction.Value, 16)
|
|
|
|
if expectedValue.Cmp(decodedTx.Amount) != 0 {
|
|
|
|
return fmt.Errorf("Value mismatch: %v %v", expectedValue, decodedTx.Amount)
|
2015-04-10 09:55:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|