Skip to content

Commit

Permalink
feat: XFL (#11)
Browse files Browse the repository at this point in the history
  • Loading branch information
9oelM committed Sep 13, 2023
1 parent 47693bf commit a6ab7cf
Show file tree
Hide file tree
Showing 10 changed files with 437 additions and 188 deletions.
14 changes: 14 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,20 @@ jobs:
chmod u+x hook-cleaner
echo $PWD >> $GITHUB_PATH
- name: "Install hook guard checker"
run: |
git clone https://github.com/RichardAH/guard-checker/
cd guard-checker
git reset --hard de69e8aa054d49612dda7046962003beb88c0749
make
chmod u+x guard_checker
echo $PWD >> $GITHUB_PATH
- name: "Install wasm-opt"
run: |
cargo install wasm-opt --locked
wasm-opt -h
- name: "Setup Node.js test environment"
uses: actions/setup-node@v3
with:
Expand Down
31 changes: 16 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ XRPL hooks in Rust.

- Install Rust nightly.
- Build and make [`hook-cleaner`](https://github.com/XRPLF/hook-cleaner-c) available in your `PATH`.
- Install [`wasm-opt` from binaryet repo](https://github.com/WebAssembly/binaryen/releases) and make it available in your `PATH`.

## Examples

Expand Down Expand Up @@ -49,28 +50,28 @@ Emitted transaction

Float

- [ ] `float_set`
- [ ] `float_multiply`
- [ ] `float_mulratio`
- [ ] `float_negate`
- [ ] `float_compare`
- [ ] `float_sum`
- [x] `float_set`
- [x] `float_multiply`
- [x] `float_mulratio`
- [x] `float_negate`
- [x] `float_compare`
- [x] `float_sum`
- [ ] `float_sto`
- [ ] `float_sto_set`
- [ ] `float_invert`
- [ ] `float_divide`
- [ ] `float_one`
- [ ] `float_exponent`
- [ ] `float_mantissa`
- [ ] `float_sign`
- [ ] `float_int`
- [x] `float_sto_set`
- [x] `float_invert`
- [x] `float_divide`
- [x] `float_one`
- [x] `float_exponent`
- [x] `float_mantissa`
- [x] `float_sign`
- [x] `float_int`
- [ ] `float_root`
- [ ] `float_log`

Ledger

- [ ] `fee_base`
- [ ] `ledger_seq`
- [x] `ledger_seq`
- [ ] `ledger_last_hash`
- [ ] `ledger_last_time`
- [ ] `ledger_nonce`
Expand Down
6 changes: 0 additions & 6 deletions c/extern.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,6 @@ float_compare(int64_t float1, int64_t float2, uint32_t mode);
extern int64_t
float_divide(int64_t float1, int64_t float2);

extern int64_t
float_exponent(int64_t float1);

extern int64_t
float_exponent_set(int64_t float1, int32_t exponent);

extern int64_t
float_int(int64_t float1, uint32_t decimal_places, uint32_t abs);

Expand Down
141 changes: 141 additions & 0 deletions examples/float.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
//! A hook that accepts any transaction coming through it

#![no_std]
#![no_main]
use core::ops::Neg;

use hooks_rs::*;

#[no_mangle]
pub extern "C" fn cbak(_: u32) -> i64 {
0
}

// TODO: more test cases
#[no_mangle]
pub extern "C" fn hook(_: u32) -> i64 {
max_iter(1);
// mulratio
if XFL::one().mulratio(false, 1, 2).unwrap_line_number()
!= XFL::one().mulratio(false, 5, 10).unwrap_line_number()
{
rollback(b"", line!().into());
}
let decimal_10 = XFL::new(-14, 1000000000000000).unwrap_line_number();
if XFL::one().mulratio(false, 10, 1).unwrap_line_number() != decimal_10 {
rollback(b"", line!().into());
};

// new
let plus_1000 = XFL::new(-12, 1000000000000000).unwrap_line_number();
let plus_999 = XFL::new(-13, 9990000000000000).unwrap_line_number();
let plus_998 = XFL::new(-13, 9980000000000000).unwrap_line_number();
let minus_1000 = (-plus_1000).unwrap_line_number();
let minus_999 = (-plus_999).unwrap_line_number();
let minus_998 = (-plus_998).unwrap_line_number();

// PartialOrd
if minus_1000 > minus_999 {
rollback(b"", line!().into());
}
if minus_999 > minus_998 {
rollback(b"", line!().into());
}
if minus_1000 > minus_998 {
rollback(b"", line!().into());
}
if plus_1000 <= plus_999 {
rollback(b"", line!().into());
}
if plus_999 <= plus_998 {
rollback(b"", line!().into());
}
if plus_1000 <= plus_998 {
rollback(b"", line!().into());
}

// exponent & mantissa
if minus_1000.exponent() != -12 {
rollback(b"", line!().into());
}
if minus_1000.mantissa() != 1000000000000000 {
rollback(b"", line!().into());
}
if minus_999.exponent() != -13 {
rollback(b"", line!().into());
}
if minus_999.mantissa() != 9990000000000000 {
rollback(b"", line!().into());
}
if minus_998.exponent() != -13 {
rollback(b"", line!().into());
}
if minus_998.mantissa() != 9980000000000000 {
rollback(b"", line!().into());
}

// to_int64
// 3.14
let approx_pi = XFL::new(-15, 3140000000000000).unwrap_line_number();

if 3 != approx_pi.to_int64(0, false).unwrap_line_number() {
rollback(b"", line!().into());
}

// negation
// 1.220111606619773e+32
let a = XFL::new(17, 1220111606619773).unwrap_line_number();
let expected_negated_enclosing: i64 = 2054861541687565949;
if (-a).unwrap_line_number().0 != expected_negated_enclosing {
rollback(b"", line!().into());
}

// multiplication
// 3.845483684710632e-29
let a = XFL::new(-44, 3845483684710632).unwrap_line_number();
// 3.604275125235076e-32
let b = XFL::new(-47, 3604275125235076).unwrap_line_number();
// 1.386018118929985e-60
let expected_multiplication_enclosing = 5009388803754921537;
if (a * b).unwrap_line_number().0 != expected_multiplication_enclosing {
rollback(b"", line!().into());
}

// division
// -0.0991285223666045
let a = XFL::new(-17, 9912852236660450)
.unwrap_line_number()
.neg()
.unwrap_line_number();
// 1.663822854409434e+29
let b = XFL::new(14, 1663822854409434).unwrap_line_number();
// -5.957877192508556e-31
let expected_division_result = XFL::new(
-46,
// TODO: Not sure if https://richardah.github.io/xfl-tools/ is giving a wrong
// float number because it's javascript, or if this is a bug in xrpld (low chance tho).
// Originally it shows 5957877192508556, but trace_float of the result shows 5957877192508580.
// For now we'll just use the result from trace_float, but need to confirm what's going on
5957877192508580,
)
.unwrap_line_number()
.neg()
.unwrap_line_number();
if (a / b).unwrap_line_number() != expected_division_result {
rollback(b"", line!().into());
}

// addition
// 123123123123123
let a = XFL::new(-1, 1231231231231230).unwrap_line_number();
// 55555555
let b = XFL::new(-8, 5555555500000000).unwrap_line_number();
// 123123178678678
let expected_addition_enclosing = 6342299507124445148;
if (a + b).unwrap_line_number().0 != expected_addition_enclosing {
rollback(b"", line!().into());
}

// Accept all
accept(b"", 0);
}
7 changes: 3 additions & 4 deletions src/api/etxn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,9 @@ pub fn etxn_details() -> Result<[u8; EMIT_DETAILS_SIZE]> {
/// Estimate the required fee for a txn to be emitted successfully
#[inline(always)]
pub fn etxn_fee_base(tx_blob: &mut [u8]) -> Result<XFL> {
let result: Result<XFL> =
unsafe { c::etxn_fee_base(tx_blob.as_mut_ptr() as u32, tx_blob.len() as u32).into() };

result
XFL::from_verified_i64(unsafe {
c::etxn_fee_base(tx_blob.as_mut_ptr() as u32, tx_blob.len() as u32)
})
}

/// Generate a 32 byte nonce for use in an emitted transaction
Expand Down

0 comments on commit a6ab7cf

Please sign in to comment.