Skip to content

Commit

Permalink
fix: get seed
Browse files Browse the repository at this point in the history
  • Loading branch information
dtynn committed Aug 9, 2021
1 parent 612dad5 commit 3af8c7a
Show file tree
Hide file tree
Showing 9 changed files with 47 additions and 80 deletions.
3 changes: 2 additions & 1 deletion venus-sealer/dep/sealer_constructor.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@ func BuildCommitmentManager(
lc fx.Lifecycle,
capi chain.API,
mapi messager.API,
rapi api.RandomnessAPI,
stmgr api.SectorStateManager,
scfg *sealer.Config,
rlock confmgr.RLocker,
Expand All @@ -192,7 +193,7 @@ func BuildCommitmentManager(
mgr, err := commitmgr.NewCommitmentMgr(
gctx,
mapi,
commitmgr.NewSealingAPIImpl(capi),
commitmgr.NewSealingAPIImpl(capi, rapi),
stmgr,
scfg,
rlock,
Expand Down
4 changes: 2 additions & 2 deletions venus-sealer/sealer/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ type SealerAPI interface {
}

type RandomnessAPI interface {
GetTicket(context.Context, types.TipSetKey, abi.ChainEpoch, []byte) (Ticket, error)
GetSeed(context.Context, types.TipSetKey, abi.ChainEpoch, []byte) (Seed, error)
GetTicket(context.Context, types.TipSetKey, abi.ChainEpoch, abi.ActorID) (Ticket, error)
GetSeed(context.Context, types.TipSetKey, abi.ChainEpoch, abi.ActorID) (Seed, error)
}

type MinerInfoAPI interface {
Expand Down
6 changes: 3 additions & 3 deletions venus-sealer/sealer/impl/commitmgr/check.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import (

"github.com/filecoin-project/go-address"
"github.com/filecoin-project/go-state-types/abi"
"github.com/filecoin-project/go-state-types/crypto"
proof2 "github.com/filecoin-project/specs-actors/v2/actors/runtime/proof"
"github.com/filecoin-project/venus/pkg/types"

"github.com/dtynn/venus-cluster/venus-sealer/sealer/api"
"github.com/dtynn/venus-cluster/venus-sealer/sealer/policy"
Expand Down Expand Up @@ -143,12 +143,12 @@ func checkCommit(ctx context.Context, si api.SectorState, proof []byte, tok api.
return &ErrMarshalAddr{err}
}

seed, err := api.ChainGetRandomnessFromBeacon(ctx, tok, crypto.DomainSeparationTag_InteractiveSealChallengeSeed, si.Seed.Epoch, buf.Bytes())
seed, err := api.GetSeed(ctx, types.EmptyTSK, seedEpoch, si.ID.Miner)
if err != nil {
return &ErrApi{fmt.Errorf("failed to get randomness for computing seal proof: %w", err)}
}

if string(seed) != string(si.Seed.Seed) {
if !bytes.Equal(seed.Seed, si.Seed.Seed) {
return &ErrBadSeed{fmt.Errorf("seed has changed")}
}

Expand Down
16 changes: 4 additions & 12 deletions venus-sealer/sealer/impl/commitmgr/sealing.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"github.com/filecoin-project/go-address"
"github.com/filecoin-project/go-state-types/abi"
"github.com/filecoin-project/go-state-types/big"
"github.com/filecoin-project/go-state-types/crypto"
"github.com/filecoin-project/go-state-types/network"
market2 "github.com/filecoin-project/specs-actors/v2/actors/builtin/market"
builtin5 "github.com/filecoin-project/specs-actors/v5/actors/builtin"
Expand All @@ -28,11 +27,13 @@ import (

type SealingAPIImpl struct {
api chainAPI.API
api.RandomnessAPI
}

func NewSealingAPIImpl(api chainAPI.API) SealingAPIImpl {
func NewSealingAPIImpl(api chainAPI.API, rand api.RandomnessAPI) SealingAPIImpl {
return SealingAPIImpl{
api: api,
api: api,
RandomnessAPI: rand,
}
}

Expand Down Expand Up @@ -239,13 +240,4 @@ func (s SealingAPIImpl) ChainBaseFee(ctx context.Context, tok api.TipSetToken) (
return ts.Blocks()[0].ParentBaseFee, nil
}

func (s SealingAPIImpl) ChainGetRandomnessFromBeacon(ctx context.Context, tok api.TipSetToken, personalization crypto.DomainSeparationTag, randEpoch abi.ChainEpoch, entropy []byte) (abi.Randomness, error) {
tsk, err := types.TipSetKeyFromBytes(tok)
if err != nil {
return nil, err
}

return s.api.ChainGetRandomnessFromBeacon(ctx, tsk, personalization, randEpoch, entropy)
}

var _ SealingAPI = (*SealingAPIImpl)(nil)
5 changes: 2 additions & 3 deletions venus-sealer/sealer/impl/commitmgr/sealing_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ import (
"github.com/filecoin-project/go-address"
"github.com/filecoin-project/go-state-types/abi"
"github.com/filecoin-project/go-state-types/big"
"github.com/filecoin-project/go-state-types/crypto"
"github.com/filecoin-project/go-state-types/network"
"github.com/filecoin-project/venus/pkg/specactors/builtin/market"
"github.com/filecoin-project/venus/pkg/specactors/builtin/miner"
"github.com/filecoin-project/venus/pkg/types"

"github.com/ipfs/go-cid"

Expand All @@ -35,6 +35,5 @@ type SealingAPI interface {
ChainHead(ctx context.Context) (api.TipSetToken, abi.ChainEpoch, error)
ChainBaseFee(ctx context.Context, tok api.TipSetToken) (abi.TokenAmount, error)

// validate random, may need to consider the place here
ChainGetRandomnessFromBeacon(ctx context.Context, tok api.TipSetToken, personalization crypto.DomainSeparationTag, randEpoch abi.ChainEpoch, entropy []byte) (abi.Randomness, error)
GetSeed(context.Context, types.TipSetKey, abi.ChainEpoch, abi.ActorID) (api.Seed, error)
}
4 changes: 2 additions & 2 deletions venus-sealer/sealer/impl/mock/randomness.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,14 @@ type random struct {
seed [32]byte
}

func (r *random) GetTicket(ctx context.Context, tsk types.TipSetKey, epoch abi.ChainEpoch, entropy []byte) (api.Ticket, error) {
func (r *random) GetTicket(ctx context.Context, tsk types.TipSetKey, epoch abi.ChainEpoch, mid abi.ActorID) (api.Ticket, error) {
return api.Ticket{
Ticket: r.ticket[:],
Epoch: epoch,
}, nil
}

func (r *random) GetSeed(ctx context.Context, tsk types.TipSetKey, epoch abi.ChainEpoch, entropy []byte) (api.Seed, error) {
func (r *random) GetSeed(ctx context.Context, tsk types.TipSetKey, epoch abi.ChainEpoch, mid abi.ActorID) (api.Seed, error) {
return api.Seed{
Seed: r.seed[:],
Epoch: epoch,
Expand Down
30 changes: 2 additions & 28 deletions venus-sealer/sealer/impl/mock/sealer.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
package mock

import (
"bytes"
"context"

"github.com/filecoin-project/go-address"
"github.com/filecoin-project/go-state-types/abi"
"github.com/filecoin-project/venus/pkg/types"

Expand Down Expand Up @@ -37,27 +35,8 @@ func (s *Sealer) AcquireDeals(ctx context.Context, sid abi.SectorID, spec api.Ac
return s.deal.Acquire(ctx, sid, spec.MaxDeals)
}

func (s *Sealer) getRandomnessEntropy(mid abi.ActorID) ([]byte, error) {
maddr, err := address.NewIDAddress(uint64(mid))
if err != nil {
return nil, err
}

var buf bytes.Buffer
if err := maddr.MarshalCBOR(&buf); err != nil {
return nil, err
}

return buf.Bytes(), nil
}

func (s *Sealer) AssignTicket(ctx context.Context, sid abi.SectorID) (api.Ticket, error) {
entropy, err := s.getRandomnessEntropy(sid.Miner)
if err != nil {
return api.Ticket{}, nil
}

return s.rand.GetTicket(ctx, types.EmptyTSK, 0, entropy)
return s.rand.GetTicket(ctx, types.EmptyTSK, 0, sid.Miner)
}

func (s *Sealer) SubmitPreCommit(ctx context.Context, sector api.AllocatedSector, info api.PreCommitOnChainInfo, reset bool) (api.SubmitPreCommitResp, error) {
Expand All @@ -74,12 +53,7 @@ func (s *Sealer) PollPreCommitState(ctx context.Context, sid abi.SectorID) (api.
}

func (s *Sealer) WaitSeed(ctx context.Context, sid abi.SectorID) (api.WaitSeedResp, error) {
entropy, err := s.getRandomnessEntropy(sid.Miner)
if err != nil {
return api.WaitSeedResp{}, err
}

seed, err := s.rand.GetSeed(ctx, types.EmptyTSK, 0, entropy)
seed, err := s.rand.GetSeed(ctx, types.EmptyTSK, 0, sid.Miner)
if err != nil {
return api.WaitSeedResp{}, err
}
Expand Down
30 changes: 28 additions & 2 deletions venus-sealer/sealer/impl/randomness/rand.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package randomness

import (
"bytes"
"context"

"github.com/filecoin-project/go-address"
"github.com/filecoin-project/go-state-types/abi"
"github.com/filecoin-project/go-state-types/crypto"
"github.com/filecoin-project/venus/pkg/types"
Expand All @@ -23,7 +25,26 @@ type Randomness struct {
api chain.API
}

func (r *Randomness) GetTicket(ctx context.Context, tsk types.TipSetKey, epoch abi.ChainEpoch, entropy []byte) (api.Ticket, error) {
func (r *Randomness) getRandomnessEntropy(mid abi.ActorID) ([]byte, error) {
maddr, err := address.NewIDAddress(uint64(mid))
if err != nil {
return nil, err
}

var buf bytes.Buffer
if err := maddr.MarshalCBOR(&buf); err != nil {
return nil, err
}

return buf.Bytes(), nil
}

func (r *Randomness) GetTicket(ctx context.Context, tsk types.TipSetKey, epoch abi.ChainEpoch, mid abi.ActorID) (api.Ticket, error) {
entropy, err := r.getRandomnessEntropy(mid)
if err != nil {
return api.Ticket{}, err
}

if tsk == types.EmptyTSK {
ts, err := r.api.ChainHead(ctx)
if err != nil {
Expand All @@ -44,7 +65,12 @@ func (r *Randomness) GetTicket(ctx context.Context, tsk types.TipSetKey, epoch a
}, nil
}

func (r *Randomness) GetSeed(ctx context.Context, tsk types.TipSetKey, epoch abi.ChainEpoch, entropy []byte) (api.Seed, error) {
func (r *Randomness) GetSeed(ctx context.Context, tsk types.TipSetKey, epoch abi.ChainEpoch, mid abi.ActorID) (api.Seed, error) {
entropy, err := r.getRandomnessEntropy(mid)
if err != nil {
return api.Seed{}, err
}

if tsk == types.EmptyTSK {
ts, err := r.api.ChainHead(ctx)
if err != nil {
Expand Down
29 changes: 2 additions & 27 deletions venus-sealer/sealer/sealer.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package sealer

import (
"bytes"
"context"
"fmt"

Expand Down Expand Up @@ -109,33 +108,14 @@ func (s *Sealer) AcquireDeals(ctx context.Context, sid abi.SectorID, spec api.Ac
return deals, nil
}

func (s *Sealer) getRandomnessEntropy(mid abi.ActorID) ([]byte, error) {
maddr, err := address.NewIDAddress(uint64(mid))
if err != nil {
return nil, err
}

var buf bytes.Buffer
if err := maddr.MarshalCBOR(&buf); err != nil {
return nil, err
}

return buf.Bytes(), nil
}

func (s *Sealer) AssignTicket(ctx context.Context, sid abi.SectorID) (api.Ticket, error) {
ts, err := s.capi.ChainHead(ctx)
if err != nil {
return api.Ticket{}, err
}

entropy, err := s.getRandomnessEntropy(sid.Miner)
if err != nil {
return api.Ticket{}, err
}

ticketEpoch := ts.Height() - policy.SealRandomnessLookback
ticket, err := s.rand.GetTicket(ctx, ts.Key(), ticketEpoch, entropy)
ticket, err := s.rand.GetTicket(ctx, ts.Key(), ticketEpoch, sid.Miner)
if err != nil {
return api.Ticket{}, err
}
Expand Down Expand Up @@ -187,12 +167,7 @@ func (s *Sealer) WaitSeed(ctx context.Context, sid abi.SectorID) (api.WaitSeedRe
}, nil
}

entropy, err := s.getRandomnessEntropy(sid.Miner)
if err != nil {
return api.WaitSeedResp{}, err
}

seed, err := s.rand.GetSeed(ctx, tsk, seedEpoch, entropy)
seed, err := s.rand.GetSeed(ctx, tsk, seedEpoch, sid.Miner)
if err != nil {
return api.WaitSeedResp{}, err
}
Expand Down

0 comments on commit 3af8c7a

Please sign in to comment.