Skip to content

Commit

Permalink
cumulative difficulty fix
Browse files Browse the repository at this point in the history
  • Loading branch information
kushti committed Dec 4, 2014
1 parent e7ab6c3 commit 22bb786
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 4 deletions.
2 changes: 2 additions & 0 deletions .gitignore
@@ -0,0 +1,2 @@
.idea/
*.iml
8 changes: 8 additions & 0 deletions README.md
Expand Up @@ -2,3 +2,11 @@ articles-papers
===============

Articles and Papers from Consensus Research


Articles
--------


Papers
------
8 changes: 4 additions & 4 deletions articles/article2.md
Expand Up @@ -143,11 +143,11 @@ generator account, block generation timestamp and transactions to be included:
Difficulty
----------------------

Within blocktree canonical blockchain is the path having max value e.g. height. We will use sum of `baseTarget`
Within blocktree canonical blockchain is the path having max value e.g. height. We will use sum of 1/`baseTarget`
to select canonical blockchain from possible options and call this function `cumulativeDifficulty`:

cumulativeDifficulty :: BlockChain -> Integer
cumulativeDifficulty BlockChain {blocks=bs} = sum(map baseTarget bs)
cumulativeDifficulty :: BlockChain -> Double
cumulativeDifficulty chain = sum(map (\bl -> 1 / (fromIntegral $ baseTarget bl) ) chain)


Transparent Forging
Expand All @@ -169,4 +169,4 @@ Conclusion & Further Work

This chapter of the series defined functional building blocks and whole forging algo as well.
Next chapter will be kind of academic paper about statistical modelling of Nxt forging algo
with some interesting results. After that we will go deeper into forging simulation with Haskell.
with some interesting results. After that we will go deeper into forging simulation with Haskell.
86 changes: 86 additions & 0 deletions articles/article4.md
@@ -0,0 +1,86 @@
Inside a Proof-of-Stake Cryptocurrency part 4: The Executable Forging Simulation
================================================================================

Introduction
------------
In first 3 parts Nxt-like 100% proof-of-stake forging algorithm was described. It would be exciting to run executable simulation of it to see
network-wide consensus in action. And now [this simulation tool is published](https://github.com/ConsensusResearch/ForgingSimulation)! Please take a look to it's README for details, and this article is about some aspects of the source code unrevealed yet.

Modules
-------

All the code is split into three modules(files):

* `blockchain.hs`, module Blockchain.Structures - basic structures and functions, building bricks for the simulated cryptocurrency network &
its forging algorithm

* `simulation.hs`, module Blockchain.Simulation - structures & functions related to simulation

* `launcher.hs`. module Main - some auxiliary functions & main function running simulation and dumping results into files

Most of basic structures and functions were decscribed in first 3 parts of the series, so now we're going to take a look into simulation functions.

systemTransform
---------------

The most important function in simulation module is `systemTransform` taking current state of a network along with timestamp and common data for the
simulation and transforming it to resulting network state by applying some partial transformers:


systemTransform :: SimulationData -> Network -> Network
systemTransform sd network = networkForge sd $ generateTransactions sd $
propagateLastBlock sd $ propagateTransactions sd $ downloadBlocksNetwork sd $
dropConnections sd $ generateConnections sd $ addNode sd $ addInvestorNode sd network

Where

* `addInvestorNode` makes some initial investor online. Network starts with just one online investor out of 20, system balance is spread uniformly in
a genesis block. Max number of investors online (14,000 seconds after genesis and on) are 13, means 65% forging power being online

* `addNode` adds a node to the simulated network with no blockchain & empty account. The node needs to download a blockchain from other nodes.

* `generateConnections` generates a connection for a node to another node if number of outgoing connection is less than a limit given in settings
file(20 by default)

* `dropConnections` works once per 60 seconds, drop oldest connection for a node, if number of outgoing connections equals to the limit

* `downloadBlocksNetwork` selects random neighbour for each peer and check it's cumulative difficulty, and if this metrics is better for the neighbour,
the node is going to download a chunk of blockchain after common chain(14,400 blocks max)

* `propagateTransactions` selects random neighbour for each peer and sends transactions from peer's unconfirmed transactions pool a naighbour doesn't have

* `propagateLastBlock` selects nodes having last block generated by other node not more than 15 seconds ago and sends it out to a random neighbour

* `generateTransactions` generates random transactions sending max 5% stake out(for account having more than 200 coins out of 1,000,000,000 issued)

* `networkForge` asks each node whether it has a right to generate a block(by it's own opinion) and if so generates blocks for forgers found and sends them out to random neighbours




Nothing-at-Stake simulation
---------------------------

By mixing [multibranch forging simulation tool](https://github.com/ConsensusResearch/MultiBranch) published previously with blockchain forging simulation tool described in the article it's possible now to simulate Nothing-at-Stake attack, e.g. by mixing nodes doing blockchain forging with
"attacker" doing multibranch forging. We'll publish our work on that soon.



Conclusion & Further Work
-------------------------
Well, it's the last article in the "Inside a Proof-of-Stake Cryptocurrency" series. All of them:

* [Part 1: Basic Structures](http://chepurnoy.org/blog/2014/10/inside-a-proof-of-stake-cryptocurrency-part-1/)

* [Part 2: Forging Algorithm](http://chepurnoy.org/blog/2014/10/inside-a-proof-of-stake-cryptocurrency-part-2/)

* [Part 3: A Local Ledger](Inside a Proof-of-Stake Cryptocurrency Part 3: A Local Ledger)

* [Part 4: The Executable Forging Simulation](http://chepurnoy.org/blog/2014/12/inside-a-proof-of-stake-cryptocurrency-part-4/)

And executable code corresponding is on GitHub: [https://github.com/ConsensusResearch/ForgingSimulation](https://github.com/ConsensusResearch/ForgingSimulation). Some modifications will be made, I want to replace all the constants with configurable parameters, at least. Performance optimization is also needed, for now it's performance isn't good for simplicity's sake.





0 comments on commit 22bb786

Please sign in to comment.