bitcoin-ledger

command
v0.0.0-...-a2a1f02 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 1, 2020 License: MIT Imports: 6 Imported by: 0

README

bitcoin-ledger example

Demonstrates a bitcoin ledger in a blockchain using the unspent transaction output model.

Table of Contents,

Documentation and reference,

GitHub Webpage

PREREQUISITES

go get -u -v github.com/sirupsen/logrus

OVERVIEW

An unspent transaction output model is a method to log the transactions of coins/value in a cryptocurrency. This type of bookkeeping does not keep a running balance. Each transaction records where the coin/value is coming from (input) and where it is going to (output).

All inputs must be tied to other outputs, but outputs can be "unspent", meaning not connected to anything. When you add up all the unspent outputs for an address you have the balance.

It's a little tricky at first but makes sense if you code it.

THIS EXAMPLE

To keep this example really simple, blocks will only contain an ID and transactions.

type blockStruct struct {
    BlockID      int64               `json:"blockID"`
    Transactions []transactionStruct `json:"transactions"`
}

Where a transaction is just a slice of inputs and outputs,

type transactionStruct struct {
    TxID    int64           `json:"txID"`
    Inputs  []inputsStruct  `json:"inputs"`
    Outputs []outputsStruct `json:"outputs"`
}

The first block (genesis) in the blockchain will contain 1 transaction. That will be the founders initial value of 100,000,000 (or 100,000 jeffCoins). Each jeffCoin has a value of 1,000 addies. Meow.

  • BlockID 0 Genesis Block
    • TxID 0 Founders start with 100,000 jeffCoins

There are 7 transaction requests thereafter (One is bad). I will be adding blocks as follows,

  • BlockID 1
    • TxID 1 Founders sends Jeff 80 jeffCoins
    • Founders send someone 80 jeffCoins (Rejected Signature Failed)
  • BlockID 2
    • TxID 2 Jeff sends Matt 50 jeffCoins & CoinVault .5 jeffCoin
  • BlockID 3
    • TxID 3 Founders sends Matt 250 jeffCoins & Jeff 13 jeffCoins
    • TxID 4 Matt sends Jill 35 jeffCoins

Then all other transactions will be pending,

  • pendingBlock
    • TxID 5 Matt sends Jeff 15 jeffCoins
    • TxID 6 Jeff sends Jill 33 jeffCoins

The pendingBlock is pending, meaning it needs to be verified by other nodes. So this block will not have any value until its part of the blockchain.

BLOCKCHAIN AND PENDING BLOCK

After the run the blockchain and pendingBlock should look like blockchain-output.txt.

This illustration may help,

IMAGE - pendingBlock-and-blockchain-flow - IMAGE

TRANSACTIONS IN LEDGER

This illustrations shows the ledger in the blockchain and pendingBlock,

IMAGE - bitcoin-transactions-inputs-and-outputs - IMAGE

This illustration shows a visual look at how the transactions relate (input/output) to each other in the blockchain,

IMAGE - bitcoin-transaction-flow-from-block-to-block - IMAGE

ADDING A TRANSACTION TO THE pendingBlock

A transaction request message is just a source address requesting to transfer value/coins to one ore more destination addresses.

This is easily seen using the following struct,

type txRequestMessageStruct struct {
    SourceAddress string              `json:"sourceAddress"`
    Destinations  []destinationStruct `json:"destinations"`
}

type destinationStruct struct {
    DestinationAddress string `json:"destinationAddress"`
    Value              int64  `json:"value"`
}

Then it is signed for verification,

type txRequestMessageSignedStruct struct {
    TxRequestMessage txRequestMessageStruct `json:"txRequestMessage"`
    Signature        string                 `json:"signature"`
}

Below are the functions/methods for processing the transaction request message. The ultimate goal is to load the transaction onto the pendingBlock which will be eventually appended onto the blockchain.

I broke it down into 5 steps (verify signature is just a mockup for simplicity),

  • processTransactionRequest()
    • STEP 1 - VERIFY SIGNATURE
      • verifySignature()
    • STEP 2 - GET BALANCE AND A LIST OF UNSPENT OUTPUTS
      • getBalance()
    • STEP 3 - CHECK IF YOU HAVE ENOUGH jeffCoins
    • STEP 4 - PICK THE UNSPENT OUTPUTS TO USE AND PROVIDE CHANGE
      • pickUnspentOutputs()
    • STEP 5 - ADD TRANSACTION to pendingBlock and MAKE CHANGE
      • addTransactionToPendingBlock()

RUN

This will process a transaction, display the blockchain and show the balances for each address (public Keys),

go run control.go bitcoin-ledger.go data.go

The blockchain and pendingBlock should look like blockchain-output.txt.

The balances in the blockchain should be,

The balance for Founders PubKey (Address) is 99657000
The balance for Jeffs PubKey (Address) is 42500
The balance for Matts PubKey (Address) is 265000
The balance for Jills PubKey (Address) is 35000
The balance for CoinVaults PubKey (Address) is 500

Remember, the pendingBlock is pending so it is not part of this calculation.

Documentation

The Go Gopher

There is no documentation for this package.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL