In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article focuses on "how to use ruby code to achieve block chain", interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "how to use ruby code to achieve block chain"!
Block chain = a linked list of blocks?
Blockchain.ruby:
Class Block attr_reader: timestamp attr_reader: data attr_reader: previous_hash attr_reader: hash def initialize (data, previous_hash) @ timestamp = Time.now @ data= data @ previous_hash = previous_hash @ hash = calc_hash end def self.first (data= "Genesis") # create genesis (big bang! First) block # # note: uses all zero for previous_hash ("0") Block.new (data, "000000000000000000000000000000000000000000000000000000000000000000") end def self.next (previous, data= "Transaction Data...") Block.new (data Previous.hash) endprivate def calc_hash sha = Digest::SHA256.new sha.update (@ timestamp.to_s + @ previous_hash + @ data) sha.hexdigest endend # class Block# let's get started## build a blockchain a block ata timeb0 = Block.first ("Genesis") b1 = Block.next (b0, "Transaction Data...") b2 = Block.next (b1, "Transaction Data.") b3 = Block.next (b2 "More Transaction Data...") blockchain = [b0, b1, b2, b3] pp blockchain
Execute the above procedure:
~ $ruby blockchain.rb
A result similar to the following will be output:
[#, #]
Wait a minute, is the block chain a linked list?
Of course not. The purpose of using the linked list is to get a reference to the previous block: in the block chain, each block must have an identifier, and this identifier must also depend on the identifier of the previous block. this means that if you want to replace a block in the block chain, you must recalculate the identifiers of all subsequent blocks. In the above implementation, you can see that when we call the calc_hash method to calculate the identifier of the block, we need to pass in the signature of the previous block.
What about the workload proof algorithm?
Now let's add the implementation of the workload proof algorithm. In a classic chunk chain, you have to calculate a hash that starts at 00 as the identifier of the block. the more zeros of the prefix, the more computation, and the more difficult it is. For simplicity, let's set the difficulty to two prefixes of 0, that is, 2 ^ 16 = 256 possibilities.
Blockchain_with_proof_of_work.rb:
Def compute_hash_with_proof_of_work (difficulty= "00") nonce = 0 loop do hash = calc_hash_with_nonce (nonce) if hash.start_with? (difficulty) return [nonce,hash] # # bingo! Proof of work if hash starts with leading zeros (00) else nonce + = 1 # # keep trying (and trying and trying) end endenddef calc_hash_with_nonce (nonce=0) sha = Digest::SHA256.new sha.update (nonce.to_s + @ timestamp.to_s + @ previous_hash + @ data) sha.hexdigestend
Now let's run the blockchain program with the addition of the POW mechanism:
~ $ruby blockchain_with_proof_of_work.rb
The output is as follows:
[#, #] at this point, I believe you have a deeper understanding of "how to implement block chain with ruby code". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.