Ethereum: Smart Contract
Aug. 23, 2016
Core Concepts:
- MPT state and storage
- Turing-Complete VM
New features:
- Gas Limit and NO Free Transactions
- Uncle blocks strengthen
- Turing Complete Script
- State Storage mounted
Motivations:
- color-ed coins ( compare later)
- SPV (skipped)
Limit on color-ed coins of bitcoin
From UTXOs to State Database
Bitcoin blockchain is a database of UTXOs, in fact.
Smart Contract need more states be recorded in blockchains.
APIs of ethereum
Command line options:
- https://github.com/ethereum/go-ethereum/wiki/Command-Line-Options
Web3:
- https://github.com/ethereum/wiki/wiki/JavaScript-API
Console or RPC
- http://ethdocs.org/en/latest/contracts-and-transactions/contracts.html
Experiment: new account
$ geth --datadir ./node1 account new
> Address: {ed0340bb8ac2d79aec6156d2c383a8f89ce13686}
$ geth --datadir ./node1 init genesis.json
Experiment: genesis.json
{
"nonce": "0x0000000000000042",
"timestamp": "0x0",
"parentHash": "0x0000000000000000000000000000000000000000000000000000000000000000",
"extraData": "0x0",
"gasLimit": "0x8000000",
"difficulty": "0x400",
"mixhash": "0x0000000000000000000000000000000000000000000000000000000000000000",
"coinbase": "0x3333333333333333333333333333333333333333",
"alloc": {
"0xffffffffffffffffffff" : {
"balance" : "10000000000000000000"
}
}
}
Experiment: mining with low difficulty
Experiment: basic transfer
Ref:
- https://ethereum.gitbooks.io/frontier-guide/content/sending_ether.html
> var sender = eth.accounts[0];
> var receiver = eth.accounts[1];
> var amount = web3.toWei(0.01, "ether")
> eth.sendTransaction({from:sender, to:receiver, value: amount})
Experiment: compile a contract with solidity
sample: multiply
contract test {
function multiply(uint a) returns(uint d)
{ return a * 7; }
}
compile:
var test_Contract = eth.compile.solidity(source);
TODO:
more inspections on test_Contract;
Experiment: Burn gas to deploy contract
Essentialy deploying a contract is to sendTransaction
- to : <must absent>
- code: byte code of compiled contract
- from: address of contract creator
Note:
To deploy a contract requires payment to miners, so remeber to personal.unlockAccout().
- http://ethereum.stackexchange.com/questions/6870/create-a-new-contract-by-json-rpc-of-cpp-ethereumNote:
This is very like the P2SH, and ehtereum is evolved from this feature of bitcoin.
Experiment: call contract
Prerequisites to call a contract:
- Contract address
- ABI
sample:
>eth.getTransactionReceipt('0xb31cc6169ceb2db952749eb89f1a965eee0ca8de8f656633d9c6195fa01f2f59').contractAddress
>var contract_address = "0xb63897f156cef728279b461663286c11c8866ff1".
>var abi = [{constant: false, inputs: [{name: 'a', type: 'unit256'}]}];
> var c = web3.eth.contract(constract_abi);
> var obj = c.at(contract_address);
> var arg1 = 3;
> var my_acccount_address = '0x2773ffb039ded4bd86c3c96bf136e9f2bf510114';
> obj.multiply.sendTransaction(arg1, {from: my_account_address});
Experiment: a contract instance with constructor
Construct a contract instance with parameters and deploy this instance on the blockchain.
Note:
1) deploy the contract code.
2) invoke the Constructor with initialization paramters list.
TODO:
- MPT state & storage tree
- gas estimate
- Web3 API Impl.
- Inside contract lifetime
- contract global registration
on reading this snap text from the book: data insertion in bitcoin’s blockchain:
It is common knowledge that extrinsic data can be stored in the Blockchain, and there are
numerous websites that provide access to a subset of that data,51–53 and some excellent sleuthing
has uncovered a variety of interesting historical artifacts that have previously been stored.54
Nevertheless, there remains confusion and misinformation about the variety of different methods
by which data can be (and has been) stored. For instance, a recent comprehensive textbook on
Bitcoin included the following:
“There’s no good way to prevent people from writing arbitrary data into the Bitcoin
block chain [sic]. One possible countermeasure is to only accept Pay-to-Script-Hash
transactions. This would make it a bit more expensive to write in arbitrary data, but it
still wouldn’t prevent it.”55
The first claim, that one cannot prevent arbitrary data insertion, is correct, since there is no
general way to distinguish between legitimate address hashes and arbitrary binary data. However,
the second claim is false, as P2SH (Pay-to-Script-Hash) transactions actually provide the least
expensive and most efficient method for storing large amounts of arbitrary data (see section 5).
while reading the second claim, i feel very superised, but then the author say about “least expensive”, i get relaxed.