Skip to content

Commit

Permalink
Fixed MetadataV14 block author retrieval #138
Browse files Browse the repository at this point in the history
  • Loading branch information
arjanz committed Oct 14, 2021
1 parent 4be05f0 commit 1cadcbe
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 16 deletions.
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ requests>=2.21.0,<3
xxhash>=1.3.0,<3
pytest>=4.4.0

scalecodec>=1.0.6,<2
scalecodec>=1.0.9,<2
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.1.0,<4',
'requests>=2.21.0,<3',
'xxhash>=1.3.0,<3',
'scalecodec>=1.0.6,<2',
'scalecodec>=1.0.9,<2',
'py-sr25519-bindings~=0.1.2',
'py-ed25519-bindings~=0.1.2',
'py-bip39-bindings~=0.1.6'
Expand Down
47 changes: 35 additions & 12 deletions substrateinterface/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -2206,10 +2206,12 @@ def __get_block_handler(self, block_hash: str, ignore_decoding_errors: bool = Fa
except BlockNotFound:
return None

def decode_block(block_data):
def decode_block(block_data, block_data_hash=None):

if block_data:
block_data['header']['hash'] = block_hash
if block_data_hash:
block_data['header']['hash'] = block_data_hash

block_data['header']['number'] = int(block_data['header']['number'], 16)

extrinsic_cls = self.runtime_config.get_decoder_class('Extrinsic')
Expand Down Expand Up @@ -2245,16 +2247,37 @@ def decode_block(block_data):

if include_author and 'PreRuntime' in log_digest.value:

if log_digest.value['PreRuntime']['engine'] == 'BABE':
validator_set = self.query("Session", "Validators", block_hash=block_hash)
rank_validator = log_digest.value['PreRuntime']['data']['authority_index']
if self.implements_scaleinfo():
if log_digest.value['PreRuntime'][0] == f"0x{b'BABE'.hex()}":
babe_predigest = self.runtime_config.create_scale_object(
type_string='RawBabePreDigest',
data=ScaleBytes(log_digest.value['PreRuntime'][1])
)

babe_predigest.decode()

validator_set = self.query("Session", "Validators", block_hash=block_hash)
rank_validator = babe_predigest[1].value['authority_index']

block_author = validator_set[rank_validator]
block_data['author'] = block_author.value

block_author = validator_set.elements[rank_validator]
block_data['author'] = block_author.value
else:
raise NotImplementedError(
f"Cannot extract author for engine {log_digest.value['PreRuntime'][0]}"
)
else:
raise NotImplementedError(
f"Cannot extract author for engine {log_digest.value['PreRuntime']['engine']}"
)

if log_digest.value['PreRuntime']['engine'] == 'BABE':
validator_set = self.query("Session", "Validators", block_hash=block_hash)
rank_validator = log_digest.value['PreRuntime']['data']['authority_index']

block_author = validator_set.elements[rank_validator]
block_data['author'] = block_author.value
else:
raise NotImplementedError(
f"Cannot extract author for engine {log_digest.value['PreRuntime']['engine']}"
)

except Exception:
if not ignore_decoding_errors:
Expand Down Expand Up @@ -2287,11 +2310,11 @@ def result_handler(message, update_nr, subscription_id):

if header_only:
response = self.rpc_request('chain_getHeader', [block_hash])
return decode_block({'header': response['result']})
return decode_block({'header': response['result']}, block_data_hash=block_hash)

else:
response = self.rpc_request('chain_getBlock', [block_hash])
return decode_block(response['result']['block'])
return decode_block(response['result']['block'], block_data_hash=block_hash)

def get_block(self, block_hash: str = None, block_number: int = None, ignore_decoding_errors: bool = False,
include_author: bool = False, finalized_only: bool = False):
Expand Down
4 changes: 2 additions & 2 deletions test/test_block.py
Original file line number Diff line number Diff line change
Expand Up @@ -248,11 +248,11 @@ def test_include_author(self):
def test_subscribe_block_headers(self):

def subscription_handler(obj, update_nr, subscription_id):
return f"callback: '{obj['header']['hash']}"
return f"callback: {obj['header']['number']}"

result = self.substrate.subscribe_block_headers(subscription_handler)

self.assertEqual(f"callback: '0xec828914eca09331dad704404479e2899a971a9b5948345dc40abca4ac818f93", result)
self.assertEqual(f"callback: 103", result)

def test_check_requirements(self):
self.assertRaises(ValueError, self.substrate.get_block,
Expand Down

0 comments on commit 1cadcbe

Please sign in to comment.