Mastering Blockchain in no time part 2 — implementation PlatoBlockchain Data Intelligence. Vertical Search. Ai.

Mastering Blockchain in no time part 2 — implementation

Purbaryandi

In this series of articles, we’ll build a simplified cryptocurrency that’s based on simple blockchain implementation in Golang in less than 5 minutes. you have to install golang first to make this project. for text editor you can use anything, here I use visual studio code.

Mastering Blockchain in no time part 2 — implementation PlatoBlockchain Data Intelligence. Vertical Search. Ai.
source : bitdegree

Did you install it? Alright, let’s do it!

if you have created the project create a file main.go . oke lets code, first we will start from main.go Let’s start with the struct Block part of “blockchain”. In the blockchain, it blocks that store valuable information. For example, bitcoin blocks store transactions, the essence of any cryptocurrency. Besides this, a block contains some technical information, like its version, current timestamp, and the hash of the previous block. In this article we’re not going to implement the block as it’s described in blockchain or Bitcoin specifications, instead, we’ll use a simplified version of it, which contains only significant information. Here’s what it looks like. here we have a struct block:

type Block struct {

Timestamp is the current timestamp (when the block is created), transactions is the actual valuable information contained in the block, prevhas stores the hash of the previous block, and Hash is the hash of the block. In Bitcoin specification Timestamp, prevhash, and Hash are block headers, which form a separate data structure, and transactions (transaction in our case) is a separate data structure. So we’re mixing them here for simplicity.

So how do we calculate the hashes? The way hashes are calculated is a very important feature of blockchain, and it’s this feature that makes blockchain secure. The thing is that calculating a hash is a computationally difficult operation, it takes some time even on fast computers (that’s why people buy powerful GPUs to mine Bitcoin). This is an intentional architectural design, which makes adding new blocks difficult, thus preventing their modification after they’re added. We’ll discuss and implement this mechanism in a future article.

For now, we’ll just take block fields, concatenate them, and calculate an SHA-256 hash on the concatenated combination. Let’s do this in Newhash method:

func NewHash(time time.Time, transactions []string, prevhash []byte) []byte {

Next, following a Golang convention, we’ll implement a function that’ll simplify the creation of a block and finish:

func Blocks(transactions []string, prevhash []byte) *Block {

we continue to create a print function. the print function is useful for printing the contents of each block that performs a transaction. here is the code:

func Print(block *Block) {

And function transaction for print transaction :

func Transaction(block *Block) {

after all the required functions have been completed then we will use it in the main function :

func main() {

okay all the code we have finished writing. let’s run it by typing go run main.go in terminal. Output :

Conclusion

We built a very simple blockchain prototype: it’s just an array of blocks, with each block having a connection to the previous one. The actual blockchain is much more complex though. In our blockchain adding new blocks is easy and fast, but in real blockchain adding new blocks requires some work: one has to perform some heavy computations before getting permission to add blocks (this mechanism is called Proof-of-Work). Also, blockchain is a distributed database that has no single decision-maker. Thus, a new block must be confirmed and approved by other participants of the network (this mechanism is called consensus). And there’re no transactions in our blockchain yet!

Source: https://medium.com/@purbaryandi/mastering-blockchain-in-no-time-part-2-implementation-433dc7967fe8?source=rss——cryptocurrency-5

Time Stamp:

More from Medium