Skip to content

Commit 03d37c0

Browse files
authored
Merge pull request #125 from getamis/feature/improve_performance
Improve performance
2 parents d4ae30d + 39eaf7c commit 03d37c0

File tree

1 file changed

+16
-1
lines changed

1 file changed

+16
-1
lines changed

consensus/istanbul/backend/engine.go

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ import (
3737
"github.com/ethereum/go-ethereum/log"
3838
"github.com/ethereum/go-ethereum/rlp"
3939
"github.com/ethereum/go-ethereum/rpc"
40+
lru "github.com/hashicorp/golang-lru"
4041
)
4142

4243
const (
@@ -88,6 +89,9 @@ var (
8889

8990
nonceAuthVote = hexutil.MustDecode("0xffffffffffffffff") // Magic nonce number to vote on adding a new validator
9091
nonceDropVote = hexutil.MustDecode("0x0000000000000000") // Magic nonce number to vote on removing a validator.
92+
93+
inmemoryAddresses = 20 // Number of recent addresses from ecrecover
94+
recentAddresses, _ = lru.NewARC(inmemoryAddresses)
9195
)
9296

9397
// Author retrieves the Ethereum address of the account that minted the given
@@ -673,12 +677,23 @@ func sigHash(header *types.Header) (hash common.Hash) {
673677

674678
// ecrecover extracts the Ethereum account address from a signed header.
675679
func ecrecover(header *types.Header) (common.Address, error) {
680+
hash := header.Hash()
681+
if addr, ok := recentAddresses.Get(hash); ok {
682+
return addr.(common.Address), nil
683+
}
684+
676685
// Retrieve the signature from the header extra-data
677686
istanbulExtra, err := types.ExtractIstanbulExtra(header)
678687
if err != nil {
679688
return common.Address{}, err
680689
}
681-
return istanbul.GetSignatureAddress(sigHash(header).Bytes(), istanbulExtra.Seal)
690+
691+
addr, err := istanbul.GetSignatureAddress(sigHash(header).Bytes(), istanbulExtra.Seal)
692+
if err != nil {
693+
return addr, err
694+
}
695+
recentAddresses.Add(hash, addr)
696+
return addr, nil
682697
}
683698

684699
// prepareExtra returns a extra-data of the given header and validators

0 commit comments

Comments
 (0)