use crypto pool (#6197)

This commit is contained in:
Alex Sharov 2022-12-04 11:59:02 +07:00 committed by GitHub
parent c401a2e9d2
commit 63b88c7d16
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 31 additions and 21 deletions

View File

@ -15,7 +15,7 @@ import (
"github.com/ledgerwatch/erigon/core/state" "github.com/ledgerwatch/erigon/core/state"
"github.com/ledgerwatch/erigon/core/types" "github.com/ledgerwatch/erigon/core/types"
"github.com/ledgerwatch/erigon/core/vm" "github.com/ledgerwatch/erigon/core/vm"
"github.com/ledgerwatch/erigon/crypto" "github.com/ledgerwatch/erigon/crypto/cryptopool"
"github.com/ledgerwatch/erigon/rpc" "github.com/ledgerwatch/erigon/rpc"
"github.com/ledgerwatch/erigon/turbo/adapter/ethapi" "github.com/ledgerwatch/erigon/turbo/adapter/ethapi"
"github.com/ledgerwatch/erigon/turbo/rpchelper" "github.com/ledgerwatch/erigon/turbo/rpchelper"
@ -149,8 +149,8 @@ func (api *APIImpl) CallBundle(ctx context.Context, txHashes []common.Hash, stat
results := []map[string]interface{}{} results := []map[string]interface{}{}
bundleHash := crypto.NewLegacyKeccak256() bundleHash := cryptopool.NewLegacyKeccak256()
defer crypto.ReturnToPoolKeccak256(bundleHash) defer cryptopool.ReturnToPoolKeccak256(bundleHash)
for _, txn := range txs { for _, txn := range txs {
msg, err := txn.AsMessage(*signer, nil, rules) msg, err := txn.AsMessage(*signer, nil, rules)

View File

@ -29,7 +29,7 @@ import (
"strings" "strings"
"github.com/ledgerwatch/erigon/common/hexutil" "github.com/ledgerwatch/erigon/common/hexutil"
"golang.org/x/crypto/sha3" "github.com/ledgerwatch/erigon/crypto/cryptopool"
) )
// Lengths of hashes and addresses in bytes. // Lengths of hashes and addresses in bytes.
@ -246,10 +246,12 @@ func (a *Address) checksumHex() []byte {
buf := a.hex() buf := a.hex()
// compute checksum // compute checksum
sha := sha3.NewLegacyKeccak256() sha := cryptopool.NewLegacyKeccak256()
//nolint:errcheck //nolint:errcheck
sha.Write(buf[2:]) sha.Write(buf[2:])
hash := sha.Sum(nil) hash := sha.Sum(nil)
cryptopool.ReturnToPoolKeccak256(sha)
for i := 2; i < len(buf); i++ { for i := 2; i < len(buf); i++ {
hashByte := hash[(i-2)/2] hashByte := hash[(i-2)/2]
if i%2 == 0 { if i%2 == 0 {
@ -507,10 +509,11 @@ func (a *Address32) checksumHex() []byte {
buf := a.hex() buf := a.hex()
// compute checksum // compute checksum
sha := sha3.NewLegacyKeccak256() sha := cryptopool.NewLegacyKeccak256()
//nolint:errcheck //nolint:errcheck
sha.Write(buf[2:]) sha.Write(buf[2:])
hash := sha.Sum(nil) hash := sha.Sum(nil)
cryptopool.ReturnToPoolKeccak256(sha)
for i := 2; i < len(buf); i++ { for i := 2; i < len(buf); i++ {
hashByte := hash[(i-2)/2] hashByte := hash[(i-2)/2]
if i%2 == 0 { if i%2 == 0 {

View File

@ -27,6 +27,7 @@ import (
"github.com/ledgerwatch/erigon/core/types" "github.com/ledgerwatch/erigon/core/types"
"github.com/ledgerwatch/erigon/core/types/accounts" "github.com/ledgerwatch/erigon/core/types/accounts"
"github.com/ledgerwatch/erigon/crypto" "github.com/ledgerwatch/erigon/crypto"
"github.com/ledgerwatch/erigon/crypto/cryptopool"
"github.com/ledgerwatch/erigon/params" "github.com/ledgerwatch/erigon/params"
"github.com/ledgerwatch/erigon/params/networkname" "github.com/ledgerwatch/erigon/params/networkname"
"github.com/ledgerwatch/erigon/rlp" "github.com/ledgerwatch/erigon/rlp"
@ -144,8 +145,8 @@ func ecrecover(header *types.Header, sigcache *lru.ARCCache, c *params.BorConfig
// SealHash returns the hash of a block prior to it being sealed. // SealHash returns the hash of a block prior to it being sealed.
func SealHash(header *types.Header, c *params.BorConfig) (hash common.Hash) { func SealHash(header *types.Header, c *params.BorConfig) (hash common.Hash) {
hasher := crypto.NewLegacyKeccak256() hasher := cryptopool.NewLegacyKeccak256()
defer crypto.ReturnToPoolKeccak256(hasher) defer cryptopool.ReturnToPoolKeccak256(hasher)
encodeSigHeader(hasher, header, c) encodeSigHeader(hasher, header, c)
hasher.Sum(hash[:0]) hasher.Sum(hash[:0])

View File

@ -40,6 +40,7 @@ import (
"github.com/ledgerwatch/erigon/core/types" "github.com/ledgerwatch/erigon/core/types"
"github.com/ledgerwatch/erigon/core/types/accounts" "github.com/ledgerwatch/erigon/core/types/accounts"
"github.com/ledgerwatch/erigon/crypto" "github.com/ledgerwatch/erigon/crypto"
"github.com/ledgerwatch/erigon/crypto/cryptopool"
"github.com/ledgerwatch/erigon/params" "github.com/ledgerwatch/erigon/params"
"github.com/ledgerwatch/erigon/rlp" "github.com/ledgerwatch/erigon/rlp"
"github.com/ledgerwatch/erigon/rpc" "github.com/ledgerwatch/erigon/rpc"
@ -524,8 +525,8 @@ func (c *Clique) APIs(chain consensus.ChainHeaderReader) []rpc.API {
// SealHash returns the hash of a block prior to it being sealed. // SealHash returns the hash of a block prior to it being sealed.
func SealHash(header *types.Header) (hash common.Hash) { func SealHash(header *types.Header) (hash common.Hash) {
hasher := crypto.NewLegacyKeccak256() hasher := cryptopool.NewLegacyKeccak256()
defer crypto.ReturnToPoolKeccak256(hasher) defer cryptopool.ReturnToPoolKeccak256(hasher)
encodeSigHeader(hasher, header) encodeSigHeader(hasher, header)
hasher.Sum(hash[:0]) hasher.Sum(hash[:0])

View File

@ -15,6 +15,7 @@ import (
"github.com/ledgerwatch/erigon-lib/common/length" "github.com/ledgerwatch/erigon-lib/common/length"
"github.com/ledgerwatch/erigon/core/rawdb" "github.com/ledgerwatch/erigon/core/rawdb"
"github.com/ledgerwatch/erigon/crypto/cryptopool"
lru "github.com/hashicorp/golang-lru" lru "github.com/hashicorp/golang-lru"
"github.com/holiman/uint256" "github.com/holiman/uint256"
@ -173,8 +174,8 @@ func ecrecover(header *types.Header, sigCache *lru.ARCCache, chainId *big.Int) (
// SealHash returns the hash of a block prior to it being sealed. // SealHash returns the hash of a block prior to it being sealed.
func SealHash(header *types.Header, chainId *big.Int) (hash common.Hash) { func SealHash(header *types.Header, chainId *big.Int) (hash common.Hash) {
hasher := crypto.NewLegacyKeccak256() hasher := cryptopool.NewLegacyKeccak256()
defer crypto.ReturnToPoolKeccak256(hasher) defer cryptopool.ReturnToPoolKeccak256(hasher)
encodeSigHeader(hasher, header, chainId) encodeSigHeader(hasher, header, chainId)
hasher.Sum(hash[:0]) hasher.Sum(hash[:0])

View File

@ -23,6 +23,7 @@ import (
"github.com/ledgerwatch/erigon/common/hexutil" "github.com/ledgerwatch/erigon/common/hexutil"
"github.com/ledgerwatch/erigon/crypto" "github.com/ledgerwatch/erigon/crypto"
"github.com/ledgerwatch/erigon/crypto/cryptopool"
) )
type bytesBacked interface { type bytesBacked interface {
@ -139,7 +140,7 @@ func bloomValues(data []byte, hashbuf []byte) (uint, byte, uint, byte, uint, byt
sha := crypto.NewKeccakState() sha := crypto.NewKeccakState()
sha.Write(data) //nolint:errcheck sha.Write(data) //nolint:errcheck
sha.Read(hashbuf) //nolint:errcheck sha.Read(hashbuf) //nolint:errcheck
crypto.ReturnToPoolKeccak256(sha) cryptopool.ReturnToPoolKeccak256(sha)
// The actual bits to flip // The actual bits to flip
v1 := byte(1 << (hashbuf[1] & 0x7)) v1 := byte(1 << (hashbuf[1] & 0x7))
v2 := byte(1 << (hashbuf[3] & 0x7)) v2 := byte(1 << (hashbuf[3] & 0x7))

View File

@ -23,6 +23,7 @@ import (
"github.com/ledgerwatch/erigon/common" "github.com/ledgerwatch/erigon/common"
"github.com/ledgerwatch/erigon/crypto" "github.com/ledgerwatch/erigon/crypto"
"github.com/ledgerwatch/erigon/crypto/cryptopool"
"github.com/ledgerwatch/erigon/rlp" "github.com/ledgerwatch/erigon/rlp"
"github.com/ledgerwatch/erigon/turbo/rlphacks" "github.com/ledgerwatch/erigon/turbo/rlphacks"
"github.com/ledgerwatch/erigon/turbo/trie" "github.com/ledgerwatch/erigon/turbo/trie"
@ -162,7 +163,7 @@ func RawRlpHash(rawRlpData rlp.RawValue) (h common.Hash) {
sha := crypto.NewKeccakState() sha := crypto.NewKeccakState()
sha.Write(rawRlpData) //nolint:errcheck sha.Write(rawRlpData) //nolint:errcheck
sha.Read(h[:]) //nolint:errcheck sha.Read(h[:]) //nolint:errcheck
crypto.ReturnToPoolKeccak256(sha) cryptopool.ReturnToPoolKeccak256(sha)
return h return h
} }
@ -170,7 +171,7 @@ func rlpHash(x interface{}) (h common.Hash) {
sha := crypto.NewKeccakState() sha := crypto.NewKeccakState()
rlp.Encode(sha, x) //nolint:errcheck rlp.Encode(sha, x) //nolint:errcheck
sha.Read(h[:]) //nolint:errcheck sha.Read(h[:]) //nolint:errcheck
crypto.ReturnToPoolKeccak256(sha) cryptopool.ReturnToPoolKeccak256(sha)
return h return h
} }
@ -185,6 +186,6 @@ func prefixedRlpHash(prefix byte, x interface{}) (h common.Hash) {
} }
//nolint:errcheck //nolint:errcheck
sha.Read(h[:]) sha.Read(h[:])
crypto.ReturnToPoolKeccak256(sha) cryptopool.ReturnToPoolKeccak256(sha)
return h return h
} }

View File

@ -30,6 +30,7 @@ import (
"os" "os"
"github.com/holiman/uint256" "github.com/holiman/uint256"
"github.com/ledgerwatch/erigon/crypto/cryptopool"
"golang.org/x/crypto/sha3" "golang.org/x/crypto/sha3"
"github.com/ledgerwatch/erigon/common" "github.com/ledgerwatch/erigon/common"
@ -66,7 +67,7 @@ type KeccakState interface {
// NewKeccakState creates a new KeccakState // NewKeccakState creates a new KeccakState
func NewKeccakState() KeccakState { func NewKeccakState() KeccakState {
return NewLegacyKeccak256().(KeccakState) return cryptopool.NewLegacyKeccak256().(KeccakState)
} }
// HashData hashes the provided data using the KeccakState and returns a 32 byte hash // HashData hashes the provided data using the KeccakState and returns a 32 byte hash
@ -87,7 +88,7 @@ func Keccak256(data ...[]byte) []byte {
d.Write(b) d.Write(b)
} }
d.Read(b) //nolint:errcheck d.Read(b) //nolint:errcheck
ReturnToPoolKeccak256(d) cryptopool.ReturnToPoolKeccak256(d)
return b return b
} }
@ -99,7 +100,7 @@ func Keccak256Hash(data ...[]byte) (h common.Hash) {
d.Write(b) d.Write(b)
} }
d.Read(h[:]) //nolint:errcheck d.Read(h[:]) //nolint:errcheck
ReturnToPoolKeccak256(d) cryptopool.ReturnToPoolKeccak256(d)
return h return h
} }

View File

@ -1,4 +1,4 @@
package crypto package cryptopool
import ( import (
"hash" "hash"

View File

@ -36,6 +36,7 @@ import (
"github.com/ledgerwatch/erigon/core/rawdb" "github.com/ledgerwatch/erigon/core/rawdb"
"github.com/ledgerwatch/erigon/core/types" "github.com/ledgerwatch/erigon/core/types"
"github.com/ledgerwatch/erigon/crypto" "github.com/ledgerwatch/erigon/crypto"
"github.com/ledgerwatch/erigon/crypto/cryptopool"
"github.com/ledgerwatch/erigon/eth/ethconfig" "github.com/ledgerwatch/erigon/eth/ethconfig"
"github.com/ledgerwatch/erigon/eth/stagedsync/stages" "github.com/ledgerwatch/erigon/eth/stagedsync/stages"
"github.com/ledgerwatch/erigon/params" "github.com/ledgerwatch/erigon/params"
@ -1838,7 +1839,7 @@ func HeadersIdx(ctx context.Context, segmentFilePath string, firstBlockNumInSegm
p.Total.Store(uint64(d.Count())) p.Total.Store(uint64(d.Count()))
hasher := crypto.NewKeccakState() hasher := crypto.NewKeccakState()
defer crypto.ReturnToPoolKeccak256(hasher) defer cryptopool.ReturnToPoolKeccak256(hasher)
var h common.Hash var h common.Hash
if err := Idx(ctx, d, firstBlockNumInSegment, tmpDir, log.LvlDebug, func(idx *recsplit.RecSplit, i, offset uint64, word []byte) error { if err := Idx(ctx, d, firstBlockNumInSegment, tmpDir, log.LvlDebug, func(idx *recsplit.RecSplit, i, offset uint64, word []byte) error {
p.Processed.Inc() p.Processed.Inc()