mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-07-26 14:46:42 +00:00
upgrade hasher pool
This commit is contained in:
parent
88a7ef233a
commit
b29998a3af
8 changed files with 51 additions and 57 deletions
|
|
@ -24,8 +24,8 @@ import (
|
||||||
"github.com/ethereum/go-ethereum"
|
"github.com/ethereum/go-ethereum"
|
||||||
"github.com/ethereum/go-ethereum/common"
|
"github.com/ethereum/go-ethereum/common"
|
||||||
"github.com/ethereum/go-ethereum/core/types"
|
"github.com/ethereum/go-ethereum/core/types"
|
||||||
|
"github.com/ethereum/go-ethereum/crypto"
|
||||||
"github.com/ethereum/go-ethereum/event"
|
"github.com/ethereum/go-ethereum/event"
|
||||||
"golang.org/x/crypto/sha3"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// Account represents an Ethereum account located at a specific location defined
|
// Account represents an Ethereum account located at a specific location defined
|
||||||
|
|
@ -196,9 +196,7 @@ func TextHash(data []byte) []byte {
|
||||||
// This gives context to the signed message and prevents signing of transactions.
|
// This gives context to the signed message and prevents signing of transactions.
|
||||||
func TextAndHash(data []byte) ([]byte, string) {
|
func TextAndHash(data []byte) ([]byte, string) {
|
||||||
msg := fmt.Sprintf("\x19Ethereum Signed Message:\n%d%s", len(data), data)
|
msg := fmt.Sprintf("\x19Ethereum Signed Message:\n%d%s", len(data), data)
|
||||||
hasher := sha3.NewLegacyKeccak256()
|
return crypto.Keccak256([]byte(msg)), msg
|
||||||
hasher.Write([]byte(msg))
|
|
||||||
return hasher.Sum(nil), msg
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// WalletEventType represents the different event types that can be fired by
|
// WalletEventType represents the different event types that can be fired by
|
||||||
|
|
|
||||||
|
|
@ -40,7 +40,6 @@ import (
|
||||||
"github.com/ethereum/go-ethereum/trie"
|
"github.com/ethereum/go-ethereum/trie"
|
||||||
"github.com/ethereum/go-ethereum/triedb"
|
"github.com/ethereum/go-ethereum/triedb"
|
||||||
"github.com/holiman/uint256"
|
"github.com/holiman/uint256"
|
||||||
"golang.org/x/crypto/sha3"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type Prestate struct {
|
type Prestate struct {
|
||||||
|
|
@ -437,10 +436,7 @@ func MakePreState(db ethdb.Database, accounts types.GenesisAlloc) *state.StateDB
|
||||||
}
|
}
|
||||||
|
|
||||||
func rlpHash(x interface{}) (h common.Hash) {
|
func rlpHash(x interface{}) (h common.Hash) {
|
||||||
hw := sha3.NewLegacyKeccak256()
|
return crypto.Keccak256RLPHash(x)
|
||||||
rlp.Encode(hw, x)
|
|
||||||
hw.Sum(h[:0])
|
|
||||||
return h
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// calcDifficulty is based on ethash.CalcDifficulty. This method is used in case
|
// calcDifficulty is based on ethash.CalcDifficulty. This method is used in case
|
||||||
|
|
|
||||||
|
|
@ -27,7 +27,6 @@ import (
|
||||||
"github.com/ethereum/go-ethereum/core/types"
|
"github.com/ethereum/go-ethereum/core/types"
|
||||||
"github.com/ethereum/go-ethereum/crypto"
|
"github.com/ethereum/go-ethereum/crypto"
|
||||||
"github.com/ethereum/go-ethereum/internal/flags"
|
"github.com/ethereum/go-ethereum/internal/flags"
|
||||||
"github.com/ethereum/go-ethereum/rlp"
|
|
||||||
"github.com/urfave/cli/v2"
|
"github.com/urfave/cli/v2"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
@ -131,9 +130,7 @@ func generateHistoryTests(clictx *cli.Context) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
func calcReceiptsHash(rcpt []*types.Receipt) common.Hash {
|
func calcReceiptsHash(rcpt []*types.Receipt) common.Hash {
|
||||||
h := crypto.NewKeccakState()
|
return crypto.Keccak256RLPHash(rcpt)
|
||||||
rlp.Encode(h, rcpt)
|
|
||||||
return common.Hash(h.Sum(nil))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func writeJSON(fileName string, value any) {
|
func writeJSON(fileName string, value any) {
|
||||||
|
|
|
||||||
|
|
@ -43,7 +43,6 @@ import (
|
||||||
"github.com/ethereum/go-ethereum/rlp"
|
"github.com/ethereum/go-ethereum/rlp"
|
||||||
"github.com/ethereum/go-ethereum/rpc"
|
"github.com/ethereum/go-ethereum/rpc"
|
||||||
"github.com/ethereum/go-ethereum/trie"
|
"github.com/ethereum/go-ethereum/trie"
|
||||||
"golang.org/x/crypto/sha3"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
|
|
@ -651,11 +650,10 @@ 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) common.Hash {
|
||||||
hasher := sha3.NewLegacyKeccak256()
|
buf := bytes.NewBuffer(nil)
|
||||||
encodeSigHeader(hasher, header)
|
encodeSigHeader(buf, header)
|
||||||
hasher.(crypto.KeccakState).Read(hash[:])
|
return crypto.Keccak256Hash(buf.Bytes())
|
||||||
return hash
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// CliqueRLP returns the rlp bytes which needs to be signed for the proof-of-authority
|
// CliqueRLP returns the rlp bytes which needs to be signed for the proof-of-authority
|
||||||
|
|
|
||||||
|
|
@ -31,11 +31,10 @@ import (
|
||||||
"github.com/ethereum/go-ethereum/core/tracing"
|
"github.com/ethereum/go-ethereum/core/tracing"
|
||||||
"github.com/ethereum/go-ethereum/core/types"
|
"github.com/ethereum/go-ethereum/core/types"
|
||||||
"github.com/ethereum/go-ethereum/core/vm"
|
"github.com/ethereum/go-ethereum/core/vm"
|
||||||
|
"github.com/ethereum/go-ethereum/crypto"
|
||||||
"github.com/ethereum/go-ethereum/params"
|
"github.com/ethereum/go-ethereum/params"
|
||||||
"github.com/ethereum/go-ethereum/rlp"
|
|
||||||
"github.com/ethereum/go-ethereum/trie"
|
"github.com/ethereum/go-ethereum/trie"
|
||||||
"github.com/holiman/uint256"
|
"github.com/holiman/uint256"
|
||||||
"golang.org/x/crypto/sha3"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// Ethash proof-of-work protocol constants.
|
// Ethash proof-of-work protocol constants.
|
||||||
|
|
@ -527,8 +526,6 @@ func (ethash *Ethash) FinalizeAndAssemble(chain consensus.ChainHeaderReader, hea
|
||||||
|
|
||||||
// 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 (ethash *Ethash) SealHash(header *types.Header) (hash common.Hash) {
|
func (ethash *Ethash) SealHash(header *types.Header) (hash common.Hash) {
|
||||||
hasher := sha3.NewLegacyKeccak256()
|
|
||||||
|
|
||||||
enc := []interface{}{
|
enc := []interface{}{
|
||||||
header.ParentHash,
|
header.ParentHash,
|
||||||
header.UncleHash,
|
header.UncleHash,
|
||||||
|
|
@ -559,9 +556,7 @@ func (ethash *Ethash) SealHash(header *types.Header) (hash common.Hash) {
|
||||||
if header.ParentBeaconRoot != nil {
|
if header.ParentBeaconRoot != nil {
|
||||||
panic("parent beacon root set on ethash")
|
panic("parent beacon root set on ethash")
|
||||||
}
|
}
|
||||||
rlp.Encode(hasher, enc)
|
return crypto.Keccak256RLPHash(enc)
|
||||||
hasher.Sum(hash[:0])
|
|
||||||
return hash
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// accumulateRewards credits the coinbase of the given block with the mining
|
// accumulateRewards credits the coinbase of the given block with the mining
|
||||||
|
|
|
||||||
|
|
@ -74,9 +74,17 @@ func NewKeccakState() KeccakState {
|
||||||
return sha3.NewLegacyKeccak256().(KeccakState)
|
return sha3.NewLegacyKeccak256().(KeccakState)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type keccakState struct {
|
||||||
|
KeccakState
|
||||||
|
hashBuf common.Hash
|
||||||
|
}
|
||||||
|
|
||||||
var hasherPool = sync.Pool{
|
var hasherPool = sync.Pool{
|
||||||
New: func() any {
|
New: func() any {
|
||||||
return sha3.NewLegacyKeccak256().(KeccakState)
|
return &keccakState{
|
||||||
|
hashBuf: common.Hash{},
|
||||||
|
KeccakState: sha3.NewLegacyKeccak256().(KeccakState),
|
||||||
|
}
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -90,28 +98,38 @@ func HashData(kh KeccakState, data []byte) (h common.Hash) {
|
||||||
|
|
||||||
// Keccak256 calculates and returns the Keccak256 hash of the input data.
|
// Keccak256 calculates and returns the Keccak256 hash of the input data.
|
||||||
func Keccak256(data ...[]byte) []byte {
|
func Keccak256(data ...[]byte) []byte {
|
||||||
b := make([]byte, 32)
|
b := Keccak256Hash(data...)
|
||||||
d := hasherPool.Get().(KeccakState)
|
return b[:]
|
||||||
d.Reset()
|
}
|
||||||
for _, b := range data {
|
|
||||||
d.Write(b)
|
// Keccak256RLP calculates and returns the Keccak256 hash of the input interface.
|
||||||
}
|
func Keccak256RLP(x interface{}) []byte {
|
||||||
d.Read(b)
|
b := Keccak256RLPHash(x)
|
||||||
hasherPool.Put(d)
|
return b[:]
|
||||||
return b
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Keccak256Hash calculates and returns the Keccak256 hash of the input data,
|
// Keccak256Hash calculates and returns the Keccak256 hash of the input data,
|
||||||
// converting it to an internal Hash data structure.
|
// converting it to an internal Hash data structure.
|
||||||
func Keccak256Hash(data ...[]byte) (h common.Hash) {
|
func Keccak256Hash(data ...[]byte) common.Hash {
|
||||||
d := hasherPool.Get().(KeccakState)
|
d := hasherPool.Get().(*keccakState)
|
||||||
d.Reset()
|
d.Reset()
|
||||||
for _, b := range data {
|
for _, b := range data {
|
||||||
d.Write(b)
|
d.Write(b)
|
||||||
}
|
}
|
||||||
d.Read(h[:])
|
d.Read(d.hashBuf[:])
|
||||||
hasherPool.Put(d)
|
hasherPool.Put(d)
|
||||||
return h
|
return d.hashBuf
|
||||||
|
}
|
||||||
|
|
||||||
|
// Keccak256RLPHash calculates and returns the Keccak256 hash of the input interface,
|
||||||
|
// converting it to an internal Hash data structure.
|
||||||
|
func Keccak256RLPHash(x interface{}) common.Hash {
|
||||||
|
d := hasherPool.Get().(*keccakState)
|
||||||
|
d.Reset()
|
||||||
|
rlp.Encode(d, x)
|
||||||
|
d.Read(d.hashBuf[:])
|
||||||
|
hasherPool.Put(d)
|
||||||
|
return d.hashBuf
|
||||||
}
|
}
|
||||||
|
|
||||||
// Keccak512 calculates and returns the Keccak512 hash of the input data.
|
// Keccak512 calculates and returns the Keccak512 hash of the input data.
|
||||||
|
|
@ -125,8 +143,7 @@ func Keccak512(data ...[]byte) []byte {
|
||||||
|
|
||||||
// CreateAddress creates an ethereum address given the bytes and the nonce
|
// CreateAddress creates an ethereum address given the bytes and the nonce
|
||||||
func CreateAddress(b common.Address, nonce uint64) common.Address {
|
func CreateAddress(b common.Address, nonce uint64) common.Address {
|
||||||
data, _ := rlp.EncodeToBytes([]interface{}{b, nonce})
|
return common.BytesToAddress(Keccak256RLP([]interface{}{b, nonce})[12:])
|
||||||
return common.BytesToAddress(Keccak256(data)[12:])
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// CreateAddress2 creates an ethereum address given the address bytes, initial
|
// CreateAddress2 creates an ethereum address given the address bytes, initial
|
||||||
|
|
|
||||||
|
|
@ -23,7 +23,6 @@ import (
|
||||||
"encoding/base64"
|
"encoding/base64"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
|
||||||
"slices"
|
"slices"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
|
@ -31,7 +30,6 @@ import (
|
||||||
"github.com/ethereum/go-ethereum/p2p/enode"
|
"github.com/ethereum/go-ethereum/p2p/enode"
|
||||||
"github.com/ethereum/go-ethereum/p2p/enr"
|
"github.com/ethereum/go-ethereum/p2p/enr"
|
||||||
"github.com/ethereum/go-ethereum/rlp"
|
"github.com/ethereum/go-ethereum/rlp"
|
||||||
"golang.org/x/crypto/sha3"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// Tree is a merkle tree of node records.
|
// Tree is a merkle tree of node records.
|
||||||
|
|
@ -262,9 +260,8 @@ const (
|
||||||
)
|
)
|
||||||
|
|
||||||
func subdomain(e entry) string {
|
func subdomain(e entry) string {
|
||||||
h := sha3.NewLegacyKeccak256()
|
src := crypto.Keccak256([]byte(e.String()))
|
||||||
io.WriteString(h, e.String())
|
return b32format.EncodeToString(src[:16])
|
||||||
return b32format.EncodeToString(h.Sum(nil)[:16])
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e *rootEntry) String() string {
|
func (e *rootEntry) String() string {
|
||||||
|
|
@ -272,9 +269,8 @@ func (e *rootEntry) String() string {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e *rootEntry) sigHash() []byte {
|
func (e *rootEntry) sigHash() []byte {
|
||||||
h := sha3.NewLegacyKeccak256()
|
prefix := fmt.Sprintf(rootPrefix+" e=%s l=%s seq=%d", e.eroot, e.lroot, e.seq)
|
||||||
fmt.Fprintf(h, rootPrefix+" e=%s l=%s seq=%d", e.eroot, e.lroot, e.seq)
|
return crypto.Keccak256([]byte(prefix))
|
||||||
return h.Sum(nil)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e *rootEntry) verifySignature(pubkey *ecdsa.PublicKey) bool {
|
func (e *rootEntry) verifySignature(pubkey *ecdsa.PublicKey) bool {
|
||||||
|
|
|
||||||
|
|
@ -25,7 +25,6 @@ import (
|
||||||
"github.com/ethereum/go-ethereum/crypto"
|
"github.com/ethereum/go-ethereum/crypto"
|
||||||
"github.com/ethereum/go-ethereum/p2p/enr"
|
"github.com/ethereum/go-ethereum/p2p/enr"
|
||||||
"github.com/ethereum/go-ethereum/rlp"
|
"github.com/ethereum/go-ethereum/rlp"
|
||||||
"golang.org/x/crypto/sha3"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// ValidSchemes is a List of known secure identity schemes.
|
// ValidSchemes is a List of known secure identity schemes.
|
||||||
|
|
@ -49,9 +48,8 @@ func SignV4(r *enr.Record, privkey *ecdsa.PrivateKey) error {
|
||||||
cpy.Set(enr.ID("v4"))
|
cpy.Set(enr.ID("v4"))
|
||||||
cpy.Set(Secp256k1(privkey.PublicKey))
|
cpy.Set(Secp256k1(privkey.PublicKey))
|
||||||
|
|
||||||
h := sha3.NewLegacyKeccak256()
|
digestHash := crypto.Keccak256RLP(cpy.AppendElements(nil))
|
||||||
rlp.Encode(h, cpy.AppendElements(nil))
|
sig, err := crypto.Sign(digestHash, privkey)
|
||||||
sig, err := crypto.Sign(h.Sum(nil), privkey)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
@ -70,9 +68,8 @@ func (V4ID) Verify(r *enr.Record, sig []byte) error {
|
||||||
return errors.New("invalid public key")
|
return errors.New("invalid public key")
|
||||||
}
|
}
|
||||||
|
|
||||||
h := sha3.NewLegacyKeccak256()
|
digestHash := crypto.Keccak256RLP(r.AppendElements(nil))
|
||||||
rlp.Encode(h, r.AppendElements(nil))
|
if !crypto.VerifySignature(entry, digestHash, sig) {
|
||||||
if !crypto.VerifySignature(entry, h.Sum(nil), sig) {
|
|
||||||
return enr.ErrInvalidSig
|
return enr.ErrInvalidSig
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue