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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
use super::types::*;
use crate::{
constants,
impls::{
contract_events_from_logs, from_interpreter_energy, lookup_module_cost,
to_interpreter_energy,
},
types::{Account, BalanceError, Contract, ContractModule, TransferError},
DebugTraceElement, ExecutionError, InvokeExecutionError,
};
use concordium_base::{
base::{AccountAddressEq, Energy, InsufficientEnergy},
constants::MAX_PARAMETER_LEN,
contracts_common::{
to_bytes, AccountAddress, AccountBalance, Address, Amount, ChainMetadata, ContractAddress,
ExchangeRates, ModuleReference, OwnedEntrypointName, OwnedReceiveName,
},
smart_contracts::{
ContractTraceElement, InstanceUpdatedEvent, OwnedContractName, OwnedParameter, WasmVersion,
},
transactions::UpdateContractPayload,
};
use concordium_smart_contract_engine::{
v0,
v1::{self, trie, InvokeResponse},
ExecResult, InterpreterEnergy,
};
use concordium_wasm::artifact::{self, CompiledFunction};
use std::collections::{btree_map, BTreeMap};
impl<'a, 'b> EntrypointInvocationHandler<'a, 'b> {
/// Used for handling the *initial* part of invoking an entrypoint.
///
/// **Preconditions:**
/// - `invoker` exists
/// - `invoker` has sufficient balance to pay for `remaining_energy`
/// - `sender` exists
/// - if the contract (`contract_address`) exists, then its `module` must
/// also exist.
fn invoke_entrypoint_initial(
&mut self,
invoker: AccountAddress,
sender: Address,
payload: UpdateContractPayload,
trace_elements_checkpoint: usize,
) -> Result<
Result<(v1::ReceiveResult<CompiledFunction>, InvocationData), InvokeResponse>,
TestConfigurationError,
> {
// Charge the base cost for updating a contract.
self.remaining_energy
.tick_energy(constants::UPDATE_CONTRACT_INSTANCE_BASE_COST)?;
// Move the amount from the sender to the contract, if any.
// And get the new self_balance.
let instance_self_balance = if payload.amount.micro_ccd() > 0 {
let transfer_result = match sender {
Address::Account(sender_account) => self.transfer_from_account_to_contract(
payload.amount,
sender_account,
payload.address,
),
Address::Contract(sender_contract) => self.transfer_from_contract_to_contract(
payload.amount,
sender_contract,
payload.address,
),
};
match transfer_result {
Ok(new_balance_from) => new_balance_from,
Err(transfer_error) => {
let kind = match transfer_error {
TransferError::BalanceError {
error: BalanceError::Overflow,
} => {
// Balance overflows are unrecoverable and short circuit.
return Err(TestConfigurationError::BalanceOverflow);
}
TransferError::BalanceError {
error: BalanceError::Insufficient,
} => v1::InvokeFailure::InsufficientAmount,
TransferError::ToMissing => v1::InvokeFailure::NonExistentContract,
};
// Return early.
return Ok(Err(v1::InvokeResponse::Failure { kind }));
}
}
} else {
match self.contract_balance(payload.address) {
Some(self_balance) => self_balance,
None => {
// Return early.
return Ok(Err(v1::InvokeResponse::Failure {
kind: v1::InvokeFailure::NonExistentContract,
}));
}
}
};
// Get the instance and artifact. To be used in several places.
let instance = self
.chain
.contracts
.get(&payload.address)
.expect("Contract known to exist at this point");
let module = self.contract_module(instance);
// Construct the receive name (or fallback receive name) and ensure its presence
// in the contract. Also returns the contract name and entrypoint name as they
// are needed further down.
let (contract_name, receive_name, entrypoint_name) = {
let borrowed_receive_name = payload.receive_name.as_receive_name();
let contract_name = borrowed_receive_name.contract_name();
let owned_contract_name =
OwnedContractName::new_unchecked(format!("init_{}", contract_name));
let owned_entrypoint_name = borrowed_receive_name.entrypoint_name().to_owned();
let receive_name = borrowed_receive_name.get_chain_name();
let fallback_receive_name = format!("{}.", contract_name);
if module.artifact.has_entrypoint(receive_name) {
(
owned_contract_name,
payload.receive_name,
owned_entrypoint_name,
)
} else if module
.artifact
.has_entrypoint(fallback_receive_name.as_str())
{
(
owned_contract_name,
OwnedReceiveName::new_unchecked(fallback_receive_name),
owned_entrypoint_name,
)
} else {
// Return early.
return Ok(Err(v1::InvokeResponse::Failure {
kind: v1::InvokeFailure::NonExistentEntrypoint,
}));
}
};
// Subtract the cost of looking up the module
self.remaining_energy
.tick_energy(lookup_module_cost(&module))?;
// Sender policies have a very bespoke serialization in
// order to allow skipping portions of them in smart contracts.
let sender_policies = {
let mut out = Vec::new();
let policy = &self
.chain
.account(invoker)
.expect("Precondition violation: invoker must exist.")
.policy;
policy
.serial_for_smart_contract(&mut out)
.expect("Writing to a vector should succeed.");
out
};
// Construct the receive context
let receive_ctx = v1::ReceiveContext {
// This should be the entrypoint specified, even if we end up
// calling the fallback entrypoint, as this will be visible to the
// contract via a host function.
entrypoint: entrypoint_name.clone(),
common: v0::ReceiveContext {
metadata: ChainMetadata {
slot_time: self.chain.parameters.block_time,
},
invoker,
self_address: payload.address,
self_balance: instance_self_balance,
sender,
owner: instance.owner,
sender_policies,
},
};
let mod_idx_before_invoke = self.next_contract_modification_index;
// Construct the instance state
let mut loader = v1::trie::Loader::new(&[][..]); // An empty loader is fine currently, as we do not use caching in this lib.
let mut mutable_state = self.contract_state(payload.address);
let mut mutable_state = mutable_state.make_fresh_generation(&mut loader);
let inner = mutable_state.get_inner(&mut loader);
let instance_state = v1::InstanceState::new(loader, inner);
// Get the initial result from invoking receive
let initial_result = self.run_interpreter(|energy| {
v1::invoke_receive(
module.artifact,
receive_ctx,
v1::ReceiveInvocation {
amount: payload.amount,
// This will either be the one provided on the fallback receive name.
receive_name: receive_name.as_receive_name(),
parameter: payload.message.as_ref(),
energy,
},
instance_state,
v1::ReceiveParams {
max_parameter_size: MAX_PARAMETER_LEN,
limit_logs_and_return_values: false,
support_queries: true,
},
)
})?;
// Set up some data needed for recursively processing the receive until the end,
// i.e. beyond interrupts.
Ok(Ok((initial_result, InvocationData {
sender,
address: payload.address,
contract_name,
entrypoint: entrypoint_name,
parameter: payload.message,
amount: payload.amount,
state: mutable_state,
trace_elements_checkpoint,
next_mod_idx_checkpoint: mod_idx_before_invoke,
mod_idx_before_invoke,
})))
}
/// Used for handling contract entrypoint invocations internally.
///
/// **Preconditions:**
/// - `invoker` exists
/// - `invoker` has sufficient balance to pay for `remaining_energy`
/// - `sender` exists
/// - if the contract (`contract_address`) exists, then its `module` must
/// also exist.
pub(crate) fn invoke_entrypoint(
&mut self,
invoker: AccountAddress,
sender: Address,
payload: UpdateContractPayload,
) -> Result<(InvokeResponse, Vec<DebugTraceElement>), TestConfigurationError> {
let mut stack = Vec::new();
let mut trace_elements = Vec::new();
stack.push(Next::Initial {
sender,
payload,
trace_elements_checkpoint: 0,
});
// Initialized to a dummy value. This will always be set or the function will
// terminate with an Err.
let mut invoke_response: Option<InvokeResponse> = None;
while let Some(invocation_data) = stack.pop() {
let (receive_result, mut invocation_data) = match invocation_data {
Next::Resume {
mut data,
config,
response,
} => {
match response {
Some(response) => {
let receive_result = self.run_interpreter(|energy| {
v1::resume_receive(
config,
response,
energy,
&mut data.state,
false, /* the state never changes on interrupts that have
* immediate handlers */
// An empty loader is fine currently, as we do not use
// caching in this lib.
v1::trie::Loader::new(&[][..]),
)
})?;
(receive_result, data)
}
None => {
// we are resuming from a contract call
let (success, call_response) = match invoke_response
.take()
.expect("Response should be available")
{
v1::InvokeResponse::Success {
data: return_value, ..
} => {
let invoke_response = v1::InvokeResponse::Success {
// The balance returned by `invoke_entrypoint`
// is the balance of the contract called. But we
// are interested in the new balance of the caller.
new_balance: self.contract_balance_unchecked(data.address),
data: return_value,
};
(true, invoke_response)
}
failure => (false, failure),
};
// Remove the last state changes if the invocation failed.
let state_changed = if !success {
self.rollback();
false // We rolled back, so no changes were
// made to this contract.
} else {
let mod_idx_after_invoke = self.modification_index(data.address);
let state_changed =
mod_idx_after_invoke != data.mod_idx_before_invoke;
if state_changed {
// Update the state field with the newest value from the
// changeset.
data.state = self.contract_state(data.address);
}
state_changed
};
// Add resume event
let resume_event = ContractTraceElement::Resumed {
address: data.address,
success,
};
self.push_regular_trace_element(
&mut trace_elements,
resume_event,
data.entrypoint.clone(),
);
let receive_result = self.run_interpreter(|energy| {
v1::resume_receive(
config,
call_response,
energy,
&mut data.state,
state_changed,
// An empty loader is fine currently, as we do not use
// caching in this lib.
v1::trie::Loader::new(&[][..]),
)
})?;
(receive_result, data)
}
}
}
Next::Initial {
sender,
payload,
trace_elements_checkpoint,
} => {
match self.invoke_entrypoint_initial(
invoker,
sender,
payload,
trace_elements_checkpoint,
)? {
Ok(x) => x,
Err(ier) => {
// invocation has failed. No more to do for this call.
// Since no traces were produced we don't have to roll them back.
invoke_response = Some(ier);
continue;
}
}
}
};
match receive_result {
v1::ReceiveResult::Success {
logs,
state_changed,
return_value,
remaining_energy,
} => {
// Update the remaining_energy field.
self.update_energy(remaining_energy);
let update_event = ContractTraceElement::Updated {
data: InstanceUpdatedEvent {
contract_version: WasmVersion::V1,
address: invocation_data.address,
instigator: invocation_data.sender,
amount: invocation_data.amount,
message: invocation_data.parameter.clone(),
receive_name: OwnedReceiveName::construct_unchecked(
invocation_data.contract_name.as_contract_name(),
invocation_data.entrypoint.as_entrypoint_name(),
),
events: contract_events_from_logs(logs),
},
};
// Add update event
self.push_regular_trace_element(
&mut trace_elements,
update_event,
invocation_data.entrypoint.clone(),
);
// Save changes to changeset.
if state_changed {
self.save_state_changes(
invocation_data.address,
&mut invocation_data.state,
);
}
invoke_response = Some(v1::InvokeResponse::Success {
new_balance: self.contract_balance_unchecked(invocation_data.address),
data: Some(return_value),
});
}
v1::ReceiveResult::Interrupt {
remaining_energy,
state_changed,
logs,
config,
interrupt,
} => {
// Update the remaining_energy field.
self.update_energy(remaining_energy);
// Create the interrupt event, which will be included for transfers, calls,
// and upgrades, but not for the remaining
// interrupts.
let interrupt_event = ContractTraceElement::Interrupted {
address: invocation_data.address,
events: contract_events_from_logs(logs),
};
// Remember what state we are in before invoking.
// This is used to report, upon resume, whether the contracts's
// state has changed.
invocation_data.mod_idx_before_invoke = if state_changed {
self.save_state_changes(invocation_data.address, &mut invocation_data.state)
} else {
self.modification_index(invocation_data.address)
};
match interrupt {
v1::Interrupt::Transfer { to, amount } => {
// Add the interrupt event
self.push_regular_trace_element(
&mut trace_elements,
interrupt_event,
invocation_data.entrypoint.clone(),
);
let response = match self.transfer_from_contract_to_account(
amount,
invocation_data.address,
to,
) {
Ok(new_balance) => v1::InvokeResponse::Success {
new_balance,
data: None,
},
Err(err) => {
let kind = match err {
TransferError::ToMissing => {
v1::InvokeFailure::NonExistentAccount
}
TransferError::BalanceError {
error: BalanceError::Insufficient,
} => v1::InvokeFailure::InsufficientAmount,
TransferError::BalanceError {
error: BalanceError::Overflow,
} => {
// Balance overflows are unrecoverable and short
// circuit.
return Err(TestConfigurationError::BalanceOverflow);
}
};
v1::InvokeResponse::Failure { kind }
}
};
let success = matches!(response, v1::InvokeResponse::Success { .. });
if success {
// Add transfer event
let transfer_event = ContractTraceElement::Transferred {
from: invocation_data.address,
amount,
to,
};
self.push_regular_trace_element(
&mut trace_elements,
transfer_event,
invocation_data.entrypoint.clone(),
);
}
// Add resume event
let resume_event = ContractTraceElement::Resumed {
address: invocation_data.address,
success,
};
self.push_regular_trace_element(
&mut trace_elements,
resume_event,
invocation_data.entrypoint.clone(),
);
self.remaining_energy.tick_energy(
concordium_base::transactions::cost::SIMPLE_TRANSFER,
)?;
stack.push(Next::Resume {
data: invocation_data,
// the state never changes on transfers
config,
response: Some(response),
});
}
v1::Interrupt::Call {
address,
parameter,
name,
amount,
} => {
// Add the interrupt event
self.push_regular_trace_element(
&mut trace_elements,
interrupt_event,
invocation_data.entrypoint.clone(),
);
match self
.chain
.contracts
.get(&address)
.map(|c| c.contract_name.as_contract_name())
{
// The contract to call does not exist.
None => {
let response = v1::InvokeResponse::Failure {
kind: v1::InvokeFailure::NonExistentContract,
};
// Add resume event
let resume_event = ContractTraceElement::Resumed {
address: invocation_data.address,
success: false,
};
self.push_regular_trace_element(
&mut trace_elements,
resume_event,
invocation_data.entrypoint.clone(),
);
stack.push(Next::Resume {
data: invocation_data,
config,
response: Some(response),
});
}
Some(contract_name) => {
// Make a checkpoint before calling another contract so that we
// may roll back.
self.checkpoint();
let receive_name = OwnedReceiveName::construct_unchecked(
contract_name,
name.as_entrypoint_name(),
);
let message = OwnedParameter::new_unchecked(parameter);
let sender = Address::Contract(invocation_data.address);
// Remember to continue the current execution after handling the
// call.
stack.push(Next::Resume {
data: invocation_data,
config,
response: None,
});
// Add the call to the stack to execute.
stack.push(Next::Initial {
sender,
payload: UpdateContractPayload {
amount,
address,
receive_name,
message,
},
trace_elements_checkpoint: trace_elements.len(),
});
}
};
}
v1::Interrupt::Upgrade { module_ref } => {
// Add the interrupt event.
self.push_regular_trace_element(
&mut trace_elements,
interrupt_event,
invocation_data.entrypoint.clone(),
);
// Charge a base cost.
self.remaining_energy
.tick_energy(constants::INITIALIZE_CONTRACT_INSTANCE_BASE_COST)?;
let response = match self.chain.modules.get(&module_ref) {
None => v1::InvokeResponse::Failure {
kind: v1::InvokeFailure::UpgradeInvalidModuleRef,
},
Some(module) => {
// Charge for the module lookup.
self.remaining_energy
.tick_energy(lookup_module_cost(module))?;
if module.artifact.export.contains_key(
invocation_data
.contract_name
.as_contract_name()
.get_chain_name(),
) {
// Update module reference in the changeset.
let old_module_ref = self.save_module_upgrade(
invocation_data.address,
module_ref,
);
// Charge for the initialization cost.
self.remaining_energy.tick_energy(
constants::INITIALIZE_CONTRACT_INSTANCE_CREATE_COST,
)?;
let upgrade_event = ContractTraceElement::Upgraded {
address: invocation_data.address,
from: old_module_ref,
to: module_ref,
};
self.push_regular_trace_element(
&mut trace_elements,
upgrade_event,
invocation_data.entrypoint.clone(),
);
v1::InvokeResponse::Success {
new_balance: self.contract_balance_unchecked(
invocation_data.address,
),
data: None,
}
} else {
v1::InvokeResponse::Failure {
kind: v1::InvokeFailure::UpgradeInvalidContractName,
}
}
}
};
let success = matches!(response, v1::InvokeResponse::Success { .. });
let resumed_event = ContractTraceElement::Resumed {
address: invocation_data.address,
success,
};
self.push_regular_trace_element(
&mut trace_elements,
resumed_event,
invocation_data.entrypoint.clone(),
);
stack.push(Next::Resume {
data: invocation_data,
config,
response: Some(response),
});
}
v1::Interrupt::QueryAccountBalance { address } => {
let response = match self.account_balance(address) {
Some(balance) => v1::InvokeResponse::Success {
new_balance: self
.contract_balance_unchecked(invocation_data.address),
data: Some(to_bytes(&balance)),
},
None => v1::InvokeResponse::Failure {
kind: v1::InvokeFailure::NonExistentAccount,
},
};
self.remaining_energy.tick_energy(
constants::CONTRACT_INSTANCE_QUERY_ACCOUNT_BALANCE_COST,
)?;
stack.push(Next::Resume {
data: invocation_data,
config,
response: Some(response),
});
}
v1::Interrupt::QueryContractBalance { address } => {
let response = match self.contract_balance(address) {
None => v1::InvokeResponse::Failure {
kind: v1::InvokeFailure::NonExistentContract,
},
Some(bal) => v1::InvokeResponse::Success {
// Balance of contract querying. Won't change. Notice the
// `data.address`.
new_balance: self
.contract_balance_unchecked(invocation_data.address),
data: Some(to_bytes(&bal)),
},
};
self.remaining_energy.tick_energy(
constants::CONTRACT_INSTANCE_QUERY_CONTRACT_BALANCE_COST,
)?;
stack.push(Next::Resume {
data: invocation_data,
config,
response: Some(response),
});
}
v1::Interrupt::QueryExchangeRates => {
let exchange_rates = ExchangeRates {
euro_per_energy: self.chain.parameters.euro_per_energy,
micro_ccd_per_euro: self.chain.parameters.micro_ccd_per_euro,
};
let response = v1::InvokeResponse::Success {
new_balance: self
.contract_balance_unchecked(invocation_data.address),
data: Some(to_bytes(&exchange_rates)),
};
self.remaining_energy.tick_energy(
constants::CONTRACT_INSTANCE_QUERY_EXCHANGE_RATE_COST,
)?;
stack.push(Next::Resume {
data: invocation_data,
config,
response: Some(response),
});
}
}
}
v1::ReceiveResult::Reject {
reason,
return_value,
remaining_energy,
} => {
self.update_energy(remaining_energy);
// Remove the failure stack traces from the list and include them in a failure
// element.
let failure_traces =
trace_elements.split_off(invocation_data.trace_elements_checkpoint);
// Create a `WithFailures` element even if `failure_traces` is empty, as the
// reject reason and energy usage is still relevant.
let with_failure = DebugTraceElement::WithFailures {
contract_address: invocation_data.address,
entrypoint: invocation_data.entrypoint.clone(),
error: InvokeExecutionError::Reject {
reason,
return_value: return_value.clone(),
},
trace_elements: failure_traces,
energy_used: self.energy_used(),
};
trace_elements.push(with_failure);
// Reset the next modification index as well.
self.next_contract_modification_index = invocation_data.next_mod_idx_checkpoint;
invoke_response = Some(v1::InvokeResponse::Failure {
kind: v1::InvokeFailure::ContractReject {
code: reason,
data: return_value,
},
});
}
v1::ReceiveResult::Trap {
error,
remaining_energy,
} => {
self.update_energy(remaining_energy);
// Remove the failure stack traces from the list and include them in a failure
// element.
let failure_traces =
trace_elements.split_off(invocation_data.trace_elements_checkpoint);
// Create a `WithFailures` element even if `failure_traces` is empty, as the
// error and energy usage is still relevant.
let with_failure = DebugTraceElement::WithFailures {
contract_address: invocation_data.address,
entrypoint: invocation_data.entrypoint.clone(),
error: InvokeExecutionError::Trap {
error: ExecutionError(error),
},
trace_elements: failure_traces,
energy_used: self.energy_used(),
};
trace_elements.push(with_failure);
// Reset the next modification index as well.
self.next_contract_modification_index = invocation_data.next_mod_idx_checkpoint;
invoke_response = Some(v1::InvokeResponse::Failure {
kind: v1::InvokeFailure::RuntimeError,
});
}
// Convert this to an error here, so that we will short circuit processing.
v1::ReceiveResult::OutOfEnergy => return Err(TestConfigurationError::OutOfEnergy),
}
}
Ok((
invoke_response.expect("Response should have been set."),
trace_elements,
))
}
/// Make a transfer from a contract to an account in the changeset.
///
/// Returns the new balance of `from`.
///
/// **Preconditions:**
/// - Assumes that `from` contract exists.
fn transfer_from_contract_to_account(
&mut self,
amount: Amount,
from: ContractAddress,
to: AccountAddress,
) -> Result<Amount, TransferError> {
// Ensure the `to` account exists.
if !self.chain.accounts.contains_key(&to.into()) {
return Err(TransferError::ToMissing);
}
// Make the transfer.
let new_balance = self.change_contract_balance(from, AmountDelta::Negative(amount))?;
self.change_account_balance(to, AmountDelta::Positive(amount))
.expect("Cannot fail when adding");
Ok(new_balance)
}
/// Make a transfer between contracts in the changeset.
///
/// Returns the new balance of `to`.
///
/// **Preconditions:**
/// - Assumes that `from` contract exists.
fn transfer_from_contract_to_contract(
&mut self,
amount: Amount,
from: ContractAddress,
to: ContractAddress,
) -> Result<Amount, TransferError> {
// Ensure the `to` contract exists.
if !self.chain.contracts.contains_key(&to) {
return Err(TransferError::ToMissing);
}
// Make the transfer.
self.change_contract_balance(from, AmountDelta::Negative(amount))?;
let new_balance = self.change_contract_balance(to, AmountDelta::Positive(amount))?;
Ok(new_balance)
}
/// Make a transfer from an account to a contract in the changeset.
///
/// Returns the new balance of `to`.
///
/// **Preconditions:**
/// - Assumes that `from` account exists.
fn transfer_from_account_to_contract(
&mut self,
amount: Amount,
from: AccountAddress,
to: ContractAddress,
) -> Result<Amount, TransferError> {
// Ensure the `to` account exists.
if !self.chain.contracts.contains_key(&to) {
return Err(TransferError::ToMissing);
}
// Make the transfer.
self.change_account_balance(from, AmountDelta::Negative(amount))?;
let new_balance = self.change_contract_balance(to, AmountDelta::Positive(amount))?;
Ok(new_balance)
}
/// Changes the contract balance in the topmost checkpoint on the changeset.
///
/// If contract isn't already present in the changeset, it is added.
///
/// Returns an error if the change in balance would go below `0`, which is a
/// valid error, or if the amounts would overflow, which is an unrecoverable
/// configuration error in the tests.
/// Otherwise, it returns the new balance of the contract.
///
/// The precondition is not part of the error type, as this is an internal
/// helper function that is only called when the precondition is met.
///
/// Returns the new balance.
///
/// **Preconditions:**
/// - Contract must exist.
fn change_contract_balance(
&mut self,
address: ContractAddress,
delta: AmountDelta,
) -> Result<Amount, BalanceError> {
match self.changeset.current_mut().contracts.entry(address) {
btree_map::Entry::Vacant(vac) => {
// get original balance
let original_balance = self
.chain
.contracts
.get(&address)
.expect("Precondition violation: contract assumed to exist")
.self_balance;
// Try to apply the balance or return an error if insufficient funds.
let new_contract_balance = delta.apply_to_balance(original_balance)?;
// Insert the changes into the changeset.
vac.insert(ContractChanges {
self_balance_delta: delta,
..ContractChanges::new(original_balance)
});
Ok(new_contract_balance)
}
btree_map::Entry::Occupied(mut occ) => {
let contract_changes = occ.get_mut();
let new_delta = contract_changes.self_balance_delta.add_delta(delta);
// Try to apply the balance or return an error if insufficient funds.
let new_contract_balance =
new_delta.apply_to_balance(contract_changes.self_balance_original)?;
contract_changes.self_balance_delta = new_delta;
Ok(new_contract_balance)
}
}
}
/// Changes the account balance in the topmost checkpoint on the changeset.
///
/// If account isn't already present in the changeset, it is added.
///
/// Returns an error if a negative delta is provided which exceeds the
/// available balance of the account. Otherwise, it returns the new
/// available balance of the account.
/// Otherwise, it returns the new balance of the account.
///
/// The precondition is not part of the error type, as this is an internal
/// helper function that is only called when the precondition is met.
///
/// **Preconditions:**
/// - Account must exist.
fn change_account_balance(
&mut self,
address: AccountAddress,
delta: AmountDelta,
) -> Result<Amount, BalanceError> {
match self.changeset.current_mut().accounts.entry(address.into()) {
btree_map::Entry::Vacant(vac) => {
// get original balance
let mut original_balance = self
.chain
.accounts
.get(&address.into())
.expect("Precondition violation: account assumed to exist")
.balance
.available();
if self.invoker == address {
// It has been checked that the invoker account has sufficient balance for
// paying.
original_balance -= self.reserved_amount;
}
// Try to apply the balance or return an error if insufficient funds.
let new_account_balance = delta.apply_to_balance(original_balance)?;
// Insert the changes into the changeset.
vac.insert(AccountChanges {
original_balance,
balance_delta: delta,
});
Ok(new_account_balance)
}
btree_map::Entry::Occupied(mut occ) => {
let account_changes = occ.get_mut();
let new_delta = account_changes.balance_delta.add_delta(delta);
// Try to apply the balance or return an error if insufficient funds.
let new_account_balance =
new_delta.apply_to_balance(account_changes.original_balance)?;
account_changes.balance_delta = new_delta;
Ok(new_account_balance)
}
}
}
/// Returns the contract balance from the topmost checkpoint on the
/// changeset. Or, alternatively, from persistence.
///
/// **Preconditions:**
/// - Contract must exist.
fn contract_balance_unchecked(&self, address: ContractAddress) -> Amount {
self.contract_balance(address)
.expect("Precondition violation: contract must exist")
}
/// Looks up the contract balance from the topmost checkpoint on the
/// changeset. Or, alternatively, from persistence.
fn contract_balance(&self, address: ContractAddress) -> Option<Amount> {
match self.changeset.current().contracts.get(&address) {
Some(changes) => Some(changes.current_balance()),
None => self.chain.contracts.get(&address).map(|c| c.self_balance),
}
}
/// Returns the contract module from the topmost checkpoint on
/// the changeset. Or, alternatively, from persistence.
///
/// **Preconditions:**
/// - Contract instance must exist (and therefore also the artifact).
/// - If the changeset contains a module reference, then it must refer a
/// deployed module.
fn contract_module(&self, contract: &Contract) -> ContractModule {
match self
.changeset
.current()
.contracts
.get(&contract.address)
.and_then(|c| c.module)
{
// Contract has been upgrade, new module exists.
Some(new_module) => self
.chain
.modules
.get(&new_module)
.expect("Precondition violation: module must exist.")
.clone(),
// Contract hasn't been upgraded. Use persisted module.
None => self
.chain
.modules
.get(&contract.module_reference)
.expect("Precondition violation: module must exist.")
.clone(),
}
}
/// Get the contract state, either from the changeset or by thawing it from
/// persistence.
///
/// **Preconditions:**
/// - Contract instance must exist.
fn contract_state(&self, address: ContractAddress) -> trie::MutableState {
match self
.changeset
.current()
.contracts
.get(&address)
.and_then(|c| c.state.clone())
{
// Contract state has been modified.
Some(modified_state) => modified_state,
// Contract state hasn't been modified. Thaw from persistence.
None => self
.chain
.contracts
.get(&address)
.expect("Precondition violation: contract must exist")
.state
.thaw(),
}
}
/// Looks up the account balance for an account by first checking
/// the changeset, then the persisted values.
fn account_balance(&self, address: AccountAddress) -> Option<AccountBalance> {
let mut account_balance = self
.chain
.accounts
.get(&address.into())
.map(|a| a.balance)?;
match self
.changeset
.current()
.accounts
.get(&address.into())
.map(|a| a.current_balance())
{
// Account exists in changeset.
// Return the staked and locked balances from persistence, as they can't change during
// entrypoint invocation.
Some(total) => Some(AccountBalance {
total,
staked: account_balance.staked,
locked: account_balance.locked,
}),
// Account doesn't exist in changeset.
None => {
if self.invoker == address {
account_balance.total -= self.reserved_amount;
}
Some(account_balance)
}
}
}
/// Saves a mutable state for a contract in the changeset.
///
/// If the contract already has an entry in the changeset, the old state
/// will be replaced. Otherwise, the entry is created and the state is
/// added.
///
/// This method also increments the `self.next_contract_modification_index`.
///
/// Returns the `modification_index` set for the contract.
///
/// **Preconditions:**
/// - Contract must exist.
fn save_state_changes(
&mut self,
address: ContractAddress,
state: &mut trie::MutableState,
) -> u32 {
let state = state.clone();
let modification_index = self.next_contract_modification_index;
match self.changeset.current_mut().contracts.entry(address) {
btree_map::Entry::Vacant(vac) => {
let original_balance = self
.chain
.contracts
.get(&address)
.expect("Precondition violation: contract must exist.")
.self_balance;
vac.insert(ContractChanges {
state: Some(state),
modification_index,
..ContractChanges::new(original_balance)
});
}
btree_map::Entry::Occupied(mut occ) => {
let changes = occ.get_mut();
changes.state = Some(state);
changes.modification_index = modification_index;
}
}
self.next_contract_modification_index += 1;
modification_index
}
/// Saves a new module reference for the contract in the changeset.
///
/// If the contract already has an entry in the changeset, the old module is
/// replaced. Otherwise, the entry is created and the module is added.
///
/// Returns the previous module, which is either the one from persistence,
/// or the most recent one from the changeset.
///
/// **Preconditions:**
/// - Contract must exist.
/// - Module must exist.
fn save_module_upgrade(
&mut self,
address: ContractAddress,
module_reference: ModuleReference,
) -> ModuleReference {
match self.changeset.current_mut().contracts.entry(address) {
btree_map::Entry::Vacant(vac) => {
let contract = self
.chain
.contracts
.get(&address)
.expect("Precondition violation: contract must exist.");
let old_module_ref = contract.module_reference;
let original_balance = contract.self_balance;
vac.insert(ContractChanges {
module: Some(module_reference),
..ContractChanges::new(original_balance)
});
old_module_ref
}
btree_map::Entry::Occupied(mut occ) => {
let changes = occ.get_mut();
let old_module_ref = match changes.module {
Some(old_module) => old_module,
None => {
self.chain
.contracts
.get(&address)
.expect("Precondition violation: contract must exist.")
.module_reference
}
};
changes.module = Some(module_reference);
old_module_ref
}
}
}
/// Returns the modification index for a contract.
///
/// It looks it up in the changeset, and if it isn't there, it will return
/// `0`.
fn modification_index(&self, address: ContractAddress) -> u32 {
self.changeset
.current()
.contracts
.get(&address)
.map_or(0, |c| c.modification_index)
}
/// Makes a new checkpoint.
fn checkpoint(&mut self) { self.changeset.checkpoint(); }
/// Roll back to the previous checkpoint.
fn rollback(&mut self) { self.changeset.rollback(); }
/// Update the `remaining_energy` field by converting the input to
/// [`InterpreterEnergy`] and then [`Energy`].
fn update_energy(&mut self, remaining_energy: u64) {
*self.remaining_energy = from_interpreter_energy(InterpreterEnergy::from(remaining_energy));
}
/// Run the interpreter with the provided function and the
/// `self.remaining_energy`.
///
/// This function ensures that the energy calculations is handled as in the
/// node.
fn run_interpreter<F>(
&mut self,
f: F,
) -> Result<v1::ReceiveResult<artifact::CompiledFunction>, InsufficientEnergy>
where
F: FnOnce(InterpreterEnergy) -> ExecResult<v1::ReceiveResult<artifact::CompiledFunction>>,
{
let available_interpreter_energy = to_interpreter_energy(*self.remaining_energy);
let res = match f(available_interpreter_energy) {
Ok(res) => res,
Err(err) => {
// An error occurred in the interpreter and it doesn't return the remaining
// energy. We convert this to a trap and set the energy to the
// last known amount.
return Ok(v1::ReceiveResult::Trap {
error: err,
remaining_energy: available_interpreter_energy.energy,
});
}
};
let mut subtract_then_convert = |remaining_energy| -> Result<u64, InsufficientEnergy> {
let remaining_energy = InterpreterEnergy::from(remaining_energy);
// Using `saturating_sub` here should be ok since we should never be able to use
// more energy than what is available.
let used_energy = from_interpreter_energy(
available_interpreter_energy.saturating_sub(remaining_energy),
);
self.remaining_energy.tick_energy(used_energy)?;
Ok(to_interpreter_energy(*self.remaining_energy).energy)
};
match res {
v1::ReceiveResult::Success {
logs,
state_changed,
return_value,
remaining_energy,
} => {
let remaining_energy = subtract_then_convert(remaining_energy)?;
Ok(v1::ReceiveResult::Success {
logs,
state_changed,
return_value,
remaining_energy,
})
}
v1::ReceiveResult::Interrupt {
remaining_energy,
state_changed,
logs,
config,
interrupt,
} => {
let remaining_energy = subtract_then_convert(remaining_energy)?;
Ok(v1::ReceiveResult::Interrupt {
remaining_energy,
state_changed,
logs,
config,
interrupt,
})
}
v1::ReceiveResult::Reject {
reason,
return_value,
remaining_energy,
} => Ok(v1::ReceiveResult::Reject {
reason,
return_value,
remaining_energy: subtract_then_convert(remaining_energy)?,
}),
v1::ReceiveResult::Trap {
error,
remaining_energy,
} => Ok(v1::ReceiveResult::Trap {
error,
remaining_energy: subtract_then_convert(remaining_energy)?,
}),
// Convert this to an error so that we will short-circuit the processing.
v1::ReceiveResult::OutOfEnergy => Err(InsufficientEnergy),
}
}
/// The energy used so far in this transaction.
fn energy_used(&self) -> Energy { self.energy_reserved - *self.remaining_energy }
/// Helper for that constructs and pushes a [`DebugTraceElement::Regular`]
/// to the `trace_elements` list provided.
fn push_regular_trace_element(
&self,
trace_elements: &mut Vec<DebugTraceElement>,
trace_element: ContractTraceElement,
entrypoint: OwnedEntrypointName,
) {
let energy_used = self.energy_used();
let new = DebugTraceElement::Regular {
entrypoint,
trace_element,
energy_used,
};
trace_elements.push(new);
}
}
impl ChangeSet {
/// Creates a new changeset with one empty [`Changes`] element on the
/// stack..
pub(crate) fn new() -> Self {
Self {
stack: vec![Changes {
contracts: BTreeMap::new(),
accounts: BTreeMap::new(),
}],
}
}
/// Make a checkpoint by putting a clone of the top element onto the stack.
fn checkpoint(&mut self) {
let cloned_top_element = self.current().clone();
self.stack.push(cloned_top_element);
}
/// Perform a rollback by popping the top element of the stack.
fn rollback(&mut self) {
self.stack
.pop()
.expect("Internal error: change set stack should never be empty.");
}
/// Get an immutable reference the current (latest) checkpoint.
fn current(&self) -> &Changes {
self.stack
.last()
.expect("Internal error: change set stack should never be empty.")
}
/// Get a mutable reference to the current (latest) checkpoint.
fn current_mut(&mut self) -> &mut Changes {
self.stack
.last_mut()
.expect("Internal error: change set stack should never be empty.")
}
/// Try to persist all changes from the changeset.
///
/// If the energy needed for storing extra state is larger than the
/// `remaining_energy`, then:
/// - no changes will be persisted,
/// - an [`OutOfEnergy`] error is returned.
///
/// Otherwise, it returns whether the state of the provided
/// `invoked_contract` was changed.
///
/// **Preconditions:**
/// - All contracts, modules, accounts referred must exist in persistence.
/// - All amount deltas must be valid (i.e. not cause underflows when added
/// to balance).
pub(crate) fn persist(
mut self,
remaining_energy: &mut Energy,
invoked_contract: ContractAddress,
persisted_accounts: &mut BTreeMap<AccountAddressEq, Account>,
persisted_contracts: &mut BTreeMap<ContractAddress, Contract>,
) -> Result<bool, InsufficientEnergy> {
let current = self.current_mut();
let mut invoked_contract_has_state_changes = false;
// Persist contract changes and collect the total increase in states sizes.
let mut collector = v1::trie::SizeCollector::default();
let mut loader = v1::trie::Loader::new(&[][..]); // An empty loader is fine currently, as we do not use caching in this lib.
let mut frozen_states: BTreeMap<ContractAddress, trie::PersistentState> = BTreeMap::new();
// Create frozen versions of all the states, to compute the energy needed.
for (addr, changes) in current.contracts.iter_mut() {
if let Some(modified_state) = &mut changes.state {
frozen_states.insert(*addr, modified_state.freeze(&mut loader, &mut collector));
}
}
// One energy per extra byte of state.
let energy_for_state_increase = Energy::from(collector.collect());
// Return an error if out of energy.
remaining_energy.tick_energy(energy_for_state_increase)?;
// Then persist all the changes.
for (addr, changes) in current.contracts.iter_mut() {
let mut contract = persisted_contracts
.get_mut(addr)
.expect("Precondition violation: contract must exist");
// Update balance.
if !changes.self_balance_delta.is_zero() {
contract.self_balance = changes
.self_balance_delta
.apply_to_balance(changes.self_balance_original)
.expect("Precondition violation: amount delta causes underflow");
}
// Update module reference.
if let Some(new_module_ref) = changes.module {
contract.module_reference = new_module_ref;
}
// Update state.
if changes.state.is_some() {
if *addr == invoked_contract {
invoked_contract_has_state_changes = true;
}
// Replace with the frozen state we created earlier.
contract.state = frozen_states
.remove(addr)
.expect("Known to exist since we just added it.");
}
}
// Persist account changes.
for (addr, changes) in current.accounts.iter() {
let mut account = persisted_accounts
.get_mut(addr)
.expect("Precondition violation: account must exist");
// Update balance.
if !changes.balance_delta.is_zero() {
account.balance.total = changes
.balance_delta
.apply_to_balance(account.balance.total)
.expect("Precondition violation: amount delta causes underflow");
}
}
Ok(invoked_contract_has_state_changes)
}
/// Traverses the last checkpoint in the changeset and collects the energy
/// needed to be charged for additional state bytes.
///
/// Returns an [`OutOfEnergy`] error if the energy needed for storing the
/// extra state is larger than `remaining_energy`.
///
/// Otherwise, it returns whether the state of the provided
/// `invoked_contract` has changed.
pub(crate) fn collect_energy_for_state(
mut self,
remaining_energy: &mut Energy,
invoked_contract: ContractAddress,
) -> Result<bool, InsufficientEnergy> {
let mut invoked_contract_has_state_changes = false;
let mut loader = v1::trie::Loader::new(&[][..]); // An empty loader is fine currently, as we do not use caching in this lib.
let mut collector = v1::trie::SizeCollector::default();
for (addr, changes) in self.current_mut().contracts.iter_mut() {
if let Some(modified_state) = &mut changes.state {
if *addr == invoked_contract {
invoked_contract_has_state_changes = true;
}
modified_state.freeze(&mut loader, &mut collector);
}
}
// One energy per extra byte in the state.
let energy_for_state_increase = Energy::from(collector.collect());
// Return an error if we run out of energy.
remaining_energy.tick_energy(energy_for_state_increase)?;
Ok(invoked_contract_has_state_changes)
}
}
impl Default for ChangeSet {
fn default() -> Self { Self::new() }
}
impl AmountDelta {
/// Create a new [`Self`], with the value `+0`.
fn new() -> Self { Self::Positive(Amount::zero()) }
/// Subtract an [`Amount`] from [`Self`].
fn subtract_amount(self, amount: Amount) -> Self {
match self {
Self::Positive(current) => {
if current >= amount {
Self::Positive(current.subtract_micro_ccd(amount.micro_ccd))
} else {
Self::Negative(amount.subtract_micro_ccd(current.micro_ccd))
}
}
Self::Negative(current) => Self::Negative(current.add_micro_ccd(amount.micro_ccd)),
}
}
/// Add an [`Amount`] from [`Self`].
fn add_amount(self, amount: Amount) -> Self {
match self {
Self::Positive(current) => Self::Positive(current.add_micro_ccd(amount.micro_ccd)),
Self::Negative(current) => {
if current >= amount {
Self::Negative(current.subtract_micro_ccd(amount.micro_ccd))
} else {
Self::Positive(amount.subtract_micro_ccd(current.micro_ccd))
}
}
}
}
/// Add two [`Self`] to create a new one.
fn add_delta(self, delta: AmountDelta) -> Self {
match delta {
AmountDelta::Positive(d) => self.add_amount(d),
AmountDelta::Negative(d) => self.subtract_amount(d),
}
}
/// Whether the [`Self`] is zero (either `+0` or `-0`).
fn is_zero(&self) -> bool {
match self {
AmountDelta::Positive(d) => d.micro_ccd == 0,
AmountDelta::Negative(d) => d.micro_ccd == 0,
}
}
/// Returns a new balance with the `AmountDelta` applied, or a
/// [`BalanceError`] error if the change would result in a negative
/// balance or an overflow.
fn apply_to_balance(&self, balance: Amount) -> Result<Amount, BalanceError> {
match self {
AmountDelta::Positive(d) => balance.checked_add(*d).ok_or(BalanceError::Overflow),
AmountDelta::Negative(d) => balance.checked_sub(*d).ok_or(BalanceError::Insufficient),
}
}
}
impl ContractChanges {
/// Create a new `Self`. The original balance must be provided, all other
/// fields take on default values.
fn new(original_balance: Amount) -> Self {
Self {
modification_index: 0,
self_balance_delta: AmountDelta::new(),
self_balance_original: original_balance,
state: None,
module: None,
}
}
/// Get the current balance by adding the original balance and the balance
/// delta.
///
/// **Preconditions:**
/// - `balance_delta + original_balance` must be larger than `0`.
fn current_balance(&self) -> Amount {
self.self_balance_delta
.apply_to_balance(self.self_balance_original)
.expect("Precondition violation: invalid `balance_delta`.")
}
}
impl AccountChanges {
/// Get the current balance by adding the original balance and the balance
/// delta.
///
/// **Preconditions:**
/// - `balance_delta + original_balance` must be larger than `0`.
fn current_balance(&self) -> Amount {
self.balance_delta
.apply_to_balance(self.original_balance)
.expect("Precondition violation: invalid `balance_delta`.")
}
}
impl From<InsufficientEnergy> for TestConfigurationError {
fn from(_: InsufficientEnergy) -> Self { Self::OutOfEnergy }
}
#[cfg(test)]
mod tests {
mod amount_delta {
use crate::{invocation::types::AmountDelta, Amount};
#[test]
fn test() {
let mut x = AmountDelta::new();
assert_eq!(x, AmountDelta::Positive(Amount::zero()));
let one = Amount::from_ccd(1);
let two = Amount::from_ccd(2);
let three = Amount::from_ccd(3);
let five = Amount::from_ccd(5);
x = x.subtract_amount(one); // -1 CCD
x = x.subtract_amount(one); // -2 CCD
assert_eq!(x, AmountDelta::Negative(two));
x = x.add_amount(five); // +3 CCD
assert_eq!(x, AmountDelta::Positive(three));
x = x.subtract_amount(five); // -2 CCD
assert_eq!(x, AmountDelta::Negative(two));
x = x.add_amount(two); // 0
x = x.add_amount(Amount::from_micro_ccd(1)); // 1 mCCD
assert_eq!(x, AmountDelta::Positive(Amount::from_micro_ccd(1)));
x = x.subtract_amount(Amount::from_micro_ccd(2)); // -1 mCCD
assert_eq!(x, AmountDelta::Negative(Amount::from_micro_ccd(1)));
}
}
}