Skip to content

Commit

Permalink
Merge pull request #27 from GeoDecConsensus/feat/stake
Browse files Browse the repository at this point in the history
feat(cometbft): configurable stake
  • Loading branch information
namn-grg authored Jun 23, 2024
2 parents d7364d3 + 734f5fc commit 9a16815
Show file tree
Hide file tree
Showing 10 changed files with 313 additions and 74 deletions.
10 changes: 6 additions & 4 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -137,10 +137,12 @@ dmypy.json
# Cython debug symbols
cython_debug/

mytestnet/
node
client

mytestnet/
persistent_peer.txt

node
client
frontend/node_modules
frontend/node_modules

.*.json
2 changes: 1 addition & 1 deletion benchmark/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def clean_node_config(i):
@staticmethod
def cleanup():
return (
f'rm -r .db-* ; rm .*.json ; mkdir -p {PathMaker.results_path()}'
f'rm -r .db-* ; rm .*.json ; rm -r mytestnet; mkdir -p {PathMaker.results_path()}'
)

@staticmethod
Expand Down
29 changes: 14 additions & 15 deletions benchmark/remote.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
# import pandas as pd

from benchmark.config import Committee, Key, NodeParameters, BenchParameters, ConfigError
from benchmark.utils import BenchError, Print, PathMaker, progress_bar
from benchmark.utils import BenchError, Print, PathMaker, progress_bar, set_weight_cometbft
from benchmark.commands import CommandMaker
from benchmark.instance import InstanceManager
from benchmark.geodec import GeoDec
Expand Down Expand Up @@ -140,7 +140,11 @@ def _update(self, hosts):

def _config(self, hosts, node_parameters, bench_parameters=None):
Print.info('Generating configuration files...')


# Cleanup all local configuration files.
cmd = CommandMaker.cleanup()
subprocess.run([cmd], shell=True, stderr=subprocess.DEVNULL)

if self.mechanism.name == 'cometbft':
# Cleanup node configuration files on hosts
for i, host in enumerate(hosts):
Expand All @@ -158,38 +162,33 @@ def _config(self, hosts, node_parameters, bench_parameters=None):
f.close()

# Create testnet config files
# cmd = [f'~/cometbft testnet --v {len(hosts)} --config ~/geodec/testdata/cometbft-config.toml']
cmd = [f'~/cometbft testnet --v {len(hosts)}']
# cmd = [f'~/cometbft testnet --v {len(hosts)} --config ~/geodec/testdata/cometbft-config.toml'] # NOTE custom configuration
subprocess.run(cmd, shell=True, stdout=subprocess.DEVNULL)

# Update the stake weights in the configuration file
set_weight_cometbft(self.settings.geo_input)

# Run the bash file and store the ouput in this file
cmd = [
# 'chmod u+x ./persistent.sh',
f'./persistent.sh {hosts_string}'
]
subprocess.run(cmd, shell=True)

# NOTE Upload configuration files.
# Upload configuration files.
progress = progress_bar(hosts, prefix='Uploading config files:')
for i, host in enumerate(hosts):
# Print.info("Sent node config file to " + host)
# NOTE: Path of the node config files
cmd = [f'scp -i {self.settings.key_path} -r ~/geodec/mytestnet/node{i} ubuntu@{host}:~/']
cmd = [f'scp -i {self.settings.key_path} -r ~/geodec/mytestnet/node{i} ubuntu@{host}:~/'] # NOTE Path of the node config files
subprocess.run(cmd, shell=True, stdout=subprocess.DEVNULL)

else:
# Cleanup all local configuration files.
cmd = CommandMaker.cleanup()
subprocess.run([cmd], shell=True, stderr=subprocess.DEVNULL)

# Recompile the latest code.
cmd = CommandMaker.compile().split()
subprocess.run(cmd, check=True, cwd=PathMaker.node_crate_path(self.settings.repo_name))
# subprocess.run(cmd, check=True, cwd=PathMaker.node_crate_path('hotstuff'))

# Create alias for the client and nodes binary.
cmd = CommandMaker.alias_binaries(PathMaker.binary_path(self.settings.repo_name), self.mechanism.name)
# cmd = CommandMaker.alias_binaries(PathMaker.binary_path('hotstuff'), self.settings.repo_name)
subprocess.run([cmd], shell=True)

