1. 程式人生 > >5.3 以太坊原始碼詳解3

5.3 以太坊原始碼詳解3

一、轉賬的概念和交易的基本流程 使用者輸入轉入的地址和金額 系統用轉賬地址的私鑰對交易進行簽名(確保這筆交易是由發起交易的所有人) 對交易進行驗證 存入交易快取池 廣播交易 二、交易的資料

type Transaction struct {
    data txdata // 交易資料
    // caches
    hash atomic.Value // 交易雜湊
    size atomic.Value // 交易大小
    from atomic.Value // 交易來源
}

type txdata struct {
    AccountNonce    uint64 //傳送者發起的交易的總數量
    Price, GasLimit *big.Int // gas價格與上線
    Recipient       *common.Address `rlp:"nil"` // nil means contract creation // 接收者的地址,如果該地址為空,代表其是一個合約的建立者
    Amount          *big.Int // 此次交易所轉的以太幣的數量
    Payload         []byte // 其他資料
    V               byte     // signature // 交易簽名資料
    R, S            *big.Int // signature 交易簽名資料
}

三、雜湊

type (
    Hash    [hashLength]byte // 雜湊值
    Address [addressLength]byte // 地址
)
func BytesToHash(b []byte) Hash {
    var h Hash
    h.SetBytes(b)
    return h
}

四、區塊資料結構

type Block struct {
    header       *Header // 區塊頭
    uncles       []*Header // 叔區塊
    transactions Transactions // 交易
    receipts     Receipts // 接收者
    // caches
    hash atomic.Value // 雜湊
    size atomic.Value // 區塊大小
    // Td is used by package core to store the total difficulty
    // of the chain up to and including the block.
    Td *big.Int // 總的難度值
    // ReceivedAt is used by package eth to track block propagation time.
    ReceivedAt time.Time // 接收時間
}

 五、區塊頭資料結構

type Header struct {
    ParentHash  common.Hash    // Hash to the previous block 上一個區塊雜湊
    UncleHash   common.Hash    // Uncles of this block 叔區塊的雜湊
    Coinbase    common.Address // The coin base address 礦工接收獎勵的地址
    Root        common.Hash    // Block Trie state “State DB”的“state tired”的RLP的根節點的雜湊值
    TxHash      common.Hash    // Tx sha “state db”中“state tired”的RLP根節點雜湊值
    ReceiptHash common.Hash    // Receipt sha “receipt tire”的RLP的根節點的雜湊值
    Bloom       Bloom          // Bloom 布隆過濾器
    Difficulty  *big.Int       // Difficulty for the current block 區塊難度
    Number      *big.Int       // The block number 區塊號
    GasLimit    *big.Int       // Gas limit 理論上的gas上限
    GasUsed     *big.Int       // Gas used 區塊內所有交易執行所產生的gas的總和
    Time        uint64         // Creation time 區塊建立時間
    Extra       []byte         // Extra data
    MixDigest   common.Hash    // for quick difficulty verification
    Nonce       BlockNonce // 隨機數
}

 六、建立新區塊

// NewBlock creates a new block. The input data is copied,
// changes to header and to the field values will not affect the
// block.
//
// The values of TxHash, UncleHash, ReceiptHash and Bloom in header
// are ignored and set to values derived from the given txs, uncles
// and receipts.
func NewBlock(header *Header, txs []*Transaction, uncles []*Header, receipts []*Receipt) *Block {
    b := &Block{header: copyHeader(header), Td: new(big.Int)}
    // TODO: panic if len(txs) != len(receipts)
    if len(txs) == 0 {
        b.header.TxHash = emptyRootHash
    } else {
        b.header.TxHash = DeriveSha(Transactions(txs))
        b.transactions = make(Transactions, len(txs))
        copy(b.transactions, txs)
    }
    if len(receipts) == 0 {
        b.header.ReceiptHash = emptyRootHash
    } else {
        b.header.ReceiptHash = DeriveSha(Receipts(receipts))
        b.header.Bloom = CreateBloom(receipts)
        b.receipts = make([]*Receipt, len(receipts))
        copy(b.receipts, receipts)
    }
    if len(uncles) == 0 {
        b.header.UncleHash = emptyUncleHash
    } else {
        b.header.UncleHash = CalcUncleHash(uncles)
        b.uncles = make([]*Header, len(uncles))
        for i := range uncles {
            b.uncles[i] = copyHeader(uncles[i])
        }
    }
    return b
}