Skip to content
This repository has been archived by the owner on Apr 15, 2024. It is now read-only.

Test runestone decipher #1

Merged
merged 3 commits into from
Sep 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 67 additions & 0 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
name: CI

on:
push:
branches:
- master
pull_request:
branches:
- master

defaults:
run:
shell: bash

env:
RUSTFLAGS: --deny warnings

jobs:
lint:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2

- name: Install Rust Toolchain Components
uses: actions-rs/toolchain@v1
with:
components: clippy, rustfmt
override: true
toolchain: stable

- uses: Swatinem/rust-cache@v2

- name: Clippy
run: cargo clippy --all --all-targets

- name: Format
run: cargo fmt --all -- --check

- name: Check for Forbidden Words
run: |
sudo apt-get install ripgrep
./bin/forbid

test:
strategy:
matrix:
os:
- macos-latest
- ubuntu-latest
- windows-latest

runs-on: ${{matrix.os}}

steps:
- uses: actions/checkout@v2

- name: Install Rust Toolchain Components
uses: actions-rs/toolchain@v1
with:
profile: minimal
toolchain: stable

- uses: Swatinem/rust-cache@v2

- name: Test
run: cargo test --all
8 changes: 8 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,17 @@ version = "0.0.0"
edition = "2021"
description = "Runes"
license = "CC0-1.0"
autotests = false

[dependencies]
bitcoin = "0.30.1"
clap = { version = "4.4.5", features = ["derive"] }
serde = { version = "1.0.188", features = ["derive"] }
serde_json = "1.0.107"

[dev-dependencies]
executable-path = "1.0.0"

[[test]]
name = "integration"
path = "tests/lib.rs"
13 changes: 13 additions & 0 deletions bin/forbid
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#!/usr/bin/env bash

set -euo pipefail

which rg > /dev/null

! rg \
--glob '!bin/forbid' \
--glob '!docs/src/bounty/frequency.tsv' \
--glob '!docs/po/*' \
--ignore-case \
'dbg!|fixme|todo|xxx' \
.
11 changes: 11 additions & 0 deletions justfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,14 @@ set positional-arguments

watch +args='test':
cargo watch --clear --exec '{{args}}'

ci: clippy forbid
cargo fmt -- --check
cargo test --all
cargo test --all -- --ignored

forbid:
./bin/forbid

clippy:
cargo clippy --all --all-targets -- -D warnings
18 changes: 9 additions & 9 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ impl std::error::Error for Error {}

impl Runestone {
pub fn decipher(transaction: &Transaction) -> Result<Option<Self>, Error> {
let Some(payload) = Runestone::payload(&transaction)? else {
let Some(payload) = Runestone::payload(transaction)? else {
return Ok(None);
};

Expand Down Expand Up @@ -143,7 +143,7 @@ mod tests {
Runestone::decipher(&Transaction {
input: Vec::new(),
output: vec![TxOut {
script_pubkey: script::Builder::new().push_slice(&[]).into_script(),
script_pubkey: script::Builder::new().push_slice([]).into_script(),
value: 0
}],
lock_time: locktime::absolute::LockTime::ZERO,
Expand Down Expand Up @@ -315,7 +315,7 @@ mod tests {
script_pubkey: script::Builder::new()
.push_opcode(opcodes::all::OP_RETURN)
.push_slice(b"RUNE_TEST")
.push_slice(&payload)
.push_slice(payload)
.into_script(),
value: 0
}],
Expand Down Expand Up @@ -354,7 +354,7 @@ mod tests {
script_pubkey: script::Builder::new()
.push_opcode(opcodes::all::OP_RETURN)
.push_slice(b"RUNE_TEST")
.push_slice(&payload)
.push_slice(payload)
.into_script(),
value: 0
}],
Expand Down Expand Up @@ -382,11 +382,11 @@ mod tests {
script_pubkey: script::Builder::new()
.push_opcode(opcodes::all::OP_RETURN)
.push_slice(b"RUNE_TEST")
.push_slice(&1u128.to_le_bytes())
.push_slice(&2u128.to_le_bytes())
.push_slice(&3u128.to_le_bytes())
.push_slice(&4u128.to_le_bytes())
.push_slice(&5u128.to_le_bytes())
.push_slice(1u128.to_le_bytes())
.push_slice(2u128.to_le_bytes())
.push_slice(3u128.to_le_bytes())
.push_slice(4u128.to_le_bytes())
.push_slice(5u128.to_le_bytes())
.into_script(),
value: 0
}],
Expand Down
1 change: 1 addition & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ fn main() -> Result<(), Box<dyn Error>> {
let message = Runestone::decipher(&transaction)?;

serde_json::to_writer_pretty(&io::stdout(), &message)?;
println!();
}
}

Expand Down
79 changes: 79 additions & 0 deletions tests/decipher.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
use super::*;

#[test]
fn transaction_with_no_runestone_returns_null() {
let child = Command::new(executable_path("runestone"))
.arg("decipher")
.stdin(Stdio::piped())
.stdout(Stdio::piped())
.stderr(Stdio::piped())
.spawn()
.unwrap();

let transaction = Transaction {
input: Vec::new(),
output: Vec::new(),
lock_time: locktime::absolute::LockTime::ZERO,
version: 0,
};

let mut buffer = Vec::new();

transaction.consensus_encode(&mut buffer).unwrap();

child.stdin.as_ref().unwrap().write_all(&buffer).unwrap();

let output = child.wait_with_output().unwrap();

assert!(output.status.success());

let stdout = str::from_utf8(&output.stdout).unwrap();

assert_eq!(stdout, "null\n");
}

#[test]
fn transaction_with_runestone_returns_serialized_runestone() {
let child = Command::new(executable_path("runestone"))
.arg("decipher")
.stdin(Stdio::piped())
.stdout(Stdio::piped())
.stderr(Stdio::piped())
.spawn()
.unwrap();

let transaction = Transaction {
input: Vec::new(),
output: vec![TxOut {
script_pubkey: script::Builder::new()
.push_opcode(opcodes::all::OP_RETURN)
.push_slice(b"RUNE_TEST")
.into_script(),
value: 0,
}],
lock_time: locktime::absolute::LockTime::ZERO,
version: 0,
};

let mut buffer = Vec::new();

transaction.consensus_encode(&mut buffer).unwrap();

child.stdin.as_ref().unwrap().write_all(&buffer).unwrap();

let output = child.wait_with_output().unwrap();

assert!(output.status.success());

let stdout = str::from_utf8(&output.stdout).unwrap();

assert_eq!(
stdout,
r#"{
"directives": [],
"decimals": null,
"symbol": null
}
"#
);
}
11 changes: 11 additions & 0 deletions tests/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
use {
bitcoin::{consensus::Encodable, locktime, opcodes, script, Transaction, TxOut},
executable_path::executable_path,
std::{
io::Write,
process::{Command, Stdio},
str,
},
};

mod decipher;
Loading