Skip to content
Permalink
Browse files

Adding Logger & BTCPoW

  • Loading branch information...
cserb committed Aug 7, 2019
1 parent 358d5d2 commit 3537b0bb086c4092691540a68842370e1d237905
@@ -3,14 +3,22 @@
let startPort=3001
let amount=$(($1 - 1))

brave file:///home/cris/Projects/crystal/cocol/explorer/index.html
./cocol -p 3000 -m --max-connections 1000 &
sleep 0.5
brave file:///home/cris/Projects/crystal/cocol/explorer/index.html &> /dev/null &
./cocol -p 3000 -m --max-connections 300 &> /dev/null &
sleep 1

for ((i=0;i<=$amount;i++));
for ((i=1;i<=$amount;i++));
do
# ../cocol -p $(($startPort + $i)) -a $(($startApiPort + $i)) > /dev/null 2>&1 &
./cocol -p $(($startPort + $i)) --max-connections $2 &
./cocol -p $(($startPort + $i)) --max-connections 5 1>> /tmp/cocol.log &
echo $i
sleep 0.2
done

for ((i=1;i<=2;i++));
do
# ../cocol -p $(($startPort + $i)) -a $(($startApiPort + $i)) > /dev/null 2>&1 &
./cocol -p $((4000 + $i)) --max-connections 5 --miner 1>> /tmp/cocol.log &
echo $i
sleep 0.2
done
@@ -1,5 +1,9 @@
version: 1.0
shards:
btcpow:
github: cocol-project/btcpow
commit: 5fe8ac7c927079c9a6015bd493a23744a4784121

callback:
github: mosop/callback
version: 0.6.3
@@ -8,6 +12,10 @@ shards:
github: crystal-loot/exception_page
version: 0.1.2

icr:
github: crystal-community/icr
commit: a0a5546e0e566891e5ce221313cb19995d0a643f

kemal:
github: kemalcr/kemal
version: 0.25.2
@@ -8,7 +8,7 @@ targets:
cocol:
main: src/cocol.cr

crystal: 0.28.2
crystal: 0.30.0

dependencies:
optarg:
@@ -17,9 +17,15 @@ dependencies:
github: kemalcr/kemal
totem:
github: icyleaf/totem
btcpow:
github: cocol-project/btcpow
branch: retarget

development_dependencies:
minitest:
github: ysbaddaden/minitest.cr
icr:
github: crystal-community/icr
branch: master

license: MPL-2.0
@@ -23,13 +23,17 @@ describe "Ledger" do
previous_hash = Ledger::Repo.ledger.last
transactions = Array(Ledger::Model::Transaction).new
nonce = 12_u64
nbits = "20100000"
randr = rand(0_u16..UInt16::MAX)

block_a = Ledger::Model::Block.new(
height: height,
transactions: transactions,
previous_hash: previous_hash,
timestamp: Time.utc_now.to_unix - 10,
nonce: nonce,
nbits: nbits,
randr: randr,
hash: "hash_a"
)
block_aa = Ledger::Model::Block.new(
@@ -38,6 +42,8 @@ describe "Ledger" do
previous_hash: previous_hash,
timestamp: Time.utc_now.to_unix - 9,
nonce: nonce,
nbits: nbits,
randr: randr,
hash: "hash_aa"
)

@@ -47,6 +53,8 @@ describe "Ledger" do
previous_hash: block_a.hash,
timestamp: Time.utc_now.to_unix - 8,
nonce: nonce,
nbits: nbits,
randr: randr,
hash: "hash_b"
)

@@ -56,6 +64,8 @@ describe "Ledger" do
previous_hash: block_b.hash,
timestamp: Time.utc_now.to_unix - 7,
nonce: nonce,
nbits: nbits,
randr: randr,
hash: "hash_c"
)

@@ -65,6 +75,8 @@ describe "Ledger" do
previous_hash: block_c.hash,
timestamp: Time.utc_now.to_unix - 6,
nonce: nonce,
nbits: nbits,
randr: randr,
hash: "hash_e"
)

@@ -110,35 +122,39 @@ describe "Ledger" do
Ledger::Repo.candidates.includes?(block_e.hash).should be_truthy
Ledger::Repo.orphans[block_e.previous_hash]?.should be_falsey
end
end
# end

