[shard] singleflight on committees bls public keys (#2546)

pull/2556/head
Edgar Aroutiounian 5 years ago committed by GitHub
parent 4d25de025c
commit 49eca7903a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 56
      shard/shard_state.go

@ -7,14 +7,17 @@ import (
"errors" "errors"
"math/big" "math/big"
"sort" "sort"
"time"
"github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/rlp" "github.com/ethereum/go-ethereum/rlp"
"github.com/harmony-one/bls/ffi/go/bls" "github.com/harmony-one/bls/ffi/go/bls"
"github.com/harmony-one/harmony/crypto/hash"
common2 "github.com/harmony-one/harmony/internal/common" common2 "github.com/harmony-one/harmony/internal/common"
"github.com/harmony-one/harmony/internal/ctxerror" "github.com/harmony-one/harmony/internal/ctxerror"
"github.com/harmony-one/harmony/numeric" "github.com/harmony-one/harmony/numeric"
"golang.org/x/crypto/sha3" "golang.org/x/crypto/sha3"
"golang.org/x/sync/singleflight"
) )
var ( var (
@ -382,23 +385,52 @@ func (c *Committee) DeepCopy() Committee {
return r return r
} }
// Hash ..
func (c *Committee) Hash() common.Hash {
return hash.FromRLPNew256(c)
}
var (
blsKeyCache singleflight.Group
)
func lookupBLSPublicKeys(
c *Committee,
) ([]*bls.PublicKey, error) {
key := c.Hash().Hex()
results, err, _ := blsKeyCache.Do(
key, func() (interface{}, error) {
slice := make([]*bls.PublicKey, len(c.Slots))
for j := range c.Slots {
committerKey := &bls.PublicKey{}
if err := c.Slots[j].BlsPublicKey.ToLibBLSPublicKey(
committerKey,
); err != nil {
return nil, err
}
slice[j] = committerKey
}
// Only made once
go func() {
time.Sleep(25 * time.Minute)
blsKeyCache.Forget(key)
}()
return slice, nil
},
)
if err != nil {
return nil, err
}
return results.([]*bls.PublicKey), nil
}
// BLSPublicKeys .. // BLSPublicKeys ..
func (c *Committee) BLSPublicKeys() ([]*bls.PublicKey, error) { func (c *Committee) BLSPublicKeys() ([]*bls.PublicKey, error) {
if c == nil { if c == nil {
return nil, ErrSubCommitteeNil return nil, ErrSubCommitteeNil
} }
return lookupBLSPublicKeys(c)
slice := make([]*bls.PublicKey, len(c.Slots))
for j := range c.Slots {
committerKey := &bls.PublicKey{}
if err := c.Slots[j].BlsPublicKey.ToLibBLSPublicKey(
committerKey,
); err != nil {
return nil, err
}
slice[j] = committerKey
}
return slice, nil
} }
var ( var (

Loading…
Cancel
Save