1. 程式人生 > >從0到1簡易區塊鏈開發手冊V0.4-實現轉賬交易的思路分析

從0到1簡易區塊鏈開發手冊V0.4-實現轉賬交易的思路分析

序列 場景 tran color value lastb ali 區塊 創建

六.轉賬交易

創世區塊創建完畢之後,按照我們的正常思路,是繼續創建新的區塊,並加入至區塊鏈中,沒錯,這確實是學習路線,但是我們首先來了解一個區塊是如何生成的,轉賬交易 ===>打包交易 ===>工作量證明 ===>生成區塊

在上文,我們提到了錢包地址這個概念,我們一般可以簡單將錢包地址理解為一個銀行賬戶,那麽交易也就可以理解為是地址與地址之間的轉賬過程。

因為這部分內容非常重要,設置可以說交易就是比特幣原理的核心,所以,為了保證大家對概念有充分的了解,本章節的理論描述部分此處摘錄liuchengxu中關於對交易的翻譯。

1.概念

交易(transaction)是比特幣的核心所在,而區塊鏈唯一的目的,也正是為了能夠安全可靠地存儲交易。在區塊鏈中,交易一旦被創建,就沒有任何人能夠再去修改或是刪除它。今天,我們將會開始實現交易。不過,由於交易是很大的話題,我會把它分為兩部分來講:在今天這個部分,我們會實現交易的基本框架。在第二部分,我們會繼續討論它的一些細節。

由於比特幣采用的是 UTXO 模型,並非賬戶模型,並不直接存在“余額”這個概念,余額需要通過遍歷整個交易歷史得來。

關於UTXO模型,這在比特幣中也是非常重要的概念模型,務必熟練掌握。

點擊此處查看相關的交易信息

技術分享圖片

圖 交易記錄
技術分享圖片

圖 輸入腳本

技術分享圖片

關於轉賬交易涉及到的內容非常多,由於時間原因,目前可能無法做到非常全面的講解,姑且將自己梳理好能夠解釋清楚的地方分享出來,由於比特幣世界中的交易規則會更加復雜化,所以,希望大家能夠通過本章節的閱讀,在一定程度上對某些概念有一些初步或者稍微深刻的理解,那麽本章節的目的也就達到了,更深的分析筆者將會在後期的工作中根據實際的工作場景進行優化並做相關記錄。

2.結構體定義

其實再轉賬交易這個功能裏面,涉及了本文所有的結構體對象,由於區塊與區塊鏈對象等在上文已經有所提及,這裏先列出跟轉賬交易關系最為密切的一些結構體。
技術分享圖片

2.1 交易Transaction

type Transaction struct {
    //1.交易ID
    TxID []byte
    //2.輸入
    Vins []*TxInput
    //3.輸出
    Vouts []*TxOutput
}
  • TxID : 交易ID,一般通過對交易進行哈希後得到
  • Vins: 交易輸入數組
  • Vouts:交易輸出數組

2.2 交易輸入TxInput

type TxInput struct {
    //1.交易ID:
    TxID []byte
    //2.下標
    Vout int
    //3.數字簽名
    Signature []byte 
    //4.原始公鑰,錢包裏的公鑰
    PublicKey []byte 

}
  • TxID: 交易ID,表示該TxInput引用的TxOutput所在的交易ID
  • Vout:下標,表示該TxInput引用的TxOutput在交易中的位置
  • Signature:數字簽名,用於對引用的TxOutput交易的解鎖
  • PublicKey: 錢包的公鑰,原始公鑰

2.3 交易輸出TxOutput

type TxOutput struct {
    //金額
    Value int64  //金額
    //鎖定腳本,也叫輸出腳本,公鑰,目前先理解為用戶名,鑰花費這筆前,必須鑰先解鎖腳本
    //ScriptPubKey string
    PubKeyHash [] byte//公鑰哈希
}
  • Value : 金額,轉賬/找零金額

  • PubKeyHash:輸出腳本,此處為公鑰哈希,用於鎖定該筆交易輸出

