Building a Blockchain with Go - Part 7 - The UTXO Set and BadgerDB Iterators

in #utopian-io6 years ago

golang.jpg

Repository

https://github.com/golang/go

What Will I Learn?

  • You will learn about the UTXO set
  • You will learn how to iterate over BadgerDB keys
  • You will learn how to add perfixes to BagerDB
  • You will learn about Blockchain optimization
  • You will learn how to add a chainstate persistence layer
  • You will learn how to batch delete keys from BadgerDB

Requirements

System Requirements:

Operating System:

  • FreeBSD 10.3 or later
  • Linux 2.6.23 or later with glibc
  • macOS 10.10 or later
  • Windows 7, Server 2008R2 or later

Required Knowledge

  • A little understanding of the Go programming language
  • Go installed on your computer
  • A text editor or IDE like Gogland (VS Code used)

Resources for Go and this Project:

Credits/Sources:

Difficulty

  • Advanced

Description

Blockchains are by definition immutable and distributed databases. As we move closer and closer towards a production level application, we need to consider how we can avoid some of the pitfalls that could potentially happen from a lack of scaling. While our testing environment has only really dealt with four to five blocks at once; each with their own transaction sets, a real blockchain can scale out to hundreds of thousands of blocks which takes up a significant portion of disk space. Before we add the peer to peer networking layer to our application, we need to ensure that each peer doesn't have to hold and read 100-200 GBs worth of data just to verify a transaction on the blockchain.

UTXO Persistance

As we've seen in these tutorials, most of the transactions in a blockchain rely on what are called Unspent Transaction Outputs. If you want to know the balance of an account, you count up the tokens inside of the set of unspent outputs which are locked by the user's public hash key. If you want to send tokens from one account to another, you also need to use this set of unspent outputs to create that transaction. The unspent transaction output set is important and it makes sense to separate this layer of data from the blockchain itself. Not all users want to run a full node to access a blockchain. Even full nodes should not have to read the entire blockchain to make a new transaction or find an account's balance. This becomes even more important as the blockchain scales out to hundreds of thousands of blocks.

utxo.png

The flowchart above shows a three block blockchain. In this example, there are three unspent transaction outputs and eight total transactions in all three blocks combined. The blockchain persistence layer stores all of the transactions and each block that contains those transactions. The UTXO set on the other hand only stores the Unspent Transaction Outputs to minimize the cost of having to create new transactions. In the case of Disk space and Performance, using the UTXO for most transactions is better and is no less secure then using the blockchain itself. This persistence layer is called the chainstate persistence layer and it is one of the major ways of optimizing and shrinking the size of a blockchain node.

Differentiating Data in BadgerDB

This UTXO set needs to be stored in the database along with our blockchain. With BadgerDB being a low level Key-Value storage solution, the main way of doing this is by using prefixes. Badger does not contain any tables nor does it have buckets but we are able to append prefixes to the keys of like data to properly work with and modify that data in bulk. It might seem intuitive to create a separate database for the chainstate apart from the blockchain database, but this would require adding a second instance of the BadgerDB database client to the application. Doing this is possible but it removes any guarantees that the application will not corrupt the data in the database.

badgerdb-delete.png

In our code, we built a DeleteByPrefix helper function. The primary function of this method is to iterate through our database and delete keys that are associated with a specific prefix by bulk. The function which is featured in the code above, opens a read-only transaction with the BadgerDB database. It then iterates through the database without fetching the values associated with the database keys. Once its collected up to one hundred thousand keys, it executes a bulk delete command to remove both the keys and values from the database. If there are more than one hundred thousand keys in the database, then the command deletes the keys in iterations of one hundred thousand at a time. This is a much more efficient process then deleting each key individually as they are being read. This function is used to reindex the UTXO set in our database as it is created and as it grows.

The Source Code for this video may be found here: https://github.com/tensor-programming/golang-blockchain/tree/part_7

Video Tutorial

Curriculum

Proof of Work Done

https://github.com/tensor-programming

Sort:  

Hi @tensor

Thank you for another great tutorial on building a blockchain with Go. This is already Part 7 of the series.

I can see that your instruction is very detailed.

Learners should be able to follow all the steps that you show in your flow chart. There are so many concepts that need to learn.

Your contribution has been evaluated according to Utopian policies and guidelines, as well as a predefined set of questions pertaining to the category.

To view those questions and the relevant answers related to your post, click here.


Need help? Write a ticket on https://support.utopian.io/.
Chat with us on Discord.
[utopian-moderator]

As always, thank you for moderating my content.

Thank you for your review, @rosatravels! Keep up the good work!

Hi, @tensor!

You just got a 7.41% upvote from SteemPlus!
To get higher upvotes, earn more SteemPlus Points (SPP). On your Steemit wallet, check your SPP balance and click on "How to earn SPP?" to find out all the ways to earn.
If you're not using SteemPlus yet, please check our last posts in here to see the many ways in which SteemPlus can improve your Steem experience on Steemit and Busy.





This post has been voted on by the SteemSTEM curation team and voting trail in collaboration with @curie.

If you appreciate the work we are doing then consider voting both projects for witness by selecting stem.witness and curie!

For additional information please join us on the SteemSTEM discord and to get to know the rest of the community!

Hi @tensor!

Your post was upvoted by @steem-ua, new Steem dApp, using UserAuthority for algorithmic post curation!
Your post is eligible for our upvote, thanks to our collaboration with @utopian-io!
Feel free to join our @steem-ua Discord server

Hey, @tensor!

Thanks for contributing on Utopian.
We’re already looking forward to your next contribution!

Get higher incentives and support Utopian.io!
Simply set @utopian.pay as a 5% (or higher) payout beneficiary on your contribution post (via SteemPlus or Steeditor).

Want to chat? Join us on Discord https://discord.gg/h52nFrV.

Vote for Utopian Witness!

Coin Marketplace

STEEM 0.18
TRX 0.13
JST 0.028
BTC 58080.30
ETH 3102.16
USDT 1.00
SBD 2.40