"Ethereum" Ethereum:smart contract

Source: Internet
Author: User
Tags commit constant hash pow prepare git clone
Introduction

History
There is a payment called P2SH (pay to script hash) in Bitcoin. With this feature, you can compile many interesting scripts (the original form of contract) like this wiki and this.

P2sh in Bitcoin:

Hash Locked Tx on ZKCP

op_sha256
<Y> op_equal
op_if
  <seller pubkey>
op_else
  <block_height+100> op_ Checklocktimeverify op_drop
  <buyer pubkey>
op_endif
Op_checksig

Complex logical control on contract, such as colored Bitcoin, and stake IPO, bitcoin have no these abilities.

Questions

How to pay fee for colored transaction on bitcoin network?

How to automatically color a coin in a chain of transactions?

Evolve

Bitcoin:utxo (n) = f (UTXO (N-1), Tx (n));

Ethereum:state (n) = f (State (N-1), Tx (n));

Ethereum:

state = Accounts + storages.

Accounts = Personal account (EOA) + contractaccount.

account = Balance + Storage.

contract account = account + EVM Byte Code.

Transaction makes transformation:
State (N-1) + Tx (n) ⇒ State (n) inventions in Ethereum

Powerful script:a turing-complete VM and Interperter

Account State and contract Stroage:use MPT (merkle-patricia-tree) as a database

Protection of network from abusing of the powerful Script:gas experiment on Ethereum private network

Checkout Go-ethereum from GitHub

$ git clone--recursive https://github.com/ethereum/go-ethereum
$ make 
$ export path= $PATH: ' pwd '/build/bin

If you prefer Cpp-ethereum, here are a steps for C + + coders on Linux, Cpp-ethereum

Create a Test account (EOA)

$ geth--datadir/tmp/nodes/node1 Account new
Passphrase:
$ geth--datadir/tmp/nodes/node1 Account List
...

Prepare a contract compiler solidity

$ sudo apt-get-y install build-essential \
        git \
        cmake \
        libgmp-dev \
        libboost-all-dev \

$ sudo add-a Pt-repository-y ppa:ethereum/ethereum
$ sudo add-apt-repository-y ppa:ethereum/ethereum-dev
$ sudo apt-get-y Update
$ sudo apt-get-y upgrade
$ sudo apt-get-y install Libcryptopp-dev

$ git clone--recursive https://git Hub.com/ethereum/solidity

$ cd solidity \
    && mkdir build \
    && CD build \
    && CMake. \
    && make install 

$ sloc--help

Prepare a Genesis Block Configure file

Reference Latest Craft

"{"
    nonce ":" 0x0000000000000042 ",     
    " timestamp ":" 0x0 ",
    " Parenthash ":" 0x0000000000000000000000000000000000000000000000000000000000000000 ",
    " Extradata ":" 0x0 ",     
    " GasLimit ":" 0x8000000 ",     
    " difficulty ":" 0x400 ",
    " Mixhash ":" 0x0000000000000000000000000000000000000000000000000000000000000000 ",
    " Coinbase ":" 0x3333333333333333333333333333333333333333 ",     
    " Alloc ": {  
        " 0x2773ffb039ded4bd86c3c96bf136e9f2bf510114 ": {
            "Balance": "20000000000000000000000"}} "


Mining with low difficulty

