From 63b88c7d1601b1180fc846cdad5fd74bc460d0cb Mon Sep 17 00:00:00 2001 From: Alex Sharov Date: Sun, 4 Dec 2022 11:59:02 +0700 Subject: [PATCH] use crypto pool (#6197) --- cmd/rpcdaemon/commands/eth_block.go | 6 +++--- common/types.go | 9 ++++++--- consensus/bor/bor.go | 5 +++-- consensus/clique/clique.go | 5 +++-- consensus/parlia/parlia.go | 5 +++-- core/types/bloom9.go | 3 ++- core/types/hashing.go | 7 ++++--- crypto/crypto.go | 7 ++++--- crypto/{ => cryptopool}/pool.go | 2 +- turbo/snapshotsync/block_snapshots.go | 3 ++- 10 files changed, 31 insertions(+), 21 deletions(-) rename crypto/{ => cryptopool}/pool.go (94%) diff --git a/cmd/rpcdaemon/commands/eth_block.go b/cmd/rpcdaemon/commands/eth_block.go index 999681c2d..b34cc0b6c 100644 --- a/cmd/rpcdaemon/commands/eth_block.go +++ b/cmd/rpcdaemon/commands/eth_block.go @@ -15,7 +15,7 @@ import ( "github.com/ledgerwatch/erigon/core/state" "github.com/ledgerwatch/erigon/core/types" "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/turbo/adapter/ethapi" "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{}{} - bundleHash := crypto.NewLegacyKeccak256() - defer crypto.ReturnToPoolKeccak256(bundleHash) + bundleHash := cryptopool.NewLegacyKeccak256() + defer cryptopool.ReturnToPoolKeccak256(bundleHash) for _, txn := range txs { msg, err := txn.AsMessage(*signer, nil, rules) diff --git a/common/types.go b/common/types.go index efa7f679b..54f54db74 100644 --- a/common/types.go +++ b/common/types.go @@ -29,7 +29,7 @@ import ( "strings" "github.com/ledgerwatch/erigon/common/hexutil" - "golang.org/x/crypto/sha3" + "github.com/ledgerwatch/erigon/crypto/cryptopool" ) // Lengths of hashes and addresses in bytes. @@ -246,10 +246,12 @@ func (a *Address) checksumHex() []byte { buf := a.hex() // compute checksum - sha := sha3.NewLegacyKeccak256() + sha := cryptopool.NewLegacyKeccak256() //nolint:errcheck sha.Write(buf[2:]) hash := sha.Sum(nil) + cryptopool.ReturnToPoolKeccak256(sha) + for i := 2; i < len(buf); i++ { hashByte := hash[(i-2)/2] if i%2 == 0 { @@ -507,10 +509,11 @@ func (a *Address32) checksumHex() []byte { buf := a.hex() // compute checksum - sha := sha3.NewLegacyKeccak256() + sha := cryptopool.NewLegacyKeccak256() //nolint:errcheck sha.Write(buf[2:]) hash := sha.Sum(nil) + cryptopool.ReturnToPoolKeccak256(sha) for i := 2; i < len(buf); i++ { hashByte := hash[(i-2)/2] if i%2 == 0 { diff --git a/consensus/bor/bor.go b/consensus/bor/bor.go index 3eeb59cf4..32ff49ca5 100644 --- a/consensus/bor/bor.go +++ b/consensus/bor/bor.go @@ -27,6 +27,7 @@ import ( "github.com/ledgerwatch/erigon/core/types" "github.com/ledgerwatch/erigon/core/types/accounts" "github.com/ledgerwatch/erigon/crypto" + "github.com/ledgerwatch/erigon/crypto/cryptopool" "github.com/ledgerwatch/erigon/params" "github.com/ledgerwatch/erigon/params/networkname" "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. func SealHash(header *types.Header, c *params.BorConfig) (hash common.Hash) { - hasher := crypto.NewLegacyKeccak256() - defer crypto.ReturnToPoolKeccak256(hasher) + hasher := cryptopool.NewLegacyKeccak256() + defer cryptopool.ReturnToPoolKeccak256(hasher) encodeSigHeader(hasher, header, c) hasher.Sum(hash[:0]) diff --git a/consensus/clique/clique.go b/consensus/clique/clique.go index 8115019e5..698b01ebc 100644 --- a/consensus/clique/clique.go +++ b/consensus/clique/clique.go @@ -40,6 +40,7 @@ import ( "github.com/ledgerwatch/erigon/core/types" "github.com/ledgerwatch/erigon/core/types/accounts" "github.com/ledgerwatch/erigon/crypto" + "github.com/ledgerwatch/erigon/crypto/cryptopool" "github.com/ledgerwatch/erigon/params" "github.com/ledgerwatch/erigon/rlp" "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. func SealHash(header *types.Header) (hash common.Hash) { - hasher := crypto.NewLegacyKeccak256() - defer crypto.ReturnToPoolKeccak256(hasher) + hasher := cryptopool.NewLegacyKeccak256() + defer cryptopool.ReturnToPoolKeccak256(hasher) encodeSigHeader(hasher, header) hasher.Sum(hash[:0]) diff --git a/consensus/parlia/parlia.go b/consensus/parlia/parlia.go index 598dcaa6b..709cb3461 100644 --- a/consensus/parlia/parlia.go +++ b/consensus/parlia/parlia.go @@ -15,6 +15,7 @@ import ( "github.com/ledgerwatch/erigon-lib/common/length" "github.com/ledgerwatch/erigon/core/rawdb" + "github.com/ledgerwatch/erigon/crypto/cryptopool" lru "github.com/hashicorp/golang-lru" "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. func SealHash(header *types.Header, chainId *big.Int) (hash common.Hash) { - hasher := crypto.NewLegacyKeccak256() - defer crypto.ReturnToPoolKeccak256(hasher) + hasher := cryptopool.NewLegacyKeccak256() + defer cryptopool.ReturnToPoolKeccak256(hasher) encodeSigHeader(hasher, header, chainId) hasher.Sum(hash[:0]) diff --git a/core/types/bloom9.go b/core/types/bloom9.go index b00bd6f52..80a919d05 100644 --- a/core/types/bloom9.go +++ b/core/types/bloom9.go @@ -23,6 +23,7 @@ import ( "github.com/ledgerwatch/erigon/common/hexutil" "github.com/ledgerwatch/erigon/crypto" + "github.com/ledgerwatch/erigon/crypto/cryptopool" ) type bytesBacked interface { @@ -139,7 +140,7 @@ func bloomValues(data []byte, hashbuf []byte) (uint, byte, uint, byte, uint, byt sha := crypto.NewKeccakState() sha.Write(data) //nolint:errcheck sha.Read(hashbuf) //nolint:errcheck - crypto.ReturnToPoolKeccak256(sha) + cryptopool.ReturnToPoolKeccak256(sha) // The actual bits to flip v1 := byte(1 << (hashbuf[1] & 0x7)) v2 := byte(1 << (hashbuf[3] & 0x7)) diff --git a/core/types/hashing.go b/core/types/hashing.go index 1e0aff398..e4e3c1f4f 100644 --- a/core/types/hashing.go +++ b/core/types/hashing.go @@ -23,6 +23,7 @@ import ( "github.com/ledgerwatch/erigon/common" "github.com/ledgerwatch/erigon/crypto" + "github.com/ledgerwatch/erigon/crypto/cryptopool" "github.com/ledgerwatch/erigon/rlp" "github.com/ledgerwatch/erigon/turbo/rlphacks" "github.com/ledgerwatch/erigon/turbo/trie" @@ -162,7 +163,7 @@ func RawRlpHash(rawRlpData rlp.RawValue) (h common.Hash) { sha := crypto.NewKeccakState() sha.Write(rawRlpData) //nolint:errcheck sha.Read(h[:]) //nolint:errcheck - crypto.ReturnToPoolKeccak256(sha) + cryptopool.ReturnToPoolKeccak256(sha) return h } @@ -170,7 +171,7 @@ func rlpHash(x interface{}) (h common.Hash) { sha := crypto.NewKeccakState() rlp.Encode(sha, x) //nolint:errcheck sha.Read(h[:]) //nolint:errcheck - crypto.ReturnToPoolKeccak256(sha) + cryptopool.ReturnToPoolKeccak256(sha) return h } @@ -185,6 +186,6 @@ func prefixedRlpHash(prefix byte, x interface{}) (h common.Hash) { } //nolint:errcheck sha.Read(h[:]) - crypto.ReturnToPoolKeccak256(sha) + cryptopool.ReturnToPoolKeccak256(sha) return h } diff --git a/crypto/crypto.go b/crypto/crypto.go index 3c4ef9ffb..0c46888af 100644 --- a/crypto/crypto.go +++ b/crypto/crypto.go @@ -30,6 +30,7 @@ import ( "os" "github.com/holiman/uint256" + "github.com/ledgerwatch/erigon/crypto/cryptopool" "golang.org/x/crypto/sha3" "github.com/ledgerwatch/erigon/common" @@ -66,7 +67,7 @@ type KeccakState interface { // NewKeccakState creates a new 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 @@ -87,7 +88,7 @@ func Keccak256(data ...[]byte) []byte { d.Write(b) } d.Read(b) //nolint:errcheck - ReturnToPoolKeccak256(d) + cryptopool.ReturnToPoolKeccak256(d) return b } @@ -99,7 +100,7 @@ func Keccak256Hash(data ...[]byte) (h common.Hash) { d.Write(b) } d.Read(h[:]) //nolint:errcheck - ReturnToPoolKeccak256(d) + cryptopool.ReturnToPoolKeccak256(d) return h } diff --git a/crypto/pool.go b/crypto/cryptopool/pool.go similarity index 94% rename from crypto/pool.go rename to crypto/cryptopool/pool.go index 737024bd3..92b038510 100644 --- a/crypto/pool.go +++ b/crypto/cryptopool/pool.go @@ -1,4 +1,4 @@ -package crypto +package cryptopool import ( "hash" diff --git a/turbo/snapshotsync/block_snapshots.go b/turbo/snapshotsync/block_snapshots.go index 789ecdb17..44a7602ba 100644 --- a/turbo/snapshotsync/block_snapshots.go +++ b/turbo/snapshotsync/block_snapshots.go @@ -36,6 +36,7 @@ import ( "github.com/ledgerwatch/erigon/core/rawdb" "github.com/ledgerwatch/erigon/core/types" "github.com/ledgerwatch/erigon/crypto" + "github.com/ledgerwatch/erigon/crypto/cryptopool" "github.com/ledgerwatch/erigon/eth/ethconfig" "github.com/ledgerwatch/erigon/eth/stagedsync/stages" "github.com/ledgerwatch/erigon/params" @@ -1838,7 +1839,7 @@ func HeadersIdx(ctx context.Context, segmentFilePath string, firstBlockNumInSegm p.Total.Store(uint64(d.Count())) hasher := crypto.NewKeccakState() - defer crypto.ReturnToPoolKeccak256(hasher) + defer cryptopool.ReturnToPoolKeccak256(hasher) var h common.Hash if err := Idx(ctx, d, firstBlockNumInSegment, tmpDir, log.LvlDebug, func(idx *recsplit.RecSplit, i, offset uint64, word []byte) error { p.Processed.Inc()