transaction.go 13.3 KB
Newer Older
F
Felix Lange 已提交
1
// Copyright 2014 The go-ethereum Authors
2
// This file is part of the go-ethereum library.
F
Felix Lange 已提交
3
//
4
// The go-ethereum library is free software: you can redistribute it and/or modify
F
Felix Lange 已提交
5 6 7 8
// 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.
//
9
// The go-ethereum library is distributed in the hope that it will be useful,
F
Felix Lange 已提交
10
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
F
Felix Lange 已提交
12 13 14
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
15
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
F
Felix Lange 已提交
16

17
package types
O
obscuren 已提交
18 19

import (
20
	"container/heap"
21
	"errors"
22
	"io"
23
	"math/big"
24
	"sync/atomic"
25
	"time"
26

27
	"github.com/ethereum/go-ethereum/common"
28
	"github.com/ethereum/go-ethereum/common/hexutil"
O
obscuren 已提交
29
	"github.com/ethereum/go-ethereum/crypto"
O
obscuren 已提交
30
	"github.com/ethereum/go-ethereum/rlp"
O
obscuren 已提交
31 32
)

33
//go:generate gencodec -type txdata -field-override txdataMarshaling -out gen_tx_json.go
34 35

var (
36
	ErrInvalidSig = errors.New("invalid transaction v, r, s values")
37
)
38

O
obscuren 已提交
39
type Transaction struct {
40 41
	data txdata    // Consensus contents of a transaction
	time time.Time // Time first seen locally (spam avoidance)
42

43 44 45 46
	// caches
	hash atomic.Value
	size atomic.Value
	from atomic.Value
47 48 49
}

type txdata struct {
50 51
	AccountNonce uint64          `json:"nonce"    gencodec:"required"`
	Price        *big.Int        `json:"gasPrice" gencodec:"required"`
52
	GasLimit     uint64          `json:"gas"      gencodec:"required"`
53 54 55
	Recipient    *common.Address `json:"to"       rlp:"nil"` // nil means contract creation
	Amount       *big.Int        `json:"value"    gencodec:"required"`
	Payload      []byte          `json:"input"    gencodec:"required"`
56 57

	// Signature values
58 59 60
	V *big.Int `json:"v" gencodec:"required"`
	R *big.Int `json:"r" gencodec:"required"`
	S *big.Int `json:"s" gencodec:"required"`
61 62

	// This is only used when marshaling to JSON.
63
	Hash *common.Hash `json:"hash" rlp:"-"`
64 65
}

66 67 68
type txdataMarshaling struct {
	AccountNonce hexutil.Uint64
	Price        *hexutil.Big
69
	GasLimit     hexutil.Uint64
70 71 72 73 74
	Amount       *hexutil.Big
	Payload      hexutil.Bytes
	V            *hexutil.Big
	R            *hexutil.Big
	S            *hexutil.Big
75 76
}

77
func NewTransaction(nonce uint64, to common.Address, amount *big.Int, gasLimit uint64, gasPrice *big.Int, data []byte) *Transaction {
J
Jeffrey Wilcke 已提交
78 79 80
	return newTransaction(nonce, &to, amount, gasLimit, gasPrice, data)
}

81
func NewContractCreation(nonce uint64, amount *big.Int, gasLimit uint64, gasPrice *big.Int, data []byte) *Transaction {
J
Jeffrey Wilcke 已提交
82
	return newTransaction(nonce, nil, amount, gasLimit, gasPrice, data)
83 84
}

85
func newTransaction(nonce uint64, to *common.Address, amount *big.Int, gasLimit uint64, gasPrice *big.Int, data []byte) *Transaction {
86 87 88 89 90
	if len(data) > 0 {
		data = common.CopyBytes(data)
	}
	d := txdata{
		AccountNonce: nonce,
J
Jeffrey Wilcke 已提交
91
		Recipient:    to,
92 93
		Payload:      data,
		Amount:       new(big.Int),
94
		GasLimit:     gasLimit,
95
		Price:        new(big.Int),
J
Jeffrey Wilcke 已提交
96
		V:            new(big.Int),
97 98
		R:            new(big.Int),
		S:            new(big.Int),
99
	}
100 101 102 103 104 105
	if amount != nil {
		d.Amount.Set(amount)
	}
	if gasPrice != nil {
		d.Price.Set(gasPrice)
	}
106 107 108 109
	return &Transaction{
		data: d,
		time: time.Now(),
	}
110 111
}