2.4 未花費交易輸出UTXO

type UTXO struct {
    //1.該output所在的交易id
    TxID []byte
    //2.該output 的下標
    Index int
    //3.output
    Output *TxOutput
}

UTXO:Unspent Transaction output

  • TxID: 該TxOutput所在的交易id
  • Index:該TxOutput 的下標
  • Output:TxOutput對象

2.5 未花費交易輸出集合 UTXOSet

type UTXOSet struct {
    BlockChian *BlockChain
}
const utxosettable = "utxoset"

定義一個常量用於標識存入數據庫中的Bucket表名

3.轉賬交易流程

單筆轉賬

$ ./mybtc send -from 源地址 -to 目標地址 -amount 轉賬金額

多筆轉賬

$ ./mybtc send   -from ‘["源地址1","源地址2","源地址N"]‘   -to   ‘["目標地址1","目標地址2","目標地址3"]‘   -amount ‘["轉賬金額1","轉賬金額2","轉賬金額3"]‘

這部分內容理解起來有些難度,所以我做了一張圖,希望能夠幫助大家能夠理順思路,這樣在後面的學習以及代碼理解上面會稍微容易一些。

技術分享圖片

本圖介紹了從創世區塊後的三次轉賬過程,分別產生了三個區塊,為了讓讀者有更直觀的了解,我又將該圖做成了動態圖的方式供大家參考,通過該圖,希望大家能夠大致對轉賬交易有個印象。

技術分享圖片

動態圖演示了新區塊中的輸入交易引用的是哪個區塊中的交易輸出,從而實現了區塊鏈每次轉賬的金額都有據可依,也從另外一個角度展示了比特幣中UTXO的概念模型。

4.代碼分析

由於代碼量巨大,為了讓整個過程的理解更加流程,我改變前面幾篇文章的思路,從執行命令的代碼塊進行一步一步的代碼分析,希望能將自己的思路理順,從而可以更好得引導讀者朋友。

同樣,因為很多概念性的東西,我不準備在文章裏面啰嗦,如果感覺閱讀難度比較大,建議先仔細閱讀這篇文章

https://github.com/liuchengxu/blockchain-tutorial

然後再回頭來看我的這篇文章,會事半功倍

func (cli *CLI) Send(from, to, amount []string) {
    bc := GetBlockChainObject()
    if bc == nil {
        fmt.Println("沒有BlockChain,無法轉賬。。")
        os.Exit(1)
    }
    defer bc.DB.Close()

    bc.MineNewBlock(from, to, amount)
    utsoSet :=&UTXOSet{bc}
    utsoSet.Update()
}

4.1 獲取blockchain對象

func GetBlockChainObject() *BlockChain {
    /*
        1.數據庫存在,讀取數據庫,返回blockchain即可
        2.數據庫 不存在,返回nil
     */

    if dbExists() {
        //fmt.Println("數據庫已經存在。。。")
        //打開數據庫
        db, err := bolt.Open(DBName, 0600, nil)
        if err != nil {
            log.Panic(err)
        }

        var blockchain *BlockChain

        err = db.View(func(tx *bolt.Tx) error {
            //打開bucket,讀取l對應的最新的hash
            b := tx.Bucket([]byte(BlockBucketName))
            if b != nil {
                //讀取最新hash
                hash := b.Get([]byte("l"))
                blockchain = &BlockChain{db, hash}
            }
            return nil
        })
        if err != nil {
            log.Panic(err)
        }
        return blockchain
    } else {
        fmt.Println("數據庫不存在,無法獲取BlockChain對象。。。")
        return nil
    }
}

判斷存儲區塊的DB文件是否存在,如果存在,直接從數據庫Bucket中讀取"l"對應Hash值,將db對象與獲取到hash值賦值給需要返回的區塊鏈對象,如果DB文件不存在,說明創世區塊並未創建,沒有區塊鏈對象,直接退出程序。

