Blockchain is one of the most revolutionary technologies in the 21st century. It is constantly maturing, and its many potentials are gradually being realized. Essentially, the blockchain is just a distributed database. The reason why the blockchain is unique is that it is not a private database, but a public database, that is, everyone who uses it owns all or at least part of this database. Any new data record can only be added to the database after the majority consent of the majority of the database holders (maintainers). Because of this, the blockchain makes cryptocurrency and smart contracts possible.
In this series of articles, we will create a simplified version of the cryptocurrency, which will be implemented based on a simplified version of the blockchain.
Block
Let's start with the block first. In the blockchain, value information is stored in blocks. For example, Bitcoin blocks store transaction records, and transaction records are the core of any cryptocurrency. In addition, the block also contains technical information, such as its version number, current timestamp, and the hash of the previous block (Hash).
In this article, what we are implementing is not a complete blockchain like Bitcoin, but a simplified version of the blockchain, which only contains the most basic core information. It's almost like this:
type Block struct {
Timestamp int64
Data []byte
PrevBlockHash []byte
Hash [] byte}
TImestamp is the current timestamp (that is, the time when the block was created), Data is the value information contained in the block, PrevBlockHash stores the hash of the previous block, and Hash stores the hash of the current block . In the standard configuration of Bitcoin, TImestamp, PrevBlockHash, and Hash are block headers, which constitute a separate data structure; and transaction records (TransacTIons, in our version are Data), are another Separate data structure. Here we have mixed the data structure together for simplicity.
So how do we calculate the hash? The way to calculate the hash is one of the important features of the blockchain, and it is this feature that makes the blockchain so secure. The key point is that calculating hash is a difficult task to calculate. It takes time, even on fast computers (this is why people buy GPUs or even specialized ASIC chips that are more powerful than CPUs for mining. ). This is deliberately designed this way. The result of this is that it is difficult to add new blocks (data) to the blockchain (database) to ensure that once new data is added, it is difficult to tamper with in the future. This mechanism will be further discussed and implemented in future articles.
Now, we only need to associate the fields in the block, and calculate a SHA-256 hash on this basis. Let's call the SetHash method:
func (b *Block) SetHash() {
TImestamp:= [] byte(strconv.FormatInt(b.Timestamp, 10))
headers := bytes.Join([][]byte{b.PrevBlockHash, b.Data, timestamp}, []byte{})
hash := sha256.Sum256 (headers)
b.Hash = hashï¼»:ï¼½
}
Next, according to the usual Golang way, we will implement a function to make it easier to create blocks:
func NewBlock(data string, prevBlockHash [] byte) *Block {
block := &Block{time.Now().Unix(), []byte(data), prevBlockHash, []byte{}}
block.SetHash() return block
}
It's that simple.
Blockchain
Now, let's implement the blockchain. Essentially, the blockchain is just a database with a specific structure, it is an ordered, back-linked list (back-linked list). This means that the blocks are arranged in the order of insertion, and each block is linked to the previous block. With this structure, users can quickly obtain the latest block in the blockchain, and can also efficiently obtain a block through the hash of the block.
In Golang, this structure can be implemented with Array and Map: Array is used to maintain ordered hash (in Go language, array is ordered); Map is used to maintain hash → block right. However, in our blockchain prototype, we only need an array, because we don't need to get the block through hash for the time being.
type Blockchain struct {
blocks []*Block
}
This is our first blockchain! I never thought it would be so simple!
Now, we need to find a way to add blocks to the blockchain:
func (bc *Blockchain) AddBlock(data string) {
prevBlock := bc.blocks[len(bc.blocks)-1]
newBlock := NewBlock(data, prevBlock.Hash)
bc.blocks = append(bc.blocks, newBlock)
}
Is this done? or……?
In order to add a new block, we need an existing block, but now there is no block in our blockchain! Therefore, in any blockchain, there should be at least one block, and this first block is called the "Genesis Block". Let's implement a method to create this "founding block":
func NewGenesisBlock() *Block {return NewBlock("Genesis Block", ï¼»]byte{})
}
Now we can create a function to create a blockchain that already contains the "founding block":
func NewBlockchain() *Blockchain {return &Blockchain{ï¼»]*Block{NewGenesisBlock()}}
}
Let's see if this blockchain can be used?
func main() {
bc:= NewBlockchain()
bc.AddBlock("Send 1 BTC to Ivan")
bc.AddBlock("Send 2 more BTC to Ivan") for _, block := range bc.blocks {
fmt.Printf("Prev. hash: %x", block.PrevBlockHash)
fmt.Printf("Data: %s", block.Data)
fmt.Printf("Hash: %x", block.Hash)
fmt.Println()
}
}
The output is:
Prev. hash:
Data: Genesis Block
Hash: aff955a50dc6cd2abfe81b8849eab15f99ed1dc333d38487024223b5fe0f1168
Prev. hash: aff955a50dc6cd2abfe81b8849eab15f99ed1dc333d38487024223b5fe0f1168
Data: Send 1 BTC to Ivan
Hash: d75ce22a840abb9b4e8fc3b60767c4ba3f46a0432d3ea15b71aef9fde6a314e1
Prev. hash: d75ce22a840abb9b4e8fc3b60767c4ba3f46a0432d3ea15b71aef9fde6a314e1
Data: Send 2 more BTC to Ivan
Hash: 561237522bb7fcfbccbc6fe0e98bbbde7427ffe01c6fb223f7562288ca2295d1
(Unexpectedly) Finished!
in conclusion
We created a minimalist blockchain prototype: it is just an array of blocks, and each zone quick link points to the previous block. Of course, the real blockchain is much more complicated than this. In our blockchain, adding a new block is very fast and very easy; but adding a new block in the real blockchain requires more work: it is a lot of work before getting permission to add blocks (This process is called "Proof-of-Work", POW). Moreover, the blockchain is a distributed database without sovereignty. Therefore, before any new block is added, it must be confirmed and allowed by other participants in the network (this mechanism is called "consensus mechanism", "Consensus")... Also, in our blockchain , There is no transaction record yet!
Keystone Jack,cat6 keystone,cat6 jack,cat6 wall jack
NINGBO UONICORE ELECTRONICS CO., LTD , https://www.uniconmelectronics.com