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 |
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.
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.
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.