/smartcontract-playground

Primary LanguageGoApache License 2.0Apache-2.0

Playgroud for Smart Contracts

This is our playgroud for smart contracts. Once the contract is mature, the contract will be moved to another repository, such as tokenapp-smartcontract.

Contracts for Measuring Temperature

There are two contracts to measure and record temperature, variant A and B. Variant B is cheaper in terms of gas used as it only records pass/fail, number of measurements, number of failed measurements and when the first measurement failed.

Variant A is more expensive, stores more data, e.g. when the measuremnt started, when it stopped and records the last n failures.

Useful links

Deployment

Currently deployment is done with the script prepare.sh which removes the lines breaks, creates an initial JavaScript file that creates the contract

 source = ...
 contract = eth.compile.solidity(source).TemperatureMeasurement -> ##A or B##
 var abi = eth.contract(contract.info.abiDefinition)
 abi.new(eth.account[1], 0, 30, 10, {from: eth.account[0], data: contract.code, gas: 600000}, function(e, contract){
    if(!e) {
      if(!contract.address) {
        console.log("Contract transaction send: https://testnet.etherscan.io/tx/" + contract.transactionHash + " waiting to be mined...");
      } else {
        console.log("Contract mined! Address: " + contract.address);
        var tx = abi.at(contract.address);
      }

    }
});

contract cost A: 773639 gas contract cost B: 391544 gas

Measurements

Since both A and B contracts are deployed, used

var tx = abiA.at("0x...") // for contract A
var tx = abiB.at("0x...") // for contract B

###Send temperature with contract A, everything good:

tx.reportTemperature.sendTransaction([1,2], [1000,2000], {from:eth.accounts[1], gas: 300000}) -> cost: 59073 gas // now 87902 gas tx.reportTemperature.sendTransaction([1,2,3], [1000,2000,3000], {from:eth.accounts[1], gas: 300000}) -> cost: 61203 gas tx.reportTemperature.sendTransaction([1,2,3,4], [1000,2000,3000,4000], {from:eth.accounts[1], gas: 300000}) -> cost: 63333 gas -> 1 more: 2130 gas

tx.reportTemperature.sendTransaction([1,2,1,2,2,1,2,2,1,2,2,1,2,2,1,2,2,1,2,2,1,2,2,1,2,2,1,2,2,1,2,2,1,2], [1000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000] , {from:eth.accounts[0], gas: 300000}) -> cost for 34 measurements: //135402

tx.reportTemperature.sendTransaction([1,2,1,2,2,1,2,2,1,2,2,1,2,2,1,2,2,1,2,2,1,2,2,1,2,2,1,2,2,1,2,2,1,2,2,1,2,2,1,2,2,1,2,2,1,2,2,1,2,2,1,2,2,1,2,2,1,2,2,1,2,2,1,2,2,1,2,2,1,2,2,1,2,2,1,2,2,1,2,2,1,2,2,1,2,2,1,2,2,1,2,2,1,2,2,1,2,2,1,2,2,1,2,2,1,2,2,1,2,2,1,2,2,1,2,2,1,2,2,1,2,2,1,2,2,1,2,2,1,2,2,1,2,2,1,2,2,1,2,2,1,2,2,1,2,2,1,2,2,1,2,2,1,2,2,1,2,2,1,2,2,1,2,2,1,2,2,1,2,2,1,2,2,1,2,2,1,2,2,1,2,2,1,2,2,1,2,2,1,2,2,1,2,2,1,2,2,1,2], [1000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000] , {from:eth.accounts[0], gas: 1400000}) -> cost for 200 measurements: 515622 (541291) -> loop twice over all measurements 843815 -> loop twice over all temp 843815 -> merged loop: 529099 -> no hash shifting: 238758

tx.reportTemperature.sendTransaction(200, [1, 0, 1, 0, 0, 2, 0, 2, 0, 0, 2, 0, 2, 0, 0, 2, 0, 2, 0, 0, 2, 0, 2, 0, 0, 2, 0, 2, 0, 0, 2, 0, 2, 0, 0, 2, 0, 2, 0, 0, 2, 0, 2, 0, 0, 2, 0, 2, 0, 0, 1, 0, 1, 0, 0, 2, 0, 2, 0, 0, 2, 0, 2, 0, 0, 2, 0, 2, 0, 0, 2, 0, 2, 0, 0, 2, 0, 2, 0, 0, 2, 0, 2, 0, 0, 2, 0, 2, 0, 0, 2, 0, 2, 0, 0, 2, 0, 2, 0, 0, 1, 0, 1, 0, 0, 2, 0, 2, 0, 0, 2, 0, 2, 0, 0, 2, 0, 2, 0, 0, 2, 0, 2, 0, 0, 2, 0, 2, 0, 0, 2, 0, 2, 0, 0, 2, 0, 2, 0, 0, 2, 0, 2, 0, 0, 2, 0, 2, 0, 0, 1, 0, 1, 0, 0, 2, 0, 2, 0, 0, 2, 0, 2, 0, 0, 2, 0, 2, 0, 0, 2, 0, 2, 0, 0, 2, 0, 2, 0, 0, 2, 0, 2, 0, 0, 2, 0, 2, 0, 0, 2, 0, 2, 0, 0, 2, 0, 2, 0, 0, 1, 0, 1, 0, 0, 2, 0, 2, 0, 0, 2, 0, 2, 0, 0, 2, 0, 2, 0, 0, 2, 0, 2, 0, 0, 2, 0, 2, 0, 0, 2, 0, 2, 0, 0, 2, 0, 2, 0, 0, 2, 0, 2, 0, 0, 2, 0, 2, 0, 0, 1, 0, 1, 0, 0, 2, 0, 2, 0, 0, 2, 0, 2, 0, 0, 2, 0, 2, 0, 0, 2, 0, 2, 0, 0, 2, 0, 2, 0, 0, 2, 0, 2, 0, 0, 2, 0, 2, 0, 0, 2, 0, 2, 0, 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 2, 0, 0, 2, 0, 2, 0, 0, 2, 0, 2, 0, 0, 2, 0, 2, 0, 0, 2, 0, 2, 0, 0], {from:eth.accounts[0], gas: 1500000}) -> cost for 200 measurements: 592240


