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-08-18 10:23:28 +00:00
|
|
|
|
2022-06-14 15:00:51 +00:00
|
|
|
cbor "github.com/ipfs/go-ipld-cbor"
|
|
|
|
cbg "github.com/whyrusleeping/cbor-gen"
|
|
|
|
"golang.org/x/xerrors"
|
2021-05-15 01:11:23 +00:00
|
|
|
|
2020-07-28 17:51:47 +00:00
|
|
|
"github.com/filecoin-project/go-address"
|
2020-09-14 20:31:03 +00:00
|
|
|
"github.com/filecoin-project/go-state-types/abi"
|
2022-09-06 15:49:29 +00:00
|
|
|
actorstypes "github.com/filecoin-project/go-state-types/actors"
|
2021-06-18 21:20:48 +00:00
|
|
|
"github.com/filecoin-project/go-state-types/big"
|
2020-07-23 00:14:54 +00:00
|
|
|
"github.com/filecoin-project/specs-actors/actors/util/adt"
|
2020-02-11 20:48:03 +00:00
|
|
|
|
2021-01-29 20:01:00 +00:00
|
|
|
bstore "github.com/filecoin-project/lotus/blockstore"
|
2022-06-14 15:00:51 +00:00
|
|
|
"github.com/filecoin-project/lotus/chain/actors"
|
|
|
|
init_ "github.com/filecoin-project/lotus/chain/actors/builtin/init"
|
2020-02-11 20:48:03 +00:00
|
|
|
"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
|
|
|
)
|
|
|
|
|
2022-09-06 15:49:29 +00:00
|
|
|
func SetupInitActor(ctx context.Context, bs bstore.Blockstore, netname string, initialActors []genesis.Actor, rootVerifier genesis.Actor, remainder genesis.Actor, av actorstypes.Version) (int64, *types.Actor, map[address.Address]address.Address, error) {
|
2020-02-12 00:58:55 +00:00
|
|
|
if len(initialActors) > MaxAccounts {
|
2020-08-19 19:54:33 +00:00
|
|
|
return 0, nil, nil, xerrors.New("too many initial actors")
|
2020-02-12 00:58:55 +00:00
|
|
|
}
|
|
|
|
|
2021-05-15 01:11:23 +00:00
|
|
|
cst := cbor.NewCborStore(bs)
|
|
|
|
ist, err := init_.MakeState(adt.WrapStore(ctx, cst), av, netname)
|
|
|
|
if err != nil {
|
|
|
|
return 0, nil, nil, err
|
|
|
|
}
|
2020-02-11 20:48:03 +00:00
|
|
|
|
2021-05-15 01:11:23 +00:00
|
|
|
if err = ist.SetNextID(MinerStart); err != nil {
|
|
|
|
return 0, nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
amap, err := ist.AddressMap()
|
|
|
|
if err != nil {
|
|
|
|
return 0, nil, nil, err
|
|
|
|
}
|
2020-02-11 20:48:03 +00:00
|
|
|
|
2020-07-28 17:51:47 +00:00
|
|
|
keyToId := map[address.Address]address.Address{}
|
2020-08-18 10:23:28 +00:00
|
|
|
counter := int64(AccountStart)
|
|
|
|
|
|
|
|
for _, a := range initialActors {
|
2020-07-15 09:26:38 +00:00
|
|
|
if a.Type == genesis.TMultisig {
|
2020-08-18 10:23:28 +00:00
|
|
|
var ainfo genesis.MultisigMeta
|
|
|
|
if err := json.Unmarshal(a.Meta, &ainfo); err != nil {
|
2020-08-19 19:54:33 +00:00
|
|
|
return 0, nil, nil, xerrors.Errorf("unmarshaling account meta: %w", err)
|
2020-08-18 10:23:28 +00:00
|
|
|
}
|
|
|
|
for _, e := range ainfo.Signers {
|
2020-08-19 09:25:38 +00:00
|
|
|
|
|
|
|
if _, ok := keyToId[e]; ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2020-08-18 10:23:28 +00:00
|
|
|
fmt.Printf("init set %s t0%d\n", e, counter)
|
|
|
|
|
|
|
|
value := cbg.CborInt(counter)
|
2020-09-14 20:31:03 +00:00
|
|
|
if err := amap.Put(abi.AddrKey(e), &value); err != nil {
|
2020-08-19 19:54:33 +00:00
|
|
|
return 0, nil, nil, err
|
2020-08-18 10:23:28 +00:00
|
|
|
}
|
|
|
|
counter = counter + 1
|
|
|
|
var err error
|
|
|
|
keyToId[e], err = address.NewIDAddress(uint64(value))
|
|
|
|
if err != nil {
|
2020-08-19 19:54:33 +00:00
|
|
|
return 0, nil, nil, err
|
2020-08-18 10:23:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
// Need to add actors for all multisigs too
|
2020-07-15 09:26:38 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2020-02-12 00:58:55 +00:00
|
|
|
if a.Type != genesis.TAccount {
|
2020-08-19 20:32:53 +00:00
|
|
|
return 0, nil, nil, xerrors.Errorf("unsupported account type: %s", a.Type)
|
2020-02-12 00:58:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var ainfo genesis.AccountMeta
|
|
|
|
if err := json.Unmarshal(a.Meta, &ainfo); err != nil {
|
2020-08-19 19:54:33 +00:00
|
|
|
return 0, nil, nil, xerrors.Errorf("unmarshaling account meta: %w", err)
|
2020-02-12 00:58:55 +00:00
|
|
|
}
|
|
|
|
|
2020-08-18 10:23:28 +00:00
|
|
|
fmt.Printf("init set %s t0%d\n", ainfo.Owner, counter)
|
2020-02-21 17:13:50 +00:00
|
|
|
|
2020-08-18 10:23:28 +00:00
|
|
|
value := cbg.CborInt(counter)
|
2020-09-14 20:31:03 +00:00
|
|
|
if err := amap.Put(abi.AddrKey(ainfo.Owner), &value); err != nil {
|
2020-08-19 19:54:33 +00:00
|
|
|
return 0, nil, nil, err
|
2020-07-28 17:51:47 +00:00
|
|
|
}
|
2020-08-18 10:23:28 +00:00
|
|
|
counter = counter + 1
|
2020-07-28 17:51:47 +00:00
|
|
|
|
|
|
|
var err error
|
|
|
|
keyToId[ainfo.Owner], err = address.NewIDAddress(uint64(value))
|
|
|
|
if err != nil {
|
2020-08-19 19:54:33 +00:00
|
|
|
return 0, nil, nil, err
|
2020-02-11 20:48:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-18 07:56:30 +00:00
|
|
|
setupMsig := func(meta json.RawMessage) error {
|
2020-08-18 10:23:28 +00:00
|
|
|
var ainfo genesis.MultisigMeta
|
2020-11-18 07:56:30 +00:00
|
|
|
if err := json.Unmarshal(meta, &ainfo); err != nil {
|
|
|
|
return xerrors.Errorf("unmarshaling account meta: %w", err)
|
2020-08-18 10:23:28 +00:00
|
|
|
}
|
|
|
|
for _, e := range ainfo.Signers {
|
2020-08-19 10:03:59 +00:00
|
|
|
if _, ok := keyToId[e]; ok {
|
|
|
|
continue
|
|
|
|
}
|
2020-08-18 10:23:28 +00:00
|
|
|
fmt.Printf("init set %s t0%d\n", e, counter)
|
|
|
|
|
|
|
|
value := cbg.CborInt(counter)
|
2020-09-14 20:31:03 +00:00
|
|
|
if err := amap.Put(abi.AddrKey(e), &value); err != nil {
|
2020-11-18 07:56:30 +00:00
|
|
|
return err
|
2020-08-18 10:23:28 +00:00
|
|
|
}
|
|
|
|
counter = counter + 1
|
|
|
|
var err error
|
|
|
|
keyToId[e], err = address.NewIDAddress(uint64(value))
|
|
|
|
if err != nil {
|
2020-11-18 07:56:30 +00:00
|
|
|
return err
|
2020-08-18 10:23:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2020-11-18 07:56:30 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if rootVerifier.Type == genesis.TAccount {
|
|
|
|
var ainfo genesis.AccountMeta
|
|
|
|
if err := json.Unmarshal(rootVerifier.Meta, &ainfo); err != nil {
|
|
|
|
return 0, nil, nil, xerrors.Errorf("unmarshaling account meta: %w", err)
|
|
|
|
}
|
|
|
|
value := cbg.CborInt(80)
|
|
|
|
if err := amap.Put(abi.AddrKey(ainfo.Owner), &value); err != nil {
|
|
|
|
return 0, nil, nil, err
|
|
|
|
}
|
|
|
|
} else if rootVerifier.Type == genesis.TMultisig {
|
|
|
|
err := setupMsig(rootVerifier.Meta)
|
|
|
|
if err != nil {
|
|
|
|
return 0, nil, nil, xerrors.Errorf("setting up root verifier msig: %w", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-12 21:58:43 +00:00
|
|
|
if remainder.Type == genesis.TAccount {
|
|
|
|
var ainfo genesis.AccountMeta
|
|
|
|
if err := json.Unmarshal(remainder.Meta, &ainfo); err != nil {
|
|
|
|
return 0, nil, nil, xerrors.Errorf("unmarshaling account meta: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: Use builtin.ReserveAddress...
|
|
|
|
value := cbg.CborInt(90)
|
|
|
|
if err := amap.Put(abi.AddrKey(ainfo.Owner), &value); err != nil {
|
|
|
|
return 0, nil, nil, err
|
|
|
|
}
|
|
|
|
} else if remainder.Type == genesis.TMultisig {
|
|
|
|
err := setupMsig(remainder.Meta)
|
|
|
|
if err != nil {
|
|
|
|
return 0, nil, nil, xerrors.Errorf("setting up remainder msig: %w", err)
|
|
|
|
}
|
2020-05-14 02:32:04 +00:00
|
|
|
}
|
|
|
|
|
2020-07-23 00:14:54 +00:00
|
|
|
amapaddr, err := amap.Root()
|
2020-02-11 20:48:03 +00:00
|
|
|
if err != nil {
|
2020-08-19 19:54:33 +00:00
|
|
|
return 0, nil, nil, err
|
2020-02-11 20:48:03 +00:00
|
|
|
}
|
|
|
|
|
2021-05-15 01:11:23 +00:00
|
|
|
if err = ist.SetAddressMap(amapaddr); err != nil {
|
|
|
|
return 0, nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
statecid, err := cst.Put(ctx, ist.GetState())
|
|
|
|
if err != nil {
|
|
|
|
return 0, nil, nil, err
|
|
|
|
}
|
|
|
|
|
2022-06-29 16:54:14 +00:00
|
|
|
actcid, ok := actors.GetActorCodeID(av, actors.InitKey)
|
|
|
|
if !ok {
|
|
|
|
return 0, nil, nil, xerrors.Errorf("failed to get init actor code ID for actors version %d", av)
|
2020-02-11 20:48:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
act := &types.Actor{
|
2021-06-18 21:20:48 +00:00
|
|
|
Code: actcid,
|
|
|
|
Head: statecid,
|
|
|
|
Balance: big.Zero(),
|
2020-02-11 20:48:03 +00:00
|
|
|
}
|
|
|
|
|
2020-08-19 19:54:33 +00:00
|
|
|
return counter, act, keyToId, nil
|
2020-02-11 20:48:03 +00:00
|
|
|
}
|