问题描述
大家好,我正在通过 py-substrate-interface 提交一个外部文件,但由于某种原因,我在执行提到的 here 示例时不断收到错误消息。 我的代码如下:
def send_funds(self,destination,amount):
self.log.info("Sending {} DOT to {} ...".format(amount,destination.strip()))
substrate = self.create_substrate_instance(self.node_ws_port[0])
keypair = Keypair.create_from_mnemonic('level payment mom grape proof display cause engage erupt rain hair arm')
print(keypair)
call = substrate.compose_call(
call_module='Balances',call_function='transfer',call_params={
'dest': destination,'value': ceil(amount * DOT)
}
)
try:
extrinsic = substrate.create_signed_extrinsic(call=call,keypair=keypair)
except Exception as e:
print(e)
try:
receipt = substrate.submit_extrinsic(extrinsic,wait_for_inclusion=True)
self.log.info("Extrinsic '{}' sent and included in block '{}'".format(receipt.extrinsic_hash,receipt.block_hash))
self.log.info("{} DOT sent to address: {}".format(amount,destination))
except SubstrateRequestException as e:
self.log.error("Failed to send: {}".format(e))
try:
extrinsic = substrate.create_signed_extrinsic(call=call,keypair=keypair)
except Exception as e:
print(e)
No more bytes available (offset: 80 / length: 72)
我该如何解决这个问题。
解决方法
大多数情况下,RemainingScaleBytesNotEmptyException
被引发,它与类型注册表相关。在 Substrate 运行时(如 Kusama、Polkadot 等)中定义了特定类型,这些类型(尚未)在元数据中公开,因此库必须包含对这些类型原语的分解。
故障排除的一些提示:
-
使用了错误的类型注册表。大多数时候 py-substrate-interface 可以自动发现它与哪个链通信,因此只需要指定
url
(https://github.com/polkascan/py-substrate-interface#autodiscover-mode)。但是对于自定义运行时或开发属性,如type_registry_preset
和ss58_format
需要手动设置 -
由于最近的运行时升级,本地类型注册表已过时,需要更新。这可以通过更新
py-scale-codec
包、运行substrate.reload_type_registry()
函数或始终使用带有use_remote_preset
kwarg 的远程类型注册表来实现(参见 https://github.com/polkascan/py-substrate-interface#keeping-type-registry-presets-up-to-date) -
在开发自定义运行时时,可以将引入的类型添加到自定义 JSON 文件中,格式类似于 https://github.com/polkascan/py-scale-codec/blob/master/scalecodec/type_registry/rococo.json 并在 init 期间提供(参见 https://github.com/polkascan/py-substrate-interface#substrate-node-template)
在深入挖掘后发现问题与奇偶校验编解码器中的编码有关,您需要根据运行时间调整网络配置。 所以我改自:
def create_substrate_instance(self,wsport):
return SubstrateInterface(
url=self.rpc_url + wsport,ss58_format=42,type_registry_preset='kusama',)
到:
def create_substrate_instance(self,type_registry_preset='substrate-node-template',)
它奏效了。