DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Because the DevOps movement has redefined engineering responsibilities, SREs now have to become stewards of observability strategy.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Related

  • Building Secure Smart Contracts: Best Practices and Common Vulnerabilities
  • Building Decentralized Applications With Node.js and Web 3.0
  • Risk Assessment Using Blockchain
  • Deploying Smart Contract on Ethereum Blockchain

Trending

  • How To Build Resilient Microservices Using Circuit Breakers and Retries: A Developer’s Guide To Surviving
  • Tired of Spring Overhead? Try Dropwizard for Your Next Java Microservice
  • Introduction to Retrieval Augmented Generation (RAG)
  • Exploring Intercooler.js: Simplify AJAX With HTML Attributes
  1. DZone
  2. Software Design and Architecture
  3. Security
  4. Why Smart Contracts in Blockchain Need to Avoid Non-Deterministic Functions

Why Smart Contracts in Blockchain Need to Avoid Non-Deterministic Functions

In this post, I will be explaining the impact of using a timestamp inside a smart contract in the supply chain scenario.

By 
Arshad Sarfarz user avatar
Arshad Sarfarz
·
Nov. 03, 17 · Tutorial
Likes (2)
Comment
Save
Tweet
Share
13.6K Views

Join the DZone community and get the full member experience.

Join For Free

Non-deterministic functions used in smart contracts have the potential to make the smart contracts totally useless. In my previous blog – “Why Do We Need Blockchain?” – I used the example of supply chain to showcase the need for a Blockchain. In this post, we take a look at the same example of a supply chain implemented using Hyperledger fabric and how the non-deterministic function used in the smart contract (Chaincode) can wreak havoc.

Determinism in Blockchain:

All operations on the Blockchain should be deterministic. Simply put, the same operation performed across different nodes should return the same result. A difference in results between the nodes for the same operation can lead to a failure in consensus, since storing this data on the ledger will lead to an inconsistent ledger state thereby making the whole smart contract useless.

Determinism also refers to the fact that the same operation replayed on a different node at a different point in time should also produce the same results. For example, a new node that's been on-boarded to the network should be able to replay all the operations and end up in the correct ledger state, just like existing nodes.

The following are a few examples of non-deterministic operations on the Blockchain:

  • Using a timestamp inside the smart contract and storing the timestamp on the ledger.
  • Calling an external API (running outside the Blockchain) from the smart contract.

In this post, I will be explaining the impact of using a timestamp inside a smart contract in the supply chain scenario.

Hyperledger Smart Contracts:

Smart contracts in Hyperledger are called Chaincode. Chaincode is a program that can be written in Go or Java. All transactions on the ledger happen through Chaincode and manage business logic complied by the members of the network. We will not go into the details of the Chaincode and focus only on how a non-deterministic function can create problems with the consensus.

Hyperledger Smart Contract

Use of Timestamp in the Chaincode:

In the supply chain example, we will be capturing the movement of the goods between various parties on the distributed ledger. A simplified representation of the ledger state, which stores information regarding the shipment of goods from the manufacturer to logistics, is provided below with some sample JSON.

Sample Ledger State

Notice the "Timestamp" property that contains the date and time (including the milliseconds) on which the goods were shipped from the manufacturer to logistics. One of the easiest ways to fetch this timestamp in Go is to use the time.Now() function. Writing this function inside the Chaincode will result in a non-deterministic operation. The Chaincode will execute on all nodes of the network and there is no guarantee that it will execute at the same time. Hence, different nodes can produce different results based on the time of execution. The JSON output depicted above corresponds to JSON created from the Chaincode execution on the “Manufacturer” node. Execution of the Chaincode on other nodes can create different JSON outputs for the "Timestamp"  property based on the time of execution.

Non-Deterministic function inside Chaincode

If each individual node updates their ledger state with this value, then the ledger state will not be consistent across nodes making the complete smart contract and the associated business functionality useless since the Hyperledger platform validates the ledger state consistency and halts further processing.

The optimal solution to capture a timestamp with milliseconds is to pass this information from the client application by invoking the Chaincode. Passing a timestamp as input to the smart contract will make sure that the same value is passed as the input to all the nodes executing the Chaincode, resulting in a deterministic operation.

Non-Deterministic outside Chaincode

Conclusion:

The above example clearly illustrates the impact of using non-deterministic functions in smart contracts and how it can be avoided by using the same function in client applications invoking smart contracts.

Blockchain Smart contract Ledger (software)

Opinions expressed by DZone contributors are their own.

Related

  • Building Secure Smart Contracts: Best Practices and Common Vulnerabilities
  • Building Decentralized Applications With Node.js and Web 3.0
  • Risk Assessment Using Blockchain
  • Deploying Smart Contract on Ethereum Blockchain

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!