8000 Update diff.rb by tkstanczak · Pull Request #1 · goerli/diff-simulation · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Update diff.rb #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 40 additions & 32 deletions diff.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,34 +42,59 @@ def log(string)
end

# seal an out-of-turn block
def seal_block_out_of_turn(num, diff, par)
_num = num + 1
_diff = diff + @DIFF_NOTURN
def seal_block_out_of_turn(id, par)
if !can_seal?(id, par)
return par
end

_num = par["number"] + 1
_signers = par["signers"].clone
_diff = par["difficulty"] + (_signers.key?(id) ? (_num - _signers[id]) : 1)
_hash = block_hash
_signers[id] = _num

block = {
"signers" => _signers,
"number" => _num,
"difficulty" => _diff,
"hash" => _hash,
"parent" => par
"parent" => par["hash"]
}
end

# seal an in-turn block
def seal_block_in_turn(num, diff, par)
_num = num + 1
_diff = diff + @DIFF_INTURN
def seal_block_in_turn(id, par)
if !can_seal?(id, par)
return par
end

_num = par["number"] + 1
_signers = par["signers"].clone
_diff = par["difficulty"] + @PEER_COUNT + 1
_hash = block_hash
_signers[id] = _num
block = {
"signers" => _signers,
"number" => _num,
"difficulty" => _diff,
"hash" => _hash,
"parent" => par
"parent" => par["hash"],
}
end

def can_seal?(id, par)
if !par["signers"].key?(id)
return true
end

(par["number"] - par["signers"][id]) >= (@PEER_COUNT / 2 + 1)
end


# use this genesis to run the chains
def seal_genesis
genesis = {
"signers" => {},
"number" => 0,
"difficulty" => 1337,
"hash" => "d1f7f7cb18b4",
Expand Down Expand Up @@ -108,9 +133,8 @@ def mine_chain(gen, peers)
# if node is in turn, seal in-turn block
if _current === @PEER_INTURN
_best = seal_block_in_turn(
peer[0]['best']["number"],
peer[0]['best']["difficulty"],
peer[0]['best']["hash"]
peer[0]['id'],
peer[0]['best']
)
_diff = _best['difficulty']
_num = _best['number']
Expand All @@ -119,9 +143,8 @@ def mine_chain(gen, peers)
# if node is not in turn, seal out-of-turn block
else
_best = seal_block_out_of_turn(
peer[0]['best']["number"],
peer[0]['best']["difficulty"],
peer[0]['best']["hash"]
peer[0]['id'],
peer[0]['best']
)
log "[SEAL] Peer #{peer[0]['id']} (head #{peer[0]['best']['hash']}) sealed NOTURN block #{_best['number']}, diff #{_best['difficulty']}, hash #{_best['hash']}, parent #{_best['parent']}&q 9318 uot;.colorize(:light_blue)
end
Expand Down Expand Up @@ -160,26 +183,11 @@ def mine_chain(gen, peers)
end
end


# check: is the network stuck?
_stuck = false
_stuck = true
peers.each do |p|
peers.each do |q|

# condition 1: same difficulty
if p[0]['best']['difficulty'] === q[0]['best']['difficulty']

# condition 2: different hash
if p[0]['best']['hash'].to_i(16) != q[0]['best']['hash'].to_i(16)

# condition 3: same diff as best in-turn block
if p[0]['best']['difficulty'] === _diff

# network is unable to reorg if all conditions are met
_stuck = true
end
end
end
if can_seal?(p[0]['id'], p[0]['best'])
_stuck = false
end
end

Expand Down
0