Skip to content

snowyukischnee/demo-consensus

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Introduction

A distributed wallet with two operation DEPOSIT and WITHDRAW

Prerequisites

Go 1.13+

Quickstarts

Build

go build main.go

Bootstrap new cluster

Start first node (create a new cluster)

./main -http 127.0.0.1:10000 -consensus 127.0.0.1:20000 -id NODE1 -bootstrap ./NODE1

Wait for first node started then start second & third nodes

./main -http 127.0.0.1:10001 -consensus 127.0.0.1:20001 -id NODE2 -join 127.0.0.1:10000 ./NODE2
./main -http 127.0.0.1:10002 -consensus 127.0.0.1:20002 -id NODE3 -join 127.0.0.1:10001 ./NODE3

Live bootstrap

Create a new node

./main -http 127.0.0.1:10003 -consensus 127.0.0.1:20003 -id NODE4 ./NODE4

Bootstrap the newly created node into existing cluster

curl -i -X POST 127.0.0.1:10001/join -H 'Content-Type:application/json' -d '{"httpAddr":"127.0.0.1:10003", "consensusAddr": "127.0.0.1:20003", "id": "NODE4"}'

Test

Check the balance

curl -X GET 127.0.0.1:10000
curl -X GET 127.0.0.1:10001
curl -X GET 127.0.0.1:10002
5000
5000
5000

deposit money into wallet

curl -i -X POST 127.0.0.1:10002/dw -H 'Content-Type:application/json' -d '{"op":"DEPOSIT", "amount": "525"}'

Then the balance is change on all nodes too

curl -X GET 127.0.0.1:10000
curl -X GET 127.0.0.1:10001
curl -X GET 127.0.0.1:10002
5525
5525
5525

withdraw money from wallet

curl -i -X POST 127.0.0.1:10002/dw -H 'Content-Type:application/json' -d '{"op":"WITHDRAW", "amount": "1725"}'

Get new balance

curl -X GET 127.0.0.1:10000
curl -X GET 127.0.0.1:10001
curl -X GET 127.0.0.1:10002
3800
3800
3800

Usage

Build

go build main.go

Run

./main -http $HTTP_ADDR -consensus $CONSENSUS_ADDR -id $NODE_ID -bootstrap -join $OTHER_ADDR /path/to/store/dir

while

-http $HTTP_ADDR declares service http endpoint (e.g 127.0.0.1:10000)

-consensus $CONSENSUS_ADDR declares consensus service endpoint (e.g 127.0.0.1:20000)

-id $NODE_ID node id (ids in the same cluster must not be duplicated)

-bootstrap to bootstrap new cluster.

  • if bootstrap is set then ignore join option.
  • if bootstrap and join is not set then create a new node (it will not functional)

-join $OTHER_ADDR used to join this newly created node to an existed cluster. (this could be http binding of any node inside cluster)

/path/to/store/dir path to log store used to snapshot & restore if node is up

About

A simple distributed wallet implementation

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages