| 
									
										
										
										
											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 ( | 
					
						
							|  |  |  | 	"bytes" | 
					
						
							| 
									
										
										
										
											2015-04-19 00:35:48 +02:00
										 |  |  | 	"errors" | 
					
						
							| 
									
										
										
										
											2015-04-10 11:55:31 +02:00
										 |  |  | 	"fmt" | 
					
						
							| 
									
										
										
										
											2015-06-14 17:55:03 -04:00
										 |  |  | 	"io" | 
					
						
							| 
									
										
										
										
											2015-04-10 11:55:31 +02:00
										 |  |  | 	"runtime" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	"github.com/ethereum/go-ethereum/common" | 
					
						
							|  |  |  | 	"github.com/ethereum/go-ethereum/core/types" | 
					
						
							| 
									
										
										
										
											2015-06-11 13:06:56 -04:00
										 |  |  | 	"github.com/ethereum/go-ethereum/logger/glog" | 
					
						
							| 
									
										
										
										
											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" | 
					
						
							|  |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // 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 { | 
					
						
							| 
									
										
										
										
											2016-01-19 23:48:50 +01:00
										 |  |  | 	Blocknumber string | 
					
						
							| 
									
										
										
										
											2015-04-10 11:55:31 +02:00
										 |  |  | 	Rlp         string | 
					
						
							|  |  |  | 	Sender      string | 
					
						
							|  |  |  | 	Transaction TtTransaction | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-08 23:07:44 +01:00
										 |  |  | func RunTransactionTestsWithReader(config *params.ChainConfig, r io.Reader, skipTests []string) error { | 
					
						
							| 
									
										
										
										
											2015-06-19 11:38:23 +02:00
										 |  |  | 	skipTest := make(map[string]bool, len(skipTests)) | 
					
						
							|  |  |  | 	for _, name := range skipTests { | 
					
						
							| 
									
										
										
										
											2015-06-14 17:55:03 -04:00
										 |  |  | 		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] { | 
					
						
							|  |  |  | 			glog.Infoln("Skipping transaction test", name) | 
					
						
							|  |  |  | 			return nil | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		// test the block | 
					
						
							| 
									
										
										
										
											2016-11-08 23:07:44 +01:00
										 |  |  | 		if err := runTransactionTest(config, test); err != nil { | 
					
						
							| 
									
										
										
										
											2015-06-14 17:55:03 -04:00
										 |  |  | 			return err | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		glog.Infoln("Transaction test passed: ", name) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return nil | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-08 23:07:44 +01:00
										 |  |  | func RunTransactionTests(config *params.ChainConfig, file string, skipTests []string) error { | 
					
						
							| 
									
										
										
										
											2015-06-19 11:38:23 +02:00
										 |  |  | 	tests := make(map[string]TransactionTest) | 
					
						
							|  |  |  | 	if err := readJsonFile(file, &tests); err != nil { | 
					
						
							|  |  |  | 		return err | 
					
						
							| 
									
										
										
										
											2015-06-10 17:04:06 -04:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-08 23:07:44 +01:00
										 |  |  | 	if err := runTransactionTests(config, tests, skipTests); err != nil { | 
					
						
							| 
									
										
										
										
											2015-04-10 11:55:31 +02:00
										 |  |  | 		return err | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2015-06-19 11:38:23 +02:00
										 |  |  | 	return nil | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2015-06-10 17:04:06 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-08 23:07:44 +01:00
										 |  |  | func runTransactionTests(config *params.ChainConfig, tests map[string]TransactionTest, skipTests []string) error { | 
					
						
							| 
									
										
										
										
											2015-06-19 11:38:23 +02:00
										 |  |  | 	skipTest := make(map[string]bool, len(skipTests)) | 
					
						
							|  |  |  | 	for _, name := range skipTests { | 
					
						
							|  |  |  | 		skipTest[name] = true | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	for name, test := range tests { | 
					
						
							| 
									
										
										
										
											2015-06-10 17:04:06 -04:00
										 |  |  | 		// if the test should be skipped, return | 
					
						
							|  |  |  | 		if skipTest[name] { | 
					
						
							| 
									
										
										
										
											2015-06-11 13:06:56 -04:00
										 |  |  | 			glog.Infoln("Skipping transaction test", name) | 
					
						
							| 
									
										
										
										
											2015-06-10 17:04:06 -04:00
										 |  |  | 			return nil | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2015-06-14 17:55:03 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-10 17:04:06 -04:00
										 |  |  | 		// test the block | 
					
						
							| 
									
										
										
										
											2016-11-08 23:07:44 +01:00
										 |  |  | 		if err := runTransactionTest(config, test); err != nil { | 
					
						
							| 
									
										
										
										
											2016-01-19 23:48:50 +01:00
										 |  |  | 			return fmt.Errorf("%s: %v", name, err) | 
					
						
							| 
									
										
										
										
											2015-04-10 11:55:31 +02:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2015-06-11 13:06:56 -04:00
										 |  |  | 		glog.Infoln("Transaction test passed: ", name) | 
					
						
							| 
									
										
										
										
											2015-06-10 17:04:06 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-04-10 11:55:31 +02:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	return nil | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-08 23:07:44 +01:00
										 |  |  | func runTransactionTest(config *params.ChainConfig, txTest TransactionTest) (err error) { | 
					
						
							| 
									
										
										
										
											2015-04-19 00:35:48 +02:00
										 |  |  | 	tx := new(types.Transaction) | 
					
						
							|  |  |  | 	err = rlp.DecodeBytes(mustConvertBytes(txTest.Rlp), tx) | 
					
						
							| 
									
										
										
										
											2015-04-10 11:55:31 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							| 
									
										
										
										
											2015-04-19 00:35:48 +02:00
										 |  |  | 		if txTest.Sender == "" { | 
					
						
							|  |  |  | 			// RLP decoding failed and this is expected (test OK) | 
					
						
							| 
									
										
										
										
											2015-04-10 11:55:31 +02:00
										 |  |  | 			return nil | 
					
						
							|  |  |  | 		} else { | 
					
						
							| 
									
										
										
										
											2015-04-19 00:35:48 +02:00
										 |  |  | 			// RLP decoding failed but is expected to succeed (test FAIL) | 
					
						
							| 
									
										
										
										
											2015-11-27 15:40:29 +01:00
										 |  |  | 			return fmt.Errorf("RLP decoding failed when expected to succeed: %s", err) | 
					
						
							| 
									
										
										
										
											2015-04-10 11:55:31 +02:00
										 |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2015-04-19 00:35:48 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-08 23:07:44 +01:00
										 |  |  | 	validationError := verifyTxFields(config, txTest, tx) | 
					
						
							| 
									
										
										
										
											2015-04-19 00:35:48 +02:00
										 |  |  | 	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) | 
					
						
							| 
									
										
										
										
											2015-11-27 15:40:29 +01:00
										 |  |  | 			return fmt.Errorf("Field validations failed after RLP decoding: %s", validationError) | 
					
						
							| 
									
										
										
										
											2015-04-19 00:35:48 +02:00
										 |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return errors.New("Should not happen: verify RLP decoding and field validation") | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-08 23:07:44 +01:00
										 |  |  | func verifyTxFields(chainConfig *params.ChainConfig, txTest TransactionTest, decodedTx *types.Transaction) (err error) { | 
					
						
							| 
									
										
										
										
											2015-04-19 00:35:48 +02:00
										 |  |  | 	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) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	}() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-02 13:44:13 +01:00
										 |  |  | 	var decodedSender common.Address | 
					
						
							| 
									
										
										
										
											2016-01-19 23:48:50 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-02 13:44:13 +01:00
										 |  |  | 	signer := types.MakeSigner(chainConfig, common.String2Big(txTest.Blocknumber)) | 
					
						
							|  |  |  | 	decodedSender, err = types.Sender(signer, decodedTx) | 
					
						
							| 
									
										
										
										
											2015-04-10 11:55:31 +02:00
										 |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		return err | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-04-19 00:35:48 +02:00
										 |  |  | 	expectedSender := mustConvertAddress(txTest.Sender) | 
					
						
							|  |  |  | 	if expectedSender != decodedSender { | 
					
						
							| 
									
										
										
										
											2016-11-08 23:07:44 +01:00
										 |  |  | 		return fmt.Errorf("Sender mismatch: %x %x", expectedSender, decodedSender) | 
					
						
							| 
									
										
										
										
											2015-04-10 11:55:31 +02:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2015-04-19 00:35:48 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	expectedData := mustConvertBytes(txTest.Transaction.Data) | 
					
						
							| 
									
										
										
										
											2015-06-11 14:05:32 +02:00
										 |  |  | 	if !bytes.Equal(expectedData, decodedTx.Data()) { | 
					
						
							|  |  |  | 		return fmt.Errorf("Tx input data mismatch: %#v %#v", expectedData, decodedTx.Data()) | 
					
						
							| 
									
										
										
										
											2015-04-10 11:55:31 +02:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2015-04-19 00:35:48 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	expectedGasLimit := mustConvertBigInt(txTest.Transaction.GasLimit, 16) | 
					
						
							| 
									
										
										
										
											2015-06-11 14:05:32 +02:00
										 |  |  | 	if expectedGasLimit.Cmp(decodedTx.Gas()) != 0 { | 
					
						
							|  |  |  | 		return fmt.Errorf("GasLimit mismatch: %v %v", expectedGasLimit, decodedTx.Gas()) | 
					
						
							| 
									
										
										
										
											2015-04-10 11:55:31 +02:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2015-04-19 00:35:48 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	expectedGasPrice := mustConvertBigInt(txTest.Transaction.GasPrice, 16) | 
					
						
							| 
									
										
										
										
											2015-06-11 14:05:32 +02:00
										 |  |  | 	if expectedGasPrice.Cmp(decodedTx.GasPrice()) != 0 { | 
					
						
							|  |  |  | 		return fmt.Errorf("GasPrice mismatch: %v %v", expectedGasPrice, decodedTx.GasPrice()) | 
					
						
							| 
									
										
										
										
											2015-04-10 11:55:31 +02:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2015-04-19 00:35:48 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	expectedNonce := mustConvertUint(txTest.Transaction.Nonce, 16) | 
					
						
							| 
									
										
										
										
											2015-06-11 14:05:32 +02:00
										 |  |  | 	if expectedNonce != decodedTx.Nonce() { | 
					
						
							|  |  |  | 		return fmt.Errorf("Nonce mismatch: %v %v", expectedNonce, decodedTx.Nonce()) | 
					
						
							| 
									
										
										
										
											2015-04-10 11:55:31 +02:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2015-04-19 00:35:48 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-08 23:07:44 +01:00
										 |  |  | 	v, r, s := decodedTx.RawSignatureValues() | 
					
						
							| 
									
										
										
										
											2015-06-11 14:05:32 +02:00
										 |  |  | 	expectedR := mustConvertBigInt(txTest.Transaction.R, 16) | 
					
						
							|  |  |  | 	if r.Cmp(expectedR) != 0 { | 
					
						
							|  |  |  | 		return fmt.Errorf("R mismatch: %v %v", expectedR, r) | 
					
						
							| 
									
										
										
										
											2015-04-10 11:55:31 +02:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2015-06-11 14:05:32 +02:00
										 |  |  | 	expectedS := mustConvertBigInt(txTest.Transaction.S, 16) | 
					
						
							|  |  |  | 	if s.Cmp(expectedS) != 0 { | 
					
						
							|  |  |  | 		return fmt.Errorf("S mismatch: %v %v", expectedS, s) | 
					
						
							| 
									
										
										
										
											2015-04-10 11:55:31 +02:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2016-11-08 23:07:44 +01:00
										 |  |  | 	expectedV := mustConvertBigInt(txTest.Transaction.V, 16) | 
					
						
							|  |  |  | 	if v.Cmp(expectedV) != 0 { | 
					
						
							| 
									
										
										
										
											2015-06-11 14:05:32 +02:00
										 |  |  | 		return fmt.Errorf("V mismatch: %v %v", expectedV, v) | 
					
						
							| 
									
										
										
										
											2015-04-10 11:55:31 +02:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2015-04-19 00:35:48 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	expectedTo := mustConvertAddress(txTest.Transaction.To) | 
					
						
							| 
									
										
										
										
											2015-06-11 14:05:32 +02:00
										 |  |  | 	if decodedTx.To() == nil { | 
					
						
							| 
									
										
										
										
											2015-04-15 22:37:16 +02:00
										 |  |  | 		if expectedTo != common.BytesToAddress([]byte{}) { // "empty" or "zero" address | 
					
						
							|  |  |  | 			return fmt.Errorf("To mismatch when recipient is nil (contract creation): %v", expectedTo) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} else { | 
					
						
							| 
									
										
										
										
											2015-06-11 14:05:32 +02:00
										 |  |  | 		if expectedTo != *decodedTx.To() { | 
					
						
							|  |  |  | 			return fmt.Errorf("To mismatch: %v %v", expectedTo, *decodedTx.To()) | 
					
						
							| 
									
										
										
										
											2015-04-15 22:37:16 +02:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2015-04-10 11:55:31 +02:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2015-04-15 22:37:16 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-04-19 00:35:48 +02:00
										 |  |  | 	expectedValue := mustConvertBigInt(txTest.Transaction.Value, 16) | 
					
						
							| 
									
										
										
										
											2015-06-11 14:05:32 +02:00
										 |  |  | 	if expectedValue.Cmp(decodedTx.Value()) != 0 { | 
					
						
							|  |  |  | 		return fmt.Errorf("Value mismatch: %v %v", expectedValue, decodedTx.Value()) | 
					
						
							| 
									
										
										
										
											2015-04-10 11:55:31 +02:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return nil | 
					
						
							|  |  |  | } |