lotus/chain/gen/genesis/genesis.go

288 lines
7.8 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-02-27 21:45:31 +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/abi/big"
"github.com/filecoin-project/specs-actors/actors/builtin"
"github.com/filecoin-project/specs-actors/actors/builtin/account"
2020-02-12 23:52:36 +00:00
"github.com/filecoin-project/specs-actors/actors/crypto"
"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/build"
"github.com/filecoin-project/lotus/chain/state"
"github.com/filecoin-project/lotus/chain/store"
"github.com/filecoin-project/lotus/chain/types"
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-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
- 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-02-12 22:12:11 +00:00
- Set precommits
- Commit presealed sectors
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)
}
// Setup reward
2020-02-25 20:54:58 +00:00
err = state.SetActor(builtin.RewardActorAddr, &types.Actor{
Code: builtin.RewardActorCodeID,
Balance: big.Int{Int: build.InitialReward},
Head: emptyobject, // TODO ?
})
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)
}
// 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 {
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)
}
}
return state, nil
}
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-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-02-25 20:54:58 +00:00
Miner: builtin.InitActorAddr,
2020-02-11 20:48:03 +00:00
Ticket: genesisticket,
EPostProof: types.EPostProof{
2020-02-27 21:45:31 +00:00
Proofs: []abi.PoStProof{{ProofBytes: []byte("not a real proof")}},
2020-02-11 20:48:03 +00:00
PostRand: []byte("i guess this is kinda random"),
},
Parents: []cid.Cid{},
Height: 0,
ParentWeight: types.NewInt(0),
ParentStateRoot: stateroot,
Messages: mmb.Cid(),
ParentMessageReceipts: emptyroot,
2020-02-12 23:52:36 +00:00
BLSAggregate: crypto.Signature{Type: crypto.SigTypeBLS, Data: []byte("signatureeee")},
BlockSig: &crypto.Signature{Type: crypto.SigTypeBLS, Data: []byte("block signatureeee")},
Timestamp: template.Timestamp,
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
}