Skip to content

Commit

Permalink
Compatibility changes to integrate new MetadataV14 format
Browse files Browse the repository at this point in the history
  • Loading branch information
arjanz committed Aug 3, 2021
1 parent ef8ae3d commit 69656d0
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 27 deletions.
3 changes: 1 addition & 2 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ requests~=2.25.1
xxhash>=2.0.0
pytest>=6.2.2

# scalecodec~=0.11.16
https://github.com/polkascan/py-scale-codec/tarball/feature/scale_info
scalecodec~=1.0.0a
py-sr25519-bindings~=0.1.2
py-ed25519-bindings~=0.1.2
py-bip39-bindings~=0.1.6
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@
'idna>=2.8',
'requests~=2.25.1',
'xxhash>=1.3.0',
'scalecodec~=0.11.16',
'scalecodec~=1.0.0a',
'py-sr25519-bindings~=0.1.2',
'py-ed25519-bindings~=0.1.2',
'py-bip39-bindings~=0.1.6'
Expand Down
26 changes: 11 additions & 15 deletions substrateinterface/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -1855,7 +1855,7 @@ def get_type_registry(self, block_hash: str = None):
for event_index, event in enumerate(module.events):

for arg_index, arg in enumerate(event.args):
self.process_metadata_typestring(arg)
self.process_metadata_typestring(arg.type)

if len(storage_functions) > 0:
for idx, storage in enumerate(storage_functions):
Expand Down Expand Up @@ -1959,12 +1959,6 @@ def get_metadata_call_functions(self, block_hash=None):
)
)

# for call_index, (module, call) in self.metadata_decoder.call_index.items():
# call_list.append(
# self.serialize_module_call(
# module, call, self.runtime_version, call_index
# )
# )
return call_list

def get_metadata_call_function(self, module_name: str, call_function_name: str, block_hash: str = None):
Expand All @@ -1984,10 +1978,11 @@ def get_metadata_call_function(self, module_name: str, call_function_name: str,
"""
self.init_runtime(block_hash=block_hash)

for call_index, (module, call) in self.metadata_decoder.call_index.items():
if module.name == module_name and \
call.get_identifier() == call_function_name:
return call
for pallet in self.metadata_decoder.pallets:
if pallet.name == module_name and pallet.calls:
for call in pallet.calls:
if call.name == call_function_name:
return call

def get_metadata_events(self, block_hash=None):
"""
Expand Down Expand Up @@ -2033,10 +2028,11 @@ def get_metadata_event(self, module_name, event_name, block_hash=None):

self.init_runtime(block_hash=block_hash)

for event_index, (module, event) in self.metadata_decoder.event_index.items():
if module.name == module_name and \
event.name == event_name:
return event
for pallet in self.metadata_decoder.pallets:
if pallet.name == module_name and pallet.events:
for event in pallet.events:
if event.name == event_name:
return event

def get_metadata_constants(self, block_hash=None):
"""
Expand Down
9 changes: 9 additions & 0 deletions test/fixtures/metadata_hex.json

Large diffs are not rendered by default.

38 changes: 29 additions & 9 deletions test/test_helper_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,11 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import os
import unittest
from unittest.mock import MagicMock

from scalecodec.type_registry import load_type_registry_file
from substrateinterface.exceptions import SubstrateRequestException

from scalecodec.base import ScaleBytes
Expand All @@ -27,13 +28,19 @@

class TestHelperFunctions(unittest.TestCase):

test_metadata_version = 'V13'

@classmethod
def setUpClass(cls):

cls.substrate = SubstrateInterface(url='dummy', ss58_format=42, type_registry_preset='kusama')

cls.metadata_fixture_dict = load_type_registry_file(
os.path.join(os.path.dirname(__file__), 'fixtures', 'metadata_hex.json')
)

metadata_decoder = cls.substrate.runtime_config.create_scale_object('MetadataVersioned')
metadata_decoder.decode(ScaleBytes(metadata_v12_hex))
metadata_decoder.decode(ScaleBytes(cls.metadata_fixture_dict[cls.test_metadata_version]))

cls.substrate.get_block_metadata = MagicMock(return_value=metadata_decoder)

Expand Down Expand Up @@ -117,9 +124,9 @@ def test_get_metadata_call_function(self):
def test_get_metadata_event(self):
event = self.substrate.get_metadata_event("Balances", "Transfer")
self.assertEqual("Transfer", event.name)
self.assertEqual('AccountId', event.args[0])
self.assertEqual('AccountId', event.args[1])
self.assertEqual('Balance', event.args[2])
self.assertEqual('AccountId', event.args[0].type)
self.assertEqual('AccountId', event.args[1].type)
self.assertEqual('Balance', event.args[2].type)

def test_get_metadata_constant(self):
constant = self.substrate.get_metadata_constant("System", "BlockHashCount")
Expand Down Expand Up @@ -147,10 +154,7 @@ def test_get_metadata_storage_function(self):
def test_get_metadata_error(self):
error = self.substrate.get_metadata_error("System", "InvalidSpecName")
self.assertEqual("InvalidSpecName", error.name)
self.assertEqual(
[' The name of specification does not match between the current runtime', ' and the new runtime.'],
error.docs
)
self.assertIsNotNone(error.docs)

def test_helper_functions_should_return_null_not_exists(self):
self.assertIsNone(self.empty_substrate.get_block_number(
Expand All @@ -172,5 +176,21 @@ def test_helper_functions_invalid_input(self):
self.assertRaises(SubstrateRequestException, self.error_substrate.get_runtime_metadata, '0x')


class TestHelperFunctionsV14(TestHelperFunctions):
test_metadata_version = 'V14'

def test_get_metadata_constant(self):
constant = self.substrate.get_metadata_constant("System", "BlockHashCount")
self.assertEqual("BlockHashCount", constant.name)
self.assertEqual("scale_info::4", constant.type)
self.assertEqual("0x60090000", f"0x{constant.constant_value.hex()}")

def test_get_metadata_storage_function(self):
storage = self.substrate.get_metadata_storage_function("System", "Account")
self.assertEqual("Account", storage.name)
self.assertEqual("scale_info::0", storage.get_params_type_string()[0])
self.assertEqual("Blake2_128Concat", storage.type['Map']['hasher'])


if __name__ == '__main__':
unittest.main()

0 comments on commit 69656d0

Please sign in to comment.