J
Jeffrey Wilcke 已提交
112 113 114 115 116
// ChainId returns which chain id this transaction was signed for (if at all)
func (tx *Transaction) ChainId() *big.Int {
	return deriveChainId(tx.data.V)
}

117
// Protected returns whether the transaction is protected from replay protection.
J
Jeffrey Wilcke 已提交
118 119 120 121 122 123 124 125 126
func (tx *Transaction) Protected() bool {
	return isProtectedV(tx.data.V)
}

func isProtectedV(V *big.Int) bool {
	if V.BitLen() <= 8 {
		v := V.Uint64()
		return v != 27 && v != 28
	}
127
	// anything not 27 or 28 is considered protected
J
Jeffrey Wilcke 已提交
128 129 130
	return true
}

A
Airead 已提交
131
// EncodeRLP implements rlp.Encoder
132 133
func (tx *Transaction) EncodeRLP(w io.Writer) error {
	return rlp.Encode(w, &tx.data)
134 135
}

136
// DecodeRLP implements rlp.Decoder
137
func (tx *Transaction) DecodeRLP(s *rlp.Stream) error {
138 139 140 141
	_, size, _ := s.Kind()
	err := s.Decode(&tx.data)
	if err == nil {
		tx.size.Store(common.StorageSize(rlp.ListSize(size)))
142
		tx.time = time.Now()
143 144
	}
	return err
145 146
}

K
Kurkó Mihály 已提交
147
// MarshalJSON encodes the web3 RPC transaction format.
148
func (tx *Transaction) MarshalJSON() ([]byte, error) {
J
Jeffrey Wilcke 已提交
149
	hash := tx.Hash()
150 151 152
	data := tx.data
	data.Hash = &hash
	return data.MarshalJSON()
153 154
}

155 156
// UnmarshalJSON decodes the web3 RPC transaction format.
func (tx *Transaction) UnmarshalJSON(input []byte) error {
157 158
	var dec txdata
	if err := dec.UnmarshalJSON(input); err != nil {
159 160
		return err
	}
161 162 163 164 165 166 167 168 169 170 171 172
	withSignature := dec.V.Sign() != 0 || dec.R.Sign() != 0 || dec.S.Sign() != 0
	if withSignature {
		var V byte
		if isProtectedV(dec.V) {
			chainID := deriveChainId(dec.V).Uint64()
			V = byte(dec.V.Uint64() - 35 - 2*chainID)
		} else {
			V = byte(dec.V.Uint64() - 27)
		}
		if !crypto.ValidateSignatureValues(V, dec.R, dec.S, false) {
			return ErrInvalidSig
		}
173
	}
174 175 176 177
	*tx = Transaction{
		data: dec,
		time: time.Now(),
	}
178 179 180
	return nil
}

181
func (tx *Transaction) Data() []byte       { return common.CopyBytes(tx.data.Payload) }
182
func (tx *Transaction) Gas() uint64        { return tx.data.GasLimit }
183
func (tx *Transaction) GasPrice() *big.Int { return new(big.Int).Set(tx.data.Price) }
184 185 186 187 188 189 190 191 192
func (tx *Transaction) GasPriceCmp(other *Transaction) int {
	return tx.data.Price.Cmp(other.data.Price)
}
func (tx *Transaction) GasPriceIntCmp(other *big.Int) int {
	return tx.data.Price.Cmp(other)
}
func (tx *Transaction) Value() *big.Int  { return new(big.Int).Set(tx.data.Amount) }
func (tx *Transaction) Nonce() uint64    { return tx.data.AccountNonce }
func (tx *Transaction) CheckNonce() bool { return true }
193

194 195
// To returns the recipient address of the transaction.
// It returns nil if the transaction is a contract creation.
196 197 198 199
func (tx *Transaction) To() *common.Address {
	if tx.data.Recipient == nil {
		return nil
	}
K
Kurkó Mihály 已提交
200 201
	to := *tx.data.Recipient
	return &to
202 203
}

