2015-07-07 03:54:22 +03:00
|
|
|
// Copyright 2015 The go-ethereum Authors
|
2015-07-22 19:48:40 +03:00
|
|
|
// This file is part of the go-ethereum library.
|
2015-07-07 03:54:22 +03:00
|
|
|
//
|
2015-07-23 19:35:11 +03:00
|
|
|
// The go-ethereum library is free software: you can redistribute it and/or modify
|
2015-07-07 03:54:22 +03: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.
|
|
|
|
//
|
2015-07-22 19:48:40 +03:00
|
|
|
// The go-ethereum library is distributed in the hope that it will be useful,
|
2015-07-07 03:54:22 +03:00
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2015-07-22 19:48:40 +03:00
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2015-07-07 03:54:22 +03:00
|
|
|
// GNU Lesser General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Lesser General Public License
|
2015-07-22 19:48:40 +03:00
|
|
|
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
2015-07-07 03:54:22 +03:00
|
|
|
|
2015-04-10 12:55:31 +03:00
|
|
|
package tests
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/common"
|
2017-07-11 14:49:14 +03:00
|
|
|
"github.com/ethereum/go-ethereum/common/hexutil"
|
2019-05-21 14:58:06 +03:00
|
|
|
"github.com/ethereum/go-ethereum/core"
|
2015-04-10 12:55:31 +03:00
|
|
|
"github.com/ethereum/go-ethereum/core/types"
|
2016-01-20 00:48:50 +02:00
|
|
|
"github.com/ethereum/go-ethereum/params"
|
2015-04-10 12:55:31 +03:00
|
|
|
"github.com/ethereum/go-ethereum/rlp"
|
|
|
|
)
|
|
|
|
|
2017-07-11 14:49:14 +03:00
|
|
|
// TransactionTest checks RLP decoding and sender derivation of transactions.
|
2015-04-10 12:55:31 +03:00
|
|
|
type TransactionTest struct {
|
2019-05-21 14:58:06 +03:00
|
|
|
RLP hexutil.Bytes `json:"rlp"`
|
|
|
|
Byzantium ttFork
|
|
|
|
Constantinople ttFork
|
2019-08-14 15:53:21 +03:00
|
|
|
Istanbul ttFork
|
2019-05-21 14:58:06 +03:00
|
|
|
EIP150 ttFork
|
|
|
|
EIP158 ttFork
|
|
|
|
Frontier ttFork
|
|
|
|
Homestead ttFork
|
2015-04-10 12:55:31 +03:00
|
|
|
}
|
|
|
|
|
2019-05-21 14:58:06 +03:00
|
|
|
type ttFork struct {
|
|
|
|
Sender common.UnprefixedAddress `json:"sender"`
|
|
|
|
Hash common.UnprefixedHash `json:"hash"`
|
2015-04-10 12:55:31 +03:00
|
|
|
}
|
|
|
|
|
2017-07-11 14:49:14 +03:00
|
|
|
func (tt *TransactionTest) Run(config *params.ChainConfig) error {
|
2019-08-14 15:53:21 +03:00
|
|
|
validateTx := func(rlpData hexutil.Bytes, signer types.Signer, isHomestead bool, isIstanbul bool) (*common.Address, *common.Hash, error) {
|
2019-05-21 14:58:06 +03:00
|
|
|
tx := new(types.Transaction)
|
|
|
|
if err := rlp.DecodeBytes(rlpData, tx); err != nil {
|
|
|
|
return nil, nil, err
|
2015-04-19 01:35:48 +03:00
|
|
|
}
|
2019-05-21 14:58:06 +03:00
|
|
|
sender, err := types.Sender(signer, tx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
// Intrinsic gas
|
2019-08-14 15:53:21 +03:00
|
|
|
requiredGas, err := core.IntrinsicGas(tx.Data(), tx.To() == nil, isHomestead, isIstanbul)
|
2019-05-21 14:58:06 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
if requiredGas > tx.Gas() {
|
|
|
|
return nil, nil, fmt.Errorf("insufficient gas ( %d < %d )", tx.Gas(), requiredGas)
|
|
|
|
}
|
|
|
|
h := tx.Hash()
|
|
|
|
return &sender, &h, nil
|
2015-04-10 12:55:31 +03:00
|
|
|
}
|
2015-04-19 01:35:48 +03:00
|
|
|
|
2019-05-21 14:58:06 +03:00
|
|
|
for _, testcase := range []struct {
|
|
|
|
name string
|
|
|
|
signer types.Signer
|
|
|
|
fork ttFork
|
|
|
|
isHomestead bool
|
2019-08-14 15:53:21 +03:00
|
|
|
isIstanbul bool
|
2019-05-21 14:58:06 +03:00
|
|
|
}{
|
2019-08-14 15:53:21 +03:00
|
|
|
{"Frontier", types.FrontierSigner{}, tt.Frontier, false, false},
|
|
|
|
{"Homestead", types.HomesteadSigner{}, tt.Homestead, true, false},
|
|
|
|
{"EIP150", types.HomesteadSigner{}, tt.EIP150, true, false},
|
|
|
|
{"EIP158", types.NewEIP155Signer(config.ChainID), tt.EIP158, true, false},
|
|
|
|
{"Byzantium", types.NewEIP155Signer(config.ChainID), tt.Byzantium, true, false},
|
|
|
|
{"Constantinople", types.NewEIP155Signer(config.ChainID), tt.Constantinople, true, false},
|
2019-11-20 17:08:07 +03:00
|
|
|
{"Istanbul", types.NewEIP155Signer(config.ChainID), tt.Istanbul, true, true},
|
2019-05-21 14:58:06 +03:00
|
|
|
} {
|
2019-08-14 15:53:21 +03:00
|
|
|
sender, txhash, err := validateTx(tt.RLP, testcase.signer, testcase.isHomestead, testcase.isIstanbul)
|
2019-05-21 14:58:06 +03:00
|
|
|
|
|
|
|
if testcase.fork.Sender == (common.UnprefixedAddress{}) {
|
|
|
|
if err == nil {
|
2019-11-23 14:51:37 +03:00
|
|
|
return fmt.Errorf("expected error, got none (address %v)[%v]", sender.String(), testcase.name)
|
2019-05-21 14:58:06 +03:00
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
// Should resolve the right address
|
|
|
|
if err != nil {
|
2019-11-23 14:51:37 +03:00
|
|
|
return fmt.Errorf("got error, expected none: %v", err)
|
2019-05-21 14:58:06 +03:00
|
|
|
}
|
|
|
|
if sender == nil {
|
|
|
|
return fmt.Errorf("sender was nil, should be %x", common.Address(testcase.fork.Sender))
|
|
|
|
}
|
|
|
|
if *sender != common.Address(testcase.fork.Sender) {
|
2019-11-23 14:51:37 +03:00
|
|
|
return fmt.Errorf("sender mismatch: got %x, want %x", sender, testcase.fork.Sender)
|
2019-05-21 14:58:06 +03:00
|
|
|
}
|
|
|
|
if txhash == nil {
|
|
|
|
return fmt.Errorf("txhash was nil, should be %x", common.Hash(testcase.fork.Hash))
|
|
|
|
}
|
|
|
|
if *txhash != common.Hash(testcase.fork.Hash) {
|
2019-11-23 14:51:37 +03:00
|
|
|
return fmt.Errorf("hash mismatch: got %x, want %x", *txhash, testcase.fork.Hash)
|
2015-04-15 23:37:16 +03:00
|
|
|
}
|
2015-04-10 12:55:31 +03:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|