diff options
author | Jimmy Hu <jimmy.hu@dexon.org> | 2018-11-14 15:32:27 +0800 |
---|---|---|
committer | Wei-Ning Huang <w@dexon.org> | 2018-12-19 20:54:27 +0800 |
commit | 47d6ac16aef156de658f5ac87d9fa17500c4f401 (patch) | |
tree | 1c789c63fbe22348a5a1de1a858e8ae324305819 | |
parent | 113d40d855d6f5dd295d46b5bf20e8db315ed01f (diff) | |
download | dexon-47d6ac16aef156de658f5ac87d9fa17500c4f401.tar.gz dexon-47d6ac16aef156de658f5ac87d9fa17500c4f401.tar.zst dexon-47d6ac16aef156de658f5ac87d9fa17500c4f401.zip |
core, dex: Optimize sender calculation in block transactions. (#22)
* Add Transactions.TouchSenders that calculates sender and update cache
* Use TouchSenders to fill the caches
-rw-r--r-- | core/blockchain.go | 4 | ||||
-rw-r--r-- | core/types/transaction.go | 30 | ||||
-rw-r--r-- | dex/app.go | 6 |
3 files changed, 40 insertions, 0 deletions
diff --git a/core/blockchain.go b/core/blockchain.go index 58f80681d..221936cea 100644 --- a/core/blockchain.go +++ b/core/blockchain.go @@ -347,6 +347,10 @@ func (bc *BlockChain) AddConfirmedBlock(block *coreTypes.Block) error { if err != nil { return err } + _, err := transactions.TouchSenders(types.MakeSigner(bc.Config(), new(big.Int))) + if err != nil { + return err + } addressMap := map[common.Address]struct{}{} for _, tx := range transactions { diff --git a/core/types/transaction.go b/core/types/transaction.go index dcb3f070f..63195f4fd 100644 --- a/core/types/transaction.go +++ b/core/types/transaction.go @@ -21,6 +21,7 @@ import ( "errors" "io" "math/big" + "sync" "sync/atomic" "github.com/dexon-foundation/dexon/common" @@ -271,6 +272,35 @@ func (s Transactions) GetRlp(i int) []byte { return enc } +// TouchSenders calculates the sender of each transaction and update the cache. +func (s Transactions) TouchSenders(signer Signer) (errorTx *Transaction, err error) { + wg := sync.WaitGroup{} + wg.Add(len(s)) + txError := make(chan error, 1) + for _, tx := range s { + go func(tx *Transaction) { + defer wg.Done() + if len(txError) > 0 { + return + } + _, err := Sender(signer, tx) + if err != nil { + select { + case txError <- err: + errorTx = tx + default: + } + } + }(tx) + } + wg.Wait() + select { + case err = <-txError: + default: + } + return +} + // 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)) diff --git a/dex/app.go b/dex/app.go index a90dfae37..7d9c9e13a 100644 --- a/dex/app.go +++ b/dex/app.go @@ -362,6 +362,12 @@ func (d *DexconApp) VerifyBlock(block *coreTypes.Block) coreTypes.BlockVerifySta return coreTypes.VerifyInvalidBlock } + _, err := transactions.TouchSenders(types.MakeSigner(d.blockchain.Config(), new(big.Int))) + if err != nil { + log.Error("Failed to calculate sender", "error", err) + return coreTypes.VerifyInvalidBlock + } + addressNonce, err := d.validateNonce(transactions) if err != nil { log.Error("Validate nonce failed", "error", err) |