diff options
author | Jimmy Hu <jimmy.hu@dexon.org> | 2018-11-14 15:32:27 +0800 |
---|---|---|
committer | Wei-Ning Huang <w@byzantine-lab.io> | 2019-06-12 17:27:18 +0800 |
commit | d151e9b72c3af7570c14ba3e9bc773e33a6c1e73 (patch) | |
tree | 84dae196d64000ca14a57472710848f3dbdd40b5 /core | |
parent | 9bf7d31fc35c88b063c0de00997504d78f40df13 (diff) | |
download | go-tangerine-d151e9b72c3af7570c14ba3e9bc773e33a6c1e73.tar go-tangerine-d151e9b72c3af7570c14ba3e9bc773e33a6c1e73.tar.gz go-tangerine-d151e9b72c3af7570c14ba3e9bc773e33a6c1e73.tar.bz2 go-tangerine-d151e9b72c3af7570c14ba3e9bc773e33a6c1e73.tar.lz go-tangerine-d151e9b72c3af7570c14ba3e9bc773e33a6c1e73.tar.xz go-tangerine-d151e9b72c3af7570c14ba3e9bc773e33a6c1e73.tar.zst go-tangerine-d151e9b72c3af7570c14ba3e9bc773e33a6c1e73.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
Diffstat (limited to 'core')
-rw-r--r-- | core/blockchain.go | 4 | ||||
-rw-r--r-- | core/types/transaction.go | 30 |
2 files changed, 34 insertions, 0 deletions
diff --git a/core/blockchain.go b/core/blockchain.go index 3b71cb64b..60730d197 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 857ac2137..46c24bd81 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)) |