Skip to content

Commit

Permalink
ci with postgres
Browse files Browse the repository at this point in the history
  • Loading branch information
bennyhodl committed Feb 7, 2025
1 parent 2215e27 commit 0e62e28
Show file tree
Hide file tree
Showing 3 changed files with 331 additions and 18 deletions.
61 changes: 61 additions & 0 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,23 @@ jobs:
check:
name: check
runs-on: ubuntu-latest
services:
postgres:
image: postgres:latest
env:
POSTGRES_DB: testdb
POSTGRES_PASSWORD: postgres
ports:
- 5432:5432
options: >-
--health-cmd pg_isready
--health-interval 10s
--health-timeout 5s
--health-retries 5
env:
DATABASE_URL: postgresql://postgres:postgres@localhost:5432/testdb

steps:
- name: Install Protoc
run: sudo apt-get update && sudo apt-get install -y protobuf-compiler
Expand All @@ -46,13 +63,34 @@ jobs:
with:
toolchain: nightly
override: true
- name: Install sqlx-cli
run: cargo install sqlx-cli
- name: Run database migrations
run: cargo sqlx migrate run --source ddk/src/storage/postgres/migrations
env:
DATABASE_URL: postgresql://postgres:postgres@localhost:5432/testdb
- name: cargo check
run: cargo check --all-features
env:
DATABASE_URL: postgresql://postgres:postgres@localhost:5432/testdb
integration_tests_prepare:
runs-on: ubuntu-latest
timeout-minutes: 30
outputs:
matrix: ${{ steps.set-matrix.outputs.matrix }}
services:
postgres:
image: postgres:latest
env:
POSTGRES_DB: testdb
POSTGRES_PASSWORD: postgres
ports:
- 5432:5432
options: >-
--health-cmd pg_isready
--health-interval 10s
--health-timeout 5s
--health-retries 5
steps:
- name: Install Protoc
run: sudo apt-get update && sudo apt-get install -y protobuf-compiler
Expand All @@ -62,6 +100,12 @@ jobs:
toolchain: nightly
components: rustfmt
override: true
- name: Install sqlx-cli
run: cargo install sqlx-cli
- name: Run database migrations
run: cargo sqlx migrate run --source ddk/src/storage/postgres/migrations
env:
DATABASE_URL: postgresql://postgres:postgres@localhost:5432/testdb
- uses: actions/cache@v3
env:
cache-name: test-cache
Expand All @@ -71,11 +115,28 @@ jobs:
- uses: actions/checkout@v4
- id: set-matrix
run: cargo test --no-run --all-features && echo "::set-output name=matrix::$(testconfig/scripts/get_test_list.sh manager_execution manager_tests contract_updater)"
env:
DATABASE_URL: postgresql://postgres:postgres@localhost:5432/testdb
integration_tests:
name: integration tests
needs: integration_tests_prepare
runs-on: ubuntu-latest
timeout-minutes: 30
services:
postgres:
image: postgres:latest
env:
POSTGRES_DB: testdb
POSTGRES_PASSWORD: postgres
ports:
- 5432:5432
options: >-
--health-cmd pg_isready
--health-interval 10s
--health-timeout 5s
--health-retries 5
env:
DATABASE_URL: postgresql://postgres:postgres@localhost:5432/testdb
strategy:
matrix:
tests: ${{ fromJson(needs.integration_tests_prepare.outputs.matrix) }}
Expand Down
133 changes: 133 additions & 0 deletions ddk-manager/src/taproot/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
use bitcoin::opcodes::all::*;
use bitcoin::taproot::LeafVersion;
use bitcoin::TapNodeHash;
use bitcoin::{
absolute::LockTime, script::Builder, transaction::Version, Amount, ScriptBuf, Transaction,
TxOut, XOnlyPublicKey,
};
use dlc::{DlcTransactions, Payout, TxInputInfo};
use secp256k1_zkp::{rand, All, Keypair, Secp256k1};
use serde::{Deserialize, Serialize};
use thiserror::Error;

