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

Implement new JSON-RPC API #1687

Merged
merged 32 commits into from
Dec 17, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
64736c6
Implement new JSON-RPC API
tomaka Nov 26, 2021
768ba6c
Merge branch 'main' into new-json-rpc-api-impl
tomaka Nov 29, 2021
41b0293
Update to changes
tomaka Nov 29, 2021
732e1d9
Merge branch 'main' into new-json-rpc-api-impl
tomaka Nov 30, 2021
2d8f497
WIP
tomaka Nov 30, 2021
ac37235
WIP
tomaka Nov 30, 2021
51e3b8e
WIP
tomaka Nov 30, 2021
d285eac
WIP
tomaka Dec 1, 2021
ef26bb2
Merge branch 'main' into new-json-rpc-api-impl
tomaka Dec 1, 2021
70050b0
Merge branch 'main' into new-json-rpc-api-impl
tomaka Dec 15, 2021
9b60fd2
Fix compilation
tomaka Dec 15, 2021
e3f3564
Update link
tomaka Dec 15, 2021
c1fdbfc
Add the chainSpec methods
tomaka Dec 15, 2021
ae88a0c
Whoops, wrong variable
tomaka Dec 15, 2021
78a7c35
Add tests for chainSpec functions
tomaka Dec 15, 2021
b3253f4
Add toLowerCase() just in case, huhu
tomaka Dec 15, 2021
ccb2ea9
Add test for chainHead
tomaka Dec 15, 2021
32b3300
Duplicate westend.json for the tests
tomaka Dec 15, 2021
66a7992
Fix the camelCase issue
tomaka Dec 15, 2021
3347e07
More camelCase fixing
tomaka Dec 15, 2021
0a99ae8
Better sudo_unstable_version
tomaka Dec 16, 2021
4540c3c
Add sudo_unstable_p2pDiscover and tests
tomaka Dec 16, 2021
b4ceaf2
Formatting of methods
tomaka Dec 16, 2021
91580fe
Merge branch 'main' into new-json-rpc-api-impl
tomaka Dec 16, 2021
842dba0
WIP
tomaka Dec 16, 2021
02486ee
Merge branch 'main' into new-json-rpc-api-impl
tomaka Dec 17, 2021
063e876
Way better follow implementation
tomaka Dec 17, 2021
4c0f11d
Remove bootnodes from test Westend
tomaka Dec 17, 2021
7a36b4a
Merge branch 'main' into new-json-rpc-api-impl
tomaka Dec 17, 2021
2536930
Finish follow implementation
tomaka Dec 17, 2021
2a2646b
Add test for chainHead_unstable_follow
tomaka Dec 17, 2021
9afd917
Rustfmt
tomaka Dec 17, 2021
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
717 changes: 706 additions & 11 deletions bin/light-base/src/json_rpc_service.rs

Large diffs are not rendered by default.

90 changes: 90 additions & 0 deletions bin/wasm-node/javascript/test/chainHead.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
// Smoldot
// Copyright (C) 2019-2021 Parity Technologies (UK) Ltd.
// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0

// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.

import test from 'ava';
import * as fs from 'fs';
import { start } from "../src/index.js";

const westendSpec = fs.readFileSync('./test/westend.json', 'utf8');

test('chainHead_unstable_follow works', async t => {
let promiseResolve;
let promiseReject;
const promise = new Promise((resolve, reject) => { promiseResolve = resolve; promiseReject = reject; });

let subscriptionId = null;

const client = start({ logCallback: () => { } });
await client
.addChain({
chainSpec: westendSpec,
jsonRpcCallback: (resp) => {
const parsed = JSON.parse(resp);

if (parsed.id == 1) {
subscriptionId = parsed.result;
} else if (parsed.method == "chainHead_unstable_followEvent" && parsed.params.subscription == subscriptionId) {
if (parsed.params.result.event == "initialized") {
if (parsed.params.result.finalizedBlockHash.toLowerCase() == "0x9d34c5a7a8ad8d73c7690a41f7a9d1a7c46e21dc8fb1638aee6ef07f45b65158" && !parsed.params.result.finalizedBlockRuntime)
promiseResolve();
else
promiseReject(resp);
}
} else {
promiseReject(resp);
}
}
})
.then((chain) => {
chain.sendJsonRpc('{"jsonrpc":"2.0","id":1,"method":"chainHead_unstable_follow","params":[false]}', 0, 0);
})
.then(() => promise)
.then(() => t.pass())
.then(() => client.terminate());
});