4.2 生成區塊

獲取到區塊鏈對象之後,我們調用MineNewBlock方法進行區塊的創建

技術分享圖片

func (bc *BlockChain) MineNewBlock(from, to, amount []string) {
    /*
    1.新建交易
    2.新建區塊:
        讀取數據庫,獲取最後一塊block
    3.存入到數據庫中
     */

    //1.新建交易集合
    var txs [] *Transaction

    utxoSet := &UTXOSet{bc}

    for i := 0; i < len(from); i++ {
        //amount[0]-->int
        amountInt, _ := strconv.ParseInt(amount[i], 10, 64)
        tx := NewSimpleTransaction(from[i], to[i], amountInt, utxoSet, txs)
        txs = append(txs, tx)

    }
    /*
    分析:循環第一次:i=0
        txs[transaction1, ]
        循環第二次:i=1
        txs [transaction1, transaction2]
     */

    //交易的驗證:
    for _, tx := range txs {
        if bc.VerifityTransaction(tx, txs) == false {
            log.Panic("數字簽名驗證失敗。。。")
        }

    }

    /*
    獎勵:reward:
    創建一個CoinBase交易--->Tx
     */
    coinBaseTransaction := NewCoinBaseTransaction(from[0])
    txs = append(txs, coinBaseTransaction)

    //2.新建區塊
    newBlock := new(Block)
    err := bc.DB.View(func(tx *bolt.Tx) error {
        b := tx.Bucket([]byte(BlockBucketName))
        if b != nil {
            //讀取數據庫
            blockBytes := b.Get(bc.Tip)
            lastBlock := DeserializeBlock(blockBytes)

            newBlock = NewBlock(txs, lastBlock.Hash, lastBlock.Height+1)

        }
        return nil
    })
    if err != nil {
        log.Panic(err)
    }

    //3.存入到數據庫中
    err = bc.DB.Update(func(tx *bolt.Tx) error {
        b := tx.Bucket([]byte(BlockBucketName))
        if b != nil {
            //將新block存入到數據庫中
            b.Put(newBlock.Hash, newBlock.Serialize())
            //更新l
            b.Put([]byte("l"), newBlock.Hash)
            //tip
            bc.Tip = newBlock.Hash
        }

        return nil
    })
    if err != nil {
        log.Panic(err)
    }
}
4.2.1 新建交易

在以上的代碼中,涉及到幾個比較重要的方法,其中一個NewSimpleTransaction用於創建交易並打包,這裏對代碼進行了簡單梳理,由於內容實在太多,在文章末尾我會將github的源代碼地址貼出,供大家查看。

(1) 創建該交易的TxInput

技術分享圖片

(2) 創建該交易的TxOutput

技術分享圖片

(3) 創建交易

技術分享圖片

NewSimpleTransaction

func NewSimpleTransaction(from, to string, amount int64, utxoSet *UTXOSet, txs []*Transaction) *Transaction {
    //1.定義Input和Output的數組
    var txInputs []*TxInput
    var txOuputs [] *TxOutput

    //2.創建Input
    /*
    創世區塊中交易ID:c16d3ad93450cd532dcd7ef53d8f396e46b2e59aa853ad44c284314c7b9db1b4
     */

    //獲取本次轉賬要使用output
    //total, spentableUTXO := bc.FindSpentableUTXOs(from, amount, txs) //map[txID]-->[]int{index}
    total, spentableUTXO := utxoSet.FindSpentableUTXOs(from, amount, txs) //map[txID]-->[]int{index}

    //獲取錢包的集合:
    wallets := GetWallets()
    wallet := wallets.WalletMap[from]

    for txID, indexArray := range spentableUTXO {
        txIDBytes, _ := hex.DecodeString(txID)
        for _, index := range indexArray {
            txInput := &TxInput{txIDBytes, index, nil, wallet.PublickKey}
            txInputs = append(txInputs, txInput)
        }
    }

    //3.創建Output

    //轉賬
    txOutput := NewTxOutput(amount, to)
    txOuputs = append(txOuputs, txOutput)

    //找零
    //txOutput2 := &TxOutput{total - amount, from}
    txOutput2 := NewTxOutput(total-amount, from)
    txOuputs = append(txOuputs, txOutput2)

    //4.創建交易
    tx := &Transaction{[]byte{}, txInputs, txOuputs}

    //設置交易的ID
    tx.SetID()

    //設置簽名
    utxoSet.BlockChian.SignTransaction(tx,wallet.PrivateKey,txs)

    return tx

}

