WIP: working on winning post integration

This commit is contained in:
Jeromy 2020-04-16 22:39:55 -07:00
parent 4153f128ef
commit 708988244d
8 changed files with 85 additions and 104 deletions

View File

@ -394,4 +394,5 @@ type BlockTemplate struct {
Messages []*types.SignedMessage Messages []*types.SignedMessage
Epoch abi.ChainEpoch Epoch abi.ChainEpoch
Timestamp uint64 Timestamp uint64
WinningPoStProof []abi.PoStProof
} }

View File

@ -324,8 +324,6 @@ func (cg *ChainGen) nextBlockProof(ctx context.Context, pts *types.TipSet, m add
return nil, nil, nil, xerrors.Errorf("compute VRF: %w", err) return nil, nil, nil, xerrors.Errorf("compute VRF: %w", err)
} }
// TODO winning post return?
return entries, eproof, &types.Ticket{VRFProof: vrfout}, nil return entries, eproof, &types.Ticket{VRFProof: vrfout}, nil
} }
@ -502,7 +500,7 @@ func (mca mca) WalletSign(ctx context.Context, a address.Address, v []byte) (*cr
type WinningPoStProver interface { type WinningPoStProver interface {
GenerateCandidates(context.Context, abi.PoStRandomness, uint64) ([]uint64, error) GenerateCandidates(context.Context, abi.PoStRandomness, uint64) ([]uint64, error)
ComputeProof(context.Context, []abi.SectorInfo, []byte) ([]abi.PoStProof, error) ComputeProof(context.Context, []abi.SectorInfo, abi.PoStRandomness) ([]abi.PoStProof, error)
} }
type wppProvider struct{} type wppProvider struct{}
@ -511,7 +509,7 @@ func (wpp *wppProvider) GenerateCandidates(ctx context.Context, _ abi.PoStRandom
return []uint64{0}, nil return []uint64{0}, nil
} }
func (wpp *wppProvider) ComputeProof(context.Context, []abi.SectorInfo, []byte) ([]abi.PoStProof, error) { func (wpp *wppProvider) ComputeProof(context.Context, []abi.SectorInfo, abi.PoStRandomness) ([]abi.PoStProof, error) {
return []abi.PoStProof{{ return []abi.PoStProof{{
ProofBytes: []byte("valid proof"), ProofBytes: []byte("valid proof"),
}}, nil }}, nil

View File

@ -130,6 +130,10 @@ func GetMinerSectorSet(ctx context.Context, sm *StateManager, ts *types.TipSet,
return LoadSectorsFromSet(ctx, sm.ChainStore().Blockstore(), mas.Sectors, filter) return LoadSectorsFromSet(ctx, sm.ChainStore().Blockstore(), mas.Sectors, filter)
} }
func GetSectorsForWinningPoSt(ctx context.Context, sm *StateManager, ts *types.TipSet, maddr address.Address) ([]*api.ChainSectorInfo, error) {
panic("TODO")
}
func StateMinerInfo(ctx context.Context, sm *StateManager, ts *types.TipSet, maddr address.Address) (miner.MinerInfo, error) { func StateMinerInfo(ctx context.Context, sm *StateManager, ts *types.TipSet, maddr address.Address) (miner.MinerInfo, error) {
var mas miner.State var mas miner.State
_, err := sm.LoadActorStateRaw(ctx, maddr, &mas, ts.ParentState()) _, err := sm.LoadActorStateRaw(ctx, maddr, &mas, ts.ParentState())

View File

@ -27,6 +27,8 @@ import (
bls "github.com/filecoin-project/filecoin-ffi" bls "github.com/filecoin-project/filecoin-ffi"
"github.com/filecoin-project/go-address" "github.com/filecoin-project/go-address"
amt "github.com/filecoin-project/go-amt-ipld/v2" amt "github.com/filecoin-project/go-amt-ipld/v2"
"github.com/filecoin-project/sector-storage/ffiwrapper"
"github.com/filecoin-project/specs-actors/actors/abi"
"github.com/filecoin-project/specs-actors/actors/builtin" "github.com/filecoin-project/specs-actors/actors/builtin"
"github.com/filecoin-project/specs-actors/actors/builtin/power" "github.com/filecoin-project/specs-actors/actors/builtin/power"
"github.com/filecoin-project/specs-actors/actors/crypto" "github.com/filecoin-project/specs-actors/actors/crypto"
@ -661,19 +663,19 @@ func (syncer *Syncer) ValidateBlock(ctx context.Context, b *types.FullBlock) err
return nil return nil
}) })
//eproofCheck := async.Err(func() error { wproofCheck := async.Err(func() error {
//if err := syncer.VerifyElectionPoStProof(ctx, h, waddr); err != nil { if err := syncer.VerifyWinningPoStProof(ctx, h, waddr); err != nil {
//return xerrors.Errorf("invalid election post: %w", err) return xerrors.Errorf("invalid election post: %w", err)
//} }
//return nil return nil
//}) })
await := []async.ErrorFuture{ await := []async.ErrorFuture{
minerCheck, minerCheck,
tktsCheck, tktsCheck,
blockSigCheck, blockSigCheck,
beaconValuesCheck, beaconValuesCheck,
//eproofCheck, wproofCheck,
winnerCheck, winnerCheck,
msgsCheck, msgsCheck,
} }
@ -705,109 +707,61 @@ func (syncer *Syncer) ValidateBlock(ctx context.Context, b *types.FullBlock) err
return merr return merr
} }
/* func (syncer *Syncer) VerifyWinningPoStProof(ctx context.Context, h *types.BlockHeader, waddr address.Address) error {
func (syncer *Syncer) VerifyElectionPoStProof(ctx context.Context, h *types.BlockHeader, waddr address.Address) error { if build.InsecurePoStValidation {
if len(h.WinPoStProof) == 0 {
return xerrors.Errorf("[TESTING] No election post proof given")
}
if string(h.WinPoStProof[0].ProofBytes) == "valid proof" {
return nil
}
return xerrors.Errorf("[TESTING] election post was invalid")
}
curTs, err := types.NewTipSet([]*types.BlockHeader{h}) curTs, err := types.NewTipSet([]*types.BlockHeader{h})
if err != nil { if err != nil {
return err return err
} }
buf := new(bytes.Buffer) // TODO: use proper DST
if err := h.Miner.MarshalCBOR(buf); err != nil { rand, err := syncer.sm.ChainStore().GetRandomness(ctx, curTs.Cids(), crypto.DomainSeparationTag_ElectionPoStChallengeSeed, h.Height-1, nil)
return xerrors.Errorf("failed to marshal miner to cbor: %w", err)
}
rand, err := syncer.sm.ChainStore().GetRandomness(ctx, curTs.Cids(), crypto.DomainSeparationTag_ElectionPoStChallengeSeed, h.Height-build.EcRandomnessLookback, buf.Bytes())
if err != nil { if err != nil {
return xerrors.Errorf("failed to get randomness for verifying election proof: %w", err) return xerrors.Errorf("failed to get randomness for verifying election proof: %w", err)
} }
if err := VerifyElectionPoStVRF(ctx, h.EPostProof.PostRand, rand, waddr); err != nil {
return xerrors.Errorf("checking eproof failed: %w", err)
}
ssize, err := stmgr.GetMinerSectorSize(ctx, syncer.sm, curTs, h.Miner)
if err != nil {
return xerrors.Errorf("failed to get sector size for miner: %w", err)
}
mid, err := address.IDFromAddress(h.Miner) mid, err := address.IDFromAddress(h.Miner)
if err != nil { if err != nil {
return xerrors.Errorf("failed to get ID from miner address %s: %w", h.Miner, err) return xerrors.Errorf("failed to get ID from miner address %s: %w", h.Miner, err)
} }
var winners []abi.PoStCandidate sectors, err := stmgr.GetSectorsForWinningPoSt(ctx, syncer.sm, curTs, h.Miner)
for _, t := range h.EPostProof.Candidates {
winners = append(winners, abi.PoStCandidate{
PartialTicket: t.Partial,
SectorID: abi.SectorID{
Number: t.SectorID,
Miner: abi.ActorID(mid),
},
ChallengeIndex: int64(t.ChallengeIndex),
})
}
if len(winners) == 0 {
return xerrors.Errorf("no candidates")
}
sectorInfo, err := stmgr.GetSectorsForElectionPost(ctx, syncer.sm, curTs, h.Miner)
if err != nil { if err != nil {
return xerrors.Errorf("getting election post sector set: %w", err) return xerrors.Errorf("getting election post sector set: %w", err)
} }
if build.InsecurePoStValidation { panic("NEED TO GET CHALLENGE epp.GenerateWinningPoStSectorChallenge")
if len(h.EPostProof.Proofs) == 0 { challenge := 0
return xerrors.Errorf("[TESTING] No election post proof given")
}
if string(h.EPostProof.Proofs[0].ProofBytes) == "valid proof" { sector := sectors[challenge]
return nil
}
return xerrors.Errorf("[TESTING] election post was invalid")
}
rt, _, err := ffiwrapper.ProofTypeFromSectorSize(ssize) ok, err := ffiwrapper.ProofVerifier.VerifyWinningPoSt(ctx, abi.WinningPoStVerifyInfo{
if err != nil { Randomness: rand,
return err Proofs: h.WinPoStProof,
} ChallengedSectors: []abi.SectorInfo{sector.Info.AsSectorInfo()},
candidates := make([]abi.PoStCandidate, 0, len(h.EPostProof.Candidates))
for _, c := range h.EPostProof.Candidates {
candidates = append(candidates, abi.PoStCandidate{
RegisteredProof: rt,
PartialTicket: c.Partial,
SectorID: abi.SectorID{Number: c.SectorID}, // this should not be an ID, we already know who the miner is...
ChallengeIndex: int64(c.ChallengeIndex),
})
}
// TODO: why do we need this here?
challengeCount := ffiwrapper.ElectionPostChallengeCount(uint64(len(sectorInfo)), 0)
hvrf := blake2b.Sum256(h.EPostProof.PostRand)
pvi := abi.PoStVerifyInfo{
Randomness: hvrf[:],
Candidates: candidates,
Proofs: h.EPostProof.Proofs,
EligibleSectors: sectorInfo,
Prover: abi.ActorID(mid), Prover: abi.ActorID(mid),
ChallengeCount: challengeCount, })
}
ok, err := ffiwrapper.ProofVerifier.VerifyElectionPost(ctx, pvi)
if err != nil { if err != nil {
return xerrors.Errorf("failed to verify election post: %w", err) return xerrors.Errorf("failed to verify election post: %w", err)
} }
if !ok { if !ok {
log.Errorf("invalid election post (%x; %v)", pvi.Randomness, candidates) log.Errorf("invalid election post (%x; %v)", rand, challenge)
return xerrors.Errorf("election post was invalid") return xerrors.Errorf("election post was invalid")
} }
return nil return nil
} }
*/
func (syncer *Syncer) checkBlockMessages(ctx context.Context, b *types.FullBlock, baseTs *types.TipSet) error { func (syncer *Syncer) checkBlockMessages(ctx context.Context, b *types.FullBlock, baseTs *types.TipSet) error {
{ {

View File

@ -40,6 +40,8 @@ type BlockHeader struct {
BeaconEntries []BeaconEntry BeaconEntries []BeaconEntry
WinPoStProof []abi.PoStProof
Parents []cid.Cid // 3 Parents []cid.Cid // 3
ParentWeight BigInt // 4 ParentWeight BigInt // 4

View File

@ -339,6 +339,26 @@ func (m *Miner) mineOne(ctx context.Context, addr address.Address, base *MiningB
return nil, nil return nil, nil
} }
// TODO: use the right dst, also NB: not using any 'entropy' in this call because nicola really didnt want it
rand, err := m.api.ChainGetRandomness(ctx, base.ts.Key(), crypto.DomainSeparationTag_ElectionPoStChallengeSeed, base.ts.Height()+base.nullRounds, nil)
if err != nil {
return nil, xerrors.Errorf("failed to get randomness for winning post: %w", err)
}
prand := abi.PoStRandomness(rand)
sx, err := m.epp.GenerateCandidates(ctx, prand, uint64(len(mbi.Sectors)))
if err != nil {
return nil, xerrors.Errorf("failed to generate candidates for winning post: %w", err)
}
si := mbi.Sectors[sx[0]]
postInp := []abi.SectorInfo{si.Info.AsSectorInfo()}
postProof, err := m.epp.ComputeProof(ctx, postInp, prand)
if err != nil {
return nil, xerrors.Errorf("failed to compute winning post proof: %w", err)
}
// get pending messages early, // get pending messages early,
pending, err := m.api.MpoolPending(context.TODO(), base.ts.Key()) pending, err := m.api.MpoolPending(context.TODO(), base.ts.Key())
if err != nil { if err != nil {
@ -346,7 +366,7 @@ func (m *Miner) mineOne(ctx context.Context, addr address.Address, base *MiningB
} }
// TODO: winning post proof // TODO: winning post proof
b, err := m.createBlock(base, addr, ticket, winner, bvals, pending) b, err := m.createBlock(base, addr, ticket, winner, bvals, postProof, pending)
if err != nil { if err != nil {
return nil, xerrors.Errorf("failed to create block: %w", err) return nil, xerrors.Errorf("failed to create block: %w", err)
} }
@ -391,7 +411,7 @@ func (m *Miner) computeTicket(ctx context.Context, addr address.Address, brand *
} }
func (m *Miner) createBlock(base *MiningBase, addr address.Address, ticket *types.Ticket, func (m *Miner) createBlock(base *MiningBase, addr address.Address, ticket *types.Ticket,
eproof *types.ElectionProof, bvals []types.BeaconEntry, pending []*types.SignedMessage) (*types.BlockMsg, error) { eproof *types.ElectionProof, bvals []types.BeaconEntry, wpostProof []abi.PoStProof, pending []*types.SignedMessage) (*types.BlockMsg, error) {
msgs, err := SelectMessages(context.TODO(), m.api.StateGetActor, base.ts, pending) msgs, err := SelectMessages(context.TODO(), m.api.StateGetActor, base.ts, pending)
if err != nil { if err != nil {
return nil, xerrors.Errorf("message filtering failed: %w", err) return nil, xerrors.Errorf("message filtering failed: %w", err)
@ -416,6 +436,7 @@ func (m *Miner) createBlock(base *MiningBase, addr address.Address, ticket *type
Messages: msgs, Messages: msgs,
Epoch: nheight, Epoch: nheight,
Timestamp: uts, Timestamp: uts,
WinningPoStProof: wpostProof,
}) })
} }

View File

@ -391,7 +391,8 @@ func mockSbBuilder(t *testing.T, nFull int, storage []int) ([]test.TestNode, []t
node.Override(new(sectorstorage.SectorManager), func() (sectorstorage.SectorManager, error) { node.Override(new(sectorstorage.SectorManager), func() (sectorstorage.SectorManager, error) {
return mock.NewMockSectorMgr(5, build.SectorSizes[0]), nil return mock.NewMockSectorMgr(5, build.SectorSizes[0]), nil
}), }),
node.Override(new(ffiwrapper.Verifier), mock.MockVerifier), node.Override(new(ffiwrapper.Verifier), ffiwrapper.ProofVerifier),
//node.Override(new(ffiwrapper.Verifier), mock.MockVerifier),
node.Unset(new(*sectorstorage.Manager)), node.Unset(new(*sectorstorage.Manager)),
)) ))
} }

View File

@ -168,7 +168,7 @@ func (wpp *StorageWpp) GenerateCandidates(ctx context.Context, randomness abi.Po
return cds, nil return cds, nil
} }
func (wpp *StorageWpp) ComputeProof(ctx context.Context, ssi []abi.SectorInfo, rand []byte) ([]abi.PoStProof, error) { func (wpp *StorageWpp) ComputeProof(ctx context.Context, ssi []abi.SectorInfo, rand abi.PoStRandomness) ([]abi.PoStProof, error) {
if build.InsecurePoStValidation { if build.InsecurePoStValidation {
log.Warn("Generating fake EPost proof! You should only see this while running tests!") log.Warn("Generating fake EPost proof! You should only see this while running tests!")
return []abi.PoStProof{{ProofBytes: []byte("valid proof")}}, nil return []abi.PoStProof{{ProofBytes: []byte("valid proof")}}, nil