太坊 (Ethereum 一智能合约 和去中心化应用平台 太坊白皮书-Ethereum.pdf · PDF file太坊 (Ethereum):一智能合约 和去中心化应用平台 原文地址:

Embed Size (px)

Citation preview

  • Ethereum:

    http://ethereum.org/ethereum.html

    -

    - -

    -

    -

    metacoins DACs

    -

    -

    2.0

    1995 Web2.0

    1.

    o

    o

    2.

    3.

    4. P2P

    o

    o

    o

    o

    o

    5.

    o

    o

    o

    o

    o

    o

    o

    o

    6.

    7.

    8.

    http://ethereum.org/ethereum.html

  • DAC

    HTTP TCP/IP

    1. H:i(H i ),

    ( H:i 1 = 1 amagimetals.com )

    2.

    3. H:i

    4. X , X H:i

    X

    0 0 1

    1

    5.

    1. -

    http://amagimetals.com/

  • Merkle tree

    2.

    1BTC

    H:i 1

    3.

    HTTP TCP

    MARKER, FROM, TO VALUEMARKER

    FROM TO VALUE

    MARKER

    if tx.output[0] != MARKER:

    break

    else if balance[tx.output[1]] < decode_value(tx.output[3]):

    break

    else if not tx.hasSignature(tx.output[1]):

    break

    else:

  • balance[tx.output[1]] -= decode_value(tx.output[3]);

    balance[tx.output[2]] += decode_value(tx.output[3]);

    X 30 X 30

    30

    TCP HTTP

    SMTP HTTPSMTP

    SMTP HTTP

    1. -

    2.

    DaemonSkynet

    3.

    Dagger, Patricia trees and

    RLP, recursive length prefix encoding,

    4.

    04 SHA256

    RIPEMD160 SHA3 20

    http://wiki.ethereum.org/index.php/Daggerhttp://wiki.ethereum.org/index.php/Patricia_Treehttp://wiki.ethereum.org/index.php/RLP

  • P2P

    P2P Yonatan

    Sompolinsky Aviv Zohar 2013 12 "Greedy Heaviest Observed Subtree" (GHOST) protocol

    bitcoind

    1.

    2.

    3

    3.

    4.

    5.

    6. 15

    7.

    8. TD(block) ("")TD TD(genesis_block) = 0 TD(B) = TD(B.parent) + sum(u.difficulty for u in

    B.uncles) + B.difficulty

    9.

    t A

    B A B B

    A 30% B 10%A 70%

    B 90%A

    90% 75%

    12.5%

    Hashcash

  • - 2128 ( 250.9 ) 2100

    mBTC/uBTC/

    1:

    103: ()

    106: ()

    109: ()

    1012:

    1015:

    1018:

    103, 106 109

    0.0001 X

    0.25X

    0.25X

    0.5X

    1 5

    2X 4X

    50% 25%

    12.5% 6.25%

    12.5% 6.25%

    25% 62.5%

    25%6.25%6.25% 62.5%

    1000001 500001.5

    * X 500002 * X, 0.0001% 1-10

  • / 1% 100

    * (0.5X) 0.5X 0.5X

    recursive length prefix encoding,RLP,

    ['dog', 'cat'] () [ 130, 67, 100, 111, 103, 67, 99, 97, 116];

    dog[ 100, 111, 103 ],

    [ 67, 100, 111, 103 ]. RLP RLP

    RLP

    0 0 0

    256 32767 [ 127, 255 ]

    [

    block_header,

    transaction_list,

    uncle_list

    ]

    Where:

    transaction_list = [

    transaction 1,

    transaction 2,

    ...

    ]

    uncle list = [

    uncle_block_header_1,

    uncle_block_header_2,

    ...

    ]

    block_header = [

    parent hash,

    sha3(rlp_encode(uncle_list)),

    coinbase address,

    state_root,

    sha3(rlp_encode(transaction_list)),

    difficulty,

    timestamp,

    extra_data,

    nonce

    ]

    transaction uncle_block_header nonce RLP

    http://wiki.ethereum.org/index.php/RLP

  • uncle_list transaction_list nonce extra_data 32

    extra_data

    state_root key, value-Merkle Patricia tree

    20 - value RLP

    [ balance, nonce, contract_root ]

    nonce 1(1)(2)

    balance

    contract_root

    contract_root 20 0

    32 0

    -

    scrypt

    -

    Dagger

    50-500MB

    RAM.

    : http://wiki.ethereum.org/index.php/Dagger

    [ nonce, receiving_address, value, [ data item 0, data item 1 ... data item n ], v, r, s ]

    nonce ( 0 -> '', 7 -> '\x07', 1000 -> '\x03\xd8'). (v,r,s)

    Electrum v 27

  • anc(block,n) block n

    60 500

    500+/-20%

    2256 2256

    C

    while

    C tx.sender

    tx.valuetx.feetx.data tx.datan contract.storage contract.address

    block.contract_storageblock.account_balanceblock.numberblock.difficultyblock.parenthashblock.basefee

    block.timestamp block.basefee

    A

    X C A (C, 100 * block.basefee, [A, X])

    100

    16

    if tx.value < 100 * block.basefee:

    stop

    elif contract.storage[1000]:

    from = tx.sender

    to = tx.data[0]

    value = tx.data[1]

    if to

  • 10

    Mastercoin

    if tx.value < block.basefee:

    stop

    if tx.sender != contract.creator:

    stop

    contract.storage[data[0]] = data[1]

    D I block.contract_storage(D)[I]

    CDO

    A 4000 1000

    D I 1000 25 B 30

    25 B A B

    B 30 80%

    B A 0.2% A

    A 1.25 A

    B

    state = contract.storage[1000]

    if state == 0:

    if tx.value < 1000 * 10^18:

    stop

    contract.storage[1001] = 998 * block.contract_storage(D)[I]

    contract.storage[1002] = block.timestamp + 30 * 86400

    contract.storage[1003] = tx.sender

    else:

    if tx.value < 200 * block.basefee:

    stop

    ethervalue = contract.storage[1000] / block.contract_storage(D)[I]

    if ethervalue >= 5000 * 10^18:

    mktx(contract.storage[1003],5000 * 10^18,0,0)

    else if block.timestamp > contract.storage[1002]:

    mktx(contract.storage[1003],ethervalue,0,0)

    mktx(A,5000 - ethervalue,0,0)

    12 1 2

  • 12 4 12 29 1.95 12 11 12 28

    2.20 12 18 12 30 1.20 12 29

    3.20 1

    Namecoin

    DNS bitcoin.org

    bitcoin.bit IP

    if tx.value < 25 * 10^18:

    stop

    if contract.storage[tx.data[0]]:

    stop

    contract.storage[tx.data[0]] = contract.storage[tx.data[1]]

    DAO

    decentralized autonomous corporation

    67%

    67%

    DAO

    [0,k]

    [1,k,L,v0,v1...vn] k L v0, v1 ... vn

    [2,k]

    2^128

    2^255 1

    C C

    k = sha3(32,tx.data[1])

    if tx.data[0] == 0:

    if contract.storage[tx.sender] == 0:

    stop

    if contract.storage[k + tx.sender] == 0:

    contract.storage[k + tx.sender] = 1

    contract.storage[k] += 1

    else if tx.data[0] == 1:

    if tx.value

  • stop

    if contract.storage[k] > 0:

    stop

    i = 3

    while i < tx.datan:

    contract.storage[k + i] = tx.data[i]

    i = i + 1

    contract.storage[k] = 1

    contract.storage[k+1] = tx.datan

    contract.storage[k+2] = tx.data[2]

    else if tx.data[0] == 2:

    if contract.storage[k] >= contract.storage[2 ^ 255] * 2 / 3:

    if tx.value

  • 5 Frank Stajano Richard Clayton Cyberdice

    SatoshiDice

    6

    7

    8 Dropbox Merkle Merkle

    Merkle

    [

    nonce,

    '',

    value,

    [

    data item 0,

    data item 1,

    ...

    ],

    v,

    r,

    s

    ]

    1.

    2. +

    3. RLP sha3 20

    4. [0 ... n-1] I, i in

    [063] STOPEXTRO

    BALANCE

  • 0[02^256-1]

    0

    [02^256-1]

    1.

    2. STEPCOUNT = 0

    3.

    o STOP 63

    o MINERFEE = 0, VOIDFEE = 0

    o STEPCOUNT 16, MINERFEE

  • S[-1] S[-2]

    (0) STOP

    (1) ADD - S[-2] + S[-1] mod 2^256

    (2) MUL - S[-2] * S[-1] mod 2^256

    (3) SUB - S[-2] - S[-1] mod 2^256

    (4) DIV - floor(S[-2] / S[-1]) S[-1] = 0,

    (5) SDIV - floor(S[-2] / S[-1]) 2^255 ( x -> 2^256 - x) S[-1]

    = 0

    (6) MOD - S[-2] mod S[-1] S[-1] = 0

    (7) SMOD - S[-2] mod S[-1], 2^255 ( x -> 2^256 - x) S[-1] =

    0

    (8) EXP - S[-2] ^ S[-1] mod 2^256

    (9) NEG - 2^256 - S[-1]

    (10) LT - 1 S[-2] < S[-1] 0

    (11) LE - 1 S[-2] S[-1] 0

    (13) GE - 1 S[-2] >= S[-1] 0

    (14) EQ - 1 S[-2] == S[-1] 0

    (15) NOT - 1 S[-1] = 0 0

    (16) MYADDRESS -

    (17) TXSENDER -

    (18) TXVALUE -

  • (19) TXDATAN -

    (20) TXDATA - S[-1] 0

    (21) BLK_PREVHASH -

    (22) BLK_COINBASE - coinbase

    (23) BLK_TIMESTAMP -

    (24) BLK_NUMBER -

    (25) BLK_DIFFICULTY -

    (26) BASEFEE -

    (32) SHA256 - S[-2] (S[-2] + ceil(S[-1] / 32) - 1) mod 2^256 ceil(S[-1] /

    32)0 32 S[-1]

    SHA256

    (33) RIPEMD160 - SHA256 RIPEMD-160

    (34) ECMUL - (S[-2],S[-1]) secp256k1 P(0,0)

    (S[-2],S[-1]) * S[-3](2^256 - 1, 2^256 - 1) S[-3]

    (35) ECADD - (S[-4],S[-3]) + (S[-2],S[-1])(2^256 - 1,2^256 - 1)

    (36) ECSIGN - (v,r,s) (v,r,s) S[-1] Eletrum RFC6979

    S[-2] mod N

    (37) ECRECOVER - (x,y) S[-4](S[-3],S[-2],S[-1])

    v,r,s v [27,28]r [0,P]s [0,N](2^256 - 1,2