Diff--git A/core/block_validator.go b/core/block_validator.go
index a7c7f76: e1c9207 100644
---a/core/block_validator.go
+ + +
b/core/block_validator.go @@ -255,6 +255,7 @@ -255,6 Validateheader (config *chainconfig, pow pow.) PoW, Header *types. Header, Pare
 //The difficulty that a new block should has when created at time
 //Given the parent block ' s time and difficulty.
 Func calcdifficulty (config *chainconfig, time, Parenttime UInt64, Parentnumber, Parentdiff *big. INT) *big. Int {
+       return big. Newint (0x400);
        If config. Ishomestead (New big. INT). ADD (Parentnumber, Common. BIG1) {
                return Calcdifficultyhomestead (Time, Parenttime, Parentnumber, Parentdiff)
        } else {

Construct a Private network

$ geth--datadir/tmp/node2/node1 Init Node1.genesis.json

Contract

Contract HelloWorld {
    string greeting;

    function Greeter (string g) public {
        greeting = g;
    }

    function show () constant Returns (string) {
        return greeting;
    }
}

var src = ' < above script code in solidity > ';
var compiled = web3.eth.compile.solidity (SRC);
contract_compiled;

{
  HelloWorld: {
    code: ' < Long long byte code > ',
    info: {
      abidefinition: [{...}, {...}],
      CompilerOptions: "< many compiling options >",
      compilerversion: "0.3.6",
      Developerdoc: {
        methods : {}
      },
      language: "Solidity",
      languageversion: "0.3.6",
      Source: "< The contract source code > ",
      Userdoc: {
        methods: {}
      }
  }}
}

var abi = compiled.HelloWorld.info.abiDefinition;

[{
    constant:true,
    inputs: [],
    name: "Show",
    outputs: [{
        Name: "",
        Type: "string"
    }],
    type: "Function"
}, {
    constant:false,
    inputs: [{
        name: "G",
        Type: "string"
    }],
    Name: "Greeter",
    outputs: [],
    type: "function"
}]

var code = Compiled.HelloWorld.info.code;

"0x606060405261021c806100126000396000f3606060405260e060020a6000350463cc80f6f38114610026578063faf27bca1461008f575b005b6040 80516020818101835260008083528054845160026001831615610100026000190190921691909104601f8101849004840282018401909552848152610 1409490928301828280156101d95780601f106101ae576101008083540402835291602001916101d9565b610024600480803590602001908201803590 6020019191908080601f01602080910402602001604051908101604052809392919081815260200183838082843750949650505050505050806000600 0509080519060200190828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f106101e357805160 ff19168380011785555b506102139291505b80821115610218576000815560010161012c565b604051808060200182810382528381815181526020019 15080519060200190808383829060006004602084601f0104600302600f01f150905090810190601f1680156101a05780820380516001836020036101 000a031916815260200191505b509250505060405180910390f35b820191906000526020600020905b8154815290600101906020018083116101bc578 29003601f168201915b5050505050905090565b82800160010185558215610124579182015b828111156101245782518260005055916020019190600101906101f5565b505050565b509056 "

Eth.getblock (' pending ');

{
  difficulty:1024,
  extradata: "0x",
  gaslimit:132523880,
  gasused:0,
  hash:null,
  logsbloom : null,
  miner:null,
  nonce:null,
  number:13,
  parenthash: " 0xe35c8ddf8aee36b1ba8d714af9d06cef05082091ef6823af28dd9d02db0e0d76 ",
  receiptroot:" 0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421 ",
  sha3uncles:" 0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347 ",
  size:511,
  stateroot:" 0x0000000000000000000000000000000000000000000000000000000000000000 ",
  timestamp:1472006680,
  totaldifficulty:0,
  transactions: [],
  transactionsroot: " 0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421 ",
  uncles: []
}

var Txhash = Web3.eth.sendTransaction ({from: ' 0x2773ffb039ded4bd86c3c96bf136e9f2bf510114 ', Data:code});

I0824 10:55:49.044146 eth/api.go:1191] 
Tx (0x5f995744897e16e04e66b0e814f7a94cedb05698f309cc2961f6c4bfb151dd4a ) Created: 
0xc7e93af8effe641d1a271105f4b2171f4c8a792b

Web3.eth.getBlock (' pending ');

{
  difficulty:1024,
  extradata: "0x",
  gaslimit:132523880,
  gasused:90000,
  hash:null,
  Logsbloom:null,
  miner:null,
  nonce:null,
  number:13,
  parenthash: " 0xe35c8ddf8aee36b1ba8d714af9d06cef05082091ef6823af28dd9d02db0e0d76 ",
  receiptroot:" 0x11fd3dd942b74c76e301b93da5daad2c678b564f9338f18a03d98505603f8259 ",
  sha3uncles:" 0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347 ",
  size:1122,
  stateRoot:" 0x0000000000000000000000000000000000000000000000000000000000000000 ",
  timestamp:1472006680,
  totaldifficulty:0,
  transactions: ["0x5f995744897e16e04e66b0e814f7a94cedb05698f309cc2961f6c4bfb151dd4a"],
  transactionsroot: "0x984dda77ce5b7b794c61a4f51c375afc96993ef237ea9781680c4ad9f2891fac",
  uncles: []
}

var template = Web3.eth.contract (ABI);
var inst = template.new ("HelloWorld", {from: ' 0x2773ffb039ded4bd86c3c96bf136e9f2bf510114 ', Data:code});

I0824 11:05:42.861268 eth/api.go:1191] 
Tx (0xc2c775160a68edd99847c91300c4ea73ebb00c721beb10e37e89c8e85a705180 ) Created:
0xd2cfb391f5abb791b5c60a4b3402c39c69816961

