From 64e18131c13b483da7f92f11f9ad2a3fd43eb47a Mon Sep 17 00:00:00 2001 From: whyrusleeping Date: Mon, 17 Aug 2020 23:38:58 -0700 Subject: [PATCH 1/6] place the remainder of unallocated funds in a 'remainder' account --- chain/gen/genesis/genesis.go | 24 +++++++++++++++++++++++- chain/gen/genesis/t01_init.go | 1 + genesis/types.go | 3 ++- 3 files changed, 26 insertions(+), 2 deletions(-) diff --git a/chain/gen/genesis/genesis.go b/chain/gen/genesis/genesis.go index bb6fc467d..434b727be 100644 --- a/chain/gen/genesis/genesis.go +++ b/chain/gen/genesis/genesis.go @@ -258,11 +258,33 @@ 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() + 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) + + template.RemainderAccount.Balance = remainingFil + + 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 + } + return state, keyIDs, nil } 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/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 } From 8942c02f28634adcad2117564711284fde8dbf20 Mon Sep 17 00:00:00 2001 From: Aayush Rajasekaran Date: Tue, 18 Aug 2020 06:01:48 -0400 Subject: [PATCH 2/6] Add a reminder comment --- chain/stmgr/stmgr.go | 1 + 1 file changed, 1 insertion(+) diff --git a/chain/stmgr/stmgr.go b/chain/stmgr/stmgr.go index f4943cba3..662a860d1 100644 --- a/chain/stmgr/stmgr.go +++ b/chain/stmgr/stmgr.go @@ -850,6 +850,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 { From 42730bcf1c2ad756d511bc13b8618ec8903419f2 Mon Sep 17 00:00:00 2001 From: whyrusleeping Date: Tue, 18 Aug 2020 10:56:54 -0700 Subject: [PATCH 3/6] handle potential overallocation --- chain/gen/genesis/genesis.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/chain/gen/genesis/genesis.go b/chain/gen/genesis/genesis.go index 434b727be..80cf6f247 100644 --- a/chain/gen/genesis/genesis.go +++ b/chain/gen/genesis/genesis.go @@ -273,6 +273,9 @@ func MakeInitialStateTree(ctx context.Context, bs bstore.Blockstore, template ge 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)) + } template.RemainderAccount.Balance = remainingFil From 574a22de6cecba62e9d3aabb9738d09e8ac7077a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Magiera?= Date: Tue, 18 Aug 2020 22:45:43 +0200 Subject: [PATCH 4/6] genesis: Set remainder account balance correctly --- chain/gen/genesis/genesis.go | 16 ++++++++++++---- genesis/types.go | 3 +-- node/node_test.go | 2 +- 3 files changed, 14 insertions(+), 7 deletions(-) diff --git a/chain/gen/genesis/genesis.go b/chain/gen/genesis/genesis.go index 80cf6f247..5d0a1cbfb 100644 --- a/chain/gen/genesis/genesis.go +++ b/chain/gen/genesis/genesis.go @@ -263,6 +263,11 @@ func MakeInitialStateTree(ctx context.Context, bs bstore.Blockstore, template ge } 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 @@ -277,15 +282,18 @@ func MakeInitialStateTree(ctx context.Context, bs bstore.Blockstore, template ge return nil, nil, xerrors.Errorf("somehow overallocated filecoin (allocated = %s)", types.FIL(totalFilAllocated)) } - template.RemainderAccount.Balance = remainingFil - 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 diff --git a/genesis/types.go b/genesis/types.go index 468a09067..7d401fd0e 100644 --- a/genesis/types.go +++ b/genesis/types.go @@ -79,6 +79,5 @@ type Template struct { NetworkName string Timestamp uint64 `json:",omitempty"` - VerifregRootKey Actor - RemainderAccount Actor + VerifregRootKey Actor } diff --git a/node/node_test.go b/node/node_test.go index 770ce1f5b..f2df15ffd 100644 --- a/node/node_test.go +++ b/node/node_test.go @@ -351,7 +351,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(), }) From abca69b4760f4e45a7483a6180559dd129fd734c Mon Sep 17 00:00:00 2001 From: whyrusleeping Date: Tue, 18 Aug 2020 14:30:49 -0700 Subject: [PATCH 5/6] configurable remainder account --- chain/gen/gen.go | 18 +++++++++++++++--- chain/gen/genesis/genesis.go | 8 +++++++- genesis/types.go | 3 ++- 3 files changed, 24 insertions(+), 5 deletions(-) diff --git a/chain/gen/gen.go b/chain/gen/gen.go index bcf9c4f10..4d1a0e2ab 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 5d0a1cbfb..8c0a2632c 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" @@ -287,6 +288,9 @@ func MakeInitialStateTree(ctx context.Context, bs bstore.Blockstore, template ge 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, @@ -317,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 { @@ -346,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/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 } From 916bebadefe61c1a50cdd58671cbb232c1a7714f Mon Sep 17 00:00:00 2001 From: whyrusleeping Date: Tue, 18 Aug 2020 14:34:35 -0700 Subject: [PATCH 6/6] setup proper template defaults --- cmd/lotus-seed/genesis.go | 9 +++++---- node/node_test.go | 18 ++++++++++-------- 2 files changed, 15 insertions(+), 12 deletions(-) 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/node/node_test.go b/node/node_test.go index f2df15ffd..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 @@ -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