Skip to content
Permalink
Browse files

Formatting and travis support

  • Loading branch information...
cserb committed May 29, 2019
1 parent 48cd463 commit 6ece18f327721c4e2a48f96526462a3646d62b8d
@@ -0,0 +1,15 @@
language: crystal

script:
- crystal spec
- crystal tool format --check
- crystal docs

deploy:
provider: pages
skip_cleanup: true
github_token: $GITHUB_TOKEN
project_name: cocol
on:
branch: master
local_dir: docs
@@ -2,7 +2,6 @@ require "spec"
require "../src/cocol/node/ledger/repo.cr"

describe "Ledger::Repo" do

context "Blocks" do
block_a = Ledger::Model::Block.genesis

@@ -24,20 +24,19 @@ describe "Ledger" do
transactions = Array(Ledger::Model::Transaction).new
nonce = 12_u64


block_a = Ledger::Model::Block.new(
height: height,
transactions: transactions,
previous_hash: previous_hash,
timestamp: Time.utc_now.to_unix-10,
timestamp: Time.utc_now.to_unix - 10,
nonce: nonce,
hash: "hash_a"
)
block_aa = Ledger::Model::Block.new(
height: height,
transactions: transactions,
previous_hash: previous_hash,
timestamp: Time.utc_now.to_unix-9,
timestamp: Time.utc_now.to_unix - 9,
nonce: nonce,
hash: "hash_aa"
)
@@ -46,7 +45,7 @@ describe "Ledger" do
height: block_a.height + 1,
transactions: transactions,
previous_hash: block_a.hash,
timestamp: Time.utc_now.to_unix-8,
timestamp: Time.utc_now.to_unix - 8,
nonce: nonce,
hash: "hash_b"
)
@@ -55,7 +54,7 @@ describe "Ledger" do
height: block_b.height + 1,
transactions: transactions,
previous_hash: block_b.hash,
timestamp: Time.utc_now.to_unix-7,
timestamp: Time.utc_now.to_unix - 7,
nonce: nonce,
hash: "hash_c"
)
@@ -64,7 +63,7 @@ describe "Ledger" do
height: block_c.height + 1,
transactions: transactions,
previous_hash: block_c.hash,
timestamp: Time.utc_now.to_unix-6,
timestamp: Time.utc_now.to_unix - 6,
nonce: nonce,
hash: "hash_e"
)
@@ -4,11 +4,11 @@ require "../src/cocol/node/ledger/model/transaction.cr"
describe "Ledger::Model::Transaction" do
context "JSON Serializable" do
txn_json = {
from: "Olivia",
to: "Teddyshum",
amount: 100,
from: "Olivia",
to: "Teddyshum",
amount: 100,
timestamp: 1449970561,
hash: "c9b0f2181f2783594c8c30f79ef8ff6231494ab50013ed0bb0fc2cd75408f791"
hash: "c9b0f2181f2783594c8c30f79ef8ff6231494ab50013ed0bb0fc2cd75408f791",
}.to_json

it "should initialize successfully" do
@@ -14,7 +14,6 @@ require "./cocol/node/event.cr"
require "./cocol/node/messenger.cr"

class Cocol::App

def run_api(port : Int32)
before_all do |env|
env.response.content_type = "application/json"
@@ -9,5 +9,4 @@ module Node
Messenger.establish_network_position
end
end

end
@@ -34,14 +34,14 @@ module Event
update = {
event: event,
peers: Messenger::Repo.peers.map { |peer| peer.handshake.port },
port: Node.settings.port,
miner: Node.settings.miner
port: Node.settings.port,
miner: Node.settings.miner,
}

if active_block = Ledger::Repo.active_block
update = update.merge({
height: active_block.height,
hash: active_block.hash
hash: active_block.hash,
})
else
update = update.merge({height: 0, hash: "-"})
@@ -53,26 +53,26 @@ module Event
def transaction(event : EventType,
transaction : Ledger::Model::Transaction) : TransactionEvent
{
event: event,
hash: transaction.hash,
amount: transaction.amount
event: event,
hash: transaction.hash,
amount: transaction.amount,
}
end

def peer(peer : Messenger::Struct::Peer) : PeerConnectionEvent
{
event: "onConnection",
event: "onConnection",
node_port: Node.settings.port,
peer_port: peer.handshake.port
peer_port: peer.handshake.port,
}
end