Web3.eth.getBlock (' pending ');

{
  difficulty:1024,
  extradata: "0x",
  gaslimit:132523880,
  gasused:147601,
  hash:null,
  Logsbloom:null,
  miner:null,
  nonce:null,
  number:13,
  parenthash: " 0xe35c8ddf8aee36b1ba8d714af9d06cef05082091ef6823af28dd9d02db0e0d76 ",
  receiptroot:" 0x7c29b5761b6719eda8b10bcd8b4d0ae36b31367acbf98d357a111f681a846f82 ",
  sha3uncles:" 0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347 ",
  size:1766,
  stateRoot:" 0x0000000000000000000000000000000000000000000000000000000000000000 ",
  timestamp:1472006680,
  totaldifficulty:0,
  transactions: ["0x5f995744897e16e04e66b0e814f7a94cedb05698f309cc2961f6c4bfb151dd4a", " 0xc2c775160a68edd99847c91300c4ea73ebb00c721beb10e37e89c8e85a705180 "],
  transactionsroot:" 0xc76c381a00cf0e043edc83e21ecb190de4e58fc82679dae4e28861d708a562c8 ",
  uncles: []
}

Miner.start (1);

I0824 11:08:35.032051 miner/miner.go:119] Starting mining operation (Cpu=1 tot=2)
I0824 11:08:35.032724 miner/ WORKER.GO:573] Commit new work on Block 2 TXs & 0 uncles. Took 539.191µs
true
I0824 11:08:35.033581 eth/backend.go:454] Automatic pregeneration of Ethash DAG on (Ethash di R:/root/.ethash)
I0824 11:08:35.033652 ethash.go:259] generating DAG for epoch 0 (size 1073739904) (0000000000000000 000000000000000000000000000000000000000000000000)
I0824 11:08:35.033850 eth/backend.go:461] checking DAG ( Ethash dir:/root/.ethash)
I0824 11:08:36.038051 ethash.go:276] done generating DAG for epoch 0, it took 1.004395871s

....... waiting ....

I0824 11:12:41.036481 Miner/worker.go:339]  mined block (#13/f3f01c4c).  Wait 5 blocks for confirmation
I0824 11:12:41.037020 miner/worker.go:573] Commit new work on block 0 TXs & 0 uncles. Took 176.146µs
I0824 11:12:41.062208 miner/worker.go:573] Commit new work on Block 0 TXs & 0 uncles. Took 195.425µs

Miner.stop ();
Web3.eth.getBlock (13);

