Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[event_graph/python] add event_graph python bindings #286

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
641 changes: 289 additions & 352 deletions Cargo.lock

Large diffs are not rendered by default.

5 changes: 3 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ members = [
"bin/lilith",

"src/sdk",
"src/sdk/python",

#"src/sdk/python",
"src/event_graph/python",
#"src/serial",
#"src/serial/derive",
#"src/serial/derive-internal",
Expand All @@ -48,6 +48,7 @@ members = [
"src/contract/deployooor",

"example/dchat/dchatd",

]

[dependencies]
Expand Down
7 changes: 7 additions & 0 deletions script/event_graph_bindings/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# build and install darkfi-eventgraph-py

follow instructions in `src/event_graph/python/README.md`

# run example

make sure the environment under `src/event_graph/python` is activated, thhen run the example python scripts under this directory
148 changes: 148 additions & 0 deletions script/event_graph_bindings/broadcast_event.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
from darkfi_eventgraph_py import event_graph as eg, p2p, sled
import asyncio
import random
import time
import subprocess
import os
# number of nodes
N = 2
P2PDATASTORE_PATH = '/tmp/p2pdatastore'
SLED_DB_PATH = '/tmp/sleddb'
os.system("rm -rf " + P2PDATASTORE_PATH+"*")
os.system("rm -rf " + SLED_DB_PATH+"*")

def get_random_node_idx():
return int(random.random()*N)

async def start_p2p(node):
await p2p.start_p2p(node)

async def get_fut_p2p(settings):
return await p2p.new_p2p(settings)

async def get_fut_eg(node, sled_db):
return await eg.new_event_graph(node, sled_db, P2PDATASTORE_PATH, False, 'dag', 1)

async def register_protocol(p2p_node, eg_node):
await p2p.register_protocol_p2p(p2p_node, eg_node)
# create p2p node
def new_nodes(starting_port=13200):
p2ps = []
event_graphs = []
for i in range(0, N):
node_id = ''
inbound_port = starting_port + i
external_port = starting_port + i
inbound_addrs = [p2p.Url("tcp://127.0.0.1:{}".format(inbound_port))]
external_addrs = []
peers = [p2p.Url("tcp://127.0.0.1:{}".format(starting_port+j)) for j in range(0,N) if j!=i]
seeds = []
app_version = p2p.new_version(0, 1, 1, '')
allowed_transports = ['tcp']
transport_mixing = False
outbound_connections = 0#N
inbound_connections = 8
outbound_connect_timeout = 15
channel_handshake_timeout = 10
channel_heartbeat_interval = 30
localnet = True
outbound_peer_discovery_cooloff_time = 30
outbound_peer_discovery_attempt_time = 5
p2p_datastore = P2PDATASTORE_PATH+'{}'.format(0)
hostlist = ''
greylist_refinery_internval = 15
white_connect_percnet = 70
gold_connect_count = 2
slot_preference_strict = False
time_with_no_connections = 30
blacklist = []
ban_policy = p2p.get_strict_banpolicy()
settings = p2p.new_settings(
node_id,
inbound_addrs,
external_addrs,
peers,
seeds,
app_version,
allowed_transports,
transport_mixing,
outbound_connections,
inbound_connections,
outbound_connect_timeout,
channel_handshake_timeout,
channel_heartbeat_interval,
localnet,
outbound_peer_discovery_cooloff_time,
outbound_peer_discovery_attempt_time,
p2p_datastore,
hostlist,
greylist_refinery_internval,
white_connect_percnet,
gold_connect_count,
slot_preference_strict,
time_with_no_connections,
blacklist,
ban_policy
)
p2p_ptr = asyncio.run(get_fut_p2p(settings))
sled_db = sled.SledDb(SLED_DB_PATH+'{}'.format(i))
event_graph = asyncio.run(get_fut_eg(p2p_ptr, sled_db))
# register event graph protocol
asyncio.run(register_protocol(p2p_ptr, event_graph))
# start p2p node
asyncio.run(start_p2p(p2p_ptr))
event_graphs+=[event_graph]
p2ps+=[p2p_ptr]
return (p2ps, event_graphs)

async def create_new_event(data, event_graph_ptr):
return await eg.new_event(data, event_graph_ptr)

async def insert_events(node, event):
ids = await node.dag_insert(event)
return ids

async def broadcast_event_onp2p(p2p_node, event):
await p2p.broadcast_p2p(p2p_node, event)

async def get_event_by_id(event_graph, event_id):
return await event_graph.dag_get(event_id)

async def dag_sync(node):
await node.dag_sync()

# create N nodes
p2ps, egs = new_nodes()

# select random node
rnd_idx = get_random_node_idx()
random_node = egs[rnd_idx]
print('random node of index {} was selected: {}'.format(rnd_idx, egs[rnd_idx]))

for evg in egs:
assert(evg.dag_len()==1)

# create new event
event = asyncio.run(create_new_event([1,2,3], random_node))
print("event: {}".format(event))

# insert event at random node
ids = asyncio.run(insert_events(random_node, [event]))
print("dag id: {}".format(ids[0]))

# broadcast the new event
random_node_p2p = p2ps[rnd_idx]
asyncio.run(broadcast_event_onp2p(random_node_p2p, event))

# dag sync
#for eg in egs:
# asyncio.run(dag_sync(eg))

# get broadcasted event
event2 = asyncio.run(get_event_by_id(egs[rnd_idx], ids[0]))
print("broadcasted event: {}".format(event2))

# assert event is broadcast to all nodes
# FIXME
#for evg in egs:
# assert(evg.dag_len()==N+1)
5 changes: 5 additions & 0 deletions src/event_graph/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,11 @@ impl EventGraph {
self.days_rotation
}

/// return dag length necessary for event graph bindings testing.
pub fn dag_len(&self) -> usize {
self.dag.len()
}

/// Sync the DAG from connected peers
pub async fn dag_sync(&self) -> Result<()> {
// We do an optimistic sync where we ask all our connected peers for
Expand Down
72 changes: 72 additions & 0 deletions src/event_graph/python/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/target

# Byte-compiled / optimized / DLL files
__pycache__/
.pytest_cache/
*.py[cod]

# C extensions
*.so

# Distribution / packaging
.Python
.venv/
env/
bin/
build/
develop-eggs/
dist/
eggs/
lib/
lib64/
parts/
sdist/
var/
include/
man/
venv/
*.egg-info/
.installed.cfg
*.egg

# Installer logs
pip-log.txt
pip-delete-this-directory.txt
pip-selfcheck.json

# Unit test / coverage reports
htmlcov/
.tox/
.coverage
.cache
nosetests.xml
coverage.xml

# Translations
*.mo

# Mr Developer
.mr.developer.cfg
.project
.pydevproject

# Rope
.ropeproject

# Django stuff:
*.log
*.pot

.DS_Store

# Sphinx documentation
docs/_build/

# PyCharm
.idea/

# VSCode
.vscode/

# Pyenv
.python-version
36 changes: 36 additions & 0 deletions src/event_graph/python/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
[package]
name = "darkfi-eventgraph-py"
version = "0.1.0"
edition = "2021"
authors = ["Dyne.org foundation <foundation@dyne.org>"]
license = "AGPL-3.0-only"
homepage = "https://dark.fi"
repository = "https://codeberg.org/darkrenaissance/darkfi"

[lib]
name = "darkfi_eventgraph_py"
crate-type = ["cdylib"]
doc = false

[dependencies]
darkfi = { path = "../../..", features = ["net", "event-graph"] }
#darkfi-sdk = {path = "../../sdk"}
sled-overlay = {version = "0.1.4"}

smol = {version = "2.0.2"}
url = {version = "2.5.2", features = ["serde"]}
futures = {version = "0.3.31"}

pyo3 = {version = "0.20.0"}
#pyo3-asyncio = {version = "0.20.0", features = ["attributes", "tokio-runtime", "async-std-runtime"]}
pyo3-asyncio = {version = "0.20.0", features = ["attributes", "async-std-runtime"]}
async-std = "1.9"
semver = "1.0"
blake3 = {version = "1.5.4", features = ["rayon"]}
#pyo3-ffi = {version = "0.22.6"}
#tokio = "1.9"
# Blockchain store


[lints]
workspace = true
12 changes: 12 additions & 0 deletions src/event_graph/python/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
.POSIX:

# Maturin binary
MATURIN = maturin

all:
$(MATURIN) build --release

dev:
$(MATURIN) develop --release

.PHONY: all dev
Loading