fix target for ePoSt IsTicketWinner fn
License: MIT Signed-off-by: Jakub Sztandera <kubuxu@protocol.ai>
This commit is contained in:
parent
0de9991dcd
commit
9e363f9266
@ -526,9 +526,11 @@ func IsRoundWinner(ctx context.Context, ts *types.TipSet, round int64, miner add
|
|||||||
return nil, xerrors.Errorf("failed to look up miners sector size: %w", err)
|
return nil, xerrors.Errorf("failed to look up miners sector size: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
snum := types.BigDiv(pow.MinerPower, types.NewInt(ssize))
|
||||||
|
|
||||||
var winners []sectorbuilder.EPostCandidate
|
var winners []sectorbuilder.EPostCandidate
|
||||||
for _, c := range candidates {
|
for _, c := range candidates {
|
||||||
if types.IsTicketWinner(c.PartialTicket[:], ssize, pow.TotalPower) {
|
if types.IsTicketWinner(c.PartialTicket[:], ssize, snum.Uint64(), pow.TotalPower) {
|
||||||
winners = append(winners, c)
|
winners = append(winners, c)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -501,7 +501,7 @@ func (syncer *Syncer) ValidateBlock(ctx context.Context, b *types.FullBlock) err
|
|||||||
return xerrors.Errorf("received block was from miner slashed at height %d", slashedAt)
|
return xerrors.Errorf("received block was from miner slashed at height %d", slashedAt)
|
||||||
}
|
}
|
||||||
|
|
||||||
_, tpow, err := stmgr.GetPower(ctx, syncer.sm, baseTs, h.Miner)
|
mpow, tpow, err := stmgr.GetPower(ctx, syncer.sm, baseTs, h.Miner)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return xerrors.Errorf("failed getting power: %w", err)
|
return xerrors.Errorf("failed getting power: %w", err)
|
||||||
}
|
}
|
||||||
@ -511,8 +511,10 @@ func (syncer *Syncer) ValidateBlock(ctx context.Context, b *types.FullBlock) err
|
|||||||
return xerrors.Errorf("failed to get sector size for block miner: %w", err)
|
return xerrors.Errorf("failed to get sector size for block miner: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
snum := types.BigDiv(mpow, types.NewInt(ssize))
|
||||||
|
|
||||||
for _, t := range h.EPostProof.Candidates {
|
for _, t := range h.EPostProof.Candidates {
|
||||||
if !types.IsTicketWinner(t.Partial, ssize, tpow) {
|
if !types.IsTicketWinner(t.Partial, ssize, snum.Uint64(), tpow) {
|
||||||
return xerrors.Errorf("miner created a block but was not a winner")
|
return xerrors.Errorf("miner created a block but was not a winner")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -174,9 +174,9 @@ var blocksPerEpoch = NewInt(build.BlocksPerEpoch)
|
|||||||
|
|
||||||
const sha256bits = 256
|
const sha256bits = 256
|
||||||
|
|
||||||
func IsTicketWinner(partialTicket []byte, ssizeI uint64, totpow BigInt) bool {
|
func IsTicketWinner(partialTicket []byte, ssizeI uint64, snum uint64, totpow BigInt) bool {
|
||||||
ssize := NewInt(ssizeI)
|
ssize := NewInt(ssizeI)
|
||||||
|
ssampled := ElectionPostChallengeCount(snum)
|
||||||
/*
|
/*
|
||||||
Need to check that
|
Need to check that
|
||||||
(h(vrfout) + 1) / (max(h) + 1) <= e * sectorSize / totalPower
|
(h(vrfout) + 1) / (max(h) + 1) <= e * sectorSize / totalPower
|
||||||
@ -185,23 +185,39 @@ func IsTicketWinner(partialTicket []byte, ssizeI uint64, totpow BigInt) bool {
|
|||||||
(h(vrfout) + 1) * totalPower <= e * sectorSize * 2^256
|
(h(vrfout) + 1) * totalPower <= e * sectorSize * 2^256
|
||||||
in 2^256 space, it is equivalent to:
|
in 2^256 space, it is equivalent to:
|
||||||
h(vrfout) * totalPower < e * sectorSize * 2^256
|
h(vrfout) * totalPower < e * sectorSize * 2^256
|
||||||
|
|
||||||
|
Because of SectorChallengeRatioDiv sampling for proofs
|
||||||
|
we need to scale this appropriately.
|
||||||
|
|
||||||
|
Let c = ceil(numSectors/SectorChallengeRatioDiv)
|
||||||
|
(c is the number of tickets a miner requests)
|
||||||
|
Accordingly we check
|
||||||
|
(h(vrfout) + 1) / 2^256 <= e * sectorSize / totalPower * snum / c
|
||||||
|
or
|
||||||
|
h(vrfout) * totalPower * c < e * sectorSize * 2^256 * snum
|
||||||
*/
|
*/
|
||||||
|
|
||||||
h := sha256.Sum256(partialTicket)
|
h := sha256.Sum256(partialTicket)
|
||||||
|
|
||||||
lhs := BigFromBytes(h[:]).Int
|
lhs := BigFromBytes(h[:]).Int
|
||||||
lhs = lhs.Mul(lhs, totpow.Int)
|
lhs = lhs.Mul(lhs, totpow.Int)
|
||||||
|
lhs = lhs.Mul(lhs, new(big.Int).SetUint64(ssampled))
|
||||||
|
|
||||||
// rhs = sectorSize * 2^256
|
// rhs = sectorSize * 2^256
|
||||||
// rhs = sectorSize << 256
|
// rhs = sectorSize << 256
|
||||||
rhs := new(big.Int).Lsh(ssize.Int, sha256bits)
|
rhs := new(big.Int).Lsh(ssize.Int, sha256bits)
|
||||||
rhs = rhs.Mul(rhs, big.NewInt(build.SectorChallengeRatioDiv))
|
rhs = rhs.Mul(rhs, new(big.Int).SetUint64(snum))
|
||||||
rhs = rhs.Mul(rhs, blocksPerEpoch.Int)
|
rhs = rhs.Mul(rhs, blocksPerEpoch.Int)
|
||||||
|
|
||||||
// h(vrfout) * totalPower < e * sectorSize * 2^256?
|
// h(vrfout) * totalPower < e * sectorSize * 2^256?
|
||||||
return lhs.Cmp(rhs) < 0
|
return lhs.Cmp(rhs) < 0
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func ElectionPostChallengeCount(sectors uint64) uint64 {
|
||||||
|
// ceil(sectors / build.SectorChallengeRatioDiv)
|
||||||
|
return (sectors + build.SectorChallengeRatioDiv - 1) / build.SectorChallengeRatioDiv
|
||||||
|
}
|
||||||
|
|
||||||
func (t *Ticket) Equals(ot *Ticket) bool {
|
func (t *Ticket) Equals(ot *Ticket) bool {
|
||||||
return bytes.Equal(t.VRFProof, ot.VRFProof)
|
return bytes.Equal(t.VRFProof, ot.VRFProof)
|
||||||
}
|
}
|
||||||
|
@ -16,6 +16,7 @@ import (
|
|||||||
|
|
||||||
"github.com/filecoin-project/lotus/build"
|
"github.com/filecoin-project/lotus/build"
|
||||||
"github.com/filecoin-project/lotus/chain/address"
|
"github.com/filecoin-project/lotus/chain/address"
|
||||||
|
"github.com/filecoin-project/lotus/chain/types"
|
||||||
"github.com/filecoin-project/lotus/node/modules/dtypes"
|
"github.com/filecoin-project/lotus/node/modules/dtypes"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -623,7 +624,7 @@ func (sb *SectorBuilder) GenerateEPostCandidates(sectorInfo SortedPublicSectorIn
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
challengeCount := ElectionPostChallengeCount(uint64(len(sectorInfo.Values())))
|
challengeCount := types.ElectionPostChallengeCount(uint64(len(sectorInfo.Values())))
|
||||||
|
|
||||||
proverID := addressToProverID(sb.Miner)
|
proverID := addressToProverID(sb.Miner)
|
||||||
return sectorbuilder.GenerateCandidates(sb.ssize, proverID, challengeSeed, challengeCount, privsectors)
|
return sectorbuilder.GenerateCandidates(sb.ssize, proverID, challengeSeed, challengeCount, privsectors)
|
||||||
@ -674,13 +675,8 @@ func (sb *SectorBuilder) Stop() {
|
|||||||
close(sb.stopping)
|
close(sb.stopping)
|
||||||
}
|
}
|
||||||
|
|
||||||
func ElectionPostChallengeCount(sectors uint64) uint64 {
|
|
||||||
// ceil(sectors / build.SectorChallengeRatioDiv)
|
|
||||||
return (sectors + build.SectorChallengeRatioDiv - 1) / build.SectorChallengeRatioDiv
|
|
||||||
}
|
|
||||||
|
|
||||||
func fallbackPostChallengeCount(sectors uint64) uint64 {
|
func fallbackPostChallengeCount(sectors uint64) uint64 {
|
||||||
challengeCount := ElectionPostChallengeCount(sectors)
|
challengeCount := types.ElectionPostChallengeCount(sectors)
|
||||||
if challengeCount > build.MaxFallbackPostChallengeCount {
|
if challengeCount > build.MaxFallbackPostChallengeCount {
|
||||||
return build.MaxFallbackPostChallengeCount
|
return build.MaxFallbackPostChallengeCount
|
||||||
}
|
}
|
||||||
|
@ -8,6 +8,7 @@ import (
|
|||||||
"go.opencensus.io/trace"
|
"go.opencensus.io/trace"
|
||||||
|
|
||||||
"github.com/filecoin-project/lotus/chain/address"
|
"github.com/filecoin-project/lotus/chain/address"
|
||||||
|
"github.com/filecoin-project/lotus/chain/types"
|
||||||
)
|
)
|
||||||
|
|
||||||
func (sb *SectorBuilder) SectorSize() uint64 {
|
func (sb *SectorBuilder) SectorSize() uint64 {
|
||||||
@ -36,7 +37,7 @@ func NewSortedPublicSectorInfo(sectors []sectorbuilder.PublicSectorInfo) SortedP
|
|||||||
}
|
}
|
||||||
|
|
||||||
func VerifyElectionPost(ctx context.Context, sectorSize uint64, sectorInfo SortedPublicSectorInfo, challengeSeed []byte, proof []byte, candidates []EPostCandidate, proverID address.Address) (bool, error) {
|
func VerifyElectionPost(ctx context.Context, sectorSize uint64, sectorInfo SortedPublicSectorInfo, challengeSeed []byte, proof []byte, candidates []EPostCandidate, proverID address.Address) (bool, error) {
|
||||||
challengeCount := ElectionPostChallengeCount(uint64(len(sectorInfo.Values())))
|
challengeCount := types.ElectionPostChallengeCount(uint64(len(sectorInfo.Values())))
|
||||||
return verifyPost(ctx, sectorSize, sectorInfo, challengeCount, challengeSeed, proof, candidates, proverID)
|
return verifyPost(ctx, sectorSize, sectorInfo, challengeCount, challengeSeed, proof, candidates, proverID)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user