-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathblock.rb
More file actions
157 lines (140 loc) · 4.28 KB
/
block.rb
File metadata and controls
157 lines (140 loc) · 4.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# -*- coding: utf-8 -*-
module Tinycoin::Core
class Block
attr_accessor :prev_hash
attr_accessor :height, :bits, :nonce
attr_accessor :time, :hash
attr_accessor :blkblock
attr_accessor :jsonstr
attr_accessor :genesis
attr_accessor :txs
attr_accessor :next, :prev
def self.validate_block_json json_str, current_bits
## ブロックのハッシュとdiffcultyの検証を行う
hashed = JSON.parse(json_str)
block = new_block_from_hash(hashed, include_hash = true)
target = Tinycoin::Core::BlockChain.get_target(current_bits).first.to_i(16)
if block.bits <= target
return block
else
raise Tinycoin::Errors::InvalidBlock
end
end
def self.new_genesis()
obj = self.new
obj.genesis = true
obj.nonce = GENESIS_NONCE
obj.bits = GENESIS_BITS
obj.time = GENESIS_TIME
obj.prev_hash = 0
obj.height = 0
obj.jsonstr = ""
obj.prev = []
obj.next = []
obj.hash = nil
obj.txs = []
return obj
end
def self.new_block(prev_hash, nonce, bits, time, height, payloadstr)
obj = self.new
obj.prev_hash = prev_hash.to_i(16)
obj.genesis = false
obj.nonce = nonce
obj.bits = bits
obj.time = time
obj.height = height
obj.jsonstr = payloadstr
obj.prev = []
obj.next = []
obj.hash = nil
obj.txs = []
return obj
end
def self.parse_json jsonstr, include_hash = false
jsonhash = JSON.parse(jsonstr)
new_block_from_hash(jsonhash, include_hash)
end
def self.new_block_from_hash hashed_json, include_hash = false
raise Tinycoin::Errors::InvalidUnknownFormat if not hashed_json["type"] == "block"
begin
obj = self.new
obj.height = hashed_json.fetch("height")
obj.prev_hash = hashed_json.fetch("prev_hash").to_i(16)
obj.nonce = hashed_json.fetch("nonce")
obj.bits = hashed_json.fetch("bits")
obj.time = hashed_json.fetch("time")
obj.jsonstr = hashed_json.fetch("jsonstr")
obj.prev = []
obj.next = []
obj.hash = nil
obj.txs = Tx.parse_from_hashs(hashed_json.fetch("txs"))
if include_hash
hash = hashed_json["hash"].to_i(16)
raise Tinycoin::Errors::InvalidFieldFormat if hash == 0
raise Tinycoin::Errors::InvalidBlock unless validate_block_hash(obj, hashed_json["hash"])
obj.hash = hash
end
return obj
rescue KeyError
raise Tinycoin::Errors::InvalidFieldFormat
end
end
def self.validate_block_hash block, hash_hexstr
truehash = Digest::SHA256.hexdigest(Digest::SHA256.digest(block.to_binary_s)).to_i(16)
truehash == hash_hexstr.to_i(16) ? true : false
end
def add_tx_as_first tx
@txs[0] = tx
end
def add_tx tx
@txs << tx
end
def to_binary_s
blkblock = generate_blkblock()
return blkblock.to_binary_s
end
def to_sha256hash
blkblock = generate_blkblock()
@hash ||= Digest::SHA256.hexdigest(Digest::SHA256.digest(blkblock.to_binary_s)).to_i(16)
return @hash
end
def to_sha256hash_s
to_sha256hash.to_s(16).rjust(64, '0')
end
def prev_sha256hash_s
@prev_hash.to_s(16).rjust(64, '0')
end
def refresh
@blkblock = @hash = nil
end
def to_hash
{ type: "block",
height: @height,
prev_hash: @prev_hash.to_s(16).rjust(64, '0'),
hash: to_sha256hash_s,
nonce: @nonce,
bits: @bits,
time: @time,
txs: @txs.map {|t| t.to_hash},
jsonstr: @jsonstr
}
end
def to_json
to_hash.to_json
end
private
def generate_blkblock
@blkblock = Tinycoin::Types::BulkBlock.new(
block_id: @height,
time: @time,
bits: @bits,
prev_hash: @prev_hash,
strlen: @jsonstr.size(),
payloadstr: @jsonstr,
nonce: @nonce,
txs: @txs.map {|t| t.generate_blk},
)
return @blkblock
end
end
end