Skip to content
This repository was archived by the owner on Mar 23, 2021. It is now read-only.

Commit dc80a15

Browse files
bors[bot]Tobin C. Hardingluckysori
authored
Merge #1689
1689: 1481 store timestamp r=mergify[bot] a=tcharding Add a timestamp column to the two accepted swap database tables. Use SQLite auto-generation of timestamp to populate this column when an accepted swap is stored in the database. Add a database function to retrieve the timestamp for an [accepted] swap id. Add an integration test that saves and retrieves a swap in order to test the validity of timestamp. Resolves: #1481 Co-authored-by: Tobin C. Harding <[email protected]> Co-authored-by: Lucas Soriano del Pino <[email protected]>
2 parents 3332f12 + ccd354c commit dc80a15

File tree

22 files changed

+83
-44
lines changed

22 files changed

+83
-44
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

api_tests/lib_sdk/actors/actor.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -208,8 +208,6 @@ export class Actor {
208208
}
209209

210210
public async assertSwapped() {
211-
this.logger.debug("Checking if swap @ %s is done", this.swap.self);
212-
213211
for (const [
214212
assetKind,
215213
expectedBalanceChange,
@@ -228,9 +226,15 @@ export class Actor {
228226
).add(expectedBalanceChange);
229227
const maximumFee = wallet.MaximumFee;
230228

231-
const actualBalance = new BigNumber(await wallet.getBalance());
232-
await expect(actualBalance.gte(expectedBalance.sub(maximumFee))).to
233-
.be.true;
229+
await expect(
230+
wallet
231+
.getBalance()
232+
.then(balance =>
233+
new BigNumber(balance).gte(
234+
expectedBalance.sub(maximumFee)
235+
)
236+
)
237+
).to.eventually.be.true;
234238
}
235239
}
236240

cnd/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ config = { version = "0.9", features = ["toml"] }
1919
debug_stub_derive = "0.3"
2020
derivative = "1"
2121
derive_more = "0.99.2"
22-
diesel = { version = "1.4", features = ["sqlite"] }
22+
diesel = { version = "1.4", features = ["sqlite", "chrono"] }
2323
diesel_migrations = "1.4.0"
2424
directories = "2.0"
2525
either = "1.5"

cnd/migrations/2019-11-11-034058_create-message-tables/up.sql

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,15 +71,17 @@ CREATE TABLE rfc003_ethereum_bitcoin_accept_messages
7171
id INTEGER NOT NULL PRIMARY KEY,
7272
swap_id UNIQUE NOT NULL,
7373
bitcoin_refund_identity NOT NULL,
74-
ethereum_redeem_identity NOT NULL
74+
ethereum_redeem_identity NOT NULL,
75+
at DATETIME DEFAULT CURRENT_TIMESTAMP
7576
);
7677

7778
CREATE TABLE rfc003_bitcoin_ethereum_accept_messages
7879
(
7980
id INTEGER NOT NULL PRIMARY KEY,
8081
swap_id UNIQUE NOT NULL,
8182
bitcoin_redeem_identity NOT NULL,
82-
ethereum_refund_identity NOT NULL
83+
ethereum_refund_identity NOT NULL,
84+
at DATETIME DEFAULT CURRENT_TIMESTAMP
8385
);
8486

8587
CREATE TABLE rfc003_decline_messages

cnd/src/db/integration_tests.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,17 +54,18 @@ macro_rules! db_roundtrip_test {
5454
db.save(saved_accept.clone()).await?;
5555

5656
let loaded_swap = Retrieve::get(&db, &swap_id).await?;
57-
let (loaded_request, loaded_accept) = db.load_accepted_swap(swap_id).await?;
57+
// If the assignment of `_at` works then we have a valid NaiveDateTime.
58+
let (loaded_request, loaded_accept, _at) = db.load_accepted_swap(&swap_id).await?;
5859
let loaded_swap_types = db.determine_types(&swap_id).await?;
5960

6061
Ok((loaded_swap, loaded_request, loaded_accept, loaded_swap_types))
6162
})?;
6263

6364
Ok(
6465
saved_request == loaded_request &&
65-
saved_accept == loaded_accept &&
66-
loaded_swap == saved_swap &&
67-
loaded_swap_types == expected_swap_types
66+
saved_accept == loaded_accept &&
67+
saved_swap == loaded_swap &&
68+
expected_swap_types == loaded_swap_types
6869
)
6970
}
7071

cnd/src/db/load_swaps.rs

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,21 @@ use crate::{
1212
messages::{Accept, Request},
1313
Ledger, SecretHash,
1414
},
15-
HashFunction, SwapId, Timestamp,
15+
HashFunction, SwapId,
1616
},
17+
timestamp::Timestamp,
1718
};
1819
use async_trait::async_trait;
20+
use chrono::NaiveDateTime;
1921
use diesel::{self, prelude::*, RunQueryDsl};
2022

