Skip to content

Commit

Permalink
Add timelock conditions
Browse files Browse the repository at this point in the history
  • Loading branch information
Rigidity committed Nov 26, 2024
1 parent d83c98a commit a94293c
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 1 deletion.
2 changes: 1 addition & 1 deletion crates/chia-sdk-driver/src/primitives/clawback.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ mod tests {
let bob = StandardLayer::new(bob_pk);

let clawback = Clawback {
timelock: NonZeroU64::MAX,
timelock: NonZeroU64::MIN,
sender_puzzle_hash: alice_puzzle_hash,
recipient_puzzle_hash: bob_puzzle_hash,
};
Expand Down
88 changes: 88 additions & 0 deletions crates/chia-sdk-test/src/simulator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,36 @@ impl Simulator {
let mut added_hints = IndexMap::new();
let mut puzzle_solutions = IndexMap::new();

if self.height < conds.height_absolute {
return Err(SimulatorError::Validation(
ErrorCode::AssertHeightAbsoluteFailed,
));
}

// TODO: Tick time differently?
if u64::from(self.height) < conds.seconds_absolute {
return Err(SimulatorError::Validation(
ErrorCode::AssertSecondsAbsoluteFailed,
));
}

if let Some(height) = conds.before_height_absolute {
if height < self.height {
return Err(SimulatorError::Validation(
ErrorCode::AssertBeforeHeightAbsoluteFailed,
));
}
}

// TODO: Tick time differently?
if let Some(seconds) = conds.before_seconds_absolute {
if seconds < self.height.into() {
return Err(SimulatorError::Validation(
ErrorCode::AssertBeforeSecondsAbsoluteFailed,
));
}
}

for coin_spend in spend_bundle.coin_spends {
puzzle_solutions.insert(
coin_spend.coin.coin_id(),
Expand Down Expand Up @@ -217,6 +247,64 @@ impl Simulator {
.copied()
.unwrap_or(CoinState::new(coin, None, Some(self.height)));

if let Some(relative_height) = spend.height_relative {
let Some(created_height) = coin_state.created_height else {
return Err(SimulatorError::Validation(
ErrorCode::EphemeralRelativeCondition,
));
};

if self.height < created_height + relative_height {
return Err(SimulatorError::Validation(
ErrorCode::AssertHeightRelativeFailed,
));
}
}

// TODO: Tick time differently?
if let Some(relative_seconds) = spend.seconds_relative {
let Some(created_height) = coin_state.created_height else {
return Err(SimulatorError::Validation(
ErrorCode::EphemeralRelativeCondition,
));
};

if u64::from(self.height) < u64::from(created_height) + relative_seconds {
return Err(SimulatorError::Validation(
ErrorCode::AssertSecondsRelativeFailed,
));
}
}

if let Some(relative_height) = spend.before_height_relative {
let Some(created_height) = coin_state.created_height else {
return Err(SimulatorError::Validation(
ErrorCode::EphemeralRelativeCondition,
));
};

if created_height + relative_height < self.height {
return Err(SimulatorError::Validation(
ErrorCode::AssertBeforeHeightRelativeFailed,
));
}
}

// TODO: Tick time differently?
if let Some(relative_seconds) = spend.before_seconds_relative {
let Some(created_height) = coin_state.created_height else {
return Err(SimulatorError::Validation(
ErrorCode::EphemeralRelativeCondition,
));
};

if u64::from(created_height) + relative_seconds < u64::from(self.height) {
return Err(SimulatorError::Validation(
ErrorCode::AssertBeforeSecondsRelativeFailed,
));
}
}

removed_coins.insert(spend.coin_id, coin_state);
}

Expand Down

0 comments on commit a94293c

Please sign in to comment.