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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
//! Traits designating required simulation agent functionality
//!
//! The traits are intended to be used in a hierarchical manner:
//!
//! * [SimState] collect all simulation agents, where fields
//! may be different agent types. This trait then describes
//! functions called during simulation execution
//! * [AgentSet] is intended as a homogeneous collection of
//! an agent type
//! * [Agent] is an individual agent that may be member of an
//! [AgentSet]
//!
//! Implementers have the flexibility to only use part of this
//! structure though, for instance an implementation of
//! [SimState] could implement an individual agent.
//!
//! Since it is a common use case to want to iterate over a
//! agents of different types, the macro `#[derive(SimState)]`
//! will automatically implement functions that iterate
//! over field containing agents.
//!
use crateTransaction;
use crate;
use crateDB;
use Address;
use RngCore;
pub use SimState;
/// Simulation agent state trait
///
/// Trait providing an interface to update the
/// state of all agents over the course of a
/// simulation. This trait can be automatically
/// derived for a struct where the fields
/// are sets of agents of a single type using the
/// [SimState] macro. This will generate the
/// code to automatically iterate
/// over each set of agents in turn.
///
/// # Examples
///
/// ```
/// use rand::RngCore;
/// use alloy_primitives::Address;
/// use verbs_rs::{DB, env::{Env, Validator}};
/// use verbs_rs::agent::{Agent, RecordedAgent, AgentVec, AgentSet, SimState};
/// use verbs_rs::contract::Transaction;
///
/// struct DummyAgent{}
///
/// impl Agent for DummyAgent {
/// fn update<D: DB, V: Validator, R: RngCore>(
/// &mut self, rng: &mut R, network: &mut Env<D, V>
/// ) -> Vec<Transaction> {
/// Vec::default()
/// }
///
/// fn get_address(&self) -> Address {
/// Address::ZERO
/// }
/// }
///
/// impl RecordedAgent<bool> for DummyAgent {
/// fn record<D: DB, V: Validator>(&mut self, _env: &mut Env<D, V>) -> bool {
/// true
/// }
/// }
///
/// #[derive(SimState)]
/// struct TestState {
/// a: AgentVec::<bool, DummyAgent>,
/// b: AgentVec::<bool, DummyAgent>,
/// }
/// ```
/// Trait defining behaviour for a single agent
///
/// This is intended to be called for each individual
/// agent at each step of the simulation, updating the
/// state of the agent and recording state data.
/// # Examples
///
/// ```
/// use rand::RngCore;
/// use alloy_primitives::Address;
/// use verbs_rs::{DB, env::{Env, Validator}};
/// use verbs_rs::agent::{Agent, RecordedAgent, AgentVec, AgentSet};
/// use verbs_rs::contract::Transaction;
///
/// struct DummyAgent{
/// state: i32,
/// }
///
/// impl Agent for DummyAgent {
/// fn update<D: DB, V: Validator, R: RngCore>(
/// &mut self, rng: &mut R, network: &mut Env<D, V>
/// ) -> Vec<Transaction> {
/// self.state += 1;
/// Vec::default()
/// }
///
/// fn get_address(&self) -> Address {
/// Address::ZERO
/// }
/// }
/// ```
/// Trait used to record the state of the agent over the course of the simulation
///
/// Each step this is called after the state of the simulation
/// is updated, and is intended to record the state of an agent
/// or some part of the state of the EVM. The actual type of data
/// returned is left to the implementer.
///
/// # Examples
///
/// ```
/// use verbs_rs::{DB, env::{Env, Validator}};
/// use verbs_rs::agent::RecordedAgent;
///
/// struct DummyAgent{
/// current_state: i32
/// }
///
/// impl RecordedAgent<i32> for DummyAgent {
/// fn record<D: DB, V: Validator>(&mut self, _env: &mut Env<D, V>) -> i32 {
/// self.current_state
/// }
/// }
/// ```
/// A homogenous collection of agents
///
/// Designed to represent a group of agents of a uniform type
/// and update and record the group state at each step of the
/// simulation.
/// Take ownership of time-series data from a set of agents
///
/// Returns a time series of vectors of records across
/// all the agents in the set.