21-
pub type AcceptedSwap<AL, BL, AA, BA> = (Request<AL, BL, AA, BA>, Accept<AL, BL>);
23+
pub type AcceptedSwap<AL, BL, AA, BA> = (Request<AL, BL, AA, BA>, Accept<AL, BL>, NaiveDateTime);
2224

2325
#[async_trait]
2426
pub trait LoadAcceptedSwap<AL: Ledger, BL: Ledger, AA: Asset, BA: Asset> {
2527
async fn load_accepted_swap(
2628
&self,
27-
swap_id: SwapId,
29+
swap_id: &SwapId,
2830
) -> anyhow::Result<AcceptedSwap<AL, BL, AA, BA>>;
2931
}
3032

@@ -45,13 +47,15 @@ struct BitcoinEthereumBitcoinEtherAcceptedSwap {
4547
// Accept fields.
4648
bitcoin_redeem_identity: Text<bitcoin::PublicKey>,
4749
ethereum_refund_identity: Text<EthereumAddress>,
50+
51+
at: NaiveDateTime,
4852
}
4953

5054
#[async_trait]
5155
impl LoadAcceptedSwap<Bitcoin, Ethereum, bitcoin::Amount, EtherQuantity> for Sqlite {
5256
async fn load_accepted_swap(
5357
&self,
54-
key: SwapId,
58+
key: &SwapId,
5559
) -> anyhow::Result<
5660
AcceptedSwap<Bitcoin, Ethereum, bitcoin::Amount, crate::ethereum::EtherQuantity>,
5761
> {
@@ -85,6 +89,7 @@ impl LoadAcceptedSwap<Bitcoin, Ethereum, bitcoin::Amount, EtherQuantity> for Sql
8589
request_messages::secret_hash,
8690
accept_messages::bitcoin_redeem_identity,
8791
accept_messages::ethereum_refund_identity,
92+
accept_messages::at,
8893
))
8994
.filter(accept_messages::swap_id.eq(key))
9095
.first(connection)
@@ -118,6 +123,7 @@ impl LoadAcceptedSwap<Bitcoin, Ethereum, bitcoin::Amount, EtherQuantity> for Sql
118123
),
119124
beta_ledger_refund_identity: (record.ethereum_refund_identity.0).0,
120125
},
126+
record.at,
121127
))
122128
}
123129
}
@@ -139,13 +145,15 @@ struct EthereumBitcoinEtherBitcoinAcceptedSwap {
139145
// Accept fields.
140146
ethereum_redeem_identity: Text<EthereumAddress>,
141147
bitcoin_refund_identity: Text<bitcoin::PublicKey>,
148+
149+
at: NaiveDateTime,
142150
}
143151

144152
#[async_trait]
145153
impl LoadAcceptedSwap<Ethereum, Bitcoin, EtherQuantity, bitcoin::Amount> for Sqlite {
146154
async fn load_accepted_swap(
147155
&self,
148-
key: SwapId,
156+
key: &SwapId,
149157
) -> anyhow::Result<AcceptedSwap<Ethereum, Bitcoin, EtherQuantity, bitcoin::Amount>> {
150158
use schema::{
151159
rfc003_ethereum_bitcoin_accept_messages as accept_messages,
@@ -177,6 +185,7 @@ impl LoadAcceptedSwap<Ethereum, Bitcoin, EtherQuantity, bitcoin::Amount> for Sql
177185
request_messages::secret_hash,
178186
accept_messages::ethereum_redeem_identity,
179187
accept_messages::bitcoin_refund_identity,
188+
accept_messages::at,
180189
))
181190
.filter(accept_messages::swap_id.eq(key))
182191
.first(connection)
@@ -210,6 +219,7 @@ impl LoadAcceptedSwap<Ethereum, Bitcoin, EtherQuantity, bitcoin::Amount> for Sql
210219
*record.bitcoin_refund_identity,
211220
),
212221
},
222+
record.at,
213223
))
214224
}
215225
}
@@ -232,13 +242,15 @@ struct BitcoinEthereumBitcoinErc20AcceptedSwap {
232242
// Accept fields.
233243
bitcoin_redeem_identity: Text<bitcoin::PublicKey>,
234244
ethereum_refund_identity: Text<EthereumAddress>,
245+
246+
at: NaiveDateTime,
235247
}
236248

