diff options
author | obscuren <geffobscura@gmail.com> | 2014-05-28 18:06:09 +0800 |
---|---|---|
committer | obscuren <geffobscura@gmail.com> | 2014-05-28 18:06:09 +0800 |
commit | 9988b1a04710e03ce7ed4b23393e2e90f06889f9 (patch) | |
tree | 82551f1c24a74c960ab65d6dcd2da492498f08fe | |
parent | 73761f7af64432b6946934c3b1db646d8e99ef07 (diff) | |
download | dexon-9988b1a04710e03ce7ed4b23393e2e90f06889f9.tar dexon-9988b1a04710e03ce7ed4b23393e2e90f06889f9.tar.gz dexon-9988b1a04710e03ce7ed4b23393e2e90f06889f9.tar.bz2 dexon-9988b1a04710e03ce7ed4b23393e2e90f06889f9.tar.lz dexon-9988b1a04710e03ce7ed4b23393e2e90f06889f9.tar.xz dexon-9988b1a04710e03ce7ed4b23393e2e90f06889f9.tar.zst dexon-9988b1a04710e03ce7ed4b23393e2e90f06889f9.zip |
Sort transactions based on the nonce
* Added a transaction sorter
-rw-r--r-- | ethchain/transaction.go | 28 | ||||
-rw-r--r-- | ethminer/miner.go | 5 |
2 files changed, 16 insertions, 17 deletions
diff --git a/ethchain/transaction.go b/ethchain/transaction.go index 2c5615f99..2cb946b3b 100644 --- a/ethchain/transaction.go +++ b/ethchain/transaction.go @@ -147,22 +147,6 @@ func (tx *Transaction) RlpValueDecode(decoder *ethutil.Value) { if len(tx.Recipient) == 0 { tx.contractCreation = true } - - /* - // If the list is of length 10 it's a contract creation tx - if decoder.Len() == 10 { - tx.contractCreation = true - tx.Init = decoder.Get(6).Bytes() - - tx.v = byte(decoder.Get(7).Uint()) - tx.r = decoder.Get(8).Bytes() - tx.s = decoder.Get(9).Bytes() - } else { - tx.v = byte(decoder.Get(6).Uint()) - tx.r = decoder.Get(7).Bytes() - tx.s = decoder.Get(8).Bytes() - } - */ } func (tx *Transaction) String() string { @@ -228,3 +212,15 @@ func (self *Receipt) String() string { self.PostState, self.CumulativeGasUsed) } + +// Transaction slice type for basic sorting +type Transactions []*Transaction + +func (s Transactions) Len() int { return len(s) } +func (s Transactions) Swap(i, j int) { s[i], s[j] = s[j], s[i] } + +type TxByNonce struct{ Transactions } + +func (s TxByNonce) Less(i, j int) bool { + return s.Transactions[i].Nonce < s.Transactions[j].Nonce +} diff --git a/ethminer/miner.go b/ethminer/miner.go index 00e04cde2..9396d33f9 100644 --- a/ethminer/miner.go +++ b/ethminer/miner.go @@ -5,6 +5,7 @@ import ( "github.com/ethereum/eth-go/ethchain" "github.com/ethereum/eth-go/ethutil" "github.com/ethereum/eth-go/ethwire" + "sort" ) type Miner struct { @@ -12,7 +13,7 @@ type Miner struct { ethereum ethchain.EthManager coinbase []byte reactChan chan ethutil.React - txs []*ethchain.Transaction + txs ethchain.Transactions uncles []*ethchain.Block block *ethchain.Block powChan chan []byte @@ -132,6 +133,8 @@ func (self *Miner) mineNewBlock() { self.block.SetUncles(self.uncles) } + // Sort the transactions by nonce in case of odd network propagation + sort.Sort(ethchain.TxByNonce{self.txs}) // Accumulate all valid transaction and apply them to the new state receipts, txs := stateManager.ApplyTransactions(self.block.State(), self.block, self.txs) self.txs = txs |