Skip to content
This repository has been archived by the owner on Dec 20, 2023. It is now read-only.

subnet initialization and subnet epoch #160

Merged
merged 3 commits into from
Apr 18, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions api/api_full.go
Original file line number Diff line number Diff line change
Expand Up @@ -896,6 +896,8 @@ type FullNode interface {
IPCListCheckpoints(ctx context.Context, sn sdk.SubnetID, from, to abi.ChainEpoch) ([]*gateway.BottomUpCheckpoint, error) //perm:read
IPCGetCheckpoint(ctx context.Context, sn sdk.SubnetID, epoch abi.ChainEpoch) (*gateway.BottomUpCheckpoint, error) //perm:read
IPCGetTopDownMsgs(ctx context.Context, gatewayAddr address.Address, sn sdk.SubnetID, tsk types.TipSetKey, nonce uint64) ([]*gateway.CrossMsg, error) //perm:read
IPCGetGenesisEpochForSubnet(ctx context.Context, gatewayAddr address.Address, sn sdk.SubnetID) (abi.ChainEpoch, error) //perm:read

// Serialized representation of IPC calls.
// This calls are serialized version of some of the IPC calls. They return directly the CBOR IPCGetCheckpointSerialized
// version of the output of the call. These are really convenient to use the same type of serialization used
Expand Down
15 changes: 15 additions & 0 deletions api/mocks/mock_full.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions api/proxy_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file modified build/actors/ipc-actors.car
Binary file not shown.
Binary file modified build/genesis/spacenet.car
Binary file not shown.
Binary file modified build/openrpc/full.json.gz
Binary file not shown.
Binary file modified build/openrpc/gateway.json.gz
Binary file not shown.
Binary file modified build/openrpc/miner.json.gz
Binary file not shown.
Binary file modified build/openrpc/worker.json.gz
Binary file not shown.
3 changes: 2 additions & 1 deletion chain/consensus/mir/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ func NewManager(ctx context.Context,
}

initialValidatorSet := membershipInfo.ValidatorSet
genesisEpoch := membershipInfo.GenesisEpoch
valSize := initialValidatorSet.Size()
// There needs to be at least one validator in the membership
if valSize == 0 {
Expand Down Expand Up @@ -157,7 +158,7 @@ func NewManager(ctx context.Context,
m.mirErrChan = make(chan error, 1)
m.mirCtx, m.mirCancel = context.WithCancel(context.Background())

m.stateManager, err = NewStateManager(ctx, initialMembership, m.confManager, node, ds, m.requestPool, cfg)
m.stateManager, err = NewStateManager(ctx, m.netName, initialMembership, abi.ChainEpoch(genesisEpoch), m.confManager, node, ds, m.requestPool, cfg)
if err != nil {
return nil, fmt.Errorf("validator %v failed to start mir state manager: %w", id, err)
}
Expand Down
32 changes: 31 additions & 1 deletion chain/consensus/mir/membership/membership.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"strings"

"github.com/consensus-shipyard/go-ipc-types/gateway"
"github.com/consensus-shipyard/go-ipc-types/sdk"
"github.com/consensus-shipyard/go-ipc-types/validator"
"github.com/multiformats/go-multiaddr"
Expand Down Expand Up @@ -41,6 +42,7 @@ func IsSourceValid(source string) error {
type Info struct {
MinValidators uint64
ValidatorSet *validator.Set
GenesisEpoch uint64
}

type Reader interface {
Expand Down Expand Up @@ -124,6 +126,7 @@ func NewOnChainMembershipClient(client rpc.JSONRPCRequestSender, subnet sdk.Subn
type AgentResponse struct {
ValidatorSet validator.Set `json:"validator_set"`
MinValidators uint64 `json:"min_validators"`
GenesisEpoch uint64 `json:"genesis_epoch"`
}

// GetMembershipInfo gets the membership config from the actor state.
Expand All @@ -142,6 +145,7 @@ func (c *OnChainMembership) GetMembershipInfo() (*Info, error) {
return &Info{
ValidatorSet: &resp.ValidatorSet,
MinValidators: resp.MinValidators,
GenesisEpoch: resp.GenesisEpoch,
}, nil
}

Expand Down Expand Up @@ -182,6 +186,31 @@ func NewSetMembershipMsg(gw address.Address, valSet *validator.Set) (*types.Sign
GasFeeCap: types.NewInt(0),
GasPremium: types.NewInt(0),
GasLimit: build.BlockGasLimit, // Make super sure this is never too little
Nonce: 0,
}
return &types.SignedMessage{Message: msg, Signature: crypto.Signature{Type: crypto.SigTypeDelegated}}, nil
}

// NewInitGenesisEpochMsg creates a new config message to initialize
// implicitly the subnet and set the genesis epoch for it.
func NewInitGenesisEpochMsg(gw address.Address, genesisEpoch abi.ChainEpoch) (*types.SignedMessage, error) {
params, err := actors.SerializeParams(&gateway.InitGenesisEpochParams{GenesisEpoch: genesisEpoch})
if err != nil {
return nil, err
}
msg := types.Message{
To: gw,
From: builtin.SystemActorAddr,
Value: abi.NewTokenAmount(0),
Method: builtin.MustGenerateFRCMethodNum("InitGenesisEpoch"),
Params: params,
GasFeeCap: types.NewInt(0),
GasPremium: types.NewInt(0),
GasLimit: build.BlockGasLimit, // Make super sure this is never too little
// the nonce must be different than other config messages for the case where
// all config messages are included in the same block, if not the one with the
// largest nonce will be discarded.
Nonce: 1,
}
return &types.SignedMessage{Message: msg, Signature: crypto.Signature{Type: crypto.SigTypeDelegated}}, nil
}
Expand All @@ -190,5 +219,6 @@ func NewSetMembershipMsg(gw address.Address, valSet *validator.Set) (*types.Sign
func IsConfigMsg(gw address.Address, msg *types.Message) bool {
return msg.To == gw &&
msg.From == builtin.SystemActorAddr &&
msg.Method == builtin.MustGenerateFRCMethodNum("SetMembership")
(msg.Method == builtin.MustGenerateFRCMethodNum("SetMembership") ||
msg.Method == builtin.MustGenerateFRCMethodNum("InitGenesisEpoch"))
}
23 changes: 19 additions & 4 deletions chain/consensus/mir/state_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"path"
"time"

"github.com/consensus-shipyard/go-ipc-types/sdk"
"github.com/consensus-shipyard/go-ipc-types/validator"
"github.com/ipfs/go-cid"
"github.com/ipfs/go-datastore"
Expand All @@ -28,6 +29,7 @@ import (
"github.com/filecoin-project/lotus/chain/gen/genesis"
"github.com/filecoin-project/lotus/chain/types"
ltypes "github.com/filecoin-project/lotus/chain/types"
"github.com/filecoin-project/lotus/node/modules/dtypes"
)

var (
Expand All @@ -52,7 +54,9 @@ type StateManager struct {
ctx context.Context

// Lotus API
api v1api.FullNode
api v1api.FullNode
netName dtypes.NetworkName
genesisEpoch abi.ChainEpoch

// The current epoch number.
currentEpoch t.EpochNr
Expand Down Expand Up @@ -97,7 +101,9 @@ type StateManager struct {

func NewStateManager(
ctx context.Context,
netName dtypes.NetworkName,
initialMembership map[t.NodeID]t.NodeAddress,
genesisEpoch abi.ChainEpoch,
cm *ConfigurationManager,
api v1api.FullNode,
ds db.DB,
Expand All @@ -106,6 +112,8 @@ func NewStateManager(
) (*StateManager, error) {
sm := StateManager{
ctx: ctx,
netName: netName,
genesisEpoch: genesisEpoch,
nextCheckpointChan: make(chan *checkpoint.StableCheckpoint, 1),
confManager: cm,
ds: ds,
Expand Down Expand Up @@ -292,14 +300,22 @@ func (sm *StateManager) ApplyTXs(txs []*requestpb.Request) error {

sm.height++

// Include initial membership into the block 1.
// Include initial configuration and subnet initialization into the block 1.
if sm.height == 1 {
info := sm.confManager.GetInitialMembershipInfo()
initialConfigMsg, err := membership.NewSetMembershipMsg(genesis.DefaultIPCGatewayAddr, info.ValidatorSet)
if err != nil {
return err
return xerrors.Errorf("error setting initial on-chain membership: %w", err)
}
valSetMsgs = append(valSetMsgs, initialConfigMsg)
// the rootnet doesn't need to be explicitly initialized.
if string(sm.netName) != sdk.RootStr {
initializeMsg, err := membership.NewInitGenesisEpochMsg(genesis.DefaultIPCGatewayAddr, sm.genesisEpoch)
if err != nil {
return xerrors.Errorf("error initializing subnet: %w", err)
}
valSetMsgs = append(valSetMsgs, initializeMsg)
}
}

// For each request in the batch
Expand Down Expand Up @@ -384,7 +400,6 @@ func (sm *StateManager) ApplyTXs(txs []*requestpb.Request) error {
if err != nil {
return xerrors.Errorf("validator %v unable to sync a block: %w", sm.id, err)
}

log.With("validator", sm.id).With("epoch", sm.currentEpoch).Infof("mined block %d : %v ", bh.Header.Height, bh.Header.Cid())

return nil
Expand Down
8 changes: 8 additions & 0 deletions chain/gen/genesis/f08_ipc_gateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"

"github.com/consensus-shipyard/go-ipc-types/gateway"
"github.com/consensus-shipyard/go-ipc-types/sdk"
ipctypes "github.com/consensus-shipyard/go-ipc-types/sdk"
"github.com/consensus-shipyard/go-ipc-types/voting"
cbor "github.com/ipfs/go-ipld-cbor"
Expand Down Expand Up @@ -46,6 +47,12 @@ func constructState(store adt.Store, network ipctypes.SubnetID, buPeriod, tdPeri
return nil, xerrors.Errorf("failed to create empty map: %w", err)
}

// if it is the rootnet no need to explicitly initialize the gateway.
initialized := false
if network == sdk.RootSubnet {
initialized = true
}

return &gateway.State{
NetworkName: network,
TotalSubnets: 0,
Expand All @@ -58,6 +65,7 @@ func constructState(store adt.Store, network ipctypes.SubnetID, buPeriod, tdPeri
BottomupNonce: 0,
AppliedTopdownNonce: 0,
TopDownCheckpointVoting: voting,
Initialized: initialized,
}, nil
}

Expand Down
26 changes: 23 additions & 3 deletions documentation/en/api-v1-unstable-methods.md
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@
* [IPCGetCheckpointSerialized](#IPCGetCheckpointSerialized)
* [IPCGetCheckpointTemplate](#IPCGetCheckpointTemplate)
* [IPCGetCheckpointTemplateSerialized](#IPCGetCheckpointTemplateSerialized)
* [IPCGetGenesisEpochForSubnet](#IPCGetGenesisEpochForSubnet)
* [IPCGetPrevCheckpointForChild](#IPCGetPrevCheckpointForChild)
* [IPCGetTopDownMsgs](#IPCGetTopDownMsgs)
* [IPCGetTopDownMsgsSerialized](#IPCGetTopDownMsgsSerialized)
Expand Down Expand Up @@ -3524,6 +3525,24 @@ Inputs:

Response: `"Ynl0ZSBhcnJheQ=="`

### IPCGetGenesisEpochForSubnet


Perms: read

Inputs:
```json
[
"f01234",
{
"Parent": "string value",
"Actor": "f01234"
}
]
```

Response: `10101`

### IPCGetPrevCheckpointForChild


Expand Down Expand Up @@ -3852,7 +3871,8 @@ Response:
},
"Sig": "Ynl0ZSBhcnJheQ=="
},
"AppliedBottomupNonce": 42
"AppliedBottomupNonce": 42,
"GenesisEpoch": 10101
}
]
```
Expand Down Expand Up @@ -3926,7 +3946,8 @@ Response:
"configuration_number": 42
},
"TotalWeight": "0"
}
},
"Initialized": true
}
```

Expand Down Expand Up @@ -3972,7 +3993,6 @@ Response:
"Genesis": "Ynl0ZSBhcnJheQ==",
"BottomUpCheckPeriod": 10101,
"TopDownCheckPeriod": 10101,
"GenesisEpoch": 10101,
"CommittedCheckpoints": {
"/": "bafy2bzacea3wsdh6y3a36tb3skempjoxqpuyompjbmfeyf34fi3uy6uue42v4"
},
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ require (
github.com/alecthomas/jsonschema v0.0.0-20200530073317-71f438968921
github.com/buger/goterm v1.0.3
github.com/chzyer/readline v1.5.0
github.com/consensus-shipyard/go-ipc-types v0.1.5-0.20230415084926-a980f00efdab
github.com/consensus-shipyard/go-ipc-types v0.1.5-0.20230417164942-65210e7fab95
github.com/containerd/cgroups v1.0.4
github.com/coreos/go-systemd/v22 v22.5.0
github.com/detailyang/go-fallocate v0.0.0-20180908115635-432fa640bd2e
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -179,8 +179,8 @@ github.com/cncf/xds/go v0.0.0-20211011173535-cb28da3451f1/go.mod h1:eXthEFrGJvWH
github.com/cockroachdb/datadriven v0.0.0-20190809214429-80d97fb3cbaa/go.mod h1:zn76sxSg3SzpJ0PPJaLDCu+Bu0Lg3sKTORVIj19EIF8=
github.com/codahale/hdrhistogram v0.0.0-20161010025455-3a0bb77429bd/go.mod h1:sE/e/2PUdi/liOCUjSTXgM1o87ZssimdTWN964YiIeI=
github.com/codegangsta/cli v1.20.0/go.mod h1:/qJNoX69yVSKu5o4jLyXAENLRyk1uhi7zkbQ3slBdOA=
github.com/consensus-shipyard/go-ipc-types v0.1.5-0.20230415084926-a980f00efdab h1:e2w5asoK16Xx5+yM5YLZWn2+efC0xlcFoFSCnsuvjVA=
github.com/consensus-shipyard/go-ipc-types v0.1.5-0.20230415084926-a980f00efdab/go.mod h1:oZQ3wFTjxmAQLFZrHkj7pagpNF7Oq+GOy/bpjjgD83A=
github.com/consensus-shipyard/go-ipc-types v0.1.5-0.20230417164942-65210e7fab95 h1:/S9hKA0N+NDmLFN8sP+YpPSxUdyhewBZKWB0yX1o85I=
github.com/consensus-shipyard/go-ipc-types v0.1.5-0.20230417164942-65210e7fab95/go.mod h1:oZQ3wFTjxmAQLFZrHkj7pagpNF7Oq+GOy/bpjjgD83A=
github.com/containerd/cgroups v0.0.0-20201119153540-4cbc285b3327/go.mod h1:ZJeTFisyysqgcCdecO57Dj79RfL0LNeGiFUqLYQRYLE=
github.com/containerd/cgroups v1.0.4 h1:jN/mbWBEaz+T1pi5OFtnkQ+8qnmEbAr1Oo1FRm5B0dA=
github.com/containerd/cgroups v1.0.4/go.mod h1:nLNQtsF7Sl2HxNebu77i1R0oDlhiTG+kO4JTrUzo6IA=
Expand Down
5 changes: 5 additions & 0 deletions itests/ipc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,11 @@ func TestIPCAccessors(t *testing.T) {

JoinSubnet(t, ctx, api, src, actorAddr)

e, err := api.IPCGetGenesisEpochForSubnet(ctx, genesis.DefaultIPCGatewayAddr, sn)
require.NoError(t, err)
// require that the genesis epoch is greater than zero
require.True(t, e > 0)

// get subnet actor state
_, err = api.IPCReadSubnetActorState(ctx, sn, types.EmptyTSK)
require.NoError(t, err)
Expand Down
17 changes: 17 additions & 0 deletions node/impl/ipc/ipc.go
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,23 @@ func (a *IPCAPI) IPCGetTopDownMsgsSerialized(ctx context.Context, gatewayAddr ad
return out, nil
}

// IPCGetGenesisEpochForSubnet returns the genesis epoch from which a subnet has been
// registered in the parent.
func (a *IPCAPI) IPCGetGenesisEpochForSubnet(ctx context.Context, gatewayAddr address.Address, sn sdk.SubnetID) (abi.ChainEpoch, error) {
st, err := a.IPCReadGatewayState(ctx, gatewayAddr, types.EmptyTSK)
if err != nil {
return 0, err
}
subnet, found, err := st.GetSubnet(a.Chain.ActorStore(ctx), sn)
if err != nil {
return 0, xerrors.Errorf("error getting subnet: %w", err)
}
if !found {
return 0, xerrors.Errorf("subnet not found in gateway")
}
return subnet.GenesisEpoch, nil
}

// readActorState reads the state of a specific actor at a specefic epoch determined by the tipset key.
//
// The function accepts the address actor and the tipSetKet from which to read the state as an input, along
Expand Down