Official Go implementation of the Ethereum protocol
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
go-ethereum/tests/transaction_test_util.go

228 lines
6.7 KiB

// Copyright 2015 The go-ethereum Authors
// This file is part of the go-ethereum library.
//
// The go-ethereum library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The go-ethereum library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
package tests
import (
"bytes"
"errors"
"fmt"
10 years ago
"io"
"runtime"
"github.com/ethereum/go-ethereum/common"
common: move big integer math to common/math (#3699) * common: remove CurrencyToString Move denomination values to params instead. * common: delete dead code * common: move big integer operations to common/math This commit consolidates all big integer operations into common/math and adds tests and documentation. There should be no change in semantics for BigPow, BigMin, BigMax, S256, U256, Exp and their behaviour is now locked in by tests. The BigD, BytesToBig and Bytes2Big functions don't provide additional value, all uses are replaced by new(big.Int).SetBytes(). BigToBytes is now called PaddedBigBytes, its minimum output size parameter is now specified as the number of bytes instead of bits. The single use of this function is in the EVM's MSTORE instruction. Big and String2Big are replaced by ParseBig, which is slightly stricter. It previously accepted leading zeros for hexadecimal inputs but treated decimal inputs as octal if a leading zero digit was present. ParseUint64 is used in places where String2Big was used to decode a uint64. The new functions MustParseBig and MustParseUint64 are now used in many places where parsing errors were previously ignored. * common: delete unused big integer variables * accounts/abi: replace uses of BytesToBig with use of encoding/binary * common: remove BytesToBig * common: remove Bytes2Big * common: remove BigTrue * cmd/utils: add BigFlag and use it for error-checked integer flags While here, remove environment variable processing for DirectoryFlag because we don't use it. * core: add missing error checks in genesis block parser * common: remove String2Big * cmd/evm: use utils.BigFlag * common/math: check for 256 bit overflow in ParseBig This is supposed to prevent silent overflow/truncation of values in the genesis block JSON. Without this check, a genesis block that set a balance larger than 256 bits would lead to weird behaviour in the VM. * cmd/utils: fixup import
8 years ago
"github.com/ethereum/go-ethereum/common/math"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/log"
"github.com/ethereum/go-ethereum/params"
"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 {
Blocknumber string
Rlp string
Sender string
Transaction TtTransaction
}
8 years ago
func RunTransactionTestsWithReader(config *params.ChainConfig, r io.Reader, skipTests []string) error {
skipTest := make(map[string]bool, len(skipTests))
for _, name := range skipTests {
10 years ago
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] {
log.Info(fmt.Sprint("Skipping transaction test", name))
10 years ago
return nil
}
// test the block
8 years ago
if err := runTransactionTest(config, test); err != nil {
10 years ago
return err
}
log.Info(fmt.Sprint("Transaction test passed: ", name))
10 years ago
}
return nil
}
8 years ago
func RunTransactionTests(config *params.ChainConfig, file string, skipTests []string) error {
tests := make(map[string]TransactionTest)
if err := readJsonFile(file, &tests); err != nil {
return err
}
8 years ago
if err := runTransactionTests(config, tests, skipTests); err != nil {
return err
}
return nil
}
8 years ago
func runTransactionTests(config *params.ChainConfig, tests map[string]TransactionTest, skipTests []string) error {
skipTest := make(map[string]bool, len(skipTests))
for _, name := range skipTests {
skipTest[name] = true
}
for name, test := range tests {
// if the test should be skipped, return
if skipTest[name] {
log.Info(fmt.Sprint("Skipping transaction test", name))
return nil
}
10 years ago
// test the block
8 years ago
if err := runTransactionTest(config, test); err != nil {
return fmt.Errorf("%s: %v", name, err)
}
log.Info(fmt.Sprint("Transaction test passed: ", name))
}
return nil
}
8 years ago
func runTransactionTest(config *params.ChainConfig, txTest TransactionTest) (err error) {
tx := new(types.Transaction)
err = rlp.DecodeBytes(mustConvertBytes(txTest.Rlp), tx)
if err != nil {
if txTest.Sender == "" {
// RLP decoding failed and this is expected (test OK)
return nil
} else {
// RLP decoding failed but is expected to succeed (test FAIL)
return fmt.Errorf("RLP decoding failed when expected to succeed: %s", err)
}
}
8 years ago
validationError := verifyTxFields(config, 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)
return fmt.Errorf("Field validations failed after RLP decoding: %s", validationError)
}
}
return errors.New("Should not happen: verify RLP decoding and field validation")
}
8 years ago
func verifyTxFields(chainConfig *params.ChainConfig, 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)
}
}()
var decodedSender common.Address
common: move big integer math to common/math (#3699) * common: remove CurrencyToString Move denomination values to params instead. * common: delete dead code * common: move big integer operations to common/math This commit consolidates all big integer operations into common/math and adds tests and documentation. There should be no change in semantics for BigPow, BigMin, BigMax, S256, U256, Exp and their behaviour is now locked in by tests. The BigD, BytesToBig and Bytes2Big functions don't provide additional value, all uses are replaced by new(big.Int).SetBytes(). BigToBytes is now called PaddedBigBytes, its minimum output size parameter is now specified as the number of bytes instead of bits. The single use of this function is in the EVM's MSTORE instruction. Big and String2Big are replaced by ParseBig, which is slightly stricter. It previously accepted leading zeros for hexadecimal inputs but treated decimal inputs as octal if a leading zero digit was present. ParseUint64 is used in places where String2Big was used to decode a uint64. The new functions MustParseBig and MustParseUint64 are now used in many places where parsing errors were previously ignored. * common: delete unused big integer variables * accounts/abi: replace uses of BytesToBig with use of encoding/binary * common: remove BytesToBig * common: remove Bytes2Big * common: remove BigTrue * cmd/utils: add BigFlag and use it for error-checked integer flags While here, remove environment variable processing for DirectoryFlag because we don't use it. * core: add missing error checks in genesis block parser * common: remove String2Big * cmd/evm: use utils.BigFlag * common/math: check for 256 bit overflow in ParseBig This is supposed to prevent silent overflow/truncation of values in the genesis block JSON. Without this check, a genesis block that set a balance larger than 256 bits would lead to weird behaviour in the VM. * cmd/utils: fixup import
8 years ago
signer := types.MakeSigner(chainConfig, math.MustParseBig256(txTest.Blocknumber))
decodedSender, err = types.Sender(signer, decodedTx)
if err != nil {
return err
}
expectedSender := mustConvertAddress(txTest.Sender)
if expectedSender != decodedSender {
8 years ago
return fmt.Errorf("Sender mismatch: %x %x", expectedSender, decodedSender)
}
expectedData := mustConvertBytes(txTest.Transaction.Data)
if !bytes.Equal(expectedData, decodedTx.Data()) {
return fmt.Errorf("Tx input data mismatch: %#v %#v", expectedData, decodedTx.Data())
}
expectedGasLimit := mustConvertBigInt(txTest.Transaction.GasLimit, 16)
if expectedGasLimit.Cmp(decodedTx.Gas()) != 0 {
return fmt.Errorf("GasLimit mismatch: %v %v", expectedGasLimit, decodedTx.Gas())
}
expectedGasPrice := mustConvertBigInt(txTest.Transaction.GasPrice, 16)
if expectedGasPrice.Cmp(decodedTx.GasPrice()) != 0 {
return fmt.Errorf("GasPrice mismatch: %v %v", expectedGasPrice, decodedTx.GasPrice())
}
expectedNonce := mustConvertUint(txTest.Transaction.Nonce, 16)
if expectedNonce != decodedTx.Nonce() {
return fmt.Errorf("Nonce mismatch: %v %v", expectedNonce, decodedTx.Nonce())
}
8 years ago
v, r, s := decodedTx.RawSignatureValues()
expectedR := mustConvertBigInt(txTest.Transaction.R, 16)
if r.Cmp(expectedR) != 0 {
return fmt.Errorf("R mismatch: %v %v", expectedR, r)
}
expectedS := mustConvertBigInt(txTest.Transaction.S, 16)
if s.Cmp(expectedS) != 0 {
return fmt.Errorf("S mismatch: %v %v", expectedS, s)
}
8 years ago
expectedV := mustConvertBigInt(txTest.Transaction.V, 16)
if v.Cmp(expectedV) != 0 {
return fmt.Errorf("V mismatch: %v %v", expectedV, v)
}
expectedTo := mustConvertAddress(txTest.Transaction.To)
if decodedTx.To() == 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 != *decodedTx.To() {
return fmt.Errorf("To mismatch: %v %v", expectedTo, *decodedTx.To())
}
}
expectedValue := mustConvertBigInt(txTest.Transaction.Value, 16)
if expectedValue.Cmp(decodedTx.Value()) != 0 {
return fmt.Errorf("Value mismatch: %v %v", expectedValue, decodedTx.Value())
}
return nil
}