FindSpentableUTXOs

func (utxoSet *UTXOSet) FindSpentableUTXOs(from string, amount int64, txs []*Transaction) (int64, map[string][]int) {
    var total int64
    //用於存儲轉賬所使用utxo
    spentableUTXOMap := make(map[string][]int)
    //1.查詢未打包可以使用的utxo:txs
    unPackageSpentableUTXOs := utxoSet.FindUnpackeSpentableUTXO(from, txs)

    for _, utxo := range unPackageSpentableUTXOs {
        total += utxo.Output.Value
        txIDStr := hex.EncodeToString(utxo.TxID)
        spentableUTXOMap[txIDStr] = append(spentableUTXOMap[txIDStr], utxo.Index)
        if total >= amount {
            return total, spentableUTXOMap
        }
    }

    //2.查詢utxotable,查詢utxo
    //已經存儲的但是未花費的utxo
    err := utxoSet.BlockChian.DB.View(func(tx *bolt.Tx) error {
        //查詢utxotable中,未花費的utxo
        b := tx.Bucket([]byte(utxosettable))
        if b != nil {
            //查詢
            c := b.Cursor()
        dbLoop:
            for k, v := c.First(); k != nil; k, v = c.Next() {
                txOutputs := DeserializeTxOutputs(v)
                for _, utxo := range txOutputs.UTXOs {
                    if utxo.Output.UnlockWithAddress(from) {
                        total += utxo.Output.Value
                        txIDStr := hex.EncodeToString(utxo.TxID)
                        spentableUTXOMap[txIDStr] = append(spentableUTXOMap[txIDStr], utxo.Index)
                        if total >= amount {
                            break dbLoop
                            //return nil
                        }
                    }
                }

            }

        }

        return nil

    })
    if err != nil {
        log.Panic(err)
    }

    return total, spentableUTXOMap
}

FindUnpackeSpentableUTXO

func (utxoSet *UTXOSet) FindUnpackeSpentableUTXO(from string, txs []*Transaction) []*UTXO {
    //存儲可以使用的未花費utxo
    var unUTXOs []*UTXO

    //存儲已經花費的input
    spentedMap := make(map[string][]int)

    for i := len(txs) - 1; i >= 0; i-- {
        //func caculate(tx *Transaction, address string, spentTxOutputMap map[string][]int, unSpentUTXOs []*UTXO) []*UTXO {
        unUTXOs = caculate(txs[i], from, spentedMap, unUTXOs)
    }

    return unUTXOs
}

caculate

