lotus/chain/gen/genesis/genesis.go

412 lines
11 KiB
Go
Raw Normal View History

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-07-15 09:26:38 +00:00
"github.com/filecoin-project/specs-actors/actors/builtin/multisig"
2020-05-14 02:32:04 +00:00
"github.com/filecoin-project/specs-actors/actors/builtin/verifreg"
"github.com/filecoin-project/specs-actors/actors/runtime"
2020-07-15 09:26:38 +00:00
"github.com/filecoin-project/specs-actors/actors/util/adt"
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:
- 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
2020-07-20 15:03:27 +00:00
initact, err := SetupInitActor(bs, template.NetworkName, template.Accounts, template.VerifregRootKey)
2020-02-12 00:58:55 +00:00
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)
}
// Setup reward
rewact, err := SetupRewardActor(bs)
if err != nil {
return nil, xerrors.Errorf("setup init actor: %w", err)
}
err = state.SetActor(builtin.RewardActorAddr, rewact)
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 {
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 {
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)
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 {
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)
}
// Setup burnt-funds
2020-02-25 20:54:58 +00:00
err = state.SetActor(builtin.BurntFundsActorAddr, &types.Actor{
Code: builtin.AccountActorCodeID,
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
// Create accounts
2020-02-12 00:58:55 +00:00
for id, info := range template.Accounts {
2020-07-15 09:26:38 +00:00
if info.Type != genesis.TAccount && info.Type != genesis.TMultisig {
return nil, xerrors.New("unsupported account type")
2020-02-12 00:58:55 +00:00
}
ida, err := address.NewIDAddress(uint64(AccountStart + id))
if err != nil {
return nil, err
}
2020-07-20 15:03:27 +00:00
if err = createAccount(ctx, bs, cst, state, ida, info); err != nil {
return nil, err
2020-02-12 00:58:55 +00:00
}
}
2020-05-14 02:32:04 +00:00
vregroot, err := address.NewIDAddress(80)
if err != nil {
return nil, err
}
2020-07-20 15:03:27 +00:00
if err = createAccount(ctx, bs, cst, state, vregroot, template.VerifregRootKey); err != nil {
2020-05-14 02:32:04 +00:00
return nil, err
}
2020-07-20 08:28:45 +00:00
return state, nil
}
2020-07-20 15:03:27 +00:00
func createAccount(ctx context.Context, bs bstore.Blockstore, cst cbor.IpldStore, state *state.StateTree, ida address.Address, info genesis.Actor) error {
// var newAddress address.Address
if (info.Type == genesis.TAccount) {
var ainfo genesis.AccountMeta
if err := json.Unmarshal(info.Meta, &ainfo); err != nil {
return xerrors.Errorf("unmarshaling account meta: %w", err)
}
st, err := cst.Put(ctx, &account.State{Address: ainfo.Owner})
if err != nil {
return err
}
err = state.SetActor(ida, &types.Actor{
Code: builtin.AccountActorCodeID,
Balance: info.Balance,
Head: st,
})
if err != nil {
return xerrors.Errorf("setting account from actmap: %w", err)
}
} else if (info.Type == genesis.TMultisig) {
var ainfo genesis.MultisigMeta
if err := json.Unmarshal(info.Meta, &ainfo); err != nil {
return xerrors.Errorf("unmarshaling account meta: %w", err)
}
pending, err := adt.MakeEmptyMap(adt.WrapStore(ctx, cst)).Root()
if err != nil {
return xerrors.Errorf("failed to create empty map: %v", err)
}
st, err := cst.Put(ctx, &multisig.State{
Signers: ainfo.Signers,
NumApprovalsThreshold: uint64(ainfo.Threshold),
StartEpoch: abi.ChainEpoch(ainfo.VestingStart),
UnlockDuration: abi.ChainEpoch(ainfo.VestingDuration),
PendingTxns: pending,
InitialBalance: info.Balance,
})
if err != nil {
return err
}
err = state.SetActor(ida, &types.Actor{
Code: builtin.MultisigActorCodeID,
Balance: info.Balance,
Head: st,
})
if err != nil {
return xerrors.Errorf("setting account from actmap: %w", err)
}
2020-05-14 02:32:04 +00:00
}
2020-07-20 08:28:45 +00:00
return nil
2020-02-12 00:58:55 +00:00
}
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 {
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)
}
2020-07-20 15:03:27 +00:00
_, err = doExecValue(ctx, vm, builtin.VerifiedRegistryActorAddr, verifier, types.NewInt(0), builtin.MethodsVerifiedRegistry.AddVerifier, mustEnc(&verifreg.AddVerifierParams{
2020-05-14 02:32:04 +00:00
Address: verifier,
Allowance: abi.NewStoragePower(int64(sum)), // eh, close enough
2020-05-14 02:32:04 +00:00
}))
if err != nil {
2020-07-20 15:03:27 +00:00
return cid.Undef, xerrors.Errorf("failed to create verifier: %w", err)
2020-05-14 02:32:04 +00:00
}
for c, amt := range verifNeeds {
_, err := doExecValue(ctx, vm, builtin.VerifiedRegistryActorAddr, verifier, types.NewInt(0), builtin.MethodsVerifiedRegistry.AddVerifiedClient, mustEnc(&verifreg.AddVerifiedClientParams{
Address: c,
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)
}
}
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
}
func MakeGenesisBlock(ctx context.Context, bs bstore.Blockstore, sys runtime.Syscalls, template genesis.Template) (*GenesisBootstrap, error) {
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)
}
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 miners failed: %w", err)
2020-02-11 20:48:03 +00:00
}
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{
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,
Timestamp: template.Timestamp,
ElectionProof: new(types.ElectionProof),
2020-04-08 15:11:42 +00:00
BeaconEntries: []types.BeaconEntry{
{
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
}