Skip to content
Permalink
Browse files

Retargeting

  • Loading branch information...
cserb committed Aug 9, 2019
1 parent 7ce6926 commit e825c8d6e05d0aff68c3958679e861fc4b7132a8
Showing with 23 additions and 47 deletions.
  1. +4 −0 .projectile
  2. +1 −1 script/start.sh
  3. +1 −1 shard.lock
  4. +1 −2 shard.yml
  5. +1 −1 src/cocol.cr
  6. +11 −8 src/cocol/node/ledger.cr
  7. +1 −1 src/cocol/node/ledger/api.cr
  8. +2 −32 src/cocol/node/ledger/model/block.cr
  9. +1 −1 src/cocol/node/ledger/repo.cr
@@ -0,0 +1,4 @@
-/docs
-/lib
-/bin
-/img
@@ -15,7 +15,7 @@ do
sleep 0.2
done

for ((i=1;i<=2;i++));
for ((i=1;i<=1;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 &
@@ -2,7 +2,7 @@ version: 1.0
shards:
btcpow:
github: cocol-project/btcpow
commit: c02a69bb7387170881ce54dd7cefa7fef32a4c3c
commit: c72181405213de8f00aaf98a896a80fca2bc9a6c

callback:
github: mosop/callback
@@ -1,5 +1,5 @@
name: cocol
version: 0.1.0-alpha
version: 0.1.2-alpha

authors:
- Cristian Șerb <cserb@pm.me>
@@ -22,7 +22,6 @@ dependencies:
version: ~> 0.5.1
btcpow:
github: cocol-project/btcpow
branch: retarget

development_dependencies:
minitest:
@@ -39,7 +39,7 @@ class Cocol::App
).size

if pending_transactions_count >= threshold
Cocol.logger.info "[#{Time.now}] [Node: #{Node.settings.port}] Mining triggered"
Cocol.logger.info "[Node: #{Node.settings.port}] Mining triggered"
mining_transactions = pending_transactions
Ledger::Repo.delete_transactions(mining_transactions)
Ledger.workflow_mine(mining_transactions)
@@ -27,6 +27,7 @@ module Ledger
genesis = Ledger::Model::Block.genesis

Ledger::Repo.blocks[genesis.hash] = genesis
Ledger::Repo.height[0] = genesis.hash
Ledger::Repo.ledger << genesis.hash
end

@@ -42,17 +43,19 @@ module Ledger
height = active_block.height + 1
end

if height % 12 == 0
first_block = Ledger::Repo.blocks[Ledger::Repo.height[height - 12]]
if height % 20 == 0
Cocol.logger.info "Retargeting Now"

first_block = Ledger::Repo.blocks[Ledger::Repo.height[height - 20]]
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,
start_time: first_block.timestamp.to_f64,
end_time: last_block.timestamp.to_f64,
wanted_timespan: 60_f64,
current_target: BTCPoW::Utils.calculate_target(from: last_block.nbits)
)
else # min difficulty
difficulty = Ledger::Model::Block::MIN_NBITS
else # last blocks difficulty
difficulty = Ledger::Repo.blocks[previous_hash].nbits
end

new_block = Ledger::Model::Block.new(
@@ -63,7 +66,7 @@ module Ledger
)

if Ledger::Repo.save_block(new_block)
Cocol.logger.info "[#{Time.now}] [Node: #{Node.settings.port}] Height: #{new_block.height} NBits: #{new_block.nbits} Mined: #{new_block.hash}"
Cocol.logger.info "[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)

@@ -21,7 +21,7 @@ post "/blocks" do |env|
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}"
Cocol.logger.info "[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)
@@ -7,37 +7,6 @@ require "./transaction"
require "btcpow"

module Ledger
# ## PoW

# module PoW
# extend self

# record Work,
# nonce : UInt64,
# hash : BlockHash

# def self.mine(difficulty_bits : Int32, block_data : BlockData) : Work
# max_nonce = BigInt.new(2) ** BigInt.new(32)
# target = BigInt.new(2) ** BigInt.new(256 - difficulty_bits)
# data = block_data.to_hash_input

# (0..max_nonce).each do |i|
# hash = calculate_hash(i.to_i64, data)
# if BigInt.new(hash, 16) < target
# return Work.new(nonce: i.to_u64, hash: hash)
# end
# end

# raise "max_nonce reached"
# end

# def self.calculate_hash(nonce : Int64, data : String) : BlockHash
# sha = OpenSSL::Digest.new("SHA256")
# sha.update("#{nonce}#{data}")
# sha.hexdigest
# end
# end

module Model
class Block
MIN_NBITS = "20000010"
@@ -80,7 +49,7 @@ module Ledger
def self.new(height : UInt64,
transactions : Array(Transaction),
previous_hash : String,
difficulty : String = MIN_NBITS)
difficulty : String)
#sleep Random.rand(5.0..6.1)
block_data = BlockData.new(
timestamp: Time.utc_now.to_unix,
@@ -89,6 +58,7 @@ module Ledger
randr: Random.rand(0_u16..UInt16::MAX),
transactions: transactions.map { |txn| txn.hash }
)
Cocol.logger.info("Miner: #{Node.settings.port} Difficulty: #{difficulty}")
work = BTCPoW.mine(difficulty: difficulty,
for: block_data.to_input)

@@ -70,13 +70,13 @@ module Ledger

# new block add to blocks
self.blocks[block.hash] = block
self.height[block.height] = block.hash
true
end

def establish(block_hash : BlockHash, height : Height) : Void
self.ledger << block_hash
self.established_height(plus: 1_u64)
self.height[height] = block_hash
end

def save_transaction(transaction : Model::Transaction) : Bool

0 comments on commit e825c8d

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