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