1
1
extern crate bincode;
2
2
extern crate generic_array;
3
3
4
- use bank:: { Account , Result } ;
4
+ use bank:: Account ;
5
5
use bincode:: deserialize;
6
6
use libloading:: { Library , Symbol } ;
7
7
use signature:: Pubkey ;
8
8
use std:: path:: PathBuf ;
9
+ use std:: result;
9
10
use transaction:: Transaction ;
10
11
12
+ pub type Result < T > = result:: Result < T , T > ;
13
+
11
14
#[ cfg( debug_assertions) ]
12
15
const CARGO_PROFILE : & str = "debug" ;
13
16
@@ -43,8 +46,7 @@ fn create_library_path(name: &str) -> PathBuf {
43
46
44
47
// All contracts export a symbol named process()
45
48
const ENTRYPOINT : & str = "process" ;
46
-
47
- type Entrypoint = unsafe extern "C" fn ( tx : & Transaction , accounts : & mut [ Account ] ) -> Result < ( ) > ;
49
+ type Entrypoint = unsafe extern "C" fn ( userdata : & [ u8 ] , accounts : & mut [ Account ] ) ;
48
50
49
51
pub const DYNAMIC_CONTRACT_ID : [ u8 ; 32 ] = [
50
52
2 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 ,
@@ -90,9 +92,7 @@ impl DynamicContract {
90
92
Ok ( s) => s,
91
93
Err ( e) => panic ! ( "{:?} Unable to find {:?} in {:?}" , e, ENTRYPOINT , & path) ,
92
94
} ;
93
- if let Err ( e) = entrypoint ( tx, accounts) {
94
- println ! ( "Transaction failed: {:?}" , e) ;
95
- }
95
+ entrypoint ( & tx. userdata , accounts) ;
96
96
}
97
97
}
98
98
DynamicContract :: BPF { .. } => {
@@ -132,19 +132,9 @@ mod tests {
132
132
133
133
#[ test]
134
134
fn test_contract_balance ( ) {
135
- let accounts = [
136
- Account {
137
- tokens : 100 ,
138
- userdata : vec ! [ 0 , 8 ] ,
139
- contract_id : Pubkey :: new ( & DYNAMIC_CONTRACT_ID ) ,
140
- } ,
141
- Account {
142
- tokens : 1 ,
143
- userdata : vec ! [ 0 , 8 ] ,
144
- contract_id : Pubkey :: new ( & DYNAMIC_CONTRACT_ID ) ,
145
- } ,
146
- ] ;
147
-
135
+ let mut accounts = vec ! [ Account :: default ( ) , Account :: default ( ) ] ;
136
+ accounts[ 0 ] . tokens = 100 ;
137
+ accounts[ 1 ] . tokens = 1 ;
148
138
assert_eq ! ( 100 , DynamicContract :: get_balance( & accounts[ 0 ] ) ) ;
149
139
assert_eq ! ( 1 , DynamicContract :: get_balance( & accounts[ 1 ] ) ) ;
150
140
}
@@ -163,18 +153,9 @@ mod tests {
163
153
fee : 42 ,
164
154
userdata : serialize ( & userdata) . unwrap ( ) ,
165
155
} ;
166
- let mut accounts = [
167
- Account {
168
- tokens : 100 ,
169
- userdata : vec ! [ 0 , 8 ] ,
170
- contract_id : Pubkey :: new ( & DYNAMIC_CONTRACT_ID ) ,
171
- } ,
172
- Account {
173
- tokens : 1 ,
174
- userdata : vec ! [ 0 , 8 ] ,
175
- contract_id : Pubkey :: new ( & DYNAMIC_CONTRACT_ID ) ,
176
- } ,
177
- ] ;
156
+ let mut accounts = vec ! [ Account :: default ( ) , Account :: default ( ) ] ;
157
+ accounts[ 0 ] . tokens = 100 ;
158
+ accounts[ 1 ] . tokens = 1 ;
178
159
179
160
DynamicContract :: process_transaction ( & tx, & mut accounts) ;
180
161
}
@@ -194,18 +175,9 @@ mod tests {
194
175
fee : 42 ,
195
176
userdata : serialize ( & userdata) . unwrap ( ) ,
196
177
} ;
197
- let mut accounts = [
198
- Account {
199
- tokens : 100 ,
200
- userdata : vec ! [ 0 , 8 ] ,
201
- contract_id : Pubkey :: new ( & DYNAMIC_CONTRACT_ID ) ,
202
- } ,
203
- Account {
204
- tokens : 1 ,
205
- userdata : vec ! [ 0 , 8 ] ,
206
- contract_id : Pubkey :: new ( & DYNAMIC_CONTRACT_ID ) ,
207
- } ,
208
- ] ;
178
+ let mut accounts = vec ! [ Account :: default ( ) , Account :: default ( ) ] ;
179
+ accounts[ 0 ] . tokens = 100 ;
180
+ accounts[ 1 ] . tokens = 1 ;
209
181
210
182
DynamicContract :: process_transaction ( & tx, & mut accounts) ;
211
183
}
@@ -224,18 +196,9 @@ mod tests {
224
196
fee : 42 ,
225
197
userdata : serialize ( & userdata) . unwrap ( ) ,
226
198
} ;
227
- let mut accounts = [
228
- Account {
229
- tokens : 100 ,
230
- userdata : vec ! [ 0 , 8 ] ,
231
- contract_id : Pubkey :: new ( & DYNAMIC_CONTRACT_ID ) ,
232
- } ,
233
- Account {
234
- tokens : 1 ,
235
- userdata : vec ! [ 0 , 8 ] ,
236
- contract_id : Pubkey :: new ( & DYNAMIC_CONTRACT_ID ) ,
237
- } ,
238
- ] ;
199
+ let mut accounts = vec ! [ Account :: default ( ) , Account :: default ( ) ] ;
200
+ accounts[ 0 ] . tokens = 100 ;
201
+ accounts[ 1 ] . tokens = 1 ;
239
202
240
203
DynamicContract :: process_transaction ( & tx, & mut accounts) ;
241
204
assert_eq ! ( 0 , accounts[ 0 ] . tokens) ;
@@ -256,25 +219,16 @@ mod tests {
256
219
fee : 42 ,
257
220
userdata : serialize ( & userdata) . unwrap ( ) ,
258
221
} ;
259
- let mut accounts = [
260
- Account {
261
- tokens : 10 ,
262
- userdata : vec ! [ 0 , 8 ] ,
263
- contract_id : Pubkey :: new ( & DYNAMIC_CONTRACT_ID ) ,
264
- } ,
265
- Account {
266
- tokens : 1 ,
267
- userdata : vec ! [ 0 , 8 ] ,
268
- contract_id : Pubkey :: new ( & DYNAMIC_CONTRACT_ID ) ,
269
- } ,
270
- ] ;
222
+ let mut accounts = vec ! [ Account :: default ( ) , Account :: default ( ) ] ;
223
+ accounts[ 0 ] . tokens = 10 ;
224
+ accounts[ 1 ] . tokens = 1 ;
271
225
272
226
DynamicContract :: process_transaction ( & tx, & mut accounts) ;
273
227
assert_eq ! ( 10 , accounts[ 0 ] . tokens) ;
274
228
assert_eq ! ( 1 , accounts[ 1 ] . tokens) ;
275
229
}
276
230
277
- // TODO add more tests to validate the Transaction and Account data is
231
+ // TODO add more tests to validate the Userdata and Account data is
278
232
// moving across the boundary correctly
279
233
280
234
}
0 commit comments