Skip to content

Commit

Permalink
[WIP] Give each miner a human readable hash (#231)
Browse files Browse the repository at this point in the history
* each miner now gets a human hash and reduce noise in logs also

* fix specs
  • Loading branch information
kingsleyh committed Nov 9, 2018
1 parent 0c271dd commit 01a8498
Show file tree
Hide file tree
Showing 8 changed files with 29 additions and 14 deletions.
12 changes: 8 additions & 4 deletions shard.lock
Expand Up @@ -2,25 +2,29 @@ version: 1.0
shards:
db:
github: crystal-lang/crystal-db
version: 0.5.0
version: 0.5.1

humanhash:
github: kingsleyh/humanhash
commit: aa533045dcc8986d6dba7f6408b8a10e578dad1e

radix:
github: luislavena/radix
version: 0.3.8

router:
github: tbrand/router.cr
version: 0.2.3
version: 0.2.7

scrypt:
github: ysbaddaden/scrypt-crystal
version: 0.2.2

sqlite3:
github: crystal-lang/crystal-sqlite3
version: 0.9.0
version: 0.10.0

tokoroten:
github: tbrand/tokoroten
version: 0.1.2
version: 0.1.3

2 changes: 2 additions & 0 deletions shard.yml
Expand Up @@ -27,6 +27,8 @@ dependencies:
github: crystal-lang/crystal-sqlite3
scrypt:
github: ysbaddaden/scrypt-crystal
humanhash:
github: kingsleyh/humanhash

crystal: 0.27.0

Expand Down
2 changes: 1 addition & 1 deletion spec/units/utils/chain_generator.cr
Expand Up @@ -40,7 +40,7 @@ module ::Units::Utils::ChainGenerator
@node = Sushi::Core::Node.new(true, true, "bind_host", 8008_i32, nil, nil, nil, nil, nil, @node_wallet, nil, false)
@blockchain = @node.blockchain
@blockchain.setup(@node)
@miner = {context: {address: miner_wallet.address, nonces: [] of UInt64}, socket: MockWebSocket.new}
@miner = {context: {address: miner_wallet.address, nonces: [] of UInt64}, socket: MockWebSocket.new, mid: "535061bddb0549f691c8b9c012a55ee2"}
@transaction_factory = TransactionFactory.new(@node_wallet)
@rpc = RPCController.new(@blockchain)
@rest = RESTController.new(@blockchain)
Expand Down
1 change: 1 addition & 0 deletions src/core.cr
Expand Up @@ -26,6 +26,7 @@ require "tokoroten"
require "http/server"
require "openssl/pkcs5"
require "openssl/digest"
require "humanhash"

require "./common"
require "./core/modules"
Expand Down
4 changes: 3 additions & 1 deletion src/core/miner/miner.cr
Expand Up @@ -14,6 +14,7 @@ module ::Sushi::Core
class Miner < HandleSocket
@wallet : Wallet
@use_ssl : Bool
@mid : String = HumanHash.uuid.digest

@workers : Array(Tokoroten::Worker) = [] of Tokoroten::Worker

Expand Down Expand Up @@ -52,6 +53,7 @@ module ::Sushi::Core
send(socket, M_TYPE_MINER_HANDSHAKE, {
version: Core::CORE_VERSION,
address: @wallet.address,
mid: @mid
})

socket.run
Expand Down Expand Up @@ -95,7 +97,7 @@ module ::Sushi::Core
block = _m_content.block
difficulty = _m_content.difficulty

info "#{magenta("UPDATED BLOCK")}: #{light_green(block.index)} at chain difficulty: #{light_cyan(block.next_difficulty - 1)} with miner difficulty: #{light_cyan(difficulty)}"
info "#{magenta("MINED BLOCK")}: #{light_green(block.index)} at chain difficulty: #{light_cyan(block.next_difficulty - 1)} with miner difficulty: #{light_cyan(difficulty)}"

debug "set difficulty: #{light_cyan(difficulty)}"
debug "set block: #{light_green(block.index)}"
Expand Down
2 changes: 1 addition & 1 deletion src/core/node.cr
Expand Up @@ -284,7 +284,7 @@ module ::Sushi::Core

def broadcast_block(socket : HTTP::WebSocket, block : Block, from : Chord::NodeContext? = nil)
if @blockchain.latest_index + 1 == block.index
info "new block coming: #{light_cyan(@blockchain.chain.size)}"
debug "new block coming: #{light_cyan(@blockchain.chain.size)}"

if _block = @blockchain.valid_block?(block)
new_block(_block)
Expand Down
15 changes: 10 additions & 5 deletions src/core/node/components/miners_manager.cr
Expand Up @@ -22,6 +22,7 @@ module ::Sushi::Core::NodeComponents
alias Miner = NamedTuple(
context: MinerContext,
socket: HTTP::WebSocket,
mid: String
)

alias Miners = Array(Miner)
Expand All @@ -40,6 +41,7 @@ module ::Sushi::Core::NodeComponents

version = _m_content.version
address = _m_content.address
mid = _m_content.mid

if Core::CORE_VERSION > version
return send(socket,
Expand All @@ -61,11 +63,12 @@ module ::Sushi::Core::NodeComponents
end

miner_context = {address: address, nonces: [] of UInt64}
miner = {context: miner_context, socket: socket}
miner = {context: miner_context, socket: socket, mid: mid}

@miners << miner

info "new miner: #{light_green(miner[:context][:address][0..7])} (#{@miners.size})"
miner_name = HumanHash.humanize(mid)
info "new miner: #{light_green(miner_name)} (#{@miners.size})"

send(socket, M_TYPE_MINER_HANDSHAKE_ACCEPTED, {
version: Core::CORE_VERSION,
Expand All @@ -92,10 +95,11 @@ module ::Sushi::Core::NodeComponents

send(miner[:socket], M_TYPE_MINER_BLOCK_UPDATE, {
block: @blockchain.mining_block,
difficulty: @blockchain.mining_block_difficulty_miner,
difficulty: @blockchain.mining_block_difficulty_miner
})
else
info "miner #{miner[:context][:address][0..7]} found nonce (nonces: #{miner[:context][:nonces].size})"
miner_name = HumanHash.humanize(miner[:mid])
debug "miner #{miner_name} found nonce (nonces: #{miner[:context][:nonces].size})"

miner[:context][:nonces].push(nonce)

Expand All @@ -116,13 +120,14 @@ module ::Sushi::Core::NodeComponents
end

def broadcast
info "#{magenta("MINED BLOCK")}: #{light_green(@blockchain.mining_block.index)} at chain difficulty: #{light_cyan(@blockchain.mining_block_difficulty)} with miner difficulty: #{light_cyan(@blockchain.mining_block_difficulty_miner)}"
debug "new block difficulty: #{@blockchain.mining_block_difficulty}, " +
"mining difficulty: #{@blockchain.mining_block_difficulty_miner}"

@miners.each do |miner|
send(miner[:socket], M_TYPE_MINER_BLOCK_UPDATE, {
block: @blockchain.mining_block,
difficulty: @blockchain.mining_block_difficulty_miner,
difficulty: @blockchain.mining_block_difficulty_miner
})
end
end
Expand Down
5 changes: 3 additions & 2 deletions src/core/protocol/protocol.cr
Expand Up @@ -21,6 +21,7 @@ module ::Sushi::Core::Protocol
JSON.mapping({
version: Int32,
address: String,
mid: String
})
end

Expand All @@ -46,7 +47,7 @@ module ::Sushi::Core::Protocol

struct M_CONTENT_MINER_FOUND_NONCE
JSON.mapping({
nonce: UInt64,
nonce: UInt64
})
end

Expand All @@ -55,7 +56,7 @@ module ::Sushi::Core::Protocol
struct M_CONTENT_MINER_BLOCK_UPDATE
JSON.mapping({
block: Block,
difficulty: Int32,
difficulty: Int32
})
end

Expand Down

0 comments on commit 01a8498

Please sign in to comment.