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-05-14 02:32:04 +00:00
|
|
|
|
2020-02-25 20:54:58 +00:00
|
|
|
"github.com/filecoin-project/specs-actors/actors/builtin"
|
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
|
|
|
"github.com/ipfs/go-hamt-ipld"
|
|
|
|
bstore "github.com/ipfs/go-ipfs-blockstore"
|
|
|
|
cbor "github.com/ipfs/go-ipld-cbor"
|
|
|
|
"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"
|
2020-02-11 20:48:03 +00:00
|
|
|
)
|
|
|
|
|
2020-02-12 00:58:55 +00:00
|
|
|
func SetupInitActor(bs bstore.Blockstore, netname string, initialActors []genesis.Actor) (*types.Actor, error) {
|
|
|
|
if len(initialActors) > MaxAccounts {
|
|
|
|
return nil, xerrors.New("too many initial actors")
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
|
|
|
cst := cbor.NewCborStore(bs)
|
2020-02-14 14:14:39 +00:00
|
|
|
amap := hamt.NewNode(cst, hamt.UseTreeBitWidth(5)) // TODO: use spec adt map
|
2020-02-11 20:48:03 +00:00
|
|
|
|
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 {
|
|
|
|
return nil, xerrors.Errorf("unsupported account type: %s", a.Type) // TODO: Support msig (skip here)
|
|
|
|
}
|
|
|
|
|
|
|
|
var ainfo genesis.AccountMeta
|
|
|
|
if err := json.Unmarshal(a.Meta, &ainfo); err != nil {
|
|
|
|
return nil, xerrors.Errorf("unmarshaling account meta: %w", err)
|
|
|
|
}
|
|
|
|
|
2020-02-21 17:13:50 +00:00
|
|
|
fmt.Printf("init set %s t0%d\n", ainfo.Owner, AccountStart+uint64(i))
|
|
|
|
|
2020-02-12 00:58:55 +00:00
|
|
|
if err := amap.Set(context.TODO(), string(ainfo.Owner.Bytes()), AccountStart+uint64(i)); err != nil {
|
2020-02-11 20:48:03 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-14 02:32:04 +00:00
|
|
|
if err := amap.Set(context.TODO(), string(RootVerifierAddr.Bytes()), 80); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-02-11 20:48:03 +00:00
|
|
|
if err := amap.Flush(context.TODO()); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
amapcid, err := cst.Put(context.TODO(), amap)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
ias.AddressMap = amapcid
|
|
|
|
|
|
|
|
statecid, err := cst.Put(context.TODO(), &ias)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
act := &types.Actor{
|
2020-02-25 20:54:58 +00:00
|
|
|
Code: builtin.InitActorCodeID,
|
2020-02-11 20:48:03 +00:00
|
|
|
Head: statecid,
|
|
|
|
}
|
|
|
|
|
|
|
|
return act, nil
|
|
|
|
}
|