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
|
2014-02-14 14:56:09 -08:00
|
|
|
|
|
|
|
import (
|
2016-01-22 02:05:17 -08:00
|
|
|
"container/heap"
|
2015-03-18 05:24:34 -07:00
|
|
|
"crypto/ecdsa"
|
2016-08-03 18:55:33 -07:00
|
|
|
"encoding/json"
|
2015-03-17 03:58:31 -07:00
|
|
|
"errors"
|
2014-05-22 08:35:26 -07:00
|
|
|
"fmt"
|
2015-06-11 05:05:32 -07:00
|
|
|
"io"
|
2014-08-09 10:09:55 -07:00
|
|
|
"math/big"
|
2015-06-25 05:45:54 -07:00
|
|
|
"sync/atomic"
|
2014-08-09 10:09:55 -07:00
|
|
|
|
2015-03-16 09:43:42 -07:00
|
|
|
"github.com/ethereum/go-ethereum/common"
|
2014-10-31 04:37:43 -07:00
|
|
|
"github.com/ethereum/go-ethereum/crypto"
|
2016-11-03 07:54:33 -07:00
|
|
|
"github.com/ethereum/go-ethereum/params"
|
2014-12-23 04:48:44 -08:00
|
|
|
"github.com/ethereum/go-ethereum/rlp"
|
2014-02-14 14:56:09 -08:00
|
|
|
)
|
|
|
|
|
2016-08-03 18:55:33 -07:00
|
|
|
var ErrInvalidSig = errors.New("invalid transaction v, r, s values")
|
|
|
|
|
|
|
|
var (
|
|
|
|
errMissingTxSignatureFields = errors.New("missing required JSON transaction signature fields")
|
|
|
|
errMissingTxFields = errors.New("missing required JSON transaction fields")
|
|
|
|
)
|
2015-07-06 02:54:11 -07:00
|
|
|
|
2014-02-14 14:56:09 -08:00
|
|
|
type Transaction struct {
|
2015-06-11 05:05:32 -07:00
|
|
|
data txdata
|
2015-06-25 05:45:54 -07:00
|
|
|
// caches
|
|
|
|
hash atomic.Value
|
|
|
|
size atomic.Value
|
|
|
|
from atomic.Value
|
2015-06-11 05:05:32 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
type txdata struct {
|
|
|
|
AccountNonce uint64
|
|
|
|
Price, GasLimit *big.Int
|
|
|
|
Recipient *common.Address `rlp:"nil"` // nil means contract creation
|
|
|
|
Amount *big.Int
|
|
|
|
Payload []byte
|
|
|
|
V byte // signature
|
|
|
|
R, S *big.Int // signature
|
2014-03-20 16:04:31 -07:00
|
|
|
}
|
|
|
|
|
2016-08-03 18:55:33 -07:00
|
|
|
type jsonTransaction struct {
|
|
|
|
Hash *common.Hash `json:"hash"`
|
|
|
|
AccountNonce *hexUint64 `json:"nonce"`
|
|
|
|
Price *hexBig `json:"gasPrice"`
|
|
|
|
GasLimit *hexBig `json:"gas"`
|
|
|
|
Recipient *common.Address `json:"to"`
|
|
|
|
Amount *hexBig `json:"value"`
|
|
|
|
Payload *hexBytes `json:"input"`
|
|
|
|
V *hexUint64 `json:"v"`
|
|
|
|
R *hexBig `json:"r"`
|
|
|
|
S *hexBig `json:"s"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewContractCreation creates a new transaction with no recipient.
|
2015-06-11 05:05:32 -07:00
|
|
|
func NewContractCreation(nonce uint64, amount, gasLimit, gasPrice *big.Int, data []byte) *Transaction {
|
|
|
|
if len(data) > 0 {
|
|
|
|
data = common.CopyBytes(data)
|
|
|
|
}
|
|
|
|
return &Transaction{data: txdata{
|
|
|
|
AccountNonce: nonce,
|
|
|
|
Recipient: nil,
|
|
|
|
Amount: new(big.Int).Set(amount),
|
|
|
|
GasLimit: new(big.Int).Set(gasLimit),
|
2016-11-07 03:23:49 -08:00
|
|
|
Price: new(big.Int),
|
2015-06-11 05:05:32 -07:00
|
|
|
Payload: data,
|
|
|
|
R: new(big.Int),
|
|
|
|
S: new(big.Int),
|
|
|
|
}}
|
|
|
|
}
|
|
|
|
|
2016-08-03 18:55:33 -07:00
|
|
|
// NewTransaction creates a new transaction with the given fields.
|
2015-06-11 05:05:32 -07:00
|
|
|
func NewTransaction(nonce uint64, to common.Address, amount, gasLimit, gasPrice *big.Int, data []byte) *Transaction {
|
|
|
|
if len(data) > 0 {
|
|
|
|
data = common.CopyBytes(data)
|
|
|
|
}
|
|
|
|
d := txdata{
|
|
|
|
AccountNonce: nonce,
|
|
|
|
Recipient: &to,
|
|
|
|
Payload: data,
|
|
|
|
Amount: new(big.Int),
|
|
|
|
GasLimit: new(big.Int),
|
|
|
|
Price: new(big.Int),
|
|
|
|
R: new(big.Int),
|
|
|
|
S: new(big.Int),
|
2015-04-04 12:41:24 -07:00
|
|
|
}
|
2015-06-11 05:05:32 -07:00
|
|
|
if amount != nil {
|
|
|
|
d.Amount.Set(amount)
|
|
|
|
}
|
|
|
|
if gasLimit != nil {
|
|
|
|
d.GasLimit.Set(gasLimit)
|
|
|
|
}
|
|
|
|
return &Transaction{data: d}
|
2014-03-20 16:04:31 -07:00
|
|
|
}
|
|
|
|
|
2016-08-03 18:55:33 -07:00
|
|
|
// DecodeRLP implements rlp.Encoder
|
2015-06-11 05:05:32 -07:00
|
|
|
func (tx *Transaction) EncodeRLP(w io.Writer) error {
|
|
|
|
return rlp.Encode(w, &tx.data)
|
2014-12-18 06:18:13 -08:00
|
|
|
}
|
|
|
|
|
2016-08-03 18:55:33 -07:00
|
|
|
// DecodeRLP implements rlp.Decoder
|
2015-06-11 05:05:32 -07:00
|
|
|
func (tx *Transaction) DecodeRLP(s *rlp.Stream) error {
|
2015-06-25 05:45:54 -07:00
|
|
|
_, size, _ := s.Kind()
|
|
|
|
err := s.Decode(&tx.data)
|
|
|
|
if err == nil {
|
|
|
|
tx.size.Store(common.StorageSize(rlp.ListSize(size)))
|
|
|
|
}
|
|
|
|
return err
|
2014-12-18 06:18:13 -08:00
|
|
|
}
|
|
|
|
|
2016-09-08 08:16:53 -07:00
|
|
|
// MarshalJSON encodes transactions into the web3 RPC response block format.
|
|
|
|
func (tx *Transaction) MarshalJSON() ([]byte, error) {
|
|
|
|
hash, v := tx.Hash(), uint64(tx.data.V)
|
|
|
|
|
|
|
|
return json.Marshal(&jsonTransaction{
|
|
|
|
Hash: &hash,
|
|
|
|
AccountNonce: (*hexUint64)(&tx.data.AccountNonce),
|
|
|
|
Price: (*hexBig)(tx.data.Price),
|
|
|
|
GasLimit: (*hexBig)(tx.data.GasLimit),
|
|
|
|
Recipient: tx.data.Recipient,
|
|
|
|
Amount: (*hexBig)(tx.data.Amount),
|
|
|
|
Payload: (*hexBytes)(&tx.data.Payload),
|
|
|
|
V: (*hexUint64)(&v),
|
|
|
|
R: (*hexBig)(tx.data.R),
|
|
|
|
S: (*hexBig)(tx.data.S),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2016-08-03 18:55:33 -07:00
|
|
|
// UnmarshalJSON decodes the web3 RPC transaction format.
|
|
|
|
func (tx *Transaction) UnmarshalJSON(input []byte) error {
|
|
|
|
var dec jsonTransaction
|
|
|
|
if err := json.Unmarshal(input, &dec); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
// Ensure that all fields are set. V, R, S are checked separately because they're a
|
|
|
|
// recent addition to the RPC spec (as of August 2016) and older implementations might
|
|
|
|
// not provide them. Note that Recipient is not checked because it can be missing for
|
|
|
|
// contract creations.
|
|
|
|
if dec.V == nil || dec.R == nil || dec.S == nil {
|
|
|
|
return errMissingTxSignatureFields
|
|
|
|
}
|
|
|
|
if !crypto.ValidateSignatureValues(byte(*dec.V), (*big.Int)(dec.R), (*big.Int)(dec.S), false) {
|
|
|
|
return ErrInvalidSig
|
|
|
|
}
|
|
|
|
if dec.AccountNonce == nil || dec.Price == nil || dec.GasLimit == nil || dec.Amount == nil || dec.Payload == nil {
|
|
|
|
return errMissingTxFields
|
|
|
|
}
|
|
|
|
// Assign the fields. This is not atomic but reusing transactions
|
|
|
|
// for decoding isn't thread safe anyway.
|
|
|
|
*tx = Transaction{}
|
|
|
|
tx.data = txdata{
|
|
|
|
AccountNonce: uint64(*dec.AccountNonce),
|
|
|
|
Recipient: dec.Recipient,
|
|
|
|
Amount: (*big.Int)(dec.Amount),
|
|
|
|
GasLimit: (*big.Int)(dec.GasLimit),
|
|
|
|
Price: (*big.Int)(dec.Price),
|
|
|
|
Payload: *dec.Payload,
|
|
|
|
V: byte(*dec.V),
|
|
|
|
R: (*big.Int)(dec.R),
|
|
|
|
S: (*big.Int)(dec.S),
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-06-11 05:05:32 -07:00
|
|
|
func (tx *Transaction) Data() []byte { return common.CopyBytes(tx.data.Payload) }
|
|
|
|
func (tx *Transaction) Gas() *big.Int { return new(big.Int).Set(tx.data.GasLimit) }
|
|
|
|
func (tx *Transaction) GasPrice() *big.Int { return new(big.Int).Set(tx.data.Price) }
|
|
|
|
func (tx *Transaction) Value() *big.Int { return new(big.Int).Set(tx.data.Amount) }
|
|
|
|
func (tx *Transaction) Nonce() uint64 { return tx.data.AccountNonce }
|
2016-07-11 02:58:10 -07:00
|
|
|
func (tx *Transaction) CheckNonce() bool { return true }
|
2014-12-18 06:18:13 -08:00
|
|
|
|
2015-06-11 05:05:32 -07:00
|
|
|
func (tx *Transaction) To() *common.Address {
|
|
|
|
if tx.data.Recipient == nil {
|
|
|
|
return nil
|
|
|
|
} else {
|
|
|
|
to := *tx.data.Recipient
|
|
|
|
return &to
|
|
|
|
}
|
2014-12-18 06:18:13 -08:00
|
|
|
}
|
|
|
|
|
2015-07-06 01:43:06 -07:00
|
|
|
// Hash hashes the RLP encoding of tx.
|
|
|
|
// It uniquely identifies the transaction.
|
2015-06-11 05:05:32 -07:00
|
|
|
func (tx *Transaction) Hash() common.Hash {
|
2015-06-25 05:45:54 -07:00
|
|
|
if hash := tx.hash.Load(); hash != nil {
|
|
|
|
return hash.(common.Hash)
|
|
|
|
}
|
2015-07-06 01:43:06 -07:00
|
|
|
v := rlpHash(tx)
|
|
|
|
tx.hash.Store(v)
|
|
|
|
return v
|
|
|
|
}
|
|
|
|
|
|
|
|
// SigHash returns the hash to be signed by the sender.
|
|
|
|
// It does not uniquely identify the transaction.
|
|
|
|
func (tx *Transaction) SigHash() common.Hash {
|
|
|
|
return rlpHash([]interface{}{
|
2015-06-11 05:05:32 -07:00
|
|
|
tx.data.AccountNonce,
|
|
|
|
tx.data.Price,
|
|
|
|
tx.data.GasLimit,
|
|
|
|
tx.data.Recipient,
|
|
|
|
tx.data.Amount,
|
|
|
|
tx.data.Payload,
|
|
|
|
})
|
2014-12-18 06:18:13 -08:00
|
|
|
}
|
|
|
|
|
2015-06-11 05:05:32 -07:00
|
|
|
func (tx *Transaction) Size() common.StorageSize {
|
2015-06-25 05:45:54 -07:00
|
|
|
if size := tx.size.Load(); size != nil {
|
|
|
|
return size.(common.StorageSize)
|
|
|
|
}
|
|
|
|
c := writeCounter(0)
|
|
|
|
rlp.Encode(&c, &tx.data)
|
|
|
|
tx.size.Store(common.StorageSize(c))
|
|
|
|
return common.StorageSize(c)
|
2014-12-18 06:18:13 -08:00
|
|
|
}
|
|
|
|
|
2016-01-19 14:50:00 -08:00
|
|
|
// From returns the address derived from the signature (V, R, S) using secp256k1
|
2016-03-15 11:08:18 -07:00
|
|
|
// elliptic curve and an error if it failed deriving or upon an incorrect
|
2016-01-19 14:50:00 -08:00
|
|
|
// signature.
|
|
|
|
//
|
|
|
|
// From Uses the homestead consensus rules to determine whether the signature is
|
|
|
|
// valid.
|
|
|
|
//
|
|
|
|
// From caches the address, allowing it to be used regardless of
|
2015-11-27 06:40:29 -08:00
|
|
|
// Frontier / Homestead. however, the first time called it runs
|
|
|
|
// signature validations, so we need two versions. This makes it
|
|
|
|
// easier to ensure backwards compatibility of things like package rpc
|
|
|
|
// where eth_getblockbynumber uses tx.From() and needs to work for
|
|
|
|
// both txs before and after the first homestead block. Signatures
|
|
|
|
// valid in homestead are a subset of valid ones in Frontier)
|
2015-06-11 05:05:32 -07:00
|
|
|
func (tx *Transaction) From() (common.Address, error) {
|
2015-11-27 06:40:29 -08:00
|
|
|
return doFrom(tx, true)
|
|
|
|
}
|
|
|
|
|
2016-01-19 14:50:00 -08:00
|
|
|
// FromFrontier returns the address derived from the signature (V, R, S) using
|
2016-03-15 11:08:18 -07:00
|
|
|
// secp256k1 elliptic curve and an error if it failed deriving or upon an
|
2016-01-19 14:50:00 -08:00
|
|
|
// incorrect signature.
|
|
|
|
//
|
|
|
|
// FromFrantier uses the frontier consensus rules to determine whether the
|
|
|
|
// signature is valid.
|
|
|
|
//
|
|
|
|
// FromFrontier caches the address, allowing it to be used regardless of
|
|
|
|
// Frontier / Homestead. however, the first time called it runs
|
|
|
|
// signature validations, so we need two versions. This makes it
|
|
|
|
// easier to ensure backwards compatibility of things like package rpc
|
|
|
|
// where eth_getblockbynumber uses tx.From() and needs to work for
|
|
|
|
// both txs before and after the first homestead block. Signatures
|
|
|
|
// valid in homestead are a subset of valid ones in Frontier)
|
2015-11-27 06:40:29 -08:00
|
|
|
func (tx *Transaction) FromFrontier() (common.Address, error) {
|
|
|
|
return doFrom(tx, false)
|
|
|
|
}
|
|
|
|
|
|
|
|
func doFrom(tx *Transaction, homestead bool) (common.Address, error) {
|
2015-06-25 05:45:54 -07:00
|
|
|
if from := tx.from.Load(); from != nil {
|
|
|
|
return from.(common.Address), nil
|
|
|
|
}
|
2015-11-27 06:40:29 -08:00
|
|
|
pubkey, err := tx.publicKey(homestead)
|
2015-06-01 11:27:20 -07:00
|
|
|
if err != nil {
|
|
|
|
return common.Address{}, err
|
2015-03-17 03:58:31 -07:00
|
|
|
}
|
|
|
|
var addr common.Address
|
2016-02-21 10:40:27 -08:00
|
|
|
copy(addr[:], crypto.Keccak256(pubkey[1:])[12:])
|
2015-06-25 05:45:54 -07:00
|
|
|
tx.from.Store(addr)
|
2015-03-17 03:58:31 -07:00
|
|
|
return addr, nil
|
2014-12-18 06:18:13 -08:00
|
|
|
}
|
|
|
|
|
2015-06-11 05:05:32 -07:00
|
|
|
// Cost returns amount + gasprice * gaslimit.
|
|
|
|
func (tx *Transaction) Cost() *big.Int {
|
|
|
|
total := new(big.Int).Mul(tx.data.Price, tx.data.GasLimit)
|
|
|
|
total.Add(total, tx.data.Amount)
|
|
|
|
return total
|
2014-12-18 06:18:13 -08:00
|
|
|
}
|
|
|
|
|
2016-08-03 18:55:33 -07:00
|
|
|
// SignatureValues returns the ECDSA signature values contained in the transaction.
|
2015-06-11 05:05:32 -07:00
|
|
|
func (tx *Transaction) SignatureValues() (v byte, r *big.Int, s *big.Int) {
|
|
|
|
return tx.data.V, new(big.Int).Set(tx.data.R), new(big.Int).Set(tx.data.S)
|
2014-12-03 05:05:19 -08:00
|
|
|
}
|
|
|
|
|
2016-11-01 14:25:59 -07:00
|
|
|
func (tx *Transaction) IsPrivate() bool {
|
|
|
|
return tx.data.V == 37 || tx.data.V == 38
|
|
|
|
}
|
|
|
|
|
2016-11-13 21:16:20 -08:00
|
|
|
func (tx *Transaction) SetPrivate() {
|
|
|
|
if tx.data.V == 28 {
|
|
|
|
tx.data.V = 38
|
|
|
|
} else {
|
|
|
|
tx.data.V = 37
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-27 06:40:29 -08:00
|
|
|
func (tx *Transaction) publicKey(homestead bool) ([]byte, error) {
|
|
|
|
if !crypto.ValidateSignatureValues(tx.data.V, tx.data.R, tx.data.S, homestead) {
|
2015-07-06 02:54:11 -07:00
|
|
|
return nil, ErrInvalidSig
|
2015-06-01 11:27:20 -07:00
|
|
|
}
|
2014-02-14 14:56:09 -08:00
|
|
|
|
2015-06-11 05:05:32 -07:00
|
|
|
// encode the signature in uncompressed format
|
|
|
|
r, s := tx.data.R.Bytes(), tx.data.S.Bytes()
|
|
|
|
sig := make([]byte, 65)
|
|
|
|
copy(sig[32-len(r):32], r)
|
|
|
|
copy(sig[64-len(s):64], s)
|
|
|
|
sig[64] = tx.data.V - 27
|
2016-11-01 14:25:59 -07:00
|
|
|
if tx.data.V > 28 {
|
|
|
|
sig[64] -= 10
|
|
|
|
}
|
2014-02-14 14:56:09 -08:00
|
|
|
|
2015-06-11 05:05:32 -07:00
|
|
|
// recover the public key from the signature
|
2015-07-06 01:43:06 -07:00
|
|
|
hash := tx.SigHash()
|
2015-06-11 05:05:32 -07:00
|
|
|
pub, err := crypto.Ecrecover(hash[:], sig)
|
2015-04-05 10:31:18 -07:00
|
|
|
if err != nil {
|
2015-06-01 11:27:20 -07:00
|
|
|
return nil, err
|
2015-04-05 10:31:18 -07:00
|
|
|
}
|
2015-06-11 05:05:32 -07:00
|
|
|
if len(pub) == 0 || pub[0] != 4 {
|
2015-06-01 11:27:20 -07:00
|
|
|
return nil, errors.New("invalid public key")
|
|
|
|
}
|
2015-06-11 05:05:32 -07:00
|
|
|
return pub, nil
|
2014-02-14 14:56:09 -08:00
|
|
|
}
|
|
|
|
|
2016-10-28 12:25:49 -07:00
|
|
|
// WithSignature returns a new transaction with the given signature.
|
|
|
|
// This signature needs to be formatted as described in the yellow paper (v+27).
|
2015-06-11 05:05:32 -07:00
|
|
|
func (tx *Transaction) WithSignature(sig []byte) (*Transaction, error) {
|
|
|
|
if len(sig) != 65 {
|
|
|
|
panic(fmt.Sprintf("wrong size for signature: got %d, want 65", len(sig)))
|
|
|
|
}
|
|
|
|
cpy := &Transaction{data: tx.data}
|
|
|
|
cpy.data.R = new(big.Int).SetBytes(sig[:32])
|
|
|
|
cpy.data.S = new(big.Int).SetBytes(sig[32:64])
|
2016-10-28 12:25:49 -07:00
|
|
|
cpy.data.V = sig[64]
|
2015-06-11 05:05:32 -07:00
|
|
|
return cpy, nil
|
2015-02-26 04:22:09 -08:00
|
|
|
}
|
|
|
|
|
2015-06-11 05:05:32 -07:00
|
|
|
func (tx *Transaction) SignECDSA(prv *ecdsa.PrivateKey) (*Transaction, error) {
|
2015-07-06 01:43:06 -07:00
|
|
|
h := tx.SigHash()
|
2016-10-28 12:25:49 -07:00
|
|
|
sig, err := crypto.SignEthereum(h[:], prv)
|
2015-03-18 05:24:34 -07:00
|
|
|
if err != nil {
|
2015-06-11 05:05:32 -07:00
|
|
|
return nil, err
|
2015-03-18 05:24:34 -07:00
|
|
|
}
|
2015-06-11 05:05:32 -07:00
|
|
|
return tx.WithSignature(sig)
|
2014-02-14 14:56:09 -08:00
|
|
|
}
|
|
|
|
|
2014-05-22 08:35:26 -07:00
|
|
|
func (tx *Transaction) String() string {
|
2015-03-17 03:58:31 -07:00
|
|
|
var from, to string
|
|
|
|
if f, err := tx.From(); err != nil {
|
|
|
|
from = "[invalid sender]"
|
|
|
|
} else {
|
|
|
|
from = fmt.Sprintf("%x", f[:])
|
|
|
|
}
|
2015-06-11 05:05:32 -07:00
|
|
|
if tx.data.Recipient == nil {
|
2015-03-17 03:58:31 -07:00
|
|
|
to = "[contract creation]"
|
|
|
|
} else {
|
2015-06-11 05:05:32 -07:00
|
|
|
to = fmt.Sprintf("%x", tx.data.Recipient[:])
|
2015-03-17 03:58:31 -07:00
|
|
|
}
|
2015-06-11 05:05:32 -07:00
|
|
|
enc, _ := rlp.EncodeToBytes(&tx.data)
|
2014-05-22 08:35:26 -07:00
|
|
|
return fmt.Sprintf(`
|
|
|
|
TX(%x)
|
|
|
|
Contract: %v
|
2015-03-17 03:58:31 -07:00
|
|
|
From: %s
|
|
|
|
To: %s
|
2014-12-23 05:33:15 -08:00
|
|
|
Nonce: %v
|
|
|
|
GasPrice: %v
|
|
|
|
GasLimit %v
|
|
|
|
Value: %v
|
2014-05-22 08:35:26 -07:00
|
|
|
Data: 0x%x
|
|
|
|
V: 0x%x
|
|
|
|
R: 0x%x
|
|
|
|
S: 0x%x
|
2014-12-19 04:34:53 -08:00
|
|
|
Hex: %x
|
2014-12-23 05:33:15 -08:00
|
|
|
`,
|
2014-05-22 08:35:26 -07:00
|
|
|
tx.Hash(),
|
2015-06-11 05:05:32 -07:00
|
|
|
len(tx.data.Recipient) == 0,
|
2015-03-17 03:58:31 -07:00
|
|
|
from,
|
|
|
|
to,
|
2015-06-11 05:05:32 -07:00
|
|
|
tx.data.AccountNonce,
|
|
|
|
tx.data.Price,
|
|
|
|
tx.data.GasLimit,
|
|
|
|
tx.data.Amount,
|
|
|
|
tx.data.Payload,
|
|
|
|
tx.data.V,
|
|
|
|
tx.data.R,
|
|
|
|
tx.data.S,
|
2015-03-18 05:24:34 -07:00
|
|
|
enc,
|
2014-12-19 04:34:53 -08:00
|
|
|
)
|
2014-05-22 08:35:26 -07:00
|
|
|
}
|
|
|
|
|
2015-06-11 05:05:32 -07:00
|
|
|
// Transaction slice type for basic sorting.
|
2014-05-28 03:06:09 -07:00
|
|
|
type Transactions []*Transaction
|
|
|
|
|
2015-08-17 05:01:41 -07:00
|
|
|
// Len returns the length of s
|
|
|
|
func (s Transactions) Len() int { return len(s) }
|
|
|
|
|
|
|
|
// Swap swaps the i'th and the j'th element in s
|
2015-03-18 05:24:34 -07:00
|
|
|
func (s Transactions) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
|
|
|
|
|
2015-08-17 05:01:41 -07:00
|
|
|
// GetRlp implements Rlpable and returns the i'th element of s in rlp
|
2015-03-18 05:24:34 -07:00
|
|
|
func (s Transactions) GetRlp(i int) []byte {
|
|
|
|
enc, _ := rlp.EncodeToBytes(s[i])
|
|
|
|
return enc
|
|
|
|
}
|
2014-05-28 03:06:09 -07:00
|
|
|
|
2015-08-17 05:01:41 -07:00
|
|
|
// Returns a new set t which is the difference between a to b
|
|
|
|
func TxDifference(a, b Transactions) (keep Transactions) {
|
|
|
|
keep = make(Transactions, 0, len(a))
|
|
|
|
|
|
|
|
remove := make(map[common.Hash]struct{})
|
|
|
|
for _, tx := range b {
|
|
|
|
remove[tx.Hash()] = struct{}{}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tx := range a {
|
|
|
|
if _, ok := remove[tx.Hash()]; !ok {
|
|
|
|
keep = append(keep, tx)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return keep
|
|
|
|
}
|
|
|
|
|
2016-01-22 02:05:17 -08:00
|
|
|
// TxByNonce implements the sort interface to allow sorting a list of transactions
|
|
|
|
// by their nonces. This is usually only useful for sorting transactions from a
|
|
|
|
// single account, otherwise a nonce comparison doesn't make much sense.
|
|
|
|
type TxByNonce Transactions
|
2014-05-28 03:06:09 -07:00
|
|
|
|
2016-01-22 02:05:17 -08:00
|
|
|
func (s TxByNonce) Len() int { return len(s) }
|
|
|
|
func (s TxByNonce) Less(i, j int) bool { return s[i].data.AccountNonce < s[j].data.AccountNonce }
|
|
|
|
func (s TxByNonce) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
|
|
|
|
|
|
|
|
// TxByPrice implements both the sort and the heap interface, making it useful
|
|
|
|
// for all at once sorting as well as individually adding and removing elements.
|
|
|
|
type TxByPrice Transactions
|
2015-08-03 12:57:09 -07:00
|
|
|
|
2016-01-22 02:05:17 -08:00
|
|
|
func (s TxByPrice) Len() int { return len(s) }
|
|
|
|
func (s TxByPrice) Less(i, j int) bool { return s[i].data.Price.Cmp(s[j].data.Price) > 0 }
|
|
|
|
func (s TxByPrice) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
|
2015-08-03 12:57:09 -07:00
|
|
|
|
2016-01-22 02:05:17 -08:00
|
|
|
func (s *TxByPrice) Push(x interface{}) {
|
|
|
|
*s = append(*s, x.(*Transaction))
|
2015-08-03 12:57:09 -07:00
|
|
|
}
|
|
|
|
|
2016-01-22 02:05:17 -08:00
|
|
|
func (s *TxByPrice) Pop() interface{} {
|
|
|
|
old := *s
|
|
|
|
n := len(old)
|
|
|
|
x := old[n-1]
|
|
|
|
*s = old[0 : n-1]
|
|
|
|
return x
|
|
|
|
}
|
2015-08-03 12:57:09 -07:00
|
|
|
|
2016-08-09 04:54:36 -07:00
|
|
|
// TransactionsByPriceAndNonce represents a set of transactions that can return
|
|
|
|
// transactions in a profit-maximising sorted order, while supporting removing
|
|
|
|
// entire batches of transactions for non-executable accounts.
|
|
|
|
type TransactionsByPriceAndNonce struct {
|
|
|
|
txs map[common.Address]Transactions // Per account nonce-sorted list of transactions
|
|
|
|
heads TxByPrice // Next transaction for each unique account (price heap)
|
|
|
|
}
|
|
|
|
|
2016-11-03 07:54:33 -07:00
|
|
|
type TransactionsByPriorityAndNonce struct {
|
|
|
|
txs map[common.Address]Transactions
|
|
|
|
heads TxByPriority
|
|
|
|
}
|
|
|
|
|
|
|
|
// TxByPriority implements both sort and the heap interface, making it useful
|
|
|
|
// for all at once sorting as well as individual adding and removing elements.
|
|
|
|
//
|
|
|
|
// It will prioritise transaction to the voting contract.
|
|
|
|
type TxByPriority Transactions
|
|
|
|
|
|
|
|
func (s TxByPriority) Len() int { return len(s) }
|
|
|
|
func (s TxByPriority) Less(i, j int) bool {
|
|
|
|
var (
|
|
|
|
iRecipient = s[i].data.Recipient
|
|
|
|
jRecipient = s[j].data.Recipient
|
|
|
|
)
|
|
|
|
|
|
|
|
// in case iReceipt is towards the voting contract and jRecipient is not towards the voting contract
|
|
|
|
// iReceipt is "smaller".
|
|
|
|
return iRecipient != nil && *iRecipient == params.QuorumVotingContractAddr && (jRecipient == nil || *jRecipient != params.QuorumVotingContractAddr)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s TxByPriority) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
|
|
|
|
func (s *TxByPriority) Push(x interface{}) {
|
|
|
|
*s = append(*s, x.(*Transaction))
|
|
|
|
}
|
|
|
|
func (s *TxByPriority) Pop() interface{} {
|
|
|
|
old := *s
|
|
|
|
n := len(old)
|
|
|
|
x := old[n-1]
|
|
|
|
*s = old[0 : n-1]
|
|
|
|
return x
|
|
|
|
}
|
|
|
|
|
2016-08-09 04:54:36 -07:00
|
|
|
// NewTransactionsByPriceAndNonce creates a transaction set that can retrieve
|
|
|
|
// price sorted transactions in a nonce-honouring way.
|
2016-01-22 02:05:17 -08:00
|
|
|
//
|
2016-08-09 04:54:36 -07:00
|
|
|
// Note, the input map is reowned so the caller should not interact any more with
|
2016-10-09 08:10:48 -07:00
|
|
|
// if after providing it to the constructor.
|
2016-08-09 04:54:36 -07:00
|
|
|
func NewTransactionsByPriceAndNonce(txs map[common.Address]Transactions) *TransactionsByPriceAndNonce {
|
2016-01-22 02:05:17 -08:00
|
|
|
// Initialize a price based heap with the head transactions
|
2016-08-09 04:54:36 -07:00
|
|
|
heads := make(TxByPrice, 0, len(txs))
|
2016-07-01 08:59:55 -07:00
|
|
|
for acc, accTxs := range txs {
|
2016-08-09 04:54:36 -07:00
|
|
|
heads = append(heads, accTxs[0])
|
2016-07-01 08:59:55 -07:00
|
|
|
txs[acc] = accTxs[1:]
|
2016-01-22 02:05:17 -08:00
|
|
|
}
|
2016-08-09 04:54:36 -07:00
|
|
|
heap.Init(&heads)
|
|
|
|
|
|
|
|
// Assemble and return the transaction set
|
|
|
|
return &TransactionsByPriceAndNonce{
|
|
|
|
txs: txs,
|
|
|
|
heads: heads,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-03 07:54:33 -07:00
|
|
|
// NewTransactionsByPriorityAndNonce creates a transaction set that can retrieve
|
|
|
|
// vote tx sorted transactions in a nonce-honouring way.
|
|
|
|
//
|
|
|
|
// Note, the input map is reowned so the caller should not interact any more with
|
|
|
|
// it after providing it to the constructor.
|
|
|
|
func NewTransactionsByPriorityAndNonce(txs map[common.Address]Transactions) *TransactionsByPriorityAndNonce {
|
|
|
|
heads := make(TxByPriority, 0, len(txs))
|
|
|
|
for acc, accTxs := range txs {
|
|
|
|
heads = append(heads, accTxs[0])
|
|
|
|
txs[acc] = accTxs[1:]
|
|
|
|
}
|
|
|
|
heap.Init(&heads)
|
|
|
|
|
|
|
|
return &TransactionsByPriorityAndNonce{
|
|
|
|
txs: txs,
|
|
|
|
heads: heads,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *TransactionsByPriorityAndNonce) Peek() *Transaction {
|
|
|
|
if len(t.heads) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return t.heads[0]
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *TransactionsByPriorityAndNonce) Shift() {
|
|
|
|
acc, _ := t.heads[0].From()
|
|
|
|
if txs, ok := t.txs[acc]; ok && len(txs) > 0 {
|
|
|
|
t.heads[0], t.txs[acc] = txs[0], txs[1:]
|
|
|
|
heap.Fix(&t.heads, 0)
|
|
|
|
} else {
|
|
|
|
heap.Pop(&t.heads)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *TransactionsByPriorityAndNonce) Pop() {
|
|
|
|
heap.Pop(&t.heads)
|
|
|
|
}
|
|
|
|
|
2016-08-09 04:54:36 -07:00
|
|
|
// Peek returns the next transaction by price.
|
|
|
|
func (t *TransactionsByPriceAndNonce) Peek() *Transaction {
|
|
|
|
if len(t.heads) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return t.heads[0]
|
|
|
|
}
|
|
|
|
|
|
|
|
// Shift replaces the current best head with the next one from the same account.
|
|
|
|
func (t *TransactionsByPriceAndNonce) Shift() {
|
|
|
|
acc, _ := t.heads[0].From() // we only sort valid txs so this cannot fail
|
|
|
|
|
|
|
|
if txs, ok := t.txs[acc]; ok && len(txs) > 0 {
|
|
|
|
t.heads[0], t.txs[acc] = txs[0], txs[1:]
|
|
|
|
heap.Fix(&t.heads, 0)
|
|
|
|
} else {
|
|
|
|
heap.Pop(&t.heads)
|
2015-08-03 12:57:09 -07:00
|
|
|
}
|
2016-08-09 04:54:36 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Pop removes the best transaction, *not* replacing it with the next one from
|
|
|
|
// the same account. This should be used when a transaction cannot be executed
|
|
|
|
// and hence all subsequent ones should be discarded from the same account.
|
|
|
|
func (t *TransactionsByPriceAndNonce) Pop() {
|
|
|
|
heap.Pop(&t.heads)
|
2015-08-03 12:57:09 -07:00
|
|
|
}
|