Skip to content
vthglyk edited this page May 12, 2016 · 43 revisions

Installation

  1. Installation of ns3 (https://www.nsnam.org/wiki/Installation). The project has been tested with versions 3.24 and 3.25.
  2. Download rapidjson (https://github.com/miloyip/rapidjson).
  3. Create a new directory named rapidjson under ns-allinone-3.xx/ns-3.xx.
  4. Copy the contents of the directory include/rapidjson (from the downloaded rapidjson project in step 2) to the newly created rapidjson folder.
  5. Update the ns-allinone-3.xx/ns-3.xx/src/applications/wscript.
    • Add the following lines in module.source:
      'model/bitcoin.cc',
      'model/bitcoin-node.cc',
      'model/bitcoin-miner.cc',
      'model/bitcoin-simple-attacker.cc',
      'model/bitcoin-selfish-miner.cc',
      'model/bitcoin-selfish-miner-trials.cc',
      'helper/bitcoin-topology-helper.cc',
      'helper/bitcoin-node-helper.cc',
      'helper/bitcoin-miner-helper.cc'
    • Add the following lines in headers.source:
      'model/bitcoin.h',
      'model/bitcoin-node.h',
      'model/bitcoin-miner.h',
      'model/bitcoin-simple-attacker.h',
      'model/bitcoin-selfish-miner.h',
      'model/bitcoin-selfish-miner-trials.h',
      'helper/bitcoin-topology-helper.h',
      'helper/bitcoin-node-helper.h',
      'helper/bitcoin-miner-helper.h'
  6. Update the ns-allinone-3.xx/ns-3.xx/src/internet/wscript.
    • Add the following line in module.source:
      'helper/ipv4-address-helper-custom.cc'
    • Add the following lines in headers.source:
      'helper/ipv4-address-helper-custom.h'
  7. Copy all the files from Bitcoin-Simulation/src to the respective folder under ns-allinone-3.xx/ns-3.xx/src/
  8. Configure ns3 with the follow command to ensure compatibility and maximum performance. CXXFLAGS="-std=c++11" ./waf configure --build-profile=optimized --out=build/optimized --with-pybindgen=/home/bill/Desktop/workspace/ns-allinone-3.24/pybindgen-0.17.0.post41+ngd10fa60 --enable-mpi --enable-static
  9. Rebuild ns3 using the ./waf command

Project structure

/src/applications/model folder: (the most important files containing all the logic)

  • bitcoin.h/.c: definitions of block class and blockchain class
  • bitcoin-node.h/.c: implementation of a simple bitcoin node
  • bitcoin-miner.h/.c: implementation of the miner (inherits from bitcoin-node)
  • bitcoin-selfish-miner.h/.c: implementation of the a selfish miner attacker which can also attempt a double spending attack

/src/applications/helper folder:

  • bitcoin-node-helper/bicoin-miner-helper.h/.c : it helps setting up each bitcoin node/miner (it is a common practice in ns3 to have helper classes). So, for example it creates the object and propagates to it information regarding the addresses of peers, downloading/uploading speeds, protocol etc..
  • bitcoin-topology-helper : constructs the bitcoin topology (creates the nodes, the links between them, sets up the location of the nodes/miners, latencies and downloading/uploading speeds (some of these are later used from the bitcoin-node-helper/bicoin-miner-helper to set up the applications).

/src/scratch/bitcoin-test.c: The actual simulation file which uses the above helpers to set up the simulation and collect stats. The simulation is configured by using the following runtime arguments:

--blockSize: The the fixed block size (Bytes) [Default: -1]. Note: If the default value is used then the blockSize follows the real bitcoin block size distribution as estimated by collecting stats from https://blockchain.info/.
--noBlocks: The number of generated blocks [Default: 100]
--nodes: The total number of nodes in the network [Default: 16] Note: The number of nodes should always be greater of equal to the number of miners. The number of miners, their hash rates and their locations can be changed by modifying the variables bitcoinMinersHash/litecoinMinersHash/dogecoinMinersHash and bitcoinMinersRegions/litecoinMinersRegions/dogecoinMinersRegions.
--minConnections: The minConnectionsPerNode of the grid [Default: -1]
--maxConnections: The maxConnectionsPerNode of the grid [Default: -1] Note: If minConnections <= 0 or maxConnections <=0, the nodes follow the connections distribution as described in https://cs.umd.edu/projects/coinscope/coinscope.pdf.
--blockIntervalMinutes: The average block generation interval in minutes [Default: 10]
--invTimeoutMins: The inv block timeout [Default: -1] Note: If the default value is used, the timeouts are twice as long as blockIntervalMinutes.
--unsolicited: Change the miners block broadcast type to UNSOLICITED [Default: false] Note: Each newly mined block is broadcast immediately to all the peers of the miner who mined it.
--relayNetwork: Change the miners block broadcast type to RELAY_NETWORK [Default: false] Note: The miners use a relay network for communicating among themselves and send compressed blocks.
--unsolicitedRelayNetwork: Change the miners block broadcast type to UNSOLICITED_RELAY_NETWORK [Default: false] Note: The miners use a relay network among themselves and send compressed blocks and each newly mined block is broadcast immediately to all the peers of the miner who mined it.
--sendheaders: Change the protocol to sendheaders [Default: false]
--litecoin: Imitate the litecoin network behaviour [Default: false] Note: Sets the --nodes=1000, --blockIntervalMinutes=2.5, follows the same connection distribution as bitcoin, but the litecoin distribution for the block generation interval and block size.
--dogecoin: Imitate the litecoin network behaviour [Default: false] Note: Sets the --nodes=650, --blockIntervalMinutes=1, follows the same connection distribution as bitcoin, but the dogecoin distribution for the block generation interval and block size.
--blockTorrent: Enable the BlockTorrent protocol [Default: false]
--chunkSize: The chunksize of the blockTorrent in Bytes [Default: -1] Note: Used only in conjuction with --blockTorrent.
--spv: Enable the spv mechanism in blockTorrent[Default: false] Note: Used only in conjuction with --blockTorrent. The nodes are able to advertise chunks of blocks which are not yet validated.

Example:
If we want to start the simulation for 1000 blocks in LITECOIN mode and with a block size of 2MB we have to enter the following command:
./waf --run "bitcoin-test --noBlocks=1000 --litecoin --blockSize=2000000"

/src/scratch/selfish-miner-test.c: The file simulating the optimal strategy for selfish mining and double-spending attack. The simulation is configured by using the following runtime arguments:

--blockIntervalMinutes: The average block generation interval in minutes [Default: 10]
--noBlocks: The number of generated blocks [Default: 100]
--iterations: The number of iterations of the attack [Default: 1]
--ud: The transaction value which is double-spent [Default: 0] Note: If ud = 0, the attacker follows the optimal strategy for selfish-mining. Otherwise, the attacker follows the optimal strategy for performing a double-spending attack of a transaction with value equals to ud.
--r: The stale block rate [Default: 0]
--unsolicited: Change the miners block broadcast type to UNSOLICITED [Default: false] Note: Each newly mined block is broadcast immediately to all the peers of the miner who mined it.
--relayNetwork: Change the miners block broadcast type to RELAY_NETWORK [Default: false] Note: The miners use a relay network for communicating among themselves and send compressed blocks.
--unsolicitedRelayNetwork: Change the miners block broadcast type to UNSOLICITED_RELAY_NETWORK [Default: false] Note: The miners use a relay network among themselves and send compressed blocks and each newly mined block is broadcast immediately to all the peers of the miner who mined it.

Other notes:

  • The number of miners, their hash rates and their locations can be changed by modifying the variables minersHash and minersRegions. The attacker's hash rate is always the last value in minersHash.
  • To modify the optimal strategy, the m_decisionMatrix in bitcoin-selfish-miner.h must be updated accordingly.
Clone this wiki locally