func caculate(tx *Transaction, address string, spentTxOutputMap map[string][]int, unSpentUTXOs []*UTXO) []*UTXO {
   //遍歷每個tx:txID,Vins,Vouts

   //遍歷所有的TxInput
   if !tx.IsCoinBaseTransaction() { //tx不是CoinBase交易,遍歷TxInput
      for _, txInput := range tx.Vins {
         //txInput-->TxInput
         full_payload := Base58Decode([]byte(address))

         pubKeyHash := full_payload[1 : len(full_payload)-addressCheckSumLen]

         if txInput.UnlockWithAddress(pubKeyHash) {
            //txInput的解鎖腳本(用戶名) 如果和要查詢的余額的用戶名相同,
            key := hex.EncodeToString(txInput.TxID)
            spentTxOutputMap[key] = append(spentTxOutputMap[key], txInput.Vout)
            /*
            map[key]-->value
            map[key] -->[]int
             */
         }
      }
   }

   //遍歷所有的TxOutput
outputs:
   for index, txOutput := range tx.Vouts { //index= 0,txoutput.鎖定腳本
      if txOutput.UnlockWithAddress(address) {
         if len(spentTxOutputMap) != 0 {
            var isSpentOutput bool //false
            //遍歷map
            for txID, indexArray := range spentTxOutputMap { //143d,[]int{1}
               //遍歷 記錄已經花費的下標的數組
               for _, i := range indexArray {
                  if i == index && hex.EncodeToString(tx.TxID) == txID {
                     isSpentOutput = true //標記當前的txOutput是已經花費
                     continue outputs
                  }
               }
            }

            if !isSpentOutput {
               //unSpentTxOutput = append(unSpentTxOutput, txOutput)
               //根據未花費的output,創建utxo對象--->數組
               utxo := &UTXO{tx.TxID, index, txOutput}
               unSpentUTXOs = append(unSpentUTXOs, utxo)
            }

         } else {
            //如果map長度為0,證明還沒有花費記錄,output無需判斷
            //unSpentTxOutput = append(unSpentTxOutput, txOutput)
            utxo := &UTXO{tx.TxID, index, txOutput}
            unSpentUTXOs = append(unSpentUTXOs, utxo)
         }
      }
   }
   return unSpentUTXOs

}
(4) 設置簽名

技術分享圖片

SignTransaction

func (bc *BlockChain) SignTransaction(tx *Transaction, privateKey ecdsa.PrivateKey, txs []*Transaction) {
   //1.判斷要簽名的tx,如果是coninbase交易直接返回
   if tx.IsCoinBaseTransaction() {
      return
   }

   //2.獲取該tx中的Input,引用之前的transaction中的未花費的output
   prevTxs := make(map[string]*Transaction)
   for _, input := range tx.Vins {
      txIDStr := hex.EncodeToString(input.TxID)
      prevTxs[txIDStr] = bc.FindTransactionByTxID(input.TxID, txs)
   }

   //3.簽名
   tx.Sign(privateKey, prevTxs)

}

Sign

func (tx *Transaction) Sign(privateKey ecdsa.PrivateKey, prevTxsmap map[string]*Transaction) {
    //1.判斷當前tx是否是coinbase交易
    if tx.IsCoinBaseTransaction() {
        return
    }

    //2.獲取input對應的output所在的tx,如果不存在,無法進行簽名
    for _, input := range tx.Vins {
        if prevTxsmap[hex.EncodeToString(input.TxID)] == nil {
            log.Panic("當前的Input,沒有找到對應的output所在的Transaction,無法簽名。。")
        }
    }

    //即將進行簽名:私鑰,要簽名的數據
    txCopy := tx.TrimmedCopy()

    for index, input := range txCopy.Vins {

        prevTx := prevTxsmap[hex.EncodeToString(input.TxID)]

        txCopy.Vins[index].Signature = nil    
        txCopy.Vins[index].PublicKey = prevTx.Vouts[input.Vout].PubKeyHash //設置input中的publickey為對應的output的公鑰哈希

        txCopy.TxID = txCopy.NewTxID()//產生要簽名的交易的TxID

        //為了方便下一個input,將數據再置為空
        txCopy.Vins[index].PublicKey = nil

        /*
        第一個參數
        第二個參數:私鑰
        第三個參數:要簽名的數據

        func Sign(rand io.Reader, priv *PrivateKey, hash []byte) (r, s *big.Int, err error)
        r + s--->sign
        input.Signatrue = sign
     */
        r,s,err:=ecdsa.Sign(rand.Reader, &privateKey, txCopy.TxID )
        if err != nil{
            log.Panic(err)
        }

        sign:=append(r.Bytes(),s.Bytes()...)
        tx.Vins[index].Signature = sign
    }

}

TrimmedCopy