def block(block : Ledger::Model::Block) : NewBlockEvent
{
event: "onNewBlock",
hash: block.hash,
event: "onNewBlock",
hash: block.hash,
previous_hash: block.previous_hash,
height: block.height
height: block.height,
}
end
end
@@ -71,7 +71,7 @@ module Ledger

def first_candidate : String
tc = Ledger::Repo.candidates.map { |c| [c, Ledger::Repo.blocks[c].timestamp] }
tcs = tc.sort { |a,b| a[1].as(Int64) <=> b[1].as(Int64) }
tcs = tc.sort { |a, b| a[1].as(Int64) <=> b[1].as(Int64) }

return tcs.last[0].as(String)
end
@@ -124,5 +124,4 @@ module Ledger
update_ledger
end
end

end
@@ -5,22 +5,21 @@ require "big"
require "./transaction"

module Ledger
### PoW
# ## PoW

module PoW
extend self

alias BlockHash = String

record Work,
nonce : UInt64,
hash : BlockHash
nonce : UInt64,
hash : BlockHash
record BlockData,
height : UInt64,
timestamp : Int64,
transactions : Array(String),
previous_hash : BlockHash do

height : UInt64,
timestamp : Int64,
transactions : Array(String),
previous_hash : BlockHash do
def to_hash_input : String
"#{height}#{timestamp}#{transactions}#{previous_hash}"
end
@@ -62,18 +61,17 @@ module Ledger
property transactions : Array(Transaction)

def initialize(@hash,
@timestamp,
@height,
@nonce,
@previous_hash,
@transactions)
@timestamp,
@height,
@nonce,
@previous_hash,
@transactions)
end

def self.new(height : UInt64,
transactions : Array(Transaction),
previous_hash : String,
difficulty_bits : Int32 = 20)

transactions : Array(Transaction),
previous_hash : String,
difficulty_bits : Int32 = 20)
sleep Random.rand(5.0..6.1)
block_data = BlockData.new(
timestamp: Time.utc_now.to_unix,
@@ -82,7 +80,7 @@ module Ledger
transactions: transactions.map { |txn| txn.hash }
)
work = PoW.mine(difficulty_bits: difficulty_bits,
block_data: block_data)
block_data: block_data)

Block.new(
hash: work.hash,
@@ -65,8 +65,8 @@ module Ledger
return false if self.blocks[block.hash]?
return false if self.height[block.height]?
return false if self.candidates.any? do |c|
self.blocks[c].height == block.height
end
self.blocks[c].height == block.height
end

# new block add to blocks
self.blocks[block.hash] = block
@@ -1,9 +1,9 @@
require "http/client"

require "./messenger/struct/handshake"
require "./messenger/struct/peer"
require "./messenger/struct/handshake"
require "./messenger/struct/peer"

require "./messenger/repo"
require "./messenger/repo"

require "./ledger"
require "./ledger/model/transaction"
@@ -70,7 +70,7 @@ module Messenger
endpoint,
headers: HTTP::Headers{
"Content-Type" => "application/json",
"X-Node-Id" => Node.settings.port.to_s
"X-Node-Id" => Node.settings.port.to_s,
},
body: body
)
@@ -1,21 +1,21 @@
get "/peers" do |_env|
Messenger::Repo.peers.map { |peer|
{
ip_addr: "localhost",
ip_addr: "localhost",
handshake: {
ident: peer.handshake.ident,
port: peer.handshake.port
}
port: peer.handshake.port,
},
}
}.to_json
end

post "/peers" do |env|
peer = Messenger::Struct::Peer.new(
Messenger::Struct::Handshake.new(
ident: UUID.new(env.params.json["ident"].as(String)),
port: env.params.json["port"].as(Int64).to_i32
)
ident: UUID.new(env.params.json["ident"].as(String)),
port: env.params.json["port"].as(Int64).to_i32
)
)

# TODO: more like if Messenger.accepts_connection?(from: peer)
@@ -31,11 +31,11 @@ end
get "/known-peers" do |_env|
Messenger::Repo.known_peers.map { |peer|
{
ip_addr: "localhost",
ip_addr: "localhost",
handshake: {
ident: peer.handshake.ident,
port: peer.handshake.port
}
port: peer.handshake.port,
},
}
}.to_json
end

0 comments on commit 6ece18f

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