CS143 Computer Networks Final Project
Shuyuan X, Weiru C, Wenlin G
The videos are too LARGE to be uploaded to GitHub. Here is the link https://drive.google.com/file/d/1QBI6_V1lLQ_Oykgiy-FpNqbsWK_u4lkC/view?usp=sharing
Clone the project,
$ git clone https://github.com/WenlinG28/Blockchain-based_Ride_Sharing_System.git
Install the dependencies,
$ cd Blockchain-based_Ride_Sharing_System
$ pip install -r requirements.txt
Start a blockchain node server,
# Windows users can follow this: https://flask.palletsprojects.com/en/1.1.x/cli/#application-discovery
$ export FLASK_APP=node_server.py
$ flask run --port 8000
One instance of our blockchain node is now up and running at port 8000.
Run the application on a different terminal session,
$ python run_app.py
The application should be up and running at http://localhost:5000.
To play around by spinning off multiple custom nodes, use the register_with/
endpoint to register a new node.
Here's a sample scenario that you might wanna try,
# Make sure you set the FLASK_APP environment variable to node_server.py before running these nodes
# already running
$ flask run --port 8000 &
# spinning up new nodes
$ flask run --port 8001 &
$ flask run --port 8002 &
You can use the following cURL requests to register the nodes at port 8001
and 8002
with the already running 8000
.
curl -X POST \
http://127.0.0.1:8001/register_with \
-H 'Content-Type: application/json' \
-d '{"node_address": "http://127.0.0.1:8000"}'
curl -X POST \
http://127.0.0.1:8002/register_with \
-H 'Content-Type: application/json' \
-d '{"node_address": "http://127.0.0.1:8000"}'
This will make the node at port 8000 aware of the nodes at port 8001 and 8002, and make the newer nodes sync the chain with the node 8000, so that they are able to actively participate in the mining process post registration.
To update the node with which the frontend application syncs (default is localhost port 8000), change CONNECTED_NODE_ADDRESS
field in the views.py file.
Once you do all this, you can run the application, create transactions (post messages via the web inteface), and once you mine the transactions, all the nodes in the network will update the chain. The chain of the nodes can also be inspected by inovking /chain
endpoint using cURL.
$ curl -X GET http://localhost:8001/chain
$ curl -X GET http://localhost:8002/chain
Install Python3 and NodeJS v8.9.4 or later for installing truffle
.
Inside this project, install truffle
:
$ npm install truffle
---------------------
# Once the installation process is finished, you should see something like this
+ [email protected]
Then use the the truffle
CLI tool to initialize a smart contract project:
$ ./node_modules/.bin/truffle init
# if truffle was installed globally (npm install -g truffle): $ truffle init
Since we already have smart contracts and unit tests for the smart contracts written in this project, enter N for both ? Overwrite contracts? (y/N)
and ? Overwrite test?
during the initialization process.
The above commands will create the following project structure:
contracts/
: Directory for Solidity contracts source code (.sol
files).migrations/
: Directory for contracts migration files.test/
: Directory for test files. Won't be covered in this tutorial.truffle.js
: Truffle configuration file.
contracts/
and migrations/
folders will already contain a Migration contract and its deploy script (1_initial_migration.js
). This contract is used by truffle to keep track of the migrations of our contracts.
From the project root folder, use truffle
to compile the contract:
$ ./node_modules/.bin/truffle compile
# or just truffle compile if installed globally
Create a file named 2_deploy_contract.js
inside migrations/
folder and add the following content to it:
var Review = artifacts.require("Review");
module.exports = function(deployer) {
deployer.deploy(Review);
};
Then run the following command from the project root folder:
./node_modules/.bin/truffle develop
# or just truffle develop if installed globally
You should see an output similar to the following:
Truffle Develop started at http://127.0.0.1:9545/
Accounts:
(0) 0x89527d9b56ca2f89cfb2dfd4de39f954d5393de7
...
(9) 0xd979bf5139b4ccb16be886a69e6db51ca5e769b9
Private Keys:
...
truffle(develop)>
We can now migrate
our contract, which calls each migration in migrations/
(in order), deploying the contracts to the blockchain.
truffle(develop)> migrate
Starting migrations...
...
2_deploy_contracts.js
=====================
Deploying 'Review'
----------------------
> transaction hash: 0x5dd32e43a668f9357a92eec6dffb57c4b8c312ef6480657bb7736ea758727347
> Blocks: 0 Seconds: 0
> contract address: 0xe2c063B744d02395393d5Aed109C7E887626bcaa
Finally we can test
our contract. You should see an output similar to the following:
truffle(develop)> test
Using network 'develop'.
Compiling your contracts...
===========================
> Compiling ./test/review_test.sol
...
> Compiled successfully using:
- solc: 0.5.16+commit.9c3226ce.Emscripten.clang
ReviewTest
✓ testWriteReviewScore (104ms)
✓ testWriteReviewDescription (99ms)
✓ testShowReviewScore (162ms)
✓ testShowReviewDescription (155ms)
✓ testDisputeDiscard (137ms)
5 passing (6s)