咨询热线

400-123-4657

notice  最新公告

NEWS

新闻动态

service phone 400-123-4657

公司动态 行业资讯

三亿体育官方网站_Python智能合约终极篇:合约执行引擎API

点击量:303    时间:2023-11-25
更多
本文摘要:在前两期的本体技术视点中,网卓新闻网,我们讲解了横跨合约静态调用与动态调用,描写了如何用于 RegisterAppCall API 与 DynamicAppCall API 横跨合约调用其他合约的函数。

在前两期的本体技术视点中,网卓新闻网,我们讲解了横跨合约静态调用与动态调用,描写了如何用于 RegisterAppCall API 与 DynamicAppCall API 横跨合约调用其他合约的函数。本期将转入本体 Python 智能合约语法专辑的终极篇,探究如何用于合约继续执行引擎 API,即 ExecutionEngine API。

它包括了3个 API,用法如下:本期语法可玩性较小,堪比 Python 智能合约界的九阴真经,完成学业了你就得意了!下面我们明确描写一下 ExecutionEngine API 的用于方法。在这之前,小伙伴们可以在本体智能合约开发工具 SmartX 中新建一个合约,回来我们展开操作者。某种程度,在文章最后我们将得出这次介绍的所有源代码以及视频介绍。02 ExecutionEngine API 用于方法用于 ExecutionEngine API 前必须将其引进。

这可以通过下面的语句构建上述三个函数的构建:from ontology.interop.System.ExecutionEngine import GetExecutingScriptHash, GetCallingScriptHash, GetEntryScriptHash2.1 GetExcutingScriptHashGetExecutingScriptHash API 尤为非常简单,它的起到是回到当前合约的合约哈希反序,即当前合约账户地址。from ontology.interop.System.ExecutionEngine import GetExecutingScriptHashdef Main(operation, args):if operation == "get_contract_hash":return get_contract_hash() return Falsedef get_contract_hash():return GetExecutingScriptHash()如图,右上角 Basic Info 表明了合约哈希,左下角控制器回到了当前合约哈希的反序。2.2 GetCallingScriptHashGetCallingScriptHash API 回到上一级调用者,即必要调用者的脚本哈希,该返回值与合约以及调用函数涉及。

因此有所不同合约、有所不同函数调用 GetCallingScriptHash 都会获得有所不同的脚本哈希,因为合约和函数是有所不同的调用者。from ontology.interop.System.ExecutionEngine import GetExecutingScriptHash, GetCallingScriptHashdef Main(operation, args):if operation == "GetCallingScriptHash_test1":return GetCallingScriptHash_test1()if operation == "GetCallingScriptHash_test2":return GetCallingScriptHash_test2() return Falsedef GetCallingScriptHash_test1():return GetCallingScriptHash() def GetCallingScriptHash_test2():return GetCallingScriptHash()如图所示,GetCallingScriptHash_test1 函数与 GetCallingScriptHash_test2 函数回到了两个有所不同的脚本哈希。

此外,将完全相同的函数放进有所不同的合约,也不会回到有所不同的脚本哈希。2.3 GetEntryScriptHash在智能合约的调用中,有必要调用者就有间接调用者(横跨合约调用)。GetEntryScriptHash,它不会回到入口(最初)调用者的脚本哈希。

我们打算两个智能合约,合约 A 与合约 B,假设合约 A 来调用合约 B 的功能函数。合约B的代码如下:from ontology.interop.System.ExecutionEngine import GetExecutingScriptHash, GetCallingScriptHash, GetEntryScriptHashfrom ontology.interop.System.Runtime import CheckWitness, GetTime, Notify, Serialize, Deserializedef Main(operation, args):if operation == "invokeB":return invokeB()if operation == "avoidToBeInvokedByContract":return avoidToBeInvokedByContract() return Falsedef invokeB():# the result of GetCallingScriptHash and GetEntryScriptHash is same# if they are invoked by the same contractcallerHash = GetCallingScriptHash()entryHash = GetEntryScriptHash()Notify([callerHash, entryHash])return [callerHash, entryHash]def avoidToBeInvokedByContract():# the purpose is to prevent hack from other contractcallerHash = GetCallingScriptHash()entryHash = GetEntryScriptHash()if callerHash != entryHash:Notify(["You are not allowed to invoke this method through contract"])return Falseelse:Notify(["You can implement what you need to do here!"])return True合约 A 的代码如下,该合约调用合约 B。

from ontology.interop.System.App import RegisterAppCallfrom ontology.interop.System.ExecutionEngine import GetExecutingScriptHash, GetCallingScriptHash, GetEntryScriptHashfrom ontology.interop.System.Runtime import CheckWitness, GetTime, Notify, Serialize, DeserializeContractB = RegisterAppCall('0f44f18d37515a917364362ec256c2110a7b1377', 'operation', 'args')def Main(operation, args):if operation == "invokeA":opt = args[0]return invokeA(opt)if operation == "checkHash":return checkHash()return Falsedef invokeA(opt):callerHash = GetCallingScriptHash()entryHash = GetEntryScriptHash()Notify(["111_invokeA",callerHash, entryHash])return ContractB(opt, [])def checkHash():Notify(["111_checkHash"])callerHash = GetCallingScriptHash()entryHash = GetEntryScriptHash()Notify([callerHash, entryHash])return True如图,首先运营 checkHash 函数,我们找到在同一个合约的同一个函数中,调用GetCallingScriptHash与GetEntryScriptHash API返回值完全相同,都是"a37ca1f1a3421d36b504769a96c06024a07b2bfa"。这是因为他们既是必要调用者,也是最初调用者(没横跨合约调用),所以两个 API 返回值完全相同。

但如果横跨合约调用呢?。


本文关键词:三亿体育官方网站

本文来源:三亿体育官方网站-www.qingstream.com


有什么问题请反馈给我们!


如有需求请您联系我们!

地址:吉林省松原市阿荣旗洛明大楼340号
电话:400-123-4657
传真:+86-123-4567
版权所有:Copyright © 2009-2023 www.qingstream.com. 三亿体育官方网站科技 版权所有

ICP备案编号:ICP备58519240号-5