aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorBJ4 <bojie@dexon.org>2018-11-15 16:02:19 +0800
committerWei-Ning Huang <w@byzantine-lab.io>2019-06-12 17:27:18 +0800
commitd945ea451bece3c8e230ae017dd443266633d634 (patch)
tree1f5f47f05639d42e836f47eaccaa90773261db7d
parentdbb3d8fd30bd33df37f13048dc334ead8d335ddc (diff)
downloadgo-tangerine-d945ea451bece3c8e230ae017dd443266633d634.tar
go-tangerine-d945ea451bece3c8e230ae017dd443266633d634.tar.gz
go-tangerine-d945ea451bece3c8e230ae017dd443266633d634.tar.bz2
go-tangerine-d945ea451bece3c8e230ae017dd443266633d634.tar.lz
go-tangerine-d945ea451bece3c8e230ae017dd443266633d634.tar.xz
go-tangerine-d945ea451bece3c8e230ae017dd443266633d634.tar.zst
go-tangerine-d945ea451bece3c8e230ae017dd443266633d634.zip
app: add cache to reuse same tx address which has already recovered (#26)
-rw-r--r--core/blockchain.go6
-rw-r--r--core/tx_pool.go2
-rw-r--r--core/types/transaction_signing.go36
-rw-r--r--dex/app.go11
4 files changed, 43 insertions, 12 deletions
diff --git a/core/blockchain.go b/core/blockchain.go
index 4f3eefc1d..c364e57d2 100644
--- a/core/blockchain.go
+++ b/core/blockchain.go
@@ -249,6 +249,7 @@ func (bc *BlockChain) GetVMConfig() *vm.Config {
type blockInfo struct {
addresses map[common.Address]struct{}
block *coreTypes.Block
+ txs types.Transactions
}
func (bc *BlockChain) AddConfirmedBlock(block *coreTypes.Block) error {
@@ -298,6 +299,7 @@ func (bc *BlockChain) AddConfirmedBlock(block *coreTypes.Block) error {
bc.confirmedBlocks[chainID][block.Hash] = &blockInfo{
addresses: addressMap,
block: block,
+ txs: transactions,
}
bc.chainLastHeight[chainID] = block.Position.Height
return nil
@@ -317,8 +319,8 @@ func (bc *BlockChain) RemoveConfirmedBlock(chainID uint32, hash coreCommon.Hash)
delete(bc.confirmedBlocks[chainID], hash)
}
-func (bc *BlockChain) GetConfirmedBlockByHash(chainID uint32, hash coreCommon.Hash) *coreTypes.Block {
- return bc.confirmedBlocks[chainID][hash].block
+func (bc *BlockChain) GetConfirmedBlockByHash(chainID uint32, hash coreCommon.Hash) (*coreTypes.Block, types.Transactions) {
+ return bc.confirmedBlocks[chainID][hash].block, bc.confirmedBlocks[chainID][hash].txs
}
func (bc *BlockChain) GetLastNonceInConfirmedBlocks(chainID uint32, address common.Address) (uint64, bool) {
diff --git a/core/tx_pool.go b/core/tx_pool.go
index 54bad9eae..fc36d50bf 100644
--- a/core/tx_pool.go
+++ b/core/tx_pool.go
@@ -965,6 +965,8 @@ func (pool *TxPool) removeTx(hash common.Hash, outofbound bool) {
delete(pool.queue, addr)
}
}
+
+ types.DeleteTxCacheByHash(hash)
}
// promoteExecutables moves transactions that have become processable from the
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
diff --git a/dex/app.go b/dex/app.go
index af8295c9a..38ca382e3 100644
--- a/dex/app.go
+++ b/dex/app.go
@@ -458,18 +458,11 @@ func (d *DexconApp) BlockDelivered(
d.chainLock(chainID)
defer d.chainUnlock(chainID)
- block := d.blockchain.GetConfirmedBlockByHash(chainID, blockHash)
+ block, txs := d.blockchain.GetConfirmedBlockByHash(chainID, blockHash)
if block == nil {
panic("Can not get confirmed block")
}
- var transactions types.Transactions
- err := rlp.DecodeBytes(block.Payload, &transactions)
- if err != nil {
- log.Error("Payload rlp decode failed", "error", err)
- panic(err)
- }
-
block.Payload = nil
dexconMeta, err := rlp.EncodeToBytes(block)
if err != nil {
@@ -485,7 +478,7 @@ func (d *DexconApp) BlockDelivered(
Round: block.Position.Round,
DexconMeta: dexconMeta,
Randomness: result.Randomness,
- }, transactions, nil, nil)
+ }, txs, nil, nil)
root, err := d.blockchain.ProcessPendingBlock(newBlock, &block.Witness)
if err != nil {