From ba0d6485461947b2ddbe7f68528b4641969a2c29 Mon Sep 17 00:00:00 2001 From: whyrusleeping Date: Wed, 7 Aug 2019 18:16:58 -0700 Subject: [PATCH] sealing garbage data into a sector that gets accepted by the chain works! --- chain/actors/actor_miner.go | 1 + chain/gen/mining.go | 7 --- lib/sectorbuilder/poll.go | 2 +- lib/sectorbuilder/sectorbuilder.go | 2 +- lib/sectorbuilder/sectorbuilder_test.go | 65 +++++++++++++++++++++++++ node/modules/storageminer.go | 32 ++++++------ storage/miner.go | 9 +++- 7 files changed, 93 insertions(+), 25 deletions(-) create mode 100644 lib/sectorbuilder/sectorbuilder_test.go diff --git a/chain/actors/actor_miner.go b/chain/actors/actor_miner.go index a3f9faab9..8eb8e6ac1 100644 --- a/chain/actors/actor_miner.go +++ b/chain/actors/actor_miner.go @@ -377,6 +377,7 @@ func AddToSectorSet(ctx context.Context, cst *hamt.CborIpldStore, ss cid.Cid, se } func ValidatePoRep(maddr address.Address, ssize types.BigInt, params *CommitSectorParams) (bool, ActorError) { + log.Infof("VALIDATE POREP: %d %x %x %x %s %d %x", ssize.Uint64(), params.CommR, params.CommD, params.CommRStar, maddr, params.SectorID.Uint64(), params.Proof) ok, err := sectorbuilder.VerifySeal(ssize.Uint64(), params.CommR, params.CommD, params.CommRStar, maddr, params.SectorID.Uint64(), params.Proof) if err != nil { return false, aerrors.Escalate(err, "verify seal failed") diff --git a/chain/gen/mining.go b/chain/gen/mining.go index 13804ca5b..9872bff32 100644 --- a/chain/gen/mining.go +++ b/chain/gen/mining.go @@ -2,7 +2,6 @@ package gen import ( "context" - "fmt" bls "github.com/filecoin-project/go-bls-sigs" cid "github.com/ipfs/go-cid" @@ -70,9 +69,6 @@ func MinerCreateBlock(ctx context.Context, cs *store.ChainStore, miner address.A if err != nil { return nil, errors.Wrap(err, "apply message failure") } - if rec.ActorErr != nil { - fmt.Println(rec.ActorErr) - } receipts = append(receipts, rec.MessageReceipt) } @@ -81,9 +77,6 @@ func MinerCreateBlock(ctx context.Context, cs *store.ChainStore, miner address.A if err != nil { return nil, errors.Wrap(err, "apply message failure") } - if rec.ActorErr != nil { - fmt.Println(rec.ActorErr) - } receipts = append(receipts, rec.MessageReceipt) } diff --git a/lib/sectorbuilder/poll.go b/lib/sectorbuilder/poll.go index d0af6270b..d2200af80 100644 --- a/lib/sectorbuilder/poll.go +++ b/lib/sectorbuilder/poll.go @@ -21,7 +21,7 @@ func (sb *SectorBuilder) pollForSealedSectors(ctx context.Context) { watching[s.SectorID] = true } - tick := time.Tick(time.Second * 30) + tick := time.Tick(time.Second * 5) for { select { case <-tick: diff --git a/lib/sectorbuilder/sectorbuilder.go b/lib/sectorbuilder/sectorbuilder.go index 96bed72dc..63cc10988 100644 --- a/lib/sectorbuilder/sectorbuilder.go +++ b/lib/sectorbuilder/sectorbuilder.go @@ -36,7 +36,7 @@ type SectorBuilderConfig struct { func New(cfg *SectorBuilderConfig) (*SectorBuilder, error) { proverId := addressToProverID(cfg.Miner) - sbp, err := sectorbuilder.InitSectorBuilder(cfg.SectorSize, 2, 2, 1, cfg.MetadataDir, proverId, cfg.SealedDir, cfg.StagedDir, 16) + sbp, err := sectorbuilder.InitSectorBuilder(cfg.SectorSize, 1, 1, 1, cfg.MetadataDir, proverId, cfg.SealedDir, cfg.StagedDir, 16) if err != nil { return nil, err } diff --git a/lib/sectorbuilder/sectorbuilder_test.go b/lib/sectorbuilder/sectorbuilder_test.go new file mode 100644 index 000000000..211bbcbbc --- /dev/null +++ b/lib/sectorbuilder/sectorbuilder_test.go @@ -0,0 +1,65 @@ +package sectorbuilder + +import ( + "context" + "fmt" + "io" + "io/ioutil" + "math/rand" + "testing" + + "github.com/filecoin-project/go-lotus/chain/address" +) + +func TestSealAndVerify(t *testing.T) { + t.Skip("this is slow") + dir, err := ioutil.TempDir("", "sbtest") + if err != nil { + t.Fatal(err) + } + + addr, err := address.NewFromString("t1tct3nfaw2q543xtybxcyw4deyxmfwkjk43u4t5y") + if err != nil { + t.Fatal(err) + } + + sb, err := New(&SectorBuilderConfig{ + SectorSize: 1024, + SealedDir: dir, + StagedDir: dir, + MetadataDir: dir, + Miner: addr, + }) + if err != nil { + t.Fatal(err) + } + + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() + + sb.Run(ctx) + + fi, err := ioutil.TempFile("", "sbtestfi") + if err != nil { + t.Fatal(err) + } + defer fi.Close() + + io.CopyN(fi, rand.New(rand.NewSource(42)), 1016) + + if _, err := sb.AddPiece("foo", 1016, fi.Name()); err != nil { + t.Fatal(err) + } + + ssinfo := <-sb.SealedSectorChan() + fmt.Println("sector sealed...") + + ok, err := VerifySeal(1024, ssinfo.CommR[:], ssinfo.CommD[:], ssinfo.CommRStar[:], addr, ssinfo.SectorID, ssinfo.Proof) + if err != nil { + t.Fatal(err) + } + + if !ok { + t.Fatal("proof failed to validate") + } +} diff --git a/node/modules/storageminer.go b/node/modules/storageminer.go index 2bcbf656a..1ff4ed221 100644 --- a/node/modules/storageminer.go +++ b/node/modules/storageminer.go @@ -18,8 +18,22 @@ import ( "github.com/filecoin-project/go-lotus/storage" ) -func SectorBuilderConfig(storagePath string) func() (*sectorbuilder.SectorBuilderConfig, error) { - return func() (*sectorbuilder.SectorBuilderConfig, error) { +func minerAddrFromDS(ds dtypes.MetadataDS) (address.Address, error) { + maddrb, err := ds.Get(datastore.NewKey("miner-address")) + if err != nil { + return address.Undef, err + } + + return address.NewFromBytes(maddrb) +} + +func SectorBuilderConfig(storagePath string) func(dtypes.MetadataDS) (*sectorbuilder.SectorBuilderConfig, error) { + return func(ds dtypes.MetadataDS) (*sectorbuilder.SectorBuilderConfig, error) { + minerAddr, err := minerAddrFromDS(ds) + if err != nil { + return nil, err + } + sp, err := homedir.Expand(storagePath) if err != nil { return nil, err @@ -29,12 +43,6 @@ func SectorBuilderConfig(storagePath string) func() (*sectorbuilder.SectorBuilde sealed := filepath.Join(sp, "sealed") staging := filepath.Join(sp, "staging") - // TODO: get the address of the miner actor - minerAddr, err := address.NewIDAddress(42) - if err != nil { - return nil, err - } - sb := §orbuilder.SectorBuilderConfig{ Miner: minerAddr, SectorSize: 1024, @@ -66,17 +74,11 @@ func SectorBuilder(mctx helpers.MetricsCtx, lc fx.Lifecycle, sbc *sectorbuilder. } func StorageMiner(mctx helpers.MetricsCtx, lc fx.Lifecycle, api api.FullNode, h host.Host, ds dtypes.MetadataDS, sb *sectorbuilder.SectorBuilder, w *wallet.Wallet) (*storage.Miner, error) { - maddrb, err := ds.Get(datastore.NewKey("miner-address")) + maddr, err := minerAddrFromDS(ds) if err != nil { return nil, err } - maddr, err := address.NewFromBytes(maddrb) - if err != nil { - return nil, err - } - - log.Infof("setting up miner: %s", maddr) sm, err := storage.NewMiner(api, maddr, h, ds, sb, w) if err != nil { return nil, err diff --git a/storage/miner.go b/storage/miner.go index 9a3c988e0..d99d00620 100644 --- a/storage/miner.go +++ b/storage/miner.go @@ -99,6 +99,14 @@ func (m *Miner) handlePostingSealedSectors(ctx context.Context) { } func (m *Miner) commitSector(ctx context.Context, sinfo sectorbuilder.SectorSealingStatus) error { + ok, err := sectorbuilder.VerifySeal(1024, sinfo.CommR[:], sinfo.CommD[:], sinfo.CommRStar[:], m.maddr, sinfo.SectorID, sinfo.Proof) + if err != nil { + log.Error("failed to verify seal we just created: ", err) + } + if !ok { + log.Error("seal we just created failed verification") + } + params := &actors.CommitSectorParams{ SectorID: types.NewInt(sinfo.SectorID), CommD: sinfo.CommD[:], @@ -168,7 +176,6 @@ func (m *Miner) runPreflightChecks(ctx context.Context) error { m.worker = worker - // try signing something with that key to make sure we can has, err := m.w.HasKey(worker) if err != nil { return errors.Wrap(err, "failed to check wallet for worker key")