Skip to content

Commit

Permalink
reqresp rpc (airalab#330)
Browse files Browse the repository at this point in the history
* recover reqres rpc

* reqresp rpc editing

* add reqres rpc integration test

* fixed reqres rpc integration test

* rm reqres rpc integration test

* fix reqres rpc unit test

* add reqres rpc integration test with sudo

* fix testrpc action sript by install protoc compiler

* fix static action sript by install protoc compiler

* fix static action sript for clippy with warnings arg

* edit reqres to pass clippy

* edit println due to clippy check
  • Loading branch information
yamukha authored Feb 13, 2023
1 parent c2cb14b commit e317698
Show file tree
Hide file tree
Showing 10 changed files with 293 additions and 27 deletions.
5 changes: 4 additions & 1 deletion .github/workflows/static.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ jobs:
- name: Checkout the source code
uses: actions/checkout@v2

- name: Install deps
run: sudo apt-get -y install protobuf-compiler

- name: Install Rust toolchain
uses: actions-rs/toolchain@v1
with:
Expand All @@ -40,4 +43,4 @@ jobs:
uses: actions-rs/cargo@v1
with:
command: clippy
args: -- -D warning
args: -- -D warnings
53 changes: 53 additions & 0 deletions .github/workflows/testrpc.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
name: Testing RPC
on:
push:
branches:
- master

jobs:
testrpc:
runs-on: ubuntu-latest
steps:
- name: Checkout the source code
uses: actions/checkout@v2

- name: install dependencies
run: |
sudo apt update
sudo apt-get install cmake coreutils libclang-dev protobuf-compiler -y
- name: Install Rust toolchain
uses: actions-rs/toolchain@v1
with:
toolchain: nightly-2022-08-05
target: ${{ matrix.target }}
components: rustfmt, clippy
default: true

- name: Install WASM target (for runtime building)
shell: bash
run: rustup target add wasm32-unknown-unknown --toolchain nightly-2022-08-05

- name: Build and run reqres example node
shell: bash
run: |
base64 --decode protocol/examples/reqres/QmUnozNz2tmzDeckoShMHbvGp1yWi4BKBiudkELwoKCdcL > private.pk8
cd protocol/examples/reqres
cargo build --release
cd ../../..
target/release/robonomics-request-response-example /ip4/127.0.0.1/tcp/61241 &
- name: Build robonomics binary
uses: actions-rs/cargo@v1
with:
command: build
args: --release

- name: Run robonomics and send test rpc reqres Ping and Get
shell: bash
run: |
cd target/release/
./robonomics --dev &
sleep 20
curl http://localhost:9933 -H "Content-Type:application/json;charset=utf-8" -d '{ "jsonrpc":"2.0", "id":1, "method":"p2p_ping", "params": ["/ip4/127.0.0.1/tcp/61241/QmUnozNz2tmzDeckoShMHbvGp1yWi4BKBiudkELwoKCdcL"] }' | grep -q Pong
curl http://127.0.0.1:9933 -H "Content-Type:application/json;charset=utf-8" -d '{ "jsonrpc":"2.0", "id":1, "method":"p2p_get", "params": ["/ip4/127.0.0.1/tcp/61241/QmUnozNz2tmzDeckoShMHbvGp1yWi4BKBiudkELwoKCdcL","Meaning42"] }' | grep -q Meaning42
3 changes: 3 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file added private.pk8
Binary file not shown.
2 changes: 2 additions & 0 deletions protocol/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,6 @@ libp2p = { version = "0.49", features = [
"tokio",
"rsa",
] }

tokio = { version = "1", features = ["full"] }

Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQDcOnccRMgZ44/DOXK12GRuCfzT
1qQDAiqYPJYe6HM7sreGTuBbZudTVVNtmIrpNcuUjYDXHYUYlm9RIIPHN3mOylo4rduaAmbZIaez
bXYN/fHhPUsAbv/OZIEYhAlmBozHoOxd0En+kDipzgwq7lpbVnbWdRtP5/6F3nRX/p2zM7GB1xWf
adJP7y737PPhGHGoRSwZNwZQqRIQVQEyx1WliJIK83ySPhtl0s4OVR3PK4rkqbcHf2iLLjmbJKw5
1t/bs9m7nMEO/jQ6/zwH4jHEnH0Ui//YRAJ9cEGnIwjh7gCwBiu3BN5Aw+7tvI77gruoxNbKjB8b
UX1Cd9ldvvNfAgMBAAECggEBALP1LnmyFtVY55P+sKa50a89iBTOsy0mKwpFj+Jnz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/iV7a32UQY0Q/i5c+wOr
JeUyC78lGE+gC0UPOWwNG409W+SiGoLt/OMERK49F+Aq9ocs6qXKJfIeisz/F3BPksH4sgymOizY
ZcxDCzdQHavXdGF/4ZWTCXpJ0NFO
45 changes: 27 additions & 18 deletions protocol/src/reqres.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ impl RequestResponseCodec for RobonomicsCodec {
}
// send Pong if somebody try in app logic to obfuscate Ping by sending Data instead of Pong
Response::Data(data) => {
if self.is_ping == false {
if !self.is_ping {
write_length_prefixed(io, data).await
} else {
write_length_prefixed(io, "".as_bytes()).await
Expand All @@ -158,7 +158,7 @@ pub fn mk_transport() -> (PeerId, transport::Boxed<(PeerId, StreamMuxerBox)>) {
let mut peer_id = id_keys.public().to_peer_id();

let f = std::fs::read("private.pk8");
let _ = match f {
match f {
Ok(mut bytes) => {
id_keys = identity::Keypair::rsa_from_pkcs8(&mut bytes).unwrap();
peer_id = id_keys.public().to_peer_id();
Expand All @@ -168,6 +168,7 @@ pub fn mk_transport() -> (PeerId, transport::Boxed<(PeerId, StreamMuxerBox)>) {
};

let transport = TokioTcpTransport::new(GenTcpConfig::default().nodelay(true));

let noise_keys = Keypair::<X25519Spec>::new()
.into_authentic(&id_keys)
.unwrap();
Expand Down Expand Up @@ -200,11 +201,15 @@ pub async fn reqres(

let (peer2_id, trans) = mk_transport();
let ping_proto2 = RequestResponse::new(RobonomicsCodec { is_ping: false }, protocols, cfg);
let mut swarm2 = SwarmBuilder::new(trans, ping_proto2, peer2_id.clone())
.executor(Box::new(|fut| {
tokio::spawn(fut);
}))
.build();
// let mut swarm2 = Swarm::new(trans, ping_proto2, peer2_id.clone());
let mut swarm2 = {
SwarmBuilder::new(trans, ping_proto2, peer2_id)
.executor(Box::new(|fut| {
tokio::spawn(fut);
}))
.build()
};

log::debug!("Local peer 2 id: {:?}", peer2_id);

let addr_remote = address;
Expand All @@ -229,15 +234,15 @@ pub async fn reqres(
String::from_utf8_lossy(&y)
);
}
let req_encoded: Vec<u8> = bincode::serialize(&format!("{}", value).into_bytes()).unwrap();
let req_encoded: Vec<u8> = bincode::serialize(&value.into_bytes()).unwrap();
swarm2
.behaviour_mut()
.send_request(&remote_peer, Request::Get(req_encoded));
} else {
println!("unsuported command {} ", method);
println!("unsuported command {method} ");
}

loop {
let received = loop {
match swarm2.select_next_some().await {
SwarmEvent::ConnectionEstablished { peer_id, .. } => {
log::debug!("Peer2 connected: {:?}", peer_id);
Expand All @@ -257,8 +262,10 @@ pub async fn reqres(
},
} => match response {
Response::Pong => {
log::debug!(" peer2 Resp{} {:?} from {:?}", request_id, &response, peer);
break;
let pong_msg =
format!(" peer2 Resp{} {:?} from {:?}", request_id, &response, peer);
log::debug!("{}", pong_msg);
break pong_msg;
}
Response::Data(data) => {
let decoded: Vec<u8> = bincode::deserialize(&data.to_vec()).unwrap();
Expand All @@ -267,19 +274,21 @@ pub async fn reqres(
String::from_utf8_lossy(&decoded[..]),
remote_peer
);
log::debug!("{}", String::from_utf8_lossy(&decoded[..]));
break;
let data_msg = format!("{}", String::from_utf8_lossy(&decoded[..]));
log::debug!("{}", data_msg);
break data_msg;
}
},

e => {
println!("Peer2 err: {:?}", e);
break;
let err_msg = format!("Peer2 err: {e:?}");
println!("{err_msg}");
break err_msg;
}
},

_ => {}
};
}
Ok("decoded".to_string())
};
Ok(received)
}
3 changes: 3 additions & 0 deletions rpc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@ repository = "https://github.com/airalab/robonomics"
targets = ["x86_64-unknown-linux-gnu"]

[dependencies]
bincode = "1.3.3"
chrono = "0.4"
codec = { package = "parity-scale-codec", version = "3.0" }
futures = "0.3.24"
jsonrpsee = { version = "0.15.1", features = ["server"] }
jsonrpsee-types = { version = "0.15.1" }
hex = "0.4"
Expand Down
Loading

0 comments on commit e317698

Please sign in to comment.