test('chainHead_unstable_unfollow works', async t => {
let promiseResolve;
let promiseReject;
const promise = new Promise((resolve, reject) => { promiseResolve = resolve; promiseReject = reject; });

let chain;

const client = start({ logCallback: () => { } });
await client
.addChain({
chainSpec: westendSpec,
jsonRpcCallback: (resp) => {
const parsed = JSON.parse(resp);

if (parsed.id == 1) {
chain.sendJsonRpc('{"jsonrpc":"2.0","id":2,"method":"chainHead_unstable_unfollow","params":[' + JSON.stringify(parsed.result) + ']}', 0, 0);
} else if (parsed.id == 2 && parsed.result == null) {
promiseResolve();
} else if (parsed.method != "chainHead_unstable_followEvent") { // Don't fail the promise on follow event
promiseReject(resp);
}
}
})
.then((c) => {
chain = c;
chain.sendJsonRpc('{"jsonrpc":"2.0","id":1,"method":"chainHead_unstable_follow","params":[false]}', 0, 0);
})
.then(() => promise)
.then(() => t.pass())
.then(() => client.terminate());
});
97 changes: 97 additions & 0 deletions bin/wasm-node/javascript/test/chainSpec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
// Smoldot
// Copyright (C) 2019-2021 Parity Technologies (UK) Ltd.
// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0

// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.

import test from 'ava';
import * as fs from 'fs';
import { start } from "../src/index.js";

const westendSpec = fs.readFileSync('./test/westend.json', 'utf8');

test('chainSpec_chainName works', async t => {
let promiseResolve;
let promiseReject;
const promise = new Promise((resolve, reject) => { promiseResolve = resolve; promiseReject = reject; });

const client = start({ logCallback: () => { } });
await client
.addChain({
chainSpec: westendSpec,
jsonRpcCallback: (resp) => {
const parsed = JSON.parse(resp);
if (parsed.result == "Westend")
promiseResolve();
else
promiseReject(resp);
}
})
.then((chain) => {
chain.sendJsonRpc('{"jsonrpc":"2.0","id":1,"method":"chainSpec_unstable_chainName","params":[]}', 0, 0);
})
.then(() => promise)
.then(() => t.pass())
.then(() => client.terminate());
});

test('chainSpec_unstable_genesisHash works', async t => {
let promiseResolve;
let promiseReject;
const promise = new Promise((resolve, reject) => { promiseResolve = resolve; promiseReject = reject; });

const client = start({ logCallback: () => { } });
await client
.addChain({
chainSpec: westendSpec,
jsonRpcCallback: (resp) => {
const parsed = JSON.parse(resp);
if (parsed.result.toLowerCase() == "0xe143f23803ac50e8f6f8e62695d1ce9e4e1d68aa36c1cd2cfd15340213f3423e")
promiseResolve();
else
promiseReject(resp);
}
})
.then((chain) => {
chain.sendJsonRpc('{"jsonrpc":"2.0","id":1,"method":"chainSpec_unstable_genesisHash","params":[]}', 0, 0);
})
.then(() => promise)
.then(() => t.pass())
.then(() => client.terminate());
});

test('chainSpec_unstable_properties works', async t => {
let promiseResolve;
let promiseReject;
const promise = new Promise((resolve, reject) => { promiseResolve = resolve; promiseReject = reject; });

const client = start({ logCallback: () => { } });
await client
.addChain({
chainSpec: westendSpec,
jsonRpcCallback: (resp) => {
const parsed = JSON.parse(resp);
if (parsed.result.ss58Format == 42 && parsed.result.tokenDecimals == 12 && parsed.result.tokenSymbol == "WND")
promiseResolve();
else
promiseReject(resp);
}
})
.then((chain) => {
chain.sendJsonRpc('{"jsonrpc":"2.0","id":1,"method":"chainSpec_unstable_properties","params":[]}', 0, 0);
})
.then(() => promise)
.then(() => t.pass())
.then(() => client.terminate());
});
122 changes: 122 additions & 0 deletions bin/wasm-node/javascript/test/sudo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
// Smoldot
// Copyright (C) 2019-2021 Parity Technologies (UK) Ltd.
// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0

// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.

import test from 'ava';
import * as fs from 'fs';
import { start } from "../src/index.js";

const westendSpec = fs.readFileSync('./test/westend.json', 'utf8');

test('sudo_unstable_p2pDiscover is available', async t => {
let promiseResolve;
let promiseReject;
const promise = new Promise((resolve, reject) => { promiseResolve = resolve; promiseReject = reject; });

const client = start({ logCallback: () => { } });
await client
.addChain({
chainSpec: westendSpec,
jsonRpcCallback: (resp) => {
const parsed = JSON.parse(resp);
if (parsed.id == 1 && parsed.result === null)
promiseResolve();
else
promiseReject(resp);
}
})
.then((chain) => {
chain.sendJsonRpc('{"jsonrpc":"2.0","id":1,"method":"sudo_unstable_p2pDiscover","params":["/ip4/1.2.3.4/tcp/30333/p2p/12D3KooWDmQPkBvQGg9wjBdFThtWj3QCDVQyHJ1apfWrHvjwbYS8"]}', 0, 0);
})
.then(() => promise)
.then(() => t.pass())
.then(() => client.terminate());
});

test('sudo_unstable_p2pDiscover returns error on invalid multiaddr', async t => {
let promiseResolve;
let promiseReject;
const promise = new Promise((resolve, reject) => { promiseResolve = resolve; promiseReject = reject; });

const client = start({ logCallback: () => { } });
await client
.addChain({
chainSpec: westendSpec,
jsonRpcCallback: (resp) => {
const parsed = JSON.parse(resp);
if (parsed.id == 1 && !!parsed.error)
promiseResolve();
else
promiseReject(resp);
}
})
.then((chain) => {
chain.sendJsonRpc('{"jsonrpc":"2.0","id":1,"method":"sudo_unstable_p2pDiscover","params":["/hello/world"]}', 0, 0);
})
.then(() => promise)
.then(() => t.pass())
.then(() => client.terminate());
});

test('sudo_unstable_p2pDiscover returns error if multiaddr doesn\'t end with /p2p', async t => {
let promiseResolve;
let promiseReject;
const promise = new Promise((resolve, reject) => { promiseResolve = resolve; promiseReject = reject; });

const client = start({ logCallback: () => { } });
await client
.addChain({
chainSpec: westendSpec,
jsonRpcCallback: (resp) => {
const parsed = JSON.parse(resp);
if (parsed.id == 1 && !!parsed.error)
promiseResolve();
else
promiseReject(resp);
}
})
.then((chain) => {
chain.sendJsonRpc('{"jsonrpc":"2.0","id":1,"method":"sudo_unstable_p2pDiscover","params":["/ip4/127.0.0.1/tcp/30333/ws"]}', 0, 0);
})
.then(() => promise)
.then(() => t.pass())
.then(() => client.terminate());
});

test('sudo_unstable_version works', async t => {
let promiseResolve;
let promiseReject;
const promise = new Promise((resolve, reject) => { promiseResolve = resolve; promiseReject = reject; });

const client = start({ logCallback: () => { } });
await client
.addChain({
chainSpec: westendSpec,
jsonRpcCallback: (resp) => {
const parsed = JSON.parse(resp);
if (parsed.result.includes("smoldot"))
promiseResolve();
else
promiseReject(resp);
}
})
.then((chain) => {
chain.sendJsonRpc('{"jsonrpc":"2.0","id":1,"method":"sudo_unstable_version","params":[]}', 0, 0);
})
.then(() => promise)
.then(() => t.pass())
.then(() => client.terminate());
});
2 changes: 1 addition & 1 deletion bin/wasm-node/javascript/test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import test from 'ava';
import * as fs from 'fs';
import { start } from "../src/index.js";

const westendSpec = fs.readFileSync('../../westend.json', 'utf8');
const westendSpec = fs.readFileSync('./test/westend.json', 'utf8');

test('invalid chain spec throws error', async t => {
const client = start({ logCallback: () => { } });
Expand Down
130 changes: 130 additions & 0 deletions bin/wasm-node/javascript/test/westend.json

Large diffs are not rendered by default.

Loading