Skip to content
This repository has been archived by the owner on Jan 14, 2020. It is now read-only.

Commit

Permalink
Update grin crates to v2.0.4 (#61)
Browse files Browse the repository at this point in the history
* Update grin crates to v2.0.4

* rustfmt

* use VersionedSlate for the owner_rpc api (mimblewimble#202)

* rustfmt

* rework kernel features support to handle fee and lock_height on features variants

* translate to/from TransactionV2 correctly in owner api and foreign api

* rustfmt

* revert CbData changes
mining node calls build_coinbase and this needs to be consistent with grin node

* cleanup imports

* fixup controller tests

* rustfmt

* fix the building
  • Loading branch information
garyyu authored Sep 6, 2019
1 parent 72f0ba0 commit d32aabd
Show file tree
Hide file tree
Showing 16 changed files with 351 additions and 245 deletions.
208 changes: 103 additions & 105 deletions Cargo.lock

Large diffs are not rendered by default.

30 changes: 15 additions & 15 deletions api/src/foreign_rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,7 @@ pub trait ForeignRpc {
"kernel": {
"excess": "08dfe86d732f2dd24bac36aa7502685221369514197c26d33fac03041d47e4b490",
"excess_sig": "8f07ddd5e9f5179cff19486034181ed76505baaad53e5d994064127b56c5841be02fa098c54c9bf638e0ee1ad5eb896caa11565f632be7b9cd65643ba371044f",
"features": "Coinbase",
"fee": "0",
"lock_height": "0"
"features": "Coinbase"
},
"key_id": "0300000000000000000000000400000000",
"output": {
Expand Down Expand Up @@ -191,7 +189,7 @@ pub trait ForeignRpc {
# ,1 ,false, false);
```
*/
fn verify_slate_messages(&self, slate: &Slate) -> Result<(), ErrorKind>;
fn verify_slate_messages(&self, slate: VersionedSlate) -> Result<(), ErrorKind>;

/**
Networked version of [Foreign::receive_tx](struct.Foreign.html#method.receive_tx).
Expand Down Expand Up @@ -512,7 +510,7 @@ pub trait ForeignRpc {
# , 5, false, true);
```
*/
fn finalize_invoice_tx(&self, slate: &Slate) -> Result<Slate, ErrorKind>;
fn finalize_invoice_tx(&self, slate: VersionedSlate) -> Result<VersionedSlate, ErrorKind>;
}

impl<W: ?Sized, C, K> ForeignRpc for Foreign<W, C, K>
Expand All @@ -529,31 +527,33 @@ where
Foreign::build_coinbase(self, block_fees).map_err(|e| e.kind())
}

fn verify_slate_messages(&self, slate: &Slate) -> Result<(), ErrorKind> {
Foreign::verify_slate_messages(self, slate).map_err(|e| e.kind())
fn verify_slate_messages(&self, slate: VersionedSlate) -> Result<(), ErrorKind> {
Foreign::verify_slate_messages(self, &Slate::from(slate)).map_err(|e| e.kind())
}

fn receive_tx(
&self,
slate: VersionedSlate,
in_slate: VersionedSlate,
dest_acct_name: Option<String>,
message: Option<String>,
) -> Result<VersionedSlate, ErrorKind> {
let version = slate.version();
let slate: Slate = slate.into();
let slate = Foreign::receive_tx(
let version = in_slate.version();
let out_slate = Foreign::receive_tx(
self,
&slate,
&Slate::from(in_slate),
dest_acct_name.as_ref().map(String::as_str),
message,
None,
)
.map_err(|e| e.kind())?;
Ok(VersionedSlate::into_version(slate, version))
Ok(VersionedSlate::into_version(out_slate, version))
}

fn finalize_invoice_tx(&self, slate: &Slate) -> Result<Slate, ErrorKind> {
Foreign::finalize_invoice_tx(self, slate).map_err(|e| e.kind())
fn finalize_invoice_tx(&self, in_slate: VersionedSlate) -> Result<VersionedSlate, ErrorKind> {
let version = in_slate.version();
let out_slate =
Foreign::finalize_invoice_tx(self, &Slate::from(in_slate)).map_err(|e| e.kind())?;
Ok(VersionedSlate::into_version(out_slate, version))
}
}

Expand Down
80 changes: 55 additions & 25 deletions api/src/owner_rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,11 @@ use uuid::Uuid;

use crate::core::core::Transaction;
use crate::keychain::{Identifier, Keychain};
use crate::libwallet::slate_versions::v2::TransactionV2;
use crate::libwallet::{
AcctPathMapping, ErrorKind, InitTxArgs, IssueInvoiceTxArgs, NodeClient, NodeHeightResult,
OutputCommitMapping, Slate, TxLogEntry, WalletBackend, WalletInfo,
OutputCommitMapping, Slate, SlateVersion, TxLogEntry, VersionedSlate, WalletBackend,
WalletInfo,
};
use crate::Owner;
use easy_jsonrpc;
Expand Down Expand Up @@ -413,7 +415,7 @@ pub trait OwnerRpc {
```
*/

fn init_send_tx(&self, args: InitTxArgs) -> Result<Slate, ErrorKind>;
fn init_send_tx(&self, args: InitTxArgs) -> Result<VersionedSlate, ErrorKind>;

/**
Networked version of [Owner::issue_invoice_tx](struct.Owner.html#method.issue_invoice_tx).
Expand Down Expand Up @@ -493,7 +495,7 @@ pub trait OwnerRpc {
```
*/

fn issue_invoice_tx(&self, args: IssueInvoiceTxArgs) -> Result<Slate, ErrorKind>;
fn issue_invoice_tx(&self, args: IssueInvoiceTxArgs) -> Result<VersionedSlate, ErrorKind>;

/**
Networked version of [Owner::process_invoice_tx](struct.Owner.html#method.process_invoice_tx).
Expand Down Expand Up @@ -641,7 +643,11 @@ pub trait OwnerRpc {
```
*/

fn process_invoice_tx(&self, slate: &Slate, args: InitTxArgs) -> Result<Slate, ErrorKind>;
fn process_invoice_tx(
&self,
slate: VersionedSlate,
args: InitTxArgs,
) -> Result<VersionedSlate, ErrorKind>;

/**
Networked version of [Owner::tx_lock_outputs](struct.Owner.html#method.tx_lock_outputs).
Expand Down Expand Up @@ -721,7 +727,11 @@ pub trait OwnerRpc {
```
*/
fn tx_lock_outputs(&self, slate: Slate, participant_id: usize) -> Result<(), ErrorKind>;
fn tx_lock_outputs(
&self,
slate: VersionedSlate,
participant_id: usize,
) -> Result<(), ErrorKind>;

/**
Networked version of [Owner::finalize_tx](struct.Owner.html#method.finalize_tx).
Expand Down Expand Up @@ -883,7 +893,7 @@ pub trait OwnerRpc {
# , 5, true, true, false);
```
*/
fn finalize_tx(&self, slate: Slate) -> Result<Slate, ErrorKind>;
fn finalize_tx(&self, slate: VersionedSlate) -> Result<VersionedSlate, ErrorKind>;

/**
Networked version of [Owner::post_tx](struct.Owner.html#method.post_tx).
Expand Down Expand Up @@ -954,7 +964,7 @@ pub trait OwnerRpc {
fn post_tx(
&self,
tx_slate_id: Option<Uuid>,
tx: &Transaction,
tx: &TransactionV2,
fluff: bool,
) -> Result<(), ErrorKind>;

Expand Down Expand Up @@ -1081,7 +1091,7 @@ pub trait OwnerRpc {
# , 5, true, true, false);
```
*/
fn get_stored_tx(&self, tx: &TxLogEntry) -> Result<Option<Transaction>, ErrorKind>;
fn get_stored_tx(&self, tx: &TxLogEntry) -> Result<Option<TransactionV2>, ErrorKind>;

/**
Networked version of [Owner::verify_slate_messages](struct.Owner.html#method.verify_slate_messages).
Expand Down Expand Up @@ -1159,7 +1169,7 @@ pub trait OwnerRpc {
# ,5 ,true, false, false);
```
*/
fn verify_slate_messages(&self, slate: &Slate) -> Result<(), ErrorKind>;
fn verify_slate_messages(&self, slate: VersionedSlate) -> Result<(), ErrorKind>;

/**
Networked version of [Owner::restore](struct.Owner.html#method.restore).
Expand Down Expand Up @@ -1297,45 +1307,65 @@ where
.map_err(|e| e.kind())
}

fn init_send_tx(&self, args: InitTxArgs) -> Result<Slate, ErrorKind> {
Owner::init_send_tx(self, args).map_err(|e| e.kind())
fn init_send_tx(&self, args: InitTxArgs) -> Result<VersionedSlate, ErrorKind> {
let slate = Owner::init_send_tx(self, args).map_err(|e| e.kind())?;
let version = SlateVersion::V2;
Ok(VersionedSlate::into_version(slate, version))
}

fn issue_invoice_tx(&self, args: IssueInvoiceTxArgs) -> Result<Slate, ErrorKind> {
Owner::issue_invoice_tx(self, args).map_err(|e| e.kind())
fn issue_invoice_tx(&self, args: IssueInvoiceTxArgs) -> Result<VersionedSlate, ErrorKind> {
let slate = Owner::issue_invoice_tx(self, args).map_err(|e| e.kind())?;
let version = SlateVersion::V2;
Ok(VersionedSlate::into_version(slate, version))
}

fn process_invoice_tx(&self, slate: &Slate, args: InitTxArgs) -> Result<Slate, ErrorKind> {
Owner::process_invoice_tx(self, slate, args).map_err(|e| e.kind())
fn process_invoice_tx(
&self,
in_slate: VersionedSlate,
args: InitTxArgs,
) -> Result<VersionedSlate, ErrorKind> {
let out_slate =
Owner::process_invoice_tx(self, &Slate::from(in_slate), args).map_err(|e| e.kind())?;
let version = SlateVersion::V2;
Ok(VersionedSlate::into_version(out_slate, version))
}

fn finalize_tx(&self, mut slate: Slate) -> Result<Slate, ErrorKind> {
Owner::finalize_tx(self, &mut slate, None, None).map_err(|e| e.kind())
fn finalize_tx(&self, in_slate: VersionedSlate) -> Result<VersionedSlate, ErrorKind> {
let out_slate =
Owner::finalize_tx(self, &Slate::from(in_slate), None, None).map_err(|e| e.kind())?;
let version = SlateVersion::V2;
Ok(VersionedSlate::into_version(out_slate, version))
}

fn tx_lock_outputs(&self, mut slate: Slate, participant_id: usize) -> Result<(), ErrorKind> {
Owner::tx_lock_outputs(self, &mut slate, participant_id).map_err(|e| e.kind())
fn tx_lock_outputs(
&self,
slate: VersionedSlate,
participant_id: usize,
) -> Result<(), ErrorKind> {
Owner::tx_lock_outputs(self, &Slate::from(slate), participant_id).map_err(|e| e.kind())
}

fn cancel_tx(&self, tx_id: Option<u32>, tx_slate_id: Option<Uuid>) -> Result<(), ErrorKind> {
Owner::cancel_tx(self, tx_id, tx_slate_id).map_err(|e| e.kind())
}

fn get_stored_tx(&self, tx: &TxLogEntry) -> Result<Option<Transaction>, ErrorKind> {
Owner::get_stored_tx(self, tx).map_err(|e| e.kind())
fn get_stored_tx(&self, tx: &TxLogEntry) -> Result<Option<TransactionV2>, ErrorKind> {
Owner::get_stored_tx(self, tx)
.map(|x| x.map(|y| TransactionV2::from(y)))
.map_err(|e| e.kind())
}

fn post_tx(
&self,
tx_slate_id: Option<Uuid>,
tx: &Transaction,
tx: &TransactionV2,
fluff: bool,
) -> Result<(), ErrorKind> {
Owner::post_tx(self, tx_slate_id, tx, fluff).map_err(|e| e.kind())
Owner::post_tx(self, tx_slate_id, &Transaction::from(tx), fluff).map_err(|e| e.kind())
}

fn verify_slate_messages(&self, slate: &Slate) -> Result<(), ErrorKind> {
Owner::verify_slate_messages(self, slate).map_err(|e| e.kind())
fn verify_slate_messages(&self, slate: VersionedSlate) -> Result<(), ErrorKind> {
Owner::verify_slate_messages(self, &Slate::from(slate)).map_err(|e| e.kind())
}

fn restore(&self) -> Result<(), ErrorKind> {
Expand Down
3 changes: 2 additions & 1 deletion controller/tests/accounts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,11 +134,13 @@ fn accounts_test_impl(test_dir: &str) -> Result<(), libwallet::Error> {
assert_eq!(txs.len(), 5);
Ok(())
})?;

// now check second account
{
let mut w = wallet1.lock();
w.set_parent_key_id_by_name("account1")?;
}

wallet::controller::owner_single_use(wallet1.clone(), |api| {
// check last confirmed height on this account is different from above (should be 0)
let (_, wallet1_info) = api.retrieve_summary_info(false, 1)?;
Expand Down Expand Up @@ -178,7 +180,6 @@ fn accounts_test_impl(test_dir: &str) -> Result<(), libwallet::Error> {
let mut w = wallet1.lock();
w.set_parent_key_id_by_name("account1")?;
}

wallet::controller::owner_single_use(wallet1.clone(), |api| {
let args = InitTxArgs {
src_acct_name: None,
Expand Down
6 changes: 1 addition & 5 deletions controller/tests/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,13 +120,9 @@ fn basic_transaction_api(test_dir: &str) -> Result<(), libwallet::Error> {

// Check we have a single kernel and that it is a Plain kernel (no lock_height).
assert_eq!(slate.tx.kernels().len(), 1);
assert_eq!(
slate.tx.kernels().first().map(|k| k.lock_height).unwrap(),
0
);
assert_eq!(
slate.tx.kernels().first().map(|k| k.features).unwrap(),
transaction::KernelFeatures::Plain
transaction::KernelFeatures::Plain { fee: 2000000 }
);

Ok(())
Expand Down
8 changes: 7 additions & 1 deletion grinrelay/src/grinrelay_address.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use crate::crypto::AddrBech32;
use crate::error::ErrorKind;
use crate::grin_util::secp::key::PublicKey;
use crate::Result;
use grin_wallet_util::grin_core::global::is_mainnet;
use grin_wallet_util::grin_core::global::{ChainTypes, CHAIN_TYPE};

pub const GRINRELAY_PREFIX: &str = "grinrelay://";
pub const GRINRELAY_ADDRESS_REGEX: &str = r"^(grinrelay://)?(?P<public_key>[0-9a-z\-]{62,67})(@(?P<domain>[a-zA-Z0-9\.]+)(:(?P<port>[0-9]*))?)?$";
Expand All @@ -32,6 +32,12 @@ pub const GRINRELAY_ADDRESS_HRP_TESTNET: &str = "tn";
pub const DEFAULT_GRINRELAY_DOMAIN: &str = "relay.grin.icu";
pub const DEFAULT_GRINRELAY_PORT: u16 = 3418;

/// Are we in mainnet?
pub fn is_mainnet() -> bool {
let param_ref = CHAIN_TYPE.read();
ChainTypes::Mainnet == *param_ref
}

pub fn hrp_bytes() -> Vec<u8> {
if is_mainnet() {
GRINRELAY_ADDRESS_HRP_MAINNET.into()
Expand Down
9 changes: 7 additions & 2 deletions impls/src/backends/lmdb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,8 @@ where
.join(filename);
let path_buf = Path::new(&path).to_path_buf();
let mut stored_tx = File::create(path_buf)?;
let tx_hex = util::to_hex(core::ser::ser_vec(tx).unwrap());;
let tx_hex =
util::to_hex(core::ser::ser_vec(tx, core::ser::ProtocolVersion::local()).unwrap());;
stored_tx.write_all(&tx_hex.as_bytes())?;
stored_tx.sync_all()?;
Ok(())
Expand Down Expand Up @@ -378,7 +379,11 @@ where
tx_f.read_to_string(&mut content)?;
let tx_bin = util::from_hex(content).unwrap();
Ok(Some(
core::ser::deserialize::<Transaction>(&mut &tx_bin[..]).unwrap(),
core::ser::deserialize::<Transaction>(
&mut &tx_bin[..],
core::ser::ProtocolVersion::local(),
)
.unwrap(),
))
}

Expand Down
11 changes: 8 additions & 3 deletions impls/src/backends/wallet_store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ use lmdb_zero as lmdb;
use lmdb_zero::traits::CreateCursor;
use lmdb_zero::LmdbResultExt;

use crate::core::ser::ProtocolVersion;
use crate::libwallet::wallet_ser as ser;
use crate::store::Error;
use crate::util::{RwLock, RwLockReadGuard};
Expand All @@ -48,6 +49,7 @@ pub struct Store {
env: Arc<lmdb::Environment>,
db: RwLock<Option<Arc<lmdb::Database<'static>>>>,
name: String,
version: ProtocolVersion,
}

impl Store {
Expand Down Expand Up @@ -91,6 +93,7 @@ impl Store {
env: Arc::new(env),
db: RwLock::new(None),
name: db_name,
version: ProtocolVersion(1),
};

{
Expand Down Expand Up @@ -212,7 +215,7 @@ impl Store {
) -> Result<Option<T>, Error> {
let res: lmdb::error::Result<&[u8]> = access.get(&db.as_ref().unwrap(), key);
match res.to_opt() {
Ok(Some(mut res)) => match ser::deserialize(&mut res) {
Ok(Some(mut res)) => match ser::deserialize(&mut res, self.version) {
Ok(res) => Ok(Some(res)),
Err(e) => {
debug!("store::get_ser failed. {}", e.to_string());
Expand Down Expand Up @@ -248,6 +251,7 @@ impl Store {
cursor,
seek: false,
prefix: from.to_vec(),
version: self.version,
_marker: marker::PhantomData,
})
}
Expand Down Expand Up @@ -285,7 +289,7 @@ impl<'a> Batch<'a> {
/// Writes a single key and its `Writeable` value to the db. Encapsulates
/// serialization.
pub fn put_ser<W: ser::Writeable>(&self, key: &[u8], value: &W) -> Result<(), Error> {
let ser_value = ser::ser_vec(value);
let ser_value = ser::ser_vec(value, self.store.version);
match ser_value {
Ok(data) => self.put(key, &data),
Err(err) => Err(Error::SerErr(format!("{}", err))),
Expand Down Expand Up @@ -352,6 +356,7 @@ where
cursor: Arc<lmdb::Cursor<'static, 'static>>,
seek: bool,
prefix: Vec<u8>,
version: ProtocolVersion,
_marker: marker::PhantomData<T>,
}

Expand Down Expand Up @@ -385,7 +390,7 @@ where
fn deser_if_prefix_match(&self, key: &[u8], value: &[u8]) -> Option<(Vec<u8>, T)> {
let plen = self.prefix.len();
if plen == 0 || (key.len() >= plen && key[0..plen] == self.prefix[..]) {
if let Ok(value) = ser::deserialize(&mut &value[..]) {
if let Ok(value) = ser::deserialize(&mut &value[..], self.version) {
Some((key.to_vec(), value))
} else {
None
Expand Down
Loading

0 comments on commit d32aabd

Please sign in to comment.