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

Commit eb40348

Browse files
bors[bot]Tobin C. Hardingthomaseizinger
authored
Merge #1666
1666: Store and load swaps from SQLite + respawn the state machine on startup r=tcharding a=tcharding This PR is the final missing piece of the database implementation. - Moves the metadata store to the database. - Loads swaps on startup from the database. - Respawns the state machines for accepted swaps. # Highly recommend reviewing this one patch by patch! Resolves: #1243 (the persistent storage epic - Epic!) Co-authored-by: Tobin C. Harding <[email protected]> Co-authored-by: Thomas Eizinger <[email protected]>
2 parents 3b1147f + 9dbee00 commit eb40348

File tree

36 files changed

+1568
-881
lines changed

36 files changed

+1568
-881
lines changed

Cargo.lock

Lines changed: 106 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import { createActors } from "../../../lib_sdk/create_actors";
2+
3+
setTimeout(function() {
4+
describe("cnd can be restarted", function() {
5+
this.timeout(60000);
6+
it("after the swap was accepted", async function() {
7+
const { alice, bob } = await createActors(
8+
"cnd_can_be_restarted.log"
9+
);
10+
11+
await alice.sendRequest();
12+
await bob.accept();
13+
14+
await alice.currentSwapIsAccepted();
15+
await bob.currentSwapIsAccepted();
16+
17+
await alice.restart();
18+
await bob.restart();
19+
20+
await alice.currentSwapIsAccepted();
21+
await bob.currentSwapIsAccepted();
22+
});
23+
});
24+
run();
25+
}, 0);

api_tests/e2e/rfc003/btc_eth/does_not_persist_data.ts

Lines changed: 0 additions & 20 deletions
This file was deleted.

api_tests/lib/cnd_runner.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
1+
import * as fs from "fs";
2+
import { promisify } from "util";
13
import { CndInstance } from "../lib_sdk/cnd_instance";
24
import { CND_CONFIGS } from "./config";
35
import { LedgerConfig } from "./ledger_runner";
46

7+
const unlinkAsync = promisify(fs.unlink);
8+
const existsAsync = promisify(fs.exists);
9+
510
export class CndRunner {
611
private runningNodes: { [key: string]: CndInstance };
712

@@ -38,6 +43,10 @@ export class CndRunner {
3843
ledgerConfig
3944
);
4045

46+
if (await existsAsync(cndconfig.dbPath)) {
47+
await unlinkAsync(cndconfig.dbPath); // delete the old database for the new test
48+
}
49+
4150
await process.start();
4251

4352
return {

api_tests/lib/config.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ export interface HttpApi {
1515

1616
export class E2ETestActorConfig {
1717
public readonly seed: Uint8Array;
18+
public readonly dbPath: string;
1819

1920
constructor(
2021
public readonly httpApiPort: number,
@@ -25,10 +26,10 @@ export class E2ETestActorConfig {
2526
this.httpApiPort = httpApiPort;
2627
this.comitPort = comitPort;
2728
this.seed = new Uint8Array(Buffer.from(seed, "hex"));
29+
this.dbPath = tempfile(`.${this.name}.sqlite`);
2830
}
2931

3032
public generateCndConfigFile(ledgerConfig: LedgerConfig): CndConfigFile {
31-
const dbPath = tempfile(`.${this.name}.sqlite`);
3233
return {
3334
http_api: {
3435
socket: {
@@ -37,7 +38,7 @@ export class E2ETestActorConfig {
3738
},
3839
},
3940
database: {
40-
sqlite: dbPath,
41+
sqlite: this.dbPath,
4142
},
4243
network: {
4344
listen: [`/ip4/0.0.0.0/tcp/${this.comitPort}`],

api_tests/lib_sdk/actors/actor.ts

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import "../../lib/setup_chai";
1010
import { Asset, AssetKind } from "../asset";
1111
import { CndInstance } from "../cnd_instance";
1212
import { Ledger, LedgerKind } from "../ledger";
13+
import { sleep } from "../utils";
1314
import { Wallets } from "../wallets";
1415
import { Actors } from "./index";
1516

@@ -47,7 +48,10 @@ export class Actor {
4748
const logger = loggerFactory(name);
4849
logger.level = "debug";
4950

50-
logger.info("Created new actor with config %s", actorConfig);
51+
logger.info(
52+
"Created new actor with config %s",
53+
JSON.stringify(actorConfig.generateCndConfigFile(ledgerConfig))
54+
);
5155

5256
return new Actor(logger, cndInstance);
5357
}
@@ -183,12 +187,24 @@ export class Actor {
183187
await this.swap.redeem(Actor.defaultActionConfig);
184188
}
185189

186-
public async assertHasNoSwaps() {
187-
this.logger.debug("Checking if we have 0 swaps");
190+
public async currentSwapIsAccepted() {
191+
let swapEntity;
192+
193+
do {
194+
swapEntity = await this.swap.getEntity();
195+
196+
await sleep(200);
197+
} while (
198+
swapEntity.properties.state.communication.status !== "ACCEPTED"
199+
);
200+
}
201+
202+
public async assertHasCurrentSwap() {
203+
this.logger.debug("Checking if we can fetch the current swap");
188204

189-
const swaps = await this.cnd.getSwaps();
205+
const response = await this.cnd.fetch(this.swap.self);
190206

191-
expect(swaps).to.have.length(0);
207+
return response;
192208
}
193209

194210
public async assertSwapped() {

cnd/Cargo.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ description = "Reference implementation of a COMIT network daemon."
77

88
[dependencies]
99
anyhow = "1"
10+
async-std = "1"
11+
async-trait = "0.1"
1012
binary_macros = "0.6"
1113
bitcoin = "0.19.1"
1214
blockchain_contracts = "0.1"
@@ -38,6 +40,7 @@ log = { version = "0.4", features = ["serde"] }
3840
maplit = "1"
3941
mime = "0.3"
4042
mime_guess = "2.0"
43+
paste = "0.1"
4144
pem = "0.7"
4245
rand = "0.7"
4346
reqwest = { version = "0.9", default-features = false }
@@ -65,7 +68,6 @@ bitcoincore-rpc = "0.8.0-rc1"
6568
maplit = "1"
6669
matches = "0.1.8"
6770
memsocket = "0.1"
68-
paste = "0.1"
6971
pretty_env_logger = "0.3"
7072
quickcheck = "0.9.0"
7173
regex = "1.3.1"

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,4 @@ DROP TABLE rfc003_ethereum_bitcoin_erc20_bitcoin_request_messages;
77
DROP TABLE rfc003_ethereum_bitcoin_accept_messages;
88
DROP TABLE rfc003_bitcoin_ethereum_accept_messages;
99
DROP TABLE rfc003_decline_messages;
10+
DROP TABLE rfc003_swaps;

0 commit comments

Comments
 (0)