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
use std::collections::BTreeMap;

use crate::scenario::model::{
    Account, AddressKey, AddressValue, BlockInfo, BytesValue, NewAddress, U64Value,
};

#[derive(Debug, Default)]
pub struct SetStateStep {
    pub comment: Option<String>,
    pub accounts: BTreeMap<AddressKey, Account>,
    pub new_addresses: Vec<NewAddress>,
    pub block_hashes: Vec<BytesValue>,
    pub previous_block_info: Box<Option<BlockInfo>>,
    pub current_block_info: Box<Option<BlockInfo>>,
}

impl SetStateStep {
    pub fn new() -> Self {
        Self::default()
    }

    pub fn put_account<A>(mut self, address_expr: A, account: Account) -> Self
    where
        AddressKey: From<A>,
    {
        let address_key = AddressKey::from(address_expr);
        self.accounts.insert(address_key, account);
        self
    }

    pub fn new_address<CA, NA>(
        mut self,
        creator_address_expr: CA,
        creator_nonce_expr: u64,
        new_address_expr: NA,
    ) -> Self
    where
        AddressValue: From<CA>,
        AddressValue: From<NA>,
    {
        self.new_addresses.push(NewAddress {
            creator_address: AddressValue::from(creator_address_expr),
            creator_nonce: U64Value::from(creator_nonce_expr),
            new_address: AddressValue::from(new_address_expr),
        });
        self
    }

    pub fn block_epoch<N>(mut self, block_epoch_expr: N) -> Self
    where
        U64Value: From<N>,
    {
        let block_epoch = U64Value::from(block_epoch_expr);

        if let Some(block_info) = &mut *self.current_block_info {
            block_info.block_epoch = Some(block_epoch);
        } else {
            *self.current_block_info = Some(BlockInfo {
                block_epoch: Some(block_epoch),
                ..Default::default()
            });
        }

        self
    }

    pub fn block_nonce<N>(mut self, block_nonce_expr: N) -> Self
    where
        U64Value: From<N>,
    {
        let block_nonce = U64Value::from(block_nonce_expr);

        if let Some(block_info) = &mut *self.current_block_info {
            block_info.block_nonce = Some(block_nonce);
        } else {
            *self.current_block_info = Some(BlockInfo {
                block_nonce: Some(block_nonce),
                ..Default::default()
            });
        }

        self
    }

    pub fn block_round<N>(mut self, block_round_expr: N) -> Self
    where
        U64Value: From<N>,
    {
        let block_round = U64Value::from(block_round_expr);

        if let Some(block_info) = &mut *self.current_block_info {
            block_info.block_round = Some(block_round);
        } else {
            *self.current_block_info = Some(BlockInfo {
                block_round: Some(block_round),
                ..Default::default()
            });
        }

        self
    }

    pub fn block_timestamp<N>(mut self, block_timestamp_expr: N) -> Self
    where
        U64Value: From<N>,
    {
        let block_timestamp = U64Value::from(block_timestamp_expr);

        if let Some(block_info) = &mut *self.current_block_info {
            block_info.block_timestamp = Some(block_timestamp);
        } else {
            *self.current_block_info = Some(BlockInfo {
                block_timestamp: Some(block_timestamp),
                ..Default::default()
            });
        }

        self
    }

    pub fn block_random_seed<B>(mut self, block_random_seed_expr: B) -> Self
    where
        BytesValue: From<B>,
    {
        let block_random_seed = BytesValue::from(block_random_seed_expr);

        if let Some(block_info) = &mut *self.current_block_info {
            block_info.block_random_seed = Some(block_random_seed);
        } else {
            *self.current_block_info = Some(BlockInfo {
                block_random_seed: Some(block_random_seed),
                ..Default::default()
            });
        }

        self
    }
}