func (tx *Transaction) TrimmedCopy() *Transaction {
    var inputs [] *TxInput
    var outputs [] *TxOutput
    for _, in := range tx.Vins {
        inputs = append(inputs, &TxInput{in.TxID, in.Vout, nil, nil})
    }

    for _, out := range tx.Vouts {
        outputs = append(outputs, &TxOutput{out.Value, out.PubKeyHash})
    }

    txCopy := &Transaction{tx.TxID, inputs, outputs}
    return txCopy

}
4.2.2 驗證交易
func (tx *Transaction) Verifity(prevTxs map[string]*Transaction)bool{
    //1.如果是coinbase交易,不需要驗證
    if tx.IsCoinBaseTransaction(){
        return true
    }

    //prevTxs
    for _,input:=range prevTxs{
        if prevTxs[hex.EncodeToString(input.TxID)] == nil{
            log.Panic("當前的input沒有找到對應的Transaction,無法驗證。。")
        }
    }

    //驗證
    txCopy:= tx.TrimmedCopy()

    curev:= elliptic.P256() //曲線

    for index,input:=range tx.Vins{
        //原理:再次獲取 要簽名的數據  + 公鑰哈希 + 簽名
        /*
        驗證簽名的有效性:
        第一個參數:公鑰
        第二個參數:簽名的數據
        第三、四個參數:簽名:r,s
        func Verify(pub *PublicKey, hash []byte, r, s *big.Int) bool
         */
        //ecdsa.Verify()

    //獲取要簽名的數據
        prevTx:=prevTxs[hex.EncodeToString(input.TxID)]

        txCopy.Vins[index].Signature = nil
        txCopy.Vins[index].PublicKey = prevTx.Vouts[input.Vout].PubKeyHash
        txCopy.TxID = txCopy.NewTxID() //要簽名的數據

        txCopy.Vins[index].PublicKey = nil

        //獲取公鑰
        /*
        type PublicKey struct {
            elliptic.Curve
            X, Y *big.Int
        }
         */

        x:=big.Int{}
        y:=big.Int{}
        keyLen:=len(input.PublicKey)
        x.SetBytes(input.PublicKey[:keyLen/2])
        y.SetBytes(input.PublicKey[keyLen/2:])

        rawPublicKey:=ecdsa.PublicKey{curev,&x,&y}

        //獲取簽名:

        r :=big.Int{}
        s :=big.Int{}

        signLen:=len(input.Signature)
        r.SetBytes(input.Signature[:signLen/2])
        s.SetBytes(input.Signature[signLen/2:])

        if ecdsa.Verify(&rawPublicKey,txCopy.TxID,&r,&s) == false{
            return false
        }

    }
    return true
}
4.2.3 創建CoinBase交易
func NewCoinBaseTransaction(address string) *Transaction {
    txInput := &TxInput{[]byte{}, -1, nil, nil}
    //txOutput := &TxOutput{10, address}
    txOutput := NewTxOutput(10, address)
    txCoinBaseTransaction := &Transaction{[]byte{}, []*TxInput{txInput}, []*TxOutput{txOutput}}
    //設置交易ID
    txCoinBaseTransaction.SetID()
    return txCoinBaseTransaction
}

在每個區塊中創建一個CoinBase交易作為獎勵機制。

4.2.4 新建區塊
func NewBlock(txs []*Transaction, prevBlockHash [] byte, height int64) *Block {
    //創建區塊
    block := &Block{height, prevBlockHash, txs, time.Now().Unix(), nil,0}
    //設置hash
    //block.SetHash()
    pow:=NewProofOfWork(block)
    hash,nonce:=pow.Run()
    block.Hash = hash
    block.Nonce = nonce

    return block
}
4.2.5 持久化存儲
    err = bc.DB.Update(func(tx *bolt.Tx) error {
        b := tx.Bucket([]byte(BlockBucketName))
        if b != nil {
            //將新block存入到數據庫中
            b.Put(newBlock.Hash, newBlock.Serialize())
            //更新l
            b.Put([]byte("l"), newBlock.Hash)
            //tip
            bc.Tip = newBlock.Hash
        }

        return nil
    })
    if err != nil {
        log.Panic(err)
    }

