lotus/lotus-soup/main.go

121 lines
3.5 KiB
Go
Raw Normal View History

package main
import (
"context"
"fmt"
"io/ioutil"
"math/rand"
"os"
"time"
"github.com/filecoin-project/lotus/api"
"github.com/testground/sdk-go/run"
"github.com/filecoin-project/oni/lotus-soup/testkit"
)
var cases = map[string]interface{}{
2020-07-03 18:06:10 +00:00
"deals-e2e": testkit.WrapTestEnvironment(dealsE2E),
2020-07-01 16:54:46 +00:00
"drand-halting": testkit.WrapTestEnvironment(dealsE2E),
}
func main() {
run.InvokeMap(cases)
}
// This is the baseline test; Filecoin 101.
//
// A network with a bootstrapper, a number of miners, and a number of clients/full nodes
// is constructed and connected through the bootstrapper.
// Some funds are allocated to each node and a number of sectors are presealed in the genesis block.
//
// The test plan:
// One or more clients store content to one or more miners, testing storage deals.
// The plan ensures that the storage deals hit the blockchain and measure the time it took.
// Verification: one or more clients retrieve and verify the hashes of stored content.
// The plan ensures that all (previously) published content can be correctly retrieved
// and measures the time it took.
//
// Preparation of the genesis block: this is the responsibility of the bootstrapper.
// In order to compute the genesis block, we need to collect identities and presealed
// sectors from each node.
// Then we create a genesis block that allocates some funds to each node and collects
// the presealed sectors.
2020-07-01 13:41:38 +00:00
func dealsE2E(t *testkit.TestEnvironment) error {
// Dispatch/forward non-client roles to defaults.
2020-07-01 13:41:38 +00:00
if t.Role != "client" {
return testkit.HandleDefaultRole(t)
2020-07-01 13:41:38 +00:00
}
2020-07-01 13:41:38 +00:00
cl, err := testkit.PrepareClient(t)
if err != nil {
return err
}
2020-07-01 13:41:38 +00:00
// This is a client role
t.RecordMessage("running client")
2020-07-01 13:41:38 +00:00
ctx := context.Background()
client := cl.FullApi
2020-07-01 13:41:38 +00:00
// select a random miner
2020-07-03 18:06:10 +00:00
minerAddr := cl.MinerAddrs[t.InitContext.GroupSeq-1]
2020-07-01 13:41:38 +00:00
if err := client.NetConnect(ctx, minerAddr.PeerAddr); err != nil {
return err
}
2020-07-01 13:41:38 +00:00
t.D().Counter(fmt.Sprintf("send-data-to,miner=%s", minerAddr.ActorAddr)).Inc(1)
2020-07-01 13:41:38 +00:00
t.RecordMessage("selected %s as the miner", minerAddr.ActorAddr)
2020-07-01 13:41:38 +00:00
time.Sleep(2 * time.Second)
2020-07-01 13:41:38 +00:00
// generate 1600 bytes of random data
data := make([]byte, 1600)
rand.New(rand.NewSource(time.Now().UnixNano())).Read(data)
2020-07-01 13:41:38 +00:00
file, err := ioutil.TempFile("/tmp", "data")
if err != nil {
return err
}
defer os.Remove(file.Name())
2020-07-01 13:41:38 +00:00
_, err = file.Write(data)
if err != nil {
return err
}
2020-07-01 13:41:38 +00:00
fcid, err := client.ClientImport(ctx, api.FileRef{Path: file.Name(), IsCAR: false})
if err != nil {
return err
}
t.RecordMessage("file cid: %s", fcid)
2020-07-01 13:41:38 +00:00
// start deal
t1 := time.Now()
deal := testkit.StartDeal(ctx, minerAddr.ActorAddr, client, fcid)
t.RecordMessage("started deal: %s", deal)
2020-07-01 13:41:38 +00:00
// TODO: this sleep is only necessary because deals don't immediately get logged in the dealstore, we should fix this
time.Sleep(2 * time.Second)
2020-07-01 13:41:38 +00:00
t.RecordMessage("waiting for deal to be sealed")
testkit.WaitDealSealed(t, ctx, client, deal)
t.D().ResettingHistogram("deal.sealed").Update(int64(time.Since(t1)))
2020-07-01 13:41:38 +00:00
carExport := true
2020-07-01 13:41:38 +00:00
t.RecordMessage("trying to retrieve %s", fcid)
testkit.RetrieveData(t, ctx, err, client, fcid, carExport, data)
t.D().ResettingHistogram("deal.retrieved").Update(int64(time.Since(t1)))
2020-07-01 13:41:38 +00:00
t.SyncClient.MustSignalEntry(ctx, testkit.StateStopMining)
2020-07-01 13:41:38 +00:00
time.Sleep(10 * time.Second) // wait for metrics to be emitted
2020-07-01 13:41:38 +00:00
// TODO broadcast published content CIDs to other clients
// TODO select a random piece of content published by some other client and retrieve it
2020-07-01 13:41:38 +00:00
t.SyncClient.MustSignalAndWait(ctx, testkit.StateDone, t.TestInstanceCount)
return nil
}