README ¶
bitcoin-ledger example
Demonstrates a bitcoin ledger in a blockchain using the unspent transaction output model.
Table of Contents,
- PREREQUISITES
- OVERVIEW
- THIS EXAMPLE
- BLOCKCHAIN AND PENDING BLOCK
- TRANSACTIONS IN LEDGER
- ADDING A TRANSACTION TO THE pendingBlock
- RUN
Documentation and reference,
- My cheat sheet on blockchains
- A blockchain with REST example I wrote that just stores data (no coins)
- I also wrote an entire cryptocurrency called jeffCoin
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,
TRANSACTIONS IN LEDGER
This illustrations shows the ledger in the blockchain and pendingBlock,
This illustration shows a visual look at how the transactions relate (input/output) to each other in the blockchain,
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()
- STEP 1 - VERIFY SIGNATURE
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 ¶
There is no documentation for this package.