fixup ID allocation and a few other things

This commit is contained in:
whyrusleeping 2020-08-19 12:54:33 -07:00
parent 073efeeacb
commit 2ab202a03d
7 changed files with 162 additions and 122 deletions

View File

@ -135,7 +135,7 @@ func MakeInitialStateTree(ctx context.Context, bs bstore.Blockstore, template ge
// Create init actor // Create init actor
initact, keyIDs, err := SetupInitActor(bs, template.NetworkName, template.Accounts, template.VerifregRootKey) idStart, initact, keyIDs, err := SetupInitActor(bs, template.NetworkName, template.Accounts, template.VerifregRootKey)
if err != nil { if err != nil {
return nil, nil, xerrors.Errorf("setup init actor: %w", err) return nil, nil, xerrors.Errorf("setup init actor: %w", err)
} }
@ -202,29 +202,39 @@ func MakeInitialStateTree(ctx context.Context, bs bstore.Blockstore, template ge
} }
// Create accounts // Create accounts
for id, info := range template.Accounts { for _, info := range template.Accounts {
if info.Type != genesis.TAccount && info.Type != genesis.TMultisig {
switch info.Type {
case genesis.TAccount:
if err := createAccountActor(ctx, cst, state, info, keyIDs); err != nil {
return nil, nil, xerrors.Errorf("failed to create account actor: %w", err)
}
case genesis.TMultisig:
ida, err := address.NewIDAddress(uint64(idStart))
if err != nil {
return nil, nil, err
}
idStart++
if err := createMultisigAccount(ctx, bs, cst, state, ida, info, keyIDs); err != nil {
return nil, nil, err
}
default:
return nil, nil, xerrors.New("unsupported account type") return nil, nil, xerrors.New("unsupported account type")
} }
ida, err := address.NewIDAddress(uint64(AccountStart + id))
if err != nil {
return nil, nil, err
}
if err = createAccount(ctx, bs, cst, state, ida, info, keyIDs); err != nil {
return nil, nil, err
}
} }
vregroot, err := address.NewIDAddress(80) vregroot, err := address.NewIDAddress(80)
if err != nil { if err != nil {
return nil, nil, err return nil, nil, err
} }
_ = vregroot
if err = createAccount(ctx, bs, cst, state, vregroot, template.VerifregRootKey, keyIDs); err != nil { if err = createMultisigAccount(ctx, bs, cst, state, vregroot, template.VerifregRootKey, keyIDs); err != nil {
return nil, nil, err return nil, nil, xerrors.Errorf("failed to set up verified registry signer: %w", err)
} }
// Setup the first verifier as ID-address 81 // Setup the first verifier as ID-address 81
@ -265,6 +275,8 @@ func MakeInitialStateTree(ctx context.Context, bs bstore.Blockstore, template ge
totalFilAllocated := big.Zero() totalFilAllocated := big.Zero()
fmt.Println("finished basic setup, time for remainder")
// flush as ForEach works on the HAMT // flush as ForEach works on the HAMT
if _, err := state.Flush(ctx); err != nil { if _, err := state.Flush(ctx); err != nil {
return nil, nil, err return nil, nil, err
@ -288,97 +300,105 @@ func MakeInitialStateTree(ctx context.Context, bs bstore.Blockstore, template ge
return nil, nil, err return nil, nil, err
} }
if err := createAccount(ctx, bs, cst, state, remAccKey, template.RemainderAccount, keyIDs); err != nil { if err := createMultisigAccount(ctx, bs, cst, state, remAccKey, template.RemainderAccount, keyIDs); err != nil {
return nil, nil, err return nil, nil, xerrors.Errorf("failed to set up remainder account: %w", err)
}
err = state.SetActor(remAccKey, &types.Actor{
Code: builtin.AccountActorCodeID,
Balance: remainingFil,
Head: emptyobject,
})
if err != nil {
return nil, nil, xerrors.Errorf("set burnt funds account actor: %w", err)
} }
return state, keyIDs, nil return state, keyIDs, nil
} }
func createAccount(ctx context.Context, bs bstore.Blockstore, cst cbor.IpldStore, state *state.StateTree, ida address.Address, info genesis.Actor, keyIDs map[address.Address]address.Address) error { func createAccountActor(ctx context.Context, cst cbor.IpldStore, state *state.StateTree, info genesis.Actor, keyIDs map[address.Address]address.Address) error {
if info.Type == genesis.TAccount { var ainfo genesis.AccountMeta
var ainfo genesis.AccountMeta if err := json.Unmarshal(info.Meta, &ainfo); err != nil {
if err := json.Unmarshal(info.Meta, &ainfo); err != nil { return xerrors.Errorf("unmarshaling account meta: %w", err)
return xerrors.Errorf("unmarshaling account meta: %w", err) }
} fmt.Println("Creating account: ", ainfo.Owner, keyIDs[ainfo.Owner])
st, err := cst.Put(ctx, &account.State{Address: ainfo.Owner}) st, err := cst.Put(ctx, &account.State{Address: ainfo.Owner})
if err != nil { if err != nil {
return err 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)
}
return nil
} 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)
}
var signers []address.Address
for _, e := range ainfo.Signers {
idAddress, _ := keyIDs[e]
// Check if actor already exists
_, err := state.GetActor(e)
if err == nil {
continue
}
st, err := cst.Put(ctx, &account.State{Address: e})
if err != nil {
return err
}
err = state.SetActor(idAddress, &types.Actor{
Code: builtin.AccountActorCodeID,
Balance: types.NewInt(0),
Head: st,
})
if err != nil {
return xerrors.Errorf("setting account from actmap: %w", err)
}
signers = append(signers, idAddress)
}
st, err := cst.Put(ctx, &multisig.State{
Signers: 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)
}
return nil
} }
return fmt.Errorf("failed to create account") ida, ok := keyIDs[ainfo.Owner]
if !ok {
return fmt.Errorf("no registered ID for account actor: %s", ainfo.Owner)
}
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)
}
return nil
}
func createMultisigAccount(ctx context.Context, bs bstore.Blockstore, cst cbor.IpldStore, state *state.StateTree, ida address.Address, info genesis.Actor, keyIDs map[address.Address]address.Address) error {
if info.Type != genesis.TMultisig {
return fmt.Errorf("can only call createMultisigAccount with multisig Actor info")
}
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)
}
var signers []address.Address
fmt.Println("setting up multisig signers")
for _, e := range ainfo.Signers {
fmt.Println("Signer: ", e)
idAddress, ok := keyIDs[e]
fmt.Println("keyIDs map: ", idAddress, ok)
// Check if actor already exists
_, err := state.GetActor(e)
if err == nil {
fmt.Println("continue signer: ", idAddress)
signers = append(signers, idAddress)
continue
}
fmt.Println("Creating account actor for: ", e, idAddress)
st, err := cst.Put(ctx, &account.State{Address: e})
if err != nil {
return err
}
err = state.SetActor(idAddress, &types.Actor{
Code: builtin.AccountActorCodeID,
Balance: types.NewInt(0),
Head: st,
})
if err != nil {
return xerrors.Errorf("setting account from actmap: %w", err)
}
signers = append(signers, idAddress)
}
st, err := cst.Put(ctx, &multisig.State{
Signers: 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)
}
return nil
} }
func VerifyPreSealedData(ctx context.Context, cs *store.ChainStore, stateroot cid.Cid, template genesis.Template, keyIDs map[address.Address]address.Address) (cid.Cid, error) { func VerifyPreSealedData(ctx context.Context, cs *store.ChainStore, stateroot cid.Cid, template genesis.Template, keyIDs map[address.Address]address.Address) (cid.Cid, error) {
@ -399,11 +419,21 @@ func VerifyPreSealedData(ctx context.Context, cs *store.ChainStore, stateroot ci
return cid.Undef, xerrors.Errorf("failed to create NewVM: %w", err) return cid.Undef, xerrors.Errorf("failed to create NewVM: %w", err)
} }
for _, m := range template.Miners { for mi, m := range template.Miners {
/*
// Add the miner to the market actor's balance table
_, err = doExec(ctx, vm, builtin.StorageMarketActorAddr, m.Owner, builtin.MethodsMarket.AddBalance, mustEnc(&m.ID))
if err != nil {
return cid.Undef, xerrors.Errorf("failed to add balance to miner: %w", err)
}
*/
for si, s := range m.Sectors {
if s.Deal.Provider != m.ID {
return cid.Undef, xerrors.Errorf("Sector %d in miner %d in template had mismatch in provider and miner ID: %s != %s", si, mi, s.Deal.Provider, m.ID)
}
// Add the miner to the market actor's balance table
_, err = doExec(ctx, vm, builtin.StorageMarketActorAddr, m.Owner, builtin.MethodsMarket.AddBalance, mustEnc(adt.Empty))
for _, s := range m.Sectors {
amt := s.Deal.PieceSize amt := s.Deal.PieceSize
verifNeeds[keyIDs[s.Deal.Client]] += amt verifNeeds[keyIDs[s.Deal.Client]] += amt
sum += amt sum += amt

View File

@ -137,7 +137,7 @@ func SetupStorageMiners(ctx context.Context, cs *store.ChainStore, sroot cid.Cid
params := mustEnc(&minerInfos[i].maddr) params := mustEnc(&minerInfos[i].maddr)
_, err := doExecValue(ctx, vm, builtin.StorageMarketActorAddr, m.Worker, m.MarketBalance, builtin.MethodsMarket.AddBalance, params) _, err := doExecValue(ctx, vm, builtin.StorageMarketActorAddr, m.Worker, m.MarketBalance, builtin.MethodsMarket.AddBalance, params)
if err != nil { if err != nil {
return cid.Undef, xerrors.Errorf("failed to create genesis miner: %w", err) return cid.Undef, xerrors.Errorf("failed to create genesis miner (add balance): %w", err)
} }
} }
@ -149,7 +149,7 @@ func SetupStorageMiners(ctx context.Context, cs *store.ChainStore, sroot cid.Cid
ret, err := doExecValue(ctx, vm, builtin.StorageMarketActorAddr, m.Worker, big.Zero(), builtin.MethodsMarket.PublishStorageDeals, mustEnc(params)) ret, err := doExecValue(ctx, vm, builtin.StorageMarketActorAddr, m.Worker, big.Zero(), builtin.MethodsMarket.PublishStorageDeals, mustEnc(params))
if err != nil { if err != nil {
return xerrors.Errorf("failed to create genesis miner: %w", err) return xerrors.Errorf("failed to create genesis miner (publish deals): %w", err)
} }
var ids market.PublishStorageDealsReturn var ids market.PublishStorageDealsReturn
if err := ids.UnmarshalCBOR(bytes.NewReader(ret)); err != nil { if err := ids.UnmarshalCBOR(bytes.NewReader(ret)); err != nil {

View File

@ -20,9 +20,9 @@ import (
bstore "github.com/filecoin-project/lotus/lib/blockstore" bstore "github.com/filecoin-project/lotus/lib/blockstore"
) )
func SetupInitActor(bs bstore.Blockstore, netname string, initialActors []genesis.Actor, rootVerifier genesis.Actor) (*types.Actor, map[address.Address]address.Address, error) { func SetupInitActor(bs bstore.Blockstore, netname string, initialActors []genesis.Actor, rootVerifier genesis.Actor) (int64, *types.Actor, map[address.Address]address.Address, error) {
if len(initialActors) > MaxAccounts { if len(initialActors) > MaxAccounts {
return nil, nil, xerrors.New("too many initial actors") return 0, nil, nil, xerrors.New("too many initial actors")
} }
var ias init_.State var ias init_.State
@ -39,7 +39,7 @@ func SetupInitActor(bs bstore.Blockstore, netname string, initialActors []genesi
if a.Type == genesis.TMultisig { if a.Type == genesis.TMultisig {
var ainfo genesis.MultisigMeta var ainfo genesis.MultisigMeta
if err := json.Unmarshal(a.Meta, &ainfo); err != nil { if err := json.Unmarshal(a.Meta, &ainfo); err != nil {
return nil, nil, xerrors.Errorf("unmarshaling account meta: %w", err) return 0, nil, nil, xerrors.Errorf("unmarshaling account meta: %w", err)
} }
for _, e := range ainfo.Signers { for _, e := range ainfo.Signers {
@ -51,13 +51,13 @@ func SetupInitActor(bs bstore.Blockstore, netname string, initialActors []genesi
value := cbg.CborInt(counter) value := cbg.CborInt(counter)
if err := amap.Put(adt.AddrKey(e), &value); err != nil { if err := amap.Put(adt.AddrKey(e), &value); err != nil {
return nil, nil, err return 0, nil, nil, err
} }
counter = counter + 1 counter = counter + 1
var err error var err error
keyToId[e], err = address.NewIDAddress(uint64(value)) keyToId[e], err = address.NewIDAddress(uint64(value))
if err != nil { if err != nil {
return nil, nil, err return 0, nil, nil, err
} }
} }
@ -66,42 +66,42 @@ func SetupInitActor(bs bstore.Blockstore, netname string, initialActors []genesi
} }
if a.Type != genesis.TAccount { if a.Type != genesis.TAccount {
return nil, nil, xerrors.Errorf("unsupported account type: %s", a.Type) // TODO: Support msig (skip here) return 0, nil, nil, xerrors.Errorf("unsupported account type: %s", a.Type) // TODO: Support msig (skip here)
} }
var ainfo genesis.AccountMeta var ainfo genesis.AccountMeta
if err := json.Unmarshal(a.Meta, &ainfo); err != nil { if err := json.Unmarshal(a.Meta, &ainfo); err != nil {
return nil, nil, xerrors.Errorf("unmarshaling account meta: %w", err) return 0, nil, nil, xerrors.Errorf("unmarshaling account meta: %w", err)
} }
fmt.Printf("init set %s t0%d\n", ainfo.Owner, counter) fmt.Printf("init set %s t0%d\n", ainfo.Owner, counter)
value := cbg.CborInt(counter) value := cbg.CborInt(counter)
if err := amap.Put(adt.AddrKey(ainfo.Owner), &value); err != nil { if err := amap.Put(adt.AddrKey(ainfo.Owner), &value); err != nil {
return nil, nil, err return 0, nil, nil, err
} }
counter = counter + 1 counter = counter + 1
var err error var err error
keyToId[ainfo.Owner], err = address.NewIDAddress(uint64(value)) keyToId[ainfo.Owner], err = address.NewIDAddress(uint64(value))
if err != nil { if err != nil {
return nil, nil, err return 0, nil, nil, err
} }
} }
if rootVerifier.Type == genesis.TAccount { if rootVerifier.Type == genesis.TAccount {
var ainfo genesis.AccountMeta var ainfo genesis.AccountMeta
if err := json.Unmarshal(rootVerifier.Meta, &ainfo); err != nil { if err := json.Unmarshal(rootVerifier.Meta, &ainfo); err != nil {
return nil, nil, xerrors.Errorf("unmarshaling account meta: %w", err) return 0, nil, nil, xerrors.Errorf("unmarshaling account meta: %w", err)
} }
value := cbg.CborInt(80) value := cbg.CborInt(80)
if err := amap.Put(adt.AddrKey(ainfo.Owner), &value); err != nil { if err := amap.Put(adt.AddrKey(ainfo.Owner), &value); err != nil {
return nil, nil, err return 0, nil, nil, err
} }
} else if rootVerifier.Type == genesis.TMultisig { } else if rootVerifier.Type == genesis.TMultisig {
var ainfo genesis.MultisigMeta var ainfo genesis.MultisigMeta
if err := json.Unmarshal(rootVerifier.Meta, &ainfo); err != nil { if err := json.Unmarshal(rootVerifier.Meta, &ainfo); err != nil {
return nil, nil, xerrors.Errorf("unmarshaling account meta: %w", err) return 0, nil, nil, xerrors.Errorf("unmarshaling account meta: %w", err)
} }
for _, e := range ainfo.Signers { for _, e := range ainfo.Signers {
if _, ok := keyToId[e]; ok { if _, ok := keyToId[e]; ok {
@ -111,13 +111,13 @@ func SetupInitActor(bs bstore.Blockstore, netname string, initialActors []genesi
value := cbg.CborInt(counter) value := cbg.CborInt(counter)
if err := amap.Put(adt.AddrKey(e), &value); err != nil { if err := amap.Put(adt.AddrKey(e), &value); err != nil {
return nil, nil, err return 0, nil, nil, err
} }
counter = counter + 1 counter = counter + 1
var err error var err error
keyToId[e], err = address.NewIDAddress(uint64(value)) keyToId[e], err = address.NewIDAddress(uint64(value))
if err != nil { if err != nil {
return nil, nil, err return 0, nil, nil, err
} }
} }
@ -125,13 +125,13 @@ func SetupInitActor(bs bstore.Blockstore, netname string, initialActors []genesi
amapaddr, err := amap.Root() amapaddr, err := amap.Root()
if err != nil { if err != nil {
return nil, nil, err return 0, nil, nil, err
} }
ias.AddressMap = amapaddr ias.AddressMap = amapaddr
statecid, err := store.Put(store.Context(), &ias) statecid, err := store.Put(store.Context(), &ias)
if err != nil { if err != nil {
return nil, nil, err return 0, nil, nil, err
} }
act := &types.Actor{ act := &types.Actor{
@ -139,5 +139,5 @@ func SetupInitActor(bs bstore.Blockstore, netname string, initialActors []genesi
Head: statecid, Head: statecid,
} }
return act, keyToId, nil return counter, act, keyToId, nil
} }

View File

@ -28,7 +28,7 @@ func doExec(ctx context.Context, vm *vm.VM, to, from address.Address, method abi
func doExecValue(ctx context.Context, vm *vm.VM, to, from address.Address, value types.BigInt, method abi.MethodNum, params []byte) ([]byte, error) { func doExecValue(ctx context.Context, vm *vm.VM, to, from address.Address, value types.BigInt, method abi.MethodNum, params []byte) ([]byte, error) {
act, err := vm.StateTree().GetActor(from) act, err := vm.StateTree().GetActor(from)
if err != nil { if err != nil {
return nil, xerrors.Errorf("doExec failed to get from actor: %w", err) return nil, xerrors.Errorf("doExec failed to get from actor (%s): %w", from, err)
} }
ret, err := vm.ApplyImplicitMessage(ctx, &types.Message{ ret, err := vm.ApplyImplicitMessage(ctx, &types.Message{

View File

@ -114,6 +114,7 @@ func PreSeal(maddr address.Address, spt abi.RegisteredSealProof, offset abi.Sect
} }
miner := &genesis.Miner{ miner := &genesis.Miner{
ID: maddr,
Owner: minerAddr.Address, Owner: minerAddr.Address,
Worker: minerAddr.Address, Worker: minerAddr.Address,
MarketBalance: big.Zero(), MarketBalance: big.Zero(),

View File

@ -26,6 +26,7 @@ type PreSeal struct {
} }
type Miner struct { type Miner struct {
ID address.Address
Owner address.Address Owner address.Address
Worker address.Address Worker address.Address
PeerId peer.ID //nolint:golint PeerId peer.ID //nolint:golint

View File

@ -2,6 +2,8 @@ package full
import ( import (
"context" "context"
"encoding/json"
"fmt"
"github.com/filecoin-project/specs-actors/actors/abi/big" "github.com/filecoin-project/specs-actors/actors/abi/big"
@ -122,6 +124,12 @@ func (a *MsigAPI) MsigPropose(ctx context.Context, msig address.Address, to addr
Params: enc, Params: enc,
} }
b, err := json.Marshal(msg)
if err != nil {
panic(err)
}
fmt.Println("prpopose message: ", string(b))
smsg, err := a.MpoolAPI.MpoolPushMessage(ctx, msg, nil) smsg, err := a.MpoolAPI.MpoolPushMessage(ctx, msg, nil)
if err != nil { if err != nil {
return cid.Undef, xerrors.Errorf("failed to push message: %w", err) return cid.Undef, xerrors.Errorf("failed to push message: %w", err)