From 16c7afc026910978d13fecfea5b16dc4891181f7 Mon Sep 17 00:00:00 2001 From: Edward Mack Date: Thu, 14 May 2020 10:37:49 -0400 Subject: [PATCH 1/4] add runtime test for Metadata_metadata call --- dot/core/service.go | 7 +++++++ lib/runtime/runtime_test.go | 11 +++++++++++ lib/runtime/types.go | 2 ++ 3 files changed, 20 insertions(+) diff --git a/dot/core/service.go b/dot/core/service.go index 64d6a82c29..12ae1b04d5 100644 --- a/dot/core/service.go +++ b/dot/core/service.go @@ -17,6 +17,7 @@ package core import ( "bytes" + "fmt" "math/big" "sync" @@ -526,3 +527,9 @@ func (s *Service) HandleSubmittedExtrinsic(ext types.Extrinsic) error { msg := &network.TransactionMessage{Extrinsics: []types.Extrinsic{ext}} return s.safeMsgSend(msg) } + +//GetMetadata calls runtime Metadata_metadata function +func (s *Service) GetMetadata() { + ret, err := s.rt.Exec(runtime.Metadata_metadata, []byte{}) + fmt.Printf("%v %v\n", ret, err) +} diff --git a/lib/runtime/runtime_test.go b/lib/runtime/runtime_test.go index c13863ca43..d3dbf729dc 100644 --- a/lib/runtime/runtime_test.go +++ b/lib/runtime/runtime_test.go @@ -65,3 +65,14 @@ func TestConcurrentRuntimeCalls(t *testing.T) { _, _ = runtime.Exec(CoreVersion, []byte{}) }() } + +func TestRuntime_Exec_Metadata(t *testing.T) { + // expected results based on results from previous runs + expected := []byte{16, 116, 101, 115, 116, 44, 112, 97, 114, 105, 116, 121, 45, 116, 101, 115, 116, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 36, 223, 106, 203, 104, 153, 7, 96, 155, 2, 0, 0, 0, 55, 227, 151, 252, 124, 145, 245, 228, 1, 0, 0, 0, 210, 188, 152, 151, 238, 208, 143, 21, 1, 0, 0, 0, 64, 254, 58, 212, 1, 248, 149, 154, 3, 0, 0, 0, 198, 233, 167, 99, 9, 243, 155, 9, 1, 0, 0, 0, 221, 113, 141, 92, 197, 50, 98, 212, 1, 0, 0, 0, 203, 202, 37, 227, 159, 20, 35, 135, 1, 0, 0, 0, 247, 139, 39, 139, 229, 63, 69, 76, 1, 0, 0, 0, 171, 60, 5, 114, 41, 31, 235, 139, 1, 0, 0, 0} + runtime := NewTestRuntime(t, POLKADOT_RUNTIME_c768a7e4c70e) + + ret, err := runtime.Exec(CoreVersion, []byte{}) + require.NoError(t, err) + + require.Equal(t, expected, ret) +} diff --git a/lib/runtime/types.go b/lib/runtime/types.go index 11bee1c0f9..8ebc9efc8e 100644 --- a/lib/runtime/types.go +++ b/lib/runtime/types.go @@ -84,6 +84,8 @@ var ( CoreInitializeBlock = "Core_initialize_block" // CoreExecuteBlock is the runtime API call Core_execute_block CoreExecuteBlock = "Core_execute_block" + // Metadata_metadata is the runtime API call Metadata_metadata + Metadata_metadata = "Metadata_metadata" // TaggedTransactionQueueValidateTransaction is the runtime API call TaggedTransactionQueue_validate_transaction TaggedTransactionQueueValidateTransaction = "TaggedTransactionQueue_validate_transaction" // AuraAPIAuthorities is the runtime API call AuraApi_authorities From 263a3d3082d53e0f6cc2c78bda3afa4bc4fa8a85 Mon Sep 17 00:00:00 2001 From: Edward Mack Date: Thu, 14 May 2020 10:56:38 -0400 Subject: [PATCH 2/4] implement RPC state_getMetadata --- dot/core/service.go | 6 ++---- dot/rpc/modules/api.go | 1 + dot/rpc/modules/state.go | 8 ++++++-- 3 files changed, 9 insertions(+), 6 deletions(-) diff --git a/dot/core/service.go b/dot/core/service.go index 12ae1b04d5..9d6246916e 100644 --- a/dot/core/service.go +++ b/dot/core/service.go @@ -17,7 +17,6 @@ package core import ( "bytes" - "fmt" "math/big" "sync" @@ -529,7 +528,6 @@ func (s *Service) HandleSubmittedExtrinsic(ext types.Extrinsic) error { } //GetMetadata calls runtime Metadata_metadata function -func (s *Service) GetMetadata() { - ret, err := s.rt.Exec(runtime.Metadata_metadata, []byte{}) - fmt.Printf("%v %v\n", ret, err) +func (s *Service) GetMetadata() ([]byte, error) { + return s.rt.Exec(runtime.Metadata_metadata, []byte{}) } diff --git a/dot/rpc/modules/api.go b/dot/rpc/modules/api.go index e9e5f169ff..70bc62a98d 100644 --- a/dot/rpc/modules/api.go +++ b/dot/rpc/modules/api.go @@ -45,6 +45,7 @@ type CoreAPI interface { GetRuntimeVersion() (*runtime.VersionAPI, error) IsBabeAuthority() bool HandleSubmittedExtrinsic(types.Extrinsic) error + GetMetadata() ([]byte, error) } // RPCAPI is the interface for methods related to RPC service diff --git a/dot/rpc/modules/state.go b/dot/rpc/modules/state.go index 27f3f10cc5..336f84cc6c 100644 --- a/dot/rpc/modules/state.go +++ b/dot/rpc/modules/state.go @@ -184,9 +184,13 @@ func (sm *StateModule) GetKeys(r *http.Request, req *StateStorageKeyRequest, res // TODO implement change storage trie so that block hash parameter works (See issue #834) } -// GetMetadata isn't implemented properly yet. -func (sm *StateModule) GetMetadata(r *http.Request, req *StateRuntimeMetadataQuery, res *StateMetadataResponse) { +// GetMetadata calls runtime Metadata_metadata function +func (sm *StateModule) GetMetadata(r *http.Request, req *StateRuntimeMetadataQuery, res *string) error { // TODO implement change storage trie so that block hash parameter works (See issue #834) + metadata, err := sm.coreAPI.GetMetadata() + // TODO use common.BytesToHex once PR #848 is merged + *res = "0x" + hex.EncodeToString(metadata) + return err } // GetRuntimeVersion Get the runtime version at a given block. From 07f351e57a58efcf84a893178648931dab779869 Mon Sep 17 00:00:00 2001 From: Edward Mack Date: Thu, 14 May 2020 11:13:16 -0400 Subject: [PATCH 3/4] add tests --- dot/rpc/modules/state_test.go | 12 ++++++++++++ lib/runtime/runtime_test.go | 10 ++++++---- 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/dot/rpc/modules/state_test.go b/dot/rpc/modules/state_test.go index 7ba204c181..b8900a21c2 100644 --- a/dot/rpc/modules/state_test.go +++ b/dot/rpc/modules/state_test.go @@ -153,6 +153,18 @@ func TestStateModule_GetStorageSize_NotFound(t *testing.T) { require.Equal(t, nil, res) } +func TestStateModule_GetMetadata(t *testing.T) { + sm := setupStateModule(t) + var res string + err := sm.GetMetadata(nil, nil, &res) + + // currently this is generating an error because runtime has not implemented Metadata_metadata yet + // expect this to change when runtime changes + require.Equal(t, "0x", res) + require.EqualError(t, err, "Failed to call the `Metadata_metadata` exported function.") + +} + func setupStateModule(t *testing.T) *StateModule { // setup service net := newNetworkService(t) diff --git a/lib/runtime/runtime_test.go b/lib/runtime/runtime_test.go index d3dbf729dc..72892b176b 100644 --- a/lib/runtime/runtime_test.go +++ b/lib/runtime/runtime_test.go @@ -67,12 +67,14 @@ func TestConcurrentRuntimeCalls(t *testing.T) { } func TestRuntime_Exec_Metadata(t *testing.T) { - // expected results based on results from previous runs - expected := []byte{16, 116, 101, 115, 116, 44, 112, 97, 114, 105, 116, 121, 45, 116, 101, 115, 116, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 36, 223, 106, 203, 104, 153, 7, 96, 155, 2, 0, 0, 0, 55, 227, 151, 252, 124, 145, 245, 228, 1, 0, 0, 0, 210, 188, 152, 151, 238, 208, 143, 21, 1, 0, 0, 0, 64, 254, 58, 212, 1, 248, 149, 154, 3, 0, 0, 0, 198, 233, 167, 99, 9, 243, 155, 9, 1, 0, 0, 0, 221, 113, 141, 92, 197, 50, 98, 212, 1, 0, 0, 0, 203, 202, 37, 227, 159, 20, 35, 135, 1, 0, 0, 0, 247, 139, 39, 139, 229, 63, 69, 76, 1, 0, 0, 0, 171, 60, 5, 114, 41, 31, 235, 139, 1, 0, 0, 0} + var expected []byte runtime := NewTestRuntime(t, POLKADOT_RUNTIME_c768a7e4c70e) - ret, err := runtime.Exec(CoreVersion, []byte{}) - require.NoError(t, err) + ret, err := runtime.Exec(Metadata_metadata, []byte{}) + // currently this is returning an error because runtime has not implemented Metadata_metadata yet + // expect this to change when runtime changes + require.EqualError(t, err, "Failed to call the `Metadata_metadata` exported function.") require.Equal(t, expected, ret) + } From 0df81ace328729e8e374ae314993be2096501d4c Mon Sep 17 00:00:00 2001 From: Edward Mack Date: Fri, 15 May 2020 10:11:17 -0400 Subject: [PATCH 4/4] utilize common.BytesToHex method --- dot/rpc/modules/state.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/dot/rpc/modules/state.go b/dot/rpc/modules/state.go index c41af8104b..ee561ea816 100644 --- a/dot/rpc/modules/state.go +++ b/dot/rpc/modules/state.go @@ -188,8 +188,7 @@ func (sm *StateModule) GetKeys(r *http.Request, req *StateStorageKeyRequest, res func (sm *StateModule) GetMetadata(r *http.Request, req *StateRuntimeMetadataQuery, res *string) error { // TODO implement change storage trie so that block hash parameter works (See issue #834) metadata, err := sm.coreAPI.GetMetadata() - // TODO use common.BytesToHex once PR #848 is merged - *res = "0x" + hex.EncodeToString(metadata) + *res = common.BytesToHex(metadata) return err }