Skip to content

Commit

Permalink
Merge pull request #797 from Chia-Network/fix-offer-memos
Browse files Browse the repository at this point in the history
  • Loading branch information
Rigidity authored Nov 22, 2024
2 parents 03f2a8d + 64e841f commit 96b0c0a
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 4 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions crates/chia-puzzles/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ arbitrary = { workspace = true, features = ["derive"], optional = true }

[dev-dependencies]
hex = { workspace = true }
anyhow = { workspace = true }

[lib]
crate-type = ["rlib"]
113 changes: 109 additions & 4 deletions crates/chia-puzzles/src/puzzles/offer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,20 +25,31 @@ pub struct NotarizedPayment {
pub struct Payment {
pub puzzle_hash: Bytes32,
pub amount: u64,
#[clvm(default)]
pub memos: Vec<Bytes>,
/// The memos should usually be set to [`None`] instead of an empty list.
/// This is for compatibility with the way the reference wallet encodes offers.
#[clvm(rest)]
pub memos: Option<Memos>,
}

#[derive(Debug, Clone, PartialEq, Eq, ToClvm, FromClvm)]
#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
#[clvm(list)]
pub struct Memos(pub Vec<Bytes>);

impl Payment {
pub fn new(puzzle_hash: Bytes32, amount: u64) -> Self {
Self::with_memos(puzzle_hash, amount, Vec::new())
Self {
puzzle_hash,
amount,
memos: None,
}
}

pub fn with_memos(puzzle_hash: Bytes32, amount: u64, memos: Vec<Bytes>) -> Self {
Self {
puzzle_hash,
amount,
memos,
memos: Some(Memos(memos)),
}
}
}
Expand Down Expand Up @@ -96,6 +107,9 @@ pub const SETTLEMENT_PAYMENTS_PUZZLE_HASH_V1: TreeHash = TreeHash::new(hex!(

#[cfg(test)]
mod tests {
use clvm_utils::tree_hash;
use clvmr::{serde::node_from_bytes, Allocator};

use super::*;

use crate::assert_puzzle_hash;
Expand All @@ -105,4 +119,95 @@ mod tests {
assert_puzzle_hash!(SETTLEMENT_PAYMENTS_PUZZLE => SETTLEMENT_PAYMENTS_PUZZLE_HASH);
assert_puzzle_hash!(SETTLEMENT_PAYMENTS_PUZZLE_V1 => SETTLEMENT_PAYMENTS_PUZZLE_HASH_V1);
}

#[test]
fn test_empty_memos() -> anyhow::Result<()> {
let mut allocator = Allocator::new();

/*
((0xd951714bbcd0d0af317b3ef432472b57e7c48d3036b4491539c186ce1377cad2
(0x2a5cbc6f5076e0517bdb1e4664b3c26e64d27178b65aaa1ae97267eee629113b 0x04a817c800 ())
))
*/
let expected_payment = node_from_bytes(
&mut allocator,
&hex!(
"
ffffa0d951714bbcd0d0af317b3ef432472b57e7c48d3036b4491539c186ce13
77cad2ffffa02a5cbc6f5076e0517bdb1e4664b3c26e64d27178b65aaa1ae972
67eee629113bff8504a817c800ff80808080
"
),
)?;

let nonce = Bytes32::from(hex!(
"d951714bbcd0d0af317b3ef432472b57e7c48d3036b4491539c186ce1377cad2"
));
let puzzle_hash = Bytes32::from(hex!(
"2a5cbc6f5076e0517bdb1e4664b3c26e64d27178b65aaa1ae97267eee629113b"
));
let amount = 20_000_000_000;
let memos = Vec::new();

let payment = Payment::with_memos(puzzle_hash, amount, memos);
let notarized_payment = SettlementPaymentsSolution {
notarized_payments: vec![NotarizedPayment {
nonce,
payments: vec![payment],
}],
}
.to_clvm(&mut allocator)?;

assert_eq!(
tree_hash(&allocator, notarized_payment),
tree_hash(&allocator, expected_payment)
);

Ok(())
}

#[test]
fn test_missing_memos() -> anyhow::Result<()> {
let mut allocator = Allocator::new();

/*
((0xd951714bbcd0d0af317b3ef432472b57e7c48d3036b4491539c186ce1377cad2
(0x2a5cbc6f5076e0517bdb1e4664b3c26e64d27178b65aaa1ae97267eee629113b 0x04a817c800)
))
*/
let expected_payment = node_from_bytes(
&mut allocator,
&hex!(
"
ffffa0d951714bbcd0d0af317b3ef432472b57e7c48d3036b4491539c186ce13
77cad2ffffa02a5cbc6f5076e0517bdb1e4664b3c26e64d27178b65aaa1ae972
67eee629113bff8504a817c800808080
"
),
)?;

let nonce = Bytes32::from(hex!(
"d951714bbcd0d0af317b3ef432472b57e7c48d3036b4491539c186ce1377cad2"
));
let puzzle_hash = Bytes32::from(hex!(
"2a5cbc6f5076e0517bdb1e4664b3c26e64d27178b65aaa1ae97267eee629113b"
));
let amount = 20_000_000_000;

let payment = Payment::new(puzzle_hash, amount);
let notarized_payment = SettlementPaymentsSolution {
notarized_payments: vec![NotarizedPayment {
nonce,
payments: vec![payment],
}],
}
.to_clvm(&mut allocator)?;

assert_eq!(
tree_hash(&allocator, notarized_payment),
tree_hash(&allocator, expected_payment)
);

Ok(())
}
}

0 comments on commit 96b0c0a

Please sign in to comment.