From 73a944cf76d0f4e0d902770ade37c3dd2ba07bee Mon Sep 17 00:00:00 2001 From: Kishan Sagathiya Date: Tue, 11 Jul 2023 13:40:38 +0530 Subject: [PATCH] feat(lib/parachain): added parachain service and registering protocols (#3277) - Added parachain service - Registered collation and validation protocol - Confirmed that we can communicate with collators by talking to them in `run()` function --- dot/core/mock_runtime_instance_test.go | 24 +-- dot/mock_node_builder_test.go | 17 ++ dot/network/message.go | 2 + dot/node.go | 8 + dot/node_integration_test.go | 8 +- dot/services.go | 6 + dot/state/mocks_runtime_test.go | 24 +-- dot/sync/mock_runtime_test.go | 24 +-- lib/babe/inherents/parachain_inherents.go | 3 +- lib/babe/mocks/runtime.go | 24 +-- lib/blocktree/mocks_test.go | 24 +-- lib/crypto/sr25519/sr25519.go | 7 +- lib/crypto/sr25519/sr25519_test.go | 4 +- lib/grandpa/mocks_runtime_test.go | 24 +-- .../approval_distribution_message.go | 7 +- .../approval_distribution_message_test.go | 14 +- lib/parachain/available_data_fetching.go | 5 +- lib/parachain/available_data_fetching_test.go | 3 +- lib/parachain/chunk_fetching.go | 3 +- lib/parachain/chunk_fetching_test.go | 3 +- lib/parachain/collation_fetching.go | 7 +- lib/parachain/collation_fetching_test.go | 11 +- lib/parachain/collation_protocol.go | 91 ++++++++++- lib/parachain/collation_protocol_test.go | 34 ++-- lib/parachain/network_protocols.go | 83 ++++++++++ lib/parachain/service.go | 145 ++++++++++++++++++ lib/parachain/statement.go | 3 +- .../statement_distribution_message.go | 5 +- .../statement_distribution_message_test.go | 23 +-- lib/parachain/statement_fetching.go | 3 +- lib/parachain/statement_fetching_test.go | 17 +- lib/parachain/statement_test.go | 17 +- .../{ => types}/testdata/westend.yaml | 0 lib/parachain/{ => types}/types.go | 5 +- lib/parachain/{ => types}/types_test.go | 36 ++--- lib/parachain/validation_protocol.go | 94 ++++++++++++ lib/parachain/validation_protocol_test.go | 23 +++ lib/runtime/interface.go | 18 +-- lib/runtime/mocks/mocks.go | 24 +-- lib/runtime/wasmer/exports.go | 33 ++-- lib/runtime/wasmer/exports_test.go | 62 ++++---- 41 files changed, 737 insertions(+), 231 deletions(-) create mode 100644 lib/parachain/network_protocols.go create mode 100644 lib/parachain/service.go rename lib/parachain/{ => types}/testdata/westend.yaml (100%) rename lib/parachain/{ => types}/types.go (98%) rename lib/parachain/{ => types}/types_test.go (87%) create mode 100644 lib/parachain/validation_protocol.go create mode 100644 lib/parachain/validation_protocol_test.go diff --git a/dot/core/mock_runtime_instance_test.go b/dot/core/mock_runtime_instance_test.go index e671aee681..a02e92fdf8 100644 --- a/dot/core/mock_runtime_instance_test.go +++ b/dot/core/mock_runtime_instance_test.go @@ -11,7 +11,7 @@ import ( common "github.com/ChainSafe/gossamer/lib/common" ed25519 "github.com/ChainSafe/gossamer/lib/crypto/ed25519" keystore "github.com/ChainSafe/gossamer/lib/keystore" - parachain "github.com/ChainSafe/gossamer/lib/parachain" + parachaintypes "github.com/ChainSafe/gossamer/lib/parachain/types" runtime "github.com/ChainSafe/gossamer/lib/runtime" transaction "github.com/ChainSafe/gossamer/lib/transaction" scale "github.com/ChainSafe/gossamer/pkg/scale" @@ -371,10 +371,10 @@ func (mr *MockInstanceMockRecorder) ParachainHostCandidateEvents() *gomock.Call } // ParachainHostCandidatePendingAvailability mocks base method. -func (m *MockInstance) ParachainHostCandidatePendingAvailability(arg0 parachain.ParaID) (*parachain.CommittedCandidateReceipt, error) { +func (m *MockInstance) ParachainHostCandidatePendingAvailability(arg0 parachaintypes.ParaID) (*parachaintypes.CommittedCandidateReceipt, error) { m.ctrl.T.Helper() ret := m.ctrl.Call(m, "ParachainHostCandidatePendingAvailability", arg0) - ret0, _ := ret[0].(*parachain.CommittedCandidateReceipt) + ret0, _ := ret[0].(*parachaintypes.CommittedCandidateReceipt) ret1, _ := ret[1].(error) return ret0, ret1 } @@ -386,7 +386,7 @@ func (mr *MockInstanceMockRecorder) ParachainHostCandidatePendingAvailability(ar } // ParachainHostCheckValidationOutputs mocks base method. -func (m *MockInstance) ParachainHostCheckValidationOutputs(arg0 parachain.ParaID, arg1 parachain.CandidateCommitments) (bool, error) { +func (m *MockInstance) ParachainHostCheckValidationOutputs(arg0 parachaintypes.ParaID, arg1 parachaintypes.CandidateCommitments) (bool, error) { m.ctrl.T.Helper() ret := m.ctrl.Call(m, "ParachainHostCheckValidationOutputs", arg0, arg1) ret0, _ := ret[0].(bool) @@ -401,10 +401,10 @@ func (mr *MockInstanceMockRecorder) ParachainHostCheckValidationOutputs(arg0, ar } // ParachainHostSessionIndexForChild mocks base method. -func (m *MockInstance) ParachainHostSessionIndexForChild() (parachain.SessionIndex, error) { +func (m *MockInstance) ParachainHostSessionIndexForChild() (parachaintypes.SessionIndex, error) { m.ctrl.T.Helper() ret := m.ctrl.Call(m, "ParachainHostSessionIndexForChild") - ret0, _ := ret[0].(parachain.SessionIndex) + ret0, _ := ret[0].(parachaintypes.SessionIndex) ret1, _ := ret[1].(error) return ret0, ret1 } @@ -416,10 +416,10 @@ func (mr *MockInstanceMockRecorder) ParachainHostSessionIndexForChild() *gomock. } // ParachainHostSessionInfo mocks base method. -func (m *MockInstance) ParachainHostSessionInfo(arg0 parachain.SessionIndex) (*parachain.SessionInfo, error) { +func (m *MockInstance) ParachainHostSessionInfo(arg0 parachaintypes.SessionIndex) (*parachaintypes.SessionInfo, error) { m.ctrl.T.Helper() ret := m.ctrl.Call(m, "ParachainHostSessionInfo", arg0) - ret0, _ := ret[0].(*parachain.SessionInfo) + ret0, _ := ret[0].(*parachaintypes.SessionInfo) ret1, _ := ret[1].(error) return ret0, ret1 } @@ -431,10 +431,10 @@ func (mr *MockInstanceMockRecorder) ParachainHostSessionInfo(arg0 interface{}) * } // ParachainHostValidatorGroups mocks base method. -func (m *MockInstance) ParachainHostValidatorGroups() (*parachain.ValidatorGroups, error) { +func (m *MockInstance) ParachainHostValidatorGroups() (*parachaintypes.ValidatorGroups, error) { m.ctrl.T.Helper() ret := m.ctrl.Call(m, "ParachainHostValidatorGroups") - ret0, _ := ret[0].(*parachain.ValidatorGroups) + ret0, _ := ret[0].(*parachaintypes.ValidatorGroups) ret1, _ := ret[1].(error) return ret0, ret1 } @@ -446,10 +446,10 @@ func (mr *MockInstanceMockRecorder) ParachainHostValidatorGroups() *gomock.Call } // ParachainHostValidators mocks base method. -func (m *MockInstance) ParachainHostValidators() ([]parachain.ValidatorID, error) { +func (m *MockInstance) ParachainHostValidators() ([]parachaintypes.ValidatorID, error) { m.ctrl.T.Helper() ret := m.ctrl.Call(m, "ParachainHostValidators") - ret0, _ := ret[0].([]parachain.ValidatorID) + ret0, _ := ret[0].([]parachaintypes.ValidatorID) ret1, _ := ret[1].(error) return ret0, ret1 } diff --git a/dot/mock_node_builder_test.go b/dot/mock_node_builder_test.go index c6fed7321d..0e81b9da8b 100644 --- a/dot/mock_node_builder_test.go +++ b/dot/mock_node_builder_test.go @@ -17,8 +17,10 @@ import ( system "github.com/ChainSafe/gossamer/dot/system" types "github.com/ChainSafe/gossamer/dot/types" babe "github.com/ChainSafe/gossamer/lib/babe" + common "github.com/ChainSafe/gossamer/lib/common" grandpa "github.com/ChainSafe/gossamer/lib/grandpa" keystore "github.com/ChainSafe/gossamer/lib/keystore" + parachain "github.com/ChainSafe/gossamer/lib/parachain" runtime "github.com/ChainSafe/gossamer/lib/runtime" gomock "github.com/golang/mock/gomock" ) @@ -135,6 +137,21 @@ func (mr *MocknodeBuilderIfaceMockRecorder) createNetworkService(config, stateSr return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "createNetworkService", reflect.TypeOf((*MocknodeBuilderIface)(nil).createNetworkService), config, stateSrvc, telemetryMailer) } +// createParachainHostService mocks base method. +func (m *MocknodeBuilderIface) createParachainHostService(net *network.Service, genesishHash common.Hash) (*parachain.Service, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "createParachainHostService", net, genesishHash) + ret0, _ := ret[0].(*parachain.Service) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// createParachainHostService indicates an expected call of createParachainHostService. +func (mr *MocknodeBuilderIfaceMockRecorder) createParachainHostService(net, genesishHash interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "createParachainHostService", reflect.TypeOf((*MocknodeBuilderIface)(nil).createParachainHostService), net, genesishHash) +} + // createRPCService mocks base method. func (m *MocknodeBuilderIface) createRPCService(params rpcServiceSettings) (*rpc.HTTPServer, error) { m.ctrl.T.Helper() diff --git a/dot/network/message.go b/dot/network/message.go index 144abe78cf..0fd510034c 100644 --- a/dot/network/message.go +++ b/dot/network/message.go @@ -24,6 +24,8 @@ const ( blockAnnounceMsgType MessageType = iota + 3 transactionMsgType ConsensusMsgType + CollationMsgType + ValidationMsgType ) // Message must be implemented by all network messages diff --git a/dot/node.go b/dot/node.go index 6c7f2922ea..7dcd6563ef 100644 --- a/dot/node.go +++ b/dot/node.go @@ -32,6 +32,7 @@ import ( "github.com/ChainSafe/gossamer/lib/genesis" "github.com/ChainSafe/gossamer/lib/grandpa" "github.com/ChainSafe/gossamer/lib/keystore" + "github.com/ChainSafe/gossamer/lib/parachain" "github.com/ChainSafe/gossamer/lib/runtime" "github.com/ChainSafe/gossamer/lib/services" "github.com/ChainSafe/gossamer/lib/utils" @@ -63,6 +64,7 @@ type nodeBuilderIface interface { dh *digest.Handler) (*core.Service, error) createGRANDPAService(config *cfg.Config, st *state.Service, ks KeyStore, net *network.Service, telemetryMailer Telemetry) (*grandpa.Service, error) + createParachainHostService(net *network.Service, genesishHash common.Hash) (*parachain.Service, error) newSyncService(config *cfg.Config, st *state.Service, finalityGadget BlockJustificationVerifier, verifier *babe.VerificationManager, cs *core.Service, net *network.Service, telemetryMailer Telemetry) (*dotsync.Service, error) @@ -363,6 +365,12 @@ func newNode(config *cfg.Config, } nodeSrvcs = append(nodeSrvcs, fg) + phs, err := builder.createParachainHostService(networkSrvc, stateSrvc.Block.GenesisHash()) + if err != nil { + return nil, err + } + nodeSrvcs = append(nodeSrvcs, phs) + syncer, err := builder.newSyncService(config, stateSrvc, fg, ver, coreSrvc, networkSrvc, telemetryMailer) if err != nil { return nil, err diff --git a/dot/node_integration_test.go b/dot/node_integration_test.go index 283437a2af..0adba4f2d2 100644 --- a/dot/node_integration_test.go +++ b/dot/node_integration_test.go @@ -32,6 +32,7 @@ import ( "github.com/ChainSafe/gossamer/lib/genesis" "github.com/ChainSafe/gossamer/lib/grandpa" "github.com/ChainSafe/gossamer/lib/keystore" + "github.com/ChainSafe/gossamer/lib/parachain" "github.com/ChainSafe/gossamer/lib/runtime" wazero_runtime "github.com/ChainSafe/gossamer/lib/runtime/wazero" "github.com/ChainSafe/gossamer/lib/trie" @@ -86,7 +87,7 @@ func TestNewNode(t *testing.T) { assert.NoError(t, err) mockServiceRegistry := NewMockServiceRegisterer(ctrl) - mockServiceRegistry.EXPECT().RegisterService(gomock.Any()).Times(8) + mockServiceRegistry.EXPECT().RegisterService(gomock.Any()).Times(9) m := NewMocknodeBuilderIface(ctrl) m.EXPECT().isNodeInitialised(initConfig.BasePath).Return(nil) @@ -120,6 +121,9 @@ func TestNewNode(t *testing.T) { return stateSrvc, nil }) + phs, err := parachain.NewService(testNetworkService, common.Hash{}) + require.NoError(t, err) + m.EXPECT().createRuntimeStorage(gomock.AssignableToTypeOf(&state.Service{})).Return(&runtime. NodeStorage{}, nil) m.EXPECT().loadRuntime(initConfig, &runtime.NodeStorage{}, gomock.AssignableToTypeOf(&state.Service{}), @@ -150,6 +154,8 @@ func TestNewNode(t *testing.T) { }) m.EXPECT().createNetworkService(initConfig, gomock.AssignableToTypeOf(&state.Service{}), gomock.AssignableToTypeOf(&telemetry.Mailer{})).Return(testNetworkService, nil) + m.EXPECT().createParachainHostService(gomock.AssignableToTypeOf(&network.Service{}), + gomock.AssignableToTypeOf(common.Hash{})).Return(phs, nil) got, err := newNode(initConfig, ks, m, mockServiceRegistry) assert.NoError(t, err) diff --git a/dot/services.go b/dot/services.go index 11682556ff..5ccd5b5330 100644 --- a/dot/services.go +++ b/dot/services.go @@ -30,6 +30,7 @@ import ( "github.com/ChainSafe/gossamer/lib/crypto/sr25519" "github.com/ChainSafe/gossamer/lib/grandpa" "github.com/ChainSafe/gossamer/lib/keystore" + "github.com/ChainSafe/gossamer/lib/parachain" "github.com/ChainSafe/gossamer/lib/runtime" "github.com/ChainSafe/gossamer/lib/runtime/wasmer" wazero_runtime "github.com/ChainSafe/gossamer/lib/runtime/wazero" @@ -472,6 +473,11 @@ func (nodeBuilder) createGRANDPAService(config *cfg.Config, st *state.Service, k return grandpa.NewService(gsCfg) } +func (nodeBuilder) createParachainHostService(net *network.Service, genesisHash common.Hash) ( + *parachain.Service, error) { + return parachain.NewService(net, genesisHash) +} + func (nodeBuilder) createBlockVerifier(st *state.Service) *babe.VerificationManager { return babe.NewVerificationManager(st.Block, st.Slot, st.Epoch) } diff --git a/dot/state/mocks_runtime_test.go b/dot/state/mocks_runtime_test.go index 4029d21a02..4eeca31e69 100644 --- a/dot/state/mocks_runtime_test.go +++ b/dot/state/mocks_runtime_test.go @@ -11,7 +11,7 @@ import ( common "github.com/ChainSafe/gossamer/lib/common" ed25519 "github.com/ChainSafe/gossamer/lib/crypto/ed25519" keystore "github.com/ChainSafe/gossamer/lib/keystore" - parachain "github.com/ChainSafe/gossamer/lib/parachain" + parachaintypes "github.com/ChainSafe/gossamer/lib/parachain/types" runtime "github.com/ChainSafe/gossamer/lib/runtime" transaction "github.com/ChainSafe/gossamer/lib/transaction" scale "github.com/ChainSafe/gossamer/pkg/scale" @@ -371,10 +371,10 @@ func (mr *MockInstanceMockRecorder) ParachainHostCandidateEvents() *gomock.Call } // ParachainHostCandidatePendingAvailability mocks base method. -func (m *MockInstance) ParachainHostCandidatePendingAvailability(arg0 parachain.ParaID) (*parachain.CommittedCandidateReceipt, error) { +func (m *MockInstance) ParachainHostCandidatePendingAvailability(arg0 parachaintypes.ParaID) (*parachaintypes.CommittedCandidateReceipt, error) { m.ctrl.T.Helper() ret := m.ctrl.Call(m, "ParachainHostCandidatePendingAvailability", arg0) - ret0, _ := ret[0].(*parachain.CommittedCandidateReceipt) + ret0, _ := ret[0].(*parachaintypes.CommittedCandidateReceipt) ret1, _ := ret[1].(error) return ret0, ret1 } @@ -386,7 +386,7 @@ func (mr *MockInstanceMockRecorder) ParachainHostCandidatePendingAvailability(ar } // ParachainHostCheckValidationOutputs mocks base method. -func (m *MockInstance) ParachainHostCheckValidationOutputs(arg0 parachain.ParaID, arg1 parachain.CandidateCommitments) (bool, error) { +func (m *MockInstance) ParachainHostCheckValidationOutputs(arg0 parachaintypes.ParaID, arg1 parachaintypes.CandidateCommitments) (bool, error) { m.ctrl.T.Helper() ret := m.ctrl.Call(m, "ParachainHostCheckValidationOutputs", arg0, arg1) ret0, _ := ret[0].(bool) @@ -401,10 +401,10 @@ func (mr *MockInstanceMockRecorder) ParachainHostCheckValidationOutputs(arg0, ar } // ParachainHostSessionIndexForChild mocks base method. -func (m *MockInstance) ParachainHostSessionIndexForChild() (parachain.SessionIndex, error) { +func (m *MockInstance) ParachainHostSessionIndexForChild() (parachaintypes.SessionIndex, error) { m.ctrl.T.Helper() ret := m.ctrl.Call(m, "ParachainHostSessionIndexForChild") - ret0, _ := ret[0].(parachain.SessionIndex) + ret0, _ := ret[0].(parachaintypes.SessionIndex) ret1, _ := ret[1].(error) return ret0, ret1 } @@ -416,10 +416,10 @@ func (mr *MockInstanceMockRecorder) ParachainHostSessionIndexForChild() *gomock. } // ParachainHostSessionInfo mocks base method. -func (m *MockInstance) ParachainHostSessionInfo(arg0 parachain.SessionIndex) (*parachain.SessionInfo, error) { +func (m *MockInstance) ParachainHostSessionInfo(arg0 parachaintypes.SessionIndex) (*parachaintypes.SessionInfo, error) { m.ctrl.T.Helper() ret := m.ctrl.Call(m, "ParachainHostSessionInfo", arg0) - ret0, _ := ret[0].(*parachain.SessionInfo) + ret0, _ := ret[0].(*parachaintypes.SessionInfo) ret1, _ := ret[1].(error) return ret0, ret1 } @@ -431,10 +431,10 @@ func (mr *MockInstanceMockRecorder) ParachainHostSessionInfo(arg0 interface{}) * } // ParachainHostValidatorGroups mocks base method. -func (m *MockInstance) ParachainHostValidatorGroups() (*parachain.ValidatorGroups, error) { +func (m *MockInstance) ParachainHostValidatorGroups() (*parachaintypes.ValidatorGroups, error) { m.ctrl.T.Helper() ret := m.ctrl.Call(m, "ParachainHostValidatorGroups") - ret0, _ := ret[0].(*parachain.ValidatorGroups) + ret0, _ := ret[0].(*parachaintypes.ValidatorGroups) ret1, _ := ret[1].(error) return ret0, ret1 } @@ -446,10 +446,10 @@ func (mr *MockInstanceMockRecorder) ParachainHostValidatorGroups() *gomock.Call } // ParachainHostValidators mocks base method. -func (m *MockInstance) ParachainHostValidators() ([]parachain.ValidatorID, error) { +func (m *MockInstance) ParachainHostValidators() ([]parachaintypes.ValidatorID, error) { m.ctrl.T.Helper() ret := m.ctrl.Call(m, "ParachainHostValidators") - ret0, _ := ret[0].([]parachain.ValidatorID) + ret0, _ := ret[0].([]parachaintypes.ValidatorID) ret1, _ := ret[1].(error) return ret0, ret1 } diff --git a/dot/sync/mock_runtime_test.go b/dot/sync/mock_runtime_test.go index 36b3842e3c..004c535d73 100644 --- a/dot/sync/mock_runtime_test.go +++ b/dot/sync/mock_runtime_test.go @@ -11,7 +11,7 @@ import ( common "github.com/ChainSafe/gossamer/lib/common" ed25519 "github.com/ChainSafe/gossamer/lib/crypto/ed25519" keystore "github.com/ChainSafe/gossamer/lib/keystore" - parachain "github.com/ChainSafe/gossamer/lib/parachain" + parachaintypes "github.com/ChainSafe/gossamer/lib/parachain/types" runtime "github.com/ChainSafe/gossamer/lib/runtime" transaction "github.com/ChainSafe/gossamer/lib/transaction" scale "github.com/ChainSafe/gossamer/pkg/scale" @@ -371,10 +371,10 @@ func (mr *MockInstanceMockRecorder) ParachainHostCandidateEvents() *gomock.Call } // ParachainHostCandidatePendingAvailability mocks base method. -func (m *MockInstance) ParachainHostCandidatePendingAvailability(arg0 parachain.ParaID) (*parachain.CommittedCandidateReceipt, error) { +func (m *MockInstance) ParachainHostCandidatePendingAvailability(arg0 parachaintypes.ParaID) (*parachaintypes.CommittedCandidateReceipt, error) { m.ctrl.T.Helper() ret := m.ctrl.Call(m, "ParachainHostCandidatePendingAvailability", arg0) - ret0, _ := ret[0].(*parachain.CommittedCandidateReceipt) + ret0, _ := ret[0].(*parachaintypes.CommittedCandidateReceipt) ret1, _ := ret[1].(error) return ret0, ret1 } @@ -386,7 +386,7 @@ func (mr *MockInstanceMockRecorder) ParachainHostCandidatePendingAvailability(ar } // ParachainHostCheckValidationOutputs mocks base method. -func (m *MockInstance) ParachainHostCheckValidationOutputs(arg0 parachain.ParaID, arg1 parachain.CandidateCommitments) (bool, error) { +func (m *MockInstance) ParachainHostCheckValidationOutputs(arg0 parachaintypes.ParaID, arg1 parachaintypes.CandidateCommitments) (bool, error) { m.ctrl.T.Helper() ret := m.ctrl.Call(m, "ParachainHostCheckValidationOutputs", arg0, arg1) ret0, _ := ret[0].(bool) @@ -401,10 +401,10 @@ func (mr *MockInstanceMockRecorder) ParachainHostCheckValidationOutputs(arg0, ar } // ParachainHostSessionIndexForChild mocks base method. -func (m *MockInstance) ParachainHostSessionIndexForChild() (parachain.SessionIndex, error) { +func (m *MockInstance) ParachainHostSessionIndexForChild() (parachaintypes.SessionIndex, error) { m.ctrl.T.Helper() ret := m.ctrl.Call(m, "ParachainHostSessionIndexForChild") - ret0, _ := ret[0].(parachain.SessionIndex) + ret0, _ := ret[0].(parachaintypes.SessionIndex) ret1, _ := ret[1].(error) return ret0, ret1 } @@ -416,10 +416,10 @@ func (mr *MockInstanceMockRecorder) ParachainHostSessionIndexForChild() *gomock. } // ParachainHostSessionInfo mocks base method. -func (m *MockInstance) ParachainHostSessionInfo(arg0 parachain.SessionIndex) (*parachain.SessionInfo, error) { +func (m *MockInstance) ParachainHostSessionInfo(arg0 parachaintypes.SessionIndex) (*parachaintypes.SessionInfo, error) { m.ctrl.T.Helper() ret := m.ctrl.Call(m, "ParachainHostSessionInfo", arg0) - ret0, _ := ret[0].(*parachain.SessionInfo) + ret0, _ := ret[0].(*parachaintypes.SessionInfo) ret1, _ := ret[1].(error) return ret0, ret1 } @@ -431,10 +431,10 @@ func (mr *MockInstanceMockRecorder) ParachainHostSessionInfo(arg0 interface{}) * } // ParachainHostValidatorGroups mocks base method. -func (m *MockInstance) ParachainHostValidatorGroups() (*parachain.ValidatorGroups, error) { +func (m *MockInstance) ParachainHostValidatorGroups() (*parachaintypes.ValidatorGroups, error) { m.ctrl.T.Helper() ret := m.ctrl.Call(m, "ParachainHostValidatorGroups") - ret0, _ := ret[0].(*parachain.ValidatorGroups) + ret0, _ := ret[0].(*parachaintypes.ValidatorGroups) ret1, _ := ret[1].(error) return ret0, ret1 } @@ -446,10 +446,10 @@ func (mr *MockInstanceMockRecorder) ParachainHostValidatorGroups() *gomock.Call } // ParachainHostValidators mocks base method. -func (m *MockInstance) ParachainHostValidators() ([]parachain.ValidatorID, error) { +func (m *MockInstance) ParachainHostValidators() ([]parachaintypes.ValidatorID, error) { m.ctrl.T.Helper() ret := m.ctrl.Call(m, "ParachainHostValidators") - ret0, _ := ret[0].([]parachain.ValidatorID) + ret0, _ := ret[0].([]parachaintypes.ValidatorID) ret1, _ := ret[1].(error) return ret0, ret1 } diff --git a/lib/babe/inherents/parachain_inherents.go b/lib/babe/inherents/parachain_inherents.go index b1731abc91..39004b98bc 100644 --- a/lib/babe/inherents/parachain_inherents.go +++ b/lib/babe/inherents/parachain_inherents.go @@ -8,6 +8,7 @@ import ( "github.com/ChainSafe/gossamer/dot/types" "github.com/ChainSafe/gossamer/lib/common" + "github.com/ChainSafe/gossamer/lib/crypto/sr25519" "github.com/ChainSafe/gossamer/pkg/scale" ) @@ -240,7 +241,7 @@ func newDisputeStatement() disputeStatement { //skipcq } // collatorID is the collator's relay-chain account ID -type collatorID []byte +type collatorID sr25519.PublicKey // collatorSignature is the signature on a candidate's block data signed by a collator. type collatorSignature signature diff --git a/lib/babe/mocks/runtime.go b/lib/babe/mocks/runtime.go index 2701d8022a..9963012cfd 100644 --- a/lib/babe/mocks/runtime.go +++ b/lib/babe/mocks/runtime.go @@ -11,7 +11,7 @@ import ( common "github.com/ChainSafe/gossamer/lib/common" ed25519 "github.com/ChainSafe/gossamer/lib/crypto/ed25519" keystore "github.com/ChainSafe/gossamer/lib/keystore" - parachain "github.com/ChainSafe/gossamer/lib/parachain" + parachaintypes "github.com/ChainSafe/gossamer/lib/parachain/types" runtime "github.com/ChainSafe/gossamer/lib/runtime" transaction "github.com/ChainSafe/gossamer/lib/transaction" scale "github.com/ChainSafe/gossamer/pkg/scale" @@ -371,10 +371,10 @@ func (mr *MockInstanceMockRecorder) ParachainHostCandidateEvents() *gomock.Call } // ParachainHostCandidatePendingAvailability mocks base method. -func (m *MockInstance) ParachainHostCandidatePendingAvailability(arg0 parachain.ParaID) (*parachain.CommittedCandidateReceipt, error) { +func (m *MockInstance) ParachainHostCandidatePendingAvailability(arg0 parachaintypes.ParaID) (*parachaintypes.CommittedCandidateReceipt, error) { m.ctrl.T.Helper() ret := m.ctrl.Call(m, "ParachainHostCandidatePendingAvailability", arg0) - ret0, _ := ret[0].(*parachain.CommittedCandidateReceipt) + ret0, _ := ret[0].(*parachaintypes.CommittedCandidateReceipt) ret1, _ := ret[1].(error) return ret0, ret1 } @@ -386,7 +386,7 @@ func (mr *MockInstanceMockRecorder) ParachainHostCandidatePendingAvailability(ar } // ParachainHostCheckValidationOutputs mocks base method. -func (m *MockInstance) ParachainHostCheckValidationOutputs(arg0 parachain.ParaID, arg1 parachain.CandidateCommitments) (bool, error) { +func (m *MockInstance) ParachainHostCheckValidationOutputs(arg0 parachaintypes.ParaID, arg1 parachaintypes.CandidateCommitments) (bool, error) { m.ctrl.T.Helper() ret := m.ctrl.Call(m, "ParachainHostCheckValidationOutputs", arg0, arg1) ret0, _ := ret[0].(bool) @@ -401,10 +401,10 @@ func (mr *MockInstanceMockRecorder) ParachainHostCheckValidationOutputs(arg0, ar } // ParachainHostSessionIndexForChild mocks base method. -func (m *MockInstance) ParachainHostSessionIndexForChild() (parachain.SessionIndex, error) { +func (m *MockInstance) ParachainHostSessionIndexForChild() (parachaintypes.SessionIndex, error) { m.ctrl.T.Helper() ret := m.ctrl.Call(m, "ParachainHostSessionIndexForChild") - ret0, _ := ret[0].(parachain.SessionIndex) + ret0, _ := ret[0].(parachaintypes.SessionIndex) ret1, _ := ret[1].(error) return ret0, ret1 } @@ -416,10 +416,10 @@ func (mr *MockInstanceMockRecorder) ParachainHostSessionIndexForChild() *gomock. } // ParachainHostSessionInfo mocks base method. -func (m *MockInstance) ParachainHostSessionInfo(arg0 parachain.SessionIndex) (*parachain.SessionInfo, error) { +func (m *MockInstance) ParachainHostSessionInfo(arg0 parachaintypes.SessionIndex) (*parachaintypes.SessionInfo, error) { m.ctrl.T.Helper() ret := m.ctrl.Call(m, "ParachainHostSessionInfo", arg0) - ret0, _ := ret[0].(*parachain.SessionInfo) + ret0, _ := ret[0].(*parachaintypes.SessionInfo) ret1, _ := ret[1].(error) return ret0, ret1 } @@ -431,10 +431,10 @@ func (mr *MockInstanceMockRecorder) ParachainHostSessionInfo(arg0 interface{}) * } // ParachainHostValidatorGroups mocks base method. -func (m *MockInstance) ParachainHostValidatorGroups() (*parachain.ValidatorGroups, error) { +func (m *MockInstance) ParachainHostValidatorGroups() (*parachaintypes.ValidatorGroups, error) { m.ctrl.T.Helper() ret := m.ctrl.Call(m, "ParachainHostValidatorGroups") - ret0, _ := ret[0].(*parachain.ValidatorGroups) + ret0, _ := ret[0].(*parachaintypes.ValidatorGroups) ret1, _ := ret[1].(error) return ret0, ret1 } @@ -446,10 +446,10 @@ func (mr *MockInstanceMockRecorder) ParachainHostValidatorGroups() *gomock.Call } // ParachainHostValidators mocks base method. -func (m *MockInstance) ParachainHostValidators() ([]parachain.ValidatorID, error) { +func (m *MockInstance) ParachainHostValidators() ([]parachaintypes.ValidatorID, error) { m.ctrl.T.Helper() ret := m.ctrl.Call(m, "ParachainHostValidators") - ret0, _ := ret[0].([]parachain.ValidatorID) + ret0, _ := ret[0].([]parachaintypes.ValidatorID) ret1, _ := ret[1].(error) return ret0, ret1 } diff --git a/lib/blocktree/mocks_test.go b/lib/blocktree/mocks_test.go index b1ea7d6183..476b2a9606 100644 --- a/lib/blocktree/mocks_test.go +++ b/lib/blocktree/mocks_test.go @@ -11,7 +11,7 @@ import ( common "github.com/ChainSafe/gossamer/lib/common" ed25519 "github.com/ChainSafe/gossamer/lib/crypto/ed25519" keystore "github.com/ChainSafe/gossamer/lib/keystore" - parachain "github.com/ChainSafe/gossamer/lib/parachain" + parachaintypes "github.com/ChainSafe/gossamer/lib/parachain/types" runtime "github.com/ChainSafe/gossamer/lib/runtime" transaction "github.com/ChainSafe/gossamer/lib/transaction" scale "github.com/ChainSafe/gossamer/pkg/scale" @@ -371,10 +371,10 @@ func (mr *MockInstanceMockRecorder) ParachainHostCandidateEvents() *gomock.Call } // ParachainHostCandidatePendingAvailability mocks base method. -func (m *MockInstance) ParachainHostCandidatePendingAvailability(arg0 parachain.ParaID) (*parachain.CommittedCandidateReceipt, error) { +func (m *MockInstance) ParachainHostCandidatePendingAvailability(arg0 parachaintypes.ParaID) (*parachaintypes.CommittedCandidateReceipt, error) { m.ctrl.T.Helper() ret := m.ctrl.Call(m, "ParachainHostCandidatePendingAvailability", arg0) - ret0, _ := ret[0].(*parachain.CommittedCandidateReceipt) + ret0, _ := ret[0].(*parachaintypes.CommittedCandidateReceipt) ret1, _ := ret[1].(error) return ret0, ret1 } @@ -386,7 +386,7 @@ func (mr *MockInstanceMockRecorder) ParachainHostCandidatePendingAvailability(ar } // ParachainHostCheckValidationOutputs mocks base method. -func (m *MockInstance) ParachainHostCheckValidationOutputs(arg0 parachain.ParaID, arg1 parachain.CandidateCommitments) (bool, error) { +func (m *MockInstance) ParachainHostCheckValidationOutputs(arg0 parachaintypes.ParaID, arg1 parachaintypes.CandidateCommitments) (bool, error) { m.ctrl.T.Helper() ret := m.ctrl.Call(m, "ParachainHostCheckValidationOutputs", arg0, arg1) ret0, _ := ret[0].(bool) @@ -401,10 +401,10 @@ func (mr *MockInstanceMockRecorder) ParachainHostCheckValidationOutputs(arg0, ar } // ParachainHostSessionIndexForChild mocks base method. -func (m *MockInstance) ParachainHostSessionIndexForChild() (parachain.SessionIndex, error) { +func (m *MockInstance) ParachainHostSessionIndexForChild() (parachaintypes.SessionIndex, error) { m.ctrl.T.Helper() ret := m.ctrl.Call(m, "ParachainHostSessionIndexForChild") - ret0, _ := ret[0].(parachain.SessionIndex) + ret0, _ := ret[0].(parachaintypes.SessionIndex) ret1, _ := ret[1].(error) return ret0, ret1 } @@ -416,10 +416,10 @@ func (mr *MockInstanceMockRecorder) ParachainHostSessionIndexForChild() *gomock. } // ParachainHostSessionInfo mocks base method. -func (m *MockInstance) ParachainHostSessionInfo(arg0 parachain.SessionIndex) (*parachain.SessionInfo, error) { +func (m *MockInstance) ParachainHostSessionInfo(arg0 parachaintypes.SessionIndex) (*parachaintypes.SessionInfo, error) { m.ctrl.T.Helper() ret := m.ctrl.Call(m, "ParachainHostSessionInfo", arg0) - ret0, _ := ret[0].(*parachain.SessionInfo) + ret0, _ := ret[0].(*parachaintypes.SessionInfo) ret1, _ := ret[1].(error) return ret0, ret1 } @@ -431,10 +431,10 @@ func (mr *MockInstanceMockRecorder) ParachainHostSessionInfo(arg0 interface{}) * } // ParachainHostValidatorGroups mocks base method. -func (m *MockInstance) ParachainHostValidatorGroups() (*parachain.ValidatorGroups, error) { +func (m *MockInstance) ParachainHostValidatorGroups() (*parachaintypes.ValidatorGroups, error) { m.ctrl.T.Helper() ret := m.ctrl.Call(m, "ParachainHostValidatorGroups") - ret0, _ := ret[0].(*parachain.ValidatorGroups) + ret0, _ := ret[0].(*parachaintypes.ValidatorGroups) ret1, _ := ret[1].(error) return ret0, ret1 } @@ -446,10 +446,10 @@ func (mr *MockInstanceMockRecorder) ParachainHostValidatorGroups() *gomock.Call } // ParachainHostValidators mocks base method. -func (m *MockInstance) ParachainHostValidators() ([]parachain.ValidatorID, error) { +func (m *MockInstance) ParachainHostValidators() ([]parachaintypes.ValidatorID, error) { m.ctrl.T.Helper() ret := m.ctrl.Call(m, "ParachainHostValidators") - ret0, _ := ret[0].([]parachain.ValidatorID) + ret0, _ := ret[0].([]parachaintypes.ValidatorID) ret1, _ := ret[1].(error) return ret0, ret1 } diff --git a/lib/crypto/sr25519/sr25519.go b/lib/crypto/sr25519/sr25519.go index 21a2f02476..785143a890 100644 --- a/lib/crypto/sr25519/sr25519.go +++ b/lib/crypto/sr25519/sr25519.go @@ -53,12 +53,12 @@ type PrivateKey struct { func VerifySignature(publicKey, signature, message []byte) error { pubKey, err := NewPublicKey(publicKey) if err != nil { - return fmt.Errorf("sr25519: %w", err) + return fmt.Errorf("creating new sr25519 public key: %w", err) } ok, err := pubKey.Verify(message, signature) if err != nil { - return fmt.Errorf("sr25519: %w", err) + return fmt.Errorf("verifying sr25519 public key: %w", err) } else if !ok { return fmt.Errorf("sr25519: %w: for message 0x%x, signature 0x%x and public key 0x%x", crypto.ErrSignatureVerificationFailed, message, signature, publicKey) @@ -305,9 +305,10 @@ func (k *PublicKey) Verify(msg, sig []byte) (bool, error) { copy(b[:], sig) s := &sr25519.Signature{} + err := s.Decode(b) if err != nil { - return false, err + return false, fmt.Errorf("decoding: %w", err) } t := sr25519.NewSigningContext(SigningContext, msg) diff --git a/lib/crypto/sr25519/sr25519_test.go b/lib/crypto/sr25519/sr25519_test.go index fcd77089f4..c1106b2c2d 100644 --- a/lib/crypto/sr25519/sr25519_test.go +++ b/lib/crypto/sr25519/sr25519_test.go @@ -149,13 +149,13 @@ func TestVerifySignature(t *testing.T) { publicKey: []byte{}, signature: signature, message: message, - err: errors.New("sr25519: cannot create public key: input is not 32 bytes"), + err: errors.New("creating new sr25519 public key: cannot create public key: input is not 32 bytes"), }, "invalid_signature_length": { publicKey: publicKey, signature: []byte{}, message: message, - err: fmt.Errorf("sr25519: invalid signature length"), + err: fmt.Errorf("verifying sr25519 public key: invalid signature length"), }, "verification_failed": { publicKey: publicKey, diff --git a/lib/grandpa/mocks_runtime_test.go b/lib/grandpa/mocks_runtime_test.go index 236cea4c69..16324e17d5 100644 --- a/lib/grandpa/mocks_runtime_test.go +++ b/lib/grandpa/mocks_runtime_test.go @@ -11,7 +11,7 @@ import ( common "github.com/ChainSafe/gossamer/lib/common" ed25519 "github.com/ChainSafe/gossamer/lib/crypto/ed25519" keystore "github.com/ChainSafe/gossamer/lib/keystore" - parachain "github.com/ChainSafe/gossamer/lib/parachain" + parachaintypes "github.com/ChainSafe/gossamer/lib/parachain/types" runtime "github.com/ChainSafe/gossamer/lib/runtime" transaction "github.com/ChainSafe/gossamer/lib/transaction" scale "github.com/ChainSafe/gossamer/pkg/scale" @@ -371,10 +371,10 @@ func (mr *MockInstanceMockRecorder) ParachainHostCandidateEvents() *gomock.Call } // ParachainHostCandidatePendingAvailability mocks base method. -func (m *MockInstance) ParachainHostCandidatePendingAvailability(arg0 parachain.ParaID) (*parachain.CommittedCandidateReceipt, error) { +func (m *MockInstance) ParachainHostCandidatePendingAvailability(arg0 parachaintypes.ParaID) (*parachaintypes.CommittedCandidateReceipt, error) { m.ctrl.T.Helper() ret := m.ctrl.Call(m, "ParachainHostCandidatePendingAvailability", arg0) - ret0, _ := ret[0].(*parachain.CommittedCandidateReceipt) + ret0, _ := ret[0].(*parachaintypes.CommittedCandidateReceipt) ret1, _ := ret[1].(error) return ret0, ret1 } @@ -386,7 +386,7 @@ func (mr *MockInstanceMockRecorder) ParachainHostCandidatePendingAvailability(ar } // ParachainHostCheckValidationOutputs mocks base method. -func (m *MockInstance) ParachainHostCheckValidationOutputs(arg0 parachain.ParaID, arg1 parachain.CandidateCommitments) (bool, error) { +func (m *MockInstance) ParachainHostCheckValidationOutputs(arg0 parachaintypes.ParaID, arg1 parachaintypes.CandidateCommitments) (bool, error) { m.ctrl.T.Helper() ret := m.ctrl.Call(m, "ParachainHostCheckValidationOutputs", arg0, arg1) ret0, _ := ret[0].(bool) @@ -401,10 +401,10 @@ func (mr *MockInstanceMockRecorder) ParachainHostCheckValidationOutputs(arg0, ar } // ParachainHostSessionIndexForChild mocks base method. -func (m *MockInstance) ParachainHostSessionIndexForChild() (parachain.SessionIndex, error) { +func (m *MockInstance) ParachainHostSessionIndexForChild() (parachaintypes.SessionIndex, error) { m.ctrl.T.Helper() ret := m.ctrl.Call(m, "ParachainHostSessionIndexForChild") - ret0, _ := ret[0].(parachain.SessionIndex) + ret0, _ := ret[0].(parachaintypes.SessionIndex) ret1, _ := ret[1].(error) return ret0, ret1 } @@ -416,10 +416,10 @@ func (mr *MockInstanceMockRecorder) ParachainHostSessionIndexForChild() *gomock. } // ParachainHostSessionInfo mocks base method. -func (m *MockInstance) ParachainHostSessionInfo(arg0 parachain.SessionIndex) (*parachain.SessionInfo, error) { +func (m *MockInstance) ParachainHostSessionInfo(arg0 parachaintypes.SessionIndex) (*parachaintypes.SessionInfo, error) { m.ctrl.T.Helper() ret := m.ctrl.Call(m, "ParachainHostSessionInfo", arg0) - ret0, _ := ret[0].(*parachain.SessionInfo) + ret0, _ := ret[0].(*parachaintypes.SessionInfo) ret1, _ := ret[1].(error) return ret0, ret1 } @@ -431,10 +431,10 @@ func (mr *MockInstanceMockRecorder) ParachainHostSessionInfo(arg0 interface{}) * } // ParachainHostValidatorGroups mocks base method. -func (m *MockInstance) ParachainHostValidatorGroups() (*parachain.ValidatorGroups, error) { +func (m *MockInstance) ParachainHostValidatorGroups() (*parachaintypes.ValidatorGroups, error) { m.ctrl.T.Helper() ret := m.ctrl.Call(m, "ParachainHostValidatorGroups") - ret0, _ := ret[0].(*parachain.ValidatorGroups) + ret0, _ := ret[0].(*parachaintypes.ValidatorGroups) ret1, _ := ret[1].(error) return ret0, ret1 } @@ -446,10 +446,10 @@ func (mr *MockInstanceMockRecorder) ParachainHostValidatorGroups() *gomock.Call } // ParachainHostValidators mocks base method. -func (m *MockInstance) ParachainHostValidators() ([]parachain.ValidatorID, error) { +func (m *MockInstance) ParachainHostValidators() ([]parachaintypes.ValidatorID, error) { m.ctrl.T.Helper() ret := m.ctrl.Call(m, "ParachainHostValidators") - ret0, _ := ret[0].([]parachain.ValidatorID) + ret0, _ := ret[0].([]parachaintypes.ValidatorID) ret1, _ := ret[1].(error) return ret0, ret1 } diff --git a/lib/parachain/approval_distribution_message.go b/lib/parachain/approval_distribution_message.go index ebb6b8ef39..296bd86deb 100644 --- a/lib/parachain/approval_distribution_message.go +++ b/lib/parachain/approval_distribution_message.go @@ -8,6 +8,7 @@ import ( "github.com/ChainSafe/gossamer/lib/common" "github.com/ChainSafe/gossamer/lib/crypto/sr25519" + parachaintypes "github.com/ChainSafe/gossamer/lib/parachain/types" "github.com/ChainSafe/gossamer/pkg/scale" ) @@ -102,7 +103,7 @@ type IndirectAssignmentCert struct { // BlockHash a block hash where the canidate appears. BlockHash common.Hash `scale:"1"` // Validator the validator index. - Validator ValidatorIndex `scale:"2"` + Validator parachaintypes.ValidatorIndex `scale:"2"` // Cert the cert itself. Cert AssignmentCert `scale:"3"` } @@ -131,7 +132,7 @@ type IndirectSignedApprovalVote struct { // CandidateIndex the index of the candidate in the list of candidates fully included as-of the block. CandidateIndex CandidateIndex `scale:"2"` // ValidatorIndex the validator index. - ValidatorIndex ValidatorIndex `scale:"3"` + ValidatorIndex parachaintypes.ValidatorIndex `scale:"3"` // Signature the signature of the validator. Signature ValidatorSignature `scale:"4"` } @@ -165,7 +166,7 @@ func (adm *ApprovalDistributionMessage) Value() (scale.VaryingDataTypeValue, err } // New returns new ApprovalDistributionMessage VDT -func (adm ApprovalDistributionMessage) New() ApprovalDistributionMessage { +func (ApprovalDistributionMessage) New() ApprovalDistributionMessage { return NewApprovalDistributionMessageVDT() } diff --git a/lib/parachain/approval_distribution_message_test.go b/lib/parachain/approval_distribution_message_test.go index 122af759b0..138d268642 100644 --- a/lib/parachain/approval_distribution_message_test.go +++ b/lib/parachain/approval_distribution_message_test.go @@ -7,6 +7,7 @@ import ( "testing" "github.com/ChainSafe/gossamer/lib/common" + parachaintypes "github.com/ChainSafe/gossamer/lib/parachain/types" "github.com/ChainSafe/gossamer/pkg/scale" "github.com/stretchr/testify/require" ) @@ -36,7 +37,7 @@ func TestEncodeApprovalDistributionMessageAssignmentModulo(t *testing.T) { approvalDistributionMessage.Set(Assignments{ Assignment{ - IndirectAssignmentCert: fakeAssignmentCert(hash, ValidatorIndex(1), false), + IndirectAssignmentCert: fakeAssignmentCert(hash, parachaintypes.ValidatorIndex(1), false), CandidateIndex: 4, }, }) @@ -63,7 +64,7 @@ func TestEncodeApprovalDistributionMessageAssignmentDelay(t *testing.T) { approvalDistributionMessage.Set(Assignments{ Assignment{ - IndirectAssignmentCert: fakeAssignmentCert(hash, ValidatorIndex(2), true), + IndirectAssignmentCert: fakeAssignmentCert(hash, parachaintypes.ValidatorIndex(2), true), CandidateIndex: 2, }, }) @@ -114,7 +115,7 @@ func TestEncodeApprovalDistributionMessageApprovals(t *testing.T) { IndirectSignedApprovalVote{ BlockHash: hash, CandidateIndex: CandidateIndex(2), - ValidatorIndex: ValidatorIndex(3), + ValidatorIndex: parachaintypes.ValidatorIndex(3), Signature: ValidatorSignature{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, @@ -140,7 +141,7 @@ func TestDecodeApprovalDistributionMessageAssignmentModulo(t *testing.T) { expectedApprovalDistributionMessage := NewApprovalDistributionMessageVDT() expectedApprovalDistributionMessage.Set(Assignments{ Assignment{ - IndirectAssignmentCert: fakeAssignmentCert(hash, ValidatorIndex(2), false), + IndirectAssignmentCert: fakeAssignmentCert(hash, parachaintypes.ValidatorIndex(2), false), CandidateIndex: 4, }, }) @@ -162,7 +163,7 @@ func TestDecodeApprovalDistributionMessageApprovals(t *testing.T) { IndirectSignedApprovalVote{ BlockHash: hash, CandidateIndex: CandidateIndex(2), - ValidatorIndex: ValidatorIndex(3), + ValidatorIndex: parachaintypes.ValidatorIndex(3), Signature: ValidatorSignature{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, @@ -175,7 +176,8 @@ func TestDecodeApprovalDistributionMessageApprovals(t *testing.T) { require.Equal(t, expectedApprovalDistributionMessage, approvalDistributionMessage) } -func fakeAssignmentCert(blockHash common.Hash, validator ValidatorIndex, useDelay bool) IndirectAssignmentCert { +func fakeAssignmentCert(blockHash common.Hash, validator parachaintypes.ValidatorIndex, useDelay bool, +) IndirectAssignmentCert { output := [32]byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32} proof := [64]byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, diff --git a/lib/parachain/available_data_fetching.go b/lib/parachain/available_data_fetching.go index 303124bfc5..56669e0674 100644 --- a/lib/parachain/available_data_fetching.go +++ b/lib/parachain/available_data_fetching.go @@ -4,6 +4,7 @@ import ( "fmt" "github.com/ChainSafe/gossamer/lib/common" + parachaintypes "github.com/ChainSafe/gossamer/lib/parachain/types" "github.com/ChainSafe/gossamer/pkg/scale" ) @@ -74,10 +75,10 @@ func (AvailableData) Index() uint { // although some of the fields may be the same for every parachain. type PersistedValidationData struct { // The parent head-data - ParentHead headData `scale:"1"` + ParentHead []byte `scale:"1"` // The relay-chain block number this is in the context of - RelayParentNumber BlockNumber `scale:"2"` + RelayParentNumber parachaintypes.BlockNumber `scale:"2"` // The relay-chain block storage root this is in the context of RelayParentStorageRoot common.Hash `scale:"3"` diff --git a/lib/parachain/available_data_fetching_test.go b/lib/parachain/available_data_fetching_test.go index f875b6dd4e..d6ba61671a 100644 --- a/lib/parachain/available_data_fetching_test.go +++ b/lib/parachain/available_data_fetching_test.go @@ -4,6 +4,7 @@ import ( "testing" "github.com/ChainSafe/gossamer/lib/common" + parachaintypes "github.com/ChainSafe/gossamer/lib/parachain/types" "github.com/ChainSafe/gossamer/pkg/scale" "github.com/stretchr/testify/require" ) @@ -31,7 +32,7 @@ func TestAvailableDataFetchingResponse(t *testing.T) { PoV: PoV{BlockData: testBytes}, ValidationData: PersistedValidationData{ ParentHead: testBytes, - RelayParentNumber: BlockNumber(4), + RelayParentNumber: parachaintypes.BlockNumber(4), RelayParentStorageRoot: testHash, MaxPovSize: 6, }, diff --git a/lib/parachain/chunk_fetching.go b/lib/parachain/chunk_fetching.go index e27f52a1d2..8200bd1160 100644 --- a/lib/parachain/chunk_fetching.go +++ b/lib/parachain/chunk_fetching.go @@ -3,6 +3,7 @@ package parachain import ( "fmt" + parachaintypes "github.com/ChainSafe/gossamer/lib/parachain/types" "github.com/ChainSafe/gossamer/pkg/scale" ) @@ -12,7 +13,7 @@ type ChunkFetchingRequest struct { CandidateHash CandidateHash `scale:"1"` // The index of the chunk to fetch. - Index ValidatorIndex `scale:"2"` + Index parachaintypes.ValidatorIndex `scale:"2"` } // Encode returns the SCALE encoding of the ChunkFetchingRequest diff --git a/lib/parachain/chunk_fetching_test.go b/lib/parachain/chunk_fetching_test.go index a666da95b7..9a1d56d762 100644 --- a/lib/parachain/chunk_fetching_test.go +++ b/lib/parachain/chunk_fetching_test.go @@ -4,6 +4,7 @@ import ( "testing" "github.com/ChainSafe/gossamer/lib/common" + parachaintypes "github.com/ChainSafe/gossamer/lib/parachain/types" "github.com/ChainSafe/gossamer/pkg/scale" "github.com/stretchr/testify/require" ) @@ -13,7 +14,7 @@ func TestEncodeChunkFetchingRequest(t *testing.T) { CandidateHash: CandidateHash{ common.MustHexToHash("0x677811d2f3ded2489685468dbdb2e4fa280a249fba9356acceb2e823820e2c19"), }, - Index: ValidatorIndex(8), + Index: parachaintypes.ValidatorIndex(8), } actualEncode, err := chunkFetchingRequest.Encode() diff --git a/lib/parachain/collation_fetching.go b/lib/parachain/collation_fetching.go index a9a4ceb811..91bee29da3 100644 --- a/lib/parachain/collation_fetching.go +++ b/lib/parachain/collation_fetching.go @@ -4,6 +4,7 @@ import ( "fmt" "github.com/ChainSafe/gossamer/lib/common" + parachaintypes "github.com/ChainSafe/gossamer/lib/parachain/types" "github.com/ChainSafe/gossamer/pkg/scale" ) @@ -14,7 +15,7 @@ type CollationFetchingRequest struct { RelayParent common.Hash `scale:"1"` // Parachain id of the collation - ParaID ParaID `scale:"2"` + ParaID parachaintypes.ParaID `scale:"2"` } // Encode returns the SCALE encoding of the CollationFetchingRequest @@ -27,8 +28,8 @@ type CollationFetchingResponse scale.VaryingDataType // Collation represents a requested collation to be delivered type Collation struct { - CandidateReceipt CandidateReceipt `scale:"1"` - PoV PoV `scale:"2"` + CandidateReceipt parachaintypes.CandidateReceipt `scale:"1"` + PoV PoV `scale:"2"` } // Index returns the index of varying data type diff --git a/lib/parachain/collation_fetching_test.go b/lib/parachain/collation_fetching_test.go index 3a5847069d..f4f0057231 100644 --- a/lib/parachain/collation_fetching_test.go +++ b/lib/parachain/collation_fetching_test.go @@ -4,6 +4,7 @@ import ( "testing" "github.com/ChainSafe/gossamer/lib/common" + parachaintypes "github.com/ChainSafe/gossamer/lib/parachain/types" "github.com/stretchr/testify/require" ) @@ -25,17 +26,17 @@ func TestCollationFetchingResponse(t *testing.T) { testHash := common.MustHexToHash("0x677811d2f3ded2489685468dbdb2e4fa280a249fba9356acceb2e823820e2c19") - var collatorID CollatorID + var collatorID parachaintypes.CollatorID tempCollatID := common.MustHexToBytes("0x48215b9d322601e5b1a95164cea0dc4626f545f98343d07f1551eb9543c4b147") copy(collatorID[:], tempCollatID) - var collatorSignature CollatorSignature + var collatorSignature parachaintypes.CollatorSignature tempSignature := common.MustHexToBytes(testDataStatement["collatorSignature"]) copy(collatorSignature[:], tempSignature) collation := Collation{ - CandidateReceipt: CandidateReceipt{ - Descriptor: CandidateDescriptor{ + CandidateReceipt: parachaintypes.CandidateReceipt{ + Descriptor: parachaintypes.CandidateDescriptor{ ParaID: uint32(1), RelayParent: testHash, Collator: collatorID, @@ -44,7 +45,7 @@ func TestCollationFetchingResponse(t *testing.T) { ErasureRoot: testHash, Signature: collatorSignature, ParaHead: testHash, - ValidationCodeHash: ValidationCodeHash(testHash), + ValidationCodeHash: parachaintypes.ValidationCodeHash(testHash), }, CommitmentsHash: testHash, }, diff --git a/lib/parachain/collation_protocol.go b/lib/parachain/collation_protocol.go index 6d8810c772..fb75b8bc33 100644 --- a/lib/parachain/collation_protocol.go +++ b/lib/parachain/collation_protocol.go @@ -1,8 +1,13 @@ package parachain import ( + "fmt" + + "github.com/ChainSafe/gossamer/dot/network" "github.com/ChainSafe/gossamer/lib/common" + parachaintypes "github.com/ChainSafe/gossamer/lib/parachain/types" "github.com/ChainSafe/gossamer/pkg/scale" + "github.com/libp2p/go-libp2p/core/peer" ) // CollationProtocol represents all network messages on the collation peer-set. @@ -75,9 +80,9 @@ func (c *CollatorProtocolMessage) Value() (val scale.VaryingDataTypeValue, err e // Declare the intent to advertise collations under a collator ID, attaching a // signature of the `PeerId` of the node using the given collator ID key. type Declare struct { - CollatorId CollatorID `scale:"1"` - ParaID uint32 `scale:"2"` - CollatorSignature CollatorSignature `scale:"3"` + CollatorId parachaintypes.CollatorID `scale:"1"` + ParaID uint32 `scale:"2"` + CollatorSignature parachaintypes.CollatorSignature `scale:"3"` } // Index returns the index of varying data type @@ -106,3 +111,83 @@ type CollationSeconded struct { func (CollationSeconded) Index() uint { return 4 } + +const MaxCollationMessageSize uint64 = 100 * 1024 + +type CollationProtocolV1 struct{} + +// Type returns CollationMsgType +func (*CollationProtocolV1) Type() network.MessageType { + return network.CollationMsgType +} + +// Hash returns the hash of the CollationProtocolV1 +func (cp *CollationProtocolV1) Hash() (common.Hash, error) { + // scale encode each extrinsic + encMsg, err := cp.Encode() + if err != nil { + return common.Hash{}, fmt.Errorf("cannot encode message: %w", err) + } + + return common.Blake2bHash(encMsg) +} + +// Encode a collator protocol message using scale encode +func (cp *CollationProtocolV1) Encode() ([]byte, error) { + enc, err := scale.Marshal(*cp) + if err != nil { + return nil, err + } + return enc, nil +} + +func decodeCollationMessage(in []byte) (network.NotificationsMessage, error) { + collationMessage := CollationProtocolV1{} + + err := scale.Unmarshal(in, &collationMessage) + if err != nil { + return nil, fmt.Errorf("cannot decode message: %w", err) + } + + return &collationMessage, nil +} + +func handleCollationMessage(_ peer.ID, msg network.NotificationsMessage) (bool, error) { + // TODO: Add things + fmt.Println("We got a collation message", msg) + return false, nil +} + +func getCollatorHandshake() (network.Handshake, error) { + return &collatorHandshake{}, nil +} + +func decodeCollatorHandshake(_ []byte) (network.Handshake, error) { + return &collatorHandshake{}, nil +} + +func validateCollatorHandshake(_ peer.ID, _ network.Handshake) error { + return nil +} + +type collatorHandshake struct{} + +// String formats a collatorHandshake as a string +func (*collatorHandshake) String() string { + return "collatorHandshake" +} + +// Encode encodes a collatorHandshake message using SCALE +func (*collatorHandshake) Encode() ([]byte, error) { + return []byte{}, nil +} + +// Decode the message into a collatorHandshake +func (*collatorHandshake) Decode(_ []byte) error { + return nil +} + +// IsValid returns true +func (*collatorHandshake) IsValid() bool { + return true +} diff --git a/lib/parachain/collation_protocol_test.go b/lib/parachain/collation_protocol_test.go index 9b90d4a480..9d269e9e61 100644 --- a/lib/parachain/collation_protocol_test.go +++ b/lib/parachain/collation_protocol_test.go @@ -9,6 +9,8 @@ import ( "github.com/ChainSafe/gossamer/pkg/scale" "github.com/stretchr/testify/require" "gopkg.in/yaml.v3" + + parachaintypes "github.com/ChainSafe/gossamer/lib/parachain/types" ) //go:embed testdata/collation_protocol.yaml @@ -27,11 +29,11 @@ func init() { func TestCollationProtocol(t *testing.T) { t.Parallel() - var collatorID CollatorID + var collatorID parachaintypes.CollatorID tempCollatID := common.MustHexToBytes("0x48215b9d322601e5b1a95164cea0dc4626f545f98343d07f1551eb9543c4b147") copy(collatorID[:], tempCollatID) - var collatorSignature CollatorSignature + var collatorSignature parachaintypes.CollatorSignature tempSignature := common.MustHexToBytes(testDataStatement["collatorSignature"]) copy(collatorSignature[:], tempSignature) @@ -41,7 +43,7 @@ func TestCollationProtocol(t *testing.T) { hash5 := getDummyHash(5) secondedEnumValue := Seconded{ - Descriptor: CandidateDescriptor{ + Descriptor: parachaintypes.CandidateDescriptor{ ParaID: uint32(1), RelayParent: hash5, Collator: collatorID, @@ -50,12 +52,12 @@ func TestCollationProtocol(t *testing.T) { ErasureRoot: hash5, Signature: collatorSignature, ParaHead: hash5, - ValidationCodeHash: ValidationCodeHash(hash5), + ValidationCodeHash: parachaintypes.ValidationCodeHash(hash5), }, - Commitments: CandidateCommitments{ - UpwardMessages: []UpwardMessage{{1, 2, 3}}, - NewValidationCode: &ValidationCode{1, 2, 3}, - HeadData: headData{1, 2, 3}, + Commitments: parachaintypes.CandidateCommitments{ + UpwardMessages: []parachaintypes.UpwardMessage{{1, 2, 3}}, + NewValidationCode: ¶chaintypes.ValidationCode{1, 2, 3}, + HeadData: []byte{1, 2, 3}, ProcessedDownwardMessages: uint32(5), HrmpWatermark: uint32(0), }, @@ -90,7 +92,7 @@ func TestCollationProtocol(t *testing.T) { Hash: hash5, UncheckedSignedFullStatement: UncheckedSignedFullStatement{ Payload: statementWithSeconded, - ValidatorIndex: ValidatorIndex(5), + ValidatorIndex: parachaintypes.ValidatorIndex(5), Signature: validatorSignature, }, }, @@ -143,3 +145,17 @@ func TestCollationProtocol(t *testing.T) { }) } } + +func TestDecodeCollationHandshake(t *testing.T) { + t.Parallel() + + testHandshake := &collatorHandshake{} + + enc, err := testHandshake.Encode() + require.NoError(t, err) + require.Equal(t, []byte{}, enc) + + msg, err := decodeCollatorHandshake(enc) + require.NoError(t, err) + require.Equal(t, testHandshake, msg) +} diff --git a/lib/parachain/network_protocols.go b/lib/parachain/network_protocols.go new file mode 100644 index 0000000000..b84941a39e --- /dev/null +++ b/lib/parachain/network_protocols.go @@ -0,0 +1,83 @@ +// Copyright 2023 ChainSafe Systems (ON) +// SPDX-License-Identifier: LGPL-3.0-only + +package parachain + +import ( + "fmt" + "strings" + + "github.com/ChainSafe/gossamer/lib/common" +) + +const ( + LEGACY_VALIDATION_PROTOCOL_V1 = "/polkadot/validation/1" + LEGACY_COLLATION_PROTOCOL_V1 = "/polkadot/collation/1" +) + +type ReqProtocolName uint + +const ( + ChunkFetchingV1 ReqProtocolName = iota + CollationFetchingV1 + PoVFetchingV1 + AvailableDataFetchingV1 + StatementFetchingV1 + DisputeSendingV1 +) + +type PeerSetProtocolName uint + +const ( + ValidationProtocolName PeerSetProtocolName = iota + CollationProtocolName +) + +func GenerateReqProtocolName(protocol ReqProtocolName, forkID string, GenesisHash common.Hash) string { + prefix := fmt.Sprintf("/%s", GenesisHash.String()) + + if forkID != "" { + prefix = fmt.Sprintf("%s/%s", prefix, forkID) + } + + switch protocol { + case ChunkFetchingV1: + return fmt.Sprintf("%s/req_chunk/1", prefix) + case CollationFetchingV1: + return fmt.Sprintf("%s/req_collation/1", prefix) + case PoVFetchingV1: + return fmt.Sprintf("%s/req_pov/1", prefix) + case AvailableDataFetchingV1: + return fmt.Sprintf("%s/req_available_data/1", prefix) + case StatementFetchingV1: + return fmt.Sprintf("%s/req_statement/1", prefix) + case DisputeSendingV1: + return fmt.Sprintf("%s/send_dispute/1", prefix) + default: + panic("unknown protocol") + } +} + +func GeneratePeersetProtocolName(protocol PeerSetProtocolName, forkID string, GenesisHash common.Hash, version uint32, +) string { + genesisHash := GenesisHash.String() + genesisHash = strings.TrimPrefix(genesisHash, "0x") + + prefix := fmt.Sprintf("/%s", genesisHash) + + if forkID != "" { + prefix = fmt.Sprintf("%s/%s", prefix, forkID) + } + + switch protocol { + case ValidationProtocolName: + return fmt.Sprintf("%s/validation/%d", prefix, version) + // message over this protocol is BitfieldDistributionMessage, StatementDistributionMessage, + // ApprovalDistributionMessage + case CollationProtocolName: + return fmt.Sprintf("%s/collation/%d", prefix, version) + // message over this protocol is CollatorProtocolMessage + default: + panic("unknown protocol") + } +} diff --git a/lib/parachain/service.go b/lib/parachain/service.go new file mode 100644 index 0000000000..2609cf9439 --- /dev/null +++ b/lib/parachain/service.go @@ -0,0 +1,145 @@ +// Copyright 2023 ChainSafe Systems (ON) +// SPDX-License-Identifier: LGPL-3.0-only + +package parachain + +import ( + "fmt" + "time" + + "github.com/ChainSafe/gossamer/dot/network" + "github.com/ChainSafe/gossamer/lib/common" + "github.com/libp2p/go-libp2p/core/peer" + "github.com/libp2p/go-libp2p/core/protocol" +) + +const ( + CollationProtocolVersion = 1 + ValidationProtocolVersion = 1 +) + +type Service struct { + Network Network +} + +func NewService(net Network, genesisHash common.Hash) (*Service, error) { + // TODO: Use actual fork id from chain spec #3373 + forkID := "" + + validationProtocolID := GeneratePeersetProtocolName( + ValidationProtocolName, forkID, genesisHash, ValidationProtocolVersion) + + // register validation protocol + err := net.RegisterNotificationsProtocol( + protocol.ID(validationProtocolID), + network.ValidationMsgType, + getValidationHandshake, + decodeValidationHandshake, + validateValidationHandshake, + decodeValidationMessage, + handleValidationMessage, + nil, + MaxValidationMessageSize, + ) + if err != nil { + // try with legacy protocol id + err1 := net.RegisterNotificationsProtocol( + protocol.ID(LEGACY_VALIDATION_PROTOCOL_V1), + network.ValidationMsgType, + getValidationHandshake, + decodeValidationHandshake, + validateValidationHandshake, + decodeValidationMessage, + handleValidationMessage, + nil, + MaxValidationMessageSize, + ) + + if err1 != nil { + return nil, fmt.Errorf("registering validation protocol, new: %w, legacy:%w", err, err1) + } + } + + collationProtocolID := GeneratePeersetProtocolName( + CollationProtocolName, forkID, genesisHash, CollationProtocolVersion) + + // register collation protocol + err = net.RegisterNotificationsProtocol( + protocol.ID(collationProtocolID), + network.CollationMsgType, + getCollatorHandshake, + decodeCollatorHandshake, + validateCollatorHandshake, + decodeCollationMessage, + handleCollationMessage, + nil, + MaxCollationMessageSize, + ) + if err != nil { + // try with legacy protocol id + err1 := net.RegisterNotificationsProtocol( + protocol.ID(LEGACY_COLLATION_PROTOCOL_V1), + network.CollationMsgType, + getCollatorHandshake, + decodeCollatorHandshake, + validateCollatorHandshake, + decodeCollationMessage, + handleCollationMessage, + nil, + MaxCollationMessageSize, + ) + + if err1 != nil { + return nil, fmt.Errorf("registering collation protocol, new: %w, legacy:%w", err, err1) + } + } + + parachainService := &Service{ + Network: net, + } + + go parachainService.run() + + return parachainService, nil +} + +// Start starts the Handler +func (Service) Start() error { + return nil +} + +// Stop stops the Handler +func (Service) Stop() error { + return nil +} + +// main loop of parachain service +func (s Service) run() { + + // NOTE: this is a temporary test, just to show that we can send messages to peers + // + time.Sleep(time.Second * 15) + // let's try sending a collation message and validation message to a peer and see what happens + collationMessage := CollationProtocolV1{} + s.Network.GossipMessage(&collationMessage) + + validationMessage := ValidationProtocolV1{} + s.Network.GossipMessage(&validationMessage) + +} + +// Network is the interface required by parachain service for the network +type Network interface { + GossipMessage(msg network.NotificationsMessage) + SendMessage(to peer.ID, msg network.NotificationsMessage) error + RegisterNotificationsProtocol(sub protocol.ID, + messageID network.MessageType, + handshakeGetter network.HandshakeGetter, + handshakeDecoder network.HandshakeDecoder, + handshakeValidator network.HandshakeValidator, + messageDecoder network.MessageDecoder, + messageHandler network.NotificationsMessageHandler, + batchHandler network.NotificationsMessageBatchHandler, + maxSize uint64, + ) error +} diff --git a/lib/parachain/statement.go b/lib/parachain/statement.go index 8fd7c83f46..35b705891b 100644 --- a/lib/parachain/statement.go +++ b/lib/parachain/statement.go @@ -4,6 +4,7 @@ import ( "fmt" "github.com/ChainSafe/gossamer/lib/common" + parachaintypes "github.com/ChainSafe/gossamer/lib/parachain/types" "github.com/ChainSafe/gossamer/pkg/scale" ) @@ -40,7 +41,7 @@ func (s *Statement) Value() (scale.VaryingDataTypeValue, error) { } // Seconded represents a statement that a validator seconds a candidate. -type Seconded CommittedCandidateReceipt +type Seconded parachaintypes.CommittedCandidateReceipt // Index returns the index of varying data type func (Seconded) Index() uint { diff --git a/lib/parachain/statement_distribution_message.go b/lib/parachain/statement_distribution_message.go index 0ba3a4d4a0..c4c2f8dbe8 100644 --- a/lib/parachain/statement_distribution_message.go +++ b/lib/parachain/statement_distribution_message.go @@ -4,6 +4,7 @@ import ( "fmt" "github.com/ChainSafe/gossamer/lib/common" + parachaintypes "github.com/ChainSafe/gossamer/lib/parachain/types" "github.com/ChainSafe/gossamer/pkg/scale" ) @@ -69,7 +70,7 @@ type UncheckedSignedFullStatement struct { Payload Statement `scale:"1"` // The index of the validator signing this statement. - ValidatorIndex ValidatorIndex `scale:"2"` + ValidatorIndex parachaintypes.ValidatorIndex `scale:"2"` // The signature by the validator of the signed payload. Signature ValidatorSignature `scale:"3"` @@ -84,7 +85,7 @@ type StatementMetadata struct { CandidateHash CandidateHash `scale:"2"` // Validator that attested the validity. - SignedBy ValidatorIndex `scale:"3"` + SignedBy parachaintypes.ValidatorIndex `scale:"3"` // Signature of seconding validator. Signature ValidatorSignature `scale:"4"` diff --git a/lib/parachain/statement_distribution_message_test.go b/lib/parachain/statement_distribution_message_test.go index f8438a87ca..0ff8bb303a 100644 --- a/lib/parachain/statement_distribution_message_test.go +++ b/lib/parachain/statement_distribution_message_test.go @@ -6,6 +6,7 @@ import ( "testing" "github.com/ChainSafe/gossamer/lib/common" + parachaintypes "github.com/ChainSafe/gossamer/lib/parachain/types" "github.com/ChainSafe/gossamer/pkg/scale" "github.com/stretchr/testify/require" "gopkg.in/yaml.v3" @@ -27,14 +28,14 @@ func init() { func TestStatementDistributionMessage(t *testing.T) { t.Parallel() - var collatorSignature CollatorSignature + var collatorSignature parachaintypes.CollatorSignature tempSignature := common.MustHexToBytes(testDataStatement["collatorSignature"]) copy(collatorSignature[:], tempSignature) var validatorSignature ValidatorSignature copy(validatorSignature[:], tempSignature) - var collatorID CollatorID + var collatorID parachaintypes.CollatorID tempCollatID := common.MustHexToBytes("0x48215b9d322601e5b1a95164cea0dc4626f545f98343d07f1551eb9543c4b147") copy(collatorID[:], tempCollatID) @@ -45,7 +46,7 @@ func TestStatementDistributionMessage(t *testing.T) { require.NoError(t, err) secondedEnumValue := Seconded{ - Descriptor: CandidateDescriptor{ + Descriptor: parachaintypes.CandidateDescriptor{ ParaID: uint32(1), RelayParent: hash5, Collator: collatorID, @@ -54,12 +55,12 @@ func TestStatementDistributionMessage(t *testing.T) { ErasureRoot: hash5, Signature: collatorSignature, ParaHead: hash5, - ValidationCodeHash: ValidationCodeHash(hash5), + ValidationCodeHash: parachaintypes.ValidationCodeHash(hash5), }, - Commitments: CandidateCommitments{ - UpwardMessages: []UpwardMessage{{1, 2, 3}}, - NewValidationCode: &ValidationCode{1, 2, 3}, - HeadData: headData{1, 2, 3}, + Commitments: parachaintypes.CandidateCommitments{ + UpwardMessages: []parachaintypes.UpwardMessage{{1, 2, 3}}, + NewValidationCode: ¶chaintypes.ValidationCode{1, 2, 3}, + HeadData: []byte{1, 2, 3}, ProcessedDownwardMessages: uint32(5), HrmpWatermark: uint32(0), }, @@ -73,7 +74,7 @@ func TestStatementDistributionMessage(t *testing.T) { Hash: hash5, UncheckedSignedFullStatement: UncheckedSignedFullStatement{ Payload: statementWithValid, - ValidatorIndex: ValidatorIndex(5), + ValidatorIndex: parachaintypes.ValidatorIndex(5), Signature: validatorSignature, }, } @@ -82,7 +83,7 @@ func TestStatementDistributionMessage(t *testing.T) { Hash: hash5, UncheckedSignedFullStatement: UncheckedSignedFullStatement{ Payload: statementWithSeconded, - ValidatorIndex: ValidatorIndex(5), + ValidatorIndex: parachaintypes.ValidatorIndex(5), Signature: validatorSignature, }, } @@ -90,7 +91,7 @@ func TestStatementDistributionMessage(t *testing.T) { secondedStatementWithLargePayload := SecondedStatementWithLargePayload{ RelayParent: hash5, CandidateHash: CandidateHash{hash5}, - SignedBy: ValidatorIndex(5), + SignedBy: parachaintypes.ValidatorIndex(5), Signature: validatorSignature, } diff --git a/lib/parachain/statement_fetching.go b/lib/parachain/statement_fetching.go index 1f9e9332ea..9080916c36 100644 --- a/lib/parachain/statement_fetching.go +++ b/lib/parachain/statement_fetching.go @@ -4,6 +4,7 @@ import ( "fmt" "github.com/ChainSafe/gossamer/lib/common" + parachaintypes "github.com/ChainSafe/gossamer/lib/parachain/types" "github.com/ChainSafe/gossamer/pkg/scale" ) @@ -28,7 +29,7 @@ func (s *StatementFetchingRequest) Encode() ([]byte, error) { type StatementFetchingResponse scale.VaryingDataType // MissingDataInStatement represents the data missing to reconstruct the full signed statement. -type MissingDataInStatement CommittedCandidateReceipt +type MissingDataInStatement parachaintypes.CommittedCandidateReceipt // Index returns the index of varying data type func (MissingDataInStatement) Index() uint { diff --git a/lib/parachain/statement_fetching_test.go b/lib/parachain/statement_fetching_test.go index bb315588a1..d606b25d44 100644 --- a/lib/parachain/statement_fetching_test.go +++ b/lib/parachain/statement_fetching_test.go @@ -4,6 +4,7 @@ import ( "testing" "github.com/ChainSafe/gossamer/lib/common" + parachaintypes "github.com/ChainSafe/gossamer/lib/parachain/types" "github.com/stretchr/testify/require" ) @@ -71,16 +72,16 @@ func TestStatementFetchingResponse(t *testing.T) { testHash := common.MustHexToHash("0x677811d2f3ded2489685468dbdb2e4fa280a249fba9356acceb2e823820e2c19") - var collatorID CollatorID + var collatorID parachaintypes.CollatorID tempCollatID := common.MustHexToBytes("0x48215b9d322601e5b1a95164cea0dc4626f545f98343d07f1551eb9543c4b147") copy(collatorID[:], tempCollatID) - var collatorSignature CollatorSignature + var collatorSignature parachaintypes.CollatorSignature tempSignature := common.MustHexToBytes(testDataStatement["collatorSignature"]) copy(collatorSignature[:], tempSignature) missingDataInStatement := MissingDataInStatement{ - Descriptor: CandidateDescriptor{ + Descriptor: parachaintypes.CandidateDescriptor{ ParaID: uint32(1), RelayParent: testHash, Collator: collatorID, @@ -89,12 +90,12 @@ func TestStatementFetchingResponse(t *testing.T) { ErasureRoot: testHash, Signature: collatorSignature, ParaHead: testHash, - ValidationCodeHash: ValidationCodeHash(testHash), + ValidationCodeHash: parachaintypes.ValidationCodeHash(testHash), }, - Commitments: CandidateCommitments{ - UpwardMessages: []UpwardMessage{{1, 2, 3}}, - NewValidationCode: &ValidationCode{1, 2, 3}, - HeadData: headData{1, 2, 3}, + Commitments: parachaintypes.CandidateCommitments{ + UpwardMessages: []parachaintypes.UpwardMessage{{1, 2, 3}}, + NewValidationCode: ¶chaintypes.ValidationCode{1, 2, 3}, + HeadData: []byte{1, 2, 3}, ProcessedDownwardMessages: uint32(5), HrmpWatermark: uint32(0), }, diff --git a/lib/parachain/statement_test.go b/lib/parachain/statement_test.go index 4aae3c431e..ee7c8e0ad7 100644 --- a/lib/parachain/statement_test.go +++ b/lib/parachain/statement_test.go @@ -4,6 +4,7 @@ import ( "testing" "github.com/ChainSafe/gossamer/lib/common" + parachaintypes "github.com/ChainSafe/gossamer/lib/parachain/types" "github.com/ChainSafe/gossamer/pkg/scale" "github.com/stretchr/testify/require" ) @@ -19,18 +20,18 @@ func getDummyHash(num byte) common.Hash { func TestStatement(t *testing.T) { t.Parallel() - var collatorID CollatorID + var collatorID parachaintypes.CollatorID tempCollatID := common.MustHexToBytes("0x48215b9d322601e5b1a95164cea0dc4626f545f98343d07f1551eb9543c4b147") copy(collatorID[:], tempCollatID) - var collatorSignature CollatorSignature + var collatorSignature parachaintypes.CollatorSignature tempSignature := common.MustHexToBytes(testDataStatement["collatorSignature"]) copy(collatorSignature[:], tempSignature) hash5 := getDummyHash(5) secondedEnumValue := Seconded{ - Descriptor: CandidateDescriptor{ + Descriptor: parachaintypes.CandidateDescriptor{ ParaID: uint32(1), RelayParent: hash5, Collator: collatorID, @@ -39,12 +40,12 @@ func TestStatement(t *testing.T) { ErasureRoot: hash5, Signature: collatorSignature, ParaHead: hash5, - ValidationCodeHash: ValidationCodeHash(hash5), + ValidationCodeHash: parachaintypes.ValidationCodeHash(hash5), }, - Commitments: CandidateCommitments{ - UpwardMessages: []UpwardMessage{{1, 2, 3}}, - NewValidationCode: &ValidationCode{1, 2, 3}, - HeadData: headData{1, 2, 3}, + Commitments: parachaintypes.CandidateCommitments{ + UpwardMessages: []parachaintypes.UpwardMessage{{1, 2, 3}}, + NewValidationCode: ¶chaintypes.ValidationCode{1, 2, 3}, + HeadData: []byte{1, 2, 3}, ProcessedDownwardMessages: uint32(5), HrmpWatermark: uint32(0), }, diff --git a/lib/parachain/testdata/westend.yaml b/lib/parachain/types/testdata/westend.yaml similarity index 100% rename from lib/parachain/testdata/westend.yaml rename to lib/parachain/types/testdata/westend.yaml diff --git a/lib/parachain/types.go b/lib/parachain/types/types.go similarity index 98% rename from lib/parachain/types.go rename to lib/parachain/types/types.go index 1eba5fc375..b4af337530 100644 --- a/lib/parachain/types.go +++ b/lib/parachain/types/types.go @@ -1,7 +1,7 @@ // Copyright 2023 ChainSafe Systems (ON) // SPDX-License-Identifier: LGPL-3.0-only -package parachain +package parachaintypes import ( "fmt" @@ -11,6 +11,9 @@ import ( "github.com/ChainSafe/gossamer/pkg/scale" ) +// The primary purpose of this package is to put types being used by other packages to avoid cyclic +// dependencies. + // NOTE: https://github.com/ChainSafe/gossamer/pull/3297#discussion_r1214740051 // ValidatorIndex Index of the validator. Used as a lightweight replacement of the `ValidatorId` when appropriate diff --git a/lib/parachain/types_test.go b/lib/parachain/types/types_test.go similarity index 87% rename from lib/parachain/types_test.go rename to lib/parachain/types/types_test.go index b4eb81ea07..068a093f43 100644 --- a/lib/parachain/types_test.go +++ b/lib/parachain/types/types_test.go @@ -1,7 +1,7 @@ // Copyright 2023 ChainSafe Systems (ON) // SPDX-License-Identifier: LGPL-3.0-only -package parachain +package parachaintypes import ( _ "embed" @@ -45,23 +45,23 @@ func Test_Validators(t *testing.T) { require.NoError(t, err) expected := []ValidatorID{ - ValidatorID(mustHexTo32BArray(t, "0xa262f83b46310770ae8d092147176b8b25e8855bcfbbe701d346b10db0c5385d")), - ValidatorID(mustHexTo32BArray(t, "0x804b9df571e2b744d65eca2d4c59eb8e4345286c00389d97bfc1d8d13aa6e57e")), - ValidatorID(mustHexTo32BArray(t, "0x4eb63e4aad805c06dc924e2f19b1dde7faf507e5bb3c1838d6a3cfc10e84fe72")), - ValidatorID(mustHexTo32BArray(t, "0x74c337d57035cd6b7718e92a0d8ea6ef710da8ab1215a057c40c4ef792155a68")), - ValidatorID(mustHexTo32BArray(t, "0xe61d138eebd2069f1a76b3570f9de6a4b196289b198e33e6f0b59cef8837c511")), - ValidatorID(mustHexTo32BArray(t, "0x94ef34321ca5d37a6e8953183406b76f8ebf6a4be5eefc3997d022ac6e0a050e")), - ValidatorID(mustHexTo32BArray(t, "0xac837e8ca589521a83e7d9a7b307d1c41a5d9b940422488236f99646d21f3841")), - ValidatorID(mustHexTo32BArray(t, "0xb61cb85f7cf7616f9ef8f95010a51a68a4eae8afcdff715cc6a8d43da4a32a12")), - ValidatorID(mustHexTo32BArray(t, "0x382f17dae6b13a8ce5a7cc805056d9b592d918c8593f077db28cb14cf08a760c")), - ValidatorID(mustHexTo32BArray(t, "0x0825ba7677597ec9453ab5dbaa9e68bf89dc36694cb6e74cbd5a9a74b167e547")), - ValidatorID(mustHexTo32BArray(t, "0xcee3f65d78a239d7d199b100295e7a2d852ae898a6b81fd867b3471f25be7237")), - ValidatorID(mustHexTo32BArray(t, "0xe2ac8f039eb02370a9577e49ffc6032e6b5bf5ff77783bdc676d1432d714fd53")), - ValidatorID(mustHexTo32BArray(t, "0xce35fa64fe7a5a6fc456ed2830e64d5d1a5dba26e7a57ab458f8cedf1ec77016")), - ValidatorID(mustHexTo32BArray(t, "0xae40e895f46c8bfb3df63c119047d7faf21c3fe3e7a91994a3f00da6fa80f848")), - ValidatorID(mustHexTo32BArray(t, "0xa0e038975cff34d01c62960828c23ec10a305fe9f5c3589c2ae40f51963e380a")), - ValidatorID(mustHexTo32BArray(t, "0x807fa54347a8957ff5ef6c28e2403c83947e5fad4aa805c914df0645a07aab5a")), - ValidatorID(mustHexTo32BArray(t, "0x4c8e878d7f558ce5086cc37ca0d5964bed54ddd6b15a6663a95fe42e36858936")), + mustHexTo32BArray(t, "0xa262f83b46310770ae8d092147176b8b25e8855bcfbbe701d346b10db0c5385d"), + mustHexTo32BArray(t, "0x804b9df571e2b744d65eca2d4c59eb8e4345286c00389d97bfc1d8d13aa6e57e"), + mustHexTo32BArray(t, "0x4eb63e4aad805c06dc924e2f19b1dde7faf507e5bb3c1838d6a3cfc10e84fe72"), + mustHexTo32BArray(t, "0x74c337d57035cd6b7718e92a0d8ea6ef710da8ab1215a057c40c4ef792155a68"), + mustHexTo32BArray(t, "0xe61d138eebd2069f1a76b3570f9de6a4b196289b198e33e6f0b59cef8837c511"), + mustHexTo32BArray(t, "0x94ef34321ca5d37a6e8953183406b76f8ebf6a4be5eefc3997d022ac6e0a050e"), + mustHexTo32BArray(t, "0xac837e8ca589521a83e7d9a7b307d1c41a5d9b940422488236f99646d21f3841"), + mustHexTo32BArray(t, "0xb61cb85f7cf7616f9ef8f95010a51a68a4eae8afcdff715cc6a8d43da4a32a12"), + mustHexTo32BArray(t, "0x382f17dae6b13a8ce5a7cc805056d9b592d918c8593f077db28cb14cf08a760c"), + mustHexTo32BArray(t, "0x0825ba7677597ec9453ab5dbaa9e68bf89dc36694cb6e74cbd5a9a74b167e547"), + mustHexTo32BArray(t, "0xcee3f65d78a239d7d199b100295e7a2d852ae898a6b81fd867b3471f25be7237"), + mustHexTo32BArray(t, "0xe2ac8f039eb02370a9577e49ffc6032e6b5bf5ff77783bdc676d1432d714fd53"), + mustHexTo32BArray(t, "0xce35fa64fe7a5a6fc456ed2830e64d5d1a5dba26e7a57ab458f8cedf1ec77016"), + mustHexTo32BArray(t, "0xae40e895f46c8bfb3df63c119047d7faf21c3fe3e7a91994a3f00da6fa80f848"), + mustHexTo32BArray(t, "0xa0e038975cff34d01c62960828c23ec10a305fe9f5c3589c2ae40f51963e380a"), + mustHexTo32BArray(t, "0x807fa54347a8957ff5ef6c28e2403c83947e5fad4aa805c914df0645a07aab5a"), + mustHexTo32BArray(t, "0x4c8e878d7f558ce5086cc37ca0d5964bed54ddd6b15a6663a95fe42e36858936"), } require.Equal(t, expected, validatorIDs) diff --git a/lib/parachain/validation_protocol.go b/lib/parachain/validation_protocol.go new file mode 100644 index 0000000000..8c390289c2 --- /dev/null +++ b/lib/parachain/validation_protocol.go @@ -0,0 +1,94 @@ +// Copyright 2023 ChainSafe Systems (ON) +// SPDX-License-Identifier: LGPL-3.0-only + +package parachain + +import ( + "fmt" + + "github.com/ChainSafe/gossamer/dot/network" + "github.com/ChainSafe/gossamer/lib/common" + "github.com/ChainSafe/gossamer/pkg/scale" + "github.com/libp2p/go-libp2p/core/peer" +) + +const MaxValidationMessageSize uint64 = 100 * 1024 + +type ValidationProtocolV1 struct { + // TODO: Implement this struct https://github.com/ChainSafe/gossamer/issues/3318 +} + +// Type returns ValidationMsgType +func (*ValidationProtocolV1) Type() network.MessageType { + return network.ValidationMsgType +} + +// Hash returns the hash of the CollationProtocolV1 +func (vp *ValidationProtocolV1) Hash() (common.Hash, error) { + encMsg, err := vp.Encode() + if err != nil { + return common.Hash{}, fmt.Errorf("cannot encode message: %w", err) + } + + return common.Blake2bHash(encMsg) +} + +// Encode a collator protocol message using scale encode +func (vp *ValidationProtocolV1) Encode() ([]byte, error) { + enc, err := scale.Marshal(*vp) + if err != nil { + return enc, err + } + return enc, nil +} + +func decodeValidationMessage(in []byte) (network.NotificationsMessage, error) { + validationMessage := ValidationProtocolV1{} + + err := scale.Unmarshal(in, &validationMessage) + if err != nil { + return nil, fmt.Errorf("cannot decode message: %w", err) + } + + return &validationMessage, nil +} + +func handleValidationMessage(_ peer.ID, msg network.NotificationsMessage) (bool, error) { + // TODO: Add things + fmt.Println("We got a validation message", msg) + return false, nil +} + +func getValidationHandshake() (network.Handshake, error) { + return &collatorHandshake{}, nil +} + +func decodeValidationHandshake(_ []byte) (network.Handshake, error) { + return &validationHandshake{}, nil +} + +func validateValidationHandshake(_ peer.ID, _ network.Handshake) error { + return nil +} + +type validationHandshake struct{} + +// String formats a validationHandshake as a string +func (*validationHandshake) String() string { + return "validationHandshake" +} + +// Encode encodes a validationHandshake message using SCALE +func (*validationHandshake) Encode() ([]byte, error) { + return []byte{}, nil +} + +// Decode the message into a validationHandshake +func (*validationHandshake) Decode(_ []byte) error { + return nil +} + +// IsValid returns true +func (*validationHandshake) IsValid() bool { + return true +} diff --git a/lib/parachain/validation_protocol_test.go b/lib/parachain/validation_protocol_test.go new file mode 100644 index 0000000000..b61928825b --- /dev/null +++ b/lib/parachain/validation_protocol_test.go @@ -0,0 +1,23 @@ +// Copyright 2023 ChainSafe Systems (ON) +// SPDX-License-Identifier: LGPL-3.0-only + +package parachain + +import ( + "testing" + + "github.com/stretchr/testify/require" +) + +func TestDecodeValidationHandshake(t *testing.T) { + t.Parallel() + + testHandshake := &validationHandshake{} + + enc, err := testHandshake.Encode() + require.NoError(t, err) + + msg, err := decodeValidationHandshake(enc) + require.NoError(t, err) + require.Equal(t, testHandshake, msg) +} diff --git a/lib/runtime/interface.go b/lib/runtime/interface.go index 473710cd99..e31484d195 100644 --- a/lib/runtime/interface.go +++ b/lib/runtime/interface.go @@ -8,7 +8,7 @@ import ( "github.com/ChainSafe/gossamer/lib/common" "github.com/ChainSafe/gossamer/lib/crypto/ed25519" "github.com/ChainSafe/gossamer/lib/keystore" - "github.com/ChainSafe/gossamer/lib/parachain" + parachaintypes "github.com/ChainSafe/gossamer/lib/parachain/types" "github.com/ChainSafe/gossamer/lib/transaction" "github.com/ChainSafe/gossamer/pkg/scale" ) @@ -50,17 +50,17 @@ type Instance interface { GrandpaSubmitReportEquivocationUnsignedExtrinsic( equivocationProof types.GrandpaEquivocationProof, keyOwnershipProof types.GrandpaOpaqueKeyOwnershipProof, ) error - ParachainHostValidators() ([]parachain.ValidatorID, error) - ParachainHostValidatorGroups() (*parachain.ValidatorGroups, error) + ParachainHostValidators() ([]parachaintypes.ValidatorID, error) + ParachainHostValidatorGroups() (*parachaintypes.ValidatorGroups, error) ParachainHostAvailabilityCores() (*scale.VaryingDataTypeSlice, error) ParachainHostCheckValidationOutputs( - parachainID parachain.ParaID, - outputs parachain.CandidateCommitments, + parachainID parachaintypes.ParaID, + outputs parachaintypes.CandidateCommitments, ) (bool, error) - ParachainHostSessionIndexForChild() (parachain.SessionIndex, error) + ParachainHostSessionIndexForChild() (parachaintypes.SessionIndex, error) ParachainHostCandidatePendingAvailability( - parachainID parachain.ParaID, - ) (*parachain.CommittedCandidateReceipt, error) + parachainID parachaintypes.ParaID, + ) (*parachaintypes.CommittedCandidateReceipt, error) ParachainHostCandidateEvents() (*scale.VaryingDataTypeSlice, error) - ParachainHostSessionInfo(sessionIndex parachain.SessionIndex) (*parachain.SessionInfo, error) + ParachainHostSessionInfo(sessionIndex parachaintypes.SessionIndex) (*parachaintypes.SessionInfo, error) } diff --git a/lib/runtime/mocks/mocks.go b/lib/runtime/mocks/mocks.go index 9db3aa6bf0..a2de78c0e9 100644 --- a/lib/runtime/mocks/mocks.go +++ b/lib/runtime/mocks/mocks.go @@ -11,7 +11,7 @@ import ( common "github.com/ChainSafe/gossamer/lib/common" ed25519 "github.com/ChainSafe/gossamer/lib/crypto/ed25519" keystore "github.com/ChainSafe/gossamer/lib/keystore" - parachain "github.com/ChainSafe/gossamer/lib/parachain" + parachaintypes "github.com/ChainSafe/gossamer/lib/parachain/types" runtime "github.com/ChainSafe/gossamer/lib/runtime" transaction "github.com/ChainSafe/gossamer/lib/transaction" scale "github.com/ChainSafe/gossamer/pkg/scale" @@ -371,10 +371,10 @@ func (mr *MockInstanceMockRecorder) ParachainHostCandidateEvents() *gomock.Call } // ParachainHostCandidatePendingAvailability mocks base method. -func (m *MockInstance) ParachainHostCandidatePendingAvailability(arg0 parachain.ParaID) (*parachain.CommittedCandidateReceipt, error) { +func (m *MockInstance) ParachainHostCandidatePendingAvailability(arg0 parachaintypes.ParaID) (*parachaintypes.CommittedCandidateReceipt, error) { m.ctrl.T.Helper() ret := m.ctrl.Call(m, "ParachainHostCandidatePendingAvailability", arg0) - ret0, _ := ret[0].(*parachain.CommittedCandidateReceipt) + ret0, _ := ret[0].(*parachaintypes.CommittedCandidateReceipt) ret1, _ := ret[1].(error) return ret0, ret1 } @@ -386,7 +386,7 @@ func (mr *MockInstanceMockRecorder) ParachainHostCandidatePendingAvailability(ar } // ParachainHostCheckValidationOutputs mocks base method. -func (m *MockInstance) ParachainHostCheckValidationOutputs(arg0 parachain.ParaID, arg1 parachain.CandidateCommitments) (bool, error) { +func (m *MockInstance) ParachainHostCheckValidationOutputs(arg0 parachaintypes.ParaID, arg1 parachaintypes.CandidateCommitments) (bool, error) { m.ctrl.T.Helper() ret := m.ctrl.Call(m, "ParachainHostCheckValidationOutputs", arg0, arg1) ret0, _ := ret[0].(bool) @@ -401,10 +401,10 @@ func (mr *MockInstanceMockRecorder) ParachainHostCheckValidationOutputs(arg0, ar } // ParachainHostSessionIndexForChild mocks base method. -func (m *MockInstance) ParachainHostSessionIndexForChild() (parachain.SessionIndex, error) { +func (m *MockInstance) ParachainHostSessionIndexForChild() (parachaintypes.SessionIndex, error) { m.ctrl.T.Helper() ret := m.ctrl.Call(m, "ParachainHostSessionIndexForChild") - ret0, _ := ret[0].(parachain.SessionIndex) + ret0, _ := ret[0].(parachaintypes.SessionIndex) ret1, _ := ret[1].(error) return ret0, ret1 } @@ -416,10 +416,10 @@ func (mr *MockInstanceMockRecorder) ParachainHostSessionIndexForChild() *gomock. } // ParachainHostSessionInfo mocks base method. -func (m *MockInstance) ParachainHostSessionInfo(arg0 parachain.SessionIndex) (*parachain.SessionInfo, error) { +func (m *MockInstance) ParachainHostSessionInfo(arg0 parachaintypes.SessionIndex) (*parachaintypes.SessionInfo, error) { m.ctrl.T.Helper() ret := m.ctrl.Call(m, "ParachainHostSessionInfo", arg0) - ret0, _ := ret[0].(*parachain.SessionInfo) + ret0, _ := ret[0].(*parachaintypes.SessionInfo) ret1, _ := ret[1].(error) return ret0, ret1 } @@ -431,10 +431,10 @@ func (mr *MockInstanceMockRecorder) ParachainHostSessionInfo(arg0 interface{}) * } // ParachainHostValidatorGroups mocks base method. -func (m *MockInstance) ParachainHostValidatorGroups() (*parachain.ValidatorGroups, error) { +func (m *MockInstance) ParachainHostValidatorGroups() (*parachaintypes.ValidatorGroups, error) { m.ctrl.T.Helper() ret := m.ctrl.Call(m, "ParachainHostValidatorGroups") - ret0, _ := ret[0].(*parachain.ValidatorGroups) + ret0, _ := ret[0].(*parachaintypes.ValidatorGroups) ret1, _ := ret[1].(error) return ret0, ret1 } @@ -446,10 +446,10 @@ func (mr *MockInstanceMockRecorder) ParachainHostValidatorGroups() *gomock.Call } // ParachainHostValidators mocks base method. -func (m *MockInstance) ParachainHostValidators() ([]parachain.ValidatorID, error) { +func (m *MockInstance) ParachainHostValidators() ([]parachaintypes.ValidatorID, error) { m.ctrl.T.Helper() ret := m.ctrl.Call(m, "ParachainHostValidators") - ret0, _ := ret[0].([]parachain.ValidatorID) + ret0, _ := ret[0].([]parachaintypes.ValidatorID) ret1, _ := ret[1].(error) return ret0, ret1 } diff --git a/lib/runtime/wasmer/exports.go b/lib/runtime/wasmer/exports.go index c083318956..ccbda0b4d1 100644 --- a/lib/runtime/wasmer/exports.go +++ b/lib/runtime/wasmer/exports.go @@ -9,7 +9,7 @@ import ( "github.com/ChainSafe/gossamer/dot/types" "github.com/ChainSafe/gossamer/lib/crypto/ed25519" - "github.com/ChainSafe/gossamer/lib/parachain" + parachaintypes "github.com/ChainSafe/gossamer/lib/parachain/types" "github.com/ChainSafe/gossamer/lib/runtime" "github.com/ChainSafe/gossamer/lib/transaction" "github.com/ChainSafe/gossamer/pkg/scale" @@ -349,13 +349,13 @@ func (in *Instance) GrandpaSubmitReportEquivocationUnsignedExtrinsic( // ParachainHostValidators returns the validator set at the current state. // The specified validators are responsible for backing parachains for the current state. -func (in *Instance) ParachainHostValidators() ([]parachain.ValidatorID, error) { +func (in *Instance) ParachainHostValidators() ([]parachaintypes.ValidatorID, error) { encodedValidators, err := in.Exec(runtime.ParachainHostValidators, []byte{}) if err != nil { return nil, fmt.Errorf("exec: %w", err) } - var validatorIDs []parachain.ValidatorID + var validatorIDs []parachaintypes.ValidatorID err = scale.Unmarshal(encodedValidators, &validatorIDs) if err != nil { return nil, fmt.Errorf("unmarshalling: %w", err) @@ -366,13 +366,13 @@ func (in *Instance) ParachainHostValidators() ([]parachain.ValidatorID, error) { // ParachainHostValidatorGroups returns the validator groups used during the current session. // The validators in the groups are referred to by the validator set Id. -func (in *Instance) ParachainHostValidatorGroups() (*parachain.ValidatorGroups, error) { +func (in *Instance) ParachainHostValidatorGroups() (*parachaintypes.ValidatorGroups, error) { encodedValidatorGroups, err := in.Exec(runtime.ParachainHostValidatorGroups, []byte{}) if err != nil { return nil, fmt.Errorf("exec: %w", err) } - var validatorGroups parachain.ValidatorGroups + var validatorGroups parachaintypes.ValidatorGroups err = scale.Unmarshal(encodedValidatorGroups, &validatorGroups) if err != nil { return nil, fmt.Errorf("unmarshalling: %w", err) @@ -388,7 +388,7 @@ func (in *Instance) ParachainHostAvailabilityCores() (*scale.VaryingDataTypeSlic return nil, fmt.Errorf("exec: %w", err) } - availabilityCores, err := parachain.NewAvailabilityCores() + availabilityCores, err := parachaintypes.NewAvailabilityCores() if err != nil { return nil, fmt.Errorf("new availability cores: %w", err) } @@ -403,8 +403,8 @@ func (in *Instance) ParachainHostAvailabilityCores() (*scale.VaryingDataTypeSlic // ParachainHostCheckValidationOutputs checks the validation outputs of a candidate. // Returns true if the candidate is valid. func (in *Instance) ParachainHostCheckValidationOutputs( - parachainID parachain.ParaID, - outputs parachain.CandidateCommitments, + parachainID parachaintypes.ParaID, + outputs parachaintypes.CandidateCommitments, ) (bool, error) { buffer := bytes.NewBuffer(nil) encoder := scale.NewEncoder(buffer) @@ -432,13 +432,13 @@ func (in *Instance) ParachainHostCheckValidationOutputs( } // ParachainHostSessionIndexForChild returns the session index that is expected at the child of a block. -func (in *Instance) ParachainHostSessionIndexForChild() (parachain.SessionIndex, error) { +func (in *Instance) ParachainHostSessionIndexForChild() (parachaintypes.SessionIndex, error) { encodedSessionIndex, err := in.Exec(runtime.ParachainHostSessionIndexForChild, []byte{}) if err != nil { return 0, fmt.Errorf("exec: %w", err) } - var sessionIndex parachain.SessionIndex + var sessionIndex parachaintypes.SessionIndex err = scale.Unmarshal(encodedSessionIndex, &sessionIndex) if err != nil { return 0, fmt.Errorf("unmarshalling: %w", err) @@ -450,8 +450,8 @@ func (in *Instance) ParachainHostSessionIndexForChild() (parachain.SessionIndex, // ParachainHostCandidatePendingAvailability returns the receipt of a candidate pending availability // for any parachain assigned to an occupied availability core. func (in *Instance) ParachainHostCandidatePendingAvailability( - parachainID parachain.ParaID, -) (*parachain.CommittedCandidateReceipt, error) { + parachainID parachaintypes.ParaID, +) (*parachaintypes.CommittedCandidateReceipt, error) { buffer := bytes.NewBuffer(nil) encoder := scale.NewEncoder(buffer) err := encoder.Encode(parachainID) @@ -464,7 +464,7 @@ func (in *Instance) ParachainHostCandidatePendingAvailability( return nil, fmt.Errorf("exec: %w", err) } - var candidateReceipt *parachain.CommittedCandidateReceipt + var candidateReceipt *parachaintypes.CommittedCandidateReceipt err = scale.Unmarshal(encodedCandidateReceipt, &candidateReceipt) if err != nil { return nil, fmt.Errorf("unmarshalling: %w", err) @@ -480,7 +480,7 @@ func (in *Instance) ParachainHostCandidateEvents() (*scale.VaryingDataTypeSlice, return nil, fmt.Errorf("exec: %w", err) } - candidateEvents, err := parachain.NewCandidateEvents() + candidateEvents, err := parachaintypes.NewCandidateEvents() if err != nil { return nil, fmt.Errorf("create new candidate events: %w", err) } @@ -493,7 +493,8 @@ func (in *Instance) ParachainHostCandidateEvents() (*scale.VaryingDataTypeSlice, } // ParachainHostSessionInfo returns the session info of the given session, if available. -func (in *Instance) ParachainHostSessionInfo(sessionIndex parachain.SessionIndex) (*parachain.SessionInfo, error) { +func (in *Instance) ParachainHostSessionInfo(sessionIndex parachaintypes.SessionIndex) ( + *parachaintypes.SessionInfo, error) { buffer := bytes.NewBuffer(nil) encoder := scale.NewEncoder(buffer) err := encoder.Encode(sessionIndex) @@ -506,7 +507,7 @@ func (in *Instance) ParachainHostSessionInfo(sessionIndex parachain.SessionIndex return nil, fmt.Errorf("exec: %w", err) } - var sessionInfo *parachain.SessionInfo + var sessionInfo *parachaintypes.SessionInfo err = scale.Unmarshal(encodedSessionInfo, &sessionInfo) if err != nil { return nil, fmt.Errorf("unmarshalling: %w", err) diff --git a/lib/runtime/wasmer/exports_test.go b/lib/runtime/wasmer/exports_test.go index 5e16c75b27..0df8bde56e 100644 --- a/lib/runtime/wasmer/exports_test.go +++ b/lib/runtime/wasmer/exports_test.go @@ -20,7 +20,7 @@ import ( "github.com/ChainSafe/gossamer/lib/common" "github.com/ChainSafe/gossamer/lib/crypto/ed25519" "github.com/ChainSafe/gossamer/lib/genesis" - "github.com/ChainSafe/gossamer/lib/parachain" + parachaintypes "github.com/ChainSafe/gossamer/lib/parachain/types" "github.com/ChainSafe/gossamer/lib/runtime" "github.com/ChainSafe/gossamer/lib/runtime/wasmer/testdata" "github.com/ChainSafe/gossamer/lib/trie" @@ -1290,24 +1290,24 @@ func TestInstance_ParachainHostValidators(t *testing.T) { response, err := rt.ParachainHostValidators() require.NoError(t, err) - expected := []parachain.ValidatorID{ - parachain.ValidatorID(mustHexTo32BArray(t, "0xa262f83b46310770ae8d092147176b8b25e8855bcfbbe701d346b10db0c5385d")), - parachain.ValidatorID(mustHexTo32BArray(t, "0x804b9df571e2b744d65eca2d4c59eb8e4345286c00389d97bfc1d8d13aa6e57e")), - parachain.ValidatorID(mustHexTo32BArray(t, "0x4eb63e4aad805c06dc924e2f19b1dde7faf507e5bb3c1838d6a3cfc10e84fe72")), - parachain.ValidatorID(mustHexTo32BArray(t, "0x74c337d57035cd6b7718e92a0d8ea6ef710da8ab1215a057c40c4ef792155a68")), - parachain.ValidatorID(mustHexTo32BArray(t, "0xe61d138eebd2069f1a76b3570f9de6a4b196289b198e33e6f0b59cef8837c511")), - parachain.ValidatorID(mustHexTo32BArray(t, "0x94ef34321ca5d37a6e8953183406b76f8ebf6a4be5eefc3997d022ac6e0a050e")), - parachain.ValidatorID(mustHexTo32BArray(t, "0xac837e8ca589521a83e7d9a7b307d1c41a5d9b940422488236f99646d21f3841")), - parachain.ValidatorID(mustHexTo32BArray(t, "0xb61cb85f7cf7616f9ef8f95010a51a68a4eae8afcdff715cc6a8d43da4a32a12")), - parachain.ValidatorID(mustHexTo32BArray(t, "0x382f17dae6b13a8ce5a7cc805056d9b592d918c8593f077db28cb14cf08a760c")), - parachain.ValidatorID(mustHexTo32BArray(t, "0x0825ba7677597ec9453ab5dbaa9e68bf89dc36694cb6e74cbd5a9a74b167e547")), - parachain.ValidatorID(mustHexTo32BArray(t, "0xcee3f65d78a239d7d199b100295e7a2d852ae898a6b81fd867b3471f25be7237")), - parachain.ValidatorID(mustHexTo32BArray(t, "0xe2ac8f039eb02370a9577e49ffc6032e6b5bf5ff77783bdc676d1432d714fd53")), - parachain.ValidatorID(mustHexTo32BArray(t, "0xce35fa64fe7a5a6fc456ed2830e64d5d1a5dba26e7a57ab458f8cedf1ec77016")), - parachain.ValidatorID(mustHexTo32BArray(t, "0xae40e895f46c8bfb3df63c119047d7faf21c3fe3e7a91994a3f00da6fa80f848")), - parachain.ValidatorID(mustHexTo32BArray(t, "0xa0e038975cff34d01c62960828c23ec10a305fe9f5c3589c2ae40f51963e380a")), - parachain.ValidatorID(mustHexTo32BArray(t, "0x807fa54347a8957ff5ef6c28e2403c83947e5fad4aa805c914df0645a07aab5a")), - parachain.ValidatorID(mustHexTo32BArray(t, "0x4c8e878d7f558ce5086cc37ca0d5964bed54ddd6b15a6663a95fe42e36858936")), + expected := []parachaintypes.ValidatorID{ + mustHexTo32BArray(t, "0xa262f83b46310770ae8d092147176b8b25e8855bcfbbe701d346b10db0c5385d"), + mustHexTo32BArray(t, "0x804b9df571e2b744d65eca2d4c59eb8e4345286c00389d97bfc1d8d13aa6e57e"), + mustHexTo32BArray(t, "0x4eb63e4aad805c06dc924e2f19b1dde7faf507e5bb3c1838d6a3cfc10e84fe72"), + mustHexTo32BArray(t, "0x74c337d57035cd6b7718e92a0d8ea6ef710da8ab1215a057c40c4ef792155a68"), + mustHexTo32BArray(t, "0xe61d138eebd2069f1a76b3570f9de6a4b196289b198e33e6f0b59cef8837c511"), + mustHexTo32BArray(t, "0x94ef34321ca5d37a6e8953183406b76f8ebf6a4be5eefc3997d022ac6e0a050e"), + mustHexTo32BArray(t, "0xac837e8ca589521a83e7d9a7b307d1c41a5d9b940422488236f99646d21f3841"), + mustHexTo32BArray(t, "0xb61cb85f7cf7616f9ef8f95010a51a68a4eae8afcdff715cc6a8d43da4a32a12"), + mustHexTo32BArray(t, "0x382f17dae6b13a8ce5a7cc805056d9b592d918c8593f077db28cb14cf08a760c"), + mustHexTo32BArray(t, "0x0825ba7677597ec9453ab5dbaa9e68bf89dc36694cb6e74cbd5a9a74b167e547"), + mustHexTo32BArray(t, "0xcee3f65d78a239d7d199b100295e7a2d852ae898a6b81fd867b3471f25be7237"), + mustHexTo32BArray(t, "0xe2ac8f039eb02370a9577e49ffc6032e6b5bf5ff77783bdc676d1432d714fd53"), + mustHexTo32BArray(t, "0xce35fa64fe7a5a6fc456ed2830e64d5d1a5dba26e7a57ab458f8cedf1ec77016"), + mustHexTo32BArray(t, "0xae40e895f46c8bfb3df63c119047d7faf21c3fe3e7a91994a3f00da6fa80f848"), + mustHexTo32BArray(t, "0xa0e038975cff34d01c62960828c23ec10a305fe9f5c3589c2ae40f51963e380a"), + mustHexTo32BArray(t, "0x807fa54347a8957ff5ef6c28e2403c83947e5fad4aa805c914df0645a07aab5a"), + mustHexTo32BArray(t, "0x4c8e878d7f558ce5086cc37ca0d5964bed54ddd6b15a6663a95fe42e36858936"), } require.Equal(t, expected, response) } @@ -1321,13 +1321,13 @@ func TestInstance_ParachainHostValidatorGroups(t *testing.T) { response, err := rt.ParachainHostValidatorGroups() require.NoError(t, err) - expected := ¶chain.ValidatorGroups{ - Validators: [][]parachain.ValidatorIndex{ + expected := ¶chaintypes.ValidatorGroups{ + Validators: [][]parachaintypes.ValidatorIndex{ {0, 1, 2, 3, 4, 5}, {6, 7, 8, 9, 10, 11}, {12, 13, 14, 15, 16}, }, - GroupRotationInfo: parachain.GroupRotationInfo{ + GroupRotationInfo: parachaintypes.GroupRotationInfo{ SessionStartBlock: 15946156, GroupRotationFrequency: 10, Now: 15946391, @@ -1365,7 +1365,7 @@ func TestInstance_ParachainHostSessionIndexForChild(t *testing.T) { response, err := rt.ParachainHostSessionIndexForChild() require.NoError(t, err) - expected := parachain.SessionIndex(27379) + expected := parachaintypes.SessionIndex(27379) require.Equal(t, expected, response) } @@ -1375,7 +1375,7 @@ func TestInstance_ParachainHostCandidatePendingAvailability(t *testing.T) { tt := getParachainHostTrie(t) rt := NewTestInstanceWithTrie(t, runtime.WESTEND_RUNTIME_v0942, tt) - response, err := rt.ParachainHostCandidatePendingAvailability(parachain.ParaID(1000)) + response, err := rt.ParachainHostCandidatePendingAvailability(parachaintypes.ParaID(1000)) require.NoError(t, err) expectedHash := parachainTestData.Expected["candidatePendingAvailability"] @@ -1415,14 +1415,14 @@ func TestInstance_ParachainHostSessionInfo(t *testing.T) { tt := getParachainHostTrie(t) rt := NewTestInstanceWithTrie(t, runtime.WESTEND_RUNTIME_v0942, tt) - response, err := rt.ParachainHostSessionInfo(parachain.SessionIndex(27379)) + response, err := rt.ParachainHostSessionInfo(parachaintypes.SessionIndex(27379)) require.NoError(t, err) - expected := ¶chain.SessionInfo{ - ActiveValidatorIndices: []parachain.ValidatorIndex{7, 12, 14, 1, 4, 16, 3, 11, 9, 6, 13, 15, 5, 0, 8, 10, 2}, + expected := ¶chaintypes.SessionInfo{ + ActiveValidatorIndices: []parachaintypes.ValidatorIndex{7, 12, 14, 1, 4, 16, 3, 11, 9, 6, 13, 15, 5, 0, 8, 10, 2}, RandomSeed: mustHexTo32BArray(t, "0x9a14667dcf973e46392904593e8caf2fb7a57904edbadf1547531657e7a56b5e"), DisputePeriod: 6, - Validators: []parachain.ValidatorID{ + Validators: []parachaintypes.ValidatorID{ mustHexTo32BArray(t, "0xa262f83b46310770ae8d092147176b8b25e8855bcfbbe701d346b10db0c5385d"), mustHexTo32BArray(t, "0x804b9df571e2b744d65eca2d4c59eb8e4345286c00389d97bfc1d8d13aa6e57e"), mustHexTo32BArray(t, "0x4eb63e4aad805c06dc924e2f19b1dde7faf507e5bb3c1838d6a3cfc10e84fe72"), @@ -1441,7 +1441,7 @@ func TestInstance_ParachainHostSessionInfo(t *testing.T) { mustHexTo32BArray(t, "0x807fa54347a8957ff5ef6c28e2403c83947e5fad4aa805c914df0645a07aab5a"), mustHexTo32BArray(t, "0x4c8e878d7f558ce5086cc37ca0d5964bed54ddd6b15a6663a95fe42e36858936"), }, - DiscoveryKeys: []parachain.AuthorityDiscoveryID{ + DiscoveryKeys: []parachaintypes.AuthorityDiscoveryID{ mustHexTo32BArray(t, "0x407a89ac6943b9d2ef1ceb5f1299941758a6af5b8f79b89b90f95a3e38179341"), mustHexTo32BArray(t, "0x307744a128c608be0dff2189557715b74734359974606d96dc4d256d61b1047d"), mustHexTo32BArray(t, "0x74fff2667b4a2cc69198ec9d3bf41f4d001ab644b45feaf89a21ff7ef3bd2618"), @@ -1460,7 +1460,7 @@ func TestInstance_ParachainHostSessionInfo(t *testing.T) { mustHexTo32BArray(t, "0xca17f0edc319c140113a44722f829aa1313da1b54298a10df49ad7d67d9de85f"), mustHexTo32BArray(t, "0x5a6bf6911fc41d8981c7c28f87e8ed4416c65e15624f7b4e36c6a1a72c7a7819"), }, - AssignmentKeys: []parachain.AssignmentID{ + AssignmentKeys: []parachaintypes.AssignmentID{ mustHexTo32BArray(t, "0x6acc35b896fe346adeda25c4031cf6a81e58dca091164370859828cc4456901a"), mustHexTo32BArray(t, "0x466627d554785807aaf50bfbdc9b8f729e8e20eb596ee5def5acd2acb72e405f"), mustHexTo32BArray(t, "0xc05cab9e7773ffaf045407579f9c8e16d56f119117421cd18a250c2e37fcb53a"), @@ -1479,7 +1479,7 @@ func TestInstance_ParachainHostSessionInfo(t *testing.T) { mustHexTo32BArray(t, "0xae7a30d143fd125490434ca7325025a2338d0b8bb28dcd9373dfd83756191022"), mustHexTo32BArray(t, "0xeeba7c46f5fa1ea21e736d9ebd7a171fb2afe0a4f828a222ea0605a4ad0e6067"), }, - ValidatorGroups: [][]parachain.ValidatorIndex{ + ValidatorGroups: [][]parachaintypes.ValidatorIndex{ { 0, 1, 2, 3, 4, 5, },