aboutsummaryrefslogtreecommitdiffstats
path: root/core/types
diff options
context:
space:
mode:
authorBJ4 <bojie@dexon.org>2018-11-15 16:02:19 +0800
committerWei-Ning Huang <w@dexon.org>2019-04-09 21:32:53 +0800
commit24e161879e478a589c8857a1b2fec66fa948d225 (patch)
treeaf407d2615b41ff4703cfa91f86708f6aced2ae7 /core/types
parentb26e4c9adaa2952a63bf811f991a3e6fb9fc5646 (diff)
downloaddexon-24e161879e478a589c8857a1b2fec66fa948d225.tar
dexon-24e161879e478a589c8857a1b2fec66fa948d225.tar.gz
dexon-24e161879e478a589c8857a1b2fec66fa948d225.tar.bz2
dexon-24e161879e478a589c8857a1b2fec66fa948d225.tar.lz
dexon-24e161879e478a589c8857a1b2fec66fa948d225.tar.xz
dexon-24e161879e478a589c8857a1b2fec66fa948d225.tar.zst
dexon-24e161879e478a589c8857a1b2fec66fa948d225.zip
app: add cache to reuse same tx address which has already recovered (#26)
Diffstat (limited to 'core/types')
-rw-r--r--core/types/transaction_signing.go36
1 files changed, 35 insertions, 1 deletions
diff --git a/core/types/transaction_signing.go b/core/types/transaction_signing.go
index cd18a7e13..a6c5c1f16 100644
--- a/core/types/transaction_signing.go
+++ b/core/types/transaction_signing.go
@@ -21,6 +21,7 @@ import (
"errors"
"fmt"
"math/big"
+ "sync"
"github.com/dexon-foundation/dexon/common"
"github.com/dexon-foundation/dexon/crypto"
@@ -31,6 +32,27 @@ var (
ErrInvalidChainId = errors.New("invalid chain id for signer")
)
+var (
+ txCache = &sync.Map{}
+)
+
+func DeleteTxCacheByHash(hash common.Hash) {
+ txCache.Delete(hash)
+}
+
+func StoreTxCache(key common.Hash, value common.Address) {
+ txCache.Store(key, value)
+}
+
+func LoadTxCache(key common.Hash) (common.Address, bool) {
+ addr, ok := txCache.Load(key)
+ if !ok {
+ return common.Address{}, ok
+ }
+
+ return addr.(common.Address), ok
+}
+
// sigCache is used to cache the derived sender and contains
// the signer used to derive it.
type sigCache struct {
@@ -125,6 +147,11 @@ func (s EIP155Signer) Equal(s2 Signer) bool {
var big8 = big.NewInt(8)
func (s EIP155Signer) Sender(tx *Transaction) (common.Address, error) {
+ addr, ok := LoadTxCache(tx.Hash())
+ if ok {
+ return addr, nil
+ }
+
if !tx.Protected() {
return HomesteadSigner{}.Sender(tx)
}
@@ -133,7 +160,14 @@ func (s EIP155Signer) Sender(tx *Transaction) (common.Address, error) {
}
V := new(big.Int).Sub(tx.data.V, s.chainIdMul)
V.Sub(V, big8)
- return recoverPlain(s.Hash(tx), tx.data.R, tx.data.S, V, true)
+
+ addr, err := recoverPlain(s.Hash(tx), tx.data.R, tx.data.S, V, true)
+ if err != nil {
+ return common.Address{}, err
+ }
+
+ StoreTxCache(tx.Hash(), addr)
+ return addr, nil
}
// SignatureValues returns signature values. This signature