Skip to content

Commit

Permalink
Try #6044:
Browse files Browse the repository at this point in the history
  • Loading branch information
spacemesh-bors[bot] authored Jun 14, 2024
2 parents 7efcf58 + a0aabf3 commit 492f132
Show file tree
Hide file tree
Showing 15 changed files with 107 additions and 32 deletions.
4 changes: 3 additions & 1 deletion activation/wire/wire_v1.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,9 +132,11 @@ func (atx *ActivationTxV1) SignedBytes() []byte {
}

func (atx *ActivationTxV1) HashInnerBytes() (result types.Hash32) {
h := hash.New()
h := hash.GetHasher()
codec.MustEncodeTo(h, &atx.InnerActivationTxV1)
h.Sum(result[:0])
h.Reset()
hash.PutHasher(h)
return result
}

Expand Down
6 changes: 5 additions & 1 deletion beacon/proposal_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,11 @@ func (hl proposalList) sort() []Proposal {
}

func (hl proposalList) hash() types.Hash32 {
hasher := hash.New()
hasher := hash.GetHasher()
defer func() {
hasher.Reset()
hash.PutHasher(hasher)
}()

for _, proposal := range hl {
// an error is never returned: https://golang.org/pkg/hash/#Hash
Expand Down
4 changes: 3 additions & 1 deletion common/types/atxid_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ type ATXIDList []ATXID

// Hash returns ATX ID list hash.
func (atxList ATXIDList) Hash() Hash32 {
hasher := hash.New()
hasher := hash.GetHasher()

for _, id := range atxList {
if _, err := hasher.Write(id.Bytes()); err != nil {
Expand All @@ -17,5 +17,7 @@ func (atxList ATXIDList) Hash() Hash32 {

var rst Hash32
hasher.Sum(rst[:0])
hasher.Reset()
hash.PutHasher(hasher)
return rst
}
7 changes: 5 additions & 2 deletions common/types/ballot.go
Original file line number Diff line number Diff line change
Expand Up @@ -273,12 +273,15 @@ func (b *Ballot) SignedBytes() []byte {

// HashInnerBytes returns the hash of the InnerBallot.
func (b *Ballot) HashInnerBytes() []byte {
h := hash.New()
h := hash.GetHasher()
_, err := codec.EncodeTo(h, &b.InnerBallot)
if err != nil {
log.With().Fatal("failed to encode InnerBallot for hashing", log.Err(err))
}
return h.Sum(nil)
sum := h.Sum(nil)
h.Reset()
hash.PutHasher(h)
return sum
}

// SetID from stored data.
Expand Down
4 changes: 3 additions & 1 deletion common/types/hashes.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,13 +101,15 @@ func CalcProposalsHash32(view []ProposalID, additionalBytes []byte) Hash32 {
// CalcProposalHash32Presorted returns the 32-byte blake3 sum of the IDs, in the order given. The pre-image is
// prefixed with additionalBytes.
func CalcProposalHash32Presorted(sortedView []ProposalID, additionalBytes []byte) Hash32 {
hasher := hash.New()
hasher := hash.GetHasher()
hasher.Write(additionalBytes)
for _, id := range sortedView {
hasher.Write(id.Bytes()) // this never returns an error: https://golang.org/pkg/hash/#Hash
}
var res Hash32
hasher.Sum(res[:0])
hasher.Reset()
hash.PutHasher(hasher)
return res
}

Expand Down
7 changes: 5 additions & 2 deletions common/types/proposal.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,12 +107,15 @@ func (p *Proposal) SignedBytes() []byte {

// HashInnerProposal returns the hash of the InnerProposal.
func (p *Proposal) HashInnerProposal() []byte {
h := hash.New()
h := hash.GetHasher()
_, err := codec.EncodeTo(h, &p.InnerProposal)
if err != nil {
log.With().Fatal("failed to encode InnerProposal for hashing", log.Err(err))
}
return h.Sum(nil)
sum := h.Sum(nil)
h.Reset()
hash.PutHasher(h)
return sum
}

// ID returns the ProposalID.
Expand Down
8 changes: 6 additions & 2 deletions config/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,18 @@ func (g *GenesisConfig) GenesisID() types.Hash20 {
}

func (g *GenesisConfig) GoldenATX() types.Hash32 {
hh := hash.New()
hh := hash.GetHasher()
parsed, err := time.Parse(time.RFC3339, g.GenesisTime)
if err != nil {
panic("code should have run Validate before this method")
}
hh.Write([]byte(strconv.FormatInt(parsed.Unix(), 10)))
hh.Write([]byte(g.ExtraData))
return types.BytesToHash(hh.Sum(nil))
sum := types.BytesToHash(hh.Sum(nil))

hh.Reset()
hash.PutHasher(hh)
return sum
}

// Validate GenesisConfig.
Expand Down
11 changes: 8 additions & 3 deletions eligibility/fixedoracle.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ func (fo *FixedRolacle) generateEligibility(ctx context.Context, expCom int) map
func hashLayerAndRound(logger log.Log, instanceID types.LayerID, round uint32) types.Hash32 {
kInBytes := make([]byte, 4)
binary.LittleEndian.PutUint32(kInBytes, round)
h := hash.New()
h := hash.GetHasher()
enc := scale.NewEncoder(h)
_, err := instanceID.EncodeScale(enc)
_, err2 := h.Write(kInBytes)
Expand All @@ -183,7 +183,10 @@ func hashLayerAndRound(logger log.Log, instanceID types.LayerID, round uint32) t
log.FieldNamed("err1", log.Err(err)),
log.FieldNamed("err2", log.Err(err2)))
}
return types.BytesToHash(h.Sum([]byte{}))
sum := types.BytesToHash(h.Sum([]byte{}))
h.Reset()
hash.PutHasher(h)
return sum
}

// Validate is required to conform to the Rolacle interface, but should never be called.
Expand Down Expand Up @@ -260,11 +263,13 @@ func (fo *FixedRolacle) Proof(
) (types.VrfSignature, error) {
kInBytes := make([]byte, 4)
binary.LittleEndian.PutUint32(kInBytes, round)
h := hash.New()
h := hash.GetHasher()
if _, err := h.Write(kInBytes); err != nil {
fo.logger.WithContext(ctx).With().Error("error writing hash", log.Err(err))
}
var proof types.VrfSignature
_, err := h.Digest().Read(proof[:])
h.Reset()
hash.PutHasher(h)
return proof, err
}
8 changes: 5 additions & 3 deletions genvm/core/hash.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,13 @@ func SigningBody(genesis, tx []byte) []byte {

// ComputePrincipal address as the last 20 bytes from blake3(scale(template || args)).
func ComputePrincipal(template Address, args scale.Encodable) Address {
hasher := hash.New()
hasher := hash.GetHasher()
encoder := scale.NewEncoder(hasher)
template.EncodeScale(encoder)
args.EncodeScale(encoder)
hash := hasher.Sum(nil)
rst := types.GenerateAddress(hash[12:])
sum := hasher.Sum(nil)
hasher.Reset()
hash.PutHasher(hasher)
rst := types.GenerateAddress(sum[12:])
return rst
}
12 changes: 7 additions & 5 deletions genvm/vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ func (v *VM) Apply(
t3 := time.Now()
blockDurationRewards.Observe(float64(time.Since(t2)))

hasher := hash.New()
hasher := hash.GetHasher()
encoder := scale.NewEncoder(hasher)
total := 0

Expand Down Expand Up @@ -249,9 +249,9 @@ func (v *VM) Apply(
}
writesPerBlock.Observe(float64(total))

var hash types.Hash32
hasher.Sum(hash[:0])
if err := layers.UpdateStateHash(tx, lctx.Layer, hash); err != nil {
var hashSum types.Hash32
hasher.Sum(hashSum[:0])
if err := layers.UpdateStateHash(tx, lctx.Layer, hashSum); err != nil {
return nil, nil, err
}
if err := tx.Commit(); err != nil {
Expand All @@ -264,6 +264,8 @@ func (v *VM) Apply(
for _, reward := range rewardsResult {
events.ReportRewardReceived(reward)
}
hasher.Reset()
hash.PutHasher(hasher)

blockDurationPersist.Observe(float64(time.Since(t3)))
blockDuration.Observe(float64(time.Since(t1)))
Expand All @@ -274,7 +276,7 @@ func (v *VM) Apply(
log.Uint32("layer", lctx.Layer.Uint32()),
log.Int("count", len(txs)-len(skipped)),
log.Duration("duration", time.Since(t1)),
log.Stringer("state_hash", hash),
log.Stringer("state_hash", hashSum),
)
return skipped, results, nil
}
Expand Down
8 changes: 5 additions & 3 deletions hare3/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,10 +108,12 @@ type Message struct {
}

func (m *Message) ToHash() types.Hash32 {
hash := hash.New()
codec.MustEncodeTo(hash, &m.Body)
h := hash.GetHasher()
codec.MustEncodeTo(h, &m.Body)
var rst types.Hash32
hash.Sum(rst[:0])
h.Sum(rst[:0])
h.Reset()
hash.PutHasher(h)
return rst
}

Expand Down
12 changes: 10 additions & 2 deletions hash/hash.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,27 @@ var New = blake3.New

// Sum computes 256-bit hash from chunks with blake3.
func Sum(chunks ...[]byte) (rst [32]byte) {
hh := New()
hh := GetHasher()
for _, chunk := range chunks {
hh.Write(chunk)
}
hh.Sum(rst[:0])

// reset the hasher and put it back in the pool
hh.Reset()
PutHasher(hh)
return rst
}

func Sum20(chunks ...[]byte) (rst [20]byte) {
hh := New()
hh := GetHasher()
for _, chunk := range chunks {
hh.Write(chunk)
}
hh.Digest().Read(rst[:])

// reset the hasher and put it back in the pool
hh.Reset()
PutHasher(hh)
return rst
}
30 changes: 30 additions & 0 deletions hash/pool.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package hash

import (
"sync"

"github.com/zeebo/blake3"
)

// Pool is a global blake3 hasher pool. It is meant to amortize allocations
// of blake3 hashers over time by allowing clients to reuse them.
var pool = &sync.Pool{
New: func() any {
return blake3.New()
},
}

// GetHasher will get a blake3 hasher from the pool.
// It may or may not allocate a new one. Consumers are expected
// to call Reset() on the hasher before putting it back in
// the pool.
func GetHasher() *blake3.Hasher {
return pool.Get().(*blake3.Hasher)
}

// PutHasher returns the hasher back to the pool.
// Consumers are expected to call Reset() on the
// instance before putting it back in the pool.
func PutHasher(hasher *blake3.Hasher) {
pool.Put(hasher)
}
7 changes: 5 additions & 2 deletions p2p/pubsub/pubsub.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,12 +204,15 @@ func DropPeerOnSyncValidationReject(handler SyncHandler, h host.Host, logger log
}

func msgID(msg *pubsubpb.Message) string {
hasher := hash.New()
hasher := hash.GetHasher()
if msg.Topic != nil {
hasher.Write([]byte(*msg.Topic))
}
hasher.Write(msg.Data)
return string(hasher.Sum(nil))
sum := string(hasher.Sum(nil))
hasher.Reset()
hash.PutHasher(hasher)
return sum
}

func getOptions(cfg Config) []pubsub.Option {
Expand Down
11 changes: 7 additions & 4 deletions txs/nano_tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,13 @@ func (n *NanoTX) MaxSpending() uint64 {
}

func (n *NanoTX) combinedHash(blockSeed []byte) []byte {
hash := hash.New()
hash.Write(blockSeed)
hash.Write(n.ID.Bytes())
return hash.Sum(nil)
h := hash.GetHasher()
h.Write(blockSeed)
h.Write(n.ID.Bytes())
sum := h.Sum(nil)
h.Reset()
hash.PutHasher(h)
return sum
}

// Better returns true if this transaction takes priority than `other`.
Expand Down

0 comments on commit 492f132

Please sign in to comment.