1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
mod fragment_log;

use crate::jormungandr::JormungandrRest;
pub use fragment_log::{assert_accepted_rejected, assert_bad_request, FragmentLogVerifier};
use jormungandr_lib::interfaces::{AccountState, Address, Value};

pub struct JormungandrStateVerifier {
    rest: JormungandrRest,
    snapshot_before: Option<StateSnapshot>,
}

impl JormungandrStateVerifier {
    pub fn new(rest: JormungandrRest) -> Self {
        Self {
            rest,
            snapshot_before: None,
        }
    }

    pub fn fragment_logs(&self) -> FragmentLogVerifier {
        FragmentLogVerifier::new(self.rest.clone())
    }

    pub fn record_address_state(mut self, addresses: Vec<&Address>) -> Self {
        self.snapshot_before = Some(StateSnapshot::new(
            addresses
                .iter()
                .map(|w| {
                    (
                        w.to_string(),
                        self.rest
                            .account_state(&as_identifier(w))
                            .expect("cannot rerieve account state"),
                    )
                })
                .collect(),
        ));
        self
    }

    pub fn value_moved_between_addresses(
        &self,
        from: &Address,
        to: &Address,
        value: Value,
    ) -> Result<(), StateVerifierError> {
        self.address_lost_value(from, value)?;
        self.address_gained_value(to, value)?;
        Ok(())
    }

    pub fn address_lost_value(
        &self,
        address: &Address,
        value: Value,
    ) -> Result<(), StateVerifierError> {
        let snapshot = self
            .snapshot_before
            .as_ref()
            .ok_or(StateVerifierError::NoSnapshot)?;
        let expected = snapshot.value_for(address)?;
        let actual = self
            .rest
            .account_state(&as_identifier(address))?
            .value()
            .checked_add(value)?;
        assert_eq!(
            expected, actual,
            "No value was deducted friom account: {} vs {}",
            expected, actual
        );
        Ok(())
    }

    pub fn no_changes(&self, addresses: Vec<&Address>) -> Result<(), StateVerifierError> {
        for address in addresses {
            self.address_has_the_same_value(address)?;
        }
        Ok(())
    }

    pub fn address_has_the_same_value(&self, address: &Address) -> Result<(), StateVerifierError> {
        let snapshot = self
            .snapshot_before
            .as_ref()
            .ok_or(StateVerifierError::NoSnapshot)?;
        let expected = snapshot.value_for(address)?;
        let actual = *self.rest.account_state(&as_identifier(address))?.value();
        assert_eq!(
            expected, actual,
            "value changed for account {:?}: {} vs {}",
            address, expected, actual
        );
        Ok(())
    }

    pub fn address_gained_value(
        &self,
        address: &Address,
        value: Value,
    ) -> Result<(), StateVerifierError> {
        let snapshot = self
            .snapshot_before
            .as_ref()
            .ok_or(StateVerifierError::NoSnapshot)?;
        let expected = snapshot.value_for(address)?.checked_add(value)?;
        let actual = *self.rest.account_state(&as_identifier(address))?.value();
        assert_eq!(
            expected, actual,
            "No value was added to account: {} vs {}",
            expected, actual
        );
        Ok(())
    }
}

fn as_identifier(address: &Address) -> jormungandr_lib::crypto::account::Identifier {
    address.1.public_key().unwrap().clone().into()
}

use std::collections::HashMap;
use thiserror::Error;
#[derive(Debug, Error)]
pub enum StateVerifierError {
    #[error("cannot find wallet in snapshot {0}")]
    NoWalletInSnapshot(String),
    #[error("no snapshot was made prior assert execution")]
    NoSnapshot,
    #[error("rest error")]
    RestError(#[from] super::RestError),
    #[error("wrong value calculation")]
    ValueError(#[from] chain_impl_mockchain::value::ValueError),
}

pub struct StateSnapshot {
    wallets: HashMap<String, AccountState>,
}

impl StateSnapshot {
    pub fn new(wallets: HashMap<String, AccountState>) -> Self {
        Self { wallets }
    }

    pub fn value_for(&self, address: &Address) -> Result<Value, StateVerifierError> {
        let address = address.to_string();
        let state = self
            .wallets
            .get(&address)
            .ok_or_else(|| StateVerifierError::NoWalletInSnapshot(address.clone()))?;
        Ok(*state.value())
    }
}