2019-07-25 22:15:33 +00:00
|
|
|
package gen
|
|
|
|
|
|
|
|
import (
|
2019-07-30 11:45:48 +00:00
|
|
|
"bytes"
|
2019-07-25 22:15:33 +00:00
|
|
|
"context"
|
2019-11-19 15:53:00 +00:00
|
|
|
"crypto/sha256"
|
|
|
|
"encoding/binary"
|
2019-09-06 06:26:02 +00:00
|
|
|
"fmt"
|
2019-11-29 03:38:18 +00:00
|
|
|
"io/ioutil"
|
2019-07-30 13:20:40 +00:00
|
|
|
"sync/atomic"
|
2019-07-25 22:15:33 +00:00
|
|
|
|
2019-11-29 03:38:18 +00:00
|
|
|
ffi "github.com/filecoin-project/filecoin-ffi"
|
|
|
|
|
2019-12-19 21:06:25 +00:00
|
|
|
sectorbuilder "github.com/filecoin-project/go-sectorbuilder"
|
2019-07-30 11:45:48 +00:00
|
|
|
"github.com/ipfs/go-blockservice"
|
|
|
|
"github.com/ipfs/go-car"
|
|
|
|
offline "github.com/ipfs/go-ipfs-exchange-offline"
|
|
|
|
"github.com/ipfs/go-merkledag"
|
2019-12-05 02:04:09 +00:00
|
|
|
peer "github.com/libp2p/go-libp2p-core/peer"
|
2019-11-21 22:21:45 +00:00
|
|
|
"go.opencensus.io/trace"
|
2019-08-15 18:45:32 +00:00
|
|
|
"golang.org/x/xerrors"
|
2019-07-30 11:45:48 +00:00
|
|
|
|
2019-12-19 20:13:17 +00:00
|
|
|
"github.com/filecoin-project/go-address"
|
2019-10-18 04:47:41 +00:00
|
|
|
"github.com/filecoin-project/lotus/api"
|
|
|
|
"github.com/filecoin-project/lotus/build"
|
|
|
|
"github.com/filecoin-project/lotus/chain/stmgr"
|
|
|
|
"github.com/filecoin-project/lotus/chain/store"
|
|
|
|
"github.com/filecoin-project/lotus/chain/types"
|
|
|
|
"github.com/filecoin-project/lotus/chain/wallet"
|
2019-11-29 03:38:18 +00:00
|
|
|
"github.com/filecoin-project/lotus/cmd/lotus-seed/seed"
|
|
|
|
"github.com/filecoin-project/lotus/genesis"
|
2019-10-18 04:47:41 +00:00
|
|
|
"github.com/filecoin-project/lotus/node/repo"
|
2019-07-26 12:19:27 +00:00
|
|
|
|
2019-07-25 22:15:33 +00:00
|
|
|
block "github.com/ipfs/go-block-format"
|
2019-07-26 12:19:27 +00:00
|
|
|
"github.com/ipfs/go-cid"
|
2019-07-25 22:15:33 +00:00
|
|
|
blockstore "github.com/ipfs/go-ipfs-blockstore"
|
2020-01-08 19:10:57 +00:00
|
|
|
logging "github.com/ipfs/go-log/v2"
|
2019-07-25 22:15:33 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var log = logging.Logger("gen")
|
|
|
|
|
2019-10-08 00:28:13 +00:00
|
|
|
const msgsPerBlock = 20
|
2019-07-30 13:20:40 +00:00
|
|
|
|
2019-07-25 22:15:33 +00:00
|
|
|
type ChainGen struct {
|
|
|
|
msgsPerBlock int
|
|
|
|
|
|
|
|
bs blockstore.Blockstore
|
|
|
|
|
|
|
|
cs *store.ChainStore
|
|
|
|
|
2019-09-06 06:26:02 +00:00
|
|
|
sm *stmgr.StateManager
|
|
|
|
|
|
|
|
genesis *types.BlockHeader
|
2019-10-05 23:18:39 +00:00
|
|
|
CurTipset *store.FullTipSet
|
2019-07-25 22:15:33 +00:00
|
|
|
|
2019-11-19 15:53:00 +00:00
|
|
|
Timestamper func(*types.TipSet, uint64) uint64
|
2019-10-10 03:04:10 +00:00
|
|
|
|
2019-07-30 13:20:40 +00:00
|
|
|
w *wallet.Wallet
|
|
|
|
|
2019-11-21 22:21:45 +00:00
|
|
|
eppProvs map[address.Address]ElectionPoStProver
|
2019-10-05 16:04:58 +00:00
|
|
|
Miners []address.Address
|
2019-07-30 13:20:40 +00:00
|
|
|
receivers []address.Address
|
|
|
|
banker address.Address
|
|
|
|
bankerNonce uint64
|
2019-07-29 19:34:09 +00:00
|
|
|
|
2019-07-30 11:45:48 +00:00
|
|
|
r repo.Repo
|
2019-07-29 19:34:09 +00:00
|
|
|
lr repo.LockedRepo
|
2019-07-25 22:15:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type mybs struct {
|
|
|
|
blockstore.Blockstore
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m mybs) Get(c cid.Cid) (block.Block, error) {
|
|
|
|
b, err := m.Blockstore.Get(c)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return b, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewGenerator() (*ChainGen, error) {
|
|
|
|
mr := repo.NewMemory(nil)
|
2019-11-12 17:59:38 +00:00
|
|
|
lr, err := mr.Lock(repo.StorageMiner)
|
2019-07-25 22:15:33 +00:00
|
|
|
if err != nil {
|
2019-08-15 18:45:32 +00:00
|
|
|
return nil, xerrors.Errorf("taking mem-repo lock failed: %w", err)
|
2019-07-25 22:15:33 +00:00
|
|
|
}
|
|
|
|
|
2019-07-29 19:34:09 +00:00
|
|
|
ds, err := lr.Datastore("/metadata")
|
|
|
|
if err != nil {
|
2019-08-15 18:45:32 +00:00
|
|
|
return nil, xerrors.Errorf("failed to get metadata datastore: %w", err)
|
2019-07-29 19:34:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bds, err := lr.Datastore("/blocks")
|
2019-07-25 22:15:33 +00:00
|
|
|
if err != nil {
|
2019-08-15 18:45:32 +00:00
|
|
|
return nil, xerrors.Errorf("failed to get blocks datastore: %w", err)
|
2019-07-25 22:15:33 +00:00
|
|
|
}
|
2019-07-29 19:34:09 +00:00
|
|
|
|
2019-07-30 11:45:48 +00:00
|
|
|
bs := mybs{blockstore.NewIdStore(blockstore.NewBlockstore(bds))}
|
2019-07-25 22:15:33 +00:00
|
|
|
|
|
|
|
ks, err := lr.KeyStore()
|
|
|
|
if err != nil {
|
2019-08-15 18:45:32 +00:00
|
|
|
return nil, xerrors.Errorf("getting repo keystore failed: %w", err)
|
2019-07-25 22:15:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
w, err := wallet.NewWallet(ks)
|
|
|
|
if err != nil {
|
2019-08-15 18:45:32 +00:00
|
|
|
return nil, xerrors.Errorf("creating memrepo wallet failed: %w", err)
|
2019-07-25 22:15:33 +00:00
|
|
|
}
|
|
|
|
|
2019-07-30 13:55:36 +00:00
|
|
|
banker, err := w.GenerateKey(types.KTSecp256k1)
|
2019-07-25 22:15:33 +00:00
|
|
|
if err != nil {
|
2019-08-15 18:45:32 +00:00
|
|
|
return nil, xerrors.Errorf("failed to generate banker key: %w", err)
|
2019-07-25 22:15:33 +00:00
|
|
|
}
|
|
|
|
|
2019-07-30 13:20:40 +00:00
|
|
|
receievers := make([]address.Address, msgsPerBlock)
|
|
|
|
for r := range receievers {
|
|
|
|
receievers[r], err = w.GenerateKey(types.KTBLS)
|
|
|
|
if err != nil {
|
2019-08-15 18:45:32 +00:00
|
|
|
return nil, xerrors.Errorf("failed to generate receiver key: %w", err)
|
2019-07-30 13:20:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-30 04:31:16 +00:00
|
|
|
maddr1, err := address.NewFromString("t0300")
|
2019-11-29 03:38:18 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
m1temp, err := ioutil.TempDir("", "preseal")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-12-06 17:53:33 +00:00
|
|
|
genm1, err := seed.PreSeal(maddr1, 1024, 0, 1, m1temp, []byte("some randomness"))
|
2019-11-29 03:38:18 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-11-30 04:31:16 +00:00
|
|
|
maddr2, err := address.NewFromString("t0301")
|
2019-11-29 04:04:46 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
m2temp, err := ioutil.TempDir("", "preseal")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-12-06 17:53:33 +00:00
|
|
|
genm2, err := seed.PreSeal(maddr2, 1024, 0, 1, m2temp, []byte("some randomness"))
|
2019-11-29 04:04:46 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-11-29 04:41:37 +00:00
|
|
|
mk1, err := w.Import(&genm1.Key)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
mk2, err := w.Import(&genm2.Key)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-09-06 20:03:28 +00:00
|
|
|
minercfg := &GenMinerCfg{
|
|
|
|
PeerIDs: []peer.ID{"peerID1", "peerID2"},
|
2019-11-29 03:38:18 +00:00
|
|
|
PreSeals: map[string]genesis.GenesisMiner{
|
|
|
|
maddr1.String(): *genm1,
|
2019-11-29 04:04:46 +00:00
|
|
|
maddr2.String(): *genm2,
|
2019-11-29 03:38:18 +00:00
|
|
|
},
|
2019-11-29 04:41:37 +00:00
|
|
|
MinerAddrs: []address.Address{maddr1, maddr2},
|
2019-09-06 20:03:28 +00:00
|
|
|
}
|
|
|
|
|
2019-07-25 22:15:33 +00:00
|
|
|
genb, err := MakeGenesisBlock(bs, map[address.Address]types.BigInt{
|
2019-11-29 04:41:37 +00:00
|
|
|
mk1: types.FromFil(40000),
|
|
|
|
mk2: types.FromFil(40000),
|
|
|
|
banker: types.FromFil(50000),
|
2019-08-30 02:59:54 +00:00
|
|
|
}, minercfg, 100000)
|
2019-07-25 22:15:33 +00:00
|
|
|
if err != nil {
|
2019-08-15 18:45:32 +00:00
|
|
|
return nil, xerrors.Errorf("make genesis block failed: %w", err)
|
2019-07-25 22:15:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
cs := store.NewChainStore(bs, ds)
|
|
|
|
|
|
|
|
genfb := &types.FullBlock{Header: genb.Genesis}
|
2019-09-06 06:26:02 +00:00
|
|
|
gents := store.NewFullTipSet([]*types.FullBlock{genfb})
|
2019-07-25 22:15:33 +00:00
|
|
|
|
2019-07-26 12:19:27 +00:00
|
|
|
if err := cs.SetGenesis(genb.Genesis); err != nil {
|
2019-08-15 18:45:32 +00:00
|
|
|
return nil, xerrors.Errorf("set genesis failed: %w", err)
|
2019-07-26 12:19:27 +00:00
|
|
|
}
|
|
|
|
|
2019-09-06 06:26:02 +00:00
|
|
|
if len(minercfg.MinerAddrs) == 0 {
|
2019-08-16 00:17:09 +00:00
|
|
|
return nil, xerrors.Errorf("MakeGenesisBlock failed to set miner address")
|
|
|
|
}
|
|
|
|
|
2019-11-21 22:21:45 +00:00
|
|
|
mgen := make(map[address.Address]ElectionPoStProver)
|
|
|
|
for _, m := range minercfg.MinerAddrs {
|
|
|
|
mgen[m] = &eppProvider{}
|
|
|
|
}
|
|
|
|
|
2019-09-06 06:26:02 +00:00
|
|
|
sm := stmgr.NewStateManager(cs)
|
|
|
|
|
2019-07-25 22:15:33 +00:00
|
|
|
gen := &ChainGen{
|
|
|
|
bs: bs,
|
|
|
|
cs: cs,
|
2019-09-06 06:26:02 +00:00
|
|
|
sm: sm,
|
2019-07-25 22:15:33 +00:00
|
|
|
msgsPerBlock: msgsPerBlock,
|
|
|
|
genesis: genb.Genesis,
|
2019-07-30 13:20:40 +00:00
|
|
|
w: w,
|
|
|
|
|
2019-12-01 04:02:52 +00:00
|
|
|
Miners: minercfg.MinerAddrs,
|
|
|
|
eppProvs: mgen,
|
2019-07-30 13:20:40 +00:00
|
|
|
banker: banker,
|
|
|
|
receivers: receievers,
|
|
|
|
|
2019-10-05 23:18:39 +00:00
|
|
|
CurTipset: gents,
|
2019-07-29 19:34:09 +00:00
|
|
|
|
2019-07-30 11:45:48 +00:00
|
|
|
r: mr,
|
2019-07-29 19:34:09 +00:00
|
|
|
lr: lr,
|
2019-07-25 22:15:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return gen, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cg *ChainGen) Genesis() *types.BlockHeader {
|
|
|
|
return cg.genesis
|
|
|
|
}
|
|
|
|
|
2019-07-30 11:45:48 +00:00
|
|
|
func (cg *ChainGen) GenesisCar() ([]byte, error) {
|
|
|
|
offl := offline.Exchange(cg.bs)
|
|
|
|
blkserv := blockservice.New(cg.bs, offl)
|
|
|
|
dserv := merkledag.NewDAGService(blkserv)
|
|
|
|
|
|
|
|
out := new(bytes.Buffer)
|
|
|
|
|
|
|
|
if err := car.WriteCar(context.TODO(), dserv, []cid.Cid{cg.Genesis().Cid()}, out); err != nil {
|
2019-11-21 22:21:45 +00:00
|
|
|
return nil, xerrors.Errorf("genesis car write car failed: %w", err)
|
2019-07-30 11:45:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return out.Bytes(), nil
|
|
|
|
}
|
|
|
|
|
2019-11-21 22:21:45 +00:00
|
|
|
func (cg *ChainGen) nextBlockProof(ctx context.Context, pts *types.TipSet, m address.Address, round int64) (*types.EPostProof, *types.Ticket, error) {
|
2019-09-06 06:26:02 +00:00
|
|
|
|
2019-11-19 15:53:00 +00:00
|
|
|
lastTicket := pts.MinTicket()
|
2019-09-06 06:26:02 +00:00
|
|
|
|
2019-10-06 03:32:56 +00:00
|
|
|
st := pts.ParentState()
|
2019-08-16 00:17:09 +00:00
|
|
|
|
2019-10-23 17:39:14 +00:00
|
|
|
worker, err := stmgr.GetMinerWorkerRaw(ctx, cg.sm, st, m)
|
2019-09-06 06:26:02 +00:00
|
|
|
if err != nil {
|
2019-10-16 07:07:16 +00:00
|
|
|
return nil, nil, xerrors.Errorf("get miner worker: %w", err)
|
2019-09-06 06:26:02 +00:00
|
|
|
}
|
2019-08-16 04:40:59 +00:00
|
|
|
|
2019-11-21 22:21:45 +00:00
|
|
|
vrfout, err := ComputeVRF(ctx, cg.w.Sign, worker, m, DSepTicket, lastTicket.VRFProof)
|
2019-08-16 00:17:09 +00:00
|
|
|
if err != nil {
|
2019-10-16 07:07:16 +00:00
|
|
|
return nil, nil, xerrors.Errorf("compute VRF: %w", err)
|
2019-08-16 00:17:09 +00:00
|
|
|
}
|
|
|
|
|
2019-08-15 02:30:21 +00:00
|
|
|
tick := &types.Ticket{
|
2019-10-09 04:38:59 +00:00
|
|
|
VRFProof: vrfout,
|
2019-08-15 02:30:21 +00:00
|
|
|
}
|
2019-08-16 00:17:09 +00:00
|
|
|
|
2019-12-03 21:27:07 +00:00
|
|
|
eproofin, err := IsRoundWinner(ctx, pts, round, m, cg.eppProvs[m], &mca{w: cg.w, sm: cg.sm})
|
2019-09-06 06:26:02 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, xerrors.Errorf("checking round winner failed: %w", err)
|
|
|
|
}
|
2019-12-03 21:27:07 +00:00
|
|
|
if eproofin == nil {
|
2019-09-06 06:26:02 +00:00
|
|
|
return nil, tick, nil
|
|
|
|
}
|
2019-12-03 18:25:56 +00:00
|
|
|
eproof, err := ComputeProof(ctx, cg.eppProvs[m], eproofin)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, xerrors.Errorf("computing proof: %w", err)
|
|
|
|
}
|
2019-09-06 06:26:02 +00:00
|
|
|
|
|
|
|
return eproof, tick, nil
|
2019-07-25 22:15:33 +00:00
|
|
|
}
|
|
|
|
|
2019-09-03 04:36:07 +00:00
|
|
|
type MinedTipSet struct {
|
|
|
|
TipSet *store.FullTipSet
|
|
|
|
Messages []*types.SignedMessage
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cg *ChainGen) NextTipSet() (*MinedTipSet, error) {
|
2019-10-05 23:18:39 +00:00
|
|
|
mts, err := cg.NextTipSetFromMiners(cg.CurTipset.TipSet(), cg.Miners)
|
2019-10-05 16:04:58 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-10-05 23:18:39 +00:00
|
|
|
cg.CurTipset = mts.TipSet
|
2019-10-05 16:04:58 +00:00
|
|
|
return mts, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cg *ChainGen) NextTipSetFromMiners(base *types.TipSet, miners []address.Address) (*MinedTipSet, error) {
|
2019-09-06 06:26:02 +00:00
|
|
|
var blks []*types.FullBlock
|
|
|
|
|
|
|
|
msgs, err := cg.getRandomMessages()
|
2019-07-25 22:15:33 +00:00
|
|
|
if err != nil {
|
2019-10-16 07:07:16 +00:00
|
|
|
return nil, xerrors.Errorf("get random messages: %w", err)
|
2019-07-25 22:15:33 +00:00
|
|
|
}
|
|
|
|
|
2019-11-19 15:53:00 +00:00
|
|
|
for round := int64(base.Height() + 1); len(blks) == 0; round++ {
|
|
|
|
for _, m := range miners {
|
|
|
|
proof, t, err := cg.nextBlockProof(context.TODO(), base, m, round)
|
2019-09-06 06:26:02 +00:00
|
|
|
if err != nil {
|
2019-10-02 20:27:41 +00:00
|
|
|
return nil, xerrors.Errorf("next block proof: %w", err)
|
2019-09-06 06:26:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if proof != nil {
|
2019-11-19 15:53:00 +00:00
|
|
|
fblk, err := cg.makeBlock(base, m, proof, t, uint64(round), msgs)
|
2019-09-06 06:26:02 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, xerrors.Errorf("making a block for next tipset failed: %w", err)
|
|
|
|
}
|
|
|
|
|
2019-11-12 10:18:46 +00:00
|
|
|
if err := cg.cs.PersistBlockHeaders(fblk.Header); err != nil {
|
2019-10-02 20:27:41 +00:00
|
|
|
return nil, xerrors.Errorf("chainstore AddBlock: %w", err)
|
2019-09-06 06:26:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
blks = append(blks, fblk)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-05 16:04:58 +00:00
|
|
|
fts := store.NewFullTipSet(blks)
|
2019-09-06 06:26:02 +00:00
|
|
|
|
|
|
|
return &MinedTipSet{
|
2019-10-05 16:04:58 +00:00
|
|
|
TipSet: fts,
|
2019-09-06 06:26:02 +00:00
|
|
|
Messages: msgs,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2019-11-21 22:21:45 +00:00
|
|
|
func (cg *ChainGen) makeBlock(parents *types.TipSet, m address.Address, eproof *types.EPostProof, ticket *types.Ticket, height uint64, msgs []*types.SignedMessage) (*types.FullBlock, error) {
|
2019-09-06 06:26:02 +00:00
|
|
|
|
2019-10-10 03:04:10 +00:00
|
|
|
var ts uint64
|
|
|
|
if cg.Timestamper != nil {
|
2019-11-19 15:53:00 +00:00
|
|
|
ts = cg.Timestamper(parents, height-parents.Height())
|
2019-10-10 03:04:10 +00:00
|
|
|
} else {
|
2019-11-19 15:53:00 +00:00
|
|
|
ts = parents.MinTimestamp() + ((height - parents.Height()) * build.BlockDelay)
|
2019-10-10 03:04:10 +00:00
|
|
|
}
|
2019-09-06 06:26:02 +00:00
|
|
|
|
2019-11-19 15:53:00 +00:00
|
|
|
fblk, err := MinerCreateBlock(context.TODO(), cg.sm, cg.w, m, parents, ticket, eproof, msgs, height, ts)
|
2019-09-06 06:26:02 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return fblk, err
|
|
|
|
}
|
|
|
|
|
2019-10-06 00:51:48 +00:00
|
|
|
// This function is awkward. It's used to deal with messages made when
|
|
|
|
// simulating forks
|
|
|
|
func (cg *ChainGen) ResyncBankerNonce(ts *types.TipSet) error {
|
|
|
|
act, err := cg.sm.GetActor(cg.banker, ts)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
cg.bankerNonce = act.Nonce
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-09-06 06:26:02 +00:00
|
|
|
func (cg *ChainGen) getRandomMessages() ([]*types.SignedMessage, error) {
|
2019-07-30 13:20:40 +00:00
|
|
|
msgs := make([]*types.SignedMessage, cg.msgsPerBlock)
|
|
|
|
for m := range msgs {
|
|
|
|
msg := types.Message{
|
2019-08-30 07:05:21 +00:00
|
|
|
To: cg.receivers[m%len(cg.receivers)],
|
2019-07-30 13:20:40 +00:00
|
|
|
From: cg.banker,
|
|
|
|
|
|
|
|
Nonce: atomic.AddUint64(&cg.bankerNonce, 1) - 1,
|
|
|
|
|
|
|
|
Value: types.NewInt(uint64(m + 1)),
|
|
|
|
|
|
|
|
Method: 0,
|
|
|
|
|
|
|
|
GasLimit: types.NewInt(10000),
|
|
|
|
GasPrice: types.NewInt(0),
|
|
|
|
}
|
|
|
|
|
2019-10-09 01:32:08 +00:00
|
|
|
sig, err := cg.w.Sign(context.TODO(), cg.banker, msg.Cid().Bytes())
|
2019-07-30 13:20:40 +00:00
|
|
|
if err != nil {
|
2019-09-03 04:36:07 +00:00
|
|
|
return nil, err
|
2019-07-30 13:20:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
msgs[m] = &types.SignedMessage{
|
|
|
|
Message: msg,
|
|
|
|
Signature: *sig,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-06 06:26:02 +00:00
|
|
|
return msgs, nil
|
2019-07-25 22:15:33 +00:00
|
|
|
}
|
2019-07-29 19:34:09 +00:00
|
|
|
|
|
|
|
func (cg *ChainGen) YieldRepo() (repo.Repo, error) {
|
|
|
|
if err := cg.lr.Close(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return cg.r, nil
|
|
|
|
}
|
2019-09-03 04:36:07 +00:00
|
|
|
|
|
|
|
type MiningCheckAPI interface {
|
2019-11-19 15:53:00 +00:00
|
|
|
ChainGetRandomness(context.Context, types.TipSetKey, int64) ([]byte, error)
|
2019-09-03 04:36:07 +00:00
|
|
|
|
|
|
|
StateMinerPower(context.Context, address.Address, *types.TipSet) (api.MinerPower, error)
|
|
|
|
|
|
|
|
StateMinerWorker(context.Context, address.Address, *types.TipSet) (address.Address, error)
|
|
|
|
|
2019-11-21 22:21:45 +00:00
|
|
|
StateMinerSectorSize(context.Context, address.Address, *types.TipSet) (uint64, error)
|
|
|
|
|
2019-11-25 04:45:13 +00:00
|
|
|
StateMinerProvingSet(context.Context, address.Address, *types.TipSet) ([]*api.ChainSectorInfo, error)
|
|
|
|
|
2019-09-03 04:36:07 +00:00
|
|
|
WalletSign(context.Context, address.Address, []byte) (*types.Signature, error)
|
|
|
|
}
|
|
|
|
|
2019-09-06 06:26:02 +00:00
|
|
|
type mca struct {
|
|
|
|
w *wallet.Wallet
|
|
|
|
sm *stmgr.StateManager
|
|
|
|
}
|
|
|
|
|
2019-11-19 15:53:00 +00:00
|
|
|
func (mca mca) ChainGetRandomness(ctx context.Context, pts types.TipSetKey, lb int64) ([]byte, error) {
|
|
|
|
return mca.sm.ChainStore().GetRandomness(ctx, pts.Cids(), int64(lb))
|
2019-09-06 06:26:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (mca mca) StateMinerPower(ctx context.Context, maddr address.Address, ts *types.TipSet) (api.MinerPower, error) {
|
2019-09-09 20:03:10 +00:00
|
|
|
mpow, tpow, err := stmgr.GetPower(ctx, mca.sm, ts, maddr)
|
2019-09-06 06:26:02 +00:00
|
|
|
if err != nil {
|
|
|
|
return api.MinerPower{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return api.MinerPower{
|
|
|
|
MinerPower: mpow,
|
|
|
|
TotalPower: tpow,
|
|
|
|
}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (mca mca) StateMinerWorker(ctx context.Context, maddr address.Address, ts *types.TipSet) (address.Address, error) {
|
2019-10-23 17:39:14 +00:00
|
|
|
return stmgr.GetMinerWorkerRaw(ctx, mca.sm, ts.ParentState(), maddr)
|
2019-09-06 06:26:02 +00:00
|
|
|
}
|
|
|
|
|
2019-11-21 22:21:45 +00:00
|
|
|
func (mca mca) StateMinerSectorSize(ctx context.Context, maddr address.Address, ts *types.TipSet) (uint64, error) {
|
|
|
|
return stmgr.GetMinerSectorSize(ctx, mca.sm, ts, maddr)
|
|
|
|
}
|
|
|
|
|
2019-11-25 04:45:13 +00:00
|
|
|
func (mca mca) StateMinerProvingSet(ctx context.Context, maddr address.Address, ts *types.TipSet) ([]*api.ChainSectorInfo, error) {
|
|
|
|
return stmgr.GetMinerProvingSet(ctx, mca.sm, ts, maddr)
|
|
|
|
}
|
|
|
|
|
2019-09-06 06:26:02 +00:00
|
|
|
func (mca mca) WalletSign(ctx context.Context, a address.Address, v []byte) (*types.Signature, error) {
|
|
|
|
return mca.w.Sign(ctx, a, v)
|
|
|
|
}
|
|
|
|
|
2019-11-21 22:21:45 +00:00
|
|
|
type ElectionPoStProver interface {
|
2019-11-25 04:45:13 +00:00
|
|
|
GenerateCandidates(context.Context, sectorbuilder.SortedPublicSectorInfo, []byte) ([]sectorbuilder.EPostCandidate, error)
|
|
|
|
ComputeProof(context.Context, sectorbuilder.SortedPublicSectorInfo, []byte, []sectorbuilder.EPostCandidate) ([]byte, error)
|
2019-11-21 22:21:45 +00:00
|
|
|
}
|
|
|
|
|
2019-12-02 12:51:16 +00:00
|
|
|
type eppProvider struct{}
|
2019-11-21 22:21:45 +00:00
|
|
|
|
2019-11-25 04:45:13 +00:00
|
|
|
func (epp *eppProvider) GenerateCandidates(ctx context.Context, _ sectorbuilder.SortedPublicSectorInfo, eprand []byte) ([]sectorbuilder.EPostCandidate, error) {
|
2019-11-21 22:21:45 +00:00
|
|
|
return []sectorbuilder.EPostCandidate{
|
2019-12-02 12:51:16 +00:00
|
|
|
{
|
2019-11-21 22:21:45 +00:00
|
|
|
SectorID: 1,
|
|
|
|
PartialTicket: [32]byte{},
|
|
|
|
Ticket: [32]byte{},
|
|
|
|
SectorChallengeIndex: 1,
|
|
|
|
},
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2019-11-25 04:45:13 +00:00
|
|
|
func (epp *eppProvider) ComputeProof(ctx context.Context, _ sectorbuilder.SortedPublicSectorInfo, eprand []byte, winners []sectorbuilder.EPostCandidate) ([]byte, error) {
|
2019-11-22 16:41:09 +00:00
|
|
|
|
|
|
|
return []byte("valid proof"), nil
|
2019-11-21 22:21:45 +00:00
|
|
|
}
|
|
|
|
|
2019-12-03 18:25:56 +00:00
|
|
|
type ProofInput struct {
|
|
|
|
sectors sectorbuilder.SortedPublicSectorInfo
|
2019-12-03 19:33:29 +00:00
|
|
|
hvrf []byte
|
2019-12-03 18:25:56 +00:00
|
|
|
winners []sectorbuilder.EPostCandidate
|
2019-12-03 19:33:29 +00:00
|
|
|
vrfout []byte
|
2019-12-03 18:25:56 +00:00
|
|
|
}
|
|
|
|
|
2019-12-03 21:27:07 +00:00
|
|
|
func IsRoundWinner(ctx context.Context, ts *types.TipSet, round int64, miner address.Address, epp ElectionPoStProver, a MiningCheckAPI) (*ProofInput, error) {
|
2019-11-19 15:53:00 +00:00
|
|
|
r, err := a.ChainGetRandomness(ctx, ts.Key(), round-build.EcRandomnessLookback)
|
2019-09-03 04:36:07 +00:00
|
|
|
if err != nil {
|
2019-12-03 21:27:07 +00:00
|
|
|
return nil, xerrors.Errorf("chain get randomness: %w", err)
|
2019-09-03 04:36:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
mworker, err := a.StateMinerWorker(ctx, miner, ts)
|
|
|
|
if err != nil {
|
2019-12-03 21:27:07 +00:00
|
|
|
return nil, xerrors.Errorf("failed to get miner worker: %w", err)
|
2019-09-03 04:36:07 +00:00
|
|
|
}
|
|
|
|
|
2019-11-21 22:21:45 +00:00
|
|
|
vrfout, err := ComputeVRF(ctx, a.WalletSign, mworker, miner, DSepElectionPost, r)
|
2019-09-03 04:36:07 +00:00
|
|
|
if err != nil {
|
2019-12-03 21:27:07 +00:00
|
|
|
return nil, xerrors.Errorf("failed to compute VRF: %w", err)
|
2019-09-03 04:36:07 +00:00
|
|
|
}
|
|
|
|
|
2019-11-25 04:45:13 +00:00
|
|
|
pset, err := a.StateMinerProvingSet(ctx, miner, ts)
|
|
|
|
if err != nil {
|
2019-12-03 21:27:07 +00:00
|
|
|
return nil, xerrors.Errorf("failed to load proving set for miner: %w", err)
|
2019-11-25 04:45:13 +00:00
|
|
|
}
|
2019-11-29 19:14:10 +00:00
|
|
|
if len(pset) == 0 {
|
2019-12-03 21:27:07 +00:00
|
|
|
return nil, nil
|
2019-11-29 19:14:10 +00:00
|
|
|
}
|
2019-11-25 04:45:13 +00:00
|
|
|
|
2019-11-27 01:46:17 +00:00
|
|
|
var sinfos []ffi.PublicSectorInfo
|
2019-11-25 04:45:13 +00:00
|
|
|
for _, s := range pset {
|
|
|
|
var commRa [32]byte
|
|
|
|
copy(commRa[:], s.CommR)
|
2019-11-27 01:46:17 +00:00
|
|
|
sinfos = append(sinfos, ffi.PublicSectorInfo{
|
2019-11-25 04:45:13 +00:00
|
|
|
SectorID: s.SectorID,
|
|
|
|
CommR: commRa,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
sectors := sectorbuilder.NewSortedPublicSectorInfo(sinfos)
|
|
|
|
|
2019-11-26 02:43:43 +00:00
|
|
|
hvrf := sha256.Sum256(vrfout)
|
|
|
|
candidates, err := epp.GenerateCandidates(ctx, sectors, hvrf[:])
|
2019-11-21 22:21:45 +00:00
|
|
|
if err != nil {
|
2019-12-03 21:27:07 +00:00
|
|
|
return nil, xerrors.Errorf("failed to generate electionPoSt candidates: %w", err)
|
2019-11-21 22:21:45 +00:00
|
|
|
}
|
|
|
|
|
2019-09-03 04:36:07 +00:00
|
|
|
pow, err := a.StateMinerPower(ctx, miner, ts)
|
|
|
|
if err != nil {
|
2019-12-03 21:27:07 +00:00
|
|
|
return nil, xerrors.Errorf("failed to check power: %w", err)
|
2019-09-03 04:36:07 +00:00
|
|
|
}
|
|
|
|
|
2019-11-21 22:21:45 +00:00
|
|
|
ssize, err := a.StateMinerSectorSize(ctx, miner, ts)
|
|
|
|
if err != nil {
|
2019-12-03 21:27:07 +00:00
|
|
|
return nil, xerrors.Errorf("failed to look up miners sector size: %w", err)
|
2019-11-21 22:21:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var winners []sectorbuilder.EPostCandidate
|
|
|
|
for _, c := range candidates {
|
2019-12-09 21:03:25 +00:00
|
|
|
if types.IsTicketWinner(c.PartialTicket[:], ssize, uint64(len(sinfos)), pow.TotalPower) {
|
2019-11-21 22:21:45 +00:00
|
|
|
winners = append(winners, c)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// no winners, sad
|
|
|
|
if len(winners) == 0 {
|
2019-12-03 21:27:07 +00:00
|
|
|
return nil, nil
|
2019-11-21 22:21:45 +00:00
|
|
|
}
|
|
|
|
|
2019-12-03 21:27:07 +00:00
|
|
|
return &ProofInput{
|
2019-12-03 18:25:56 +00:00
|
|
|
sectors: sectors,
|
|
|
|
hvrf: hvrf[:],
|
|
|
|
winners: winners,
|
|
|
|
vrfout: vrfout,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func ComputeProof(ctx context.Context, epp ElectionPoStProver, pi *ProofInput) (*types.EPostProof, error) {
|
|
|
|
proof, err := epp.ComputeProof(ctx, pi.sectors, pi.hvrf, pi.winners)
|
2019-11-21 22:21:45 +00:00
|
|
|
if err != nil {
|
2019-12-03 18:25:56 +00:00
|
|
|
return nil, xerrors.Errorf("failed to compute snark for election proof: %w", err)
|
2019-11-21 22:21:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ept := types.EPostProof{
|
|
|
|
Proof: proof,
|
2019-12-03 18:25:56 +00:00
|
|
|
PostRand: pi.vrfout,
|
2019-11-21 22:21:45 +00:00
|
|
|
}
|
2019-12-03 18:25:56 +00:00
|
|
|
for _, win := range pi.winners {
|
2019-12-06 08:32:57 +00:00
|
|
|
part := make([]byte, 32)
|
|
|
|
copy(part, win.PartialTicket[:])
|
2019-11-28 12:46:56 +00:00
|
|
|
ept.Candidates = append(ept.Candidates, types.EPostTicket{
|
2019-12-06 08:32:57 +00:00
|
|
|
Partial: part,
|
2019-11-21 22:21:45 +00:00
|
|
|
SectorID: win.SectorID,
|
|
|
|
ChallengeIndex: win.SectorChallengeIndex,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-12-03 18:25:56 +00:00
|
|
|
return &ept, nil
|
2019-09-03 04:36:07 +00:00
|
|
|
}
|
|
|
|
|
2019-09-06 06:26:02 +00:00
|
|
|
type SignFunc func(context.Context, address.Address, []byte) (*types.Signature, error)
|
2019-09-03 04:36:07 +00:00
|
|
|
|
2019-11-21 22:21:45 +00:00
|
|
|
const (
|
|
|
|
DSepTicket = 1
|
|
|
|
DSepElectionPost = 2
|
|
|
|
)
|
|
|
|
|
|
|
|
func hashVRFBase(personalization uint64, miner address.Address, input []byte) ([]byte, error) {
|
|
|
|
if miner.Protocol() != address.ID {
|
|
|
|
return nil, xerrors.Errorf("miner address for compute VRF must be an ID address")
|
|
|
|
}
|
|
|
|
|
|
|
|
var persbuf [8]byte
|
|
|
|
binary.LittleEndian.PutUint64(persbuf[:], personalization)
|
|
|
|
|
|
|
|
h := sha256.New()
|
|
|
|
h.Write(persbuf[:])
|
|
|
|
h.Write([]byte{0})
|
|
|
|
h.Write(input)
|
|
|
|
h.Write([]byte{0})
|
|
|
|
h.Write(miner.Bytes())
|
|
|
|
|
|
|
|
return h.Sum(nil), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func VerifyVRF(ctx context.Context, worker, miner address.Address, p uint64, input, vrfproof []byte) error {
|
2019-12-02 12:51:16 +00:00
|
|
|
_, span := trace.StartSpan(ctx, "VerifyVRF")
|
2019-11-21 22:21:45 +00:00
|
|
|
defer span.End()
|
|
|
|
|
|
|
|
vrfBase, err := hashVRFBase(p, miner, input)
|
|
|
|
if err != nil {
|
|
|
|
return xerrors.Errorf("computing vrf base failed: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
sig := &types.Signature{
|
|
|
|
Type: types.KTBLS,
|
|
|
|
Data: vrfproof,
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := sig.Verify(worker, vrfBase); err != nil {
|
|
|
|
return xerrors.Errorf("vrf was invalid: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func ComputeVRF(ctx context.Context, sign SignFunc, worker, miner address.Address, p uint64, input []byte) ([]byte, error) {
|
|
|
|
sigInput, err := hashVRFBase(p, miner, input)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
sig, err := sign(ctx, worker, sigInput)
|
2019-09-06 06:26:02 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-09-03 04:36:07 +00:00
|
|
|
|
2019-09-06 06:26:02 +00:00
|
|
|
if sig.Type != types.KTBLS {
|
|
|
|
return nil, fmt.Errorf("miner worker address was not a BLS key")
|
|
|
|
}
|
2019-09-03 04:36:07 +00:00
|
|
|
|
2019-09-06 06:26:02 +00:00
|
|
|
return sig.Data, nil
|
2019-09-03 04:36:07 +00:00
|
|
|
}
|