我是一名拥有C ++和Python背景的程序员,他最近偶然发现了Julia,我非常喜欢它所提供的功能。为了更加熟悉区块链实现和Julia同时,我有点雄心勃勃,并试图通过转换{{3}发布的Python实现来在Julia中创建区块链的基本实现(作者解释了每种方法应该比我做得更好)。
但是,我在创建实际的Blockchain
结构时遇到了问题。为了创建创世块,Hackernoon建议我在构造函数中调用成员函数new_block
。到目前为止,我还没有能够弄清楚如何在朱莉娅中最好地复制这个。这就是我到目前为止所拥有的:
import JSON
import SHA
mutable struct Blockchain
chain::Array{Dict{String, Any}}
current_transactions::Array{String}
block::Dict{String, Any}
new_block::Function
function Blockchain(chain::Array{Dict{String, Any}}, current::Array{String})
new(chain, current, new_block(previous_hash=1, proof=100))
# ^issue calling function within the constructor here
end
end
当我尝试运行我的代码时,出现以下错误:
invalid redefinition of constant Blockchain
。
这里是new_block
功能:
function new_block(proof::String, previous_hash::String=nothing)
block = Dict(
"index" => length(chain) + 1,
"timestamp" => time(),
"transactions" => current_transactions,
"proof" => proof,
"previous_hash" => previous_hash | hash(chain[end]),
)
current_transactions = []
append!(chain, block)
return block
end
以下是我目前拥有的其他功能:
function new_transaction(this::Blockchain, sender::String, recipient::String, amount::Int)
transaction = Dict(
"sender"=> sender,
"recipient"=> recipient,
"amount"=> amount,
)
append!(this.current_transactions, transaction)
return length(this.chain) + 1
end
function hash(block::Blockchain)
block_string = JSON.dumps(block, sort_keys=true).encode()
return sha256(block_string).hexdigest()
end
我可能对Julia中的类型/结构如何工作有一些误解;我的大部分信息都是从第三方网站获得的,还有官方文档。以下是我一直依赖的一些资料来源:
更聪明/更有效的方式来尝试完成我将会非常感激。
根据给定的建议,以下是我所做的一些更改:
struct Blockchain
chain::Array{Dict{String, Any}}
current_transactions::Array{String}
function Blockchain(chain::Array{Dict{String, Any}}, current::Array{String})
new(chain, current)
end
end
function new_block!(this::Blockchain, proof::Int, previous_hash::Int=nothing)
block = Dict(
"index" => length(this.chain) + 1,
"timestamp" => time(),
"transactions" => this.current_transactions,
"proof" => proof,
)
if previous_hash == nothing
block["previous_hash"] = hash(this.chain[end])
else
block["previous_hash"] = previous_hash
end
this.current_transactions = []
append!(this.chain, block)
return this
end
我意识到block
属性没用,因为它只存在于chain
,所以我删除了它。
此外,这里是一个没有内部构造函数的替代Blockchain
定义:
struct Blockchain
chain::Array{Dict{String, Any}}
current_transactions::Array{String}
Blockchain(x::Array{Dict{String, Any}}, y::Array{String}) = new(x, y)
end
答案 0 :(得分:1)
免责声明。这可能不必然是您问题的答案。但我想将其作为答案发布,因为评论不允许我轻松地表达下面的内容。
mutable struct Blockchain
chain::Array{Dict{String, Any}}
current_transactions::Array{String}
block::Dict{String, Any}
new_block::Function
function Blockchain(chain::Array{Dict{String, Any}}, current::Array{String})
new(chain, current, new_block(previous_hash=1, proof=100))
# ^issue calling function within the constructor here
end
end
在此,我假设您正在尝试向您的struct
添加某些成员函数功能,因为您已经声明自己来自C++
背景。但是,这不是朱利安。在Julia中,正如@crstnbr已经建议的那样,我们需要定义作用于对象的全局函数。惯例是在函数末尾添加!
以指示该函数将至少更改其中一个参数。
然后,检查new_block
的定义:
function new_block(proof::String, previous_hash::String=nothing)
block = Dict(
"index" => length(chain) + 1, # which chain?
"timestamp" => time(),
"transactions" => current_transactions, # which current_transactions?
"proof" => proof,
"previous_hash" => previous_hash | hash(chain[end]), # which chain?
)
current_transactions = []
append!(chain, block) # which chain?
return block
end
我注意到了几个严重的错误。首先,您尝试使用一些未定义的变量,例如chain
和current_transactions
。我假设,再次从C++
,您认为new_block
将是Blockchain
的成员函数,因此,可以看到<{1}} chain
成员变量。这不 Julia
的工作原理。第二个问题是你如何试着致电new_block
:
new_block(previous_hash=1, proof=100)
此调用完全错误。上述通话符号依赖于keyword arguments;但是,您的函数定义仅具有位置参数。为了能够支持关键字参数,您需要将函数定义更改为如下所示:
function new_block(; proof::String, previous_hash::String=nothing)
# ^ note the semi-colon here
# ...
end
最后,您将proof
和previous_hash
定义为String
类型,但请使用1
,100
和nothing
进行调用,类型为Int
,Int
和Void
。
我无法理解您对Blockchain应用程序的设计选择,但我强烈建议您一步一步地使用更简单的示例来学习该语言。例如,如果您只是尝试以下示例,您将了解类型注释在Julia中的工作原理:
Main> f(s::String = nothing) = s
f (generic function with 2 methods)
Main> f()
ERROR: MethodError: no method matching f(::Void)
Closest candidates are:
f(::String) at none:1
f() at none:1
Stacktrace:
[1] f() at ./none:1
[2] eval(::Module, ::Any) at ./boot.jl:235
Main> g(s::String) = s
g (generic function with 1 method)
Main> g(100)
ERROR: MethodError: no method matching g(::Int64)
Closest candidates are:
g(::String) at none:1
Stacktrace:
[1] eval(::Module, ::Any) at ./boot.jl:235
Main> h1(var1 = 1, var2 = 100) = var1 + var2
h1 (generic function with 3 methods)
Main> h1(var2 = 5, var1 = 6)
ERROR: function h1 does not accept keyword arguments
Stacktrace:
[1] kwfunc(::Any) at ./boot.jl:237
[2] eval(::Module, ::Any) at ./boot.jl:235
最后一条评论是,就我的例子而言,您不需要mutable struct
。 struct
应该只是帮助您完成设计 - 您仍然可以添加/修改其chain
,current_transactions
和block
变量。再次检查以下更简单的示例:
Main> struct MyType
a::Vector{Float64}
end
Main> m = MyType([1,2,3]);
Main> append!(m.a, 4);
Main> m
MyType([1.0, 2.0, 3.0, 4.0])
在上面的示例中,您可以将MyType
的{{1}}变量视为a
中的double * const a
。您 允许将C++
更改为指向其他内存位置,但您可以修改a
指向的内存位置。
简而言之,您应该明确尝试逐步学习官方文档中的语言,并在此处发布涉及真正最少示例的问题。从这个意义上说,你的例子很复杂。