Skip to content

Commit 24b0b55

Browse files
committed
reorg and init integration with hybrid consensus
1 parent 5b20780 commit 24b0b55

29 files changed

+314
-268
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ Refer to:
44
https://arxiv.org/abs/1805.01457
55
https://eprint.iacr.org/2016/917.pdf
66

7+
## Run
8+
9+
Refer to `trueconsensus/README.md` for further instructions
710

811
### Parameterized by following..
912

trueconsensus/fastchain/README.md renamed to trueconsensus/README.md

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ This is a `Practical Byzantine Fault Tolerance` implementation of Miguel Castro
66

77
#### Configure paths and tunables
88

9-
Fill up the config files `pbft_logistics.cfg` and `pbft_tunables.yaml` or use defaults.
9+
Fill up the config files `conf/pbft_logistics.cfg` and `conf/pbft_tunables.yaml` or use defaults.
1010

1111
#### Install dependencies
1212

@@ -33,19 +33,22 @@ OR
3333
Then proceed as follows:
3434

3535
```
36-
# generate requests_pb2.py from requests.proto file
37-
./proto.sh
36+
# change to folder trueconsensus
37+
cd trueconsensus/
38+
39+
# generate proto/requests_pb2.py from proto/requests.proto file
40+
./utils/generate_proto.sh
3841
3942
# generate asymm keypairs
40-
python make_keys.py
43+
python -m utils.make_keys
4144
4245
# generate requests
43-
./generate_requests_dat.py
46+
python -m utils.generate_requests_dat
4447
```
4548

4649
### Run
4750

48-
Server: `./server.py`
51+
Server: `./engine.py`
4952

5053
Client: `./client.py`
5154

trueconsensus/fastchain/client.py renamed to trueconsensus/client.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111
import socks
1212
# import time
1313

14-
import request_pb2
15-
from config import client_address, \
14+
from proto import request_pb2
15+
from fastchain.config import client_address, \
1616
client_id, \
1717
RL, \
1818
client_logger, \

trueconsensus/dapps/__init__.py

Whitespace-only changes.

trueconsensus/fastchain/bank.py renamed to trueconsensus/dapps/bank.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import proto_message as message
2-
from config import config_general
1+
from proto import proto_message as message
2+
from fastchain.config import config_general
33

44

55
class bank:

trueconsensus/engine.py

100644100755
Lines changed: 152 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,19 +14,164 @@
1414
# See the License for the specific language governing permissions and
1515
# limitations under the License.
1616

17+
import os
18+
import sys
19+
# import yaml
20+
import signal
21+
from datetime import datetime
22+
import socket
23+
# from random import random
24+
from argparse import RawTextHelpFormatter, \
25+
ArgumentParser
26+
27+
from threading import Timer, Thread
28+
29+
from fastchain import node
30+
from fastchain.config import config_yaml, \
31+
threading_enabled, \
32+
_logger, \
33+
N, \
34+
RL
35+
1736
from snailchain import SnailChain
18-
from fastchain.bft import NodeBFT, \
37+
from fastchain.bft_committee import NodeBFT, \
1938
ViewChangeInit, \
2039
LedgerLog, \
21-
BFTcommittee, \
22-
SubProtoDailyBFT, \
40+
BFTcommittee
41+
42+
from fastchain.subprotocol import SubProtoDailyBFT, \
2343
Mempools
2444

45+
parser = ArgumentParser(formatter_class=RawTextHelpFormatter,
46+
description="""PBFT standalone server demo""")
47+
48+
49+
def suicide():
50+
# import pdb; pdb.set_trace()
51+
# sys.exit()
52+
# quit()
53+
os.kill(os.getpid(), signal.SIGINT)
54+
55+
56+
def signal_handler(event, frame):
57+
sys.stdout.write("handling signal: %s\n" % event)
58+
sys.stdout.flush()
59+
_logger.error("Kill signal (%s) detected. Stopping pbft.." % event)
60+
countdown = 3 # seconds
61+
if event == signal.SIGINT:
62+
print("Committing deliberate suicide in %s seconds" % countdown)
63+
t = Timer(countdown, suicide)
64+
t.start()
65+
sys.exit(130) # Ctrl-C for bash
66+
67+
68+
def init_server(id):
69+
global RL
70+
try:
71+
ip, port = RL[id]
72+
except IndexError as E:
73+
quit("%s Ran out of replica list. No more server config to try" % E)
74+
s = socket.socket()
75+
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
76+
# host = socket.gethostname()
77+
host = "0.0.0.0"
78+
s.bind((host, port)) # on EC2 we cannot directly bind on public addr
79+
s.listen(50)
80+
s.setblocking(0)
81+
_logger.debug("Server [%s] -- listening on port %s" % (id, port))
82+
_logger.debug("IP: %s" % ip)
83+
return s
84+
85+
86+
class ThreadedExecution(object):
2587

