-
Notifications
You must be signed in to change notification settings - Fork 5
/
Miner.m
41 lines (36 loc) · 966 Bytes
/
Miner.m
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
classdef Miner < handle
properties
blockchain
end
methods
function obj = Miner(blockchain)
obj.blockchain = blockchain;
end
function mine(obj, newData)
% get the last block on the current blockchain
latestBlock = obj.blockchain.getLatest();
% construct a new block
newBlock = Block(latestBlock.index+1,...
newData,...
latestBlock.selfHash);
% find appropriate selfhash
not_found = true;
iter = 1;
Opt.Method = 'SHA-256';
Opt.Input = 'ascii';
tic
while(not_found)
newHash = DataHash([strcat(newBlock.getCombined(), num2str(iter))]);
if(strcmp(newHash(1 : 3), '000'))
newBlock.nonce = iter; % solve violently
newBlock.selfHash = newHash; % if the approproate selfhash is found
disp(newHash)
obj.blockchain.addBlock(newBlock); % add selfhash to blockchain
break
end
iter = iter + 1;
end
toc
end
end
end