new contract cost contract A2: 1110688 tx.generateReport([1,2,1,2,2,1,2,2,1,2,2,1,2,2,1,2,2,1,2,2,1,2,2,1,2,2,1,2,2,1,2,2,1,2,2,1,2,2,1,2,2,1,2,2,1,2,2,1,2,2,1,2,2,1,2,2,1,2,2,1,2,2,1,2,2,1,2,2,1,2,2,1,2,2,1,2,2,1,2,2,1,2,2,1,2,2,1,2,2,1,2,2,1,2,2,1,2,2,1,2,2,1,2,2,1,2,2,1,2,2,1,2,2,1,2,2,1,2,2,1,2,2,1,2,2,1,2,2,1,2,2,1,2,2,1,2,2,1,2,2,1,2,2,1,2,2,1,2,2,1,2,2,1,2,2,1,2,2,1,2,2,1,2,2,1,2,2,1,2,2,1,2,2,1,2,2,1,2,2,1,2,2,1,2,2,1,2,2,1,2,2,1,2,2,1,2,2,1,2], [1000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000]) --> result [[], [], 0, 199, "0xa56dbfa51f222607e717ee682fcdadf0d5255b16d3421a70dfef93879f3f7802"] tx.reportResult.sendTransaction([], [],0, 199, 1000, 2000, "0xa56dbfa51f222607e717ee682fcdadf0d5255b16d3421a70dfef93879f3f7802", {from:eth.accounts[0], gas: 300000}) --> cost: 97413 tx.verifyReport(0, [1,2,1,2,2,1,2,2,1,2,2,1,2,2,1,2,2,1,2,2,1,2,2,1,2,2,1,2,2,1,2,2,1,2,2,1,2,2,1,2,2,1,2,2,1,2,2,1,2,2,1,2,2,1,2,2,1,2,2,1,2,2,1,2,2,1,2,2,1,2,2,1,2,2,1,2,2,1,2,2,1,2,2,1,2,2,1,2,2,1,2,2,1,2,2,1,2,2,1,2,2,1,2,2,1,2,2,1,2,2,1,2,2,1,2,2,1,2,2,1,2,2,1,2,2,1,2,2,1,2,2,1,2,2,1,2,2,1,2,2,1,2,2,1,2,2,1,2,2,1,2,2,1,2,2,1,2,2,1,2,2,1,2,2,1,2,2,1,2,2,1,2,2,1,2,2,1,2,2,1,2,2,1,2,2,1,2,2,1,2,2,1,2,2,1,2,2,1,2], [1000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000])

###Send temperature with contract A, temperature failures:

tx.reportTemperature.sendTransaction([1,31], [1000,2000], {from:eth.accounts[1], gas: 300000}) -> cost: 104683 tx.reportTemperature.sendTransaction([1,31,32], [1000,2000,3000], {from:eth.accounts[1], gas: 300000}) -> cost: 87361 tx.reportTemperature.sendTransaction([1,31,32,33], [1000,2000,3000,4000], {from:eth.accounts[1], gas: 300000}) -> cost: 100029 -> 1 more: 12668 gas

###Send temperature with contract B, everything good:

tx.reportTemperature.sendTransaction([1,2], [1000,2000], {from:eth.accounts[1], gas: 300000}) -> cost: 34812 gas tx.reportTemperature.sendTransaction([1,2,3], [1000,2000,3000], {from:eth.accounts[1], gas: 300000}) -> cost: 35547 gas tx.reportTemperature.sendTransaction([1,2,3,4], [1000,2000,3000,4000], {from:eth.accounts[1], gas: 300000}) -> cost: 36282 gas -> 1 more: 735 gas

###Send temperature with contract B, temperature failures:

tx.reportTemperature.sendTransaction([1,31], [1000,2000], {from:eth.accounts[1], gas: 300000}) -> cost: 54976 tx.reportTemperature.sendTransaction([1,31,32], [1000,2000,3000], {from:eth.accounts[1], gas: 300000}) -> cost: 35475 tx.reportTemperature.sendTransaction([1,31,32,33], [1000,2000,3000,4000], {from:eth.accounts[1], gas: 300000}) -> cost: 36174 -> 1 more: 699 gas