204 205
// Hash hashes the RLP encoding of tx.
// It uniquely identifies the transaction.
206
func (tx *Transaction) Hash() common.Hash {
207 208 209
	if hash := tx.hash.Load(); hash != nil {
		return hash.(common.Hash)
	}
210 211 212 213 214
	v := rlpHash(tx)
	tx.hash.Store(v)
	return v
}

215
// Size returns the true RLP encoded storage size of the transaction, either by
216
// encoding and returning it, or returning a previously cached value.
217
func (tx *Transaction) Size() common.StorageSize {
218 219 220 221 222 223 224
	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)
225 226
}

J
Jeffrey Wilcke 已提交
227 228 229 230 231 232 233
// AsMessage returns the transaction as a core.Message.
//
// AsMessage requires a signer to derive the sender.
//
// XXX Rename message to something less arbitrary?
func (tx *Transaction) AsMessage(s Signer) (Message, error) {
	msg := Message{
234
		nonce:      tx.data.AccountNonce,
235 236
		gasLimit:   tx.data.GasLimit,
		gasPrice:   new(big.Int).Set(tx.data.Price),
237 238 239 240
		to:         tx.data.Recipient,
		amount:     tx.data.Amount,
		data:       tx.data.Payload,
		checkNonce: true,
241
	}
O
obscuren 已提交
242

J
Jeffrey Wilcke 已提交
243 244 245 246
	var err error
	msg.from, err = Sender(s, tx)
	return msg, err
}
O
obscuren 已提交
247

248
// WithSignature returns a new transaction with the given signature.
249
// This signature needs to be in the [R || S || V] format where V is 0 or 1.
J
Jeffrey Wilcke 已提交
250
func (tx *Transaction) WithSignature(signer Signer, sig []byte) (*Transaction, error) {
251 252 253 254
	r, s, v, err := signer.SignatureValues(tx, sig)
	if err != nil {
		return nil, err
	}
255 256 257 258
	cpy := &Transaction{
		data: tx.data,
		time: tx.time,
	}
259 260
	cpy.data.R, cpy.data.S, cpy.data.V = r, s, v
	return cpy, nil
261 262
}

J
Jeffrey Wilcke 已提交
263 264
// Cost returns amount + gasprice * gaslimit.
func (tx *Transaction) Cost() *big.Int {
265
	total := new(big.Int).Mul(tx.data.Price, new(big.Int).SetUint64(tx.data.GasLimit))
J
Jeffrey Wilcke 已提交
266 267 268 269
	total.Add(total, tx.data.Amount)
	return total
}

270 271 272
// RawSignatureValues returns the V, R, S signature values of the transaction.
// The return values should not be modified by the caller.
func (tx *Transaction) RawSignatureValues() (v, r, s *big.Int) {
J
Jeffrey Wilcke 已提交
273
	return tx.data.V, tx.data.R, tx.data.S
O
obscuren 已提交
274 275
}

K
Kurkó Mihály 已提交
276
// Transactions is a Transaction slice type for basic sorting.
O
obscuren 已提交
277 278
type Transactions []*Transaction

K
Kurkó Mihály 已提交
279
// Len returns the length of s.
280 281
func (s Transactions) Len() int { return len(s) }

K
Kurkó Mihály 已提交
282
// Swap swaps the i'th and the j'th element in s.
283 284
func (s Transactions) Swap(i, j int) { s[i], s[j] = s[j], s[i] }

K
Kurkó Mihály 已提交
285
// GetRlp implements Rlpable and returns the i'th element of s in rlp.
286 287 288 289
func (s Transactions) GetRlp(i int) []byte {
	enc, _ := rlp.EncodeToBytes(s[i])
	return enc
}
O
obscuren 已提交
290

291 292 293
// TxDifference returns a new set which is the difference between a and b.
func TxDifference(a, b Transactions) Transactions {
	keep := make(Transactions, 0, len(a))
294 295 296 297 298 299 300 301 302 303 304 305 306 307 308

	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
}

309 310 311 312
// 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
O
obscuren 已提交
313

314 315 316 317
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] }

318
// TxByPriceAndTime implements both the sort and the heap interface, making it useful
319
// for all at once sorting as well as individually adding and removing elements.
320 321 322 323 324 325 326 327 328
type TxByPriceAndTime Transactions