237249
#[async_trait]
238250
impl LoadAcceptedSwap<Bitcoin, Ethereum, bitcoin::Amount, Erc20Token> for Sqlite {
239251
async fn load_accepted_swap(
240252
&self,
241-
key: SwapId,
253+
key: &SwapId,
242254
) -> anyhow::Result<AcceptedSwap<Bitcoin, Ethereum, bitcoin::Amount, Erc20Token>> {
243255
use schema::{
244256
rfc003_bitcoin_ethereum_accept_messages as accept_messages,
@@ -271,6 +283,7 @@ impl LoadAcceptedSwap<Bitcoin, Ethereum, bitcoin::Amount, Erc20Token> for Sqlite
271283
request_messages::secret_hash,
272284
accept_messages::bitcoin_redeem_identity,
273285
accept_messages::ethereum_refund_identity,
286+
accept_messages::at,
274287
))
275288
.filter(accept_messages::swap_id.eq(key))
276289
.first(connection)
@@ -307,6 +320,7 @@ impl LoadAcceptedSwap<Bitcoin, Ethereum, bitcoin::Amount, Erc20Token> for Sqlite
307320
),
308321
beta_ledger_refund_identity: (record.ethereum_refund_identity.0).0,
309322
},
323+
record.at,
310324
))
311325
}
312326
}
@@ -329,13 +343,15 @@ struct EthereumBitcoinErc20BitcoinAcceptedSwap {
329343
// Accept fields.
330344
ethereum_redeem_identity: Text<EthereumAddress>,
331345
bitcoin_refund_identity: Text<bitcoin::PublicKey>,
346+
347+
at: NaiveDateTime,
332348
}
333349

334350
#[async_trait]
335351
impl LoadAcceptedSwap<Ethereum, Bitcoin, Erc20Token, bitcoin::Amount> for Sqlite {
336352
async fn load_accepted_swap(
337353
&self,
338-
key: SwapId,
354+
key: &SwapId,
339355
) -> anyhow::Result<AcceptedSwap<Ethereum, Bitcoin, Erc20Token, bitcoin::Amount>> {
340356
use schema::{
341357
rfc003_ethereum_bitcoin_accept_messages as accept_messages,
@@ -368,6 +384,7 @@ impl LoadAcceptedSwap<Ethereum, Bitcoin, Erc20Token, bitcoin::Amount> for Sqlite
368384
request_messages::secret_hash,
369385
accept_messages::ethereum_redeem_identity,
370386
accept_messages::bitcoin_refund_identity,
387+
accept_messages::at,
371388
))
372389
.filter(accept_messages::swap_id.eq(key))
373390
.first(connection)
@@ -404,6 +421,7 @@ impl LoadAcceptedSwap<Ethereum, Bitcoin, Erc20Token, bitcoin::Amount> for Sqlite
404421
*record.bitcoin_refund_identity,
405422
),
406423
},
424+
record.at,
407425
))
408426
}
409427
}

cnd/src/db/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ impl Sqlite {
8686
async fn role(&self, key: &SwapId) -> anyhow::Result<Role> {
8787
use self::schema::rfc003_swaps as swaps;
8888

89-
let record: QueryableSwap = self
89+
let record: QueryableSwapRole = self
9090
.do_in_transaction(|connection| {
9191
let key = Text(key);
9292

@@ -118,7 +118,7 @@ fn ensure_folder_tree_exists(path: &Path) -> anyhow::Result<()> {
118118
}
119119

120120
#[derive(Queryable, Debug, Clone, PartialEq)]
121-
struct QueryableSwap {
121+
struct QueryableSwapRole {
122122
pub swap_id: Text<SwapId>,
123123
pub role: Text<Role>,
124124
}

cnd/src/db/save.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,7 @@ impl Save<Request<Ethereum, Bitcoin, Erc20Token, bitcoin::Amount>> for Sqlite {
317317
Ok(())
318318
}
319319
}
320+
320321
#[derive(Insertable, Debug, Copy, Clone)]
321322
#[table_name = "rfc003_ethereum_bitcoin_accept_messages"]
322323
struct InsertableEthereumBitcoinAcceptMessage {

cnd/src/db/schema.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ table! {
7474
swap_id -> Text,
7575
ethereum_redeem_identity -> Text,
7676
bitcoin_refund_identity -> Text,
77+
at -> Timestamp,
7778
}
7879
}
7980

@@ -83,6 +84,7 @@ table! {
8384
swap_id -> Text,
8485
bitcoin_redeem_identity -> Text,
8586
ethereum_refund_identity -> Text,
87+
at -> Timestamp,
8688
}
8789
}
8890

cnd/src/http_api/action.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,9 @@ use crate::{
77
bitcoin::{SendToAddress, SpendOutput},
88
ethereum,
99
},
10-
ledger, SwapId, Timestamp,
10+
ledger, SwapId,
1111
},
12+
timestamp::Timestamp,
1213
};
1314
use anyhow::Context;
1415
use blockchain_contracts::bitcoin::witness;

0 commit comments

Comments
 (0)