4.3 更新UTXO集

func (utxoSet *UTXOSet) Update() {
    /*
    表:key:txID
        value:TxOutputs
            UTXOs []UTXO
     */

    //1.獲取最後(從後超前遍歷)一個區塊,遍歷該區塊中的所有tx
    newBlock := utxoSet.BlockChian.Iterator().Next()
    //2.獲取所有的input
    inputs := [] *TxInput{}
    //遍歷交易,獲取所有的input
    for _, tx := range newBlock.Txs {
        if !tx.IsCoinBaseTransaction() {
            for _, in := range tx.Vins {
                inputs = append(inputs, in)
            }
        }
    }

    //存儲該區塊中的,tx中的未花費
    outsMap := make(map[string]*TxOutputs)

    //3.獲取所有的output
    for _, tx := range newBlock.Txs {
        utxos := []*UTXO{}
        //找出交易中的未花費
        for index, output := range tx.Vouts {
            isSpent := false
            //遍歷inputs的數組,比較是否有intput和該output對應,如果滿足,表示花費了
            for _, input := range inputs {
                if bytes.Compare(tx.TxID, input.TxID) == 0 && index == input.Vout {
                    if bytes.Compare(output.PubKeyHash, PubKeyHash(input.PublicKey)) == 0 {
                        isSpent = true
                    }
                }
            }
            if isSpent == false {
                //output未花
                utxo := &UTXO{tx.TxID, index, output}
                utxos = append(utxos, utxo)
            }
        }

        //utxos,
        if len(utxos) > 0 {
            txIDStr := hex.EncodeToString(tx.TxID)
            outsMap[txIDStr] = &TxOutputs{utxos}
        }

    }

    //刪除花費了數據,添加未花費
    err := utxoSet.BlockChian.DB.Update(func(tx *bolt.Tx) error {

        b := tx.Bucket([]byte(utxosettable))
        if b != nil {
            //遍歷inputs,刪除
            for _, input := range inputs {
                txOutputsBytes := b.Get(input.TxID)
                if len(txOutputsBytes) == 0 {
                    continue
                }

                //反序列化
                txOutputs := DeserializeTxOutputs(txOutputsBytes)
                //是否需要被刪除
                isNeedDelete := false

                //存儲該txoutout中未花費utxo
                utxos := []*UTXO{}

                for _, utxo := range txOutputs.UTXOs {
                    if bytes.Compare(utxo.Output.PubKeyHash, PubKeyHash(input.PublicKey)) == 0 && input.Vout == utxo.Index {
                        isNeedDelete = true
                    } else {
                        utxos = append(utxos, utxo)
                    }
                }

                if isNeedDelete == true {
                    b.Delete(input.TxID)
                    if len(utxos) > 0 {
                        txOutputs := &TxOutputs{utxos}
                        b.Put(input.TxID, txOutputs.Serialize())
                    }
                }
            }

            //遍歷map,添加
            for txIDStr, txOutputs := range outsMap {
                txID, _ := hex.DecodeString(txIDStr)
                b.Put(txID, txOutputs.Serialize())

            }

        }

        return nil
    })

    if err != nil {
        log.Panic(err)
    }
}
  • 刪除本次交易產生的input對應的utxo
  • 添加本次交易產生的新utxo

5.代碼共享

由於轉賬交易這一塊的內容代碼量特別大,腦圖跟交易流程圖我也是花費了大量的時間進行整理,但是要一項一項進行代碼分析,時間成本還是太大了,所以,將github的代碼共享給大家,可以照著文章思路與思維導圖中的路線進行適當的分析,https://github.com/DiaboFong/MyPublicChain

從0到1簡易區塊鏈開發手冊V0.4-實現轉賬交易的思路分析