Skip to content
Snippets Groups Projects
Commit 5c6d5685 authored by DETCHART Jonathan's avatar DETCHART Jonathan
Browse files

Revert "Merge branch 'get_record_fix' into 'master'"

parent 50610e3e
Branches
Tags
1 merge request!7Revert "Merge branch 'get_record_fix' into 'master'"
......@@ -26,9 +26,6 @@ axum-macros = "0.4.1"
rand = "0.8.5"
ark-poly = "0.4.2"
serde_json = "1.0.116"
bs58 = "0.5.1"
rs_merkle = "1.4.2"
resolve-path = "0.1.0"
[dependencies.libp2p]
default-features = false
......
......@@ -161,6 +161,30 @@ export def add-file [
$"add-file/($key)/($content)" | run-command $node
}
export def put-record [
block_hash: string,
block_dir: string,
--node: string = $DEFAULT_IP
]: nothing -> any {
log debug $"putting record ($block_dir)/($block_hash) into ($node)"
$"put-record/($block_hash)/($block_dir | slash replace)" | run-command $node
}
export def get-record [
key: string,
--output: string,
--node: string = $DEFAULT_IP
]: nothing -> any {
log debug $"getting record with key ($key) from ($node)"
let result = $"get-record/($key)" | run-command $node
if $output != null {
$result | bytes from_int out> $output
} else {
$result
}
}
export def decode-blocks [
block_dir: string,
block_hashes: list<string>,
......@@ -188,16 +212,6 @@ export def encode-file [
$"encode-file/($list_args | str join '/')" | run-command $node
}
export def get-block-from [
peer_id_base_58: string,
file_hash: string,
block_hash: string,
--node: string = $DEFAULT_IP
] nothing -> any {
log debug $"get block ($block_hash) part of file ($file_hash) from peer ($peer_id_base_58)"
$"get-block-from/($peer_id_base_58)/($file_hash)/($block_hash)" | run-command $node | bytes from_int
}
def "slash replace" [] string -> string {
$in | str replace --all '/' '%2F'
}
......@@ -3,7 +3,6 @@
use axum::extract::{Path, State};
use axum::http::StatusCode;
use axum::response::{IntoResponse, Json, Response};
use bs58;
use futures::channel::oneshot::{self, Canceled};
use futures::SinkExt;
use libp2p::swarm::NetworkInfo;
......@@ -63,11 +62,11 @@ pub(crate) enum DragoonCommand {
multiaddr: String,
sender: Sender<()>,
},
// DragoonGet {
// peerid: String,
// key: String,
// sender: Sender<Vec<u8>>,
// },
DragoonGet {
peerid: String,
key: String,
sender: Sender<Vec<u8>>,
},
DragoonPeers {
sender: Sender<HashSet<PeerId>>,
},
......@@ -84,13 +83,7 @@ pub(crate) enum DragoonCommand {
encode_mat_k: usize,
encode_mat_n: usize,
powers_path: String,
sender: Sender<(String, String)>,
},
GetBlockFrom {
peer_id: PeerId,
file_hash: String,
block_hash: String,
sender: Sender<Vec<u8>>,
sender: Sender<String>,
},
GetConnectedPeers {
sender: Sender<Vec<PeerId>>,
......@@ -106,12 +99,21 @@ pub(crate) enum DragoonCommand {
},
GetProviders {
key: String,
sender: Sender<Vec<PeerId>>,
sender: Sender<HashSet<PeerId>>,
},
GetRecord {
key: String,
sender: Sender<Vec<u8>>,
},
Listen {
multiaddr: String,
sender: Sender<u64>,
},
PutRecord {
block_hash: String,
block_dir: String,
sender: Sender<()>,
},
RemoveListener {
listener_id: u64,
sender: Sender<bool>,
......@@ -129,17 +131,18 @@ impl std::fmt::Display for DragoonCommand {
DragoonCommand::Bootstrap { .. } => write!(f, "bootstrap"),
DragoonCommand::DecodeBlocks { .. } => write!(f, "decode-blocks"),
DragoonCommand::Dial { .. } => write!(f, "dial"),
//DragoonCommand::DragoonGet { .. } => write!(f, "dragoon-get"),
DragoonCommand::DragoonGet { .. } => write!(f, "dragoon-get"),
DragoonCommand::DragoonPeers { .. } => write!(f, "dragoon-peers"),
DragoonCommand::DragoonSend { .. } => write!(f, "dragoon-send"),
DragoonCommand::EncodeFile { .. } => write!(f, "encode-file"),
DragoonCommand::GetBlockFrom { .. } => write!(f, "get-block-from"),
DragoonCommand::GetConnectedPeers { .. } => write!(f, "get-connected-peers"),
DragoonCommand::GetListeners { .. } => write!(f, "get-listener"),
DragoonCommand::GetPeerId { .. } => write!(f, "get-peer-id"),
DragoonCommand::GetNetworkInfo { .. } => write!(f, "get-network-info"),
DragoonCommand::GetProviders { .. } => write!(f, "get-providers"),
DragoonCommand::GetRecord { .. } => write!(f, "get-record"),
DragoonCommand::Listen { .. } => write!(f, "listen"),
DragoonCommand::PutRecord { .. } => write!(f, "put-record"),
DragoonCommand::RemoveListener { .. } => write!(f, "remove-listener"),
DragoonCommand::StartProvide { .. } => write!(f, "start-provide"),
}
......@@ -233,13 +236,13 @@ pub(crate) async fn create_cmd_dial(
dragoon_command!(state, Dial, multiaddr)
}
// pub(crate) async fn create_cmd_dragoon_get(
// Path((peerid, key)): Path<(String, String)>,
// State(state): State<Arc<AppState>>,
// ) -> Response {
// info!("running command `dragoon_get`");
// dragoon_command!(state, DragoonGet, peerid, key)
// }
pub(crate) async fn create_cmd_dragoon_get(
Path((peerid, key)): Path<(String, String)>,
State(state): State<Arc<AppState>>,
) -> Response {
info!("running command `dragoon_get`");
dragoon_command!(state, DragoonGet, peerid, key)
}
pub(crate) async fn create_cmd_dragoon_peers(State(state): State<Arc<AppState>>) -> Response {
info!("running command `dragoon_peers`");
......@@ -271,16 +274,6 @@ pub(crate) async fn create_cmd_encode_file(
)
}
pub(crate) async fn create_cmd_get_block_from(
Path((peer_id_base_58, file_hash, block_hash)): Path<(String, String, String)>,
State(state): State<Arc<AppState>>,
) -> Response {
info!("running command `get_from_from`");
let bytes = bs58::decode(peer_id_base_58).into_vec().unwrap();
let peer_id = PeerId::from_bytes(&bytes).unwrap();
dragoon_command!(state, GetBlockFrom, peer_id, file_hash, block_hash)
}
pub(crate) async fn create_cmd_get_connected_peers(State(state): State<Arc<AppState>>) -> Response {
info!("running command `get_connected_peers`");
dragoon_command!(state, GetConnectedPeers)
......@@ -338,6 +331,14 @@ pub(crate) async fn create_cmd_get_network_info(State(state): State<Arc<AppState
dragoon_command!(state, GetNetworkInfo)
}
pub(crate) async fn create_cmd_get_record(
Path(key): Path<String>,
State(state): State<Arc<AppState>>,
) -> Response {
info!("running command `get_record`");
dragoon_command!(state, GetRecord, key)
}
pub(crate) async fn create_cmd_listen(
Path(multiaddr): Path<String>,
State(state): State<Arc<AppState>>,
......@@ -346,6 +347,14 @@ pub(crate) async fn create_cmd_listen(
dragoon_command!(state, Listen, multiaddr)
}
pub(crate) async fn create_cmd_put_record(
Path((block_hash, block_dir)): Path<(String, String)>,
State(state): State<Arc<AppState>>,
) -> Response {
info!("running command `put_record`");
dragoon_command!(state, PutRecord, block_hash, block_dir)
}
pub(crate) async fn create_cmd_remove_listener(
Path(listener_id): Path<u64>,
State(state): State<Arc<AppState>>,
......
......@@ -24,7 +24,7 @@ use komodo::{fs, Block};
use anyhow::Result;
use std::path::PathBuf;
use std::path::Path;
use tracing::info;
#[derive(Debug)]
......@@ -45,6 +45,7 @@ where
#[derive(Debug)]
pub(crate) enum Failure {
Timeout,
Unsupported,
Other { error: anyhow::Error },
}
......@@ -59,6 +60,7 @@ impl fmt::Display for Failure {
match self {
Failure::Timeout => f.write_str("Protocol timeout"),
Failure::Other { error } => write!(f, "Protocol error: {error}"),
Failure::Unsupported => write!(f, "Protocol not supported"),
}
}
}
......@@ -68,6 +70,7 @@ impl Error for Failure {
match self {
Failure::Timeout => None,
Failure::Other { error } => Some(&**error),
Failure::Unsupported => None,
}
}
}
......@@ -476,21 +479,6 @@ where
}
}
pub(crate) fn read_block_from_disk<F, G>(block_hash: String, block_dir: PathBuf) -> Result<Vec<u8>>
where
F: PrimeField,
G: CurveGroup<ScalarField = F>,
{
let block =
match fs::read_blocks::<F, G>(&[block_hash], &block_dir, Compress::Yes, Validate::Yes) {
Ok(vec) => vec[0].clone().1,
Err(e) => return Err(e), // ? would it be better to return the error
};
let mut buf = vec![0; block.serialized_size(Compress::Yes)];
block.serialize_with_mode(&mut buf[..], Compress::Yes)?;
Ok(buf)
}
pub(crate) async fn dragoon_send_data<S, F, G>(
mut stream: S,
block_hash: String,
......@@ -501,7 +489,17 @@ where
F: PrimeField,
G: CurveGroup<ScalarField = F>,
{
let buf = read_block_from_disk::<F, G>(block_hash, PathBuf::from(block_dir))?;
let block = match fs::read_blocks::<F, G>(
&[block_hash],
Path::new(&block_dir),
Compress::Yes,
Validate::Yes,
) {
Ok(vec) => vec[0].clone().1,
Err(_) => panic!("Could not read block from disk"), // ? would it be better to return the error
};
let mut buf = vec![0; block.serialized_size(Compress::Yes)];
block.serialize_with_mode(&mut buf[..], Compress::Yes)?;
stream.write_all(&buf[..]).await?;
stream.flush().await?;
let mut recv_payload = [0u8; 4];
......
This diff is collapsed.
......@@ -54,15 +54,20 @@ pub(crate) async fn main() -> Result<(), Box<dyn Error>> {
get(commands::create_cmd_get_providers),
)
.route("/bootstrap", get(commands::create_cmd_bootstrap))
.route(
"/put-record/:block_hash/:block_dir",
get(commands::create_cmd_put_record),
)
.route("/get-record/:key", get(commands::create_cmd_get_record))
.route("/dragoon/peers", get(commands::create_cmd_dragoon_peers))
.route(
"/dragoon/send/:peer/:block_hash/:block_path",
get(commands::create_cmd_dragoon_send),
)
// .route(
// "/dragoon/get/:peer/:key",
// get(commands::create_cmd_dragoon_get),
// )
.route(
"/dragoon/get/:peer/:key",
get(commands::create_cmd_dragoon_get),
)
.route(
"/decode-blocks/:block-dir/:block_hashes/:output_filename",
get(commands::create_cmd_decode_blocks),
......@@ -70,8 +75,7 @@ pub(crate) async fn main() -> Result<(), Box<dyn Error>> {
.route(
"/encode-file/:file_path/:replace-blocks/:encoding-method/:encode_mat_k/:encode_mat_n/:powers_path",
get(commands::create_cmd_encode_file),
)
.route("/get-block-from/:peer_id_base_58/:file_hash/:block_hash", get(commands::create_cmd_get_block_from));
);
let router = router.with_state(Arc::new(app::AppState::new(cmd_sender)));
......@@ -93,12 +97,11 @@ pub(crate) async fn main() -> Result<(), Box<dyn Error>> {
tokio::spawn(http_server);
let kp = get_keypair(id);
let peer_id = kp.public().to_peer_id();
info!("IP/port: {}", ip_port);
info!("Peer ID: {} ({})", peer_id, id);
info!("Peer ID: {} ({})", kp.public().to_peer_id(), id);
let swarm = dragoon_network::create_swarm::<Fr, G1Projective>(kp).await?;
let network = DragoonNetwork::new(swarm, cmd_receiver, peer_id, true);
let network = DragoonNetwork::new(swarm, cmd_receiver);
tokio::spawn(network.run::<DensePolynomial<Fr>>());
let shutdown = signal::ctrl_c();
......
......@@ -27,7 +27,7 @@ macro_rules! impl_Convert {
}
// impl convert for all the types that are already Serialize and thus just return themselves
impl_Convert!(for u64, String, bool, &str, Vec<Multiaddr>, Vec<u8>, (String, String));
impl_Convert!(for u64, String, bool, &str, Vec<Multiaddr>, Vec<u8>);
impl ConvertSer for PeerId {
fn convert_ser(&self) -> impl Serialize {
......
......@@ -10,7 +10,7 @@ let sleep_duration = 5sec
mut node_is_setup = false
let output_dir: path = "/tmp/dragoon_test/received_blocks"
let test_file: path = "tests/assets/dragoon_32/dragoon_32x32.png"
let res_filename = "reconstructed_file.png"
let res_filename = "reconstructed_file"
# create the nodes
swarm run --no-shell $SWARM
......@@ -63,40 +63,26 @@ try {
# Encode the file into blocks, put them to a directory named blocks next to the file
print "Node 0 encodes the file into blocks"
let encode_res = app encode-file --node $SWARM.0.ip_port $test_file
let block_hashes = $encode_res.1 | from json #! This is a string not a list, need to convert
let file_hash = $encode_res.0
let result_string = app encode-file --node $SWARM.0.ip_port $test_file #! This is a string not a list, need to convert
print $result_string
let block_hashes = $result_string | from json
print $"The file got cut into blocks, block hashes are: ($block_hashes)"
print $"The hash of the file is: ($file_hash)"
print "\nNode 0 starts providing the blocks"
print "\nNode 0 starts putting blocks into records"
for $hash in $block_hashes {
print $"Node 0 starts providing the block with hash ($hash)"
app start-provide --node $SWARM.0.ip_port $hash
print $"Node 0 provides a record for the block with hash ($hash)"
app put-record --node $SWARM.0.ip_port $hash $"($test_file | path dirname)/blocks"
}
print "Node 0 finished announcing which blocks it provides\n"
print "Node 0 finished putting blocks into records\n"
print "Node 1 starts searching for the providers with the hashes of the blocks"
mut providers = []
print "Node 1 starts searching for the blocks with the hashes"
for $hash in $block_hashes {
print $"Node 1 searching for the provider of the block with hash ($hash)"
$providers = ($providers | append (app get-providers --node $SWARM.1.ip_port $hash | get 0))
sleep 5sec
print $"Node 1 getting the record for block with hash ($hash)"
app get-record --node $SWARM.1.ip_port --output $"($output_dir)/($hash)" $hash
}
print "Node 1 finished searching for the providers\n"
print $"The providers are:"
print $providers
print "Creating the directory to receive the files"
mkdir $output_dir
print "\nNode 1 ask for the blocks to node 0"
for $i in 0..<($block_hashes | length) {
let $hash = $block_hashes | get $i
print $"Getting block ($hash)"
app get-block-from --node $SWARM.1.ip_port ($providers | get $i) $file_hash ($hash) | save $"($output_dir)/($hash)"
}
print "Finished getting all the blocks\n"
print "Node 1 finished searching for the blocks\n"
print "Node 1 reconstructs the file with the blocks"
app decode-blocks --node $SWARM.1.ip_port $output_dir $block_hashes $res_filename
......@@ -104,14 +90,8 @@ try {
print "Killing the swarm"
swarm kill --no-shell
print "Checking the difference between the original and reconstructed file"
let difference = diff $"($output_dir)/../($res_filename)" $test_file
if $difference == "" {
print "Test successful !"
} else {
print "test failed, there was a difference between the files"
error make {msg: "Exit to catch"}
}
let difference = diff $"($output_dir)/($res_filename)" $test_file
assert equal $difference ""
} catch {
print "Killing the swarm"
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment