2015-07-07 00:54:22 +00:00
|
|
|
// Copyright 2015 The go-ethereum Authors
|
2015-07-22 16:48:40 +00:00
|
|
|
// This file is part of the go-ethereum library.
|
2015-07-07 00:54:22 +00:00
|
|
|
//
|
2015-07-23 16:35:11 +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.
|
|
|
|
//
|
2015-07-22 16:48:40 +00:00
|
|
|
// 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
|
2015-07-22 16:48:40 +00:00
|
|
|
// 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
|
2015-07-22 16:48:40 +00:00
|
|
|
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
2015-07-07 00:54:22 +00:00
|
|
|
|
2015-04-10 09:55:31 +00:00
|
|
|
package tests
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/common"
|
2017-07-11 11:49:14 +00:00
|
|
|
"github.com/ethereum/go-ethereum/common/hexutil"
|
2019-05-21 11:58:06 +00:00
|
|
|
"github.com/ethereum/go-ethereum/core"
|
2015-04-10 09:55:31 +00:00
|
|
|
"github.com/ethereum/go-ethereum/core/types"
|
2016-01-19 22:48:50 +00:00
|
|
|
"github.com/ethereum/go-ethereum/params"
|
2015-04-10 09:55:31 +00:00
|
|
|
"github.com/ethereum/go-ethereum/rlp"
|
|
|
|
)
|
|
|
|
|
2017-07-11 11:49:14 +00:00
|
|
|
// TransactionTest checks RLP decoding and sender derivation of transactions.
|
2015-04-10 09:55:31 +00:00
|
|
|
type TransactionTest struct {
|
2019-05-21 11:58:06 +00:00
|
|
|
RLP hexutil.Bytes `json:"rlp"`
|
|
|
|
Byzantium ttFork
|
|
|
|
Constantinople ttFork
|
2019-08-14 12:53:21 +00:00
|
|
|
Istanbul ttFork
|
2019-05-21 11:58:06 +00:00
|
|
|
EIP150 ttFork
|
|
|
|
EIP158 ttFork
|
|
|
|
Frontier ttFork
|
|
|
|
Homestead ttFork
|
2015-04-10 09:55:31 +00:00
|
|
|
}
|
|
|
|
|
2019-05-21 11:58:06 +00:00
|
|
|
type ttFork struct {
|
|
|
|
Sender common.UnprefixedAddress `json:"sender"`
|
|
|
|
Hash common.UnprefixedHash `json:"hash"`
|
2015-04-10 09:55:31 +00:00
|
|
|
}
|
|
|
|
|
2017-07-11 11:49:14 +00:00
|
|
|
func (tt *TransactionTest) Run(config *params.ChainConfig) error {
|
2019-08-14 12:53:21 +00:00
|
|
|
validateTx := func(rlpData hexutil.Bytes, signer types.Signer, isHomestead bool, isIstanbul bool) (*common.Address, *common.Hash, error) {
|
2019-05-21 11:58:06 +00:00
|
|
|
tx := new(types.Transaction)
|
|
|
|
if err := rlp.DecodeBytes(rlpData, tx); err != nil {
|
|
|
|
return nil, nil, err
|
2015-04-18 22:35:48 +00:00
|
|
|
}
|
2019-05-21 11:58:06 +00:00
|
|
|
sender, err := types.Sender(signer, tx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
// Intrinsic gas
|
2023-01-11 09:05:47 +00:00
|
|
|
requiredGas, err := core.IntrinsicGas(tx.Data(), tx.AccessList(), tx.To() == nil, isHomestead, isIstanbul, false)
|
2019-05-21 11:58:06 +00: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 09:55:31 +00:00
|
|
|
}
|
2015-04-18 22:35:48 +00:00
|
|
|
|
2019-05-21 11:58:06 +00:00
|
|
|
for _, testcase := range []struct {
|
|
|
|
name string
|
|
|
|
signer types.Signer
|
|
|
|
fork ttFork
|
|
|
|
isHomestead bool
|
2019-08-14 12:53:21 +00:00
|
|
|
isIstanbul bool
|
2019-05-21 11:58:06 +00:00
|
|
|
}{
|
2019-08-14 12:53:21 +00: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 14:08:07 +00:00
|
|
|
{"Istanbul", types.NewEIP155Signer(config.ChainID), tt.Istanbul, true, true},
|
2019-05-21 11:58:06 +00:00
|
|
|
} {
|
2019-08-14 12:53:21 +00:00
|
|
|
sender, txhash, err := validateTx(tt.RLP, testcase.signer, testcase.isHomestead, testcase.isIstanbul)
|
2019-05-21 11:58:06 +00:00
|
|
|
|
|
|
|
if testcase.fork.Sender == (common.UnprefixedAddress{}) {
|
|
|
|
if err == nil {
|
2019-11-23 11:51:37 +00:00
|
|
|
return fmt.Errorf("expected error, got none (address %v)[%v]", sender.String(), testcase.name)
|
2019-05-21 11:58:06 +00:00
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
// Should resolve the right address
|
|
|
|
if err != nil {
|
2019-11-23 11:51:37 +00:00
|
|
|
return fmt.Errorf("got error, expected none: %v", err)
|
2019-05-21 11:58:06 +00: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 11:51:37 +00:00
|
|
|
return fmt.Errorf("sender mismatch: got %x, want %x", sender, testcase.fork.Sender)
|
2019-05-21 11:58:06 +00: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 11:51:37 +00:00
|
|
|
return fmt.Errorf("hash mismatch: got %x, want %x", *txhash, testcase.fork.Hash)
|
2015-04-15 20:37:16 +00:00
|
|
|
}
|
2015-04-10 09:55:31 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|