2015-07-06 17:54:22 -07:00
|
|
|
// Copyright 2014 The go-ethereum Authors
|
2015-07-22 09:48:40 -07:00
|
|
|
// This file is part of the go-ethereum library.
|
2015-07-06 17:54:22 -07:00
|
|
|
//
|
2015-07-23 09:35:11 -07:00
|
|
|
// The go-ethereum library is free software: you can redistribute it and/or modify
|
2015-07-06 17:54:22 -07: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 09:48:40 -07:00
|
|
|
// The go-ethereum library is distributed in the hope that it will be useful,
|
2015-07-06 17:54:22 -07:00
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2015-07-22 09:48:40 -07:00
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2015-07-06 17:54:22 -07: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 09:48:40 -07:00
|
|
|
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
2015-07-06 17:54:22 -07:00
|
|
|
|
2014-11-18 07:58:22 -08:00
|
|
|
package types
|
2015-03-16 17:34:18 -07:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2015-03-25 09:05:29 -07:00
|
|
|
"crypto/ecdsa"
|
2017-01-26 12:16:24 -08:00
|
|
|
"encoding/json"
|
2015-03-16 17:34:18 -07:00
|
|
|
"math/big"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/common"
|
2015-03-25 09:05:29 -07:00
|
|
|
"github.com/ethereum/go-ethereum/crypto"
|
2015-03-16 17:34:18 -07:00
|
|
|
"github.com/ethereum/go-ethereum/rlp"
|
|
|
|
)
|
|
|
|
|
|
|
|
// The values in those tests are from the Transaction Tests
|
|
|
|
// at github.com/ethereum/tests.
|
|
|
|
var (
|
2015-06-11 05:05:32 -07:00
|
|
|
emptyTx = NewTransaction(
|
|
|
|
0,
|
2015-03-16 17:34:18 -07:00
|
|
|
common.HexToAddress("095e7baea6a6c7c4c2dfeb977efac326af552d87"),
|
2017-11-13 03:47:27 -08:00
|
|
|
big.NewInt(0), 0, big.NewInt(0),
|
2015-03-16 17:34:18 -07:00
|
|
|
nil,
|
|
|
|
)
|
|
|
|
|
2015-06-11 05:05:32 -07:00
|
|
|
rightvrsTx, _ = NewTransaction(
|
|
|
|
3,
|
|
|
|
common.HexToAddress("b94f5374fce5edbc8e2a8697c15331677e6ebf0b"),
|
|
|
|
big.NewInt(10),
|
2017-11-13 03:47:27 -08:00
|
|
|
2000,
|
2015-06-11 05:05:32 -07:00
|
|
|
big.NewInt(1),
|
|
|
|
common.FromHex("5544"),
|
|
|
|
).WithSignature(
|
2016-11-02 05:44:13 -07:00
|
|
|
HomesteadSigner{},
|
2017-01-05 02:35:23 -08:00
|
|
|
common.Hex2Bytes("98ff921201554726367d2be8c804a7ff89ccf285ebc57dff8ae4c44b9c19ac4a8887321be575c8095f789dd4c743dfe42c1820f9231f98a962b210e3ac2452a301"),
|
2015-06-11 05:05:32 -07:00
|
|
|
)
|
2018-07-24 23:36:01 -07:00
|
|
|
|
|
|
|
rightvrsTx2, _ = NewTransaction(
|
|
|
|
3,
|
|
|
|
common.HexToAddress("b94f5374fce5edbc8e2a8697c15331677e6ebf0b"),
|
|
|
|
big.NewInt(10),
|
|
|
|
2000,
|
|
|
|
big.NewInt(0),
|
|
|
|
common.FromHex("5544"),
|
|
|
|
).WithSignature(
|
|
|
|
HomesteadSigner{},
|
|
|
|
common.Hex2Bytes("98ff921201554726367d2be8c804a7ff89ccf285ebc57dff8ae4c44b9c19ac4a8887321be575c8095f789dd4c743dfe42c1820f9231f98a962b210e3ac2452a301"),
|
|
|
|
)
|
2015-03-16 17:34:18 -07:00
|
|
|
)
|
|
|
|
|
2015-07-06 01:43:06 -07:00
|
|
|
func TestTransactionSigHash(t *testing.T) {
|
2017-10-01 02:03:28 -07:00
|
|
|
var homestead HomesteadSigner
|
|
|
|
if homestead.Hash(emptyTx) != common.HexToHash("c775b99e7ad12f50d819fcd602390467e28141316969f4b57f0626f74fe3b386") {
|
2015-03-16 17:34:18 -07:00
|
|
|
t.Errorf("empty transaction hash mismatch, got %x", emptyTx.Hash())
|
|
|
|
}
|
2017-10-01 02:03:28 -07:00
|
|
|
if homestead.Hash(rightvrsTx) != common.HexToHash("fe7a79529ed5f7c3375d06b26b186a8644e0e16c373d7a12be41c62d6042b77a") {
|
2015-03-16 17:34:18 -07:00
|
|
|
t.Errorf("RightVRS transaction hash mismatch, got %x", rightvrsTx.Hash())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestTransactionEncode(t *testing.T) {
|
|
|
|
txb, err := rlp.EncodeToBytes(rightvrsTx)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("encode error: %v", err)
|
|
|
|
}
|
|
|
|
should := common.FromHex("f86103018207d094b94f5374fce5edbc8e2a8697c15331677e6ebf0b0a8255441ca098ff921201554726367d2be8c804a7ff89ccf285ebc57dff8ae4c44b9c19ac4aa08887321be575c8095f789dd4c743dfe42c1820f9231f98a962b210e3ac2452a3")
|
|
|
|
if !bytes.Equal(txb, should) {
|
|
|
|
t.Errorf("encoded RLP mismatch, got %x", txb)
|
|
|
|
}
|
|
|
|
}
|
2015-03-25 09:05:29 -07:00
|
|
|
|
2018-07-24 23:36:01 -07:00
|
|
|
// Test from the original quorum implementation
|
|
|
|
func TestTransactionEncode2(t *testing.T) {
|
|
|
|
txb, err := rlp.EncodeToBytes(rightvrsTx2)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("encode error: %v", err)
|
|
|
|
}
|
|
|
|
should := common.FromHex("f86103808207d094b94f5374fce5edbc8e2a8697c15331677e6ebf0b0a8255441ca098ff921201554726367d2be8c804a7ff89ccf285ebc57dff8ae4c44b9c19ac4aa08887321be575c8095f789dd4c743dfe42c1820f9231f98a962b210e3ac2452a3")
|
|
|
|
if !bytes.Equal(txb, should) {
|
|
|
|
t.Errorf("encoded RLP mismatch, got %x", txb)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-25 09:05:29 -07:00
|
|
|
func decodeTx(data []byte) (*Transaction, error) {
|
|
|
|
var tx Transaction
|
2016-11-02 05:44:13 -07:00
|
|
|
t, err := &tx, rlp.Decode(bytes.NewReader(data), &tx)
|
|
|
|
|
|
|
|
return t, err
|
2015-03-25 09:05:29 -07:00
|
|
|
}
|
|
|
|
|
2015-06-04 07:52:23 -07:00
|
|
|
func defaultTestKey() (*ecdsa.PrivateKey, common.Address) {
|
2017-05-23 04:58:03 -07:00
|
|
|
key, _ := crypto.HexToECDSA("45a915e4d060149eb4365960e6a7a45f334393093061116b197e3240065ff2d8")
|
2015-03-25 09:05:29 -07:00
|
|
|
addr := crypto.PubkeyToAddress(key.PublicKey)
|
|
|
|
return key, addr
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRecipientEmpty(t *testing.T) {
|
|
|
|
_, addr := defaultTestKey()
|
|
|
|
tx, err := decodeTx(common.Hex2Bytes("f8498080808080011ca09b16de9d5bdee2cf56c28d16275a4da68cd30273e2525f3959f5d62557489921a0372ebd8fb3345f7db7b5a86d42e24d36e983e259b0664ceb8c227ec9af572f3d"))
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
t.FailNow()
|
|
|
|
}
|
2016-11-02 05:44:13 -07:00
|
|
|
|
|
|
|
from, err := Sender(HomesteadSigner{}, tx)
|
2015-03-25 09:05:29 -07:00
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
t.FailNow()
|
|
|
|
}
|
2015-06-04 07:52:23 -07:00
|
|
|
if addr != from {
|
2015-03-25 09:05:29 -07:00
|
|
|
t.Error("derived address doesn't match")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRecipientNormal(t *testing.T) {
|
|
|
|
_, addr := defaultTestKey()
|
|
|
|
|
|
|
|
tx, err := decodeTx(common.Hex2Bytes("f85d80808094000000000000000000000000000000000000000080011ca0527c0d8f5c63f7b9f41324a7c8a563ee1190bcbf0dac8ab446291bdbf32f5c79a0552c4ef0a09a04395074dab9ed34d3fbfb843c2f2546cc30fe89ec143ca94ca6"))
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
|
2016-11-02 05:44:13 -07:00
|
|
|
from, err := Sender(HomesteadSigner{}, tx)
|
2015-03-25 09:05:29 -07:00
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
|
2015-06-04 07:52:23 -07:00
|
|
|
if addr != from {
|
2015-03-25 09:05:29 -07:00
|
|
|
t.Error("derived address doesn't match")
|
|
|
|
}
|
|
|
|
}
|
2016-01-22 02:05:17 -08:00
|
|
|
|
|
|
|
// Tests that transactions can be correctly sorted according to their price in
|
|
|
|
// decreasing order, but at the same time with increasing nonces when issued by
|
|
|
|
// the same account.
|
|
|
|
func TestTransactionPriceNonceSort(t *testing.T) {
|
|
|
|
// Generate a batch of accounts to start with
|
|
|
|
keys := make([]*ecdsa.PrivateKey, 25)
|
|
|
|
for i := 0; i < len(keys); i++ {
|
|
|
|
keys[i], _ = crypto.GenerateKey()
|
|
|
|
}
|
2016-11-02 05:44:13 -07:00
|
|
|
|
|
|
|
signer := HomesteadSigner{}
|
2016-01-22 02:05:17 -08:00
|
|
|
// Generate a batch of transactions with overlapping values, but shifted nonces
|
2016-07-01 08:59:55 -07:00
|
|
|
groups := map[common.Address]Transactions{}
|
2016-01-22 02:05:17 -08:00
|
|
|
for start, key := range keys {
|
2016-07-01 08:59:55 -07:00
|
|
|
addr := crypto.PubkeyToAddress(key.PublicKey)
|
2016-01-22 02:05:17 -08:00
|
|
|
for i := 0; i < 25; i++ {
|
2017-11-13 03:47:27 -08:00
|
|
|
tx, _ := SignTx(NewTransaction(uint64(start+i), common.Address{}, big.NewInt(100), 100, big.NewInt(int64(start+i)), nil), signer, key)
|
2016-07-01 08:59:55 -07:00
|
|
|
groups[addr] = append(groups[addr], tx)
|
2016-01-22 02:05:17 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// Sort the transactions and cross check the nonce ordering
|
2017-09-07 14:22:27 -07:00
|
|
|
txset := NewTransactionsByPriceAndNonce(signer, groups)
|
2016-08-09 04:54:36 -07:00
|
|
|
|
|
|
|
txs := Transactions{}
|
2017-10-11 03:35:44 -07:00
|
|
|
for tx := txset.Peek(); tx != nil; tx = txset.Peek() {
|
|
|
|
txs = append(txs, tx)
|
|
|
|
txset.Shift()
|
|
|
|
}
|
|
|
|
if len(txs) != 25*25 {
|
|
|
|
t.Errorf("expected %d transactions, found %d", 25*25, len(txs))
|
2016-08-09 04:54:36 -07:00
|
|
|
}
|
2016-01-22 02:05:17 -08:00
|
|
|
for i, txi := range txs {
|
2016-11-02 05:44:13 -07:00
|
|
|
fromi, _ := Sender(signer, txi)
|
2016-01-22 02:05:17 -08:00
|
|
|
|
|
|
|
// Make sure the nonce order is valid
|
|
|
|
for j, txj := range txs[i+1:] {
|
2016-11-02 05:44:13 -07:00
|
|
|
fromj, _ := Sender(signer, txj)
|
2016-01-22 02:05:17 -08:00
|
|
|
|
|
|
|
if fromi == fromj && txi.Nonce() > txj.Nonce() {
|
|
|
|
t.Errorf("invalid nonce ordering: tx #%d (A=%x N=%v) < tx #%d (A=%x N=%v)", i, fromi[:4], txi.Nonce(), i+j, fromj[:4], txj.Nonce())
|
|
|
|
}
|
|
|
|
}
|
2018-05-29 05:21:04 -07:00
|
|
|
|
|
|
|
// If the next tx has different from account, the price must be lower than the current one
|
|
|
|
if i+1 < len(txs) {
|
|
|
|
next := txs[i+1]
|
|
|
|
fromNext, _ := Sender(signer, next)
|
|
|
|
if fromi != fromNext && txi.GasPrice().Cmp(next.GasPrice()) < 0 {
|
|
|
|
t.Errorf("invalid gasprice ordering: tx #%d (A=%x P=%v) < tx #%d (A=%x P=%v)", i, fromi[:4], txi.GasPrice(), i+1, fromNext[:4], next.GasPrice())
|
2016-01-22 02:05:17 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-01-26 12:16:24 -08:00
|
|
|
|
|
|
|
// TestTransactionJSON tests serializing/de-serializing to/from JSON.
|
|
|
|
func TestTransactionJSON(t *testing.T) {
|
|
|
|
key, err := crypto.GenerateKey()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("could not generate key: %v", err)
|
|
|
|
}
|
|
|
|
signer := NewEIP155Signer(common.Big1)
|
|
|
|
|
2018-09-29 13:47:59 -07:00
|
|
|
transactions := make([]*Transaction, 0, 50)
|
2017-01-26 12:16:24 -08:00
|
|
|
for i := uint64(0); i < 25; i++ {
|
|
|
|
var tx *Transaction
|
|
|
|
switch i % 2 {
|
|
|
|
case 0:
|
2017-11-13 03:47:27 -08:00
|
|
|
tx = NewTransaction(i, common.Address{1}, common.Big0, 1, common.Big2, []byte("abcdef"))
|
2017-01-26 12:16:24 -08:00
|
|
|
case 1:
|
2017-11-13 03:47:27 -08:00
|
|
|
tx = NewContractCreation(i, common.Big0, 1, common.Big2, []byte("abcdef"))
|
2017-01-26 12:16:24 -08:00
|
|
|
}
|
2018-09-29 13:47:59 -07:00
|
|
|
transactions = append(transactions, tx)
|
2017-01-26 12:16:24 -08:00
|
|
|
|
2018-09-29 13:47:59 -07:00
|
|
|
signedTx, err := SignTx(tx, signer, key)
|
2017-01-26 12:16:24 -08:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("could not sign transaction: %v", err)
|
|
|
|
}
|
|
|
|
|
2018-09-29 13:47:59 -07:00
|
|
|
transactions = append(transactions, signedTx)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tx := range transactions {
|
2017-01-26 12:16:24 -08:00
|
|
|
data, err := json.Marshal(tx)
|
|
|
|
if err != nil {
|
2018-09-29 13:47:59 -07:00
|
|
|
t.Fatalf("json.Marshal failed: %v", err)
|
2017-01-26 12:16:24 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
var parsedTx *Transaction
|
|
|
|
if err := json.Unmarshal(data, &parsedTx); err != nil {
|
2018-09-29 13:47:59 -07:00
|
|
|
t.Fatalf("json.Unmarshal failed: %v", err)
|
2017-01-26 12:16:24 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
// compare nonce, price, gaslimit, recipient, amount, payload, V, R, S
|
|
|
|
if tx.Hash() != parsedTx.Hash() {
|
|
|
|
t.Errorf("parsed tx differs from original tx, want %v, got %v", tx, parsedTx)
|
|
|
|
}
|
|
|
|
if tx.ChainId().Cmp(parsedTx.ChainId()) != 0 {
|
|
|
|
t.Errorf("invalid chain id, want %d, got %d", tx.ChainId(), parsedTx.ChainId())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|