{difficulty:1024, Extradata: "0xd78301040a844765746887676f312e352e31856c696e7578", gaslimit:132523880, gasUsed: 147601, Hash: "0xf3f01c4cc3f620a24d73b441f027b8ebb0ca49a65537066b10f78f3691d37179", Logsbloom: " 0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
  000000000000000000000000000000 ", Miner:" 0x2773ffb039ded4bd86c3c96bf136e9f2bf510114 ", Nonce:" 0X2E6B7C14179FCFBA ", Number:13, Parenthash: "0xe35c8ddf8aee36b1ba8d714af9d06cef05082091ef6823af28dd9d02db0e0d76", ReceiptRoot: " 0x633761ffaf166a6195633b05c276057b1fa6d081799980daad86f9b71e52ced2 ", Sha3uncles:" 0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347 ", size:1790, StateRoot:" 0
  Xfebaec28d6a0131444f179febd692469bb15288e2869a92e1918268f66eab83c ", timestamp:1472008115, totaldifficulty:14336, Transactions: ["0x5f995744897e16e04e66b0e814f7a94cedb05698f309cc2961f6c4bfb151dd4a", " 0xc2c775160a68edd99847c91300c4ea73ebb00c721beb10e37e89c8e85a705180 "], Transactionsroot:" 0xc76c381a00cf0e043edc83e21ecb190de4e58fc82679dae4e28861d708a562c8 ", Uncles: []}

Web3.eth.getTransactionReceipt (' 0x5f995744897e16e04e66b0e814f7a94cedb05698f309cc2961f6c4bfb151dd4a ');

{
  Blockhash: "0xf3f01c4cc3f620a24d73b441f027b8ebb0ca49a65537066b10f78f3691d37179",
  blocknumber:13,
  Contractaddress: "0xc7e93af8effe641d1a271105f4b2171f4c8a792b",
  cumulativegasused:90000, from
  : " 0x2773ffb039ded4bd86c3c96bf136e9f2bf510114 ",
  gasused:90000,
  logs: [],
  root:" 44af6b2ec6b03f0ab6eb5e384188bd3c7e0687395a587310f27d78aa48925f96 ",
  to:null,
  transactionhash:" 0x5f995744897e16e04e66b0e814f7a94cedb05698f309cc2961f6c4bfb151dd4a ",
  transactionindex:0
}

Web3.eth.getTransactionReceipt (' 0xc2c775160a68edd99847c91300c4ea73ebb00c721beb10e37e89c8e85a705180 ');

{
  Blockhash: "0xf3f01c4cc3f620a24d73b441f027b8ebb0ca49a65537066b10f78f3691d37179",
  blocknumber:13,
  Contractaddress: "0xd2cfb391f5abb791b5c60a4b3402c39c69816961",
  cumulativegasused:147601, from
  : " 0x2773ffb039ded4bd86c3c96bf136e9f2bf510114 ",
  gasused:57601,
  logs: [],
  root:" 535656cd46445389bafecd6620d2541a98a94960876fca424e0815bb3f2e7c24 ",
  to:null,
  transactionhash:" 0xc2c775160a68edd99847c91300c4ea73ebb00c721beb10e37e89c8e85a705180 ",
  transactionindex:1
}

var inst = template.at (' 0xc7e93af8effe641d1a271105f4b2171f4c8a792b ');
Contract_inst.show ();

New BigNumber () not a base number: at 
raise (web3.js:14426:29) at
web3.js:14414:33 at
bignumber (web3.j s:13461:28) at
web3.js:1127:23 at web3.js:1656:20 for web3.js:840:16 at
web3.js:839:12
at Web3.js:4012:18 at
web3.js:4035:16 at
web3.js:4145:12

Failed Because this reason Leveldb

To inspect state in ' DataDir '/.ethereum/chainstate, use leveldbutil

Levedbutil Dump 000002.log
TODOCheck it by event log; difference between Contract Creatingand P2shDebug Cpp-ethereum or Go-ethereum Tech Details of EthereumMerkle-patricia-tree state (including user account, contract account, storage) management Evm:turing-complete VMS with gas Limit Lifetime of a contract rlp:recursive prefix length Uncle blocks POW and DAG algorithm Event and Log ReferenceStoragediscuss ethereumblockformat statetree expensivebutcoolstorage yellowpaper DecentralizedUpdate BlockChainSpec Storageofdata historystorage SMTP.experiment.on.ethereum like Bitmail AppendixLEVELDB dump on local mined 3 blocks
---offset 0; Sequence 8
  put ' mipmap-log-bloom-\x00\x00\x00\x00\x00\x0fb@ ' \x00\x00\x00\x00\x00

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.