update to use VRF digest appropriately and simplify drawing rand past genesis

This commit is contained in:
Henri S 2020-02-25 21:47:42 -08:00
parent 1b6646b55a
commit cf0a3af1f8
3 changed files with 13 additions and 20 deletions

View File

@ -890,12 +890,12 @@ func (cs *ChainStore) TryFillTipSet(ts *types.TipSet) (*FullTipSet, error) {
}
func drawRandomness(t *types.Ticket, pers crypto.DomainSeparationTag, round int64, entropy []byte) ([]byte, error) {
// TODO: Make this spec compliant
h := blake2b.New256()
if err := binary.Write(h, binary.BigEndian, int64(pers)); err != nil {
return nil, xerrors.Errorf("deriving randomness: %w", err)
}
h.Write(t.VRFProof)
VRFDigest := blake2b.Sum256(t.VRFProof)
h.Write(VRFDigest[:])
if err := binary.Write(h, binary.BigEndian, round); err != nil {
return nil, xerrors.Errorf("deriving randomness: %w", err)
}
@ -921,25 +921,12 @@ func (cs *ChainStore) GetRandomness(ctx context.Context, blks []cid.Cid, pers cr
mtb := nts.MinTicketBlock()
if int64(nts.Height()) <= round {
// if at (or just past -- for null epochs) appropriate epoch
// or at genesis (works for negative epochs)
if int64(nts.Height()) <= round || mtb.Height == 0 {
return drawRandomness(nts.MinTicketBlock().Ticket, pers, round, entropy)
}
// special case for lookback behind genesis block
// TODO(spec): this is not in the spec, need to sync that
if mtb.Height == 0 {
// round is negative
thash, err := drawRandomness(mtb.Ticket, pers, round*-1, entropy)
if err != nil {
return nil, err
}
// for negative lookbacks, just use the hash of the positive tickethash value
h := blake2b.Sum256(thash)
return h[:], nil
}
blks = mtb.Parents
}
}

View File

@ -9,6 +9,7 @@ import (
block "github.com/ipfs/go-block-format"
"github.com/ipfs/go-cid"
"github.com/minio/blake2b-simd"
"github.com/minio/sha256-simd"
"github.com/multiformats/go-multihash"
xerrors "golang.org/x/xerrors"
@ -214,5 +215,7 @@ func ElectionPostChallengeCount(sectors uint64, faults uint64) uint64 {
}
func (t *Ticket) Equals(ot *Ticket) bool {
return bytes.Equal(t.VRFProof, ot.VRFProof)
tDigest := blake2b.Sum256(t.VRFProof)
otDigest := blake2b.Sum256(ot.VRFProof)
return bytes.Equal(tDigest[:], otDigest[:])
}

View File

@ -10,6 +10,7 @@ import (
"github.com/filecoin-project/specs-actors/actors/abi"
"github.com/ipfs/go-cid"
logging "github.com/ipfs/go-log/v2"
"github.com/minio/blake2b-simd"
cbg "github.com/whyrusleeping/cbor-gen"
"golang.org/x/xerrors"
)
@ -170,7 +171,9 @@ func (ts *TipSet) Equals(ots *TipSet) bool {
}
func (t *Ticket) Less(o *Ticket) bool {
return bytes.Compare(t.VRFProof, o.VRFProof) < 0
tDigest := blake2b.Sum256(t.VRFProof)
oDigest := blake2b.Sum256(o.VRFProof)
return bytes.Compare(tDigest[:], oDigest[:]) < 0
}
func (ts *TipSet) MinTicket() *Ticket {