Skip to content

Commit 47ee03c

Browse files
authored
V2 slate versioning enablement (#85)
* fix for command line listener port override * reduce parameter query size * Add slate versioning * rustfmt * bump version number * Add tests for slate version conversion * rustfmt * Updates and test addition based on bdap's review * rustfmt
1 parent 54bd364 commit 47ee03c

File tree

17 files changed

+3787
-278
lines changed

17 files changed

+3787
-278
lines changed

Cargo.lock

+222-221
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+7-7
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "grin_wallet"
3-
version = "1.1.0-beta.1"
3+
version = "1.1.0-beta.2"
44
authors = ["Grin Developers <[email protected]>"]
55
description = "Simple, private and scalable cryptocurrency implementation based on the MimbleWimble chain format."
66
license = "Apache-2.0"
@@ -29,13 +29,13 @@ prettytable-rs = "0.7"
2929
log = "0.4"
3030
linefeed = "0.5"
3131

32-
grin_wallet_api = { path = "./api", version = "1.1.0-beta.1" }
33-
grin_wallet_impls = { path = "./impls", version = "1.1.0-beta.1" }
34-
grin_wallet_libwallet = { path = "./libwallet", version = "1.1.0-beta.1" }
35-
grin_wallet_controller = { path = "./controller", version = "1.1.0-beta.1" }
36-
grin_wallet_config = { path = "./config", version = "1.1.0-beta.1" }
32+
grin_wallet_api = { path = "./api", version = "1.1.0-beta.2" }
33+
grin_wallet_impls = { path = "./impls", version = "1.1.0-beta.2" }
34+
grin_wallet_libwallet = { path = "./libwallet", version = "1.1.0-beta.2" }
35+
grin_wallet_controller = { path = "./controller", version = "1.1.0-beta.2" }
36+
grin_wallet_config = { path = "./config", version = "1.1.0-beta.2" }
3737

38-
grin_wallet_util = { path = "./util", version = "1.1.0-beta.1" }
38+
grin_wallet_util = { path = "./util", version = "1.1.0-beta.2" }
3939

4040
[build-dependencies]
4141
built = "0.3"

api/Cargo.toml

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "grin_wallet_api"
3-
version = "1.1.0-beta.1"
3+
version = "1.1.0-beta.2"
44
authors = ["Grin Developers <[email protected]>"]
55
description = "Grin Wallet API"
66
license = "Apache-2.0"
@@ -18,11 +18,11 @@ serde_json = "1"
1818
easy-jsonrpc = "0.4.1"
1919
chrono = { version = "0.4.4", features = ["serde"] }
2020

21-
grin_wallet_libwallet = { path = "../libwallet", version = "1.1.0-beta.1" }
22-
grin_wallet_config = { path = "../config", version = "1.1.0-beta.1" }
23-
grin_wallet_impls = { path = "../impls", version = "1.1.0-beta.1" }
21+
grin_wallet_libwallet = { path = "../libwallet", version = "1.1.0-beta.2" }
22+
grin_wallet_config = { path = "../config", version = "1.1.0-beta.2" }
23+
grin_wallet_impls = { path = "../impls", version = "1.1.0-beta.2" }
2424

25-
grin_wallet_util = { path = "../util", version = "1.1.0-beta.1" }
25+
grin_wallet_util = { path = "../util", version = "1.1.0-beta.2" }
2626

2727
[dev-dependencies]
2828
serde_json = "1"

api/src/foreign_rpc.rs

+9-6
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@
1515
//! JSON-RPC Stub generation for the Foreign API
1616
1717
use crate::keychain::Keychain;
18-
use crate::libwallet::slate::Slate;
1918
use crate::libwallet::types::{BlockFees, CbData, InitTxArgs, NodeClient, WalletBackend};
2019
use crate::libwallet::ErrorKind;
20+
use crate::libwallet::{Slate, VersionedSlate};
2121
use crate::Foreign;
2222
use easy_jsonrpc;
2323

@@ -310,10 +310,10 @@ pub trait ForeignRpc {
310310
*/
311311
fn receive_tx(
312312
&self,
313-
slate: Slate,
313+
slate: VersionedSlate,
314314
dest_acct_name: Option<String>,
315315
message: Option<String>,
316-
) -> Result<Slate, ErrorKind>;
316+
) -> Result<VersionedSlate, ErrorKind>;
317317
}
318318

319319
impl<W: ?Sized, C, K> ForeignRpc for Foreign<W, C, K>
@@ -332,18 +332,21 @@ where
332332

333333
fn receive_tx(
334334
&self,
335-
slate: Slate,
335+
slate: VersionedSlate,
336336
dest_acct_name: Option<String>,
337337
message: Option<String>,
338-
) -> Result<Slate, ErrorKind> {
338+
) -> Result<VersionedSlate, ErrorKind> {
339+
let version = slate.version();
340+
let slate: Slate = slate.into();
339341
let slate = Foreign::receive_tx(
340342
self,
341343
&slate,
342344
dest_acct_name.as_ref().map(String::as_str),
343345
message,
344346
)
345347
.map_err(|e| e.kind())?;
346-
Ok(slate)
348+
349+
Ok(VersionedSlate::into_version(slate, version))
347350
}
348351
}
349352

api/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -43,5 +43,6 @@ pub use crate::foreign_rpc::ForeignRpc;
4343
pub use crate::owner::Owner;
4444
pub use crate::owner_rpc::OwnerRpc;
4545