describe "Workflow Mining" do
# describe "Workflow Mining" do
new_block_parent = Ledger::Repo.candidates.first
Ledger.workflow_mine(
transactions: Ledger::Repo.pending_transactions.values,
difficulty_bits: 1
spawn { Ledger.workflow_mine(transactions: Ledger::Repo.pending_transactions.values) }
# sleep 0.2
block_fee = Ledger::Model::Block.new(
height: block_c.height + 2,
transactions: transactions,
previous_hash: block_c.hash,
timestamp: Time.utc_now.to_unix - 6,
nonce: nonce,
nbits: nbits,
randr: randr,
hash: "hash_fee"
)
pp block_c.height + 2
if Ledger::Repo.save_block(block_fee)
puts "block_fee saved!!!!"
Ledger.workflow_assign_block(block_fee)
end

it "it became first candidate" do
new_block_parent.should eq(Ledger::Repo.ledger.last)
new_block = Ledger::Repo.blocks[Ledger::Repo.candidates.first]
new_block.previous_hash.should eq(new_block_parent)
Ledger::Repo.blocks[Ledger::Repo.candidates.first].height.should eq(Ledger::Repo.blocks[new_block_parent].height + 1)
end

txn = Ledger::Model::Transaction.new(
from: "Olivia",
to: "Teddyshum",
amount: 100_f32,
)
Ledger::Repo.pending_transactions[txn.hash] = txn

Ledger.workflow_mine(
transactions: Ledger::Repo.pending_transactions.values,
difficulty_bits: 1
)
it "deleted used transactions" do
Ledger::Repo.pending_transactions.empty?.should be_true
end
puts "----"
p Ledger::Repo.blocks
p Ledger::Repo.ledger
p Ledger::Repo.height
p Ledger::Repo.candidates
puts "---"
end
end
@@ -5,6 +5,8 @@ require "totem"
require "uuid"
require "uuid/json"

require "./cocol/logger.cr"

require "./cocol/cli/argument"

require "./cocol/node/settings"
@@ -27,7 +29,7 @@ class Cocol::App
Kemal.run(port: port, args: nil)
end

def self.block_creation_loop
def self.block_mining_loop
threshold = 2
loop do
sleep 1
@@ -37,7 +39,10 @@ class Cocol::App
).size

if pending_transactions_count >= threshold
Ledger.workflow_mine(pending_transactions)
Cocol.logger.info "[#{Time.now}] [Node: #{Node.settings.port}] Mining triggered"
mining_transactions = pending_transactions
Ledger::Repo.delete_transactions(mining_transactions)
Ledger.workflow_mine(mining_transactions)
end
end
end
@@ -53,14 +58,19 @@ class Cocol::App
spawn Node.start

if args.miner?
spawn block_creation_loop
spawn block_mining_loop
end

if args.update?
spawn Ledger.update_ledger
end

cocol.run_api(port: args.port.to_i32)
spawn { cocol.run_api(port: args.port.to_i32) }

loop do
# nothing
sleep 0.01
end
end
end

@@ -0,0 +1,7 @@
module Cocol
extend self

def logger
@@logger ||= Logger.new(STDOUT, level: Logger::INFO)
end
end

This file was deleted.

@@ -10,6 +10,8 @@ require "./messenger.cr"

require "./ledger/api.cr"

require "btcpow"

module Ledger
extend self

@@ -28,29 +30,41 @@ module Ledger
Ledger::Repo.ledger << genesis.hash
end

def workflow_mine(transactions : Array(Ledger::Model::Transaction), difficulty_bits : Int32 = 1) : Void
def workflow_mine(transactions : Array(Ledger::Model::Transaction)) : Void
active_block = Ledger::Repo.active_block
raise "No active block" unless active_block

if Ledger::Repo.candidates.size > 0
previous_hash = first_candidate()
# previous_hash = Ledger::Repo.candidates.first
height = Ledger::Repo.blocks[previous_hash].height + 1
else
previous_hash = active_block.hash
height = active_block.height + 1
end

if height % 12 == 0
first_block = Ledger::Repo.blocks[Ledger::Repo.height[height - 12]]
last_block = Ledger::Repo.blocks[Ledger::Repo.height[height - 1]]
difficulty = BTCPoW::Utils.retarget(
start_time: first_block.timestamp,
end_time: last_block.timestamp,
wanted_timespan: 1_u32,
current_target: BTCPoW::Utils.calculate_target(from: last_block.nbits)
)
else # min difficulty
difficulty = Ledger::Model::Block::MIN_NBITS
end

new_block = Ledger::Model::Block.new(
height: height,
transactions: transactions,
previous_hash: previous_hash,
difficulty_bits: difficulty_bits
difficulty: difficulty
)

if Ledger::Repo.save_block(new_block)
pp "[#{Time.now}] [Node: #{Node.settings.port}] Mined: #{new_block.hash}"
Ledger::Repo.delete_transactions(transactions)
Cocol.logger.info "[#{Time.now}] [Node: #{Node.settings.port}] Height: #{new_block.height} NBits: #{new_block.nbits} Mined: #{new_block.hash}"
# Ledger::Repo.delete_transactions(transactions)
workflow_assign_block(new_block)

spawn { Messenger.broadcast to: "/blocks", body: new_block.to_json }
@@ -20,6 +20,9 @@ post "/blocks" do |env|
new_block = Ledger::Model::Block.from_json(
env.request.body.not_nil!)
if Ledger::Repo.save_block(new_block)
if Node.settings.port > 4000
Cocol.logger.info "[#{Time.now}] [Node: #{Node.settings.port}] Height: #{new_block.height} NBits: #{new_block.nbits} Saved: #{new_block.hash}"
end
spawn do
Ledger.workflow_assign_block(new_block)
Messenger.broadcast to: "/blocks", body: new_block.to_json

0 comments on commit 3537b0b

Please sign in to comment.
You can’t perform that action at this time.