plugeth/tests/transaction_test_util.go

133 lines
4.4 KiB
Go
Raw Normal View History

2015-07-07 00:54:22 +00:00
// Copyright 2015 The go-ethereum Authors
// This file is part of the go-ethereum library.
2015-07-07 00:54:22 +00:00
//
// The go-ethereum library is free software: you can redistribute it and/or modify
2015-07-07 00:54:22 +00:00
// 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,
2015-07-07 00:54:22 +00:00
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2015-07-07 00:54:22 +00:00
// 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/>.
2015-07-07 00:54:22 +00:00
package tests
import (
"bytes"
"errors"
"fmt"
"math/big"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
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
2017-02-26 21:21:51 +00:00
"github.com/ethereum/go-ethereum/common/math"
"github.com/ethereum/go-ethereum/core/types"
2016-01-19 22:48:50 +00:00
"github.com/ethereum/go-ethereum/params"
"github.com/ethereum/go-ethereum/rlp"
)
// TransactionTest checks RLP decoding and sender derivation of transactions.
type TransactionTest struct {
json ttJSON
}
type ttJSON struct {
BlockNumber math.HexOrDecimal64 `json:"blockNumber"`
RLP hexutil.Bytes `json:"rlp"`
Sender hexutil.Bytes `json:"sender"`
Transaction *ttTransaction `json:"transaction"`
2015-06-14 21:55:03 +00:00
}
//go:generate gencodec -type ttTransaction -field-override ttTransactionMarshaling -out gen_tttransaction.go
type ttTransaction struct {
Data []byte `gencodec:"required"`
GasLimit uint64 `gencodec:"required"`
GasPrice *big.Int `gencodec:"required"`
Nonce uint64 `gencodec:"required"`
Value *big.Int `gencodec:"required"`
R *big.Int `gencodec:"required"`
S *big.Int `gencodec:"required"`
V *big.Int `gencodec:"required"`
To common.Address `gencodec:"required"`
}
type ttTransactionMarshaling struct {
Data hexutil.Bytes
GasLimit math.HexOrDecimal64
GasPrice *math.HexOrDecimal256
Nonce math.HexOrDecimal64
Value *math.HexOrDecimal256
R *math.HexOrDecimal256
S *math.HexOrDecimal256
V *math.HexOrDecimal256
}
func (tt *TransactionTest) Run(config *params.ChainConfig) error {
tx := new(types.Transaction)
if err := rlp.DecodeBytes(tt.json.RLP, tx); err != nil {
if tt.json.Transaction == nil {
return nil
}
return fmt.Errorf("RLP decoding failed: %v", err)
}
// Check sender derivation.
signer := types.MakeSigner(config, new(big.Int).SetUint64(uint64(tt.json.BlockNumber)))
sender, err := types.Sender(signer, tx)
if err != nil {
return err
}
if sender != common.BytesToAddress(tt.json.Sender) {
return fmt.Errorf("Sender mismatch: got %x, want %x", sender, tt.json.Sender)
}
// Check decoded fields.
err = tt.json.Transaction.verify(signer, tx)
if tt.json.Sender == nil && err == nil {
return errors.New("field validations succeeded but should fail")
}
if tt.json.Sender != nil && err != nil {
return fmt.Errorf("field validations failed after RLP decoding: %s", err)
}
return nil
}
func (tt *ttTransaction) verify(signer types.Signer, tx *types.Transaction) error {
if !bytes.Equal(tx.Data(), tt.Data) {
return fmt.Errorf("Tx input data mismatch: got %x want %x", tx.Data(), tt.Data)
}
if tx.Gas() != tt.GasLimit {
return fmt.Errorf("GasLimit mismatch: got %d, want %d", tx.Gas(), tt.GasLimit)
}
if tx.GasPrice().Cmp(tt.GasPrice) != 0 {
return fmt.Errorf("GasPrice mismatch: got %v, want %v", tx.GasPrice(), tt.GasPrice)
}
if tx.Nonce() != tt.Nonce {
return fmt.Errorf("Nonce mismatch: got %v, want %v", tx.Nonce(), tt.Nonce)
}
v, r, s := tx.RawSignatureValues()
if r.Cmp(tt.R) != 0 {
return fmt.Errorf("R mismatch: got %v, want %v", r, tt.R)
}
if s.Cmp(tt.S) != 0 {
return fmt.Errorf("S mismatch: got %v, want %v", s, tt.S)
}
if v.Cmp(tt.V) != 0 {
return fmt.Errorf("V mismatch: got %v, want %v", v, tt.V)
}
if tx.To() == nil {
if tt.To != (common.Address{}) {
return fmt.Errorf("To mismatch when recipient is nil (contract creation): %x", tt.To)
}
} else if *tx.To() != tt.To {
return fmt.Errorf("To mismatch: got %x, want %x", *tx.To(), tt.To)
}
if tx.Value().Cmp(tt.Value) != 0 {
return fmt.Errorf("Value mismatch: got %x, want %x", tx.Value(), tt.Value)
}
return nil
}