element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
Internet of Things
  • Technologies
  • More
Internet of Things
Blog Blockchain - Smart Contract Test
  • Blog
  • Forum
  • Documents
  • Quiz
  • Events
  • Polls
  • Members
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Join Internet of Things to participate - click to join for free!
  • Share
  • More
  • Cancel
Group Actions
  • Group RSS
  • More
  • Cancel
Engagement
  • Author Author: Jan Cumps
  • Date Created: 8 Apr 2020 2:50 PM Date Created
  • Views 635 views
  • Likes 3 likes
  • Comments 0 comments
  • android
  • infineon
  • security2go
  • security_2go
  • blockchain
  • rt
  • cryptoauthentication
  • cryptocurrency
Related
Recommended

Blockchain - Smart Contract Test

Jan Cumps
Jan Cumps
8 Apr 2020

This post is part of the Infineon Blockchain Starter Kit road test.

Blockchain - outside of the bitcoin context - is new to me.

Follow along with me on this path to learn the technology....

 

 

Let's dive further into the example Android App "coinfinity". This is the 2nd real thing: Submit a Smart Contract transaction

image

 

What is a Smart Contract?

 

A smart contact is a chunk of executable code. It can be executed "in the block chain".

It's called a contract because it performs transactions in the blockchain that are agreed by all partners.

It has a known set of inputs and a known effect. The code can be validated (not changed !) by anyone that participates.

You can see the contract as a mechanism to create a simple or complex booking on the chain.

As Ian Allison puts it: "...implement self-enforcing business logic embodied in smart contracts, leaving a non-repudiable and authoritative record of transactions."

 

In traceability scenarios, you could use a smart contract as the interface to record the goods transfered from one trusted party to another.

Another example is to record a vote, as in Infineon's example.

 

Example Smart Contract: Vote

 

The chain used in the Infinion Android app is one of Ethereum's examples: the "Simple Public Voting/Poll Demo".

It's written in Solidity. You can review the code (an essential part maintaining trust).

The code is not hard to understand. And that's a good thing because it should be review-friendly, so that people trust it as code that has a predictable effect and no hidden / obfuscated side effects:

 

contract Voting is Ownable, Destructible, CanRescueERC20 {

    /**
     * @dev number of possible choices. Constant set at compile time.
     */
    uint8 internal constant NUMBER_OF_CHOICES = 4;

    /**
     * @notice Number of total cast votes (uint40 is enough as at most
     *     we support 4 choices and 2^32 votes per choice).
     */
    uint40 public voteCountTotal;

    /**
     * @notice Number of votes, summarized per choice.
     *
     * @dev uint32 allows 4,294,967,296 possible votes per choice, should be enough,
     *     and still allows 8 entries to be packed in a single storage slot
     *     (EVM wordsize is 256 bit). And of course we check for overflows.
     */
    uint32[NUMBER_OF_CHOICES] internal currentVoteResults;

// ...

    /**
     * @notice Event gets emitted every time when a new vote is cast.
     *
     * @param addedVote choice in the vote
     * @param allVotes array containing updated intermediate result
     */
    event NewVote(uint8 indexed addedVote, uint32[NUMBER_OF_CHOICES] allVotes);

 function castVote(string voterName, uint8 givenVote)
    external {
        // answer must be given
        require(givenVote < numberOfChoices(), "Choice must be less than contract configured numberOfChoices.");


        // DEMO MODE: FOR EASIER TESTING, WE ALLOW UNLIMITED VOTES PER ADDRESS.
        // check if already voted
        //require(!votersInfo[msg.sender].exists, "This address has already voted. Vote denied.");


        //  voter name has to have at least 3 bytes (note: with utf8 some chars have
        // more than 1 byte, so this check is not fully accurate but ok here)
        require(bytes(voterName).length > 2, "Name of voter is too short.");


        // everything ok, add voter
        votersInfo[msg.sender] = Voter(true, givenVote, voterName);
        voteCountTotal = safeAdd40(voteCountTotal, 1);
        currentVoteResults[givenVote] = safeAdd32(currentVoteResults[givenVote], 1);


        // emit a NewVote event at this point in time, so that a web3 Dapp
        // can react it to it immediately. Emit full current vote state, as
        // events are cheaper for light clients than querying the state.
        emit NewVote(givenVote, currentVoteResults);
    }

note: these are snippets of the contract code. Check the link above to code block for the full contract.

 

In our case, the contract interface accept a given set of inputs. Some are common with a plain budget transfer (address, gas price, gas limit).

The address in this case is not the name of receiver address, but the smart contract address on the Ethereum network.

The inputs specific to this contract are the Voting name (free text) and the vote (a number between 0 and 3 - representing the radio button selected on the screen.

image

In the previous posts I dived deep into the Android code to show what happens when you submit a transaction.

This time I leave it as an exercise for the interested to download the code and view it in Android Studio.

These classes play a role:

  • VotingActivityOld
  • VotingUtilsOld
  • VotingOld

 

If you followed the App specific earlier posts and if you open the contract definition in a browser next to it, you 'll find enough common gooks to detect the flow.

 

image

The summary is that the screen values are recorded into a contract object, the Infineon 2Go smart card is used to sign the contract hash, then a call to the web3j api is made to submit the signed contract.

Read the previous posts but replace "Receiver address" by "Contract address".

It's worth doing the exercise, because next to understanding how a smart contract is coded and submitted, you can also appreciate some well written template based java code.

I did it, with the app running in debug mode on my phone. I set a breakpoint in line 141 of VotingActivityOld::resolveIntent().a

Then stepped all the way from filling the contract, hashing it, signing it and using the web3j calls.

 

The image in the post header shows the app when voting, and the resulting transaction's details in the Ethereum block chain.

 

 

Related Blog
Blockchain - HyperLedger Fabric pt 1: post and search transactions on a distributed trusted ledger
Blockchain - Hyperledger Burrow: set up a distributed ledger
Blockchain - Get Some Crypto Currency on an Infineon Security 2Go Card
Blockchain - Debug the Infineon Demo App with Android Studio and Your Phone
Blockchain - Analyse the Infineon Android Demo App - part a: Detect Card
Blockchain - Analyse the Infineon Android Demo App - part b: Retrieve Ethereum Crypto Currency Balance
Blockchain - Analyse the Infineon Android Demo App - part c: Transaction, Move Crypto Currency to Another Account
Blockchain - Talk Directly to the Infineon 2Go Smart Cards API
Blockchain - Smart Contract Test
Road Test notepad: Infineon Blockchain Starter Kit
Infineon Blockchain Starter Kit - Review
Blockchain - HyperLedger Fabric pt 2: application using blockchain and smart contract
Blockchain - HyperLedger Fabric pt 3: web console
  • Sign in to reply
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2025 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube