Skip to content

Commit 4dabdbe

Browse files
committed
Allow transaction replacement. Txs should not get stuck
1 parent 5a86aa2 commit 4dabdbe

File tree

7 files changed

+387
-153
lines changed

7 files changed

+387
-153
lines changed

src/client/src/bin/http_server.rs

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
use actix_web::web::Data;
22
use actix_web::{middleware, web, App, HttpResponse, HttpServer};
33
use options_lib::OptionsContract;
4-
use options_rpc::ClientArgs;
54
use options_rpc::rpc::OptionOps;
5+
use options_rpc::ClientArgs;
66

7-
use options_rpc::data_structures::{ContractId, InfoResponse, InitArgs, NetworkParams, ContractArgs};
7+
use options_rpc::data_structures::{
8+
ContractArgs, ContractId, InfoResponse, InitArgs, NetworkParams,
9+
};
810

911
async fn info(item: web::Json<ContractId>, args: Data<ClientArgs>) -> HttpResponse {
1012
let db = args.read_options_db();
@@ -31,47 +33,59 @@ async fn list(args: Data<ClientArgs>) -> HttpResponse {
3133
async fn init(init_args: web::Json<InitArgs>, data: Data<ClientArgs>) -> HttpResponse {
3234
let mut db = data.read_options_db();
3335
let e_cli = data.elements_cli();
34-
let net = NetworkParams {network: data.network.clone()};
36+
let net = NetworkParams {
37+
network: data.network.clone(),
38+
};
3539
let res = e_cli.initialize(&net, &init_args, &mut db);
3640
HttpResponse::Ok().json(res) // <- send response
3741
}
3842

3943
async fn fund(fund_args: web::Json<ContractArgs>, data: Data<ClientArgs>) -> HttpResponse {
4044
let db = data.read_options_db();
4145
let e_cli = data.elements_cli();
42-
let net = NetworkParams {network: data.network.clone()};
46+
let net = NetworkParams {
47+
network: data.network.clone(),
48+
};
4349
let res = e_cli.fund(&net, &fund_args, &db);
4450
HttpResponse::Ok().json(res) // <- send response
4551
}
4652

4753
async fn exercise(exercise_args: web::Json<ContractArgs>, data: Data<ClientArgs>) -> HttpResponse {
4854
let db = data.read_options_db();
4955
let e_cli = data.elements_cli();
50-
let net = NetworkParams {network: data.network.clone()};
56+
let net = NetworkParams {
57+
network: data.network.clone(),
58+
};
5159
let res = e_cli.exercise(&net, &exercise_args, &db);
5260
HttpResponse::Ok().json(res) // <- send response
5361
}
5462

5563
async fn cancel(cancel_args: web::Json<ContractArgs>, data: Data<ClientArgs>) -> HttpResponse {
5664
let db = data.read_options_db();
5765
let e_cli = data.elements_cli();
58-
let net = NetworkParams {network: data.network.clone()};
66+
let net = NetworkParams {
67+
network: data.network.clone(),
68+
};
5969
let res = e_cli.cancel(&net, &cancel_args, &db);
6070
HttpResponse::Ok().json(res) // <- send response
6171
}
6272

6373
async fn expiry(expiry_args: web::Json<ContractArgs>, data: Data<ClientArgs>) -> HttpResponse {
6474
let db = data.read_options_db();
6575
let e_cli = data.elements_cli();
66-
let net = NetworkParams {network: data.network.clone()};
76+
let net = NetworkParams {
77+
network: data.network.clone(),
78+
};
6779
let res = e_cli.expiry(&net, &expiry_args, &db);
6880
HttpResponse::Ok().json(res) // <- send response
6981
}
7082

7183
async fn settle(settle_args: web::Json<ContractArgs>, data: Data<ClientArgs>) -> HttpResponse {
7284
let db = data.read_options_db();
7385
let e_cli = data.elements_cli();
74-
let net = NetworkParams {network: data.network.clone()};
86+
let net = NetworkParams {
87+
network: data.network.clone(),
88+
};
7589
let res = e_cli.settle(&net, &settle_args, &db);
7690
HttpResponse::Ok().json(res) // <- send response
7791
}
@@ -102,4 +116,4 @@ async fn main() -> std::io::Result<()> {
102116
.bind(("127.0.0.1", 8080))?
103117
.run()
104118
.await
105-
}
119+
}

src/client/src/bin/opt_cli.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
use std::fmt::Debug;
2-
use std::path::{PathBuf};
3-
use std::{env, panic, process};
41
use options_rpc::data_structures::InfoResponse;
52
use options_rpc::{data_structures, utils};
3+
use std::fmt::Debug;
4+
use std::path::PathBuf;
5+
use std::{env, panic, process};
66

77
use clap::{Parser, Subcommand};
8-
use data_structures::{InitArgs, ContractArgs, ContractId, NetworkParams};
9-
use elementsd::bitcoincore_rpc::{Client};
8+
use data_structures::{ContractArgs, ContractId, InitArgs, NetworkParams};
9+
use elementsd::bitcoincore_rpc::Client;
1010
use options_lib::OptionsContract;
1111
use options_rpc::rpc::OptionOps;
1212

@@ -135,4 +135,4 @@ where
135135
T: ?Sized + Serialize + Debug,
136136
{
137137
println!("{}", serde_json::to_string_pretty(value).unwrap())
138-
}
138+
}

src/client/src/contract.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@ pub struct OptionsBook {
1515
impl OptionsBook {
1616
/// Creates a new [`OptionsBook`].
1717
pub fn new(path: &Path) -> Self {
18-
Self { book: sled::open(path).unwrap() }
18+
Self {
19+
book: sled::open(path).unwrap(),
20+
}
1921
}
2022

2123
/// Gets the contract from the book. Panic if the contract is not found
@@ -27,6 +29,8 @@ impl OptionsBook {
2729
/// Inserts a contract into the book
2830
pub fn insert(&self, contract: &OptionsContract) {
2931
let key = contract.id();
30-
self.book.insert(key.as_inner(), contract.serialize()).unwrap();
32+
self.book
33+
.insert(key.as_inner(), contract.serialize())
34+
.unwrap();
3135
}
3236
}

src/client/src/data_structures.rs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
//! Data structures shared across local client and RPC server
22
3-
use std::path::{PathBuf};
4-
use std::{panic};
3+
use std::panic;
4+
use std::path::PathBuf;
55

6+
use bitcoin::hashes::hex::FromHex;
67
use clap::Args;
78
use elementsd::bitcoincore_rpc::bitcoin;
8-
use bitcoin::hashes::hex::FromHex;
9-
use elementsd::bitcoincore_rpc::{Client};
9+
use elementsd::bitcoincore_rpc::Client;
10+
use options_lib::miniscript::elements::{self, AddressParams, AssetId};
1011
use options_lib::OptionsContract;
11-
use options_lib::miniscript::elements::{AddressParams, AssetId, self};
1212
use secp256k1::hashes::sha256;
13-
use serde::{Serialize, Deserialize};
13+
use serde::{Deserialize, Serialize};
1414

1515
use crate::contract::OptionsBook;
1616
use crate::rpc::OptionOps;
@@ -85,7 +85,6 @@ pub struct InfoResponse {
8585
}
8686

8787
impl InfoResponse {
88-
8988
pub fn from_contract(contract: &OptionsContract, e_cli: &Client) -> Self {
9089
InfoResponse {
9190
id: contract.id(),
@@ -170,4 +169,4 @@ impl ClientArgs {
170169
let data_dir = utils::_data_dir(&self.data_dir);
171170
utils::_new_client(data_dir, self.rpc_port, &self.network, &self.wallet_name)
172171
}
173-
}
172+
}

src/client/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1+
pub mod contract;
12
///! Library for exporting common utilities to the rest of the project.
23
pub mod data_structures;
3-
pub mod contract;
44
pub mod rpc;
55
pub mod utils;
6-
pub use data_structures::ClientArgs;
6+
pub use data_structures::ClientArgs;

0 commit comments

Comments
 (0)