-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathblock_storage.lua
More file actions
63 lines (54 loc) · 1.74 KB
/
block_storage.lua
File metadata and controls
63 lines (54 loc) · 1.74 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
local modname = core.get_current_modname()
local modpath = core.get_modpath(modname)
local sha = dofile(modpath .. "/lib/sha/sha2.lua")
local world_path = core.get_worldpath()
local file = world_path .. "/testcoin/chain_data.json"
testcoin.blocks = {}
-- seed: bf532cd7b4a9af3c00cc02c54943f7a1659c5fb7906f2776b691604d603fa6bc
testcoin.vtx_coinbase = {
hash = "00000031a3177ebd6f1e348126c87762c7e991590d52cd3d9a47df220289304d",
ref = "00000031a3170000000000000000000000000000000000000000000000000000"
}
function testcoin.save_chain()
local output = io.open(file, "w")
if output then
output:write(core.write_json(testcoin.blocks))
io.close(output);
end
end
function testcoin.read_chain()
local input = io.open(file, "r")
if input then
testcoin.blocks = core.parse_json(input:read("*all"))
input:close()
else
testcoin.add_block()
end
end
function testcoin.add_block(data)
local block = {
prev_block = testcoin.chain_tip.hash,
data = data,
time = os.date()
}
local block_str = core.write_json(block)
local hash = sha.sha256(block_str)
local block_data = {
block = block,
hash = hash
}
table.insert(testcoin.blocks, block_data)
testcoin.save_chain();
local height = #testcoin.blocks
testcoin.chain_tip.hash = hash
testcoin.chain_tip.height = height
testcoin.chain_tip.prev = block.prev_block
testcoin.chain_tip.timestamp = block.time
return hash, height
end
------------------------------------------------
local function load_chain()
testcoin.read_chain()
core.log("Loaded chain from file with height '" .. testcoin.chain_tip.height .. "' at hash 0x" .. testcoin.chain_tip.hash)
end
load_chain();