diff --git a/chain/gen/gen.go b/chain/gen/gen.go index d86b00ce1..f9bf90bfd 100644 --- a/chain/gen/gen.go +++ b/chain/gen/gen.go @@ -107,6 +107,17 @@ var DefaultVerifregRootkeyActor = genesis.Actor{ Meta: rootkeyMultisig.ActorMeta(), } +var remAccTestKey, _ = address.NewFromString("t1ceb34gnsc6qk5dt6n7xg6ycwzasjhbxm3iylkiy") +var remAccMeta = genesis.AccountMeta{ + Owner: remAccTestKey, +} + +var DefaultRemainderAccountActor = genesis.Actor{ + Type: genesis.TAccount, + Balance: big.NewInt(0), + Meta: remAccMeta.ActorMeta(), +} + func NewGeneratorWithSectors(numSectors int) (*ChainGen, error) { saminer.SupportedProofTypes = map[abi.RegisteredSealProof]struct{}{ abi.RegisteredSealProof_StackedDrg2KiBV1: {}, @@ -210,9 +221,10 @@ func NewGeneratorWithSectors(numSectors int) (*ChainGen, error) { *genm1, *genm2, }, - VerifregRootKey: DefaultVerifregRootkeyActor, - NetworkName: "", - Timestamp: uint64(build.Clock.Now().Add(-500 * time.Duration(build.BlockDelaySecs) * time.Second).Unix()), + VerifregRootKey: DefaultVerifregRootkeyActor, + RemainderAccount: DefaultRemainderAccountActor, + NetworkName: "", + Timestamp: uint64(build.Clock.Now().Add(-500 * time.Duration(build.BlockDelaySecs) * time.Second).Unix()), } genb, err := genesis2.MakeGenesisBlock(context.TODO(), bs, sys, tpl) diff --git a/chain/gen/genesis/genesis.go b/chain/gen/genesis/genesis.go index 2bc8ec27d..d779c5820 100644 --- a/chain/gen/genesis/genesis.go +++ b/chain/gen/genesis/genesis.go @@ -3,6 +3,7 @@ package genesis import ( "context" "encoding/json" + "fmt" "github.com/ipfs/go-cid" "github.com/ipfs/go-datastore" @@ -258,11 +259,47 @@ func MakeInitialStateTree(ctx context.Context, bs bstore.Blockstore, template ge Balance: types.NewInt(0), Head: verifierState, }) - if err != nil { return nil, nil, xerrors.Errorf("setting account from actmap: %w", err) } + totalFilAllocated := big.Zero() + + // flush as ForEach works on the HAMT + if _, err := state.Flush(ctx); err != nil { + return nil, nil, err + } + err = state.ForEach(func(addr address.Address, act *types.Actor) error { + totalFilAllocated = big.Add(totalFilAllocated, act.Balance) + return nil + }) + if err != nil { + return nil, nil, xerrors.Errorf("summing account balances in state tree: %w", err) + } + + totalFil := big.Mul(big.NewInt(int64(build.FilBase)), big.NewInt(int64(build.FilecoinPrecision))) + remainingFil := big.Sub(totalFil, totalFilAllocated) + if remainingFil.Sign() < 0 { + return nil, nil, xerrors.Errorf("somehow overallocated filecoin (allocated = %s)", types.FIL(totalFilAllocated)) + } + + remAccKey, err := address.NewIDAddress(90) + if err != nil { + return nil, nil, err + } + + if err := createAccount(ctx, bs, cst, state, remAccKey, template.RemainderAccount); err != nil { + return nil, nil, 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 } @@ -284,6 +321,7 @@ func createAccount(ctx context.Context, bs bstore.Blockstore, cst cbor.IpldStore 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 { @@ -313,9 +351,10 @@ func createAccount(ctx context.Context, bs bstore.Blockstore, cst cbor.IpldStore if err != nil { return xerrors.Errorf("setting account from actmap: %w", err) } + return nil } - return nil + return fmt.Errorf("failed to create account") } func VerifyPreSealedData(ctx context.Context, cs *store.ChainStore, stateroot cid.Cid, template genesis.Template, keyIDs map[address.Address]address.Address) (cid.Cid, error) { diff --git a/chain/gen/genesis/t01_init.go b/chain/gen/genesis/t01_init.go index 3cf0d66be..c3708329b 100644 --- a/chain/gen/genesis/t01_init.go +++ b/chain/gen/genesis/t01_init.go @@ -4,6 +4,7 @@ import ( "context" "encoding/json" "fmt" + "github.com/filecoin-project/go-address" "github.com/filecoin-project/specs-actors/actors/builtin" diff --git a/chain/stmgr/stmgr.go b/chain/stmgr/stmgr.go index 4a258f10f..55b562231 100644 --- a/chain/stmgr/stmgr.go +++ b/chain/stmgr/stmgr.go @@ -851,6 +851,7 @@ func (sm *StateManager) setupGenesisActors(ctx context.Context) error { } } else if act.Code == builtin.AccountActorCodeID { + // should exclude burnt funds actor and "remainder account actor" // should only ever be "faucet" accounts in testnets kaddr, err := address.NewFromBytes([]byte(k)) if err != nil { diff --git a/cmd/lotus-seed/genesis.go b/cmd/lotus-seed/genesis.go index 3a9545d2d..f2bff4d6e 100644 --- a/cmd/lotus-seed/genesis.go +++ b/cmd/lotus-seed/genesis.go @@ -48,10 +48,11 @@ var genesisNewCmd = &cli.Command{ return xerrors.New("seed genesis new [genesis.json]") } out := genesis.Template{ - Accounts: []genesis.Actor{}, - Miners: []genesis.Miner{}, - VerifregRootKey: gen.DefaultVerifregRootkeyActor, - NetworkName: cctx.String("network-name"), + Accounts: []genesis.Actor{}, + Miners: []genesis.Miner{}, + VerifregRootKey: gen.DefaultVerifregRootkeyActor, + RemainderAccount: gen.DefaultRemainderAccountActor, + NetworkName: cctx.String("network-name"), } if out.NetworkName == "" { out.NetworkName = "localnet-" + uuid.New().String() diff --git a/genesis/types.go b/genesis/types.go index 7d401fd0e..468a09067 100644 --- a/genesis/types.go +++ b/genesis/types.go @@ -79,5 +79,6 @@ type Template struct { NetworkName string Timestamp uint64 `json:",omitempty"` - VerifregRootKey Actor + VerifregRootKey Actor + RemainderAccount Actor } diff --git a/node/node_test.go b/node/node_test.go index 770ce1f5b..102fcc912 100644 --- a/node/node_test.go +++ b/node/node_test.go @@ -212,10 +212,11 @@ func builder(t *testing.T, nFull int, storage []test.StorageMiner) ([]test.TestN genms = append(genms, *genm) } templ := &genesis.Template{ - Accounts: genaccs, - Miners: genms, - Timestamp: uint64(time.Now().Unix() - 10000), // some time sufficiently far in the past - VerifregRootKey: gen.DefaultVerifregRootkeyActor, + Accounts: genaccs, + Miners: genms, + Timestamp: uint64(time.Now().Unix() - 10000), // some time sufficiently far in the past + VerifregRootKey: gen.DefaultVerifregRootkeyActor, + RemainderAccount: gen.DefaultRemainderAccountActor, } // END PRESEAL SECTION @@ -351,7 +352,7 @@ func mockSbBuilder(t *testing.T, nFull int, storage []test.StorageMiner) ([]test genaccs = append(genaccs, genesis.Actor{ Type: genesis.TAccount, - Balance: big.Mul(big.NewInt(400_000_000_000), types.NewInt(build.FilecoinPrecision)), + Balance: big.Mul(big.NewInt(400_000_000), types.NewInt(build.FilecoinPrecision)), Meta: (&genesis.AccountMeta{Owner: wk.Address}).ActorMeta(), }) @@ -362,10 +363,11 @@ func mockSbBuilder(t *testing.T, nFull int, storage []test.StorageMiner) ([]test genms = append(genms, *genm) } templ := &genesis.Template{ - Accounts: genaccs, - Miners: genms, - Timestamp: uint64(time.Now().Unix()) - (build.BlockDelaySecs * 20000), - VerifregRootKey: gen.DefaultVerifregRootkeyActor, + Accounts: genaccs, + Miners: genms, + Timestamp: uint64(time.Now().Unix()) - (build.BlockDelaySecs * 20000), + VerifregRootKey: gen.DefaultVerifregRootkeyActor, + RemainderAccount: gen.DefaultRemainderAccountActor, } // END PRESEAL SECTION