46+
pub use crate::foreign_rpc::foreign_rpc as foreign_rpc_client;
4647
pub use crate::foreign_rpc::run_doctest_foreign;
4748
pub use crate::owner_rpc::run_doctest_owner;

api/tests/slate_versioning.rs

+131
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
// Copyright 2019 The Grin Developers
2+
// Licensed under the Apache License, Version 2.0 (the "License");
3+
// you may not use this file except in compliance with the License.
4+
// You may obtain a copy of the License at
5+
//
6+
// http://www.apache.org/licenses/LICENSE-2.0
7+
//
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
14+
//! core::libtx specific tests
15+
use grin_wallet_api::foreign_rpc_client;
16+
use grin_wallet_api::run_doctest_foreign;
17+
use grin_wallet_libwallet::{Slate, SlateVersion, VersionedSlate};
18+
use serde_json;
19+
use serde_json::Value;
20+
use tempfile::tempdir;
21+
//use grin_wallet_libwallet::slate_versions::v1::SlateV1;
22+
//use grin_wallet_libwallet::slate_versions::v2::SlateV2;
23+
24+
// test all slate conversions
25+
#[test]
26+
fn receive_versioned_slate() {
27+
// as in doctests, except exercising versioning functionality
28+
// by accepting and responding with a V1 slate
29+
30+
let dir = tempdir().map_err(|e| format!("{:#?}", e)).unwrap();
31+
let dir = dir
32+
.path()
33+
.to_str()
34+
.ok_or("Failed to convert tmpdir path to string.".to_owned())
35+
.unwrap();
36+
37+
let v1_req = include_str!("slates/v1_req.slate");
38+
let v1_resp = include_str!("slates/v1_res.slate");
39+
40+
// leave here for the ability to create earlier slate versions
41+
// for test input
42+
/*let v: Value = serde_json::from_str(v1_req).unwrap();
43+
let v2_slate = v["params"][0].clone();
44+
println!("{}", v2_slate);
45+
let v2_slate_str = v2_slate.to_string();
46+
println!("{}", v2_slate_str);
47+
let v2: SlateV2 = serde_json::from_str(&v2_slate.to_string()).unwrap();
48+
let v1 = SlateV1::from(v2);
49+
let v1_str = serde_json::to_string_pretty(&v1).unwrap();
50+
panic!("{}", v1_str);*/
51+
52+
let request_val: Value = serde_json::from_str(v1_req).unwrap();
53+
let expected_response: Value = serde_json::from_str(v1_resp).unwrap();
54+
55+
let response = run_doctest_foreign(request_val, dir, 5, true)
56+
.unwrap()
57+
.unwrap();
58+
59+
if response != expected_response {
60+
panic!(
61+
"(left != right) \nleft: {}\nright: {}",
62+
serde_json::to_string_pretty(&response).unwrap(),
63+
serde_json::to_string_pretty(&expected_response).unwrap()
64+
);
65+
}
66+
}
67+
68+
/// call ForeignRpc::receive_tx on vs and return the result
69+
fn receive_tx(vs: VersionedSlate) -> VersionedSlate {
70+
let dir = tempdir().map_err(|e| format!("{:#?}", e)).unwrap();
71+
let dir = dir
72+
.path()
73+
.to_str()
74+
.ok_or("Failed to convert tmpdir path to string.".to_owned())
75+
.unwrap();
76+
let bound_method = foreign_rpc_client::receive_tx(
77+
vs,
78+
None,
79+
Some("Thanks for saving my dog from that tree, bddap.".into()),
80+
)
81+
.unwrap();
82+
let (call, tracker) = bound_method.call();
83+
let json_response = run_doctest_foreign(call.as_request(), dir, 5, false)
84+
.unwrap()
85+
.unwrap();
86+
let mut response = easy_jsonrpc::Response::from_json_response(json_response).unwrap();
87+
tracker.get_return(&mut response).unwrap().unwrap()
88+
}
89+
90+
#[test]
91+
fn version_unchanged() {
92+
let req: Value = serde_json::from_str(include_str!("slates/v1_req.slate")).unwrap();
93+
let slate: VersionedSlate = serde_json::from_value(req["params"][0].clone()).unwrap();
94+
let slate_req: Slate = slate.into();
95+
96+
assert_eq!(
97+
receive_tx(VersionedSlate::into_version(
98+
slate_req.clone(),
99+
SlateVersion::V0
100+
))
101+
.version(),
102+
SlateVersion::V0
103+
);
104+
105+
assert_eq!(
106+
receive_tx(VersionedSlate::into_version(
107+
slate_req.clone(),
108+
SlateVersion::V1
109+
))
110+
.version(),
111+
SlateVersion::V1
112+
);
113+
114+
assert_eq!(
115+
receive_tx(VersionedSlate::into_version(
116+
slate_req.clone(),
117+
SlateVersion::V2
118+
))
119+
.version(),
120+
SlateVersion::V2
121+
);
122+
123+
// compile time test will remind us to update these tests when updating slate format
124+
fn _all_versions_tested(vs: VersionedSlate) {
125+
match vs {
126+
VersionedSlate::V0(_) => (),
127+
VersionedSlate::V1(_) => (),
128+
VersionedSlate::V2(_) => (),
129+
}
130+
}
131+
}

0 commit comments

Comments
 (0)