# Generate configuration files.
Expand Down Expand Up @@ -287,9 +286,9 @@ def _run_single(self, hosts, rate, bench_parameters, node_parameters, debug=Fals
# Print.info("Persistent Peers: " + persistent_peers)

# Run the clients
# committee = Committee.load(PathMaker.committee_file()) # TODO for cometbft
# committee = Committee.load(PathMaker.committee_file()) # TODO for cometbft
addresses = [f'{x}:{self.settings.ports["front"]}' for x in hosts]
# rate_share = ceil(rate / committee.size()) # TODO Take faults into account.
# rate_share = ceil(rate / committee.size()) # TODO Take faults into account.
rate_share = ceil(rate / len(hosts))
duration = bench_parameters.duration # Duration for which the client should run
client_logs = [PathMaker.client_log_file(i) for i in range(len(hosts))]
Expand Down
25 changes: 25 additions & 0 deletions benchmark/utils.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import csv
import json
from os.path import join


Expand Down Expand Up @@ -165,3 +167,26 @@ def printProgressBar(iteration):
yield item
printProgressBar(i + 1)
print()

def set_weight_cometbft(geo_input_file):
# Get the stake values
stakes = []
with open(geo_input_file, mode='r') as file:
csv_reader = csv.DictReader(file)
for row in csv_reader:
if row['stake'] and row['id']:
stakes.append(row['stake'])

def get_path(i):
return './mytestnet/node{i}/config/genesis.json'.format(i=i)

# Set the weights
for i in range(len(stakes)):
path = get_path(i)
with open(path, 'r') as file:
data = json.load(file)
for j, validator in enumerate(data['validators']):
validator['power'] = stakes[j] # New value for the "power" field

with open(path, 'w') as file:
json.dump(data, file, indent=4)
4 changes: 2 additions & 2 deletions fab-params.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@
"bench_params": {
"faults": 0,
"nodes": [4],
"rate": [10000],
"rate": [1000],
"tx_size": 256,
"duration": 100,
"duration": 60,
"runs": 1
},
"node_params": {
Expand Down
43 changes: 28 additions & 15 deletions frontend/public/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -42,32 +42,45 @@
cursor: pointer;
font-size: 16px;
}
#selected-locations {
padding: 10px;
background-color: #282828;
color: #ffffff;
overflow-y: auto;
max-height: 50vh;
#selected-locations-table {
width: 100%;
border-collapse: collapse;
}
.location-entry {
margin-bottom: 10px;
#selected-locations-table th,
#selected-locations-table td {
border: 1px solid #0ff;
padding: 8px;
text-align: center;
}
.location-entry label {
margin-right: 10px;
#selected-locations-table th {
background-color: #282828;
color: #0ff;
}
.location-entry input {
width: 50px;
#selected-locations-table td {
background-color: #1b1b1b;
color: #ffffff;
}
</style>
</head>
<body>
<div id="map"></div>
<div class="controls">
<button class="button" onclick="saveSelected()">Save Selected Coordinates</button>
Download Geo Input
<button class="button" onclick="saveSelected()">
Save Selected Coordinates
</button>
</div>
<div id="selected-locations"></div>
<div id="selected-locations-container">
<table id="selected-locations-table">
<thead>
<tr>
<th>Name</th>
<th>Count</th>
<th>Stake</th>
</tr>
</thead>
<tbody id="selected-locations"></tbody>
</table>
</div>

<script src="https://unpkg.com/leaflet/dist/leaflet.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/PapaParse/5.3.0/papaparse.min.js"></script>
Expand Down
64 changes: 32 additions & 32 deletions frontend/public/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ document.addEventListener("DOMContentLoaded", function () {
)
if (index === -1) {
place.count = 1 // Initialize count
place.stake = 1 // Initialize stake to 1
selectedCoordinates.push(place)
addLocationEntry(place)

Expand All @@ -58,46 +59,44 @@ document.addEventListener("DOMContentLoaded", function () {
})

function addLocationEntry(place) {
const entryDiv = document.createElement("div")
entryDiv.className = "location-entry"
entryDiv.dataset.latitude = place.latitude
entryDiv.dataset.longitude = place.longitude
const row = document.createElement("tr")
row.className = "location-entry"
row.dataset.latitude = place.latitude
row.dataset.longitude = place.longitude

const label = document.createElement("label")
label.textContent = place.name
const nameCell = document.createElement("td")
nameCell.textContent = place.name

const input = document.createElement("input")
input.type = "number"
input.value = place.count ? place.count : 1
input.addEventListener("input", function () {
place.count = parseInt(input.value, 10)
const marker = markers.find(
(m) =>
m.getLatLng().lat === place.latitude &&
m.getLatLng().lng === place.longitude
)
if (marker) {
marker
.bindTooltip(`${place.name} (Count: ${place.count})`, {
permanent: true,
direction: "right",
className: "neon-text",
})
.openTooltip()
}
const countCell = document.createElement("td")
const countInput = document.createElement("input")
countInput.type = "number"
countInput.value = place.count
countInput.addEventListener("input", function () {
place.count = parseInt(countInput.value, 10)
})
countCell.appendChild(countInput)

const stakeCell = document.createElement("td")
const stakeInput = document.createElement("input")
stakeInput.type = "number"
stakeInput.value = place.stake
stakeInput.addEventListener("input", function () {
place.stake = parseInt(stakeInput.value, 10)
})
stakeCell.appendChild(stakeInput)

entryDiv.appendChild(label)
entryDiv.appendChild(input)
selectedLocationsContainer.appendChild(entryDiv)
row.appendChild(nameCell)
row.appendChild(countCell)
row.appendChild(stakeCell)
selectedLocationsContainer.appendChild(row)
}

function removeLocationEntry(place) {
const entryDiv = selectedLocationsContainer.querySelector(
`.location-entry[data-latitude='${place.latitude}'][data-longitude='${place.longitude}']`
const row = selectedLocationsContainer.querySelector(
`tr.location-entry[data-latitude='${place.latitude}'][data-longitude='${place.longitude}']`
)
if (entryDiv) {
selectedLocationsContainer.removeChild(entryDiv)
if (row) {
selectedLocationsContainer.removeChild(row)
}
}

Expand All @@ -116,6 +115,7 @@ document.addEventListener("DOMContentLoaded", function () {
document.body.removeChild(link)
}
}

window.saveSelected = function () {
fetch("/save-coordinates", {
method: "POST",
Expand Down
Loading

0 comments on commit 9a16815

Please # to comment.