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
- Comparison of IPFS and EdgeFS for Secure Edge/IoT Computing Use Cases
- Five major problems read through the PoA consensus algorithm
- Analysis | Pre-selection and pre-submission mechanisms for Byzantine fault-tolerant consensus
02 Runtime API Usage
From ontology.interop.System.Runtime import GetTime, CheckWitness, Notify, Serialize, Deserialize from ontology.interop.Ontology.Runtime import Base58ToAddress, AddressToBase58, GetCurrentBlockHas
From ontology.interop.System.Runtime import Notify Def demo(): Notify("hello world")
Little friends can view in Logs:
From ontology.interop.System.Runtime import GetTime
Def demo():
Time=GetTime()
Return time # returns the timestamp in seconds
From ontology.interop.Ontology.Runtime import GetCurrentBlockHash
Def demo():
Block_hash = GetCurrentBlockHash()
Return block_hash
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
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)
- 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
We will continue to update Blocking; if you have any questions or suggestions, please contact us!
Was this article helpful?
93 out of 132 found this helpful
Related articles
- Unlock Bitcoin for more apps, Miniscript yesterday, today and tomorrow!
- Technical Guide | Teach you to discuss Wasm contract development: (C++)
- Getting started with blockchain | Ethereum 2.0 terminology at a glance
- Defects of the heaviest chain rule: "The Crown Prince" in the "Public Ancestral Blocks"
- Technical Perspectives | Reflections on Citation Dynamic Language Object Models in Virtual Machines
- Technical Guide | Teach you to discuss Wasm contract development
- Getting started with IPFS | 50 questions about IPFS (4)