ruby - 如何转换为Crystal ruby​​的Array的多个分配

标签 ruby types compiler-errors blockchain crystal-lang

我有一个很小的(以前) ruby 区块链脚本,我试图将其转换为Crystal,到目前为止看起来像这样:

#  build your own blockchain from scratch in crystal!
#
#  to run use:
#    $ crystal ./blockchain_with_proof_of_work.cr

require "openssl" # for hash checksum digest function SHA256

class Block
  getter index : Int32
  getter timestamp : Time
  getter data : String
  getter previous_hash : String
  getter nonce : Int32 # # proof of work if hash starts with leading zeros (00)
  getter hash : String

  def initialize(index, data, previous_hash)
    @index = index
    @timestamp = Time.now
    @data = data
    @previous_hash = previous_hash
    @nonce, @hash = compute_hash_with_proof_of_work
  end

  def compute_hash_with_proof_of_work(difficulty = "00")
    nonce = 0
    loop do
      hash = calc_hash_with_nonce(nonce)
      if hash.starts_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
    end
  end

  def calc_hash_with_nonce(nonce = 0)
    sha = OpenSSL::Digest.new("SHA256")
    sha.update(nonce.to_s + @index.to_s + @timestamp.to_s + @data + @previous_hash)
    sha.hexdigest
  end

  def self.first(data = "Genesis") # create genesis (big bang! first) block
    # # uses index zero (0) and arbitrary previous_hash ("0")
    Block.new(0, data, "0")
  end

  def self.next(previous, data = "Transaction Data...")
    Block.new(previous.index + 1, data, previous.hash)
  end
end # class Block

#####
# # let's get started
# #   build a blockchain a block at a time

b0 = 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]

puts blockchain

######
#  will print something like:
#
# [#<Block:0x1e204f0
#   @data="Genesis",
#   @hash="00b8e77e27378f9aa0afbcea3a2882bb62f6663771dee053364beb1887e18bcf",
#   @index=0,
#   @nonce=242,
#   @previous_hash="0",
#   @timestamp=2017-09-20 20:13:38 +0200>,
#  #<Block:0x1e56e20
#   @data="Transaction Data...",
#   @hash="00aae8d2e9387e13c71b33f8cd205d336ac250d2828011f5970062912985a9af",
#   @index=1,
#   @nonce=46,
#   @previous_hash=
#    "00b8e77e27378f9aa0afbcea3a2882bb62f6663771dee053364beb1887e18bcf",
#   @timestamp=2017-09-20 20:13:38 +0200>,
#  #<Block:0x1e2bd58
#   @data="Transaction Data......",
#   @hash="00ea45e0f4683c3bec4364f349ee2b6816be0c9fd95cfd5ffcc6ed572c62f190",
#   @index=2,
#   @nonce=350,
#   @previous_hash=
#    "00aae8d2e9387e13c71b33f8cd205d336ac250d2828011f5970062912985a9af",
#   @timestamp=2017-09-20 20:13:38 +0200>,
#  #<Block:0x1fa8338
#   @data="More Transaction Data...",
#   @hash="00436f0fca677652963e904ce4c624606a255946b921132d5b1f70f7d86c4ab8",
#   @index=3,
#   @nonce=59,
#   @previous_hash=
#    "00ea45e0f4683c3bec4364f349ee2b6816be0c9fd95cfd5ffcc6ed572c62f190",
#   @timestamp=2017-09-20 20:13:38 +0200>]

但是,当我运行它时,出现一个错误,指出:
Error in blockchain.cr/blockchain_with_proof_of_work.cr:57: instantiating 
'Block:Class#first(String)'

b0 = Block.first("Genesis")
           ^~~~~

in blockchain.cr/blockchain_with_proof_of_work.cr:45: instantiating 
'Block:Class#new(Int32, String, String)'

Block.new(0, data, "0")
      ^~~

in blockchain.cr/blockchain_with_proof_of_work.cr:22: instance variable 
'@nonce' of Block must be Int32, not (Int32 | String)

    @nonce, @hash = compute_hash_with_proof_of_work
    ^~~~~~

查看Crystal docs on multiple assignment,我不确定如何重构此方法,以确保它不会失败,而Crystal不会自动进行静态静态类型检查和类型推断?有问题的方法(返回两个类型的数组)似乎没有包含在文档中:
@nonce, @hash = compute_hash_with_proof_of_work # return [nonce, hash]

最佳答案

将数组分解为多个赋值时,Crystal无法推断每个元素的确切类型。因此,分配给实例变量@nonce的值可以是Int32String。您应该改用元组:return {nonce, hash}(第29行)。元组具有位置类型声明,并且由于没有在堆上分配内存,因此它比Array性能更高。

关于ruby - 如何转换为Crystal ruby​​的Array的多个分配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48299395/

相关文章:

ruby - 对 ruby​​ CONSTANT 的困惑

javascript - typescript :有没有办法检查变量是否是具有嵌套属性的接口(interface)定义对象?

c++ - 如何摆脱这个链接错误?

java - eclipse xtext : How can I know if file contain errors

ruby - 用 ruby​​ 进行时间序列重采样

ruby-on-rails - 无法在 Rails 中显示相关记录

ruby - 合并 Ruby 脚本

types - 如何将 Elixir 元组转换为位串?

swift - 具有通用类型的协议(protocol)函数

c++ - 没有规则使目标 *.so