validate contract states

This commit is contained in:
wiesche
2026-09-02 18:33:33 +02:00
parent 210a5a6ded
commit daa40683e9
5 changed files with 82 additions and 17 deletions
-2
View File
@@ -73,8 +73,6 @@ Separating side effects until the 'save_step' part would make these functions mu
#### Tests
- signing the same slate twice, which `verify_not_signed` guards
- invalid or inconsistent slates and unexpected states
- slate contents at each step, not only the end state
- the foreign API entry points for `new` and `sign`
- Slatepack version negotiation with the last supported wallet release
+2
View File
@@ -98,6 +98,8 @@ where
{
let mut sl = slate.clone();
contract::utils::verify_not_signed(w, sl.id)?;
// Reject terminal and unknown states before setup reserves keys.
contract::slate::next_state(&sl.state)?;
// Ensure net_change has been provided
let expected_net_change =
+11 -10
View File
@@ -438,26 +438,27 @@ where
Ok(())
}
/// Transition the slate state to the next one
pub fn transition_state(slate: &mut Slate) -> Result<(), Error> {
// We don't really use these states right now apart from leaving it to derive expected net_change.
// This suggests these can't be used for manipulation. It doesn't hurt to think a bit more if that's the case.
let new_state = match slate.state {
/// Return the next contract slate state
pub(super) fn next_state(state: &SlateState) -> Result<SlateState, Error> {
let next = match state {
SlateState::Invoice1 => SlateState::Invoice2,
SlateState::Invoice2 => SlateState::Invoice3,
SlateState::Standard1 => SlateState::Standard2,
SlateState::Standard2 => SlateState::Standard3,
// Unknown, or a slate that has already reached its final state, is not something
// we can advance. We have signed by this point and the wallet state is persisted
// next, so report it rather than reporting a Standard3 that never happened.
ref s => {
// Unknown and final states cannot be advanced.
s => {
return Err(Error::GenericError(format!(
"Cannot advance a contract slate in state {}",
s
)))
}
};
slate.state = new_state;
Ok(next)
}
/// Transition the slate state to the next one
pub(super) fn transition_state(slate: &mut Slate) -> Result<(), Error> {
slate.state = next_state(&slate.state)?;
// NOTE: It's possible to never reach the step3. A self-spend has only 2 steps: new -> sign.
Ok(())
}