lotus/chain/gen/genesis/t01_init.go

93 lines
2.4 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-21 17:13:50 +00:00
"fmt"
2020-07-28 17:51:47 +00:00
"github.com/filecoin-project/go-address"
2020-05-14 02:32:04 +00:00
2020-02-25 20:54:58 +00:00
"github.com/filecoin-project/specs-actors/actors/builtin"
"github.com/filecoin-project/specs-actors/actors/util/adt"
2020-02-11 20:48:03 +00:00
2020-02-25 20:35:15 +00:00
init_ "github.com/filecoin-project/specs-actors/actors/builtin/init"
2020-02-11 20:48:03 +00:00
cbor "github.com/ipfs/go-ipld-cbor"
cbg "github.com/whyrusleeping/cbor-gen"
2020-02-11 20:48:03 +00:00
"golang.org/x/xerrors"
"github.com/filecoin-project/lotus/chain/types"
2020-02-12 00:58:55 +00:00
"github.com/filecoin-project/lotus/genesis"
bstore "github.com/filecoin-project/lotus/lib/blockstore"
2020-02-11 20:48:03 +00:00
)
2020-07-28 17:51:47 +00:00
func SetupInitActor(bs bstore.Blockstore, netname string, initialActors []genesis.Actor, rootVerifier genesis.Actor) (*types.Actor, map[address.Address]address.Address, error) {
2020-02-12 00:58:55 +00:00
if len(initialActors) > MaxAccounts {
2020-07-28 17:51:47 +00:00
return nil, nil, xerrors.New("too many initial actors")
2020-02-12 00:58:55 +00:00
}
2020-02-25 20:35:15 +00:00
var ias init_.State
2020-02-12 00:58:55 +00:00
ias.NextID = MinerStart
ias.NetworkName = netname
2020-02-11 20:48:03 +00:00
store := adt.WrapStore(context.TODO(), cbor.NewCborStore(bs))
amap := adt.MakeEmptyMap(store)
2020-02-11 20:48:03 +00:00
2020-07-28 17:51:47 +00:00
keyToId := map[address.Address]address.Address{}
2020-02-12 00:58:55 +00:00
for i, a := range initialActors {
2020-07-15 09:26:38 +00:00
if a.Type == genesis.TMultisig {
continue
}
2020-02-12 00:58:55 +00:00
if a.Type != genesis.TAccount {
2020-07-28 17:51:47 +00:00
return nil, nil, xerrors.Errorf("unsupported account type: %s", a.Type) // TODO: Support msig (skip here)
2020-02-12 00:58:55 +00:00
}
var ainfo genesis.AccountMeta
if err := json.Unmarshal(a.Meta, &ainfo); err != nil {
2020-07-28 17:51:47 +00:00
return nil, nil, xerrors.Errorf("unmarshaling account meta: %w", err)
2020-02-12 00:58:55 +00:00
}
fmt.Printf("init set %s t0%d\n", ainfo.Owner, AccountStart+int64(i))
2020-02-21 17:13:50 +00:00
value := cbg.CborInt(AccountStart + int64(i))
if err := amap.Put(adt.AddrKey(ainfo.Owner), &value); err != nil {
2020-07-28 17:51:47 +00:00
return nil, nil, err
}
var err error
keyToId[ainfo.Owner], err = address.NewIDAddress(uint64(value))
if err != nil {
return nil, nil, err
2020-02-11 20:48:03 +00:00
}
}
2020-07-20 15:03:27 +00:00
if rootVerifier.Type == genesis.TAccount {
var ainfo genesis.AccountMeta
if err := json.Unmarshal(rootVerifier.Meta, &ainfo); err != nil {
2020-07-28 17:51:47 +00:00
return nil, nil, xerrors.Errorf("unmarshaling account meta: %w", err)
2020-07-20 15:03:27 +00:00
}
2020-07-24 09:34:48 +00:00
value := cbg.CborInt(80)
if err := amap.Put(adt.AddrKey(ainfo.Owner), &value); err != nil {
2020-07-28 17:51:47 +00:00
return nil, nil, err
2020-07-20 15:03:27 +00:00
}
2020-05-14 02:32:04 +00:00
}
amapaddr, err := amap.Root()
2020-02-11 20:48:03 +00:00
if err != nil {
2020-07-28 17:51:47 +00:00
return nil, nil, err
2020-02-11 20:48:03 +00:00
}
ias.AddressMap = amapaddr
2020-02-11 20:48:03 +00:00
statecid, err := store.Put(store.Context(), &ias)
2020-02-11 20:48:03 +00:00
if err != nil {
2020-07-28 17:51:47 +00:00
return nil, nil, err
2020-02-11 20:48:03 +00:00
}
act := &types.Actor{
2020-02-25 20:54:58 +00:00
Code: builtin.InitActorCodeID,
2020-02-11 20:48:03 +00:00
Head: statecid,
}
2020-07-28 17:51:47 +00:00
return act, keyToId, nil
2020-02-11 20:48:03 +00:00
}