#[derive(Error, Debug, Clone)]
enum TaprootDlcError {
#[error("Not a taproor script pubkey")]
NotTaproot,
#[error("Adaptor Signature is not valid.")]
InvalidAdaptorSignature,
#[error("Numeric contracts not supported")]
NumericContract,
#[error("Secp error")]
Secp,
#[error("Generating Address")]
GetAddress,
#[error("{0}")]
General(String),
#[error("Esplora skill issue.")]
Esplora,
#[error("Oracle error")]
Oracle,
}

/// Contains the parameters required for creating DLC transactions for a single
/// party. Specifically these are the common fields between Offer and Accept
/// messages.
#[derive(Clone, Debug)]
#[cfg_attr(
feature = "use-serde",
derive(Serialize, Deserialize),
serde(rename_all = "camelCase")
)]
pub struct TaprootPartyParams {
/// The public key for the fund multisig script
pub fund_pubkey: XOnlyPublicKey,
/// An address to receive change
pub change_script_pubkey: ScriptBuf,
/// Id used to order fund outputs
pub change_serial_id: u64,
/// An address to receive the outcome amount
pub payout_script_pubkey: ScriptBuf,
/// Id used to order CET outputs
pub payout_serial_id: u64,
/// A list of inputs to fund the contract
pub inputs: Vec<TxInputInfo>,
/// The sum of the inputs values.
pub input_amount: Amount,
/// The collateral put in the contract by the party
pub collateral: Amount,
}

fn create_taproot_dlc_transactions(
secp: &Secp256k1<All>,
offer_params: &TaprootPartyParams,
accept_params: &TaprootPartyParams,
payouts: &[Payout],
refund_lock_time: u32,
fee_rate_per_vb: u64,
fund_lock_time: u32,
cet_lock_time: u32,
fund_output_serial_id: u64,
) -> Result<DlcTransactions, TaprootDlcError> {
let (funding_transaction, funding_script) = create_funding_transaction(
secp,
&offer_params.fund_pubkey,
&accept_params.fund_pubkey,
accept_params.collateral,
offer_params.collateral,
)?;

todo!()
}

fn create_funding_transaction(
secp: &Secp256k1<All>,
offer_pubkey: &XOnlyPublicKey,
accept_pubkey: &XOnlyPublicKey,
accept: Amount,
offer: Amount,
) -> Result<(Transaction, ScriptBuf), TaprootDlcError> {
let funding_script = create_funding_script(secp, offer_pubkey, accept_pubkey)?;

let transaction = Transaction {
version: Version::TWO,
lock_time: LockTime::ZERO,
input: vec![],
output: vec![TxOut {
value: accept + offer,
script_pubkey: funding_script.clone(),
}],
};

Ok((transaction, funding_script))
}

fn create_funding_script(
secp: &Secp256k1<All>,
offer_pubkey: &XOnlyPublicKey,
accept_pubkey: &XOnlyPublicKey,
) -> Result<ScriptBuf, TaprootDlcError> {
// Can use the serial ordering in rust-dlc insteaf
let (first_pubkey, second_pubkey) = if offer_pubkey < accept_pubkey {
(offer_pubkey, accept_pubkey)
} else {
(accept_pubkey, offer_pubkey)
};

let script_spend = Builder::new()
.push_x_only_key(&first_pubkey)
.push_opcode(OP_CHECKSIG)
.push_x_only_key(&second_pubkey)
.push_opcode(OP_CHECKSIGADD)
.push_int(2)
.push_opcode(OP_NUMEQUALVERIFY)
.into_script();

let tap_tree = TapNodeHash::from_script(script_spend.as_script(), LeafVersion::TapScript);

// Does this need to be stored? Or is it just a throwaway key?
let internal_key = Keypair::new(secp, &mut rand::thread_rng());
let internal_pubkey = internal_key.x_only_public_key().0;

Ok(ScriptBuf::new_p2tr(&secp, internal_pubkey, Some(tap_tree)))
}
Loading

0 comments on commit 0e62e28

Please sign in to comment.