Skip to content

Commit

Permalink
test(e2e): test sha256 TLC
Browse files Browse the repository at this point in the history
  • Loading branch information
doitian committed Jun 28, 2024
1 parent ac39259 commit 871095b
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 40 deletions.
28 changes: 17 additions & 11 deletions .github/workflows/e2e.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,24 @@ jobs:
- invoice-ops
- open-use-close-a-channel
- udt
- xudt
- reestablish
release:
- "0.116.1"
name: e2e test for ${{ matrix.workflow }}
test_env:
- "test"
extra_bru_args:
- ""
include:
# add an extra workflow to run udt using the env file xudt-test
- workflow: "udt"
test_env: "xudt-test"
release: "0.116.1"
# add an extra workflow to run 3-nodes-transfer with sha256 hash algorithm
- workflow: "3-nodes-transfer"
test_env: "test"
release: "0.116.1"
extra_bru_args: "--env-var HASH_ALGORITHM=sha256"
name: e2e test for ${{ matrix.workflow }} --env ${{ matrix.test_env }} ${{ matrix.extra_bru_args }}
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
Expand All @@ -29,7 +42,7 @@ jobs:
tar -xvaf "ckb_v${version}_x86_64-unknown-linux-gnu-portable.tar.gz"
sudo mv "ckb_v${version}_x86_64-unknown-linux-gnu-portable"/* /usr/local/bin/
- name: Start nodes
- name: Run e2e workflow
run: |
# Prebuild the program so that we can run the following script faster
cargo build
Expand All @@ -55,14 +68,7 @@ jobs:
fi
done
test_dir="${{ matrix.workflow }}"
test_env="test"
if [[ "${{ matrix.workflow }}" == "xudt" ]]; then
test_dir="udt"
test_env="xudt-test"
fi
(cd ./tests/bruno; npm exec -- @usebruno/cli run e2e/${test_dir} -r --env ${test_env} ) &
(cd ./tests/bruno; npm exec -- @usebruno/cli run e2e/${{ matrix.workflow }} -r --env ${{ matrix.test_env }} ${{ matrix.extra_bru_args }} ) &
# -n means we will exit when any of the background processes exits.
# https://www.gnu.org/software/bash/manual/bash.html#index-wait
Expand Down
17 changes: 15 additions & 2 deletions src/invoice/invoice_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ use bitcoin::{
Message, PublicKey,
},
};
use ckb_hash::blake2b_256;
use ckb_types::{
packed::{Byte, Script},
prelude::{Pack, Unpack},
Expand Down Expand Up @@ -566,6 +565,10 @@ impl InvoiceBuilder {
self.add_attr(Attribute::UdtScript(CkbScript(script)))
}

pub fn hash_algorithm(self, algorithm: HashAlgorithm) -> Self {
self.add_attr(Attribute::HashAlgorithm(algorithm))
}

attr_setter!(description, Description, String);
attr_setter!(payee_pub_key, PayeePublicKey, PublicKey);
attr_setter!(expiry_time, ExpiryTime, Duration);
Expand All @@ -579,7 +582,16 @@ impl InvoiceBuilder {
return Err(InvoiceError::BothPaymenthashAndPreimage);
}
let payment_hash: Hash256 = if let Some(preimage) = preimage {
blake2b_256(preimage.as_ref()).into()
let algo = self
.attrs
.iter()
.find_map(|attr| match attr {
Attribute::HashAlgorithm(algo) => Some(algo),
_ => None,
})
.copied()
.unwrap_or_default();
algo.hash(preimage.as_ref()).into()
} else if let Some(payment_hash) = self.payment_hash {
payment_hash
} else {
Expand Down Expand Up @@ -721,6 +733,7 @@ mod tests {
key::{KeyPair, Secp256k1},
secp256k1::SecretKey,
};
use ckb_hash::blake2b_256;
use std::time::{SystemTime, UNIX_EPOCH};

fn gen_rand_public_key() -> PublicKey {
Expand Down
5 changes: 5 additions & 0 deletions src/rpc/invoice.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::time::Duration;

use crate::ckb::hash_algorithm::HashAlgorithm;
use crate::ckb::serde_utils::{U128Hex, U64Hex};
use crate::ckb::types::Hash256;
use crate::invoice::{CkbInvoice, Currency, InvoiceBuilder, InvoiceStore};
Expand All @@ -25,6 +26,7 @@ pub struct NewInvoiceParams {
#[serde_as(as = "Option<U64Hex>")]
pub final_htlc_timeout: Option<u64>,
pub udt_type_script: Option<Script>,
pub hash_algorithm: Option<HashAlgorithm>,
}

#[derive(Clone, Serialize, Deserialize)]
Expand Down Expand Up @@ -96,6 +98,9 @@ where
if let Some(udt_type_script) = &params.udt_type_script {
invoice_builder = invoice_builder.udt_type_script(udt_type_script.clone().into());
};
if let Some(hash_algorithm) = params.hash_algorithm {
invoice_builder = invoice_builder.hash_algorithm(hash_algorithm);
};

match invoice_builder.build() {
Ok(invoice) => match self.store.insert_invoice(invoice.clone()) {
Expand Down
33 changes: 20 additions & 13 deletions tests/bruno/e2e/3-nodes-transfer/11-node3-gen-invoice.bru
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,6 @@ headers {
Accept: application/json
}

script:pre-request {
// generate random preimage
function generateRandomPreimage() {
let hash = '0x';
for (let i = 0; i < 64; i++) {
hash += Math.floor(Math.random() * 16).toString(16);
}
return hash;
}
const payment_preimage = generateRandomPreimage();
bru.setVar("payment_preimage", payment_preimage);
}

body:json {
{
"id": "42",
Expand All @@ -51,6 +38,26 @@ assert {
res.body.result: isDefined
}

script:pre-request {
// generate random preimage
function generateRandomPreimage() {
let hash = '0x';
for (let i = 0; i < 64; i++) {
hash += Math.floor(Math.random() * 16).toString(16);
}
return hash;
}
const payment_preimage = generateRandomPreimage();
bru.setVar("payment_preimage", payment_preimage);
let hash_algorithm = bru.getEnvVar("HASH_ALGORITHM");
if (hash_algorithm !== null) {
let body = req.getBody();
body.params[0].payment_preimage = payment_preimage;
body.params[0].hash_algorithm = hash_algorithm;
req.setBody(body);
}
}

script:post-response {
// Sleep for sometime to make sure current operation finishes before next request starts.
await new Promise(r => setTimeout(r, 100));
Expand Down
33 changes: 20 additions & 13 deletions tests/bruno/e2e/3-nodes-transfer/16-node3-gen-invoice.bru
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,6 @@ headers {
Accept: application/json
}

script:pre-request {
// generate random preimage
function generateRandomPreimage() {
let hash = '0x';
for (let i = 0; i < 64; i++) {
hash += Math.floor(Math.random() * 16).toString(16);
}
return hash;
}
const payment_preimage = generateRandomPreimage();
bru.setVar("payment_preimage", payment_preimage);
}

body:json {
{
"id": "42",
Expand All @@ -51,6 +38,26 @@ assert {
res.body.result: isDefined
}

script:pre-request {
// generate random preimage
function generateRandomPreimage() {
let hash = '0x';
for (let i = 0; i < 64; i++) {
hash += Math.floor(Math.random() * 16).toString(16);
}
return hash;
}
const payment_preimage = generateRandomPreimage();
bru.setVar("payment_preimage", payment_preimage);
let hash_algorithm = bru.getEnvVar("HASH_ALGORITHM");
if (hash_algorithm !== null) {
let body = req.getBody();
body.params[0].payment_preimage = payment_preimage;
body.params[0].hash_algorithm = hash_algorithm;
req.setBody(body);
}
}

script:post-response {
// Sleep for sometime to make sure current operation finishes before next request starts.
await new Promise(r => setTimeout(r, 100));
Expand Down
3 changes: 2 additions & 1 deletion tests/nodes/start.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#!/usr/bin/env bash

set -euo pipefail
export SHELLOPTS
export RUST_BACKTRACE=full RUST_LOG=info,cfn_node=debug

Expand Down Expand Up @@ -65,4 +66,4 @@ while true; do
exit 1
fi
sleep 1
done
done

0 comments on commit 871095b

Please sign in to comment.