26-
class DailyOffChainConsensus(object):
2788
def __init__(self):
28-
self.chain = []
29-
self._lambda = None
89+
pass
90+
91+
def run(self, ID):
92+
sys.stdout.write("run started\n")
93+
sys.stdout.flush()
94+
socket_obj = init_server(ID)
95+
n = node.Node(ID, 0, N)
96+
# n.init_keys(N)
97+
n.init_replica_map(socket_obj)
98+
n.server_loop()
99+
sys.stdout.write("run exited\n")
100+
sys.stdout.flush()
101+
102+
def launch(self):
103+
threads = []
104+
for i in range(N):
105+
thread = Thread(target=self.run, args=[i])
106+
thread.start()
107+
threads.append(thread)
30108

31-
def preproess(self):
109+
for thread in threads:
110+
thread.join()
111+
112+
sys.stdout.write("join completed\n")
113+
sys.stdout.flush()
114+
115+
116+
class NonThreadedExecution(object):
117+
'''
118+
Finds sockets that aren't busy and attempts to establish and launch testbed
119+
'''
120+
def __init__(self):
32121
pass
122+
123+
def init_server_socket(self, _id=None):
124+
"""
125+
triggers setup using testbed_config. Increments given server id
126+
if that (ip, socket) from Replica List RL is already in use.
127+
"""
128+
global N
129+
c = _id
130+
while c < N:
131+
s = None
132+
try:
133+
s = init_server(c)
134+
except OSError as E:
135+
_logger.error("%s -- Server ID: [%s]" % (E, c))
136+
c -= 1
137+
if s:
138+
return s, c
139+
140+
def launch(self):
141+
socket_obj, _id = self.init_server_socket(
142+
_id=config_yaml["testbed_config"]["server_id_init"] - 1
143+
)
144+
n = node.Node(_id, 0, N)
145+
# n.init_keys(N)
146+
n.init_replica_map(socket_obj)
147+
n.server_loop()
148+
149+
150+
# def pbft_usage():
151+
# parser.add_argument("-n", "--nodes", dest="node_count", action='store',
152+
# help="# of PBFT nodes to be launched")
153+
# parser.add_argument("-id", "--node-id", dest="node_id",
154+
# action='store_true',
155+
# help="")
156+
# parser.add_argument("-ts", "--tune-settings", dest="tune",
157+
# action='store_true',
158+
# help="")
159+
160+
161+
def main():
162+
print("Start time: ", datetime.now())
163+
print("Threading enabled: ", threading_enabled)
164+
165+
signal.signal(signal.SIGINT, signal_handler)
166+
signal.signal(signal.SIGTERM, signal_handler)
167+
168+
# import pdb; pdb.set_trace()
169+
if threading_enabled:
170+
ThreadedExecution().launch()
171+
else:
172+
NonThreadedExecution().launch()
173+
174+
175+
if __name__ == "__main__":
176+
# import pdb; pdb.set_trace()
177+
main()

trueconsensus/fastchain/__init__.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,5 @@
22
Package import structure
33
'''
44

5-
from fastchain.bft import *
5+
from fastchain.bft_committee import *
66
from fastchain.node import *
7-

trueconsensus/fastchain/bft.py renamed to trueconsensus/fastchain/bft_committee.py

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,20 @@
1919
import os
2020
import uuid
2121
import random
22-
import ecdsa_sig as sig
2322
from db.backends.level import LevelDB
2423

25-
from fastchain.config import LAMBDA, config_yaml
24+
from fastchain import ecdsa_sig as sig
25+
from fastchain.log_maintainer import LedgerLog
26+
from fastchain.config import config_yaml
2627
# from logging import ledger
2728

2829
from fastchain.node import Node
30+
2931
from collections import OrderedDict, \
3032
defaultdict, \
31-
namedtuple
33+
namedtuple # for transaction tuple, use struct?
34+
35+
LAMBDA = config_yaml['bft_committee']['lambda']
3236

3337

3438
def generate_block(genesis=True):
@@ -48,6 +52,15 @@ def generate_txns(R, l):
4852
# return uuid.uuid4().hex
4953

5054

55+
class DailyOffChainConsensus(object):
56+
def __init__(self):
57+
self.chain = []
58+
self._lambda = None
59+
60+
def preproess(self):
61+
pass
62+
63+
5164
class NodeBFT(Node):
5265
'''
5366
@types:
@@ -67,9 +80,13 @@ class NodeBFT(Node):
6780
def __init__(self, id=None, type=None):
6881
self.NodeId = id
6982
self._type = 'BFTmember'
70-
self.new_row = namedtuple('row', ['R','l','txn'])
83+
self.new_row = namedtuple('row', ['R', 'l', 'txn'])
84+
# TODO: maybe use ctypes.Structure or struct.Struct ?
7185
self.nonce = 0
7286

87+
def launch_boot_nodes(self):
88+
return
89+
7390
def log_to_snailchain(self):
7491
return
7592

@@ -109,7 +126,6 @@ def call_to_viewchange(self):
109126
start = time.time()
110127
while true:
111128
response = VC.wait_for_reply()
112-
if
113129
if response is not None:
114130
break
115131
return
@@ -162,3 +178,4 @@ def fork_vbft(self):
162178
pass
163179

164180
def update_mempool_subprotocol(self):
181+
pass

0 commit comments

Comments
 (0)