2020-02-11 20:48:03 +00:00
|
|
|
package genesis
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2020-02-12 00:58:55 +00:00
|
|
|
"encoding/json"
|
2020-02-11 20:48:03 +00:00
|
|
|
|
|
|
|
"github.com/filecoin-project/go-amt-ipld/v2"
|
2020-05-14 02:32:04 +00:00
|
|
|
"github.com/filecoin-project/specs-actors/actors/abi"
|
2020-02-12 00:58:55 +00:00
|
|
|
"github.com/filecoin-project/specs-actors/actors/builtin"
|
|
|
|
"github.com/filecoin-project/specs-actors/actors/builtin/account"
|
2020-05-14 02:32:04 +00:00
|
|
|
"github.com/filecoin-project/specs-actors/actors/builtin/verifreg"
|
2020-02-18 07:15:30 +00:00
|
|
|
"github.com/filecoin-project/specs-actors/actors/runtime"
|
2020-02-11 20:48:03 +00:00
|
|
|
"github.com/ipfs/go-cid"
|
|
|
|
"github.com/ipfs/go-datastore"
|
|
|
|
bstore "github.com/ipfs/go-ipfs-blockstore"
|
|
|
|
cbor "github.com/ipfs/go-ipld-cbor"
|
|
|
|
logging "github.com/ipfs/go-log/v2"
|
|
|
|
"golang.org/x/xerrors"
|
|
|
|
|
|
|
|
"github.com/filecoin-project/go-address"
|
|
|
|
|
|
|
|
"github.com/filecoin-project/lotus/chain/state"
|
|
|
|
"github.com/filecoin-project/lotus/chain/store"
|
|
|
|
"github.com/filecoin-project/lotus/chain/types"
|
2020-05-14 02:32:04 +00:00
|
|
|
"github.com/filecoin-project/lotus/chain/vm"
|
2020-02-12 00:58:55 +00:00
|
|
|
"github.com/filecoin-project/lotus/genesis"
|
2020-02-11 20:48:03 +00:00
|
|
|
)
|
|
|
|
|
2020-02-12 00:58:55 +00:00
|
|
|
const AccountStart = 100
|
|
|
|
const MinerStart = 1000
|
|
|
|
const MaxAccounts = MinerStart - AccountStart
|
|
|
|
|
2020-02-11 20:48:03 +00:00
|
|
|
var log = logging.Logger("genesis")
|
|
|
|
|
|
|
|
type GenesisBootstrap struct {
|
|
|
|
Genesis *types.BlockHeader
|
|
|
|
}
|
2020-04-21 21:38:26 +00:00
|
|
|
|
2020-02-12 00:58:55 +00:00
|
|
|
/*
|
|
|
|
From a list of parameters, create a genesis block / initial state
|
|
|
|
|
|
|
|
The process:
|
2020-02-12 01:15:02 +00:00
|
|
|
- Bootstrap state (MakeInitialStateTree)
|
2020-02-12 22:12:11 +00:00
|
|
|
- Create empty state
|
2020-02-14 21:38:18 +00:00
|
|
|
- Create system actor
|
2020-02-12 22:12:11 +00:00
|
|
|
- Make init actor
|
2020-02-12 00:58:55 +00:00
|
|
|
- Create accounts mappings
|
|
|
|
- Set NextID to MinerStart
|
|
|
|
- Setup Reward (1.4B fil)
|
|
|
|
- Setup Cron
|
2020-02-12 22:12:11 +00:00
|
|
|
- Create empty power actor
|
2020-02-12 00:58:55 +00:00
|
|
|
- Create empty market
|
2020-04-21 18:25:43 +00:00
|
|
|
- Create verified registry
|
2020-02-12 00:58:55 +00:00
|
|
|
- Setup burnt fund address
|
2020-02-12 22:12:11 +00:00
|
|
|
- Initialize account / msig balances
|
2020-02-12 00:58:55 +00:00
|
|
|
- Instantiate early vm with genesis syscalls
|
|
|
|
- Create miners
|
2020-02-12 22:12:11 +00:00
|
|
|
- Each:
|
|
|
|
- power.CreateMiner, set msg value to PowerBalance
|
2020-02-12 00:58:55 +00:00
|
|
|
- market.AddFunds with correct value
|
2020-02-12 02:13:00 +00:00
|
|
|
- market.PublishDeals for related sectors
|
2020-06-26 13:23:52 +00:00
|
|
|
- Set network power in the power actor to what we'll have after genesis creation
|
|
|
|
- For each precommitted sector
|
|
|
|
- Get deal weight
|
|
|
|
- Calculate QA Power
|
|
|
|
- Remove fake power from the power actor
|
|
|
|
- Calculate pledge
|
|
|
|
- Precommit
|
|
|
|
- Confirm valid
|
2020-02-12 00:58:55 +00:00
|
|
|
|
|
|
|
Data Types:
|
|
|
|
|
|
|
|
PreSeal :{
|
2020-02-12 22:12:11 +00:00
|
|
|
CommR CID
|
|
|
|
CommD CID
|
|
|
|
SectorID SectorNumber
|
|
|
|
Deal market.DealProposal # Start at 0, self-deal!
|
2020-02-12 00:58:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Genesis: {
|
|
|
|
Accounts: [ # non-miner, non-singleton actors, max len = MaxAccounts
|
|
|
|
{
|
|
|
|
Type: "account" / "multisig",
|
|
|
|
Value: "attofil",
|
|
|
|
[Meta: {msig settings, account key..}]
|
|
|
|
},...
|
|
|
|
],
|
|
|
|
Miners: [
|
|
|
|
{
|
|
|
|
Owner, Worker Addr # ID
|
|
|
|
MarketBalance, PowerBalance TokenAmount
|
|
|
|
SectorSize uint64
|
|
|
|
PreSeals []PreSeal
|
|
|
|
},...
|
|
|
|
],
|
|
|
|
}
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
func MakeInitialStateTree(ctx context.Context, bs bstore.Blockstore, template genesis.Template) (*state.StateTree, error) {
|
|
|
|
// Create empty state tree
|
|
|
|
|
|
|
|
cst := cbor.NewCborStore(bs)
|
2020-02-17 17:19:06 +00:00
|
|
|
_, err := cst.Put(context.TODO(), []struct{}{})
|
|
|
|
if err != nil {
|
|
|
|
return nil, xerrors.Errorf("putting empty object: %w", err)
|
|
|
|
}
|
|
|
|
|
2020-02-12 00:58:55 +00:00
|
|
|
state, err := state.NewStateTree(cst)
|
|
|
|
if err != nil {
|
|
|
|
return nil, xerrors.Errorf("making new state tree: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
emptyobject, err := cst.Put(context.TODO(), []struct{}{})
|
|
|
|
if err != nil {
|
|
|
|
return nil, xerrors.Errorf("failed putting empty object: %w", err)
|
|
|
|
}
|
|
|
|
|
2020-02-14 21:38:18 +00:00
|
|
|
// Create system actor
|
|
|
|
|
|
|
|
sysact, err := SetupSystemActor(bs)
|
|
|
|
if err != nil {
|
|
|
|
return nil, xerrors.Errorf("setup init actor: %w", err)
|
|
|
|
}
|
2020-02-25 20:54:58 +00:00
|
|
|
if err := state.SetActor(builtin.SystemActorAddr, sysact); err != nil {
|
2020-02-14 21:38:18 +00:00
|
|
|
return nil, xerrors.Errorf("set init actor: %w", err)
|
|
|
|
}
|
|
|
|
|
2020-02-12 00:58:55 +00:00
|
|
|
// Create init actor
|
|
|
|
|
|
|
|
initact, err := SetupInitActor(bs, template.NetworkName, template.Accounts)
|
|
|
|
if err != nil {
|
|
|
|
return nil, xerrors.Errorf("setup init actor: %w", err)
|
|
|
|
}
|
2020-02-25 20:54:58 +00:00
|
|
|
if err := state.SetActor(builtin.InitActorAddr, initact); err != nil {
|
2020-02-12 00:58:55 +00:00
|
|
|
return nil, xerrors.Errorf("set init actor: %w", err)
|
|
|
|
}
|
|
|
|
|
2020-02-12 01:15:02 +00:00
|
|
|
// Setup reward
|
2020-03-04 21:21:24 +00:00
|
|
|
rewact, err := SetupRewardActor(bs)
|
|
|
|
if err != nil {
|
|
|
|
return nil, xerrors.Errorf("setup init actor: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = state.SetActor(builtin.RewardActorAddr, rewact)
|
2020-02-12 01:15:02 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, xerrors.Errorf("set network account actor: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Setup cron
|
|
|
|
cronact, err := SetupCronActor(bs)
|
|
|
|
if err != nil {
|
|
|
|
return nil, xerrors.Errorf("setup cron actor: %w", err)
|
|
|
|
}
|
2020-02-25 20:54:58 +00:00
|
|
|
if err := state.SetActor(builtin.CronActorAddr, cronact); err != nil {
|
2020-02-12 01:15:02 +00:00
|
|
|
return nil, xerrors.Errorf("set cron actor: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create empty power actor
|
|
|
|
spact, err := SetupStoragePowerActor(bs)
|
|
|
|
if err != nil {
|
|
|
|
return nil, xerrors.Errorf("setup storage market actor: %w", err)
|
|
|
|
}
|
2020-02-25 20:54:58 +00:00
|
|
|
if err := state.SetActor(builtin.StoragePowerActorAddr, spact); err != nil {
|
2020-02-12 01:15:02 +00:00
|
|
|
return nil, xerrors.Errorf("set storage market actor: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create empty market actor
|
2020-02-12 07:44:20 +00:00
|
|
|
marketact, err := SetupStorageMarketActor(bs)
|
2020-02-12 01:15:02 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, xerrors.Errorf("setup storage market actor: %w", err)
|
|
|
|
}
|
2020-02-25 20:54:58 +00:00
|
|
|
if err := state.SetActor(builtin.StorageMarketActorAddr, marketact); err != nil {
|
2020-02-12 01:15:02 +00:00
|
|
|
return nil, xerrors.Errorf("set market actor: %w", err)
|
|
|
|
}
|
|
|
|
|
2020-04-21 18:25:43 +00:00
|
|
|
// Create verified registry
|
|
|
|
verifact, err := SetupVerifiedRegistryActor(bs)
|
|
|
|
if err != nil {
|
|
|
|
return nil, xerrors.Errorf("setup storage market actor: %w", err)
|
|
|
|
}
|
|
|
|
if err := state.SetActor(builtin.VerifiedRegistryActorAddr, verifact); err != nil {
|
|
|
|
return nil, xerrors.Errorf("set market actor: %w", err)
|
|
|
|
}
|
|
|
|
|
2020-02-12 01:15:02 +00:00
|
|
|
// Setup burnt-funds
|
2020-02-25 20:54:58 +00:00
|
|
|
err = state.SetActor(builtin.BurntFundsActorAddr, &types.Actor{
|
|
|
|
Code: builtin.AccountActorCodeID,
|
2020-02-12 01:15:02 +00:00
|
|
|
Balance: types.NewInt(0),
|
|
|
|
Head: emptyobject,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, xerrors.Errorf("set burnt funds account actor: %w", err)
|
|
|
|
}
|
2020-02-12 00:58:55 +00:00
|
|
|
|
2020-02-12 01:15:02 +00:00
|
|
|
// Create accounts
|
2020-02-12 00:58:55 +00:00
|
|
|
for id, info := range template.Accounts {
|
|
|
|
if info.Type != genesis.TAccount {
|
|
|
|
return nil, xerrors.New("unsupported account type") // TODO: msigs
|
|
|
|
}
|
|
|
|
|
|
|
|
ida, err := address.NewIDAddress(uint64(AccountStart + id))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var ainfo genesis.AccountMeta
|
|
|
|
if err := json.Unmarshal(info.Meta, &ainfo); err != nil {
|
|
|
|
return nil, xerrors.Errorf("unmarshaling account meta: %w", err)
|
|
|
|
}
|
|
|
|
|
2020-02-14 14:14:39 +00:00
|
|
|
st, err := cst.Put(ctx, &account.State{Address: ainfo.Owner})
|
2020-02-12 00:58:55 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = state.SetActor(ida, &types.Actor{
|
2020-02-25 20:54:58 +00:00
|
|
|
Code: builtin.AccountActorCodeID,
|
2020-02-12 00:58:55 +00:00
|
|
|
Balance: info.Balance,
|
|
|
|
Head: st,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, xerrors.Errorf("setting account from actmap: %w", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-14 02:32:04 +00:00
|
|
|
vregroot, err := address.NewIDAddress(80)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
vrst, err := cst.Put(ctx, &account.State{Address: RootVerifierAddr})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = state.SetActor(vregroot, &types.Actor{
|
|
|
|
Code: builtin.AccountActorCodeID,
|
|
|
|
Balance: types.NewInt(0),
|
|
|
|
Head: vrst,
|
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, xerrors.Errorf("setting account from actmap: %w", err)
|
|
|
|
}
|
|
|
|
|
2020-02-12 00:58:55 +00:00
|
|
|
return state, nil
|
|
|
|
}
|
|
|
|
|
2020-05-14 02:32:04 +00:00
|
|
|
func VerifyPreSealedData(ctx context.Context, cs *store.ChainStore, stateroot cid.Cid, template genesis.Template) (cid.Cid, error) {
|
|
|
|
verifNeeds := make(map[address.Address]abi.PaddedPieceSize)
|
|
|
|
var sum abi.PaddedPieceSize
|
|
|
|
for _, m := range template.Miners {
|
|
|
|
for _, s := range m.Sectors {
|
2020-05-14 04:00:32 +00:00
|
|
|
amt := s.Deal.PieceSize
|
2020-05-14 02:32:04 +00:00
|
|
|
verifNeeds[s.Deal.Client] += amt
|
|
|
|
sum += amt
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
verifier, err := address.NewIDAddress(80)
|
|
|
|
if err != nil {
|
|
|
|
return cid.Undef, err
|
|
|
|
}
|
|
|
|
|
|
|
|
vm, err := vm.NewVM(stateroot, 0, &fakeRand{}, cs.Blockstore(), &fakedSigSyscalls{cs.VMSys()})
|
|
|
|
if err != nil {
|
|
|
|
return cid.Undef, xerrors.Errorf("failed to create NewVM: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = doExecValue(ctx, vm, builtin.VerifiedRegistryActorAddr, RootVerifierAddr, types.NewInt(0), builtin.MethodsVerifiedRegistry.AddVerifier, mustEnc(&verifreg.AddVerifierParams{
|
|
|
|
Address: verifier,
|
2020-05-14 04:00:32 +00:00
|
|
|
Allowance: abi.NewStoragePower(int64(sum)), // eh, close enough
|
2020-05-14 02:32:04 +00:00
|
|
|
|
|
|
|
}))
|
|
|
|
if err != nil {
|
|
|
|
return cid.Undef, xerrors.Errorf("failed to failed to create verifier: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
for c, amt := range verifNeeds {
|
|
|
|
_, err := doExecValue(ctx, vm, builtin.VerifiedRegistryActorAddr, verifier, types.NewInt(0), builtin.MethodsVerifiedRegistry.AddVerifiedClient, mustEnc(&verifreg.AddVerifiedClientParams{
|
|
|
|
Address: c,
|
2020-05-14 04:00:32 +00:00
|
|
|
Allowance: abi.NewStoragePower(int64(amt)),
|
2020-05-14 02:32:04 +00:00
|
|
|
}))
|
|
|
|
if err != nil {
|
|
|
|
return cid.Undef, xerrors.Errorf("failed to add verified client: %w", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-14 11:45:45 +00:00
|
|
|
st, err := vm.Flush(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return cid.Cid{}, xerrors.Errorf("vm flush: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return st, nil
|
2020-05-14 02:32:04 +00:00
|
|
|
}
|
|
|
|
|
2020-02-18 07:15:30 +00:00
|
|
|
func MakeGenesisBlock(ctx context.Context, bs bstore.Blockstore, sys runtime.Syscalls, template genesis.Template) (*GenesisBootstrap, error) {
|
2020-02-12 01:15:02 +00:00
|
|
|
st, err := MakeInitialStateTree(ctx, bs, template)
|
2020-02-11 20:48:03 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, xerrors.Errorf("make initial state tree failed: %w", err)
|
|
|
|
}
|
|
|
|
|
2020-02-12 01:15:02 +00:00
|
|
|
stateroot, err := st.Flush(ctx)
|
2020-02-11 20:48:03 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, xerrors.Errorf("flush state tree failed: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// temp chainstore
|
|
|
|
cs := store.NewChainStore(bs, datastore.NewMapDatastore(), sys)
|
2020-05-14 02:32:04 +00:00
|
|
|
|
|
|
|
// Verify PreSealed Data
|
|
|
|
stateroot, err = VerifyPreSealedData(ctx, cs, stateroot, template)
|
|
|
|
if err != nil {
|
|
|
|
return nil, xerrors.Errorf("failed to verify presealed data: %w", err)
|
|
|
|
}
|
|
|
|
|
2020-02-12 02:13:00 +00:00
|
|
|
stateroot, err = SetupStorageMiners(ctx, cs, stateroot, template.Miners)
|
2020-02-11 20:48:03 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, xerrors.Errorf("setup storage miners failed: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
cst := cbor.NewCborStore(bs)
|
|
|
|
|
|
|
|
emptyroot, err := amt.FromArray(ctx, cst, nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, xerrors.Errorf("amt build failed: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
mm := &types.MsgMeta{
|
|
|
|
BlsMessages: emptyroot,
|
|
|
|
SecpkMessages: emptyroot,
|
|
|
|
}
|
|
|
|
mmb, err := mm.ToStorageBlock()
|
|
|
|
if err != nil {
|
|
|
|
return nil, xerrors.Errorf("serializing msgmeta failed: %w", err)
|
|
|
|
}
|
|
|
|
if err := bs.Put(mmb); err != nil {
|
|
|
|
return nil, xerrors.Errorf("putting msgmeta block to blockstore: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Infof("Empty Genesis root: %s", emptyroot)
|
|
|
|
|
|
|
|
genesisticket := &types.Ticket{
|
|
|
|
VRFProof: []byte("vrf proof0000000vrf proof0000000"),
|
|
|
|
}
|
|
|
|
|
|
|
|
b := &types.BlockHeader{
|
2020-04-08 19:06:41 +00:00
|
|
|
Miner: builtin.SystemActorAddr,
|
|
|
|
Ticket: genesisticket,
|
2020-02-11 20:48:03 +00:00
|
|
|
Parents: []cid.Cid{},
|
|
|
|
Height: 0,
|
|
|
|
ParentWeight: types.NewInt(0),
|
|
|
|
ParentStateRoot: stateroot,
|
|
|
|
Messages: mmb.Cid(),
|
|
|
|
ParentMessageReceipts: emptyroot,
|
2020-03-21 22:25:00 +00:00
|
|
|
BLSAggregate: nil,
|
|
|
|
BlockSig: nil,
|
2020-02-12 01:15:02 +00:00
|
|
|
Timestamp: template.Timestamp,
|
2020-05-08 17:59:18 +00:00
|
|
|
ElectionProof: new(types.ElectionProof),
|
2020-04-08 15:11:42 +00:00
|
|
|
BeaconEntries: []types.BeaconEntry{
|
2020-04-08 19:37:04 +00:00
|
|
|
{
|
2020-04-08 15:11:42 +00:00
|
|
|
Round: 0,
|
|
|
|
Data: make([]byte, 32),
|
|
|
|
},
|
|
|
|
},
|
2020-02-11 20:48:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
sb, err := b.ToStorageBlock()
|
|
|
|
if err != nil {
|
|
|
|
return nil, xerrors.Errorf("serializing block header failed: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := bs.Put(sb); err != nil {
|
|
|
|
return nil, xerrors.Errorf("putting header to blockstore: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return &GenesisBootstrap{
|
|
|
|
Genesis: b,
|
|
|
|
}, nil
|
|
|
|
}
|