diff options
author | Determinant <[email protected]> | 2020-09-15 23:55:34 -0400 |
---|---|---|
committer | Determinant <[email protected]> | 2020-09-15 23:55:34 -0400 |
commit | 78745551c077bf54151202138c2629f288769561 (patch) | |
tree | 2b628e99fd110617089778fa91235ecd2888f4ef /core/types | |
parent | 7d1388c743b4ec8f4a86bea95bfada785dee83f7 (diff) |
WIP: geth-tavum
Diffstat (limited to 'core/types')
-rw-r--r-- | core/types/bloom9.go | 4 | ||||
-rw-r--r-- | core/types/derive_sha.go | 21 | ||||
-rw-r--r-- | core/types/gen_header_json.go | 4 | ||||
-rw-r--r-- | core/types/gen_log_json.go | 22 | ||||
-rw-r--r-- | core/types/gen_receipt_json.go | 4 | ||||
-rw-r--r-- | core/types/gen_tx_json.go | 4 | ||||
-rw-r--r-- | core/types/log.go | 10 | ||||
-rw-r--r-- | core/types/receipt.go | 10 | ||||
-rw-r--r-- | core/types/transaction.go | 75 | ||||
-rw-r--r-- | core/types/transaction_signing.go | 6 |
10 files changed, 94 insertions, 66 deletions
diff --git a/core/types/bloom9.go b/core/types/bloom9.go index 76923c4..d045c9e 100644 --- a/core/types/bloom9.go +++ b/core/types/bloom9.go @@ -20,8 +20,8 @@ import ( "fmt" "math/big" - "github.com/ava-labs/go-ethereum/common/hexutil" - "github.com/ava-labs/go-ethereum/crypto" + "github.com/ethereum/go-ethereum/common/hexutil" + "github.com/ethereum/go-ethereum/crypto" ) type bytesBacked interface { diff --git a/core/types/derive_sha.go b/core/types/derive_sha.go index 8234191..7d40c7f 100644 --- a/core/types/derive_sha.go +++ b/core/types/derive_sha.go @@ -19,23 +19,30 @@ package types import ( "bytes" - "github.com/ava-labs/go-ethereum/common" - "github.com/ava-labs/go-ethereum/rlp" - "github.com/ava-labs/go-ethereum/trie" + "github.com/ethereum/go-ethereum/common" + "github.com/ethereum/go-ethereum/rlp" ) +// DerivableList is the interface which can derive the hash. type DerivableList interface { Len() int GetRlp(i int) []byte } -func DeriveSha(list DerivableList) common.Hash { +// Hasher is the tool used to calculate the hash of derivable list. +type Hasher interface { + Reset() + Update([]byte, []byte) + Hash() common.Hash +} + +func DeriveSha(list DerivableList, hasher Hasher) common.Hash { + hasher.Reset() keybuf := new(bytes.Buffer) - trie := new(trie.Trie) for i := 0; i < list.Len(); i++ { keybuf.Reset() rlp.Encode(keybuf, uint(i)) - trie.Update(keybuf.Bytes(), list.GetRlp(i)) + hasher.Update(keybuf.Bytes(), list.GetRlp(i)) } - return trie.Hash() + return hasher.Hash() } diff --git a/core/types/gen_header_json.go b/core/types/gen_header_json.go index 48b35db..4212b8d 100644 --- a/core/types/gen_header_json.go +++ b/core/types/gen_header_json.go @@ -7,8 +7,8 @@ import ( "errors" "math/big" - "github.com/ava-labs/go-ethereum/common" - "github.com/ava-labs/go-ethereum/common/hexutil" + "github.com/ethereum/go-ethereum/common" + "github.com/ethereum/go-ethereum/common/hexutil" ) var _ = (*headerMarshaling)(nil) diff --git a/core/types/gen_log_json.go b/core/types/gen_log_json.go index 0e48863..90e1c14 100644 --- a/core/types/gen_log_json.go +++ b/core/types/gen_log_json.go @@ -6,8 +6,8 @@ import ( "encoding/json" "errors" - "github.com/ava-labs/go-ethereum/common" - "github.com/ava-labs/go-ethereum/common/hexutil" + "github.com/ethereum/go-ethereum/common" + "github.com/ethereum/go-ethereum/common/hexutil" ) var _ = (*logMarshaling)(nil) @@ -20,9 +20,9 @@ func (l Log) MarshalJSON() ([]byte, error) { Data hexutil.Bytes `json:"data" gencodec:"required"` BlockNumber hexutil.Uint64 `json:"blockNumber"` TxHash common.Hash `json:"transactionHash" gencodec:"required"` - TxIndex hexutil.Uint `json:"transactionIndex" gencodec:"required"` + TxIndex hexutil.Uint `json:"transactionIndex"` BlockHash common.Hash `json:"blockHash"` - Index hexutil.Uint `json:"logIndex" gencodec:"required"` + Index hexutil.Uint `json:"logIndex"` Removed bool `json:"removed"` } var enc Log @@ -46,9 +46,9 @@ func (l *Log) UnmarshalJSON(input []byte) error { Data *hexutil.Bytes `json:"data" gencodec:"required"` BlockNumber *hexutil.Uint64 `json:"blockNumber"` TxHash *common.Hash `json:"transactionHash" gencodec:"required"` - TxIndex *hexutil.Uint `json:"transactionIndex" gencodec:"required"` + TxIndex *hexutil.Uint `json:"transactionIndex"` BlockHash *common.Hash `json:"blockHash"` - Index *hexutil.Uint `json:"logIndex" gencodec:"required"` + Index *hexutil.Uint `json:"logIndex"` Removed *bool `json:"removed"` } var dec Log @@ -74,17 +74,15 @@ func (l *Log) UnmarshalJSON(input []byte) error { return errors.New("missing required field 'transactionHash' for Log") } l.TxHash = *dec.TxHash - if dec.TxIndex == nil { - return errors.New("missing required field 'transactionIndex' for Log") + if dec.TxIndex != nil { + l.TxIndex = uint(*dec.TxIndex) } - l.TxIndex = uint(*dec.TxIndex) if dec.BlockHash != nil { l.BlockHash = *dec.BlockHash } - if dec.Index == nil { - return errors.New("missing required field 'logIndex' for Log") + if dec.Index != nil { + l.Index = uint(*dec.Index) } - l.Index = uint(*dec.Index) if dec.Removed != nil { l.Removed = *dec.Removed } diff --git a/core/types/gen_receipt_json.go b/core/types/gen_receipt_json.go index f7cde61..790ed65 100644 --- a/core/types/gen_receipt_json.go +++ b/core/types/gen_receipt_json.go @@ -7,8 +7,8 @@ import ( "errors" "math/big" - "github.com/ava-labs/go-ethereum/common" - "github.com/ava-labs/go-ethereum/common/hexutil" + "github.com/ethereum/go-ethereum/common" + "github.com/ethereum/go-ethereum/common/hexutil" ) var _ = (*receiptMarshaling)(nil) diff --git a/core/types/gen_tx_json.go b/core/types/gen_tx_json.go index 0410632..e676058 100644 --- a/core/types/gen_tx_json.go +++ b/core/types/gen_tx_json.go @@ -7,8 +7,8 @@ import ( "errors" "math/big" - "github.com/ava-labs/go-ethereum/common" - "github.com/ava-labs/go-ethereum/common/hexutil" + "github.com/ethereum/go-ethereum/common" + "github.com/ethereum/go-ethereum/common/hexutil" ) var _ = (*txdataMarshaling)(nil) diff --git a/core/types/log.go b/core/types/log.go index 0b6184f..88274e3 100644 --- a/core/types/log.go +++ b/core/types/log.go @@ -19,9 +19,9 @@ package types import ( "io" - "github.com/ava-labs/go-ethereum/common" - "github.com/ava-labs/go-ethereum/common/hexutil" - "github.com/ava-labs/go-ethereum/rlp" + "github.com/ethereum/go-ethereum/common" + "github.com/ethereum/go-ethereum/common/hexutil" + "github.com/ethereum/go-ethereum/rlp" ) //go:generate gencodec -type Log -field-override logMarshaling -out gen_log_json.go @@ -44,11 +44,11 @@ type Log struct { // hash of the transaction TxHash common.Hash `json:"transactionHash" gencodec:"required"` // index of the transaction in the block - TxIndex uint `json:"transactionIndex" gencodec:"required"` + TxIndex uint `json:"transactionIndex"` // hash of the block in which the transaction was included BlockHash common.Hash `json:"blockHash"` // index of the log in the block - Index uint `json:"logIndex" gencodec:"required"` + Index uint `json:"logIndex"` // The Removed field is true if this log was reverted due to a chain reorganisation. // You must pay attention to this field if you receive logs through a filter query. diff --git a/core/types/receipt.go b/core/types/receipt.go index 46538e8..4d57f59 100644 --- a/core/types/receipt.go +++ b/core/types/receipt.go @@ -24,11 +24,11 @@ import ( "math/big" "unsafe" - "github.com/ava-labs/coreth/params" - "github.com/ava-labs/go-ethereum/common" - "github.com/ava-labs/go-ethereum/common/hexutil" - "github.com/ava-labs/go-ethereum/crypto" - "github.com/ava-labs/go-ethereum/rlp" + "github.com/ethereum/coreth/params" + "github.com/ethereum/go-ethereum/common" + "github.com/ethereum/go-ethereum/common/hexutil" + "github.com/ethereum/go-ethereum/crypto" + "github.com/ethereum/go-ethereum/rlp" ) //go:generate gencodec -type Receipt -field-override receiptMarshaling -out gen_receipt_json.go diff --git a/core/types/transaction.go b/core/types/transaction.go index d9ada38..ec19744 100644 --- a/core/types/transaction.go +++ b/core/types/transaction.go @@ -22,11 +22,12 @@ import ( "io" "math/big" "sync/atomic" + "time" - "github.com/ava-labs/go-ethereum/common" - "github.com/ava-labs/go-ethereum/common/hexutil" - "github.com/ava-labs/go-ethereum/crypto" - "github.com/ava-labs/go-ethereum/rlp" + "github.com/ethereum/go-ethereum/common" + "github.com/ethereum/go-ethereum/common/hexutil" + "github.com/ethereum/go-ethereum/crypto" + "github.com/ethereum/go-ethereum/rlp" ) //go:generate gencodec -type txdata -field-override txdataMarshaling -out gen_tx_json.go @@ -36,7 +37,9 @@ var ( ) type Transaction struct { - data txdata + data txdata // Consensus contents of a transaction + time time.Time // Time first seen locally (spam avoidance) + // caches hash atomic.Value size atomic.Value @@ -100,8 +103,10 @@ func newTransaction(nonce uint64, to *common.Address, amount *big.Int, gasLimit if gasPrice != nil { d.Price.Set(gasPrice) } - - return &Transaction{data: d} + return &Transaction{ + data: d, + time: time.Now(), + } } // ChainId returns which chain id this transaction was signed for (if at all) @@ -134,8 +139,8 @@ func (tx *Transaction) DecodeRLP(s *rlp.Stream) error { err := s.Decode(&tx.data) if err == nil { tx.size.Store(common.StorageSize(rlp.ListSize(size))) + tx.time = time.Now() } - return err } @@ -153,7 +158,6 @@ func (tx *Transaction) UnmarshalJSON(input []byte) error { if err := dec.UnmarshalJSON(input); err != nil { return err } - withSignature := dec.V.Sign() != 0 || dec.R.Sign() != 0 || dec.S.Sign() != 0 if withSignature { var V byte @@ -167,17 +171,25 @@ func (tx *Transaction) UnmarshalJSON(input []byte) error { return ErrInvalidSig } } - - *tx = Transaction{data: dec} + *tx = Transaction{ + data: dec, + time: time.Now(), + } return nil } func (tx *Transaction) Data() []byte { return common.CopyBytes(tx.data.Payload) } func (tx *Transaction) Gas() uint64 { return 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 } -func (tx *Transaction) CheckNonce() bool { return true } +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 } // To returns the recipient address of the transaction. // It returns nil if the transaction is a contract creation. @@ -240,7 +252,10 @@ func (tx *Transaction) WithSignature(signer Signer, sig []byte) (*Transaction, e if err != nil { return nil, err } - cpy := &Transaction{data: tx.data} + cpy := &Transaction{ + data: tx.data, + time: tx.time, + } cpy.data.R, cpy.data.S, cpy.data.V = r, s, v return cpy, nil } @@ -300,19 +315,27 @@ 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 +// TxByPriceAndTime 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 - -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] } +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) + } + return cmp > 0 +} +func (s TxByPriceAndTime) Swap(i, j int) { s[i], s[j] = s[j], s[i] } -func (s *TxByPrice) Push(x interface{}) { +func (s *TxByPriceAndTime) Push(x interface{}) { *s = append(*s, x.(*Transaction)) } -func (s *TxByPrice) Pop() interface{} { +func (s *TxByPriceAndTime) Pop() interface{} { old := *s n := len(old) x := old[n-1] @@ -325,7 +348,7 @@ func (s *TxByPrice) Pop() interface{} { // 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) + heads TxByPriceAndTime // Next transaction for each unique account (price heap) signer Signer // Signer for the set of transactions } @@ -335,8 +358,8 @@ type TransactionsByPriceAndNonce struct { // Note, the input map is reowned so the caller should not interact any more with // if after providing it to the constructor. func NewTransactionsByPriceAndNonce(signer Signer, txs map[common.Address]Transactions) *TransactionsByPriceAndNonce { - // Initialize a price based heap with the head transactions - heads := make(TxByPrice, 0, len(txs)) + // Initialize a price and received time based heap with the head transactions + heads := make(TxByPriceAndTime, 0, len(txs)) for from, accTxs := range txs { heads = append(heads, accTxs[0]) // Ensure the sender address is from the signer diff --git a/core/types/transaction_signing.go b/core/types/transaction_signing.go index cb556eb..ecc65d5 100644 --- a/core/types/transaction_signing.go +++ b/core/types/transaction_signing.go @@ -22,9 +22,9 @@ import ( "fmt" "math/big" - "github.com/ava-labs/coreth/params" - "github.com/ava-labs/go-ethereum/common" - "github.com/ava-labs/go-ethereum/crypto" + "github.com/ethereum/coreth/params" + "github.com/ethereum/go-ethereum/common" + "github.com/ethereum/go-ethereum/crypto" ) var ( |