Technical Perspectives | How much does the Python Smart Contract Execution API know?

01 lead

In the previous issue, we introduced the Ontology Smart Contract Storage API . I believe that many small partners understand how to call the relevant API for persistent storage when developing Python smart contracts on the ontology. In this issue we discuss how to use the Runtime API (Contract Execution API). The Runtime API has eight related APIs that provide a common interface for contract execution, helping developers get data, transform data, and validate data. A brief description of these 8 APIs is as follows:

Figure | Network

Let's take a closer look at how to use these 8 APIs. Prior to this, the small partners can create a new contract in the Ontology Smart Contract Development Tool SmartX and follow us. Again, at the end of the article we will give you all the source code and video explanations for this tutorial.

02 Runtime API Usage

The reference to the Runtime API is divided into two paths: ontology.interop.System.Runtime and ontology.interop.Ontology.Runtime. Among them, the Ontology path contains a new API. The following statements reference these APIs.

 From ontology.interop.System.Runtime import GetTime, CheckWitness, Notify, Serialize, Deserialize from ontology.interop.Ontology.Runtime import Base58ToAddress, AddressToBase58, GetCurrentBlockHas 
2.1 Notify API
The Notify function pushes events to the entire network. In the following example, the function Notify will return a "hello world" hex string and push it to the entire network.
  From ontology.interop.System.Runtime import Notify
 Def demo():
     Notify("hello world") 

Little friends can view in Logs:

2.2 GetTime API
The GetTime function returns the current timestamp, which returns the Unix time at which the function was called, in seconds.
  From ontology.interop.System.Runtime import GetTime
 Def demo():
     Time=GetTime()
     Return time # returns the timestamp in seconds 
2.3 GetCurrentBlockHash API
The GetCurrentBlockHash function returns the hash of the current block.
  From ontology.interop.Ontology.Runtime import GetCurrentBlockHash
 Def demo():
     Block_hash = GetCurrentBlockHash()
     Return block_hash 
2.4 Serialize and Deserialize
This is a pair of serialization and deserialization functions. The Serialize function serializes an object into a byte array object, and the Deserialize function deserializes the byte array into the original object. The following code snippet implements serialization of incoming parameters and stores them in the persistent store of the contract. It also implements the fetching of data from the contract's persistent store and deserializing it.

  From ontology.interop.System.Runtime import Notify, Serialize, Deserialize
 From ontology.interop.System.Storage import Put, Get, GetContext
 Def Main(operation, args):
     If operation == 'serialize_to_bytearray':
         Data = args[0]
         Return serialize_to_bytearray(data)
     If operation == 'deserialize_from_bytearray':
         Key = args[0]
         Return deserialize_from_bytearray(key)
     Return False
 Def serialize_to_bytearray(data):
     Sc = GetContext()
     Key = "1"
     Byte_data = Serialize(data) # Serialize the passed arguments Put(sc, key, byte_data) # Store the serialized data in the blockchain 

Def deserialize_from_bytearray(key): sc = GetContext() byte_data = Get(sc, key) # Extract data from the blockchain by key data = Deserialize(byte_data) # Deserialize the bytearray data into the original type data return data

2.5 Base58ToAddress & AdressToBase58

  From ontology.interop.Ontology.Runtime import Base58ToAddress, AddressToBase58
 Def demo():
     Base58_addr="AV1GLfVzw28vtK3d1kVGxv5xuWU59P6Sgn"
     Addr=Base58ToAddress(base58_addr) # Convert base58 address to bytearray form address Notify(addr)
     Base58_addr=AddressToBase58(addr) #convert the bytearray address to base58 address Notify(base58_addr) 
This is a pair of address translation functions. Among them, the Base58ToAddress function converts the base58 encoded address into a byte array form address, and AddressToBase58 converts the byte array form address into a base58 encoded address.
2.6 CheckWitness
The CheckWitness(fromAcct) function has two functions:

  • Verify that the current function caller is not fromAcct. If yes (ie signature verification passed), the function returns.
  • Check if the current function caller is a contract. If it is a contract, and the function is executed from the contract, the verification is passed. That is, verify that fromAcct is the return value of GetCallingScriptHash(). Among them, the GetCallingScriptHash() function can get the contract hash value of the current smart contract.

GetCallingScriptHash():

https://github.com/ontio/ontology-python-compiler/blob/master/ontology/interop/System/ExecutionEngine.py

  From ontology.interop.System.Runtime import CheckWitness
 From ontology.interop.Ontology.Runtime import Base58ToAddress
 Def demo():
     Addr=Base58ToAddress("AW8hN1KhHE3fLDoPAwrhtjD1P7vfad3v8z")
     Res=CheckWitness(addr) # Verify that the caller address is AW8hN1KhHE3fLDoPAwrhtjD1P7vfad3v8z
     Return res 

03 Summary

In this technical point of view, we introduced the Runtime API of the ontology blockchain, which is very useful in ontology Python smart contracts. In the next technical point of view, we will introduce the Native API to explore how to transfer funds in ontology smart contracts. In all the grammar parts of this issue, we provide Chinese videos, and the friends can watch the lessons.

We will continue to update Blocking; if you have any questions or suggestions, please contact us!

Share:

Was this article helpful?

93 out of 132 found this helpful

Discover more

Web3

Chinese Gaming Company Plans Ambitious $100 Million Crypto Acquisition

The brand's strategy focuses on Hong Kong's growing influence as a hub for digital assets.

Market

Spot Bitcoin ETFs Hold More Bitcoin than MicroStrategy, Exclusions Apply 🚀

Bitcoin exchange-traded funds (ETFs) currently hold an impressive combined total of 192,255 bitcoins as of Wednesday,...

Market

Can Bitcoin really become a safe haven under the financial crisis?

The financial crisis is one of the most influential and most affected crises in the world today. It has far-reaching ...

Blockchain

Viewpoint | Bakkt myth is being pierced? The disadvantage of not being a teacher does not mean that Bitcoin futures have become a "water dog"

About the author: Galen Moore is a member of the CoinDesk Research team. The opinions expressed in this article are a...

Market

Even a ten-day break of $6,600 in bitcoin will continue to rise?

At 10:05 am today, AICoin market data showed that the spot price of the mainstream exchanges of BTC all exceeded $6,5...

Blockchain

Bitcoin that originally became the foundation of Web 3.0 and is the king of blockchain

Author | Muneeb Ali Translators | Production | Blockchain Base Camp It is undeniable that Bitcoin is still the undisp...