func (s TxByPriceAndTime) Len() int { return len(s) }
func (s TxByPriceAndTime) Less(i, j int) bool {
	// If the prices are equal, use the time the transaction was first seen for
	// deterministic sorting
	cmp := s[i].data.Price.Cmp(s[j].data.Price)
	if cmp == 0 {
		return s[i].time.Before(s[j].time)
329
	}
330
	return cmp > 0
331
}
332
func (s TxByPriceAndTime) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
333

334
func (s *TxByPriceAndTime) Push(x interface{}) {
335
	*s = append(*s, x.(*Transaction))
336 337
}

338
func (s *TxByPriceAndTime) Pop() interface{} {
339 340 341 342 343 344
	old := *s
	n := len(old)
	x := old[n-1]
	*s = old[0 : n-1]
	return x
}
345

346
// TransactionsByPriceAndNonce represents a set of transactions that can return
K
Kurkó Mihály 已提交
347
// transactions in a profit-maximizing sorted order, while supporting removing
348 349
// entire batches of transactions for non-executable accounts.
type TransactionsByPriceAndNonce struct {
350
	txs    map[common.Address]Transactions // Per account nonce-sorted list of transactions
351
	heads  TxByPriceAndTime                // Next transaction for each unique account (price heap)
352
	signer Signer                          // Signer for the set of transactions
353 354 355 356
}

// NewTransactionsByPriceAndNonce creates a transaction set that can retrieve
// price sorted transactions in a nonce-honouring way.
357
//
358
// Note, the input map is reowned so the caller should not interact any more with
359 360
// if after providing it to the constructor.
func NewTransactionsByPriceAndNonce(signer Signer, txs map[common.Address]Transactions) *TransactionsByPriceAndNonce {
361
	// Initialize a price and received time based heap with the head transactions
362
	heads := make(TxByPriceAndTime, 0, len(txs))
363
	for from, accTxs := range txs {
364
		heads = append(heads, accTxs[0])
365 366
		// Ensure the sender address is from the signer
		acc, _ := Sender(signer, accTxs[0])
367
		txs[acc] = accTxs[1:]
368 369 370
		if from != acc {
			delete(txs, from)
		}
371
	}
372 373 374 375
	heap.Init(&heads)

	// Assemble and return the transaction set
	return &TransactionsByPriceAndNonce{
376 377 378
		txs:    txs,
		heads:  heads,
		signer: signer,
379 380 381 382 383 384 385 386 387 388 389 390 391
	}
}

// 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() {
392
	acc, _ := Sender(t.signer, t.heads[0])
393 394 395 396 397
	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)
398
	}
399 400 401 402 403 404 405
}

// 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)
406
}
J
Jeffrey Wilcke 已提交
407 408 409 410 411

// Message is a fully derived transaction and implements core.Message
//
// NOTE: In a future PR this will be removed.
type Message struct {
412 413 414 415 416 417 418 419
	to         *common.Address
	from       common.Address
	nonce      uint64
	amount     *big.Int
	gasLimit   uint64
	gasPrice   *big.Int
	data       []byte
	checkNonce bool
J
Jeffrey Wilcke 已提交
420 421
}

422
func NewMessage(from common.Address, to *common.Address, nonce uint64, amount *big.Int, gasLimit uint64, gasPrice *big.Int, data []byte, checkNonce bool) Message {
J
Jeffrey Wilcke 已提交
423
	return Message{
424 425 426 427 428
		from:       from,
		to:         to,
		nonce:      nonce,
		amount:     amount,
		gasLimit:   gasLimit,
429
		gasPrice:   gasPrice,
430 431
		data:       data,
		checkNonce: checkNonce,
J
Jeffrey Wilcke 已提交
432 433 434 435 436
	}
}

func (m Message) From() common.Address { return m.from }
func (m Message) To() *common.Address  { return m.to }
437
func (m Message) GasPrice() *big.Int   { return m.gasPrice }
J
Jeffrey Wilcke 已提交
438
func (m Message) Value() *big.Int      { return m.amount }
439
func (m Message) Gas() uint64          { return m.gasLimit }
J
Jeffrey Wilcke 已提交
440 441
func (m Message) Nonce() uint64        { return m.nonce }
func (m Message) Data() []byte         { return m.data }
442
func (m Message) CheckNonce() bool     { return m.checkNonce }