ontology nlp_如何使用Python on Ontology编写智能合约? 第2部分:存储API

ontology nlp

image
Medium blog Medium博客上发布的官方教程

Excited to publish it for Habr readers. Feel free to ask any related questions and suggest a better format for tutorial materials

激动地将其发布给Habr读者。 随意提出任何相关问题,并为教程材料提出更好的格式

前言 (Foreword)

Earlier, in Part 1, we introduced the Blockchain & Block API of Ontology’s smart contract. Today we will discuss how to use the second module:

在前面的第1部分中,我们介绍了本体论智能合约的Blockchain&Block API 。 今天我们将讨论如何使用第二个模块:

存储API (Storage API)

. The Storage API has five related APIs that enable addition, deletion, and changes to persistent storage in blockchain smart contracts. Here’s a brief description of the five APIs:

。 存储API具有五个相关的API,可以在区块链智能合约中添加,删除和更改持久性存储。 以下是这五个API的简要说明:

image

Let’s take a closer look at how to use these five APIs. First, create a new contract SmartX and then follow the steps below. Aa usual, at the end of the article, we will provide the GitHub link of the source code.

让我们仔细看看如何使用这五个API。 首先,创建一个新的合同SmartX ,然后执行以下步骤。 通常,在文章结尾,我们将提供源代码的GitHub链接。

2如何使用存储API (2 How to Use Storage API)

2.1 GetContext和GetReadOnlyContext (2.1 GetContext & GetReadOnlyContext)

GetContext和GetReadOnlyContext (GetContext & GetReadOnlyContext)

gets the context in which the current smart contract runs. The return value is the reverse of the current smart contract hash. As the name implies,

获取运行当前智能合约的上下文。 返回值与当前智能合约哈希相反。 顾名思义,

GetReadOnlyContext (GetReadOnlyContext )

gets the context of the read-only mode. In the example below, the return value is the reverse of the contract hash displayed in the upper right corner.

获取只读模式的上下文。 在下面的示例中,返回值与右上角显示的合同哈希相反。

image

2.2放 (2.2 Put)

The

(Put )

function is responsible for storing the data on the blockchain in the form of a dictionary. As shown, Put accepts three parameters.

函数负责以字典的形式将数据存储在区块链上。 如图所示,Put接受三个参数。

GetContext (GetContext )

gets the context of the current smart contract running, the key is the key value that needs to store data, and value is the value of the data that needs to be stored. Please note that if the key value is already in storage, the function will update its corresponding value.

获取当前运行的智能合约的上下文,键是需要存储数据的键值,值是需要存储的数据的值。 请注意,如果键值已在存储中,则该函数将更新其相应的值。

image

2.3获取 (2.3 Get)

The

得到 (Get )

function is responsible for reading the data in the existing blockchain through the key value. In the example below, you can fill in the key value in the parameter panel on the right to run the function and read the data corresponding to the key value in the blockchain:

函数负责通过键值读取现有区块链中的数据。 在下面的示例中,您可以在右侧的参数面板中填写键值以运行该功能,并读取与区块链中的键值对应的数据:

image

2.4删除 (2.4 Delete)

The

删除 (delete )

function is responsible for deleting the data in the blockchain through the key value. In the example below, you can fill in the key value to run the function in the parameter panel on the right and delete the data corresponding to the key value in the blockchain:

函数负责通过键值删除区块链中的数据。 在下面的示例中,您可以在右侧的参数面板中填写键值以运行该函数,并在区块链中删除与键值对应的数据:

image

3 Storage API示例代码 (3 Storage API Sample Code)

The following code gives a detailed example of the use of five APIs:

以下代码给出了使用五个API的详细示例:

GetContext; 得到; 放; 删除; 和GetReadOnlyContext ( GetContext; Get; Put; Delete; and GetReadOnlyContext)

. You can try to run these APIs on

。 您可以尝试在以下位置运行这些API

SmartX. SmartX
from ontology.interop.System.Storage import GetContext, Get, Put, Delete, GetReadOnlyContext
from ontology.interop.System.Runtime import Notify

def Main(operation,args):
    if operation == 'get_sc':
        return get_sc()
    if operation == 'get_read_only_sc':
        return get_read_only_sc()
    if operation == 'get_data':
        key=args[0]
        return get_data(key)
    if operation == 'save_data':
        key=args[0]
        value=args[1]
        return save_data(key, value)
    if operation == 'delete_data':
        key=args[0]
        return delete_data(key)
    return False

def get_sc():
    return GetContext()
    
def get_read_only_sc():
    return GetReadOnlyContext()

def get_data(key):
    sc=GetContext() 
    data=Get(sc,key)
    return data
    
def save_data(key, value):
    sc=GetContext() 
    Put(sc,key,value)
    
def delete_data(key):
    sc=GetContext() 
    Delete(sc,key)

后记 (Afterword)

Blockchain storage is the core of the entire blockchain system.

区块链存储是整个区块链系统的核心。

Ontology Storage API的使用非常简单且对开发人员友好。 (The use of the Ontology Storage API is very simple and developer-friendly.)

On the other hand, storage is the focus of hackers, such as the security threat we mentioned in one previous article: storage injection attack, developers must pay special attention to code security when writing storage-related code.

另一方面,存储是黑客关注的焦点,例如我们在前一篇文章中提到的安全威胁: 存储注入攻击 ,开发人员在编写与存储相关的代码时必须特别注意代码安全性。

Find the detailed tutorial on GitHub here.

此处找到有关GitHub的详细教程。

In the next article, we will discuss how to use the

在下一篇文章中,我们将讨论如何使用

运行时API (Runtime API)

. Stay tuned!

。 敬请关注!



Are you a developer? Make sure you have joined our tech community on Discord. Also, take a look at the Developer Center on our website, there you can find developer tools, documentation, and more.

您是开发人员吗? 确保您已加入Discord上的我们的技术社区。 另外,请访问我们网站上的开发人员中心,在那里您可以找到开发人员工具,文档等。



在其他地方找到本体 (Find Ontology elsewhere)
Ontology website 本体网站 GitHub / GitHub / Discord Discord

Telegram (English/ Russian )

电报( 英文 / 俄文 )

Twitter / Twitter / Reddit / Reddit /

翻译自: https://habr.com/en/post/470720/

ontology nlp

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值