-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathExample.mo
476 lines (454 loc) · 21.1 KB
/
Example.mo
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
/**
* Module : ICTC Saga Test
* Author : ICLighthouse Team
* Stability : Experimental
* Github : https://github.com/iclighthouse/ICTC/
*/
import Array "mo:base/Array";
import Blob "mo:base/Blob";
import Nat "mo:base/Nat";
import Nat64 "mo:base/Nat64";
// import Iter "mo:base/Iter";
import Time "mo:base/Time";
import Option "mo:base/Option";
import Error "mo:base/Error";
import Principal "mo:base/Principal";
import Binary "mo:icl/Binary";
import CallType "../src/CallType";
import TA "../src/TA";
import SagaTM "../src/SagaTM";
shared(installMsg) actor class Example() = this {
public type CustomCallType = {
#This: {
#foo : (Nat);
};
};
type CallType = CallType.CallType<CustomCallType>;
type Task = SagaTM.Task<CustomCallType>;
type TaskResult = CallType.TaskResult;
private var tokenA_canister = Principal.fromText("ueghb-uqaaa-aaaak-aaioa-cai");
private var tokenB_canister = Principal.fromText("udhbv-ziaaa-aaaak-aaioq-cai");
private var x : Nat = 0;
private func slice<T>(a: [T], from: Nat, to: ?Nat): [T]{
let len = a.size();
if (len == 0) { return []; };
var to_: Nat = Option.get(to, Nat.sub(len, 1));
if (len <= to_){ to_ := len - 1; };
var na: [T] = [];
var i: Nat = from;
while ( i <= to_ ){
na := TA.arrayAppend(na, Array.make(a[i]));
i += 1;
};
return na;
};
private func foo(_count: Nat) : Nat{
x += _count;
return x;
};
private func _localCall(_callee: Principal, _cycles: Nat, _args: CallType, _receipt: ?SagaTM.Receipt) : async (TaskResult){
switch(_args){
case(#custom(#This(method))){
switch(method){
case(#foo(count)){
var result = foo(count); // Receipt
return (#Done, ?#result(?(Binary.BigEndian.fromNat64(Nat64.fromNat(result)), debug_show(result))), null);
};
};
};
case(_){ return (#Error, null, ?{code=#future(9901); message="Non-local function."; });};
};
};
// private func _localAsync(_args: SagaTM.CallType, _receipt: ?SagaTM.Receipt) : async (TaskResult){
// switch(_args){
// case(#This(method)){
// switch(method){
// case(#foo(count)){
// var result = foo(count); // Receipt
// return (#Done, ?#This(#foo(result)), null);
// };
// };
// };
// case(_){ return (#Error, null, ?{code=#future(9901); message="Non-local function."; });};
// };
// };
private func _taskCallback(_name: Text, _tid: SagaTM.Ttid, _task: Task, _result: TaskResult) : async (){
taskLogs := TA.arrayAppend(taskLogs, [(_tid, _task, _result)]);
};
private func _orderCallback(_name: Text, _oid: SagaTM.Toid, _status: SagaTM.OrderStatus, _data: ?Blob) : async (){
orderLogs := TA.arrayAppend(orderLogs, [(_oid, _status)]);
};
private stable var taskLogs: [(SagaTM.Ttid, Task, TaskResult)] = [];
private stable var orderLogs: [(SagaTM.Toid, SagaTM.OrderStatus)] = [];
private var saga: ?SagaTM.SagaTM<CustomCallType> = null;
private func _getSaga() : SagaTM.SagaTM<CustomCallType> {
switch(saga){
case(?(_saga)){ return _saga };
case(_){
let _saga = SagaTM.SagaTM<CustomCallType>(Principal.fromActor(this), ?_localCall, ?_taskCallback, ?_orderCallback); //?_taskCallback, ?_orderCallback
saga := ?_saga;
return _saga;
};
};
};
public query func getX() : async Nat{
x;
};
public query func getTaskLogs() : async [(SagaTM.Ttid, Task, TaskResult)]{
return taskLogs;
};
public query func getOrderLogs() : async [(SagaTM.Toid, SagaTM.OrderStatus)]{
return orderLogs;
};
public shared func clearLogs() : async (){
taskLogs := [];
orderLogs := [];
};
public shared func claimTestTokens(_account: Text) : async (){
let act = TA.TA<CustomCallType>(50, 24*3600*1000000000, _localCall, null, ?_taskCallback);
var task = _task(tokenA_canister, #DRC20(#drc20_transfer(_account, 5000000000, null, null, null)), []);
let _tid1 = act.push(task);
task := _task(tokenB_canister, #DRC20(#drc20_transfer(_account, 5000000000, null, null, null)), []);
let _tid2 = act.push(task);
let _f = act.run();
};
private func _buildTask(_businessId: ?Blob, _callee: Principal, _callType: CallType, _preTtid: [SagaTM.Ttid]) : SagaTM.PushTaskRequest<CustomCallType>{
return {
callee = _callee;
callType = _callType;
preTtid = _preTtid;
attemptsMax = ?1;
recallInterval = ?0; // nanoseconds
cycles = 0;
data = _businessId;
};
};
private func _task(_callee: Principal, _callType: CallType, _preTtid: [SagaTM.Ttid]) : Task{
return {
callee = _callee;
callType = _callType;
preTtid = _preTtid;
toid = null;
forTtid = null;
attemptsMax = 3;
recallInterval = (5*1000000000); // nanoseconds
cycles = 0;
data = null;
time = Time.now();
};
};
public shared(msg) func swap1(_to: Text): async (SagaTM.Toid, ?SagaTM.OrderStatus){
let valueA: Nat = 100000000;
let valueB: Nat = 200000000;
let tokenFee: Nat = 100000;
let caller: Text = Principal.toText(msg.caller);
let to: Text = _to;
let contract: Text = Principal.toText(Principal.fromActor(this));
// Transaction:
// tokenA: transferFrom caller -> contract 1.00000010
// tokenB: transferFrom to -> contract 2.00000010
// local: _foo x+100
// tokenA: transfer contract -> to 1.00000000
// tokenB: transfer contract -> caller 2.00000000
let oid = _getSaga().create("swap1", #Forward, null, null);
var task = _buildTask(null, tokenA_canister, #DRC20(#drc20_transferFrom(caller, contract, valueA+tokenFee, null, null, null)), []);
let _tid1 =_getSaga().push(oid, task, null, null);
task := _buildTask(null, tokenB_canister, #DRC20(#drc20_transferFrom(to, contract, valueB+tokenFee, null, null, null)), []);
let _tid2 =_getSaga().push(oid, task, null, null);
task := _buildTask(null, Principal.fromActor(this), #custom(#This(#foo(1))), []);
let _tid3 =_getSaga().push(oid, task, null, null);
task := _buildTask(null, tokenA_canister, #DRC20(#drc20_transfer(to, valueA, null, null, null)), []);
let _tid4 =_getSaga().push(oid, task, null, null);
task := _buildTask(null, tokenB_canister, #DRC20(#drc20_transfer(caller, valueB, null, null, null)), []);
let _tid5 =_getSaga().push(oid, task, null, null);
_getSaga().close(oid);
let res = await _getSaga().run(oid);
return (oid, res);
};
// The callee achieves internal task atomicity
// Caller takes a variety of ways to achieve eventual consistency, including
// ** Retries
// ** Automatic reversal task
// ** Governance or manual reversal task
// Debit first, credit later principle (receive first, freezable txn first)
// Caller-led principle (the caller acts as coordinator)
public shared(msg) func swap2(_to: Text): async (SagaTM.Toid, ?SagaTM.OrderStatus){
let valueA: Nat = 100000000;
let valueB: Nat = 200000000;
let tokenFee: Nat = 100000;
let caller: Text = Principal.toText(msg.caller);
let to: Text = _to;
let contract: Text = Principal.toText(Principal.fromActor(this));
// Transaction:
// tokenA: transferFrom caller -> contract 1.00000010 // Rollback when an exception occurs
// tokenB: transferFrom to -> contract 2.00000010 // Rollback when an exception occurs
// local: _foo x+100 // Skip when an exception occurs
// tokenA: transfer contract -> to 1.00000000 // Block when an exception occurs
// tokenB: transfer contract -> caller 2.00000000 // Block when an exception occurs
let oid = _getSaga().create("swap2", #Backward, null, null);
var task = _buildTask(null, tokenA_canister, #DRC20(#drc20_transferFrom(caller, contract, valueA+tokenFee, null, null, null)), []);
var comp = _buildTask(null, tokenA_canister, #DRC20(#drc20_transfer(caller, valueA, null, null, null)), []);
let _tid1 =_getSaga().push(oid, task, ?comp, null);
task := _buildTask(null, tokenB_canister, #DRC20(#drc20_transferFrom(to, contract, valueB+tokenFee, null, null, null)), []);
comp := _buildTask(null, tokenB_canister, #DRC20(#drc20_transfer(to, valueB, null, null, null)), []);
let _tid2 =_getSaga().push(oid, task, ?comp, null);
task := _buildTask(null, Principal.fromActor(this), #custom(#This(#foo(1))), []);
comp := _buildTask(null, Principal.fromActor(this), #__skip, []);
let _tid3 =_getSaga().push(oid, task, ?comp, null);
task := _buildTask(null, tokenA_canister, #DRC20(#drc20_transfer(to, valueA, null, null, null)), []);
let _tid4 =_getSaga().push(oid, task, null, null);
task := _buildTask(null, tokenB_canister, #DRC20(#drc20_transfer(caller, valueB, null, null, null)), []);
let _tid5 =_getSaga().push(oid, task, null, null);
_getSaga().close(oid);
let res = await _getSaga().run(oid);
return (oid, res);
};
public shared(msg) func swap3(_to: Text): async (SagaTM.Toid, ?SagaTM.OrderStatus){
let valueA: Nat = 100000000;
let valueB: Nat = 200000000;
let caller: Text = Principal.toText(msg.caller);
let to: Text = _to;
// Transaction:
// tokenA: lockTransfer caller -> to 1.00000000 comp: execute
// tokenB: lockTransfer to -> caller 2.00000000 comp: execute
// local: _foo x+100
// tokenA: executeTransfer caller -> to 1.00000000
// tokenB: executeTransfer to -> caller 2.00000000
let oid = _getSaga().create("swap3", #Backward, null, null);
var task = _buildTask(null, tokenA_canister, #DRC20(#drc20_lockTransferFrom(caller, to, valueA, 100000, null, null, null, null)), []);
var comp = _buildTask(null, tokenA_canister, #DRC20(#drc20_executeTransfer(#AutoFill, #fallback, null, null, null, null)), []);
let tid1 =_getSaga().push(oid, task, ?comp, null);
task := _buildTask(null, tokenB_canister, #DRC20(#drc20_lockTransferFrom(to, caller, valueB, 100000, null, null, null, null)), []);
comp := _buildTask(null, tokenB_canister, #DRC20(#drc20_executeTransfer(#AutoFill, #fallback, null, null, null, null)), []);
let tid2 =_getSaga().push(oid, task, ?comp, null);
task := _buildTask(null, Principal.fromActor(this), #custom(#This(#foo(1))), []);
comp := _buildTask(null, Principal.fromActor(this), #__skip, []);
let _tid3 =_getSaga().push(oid, task, null, null);
let _res = await _getSaga().run(oid);
var txid1 : Blob = Blob.fromArray([]);
switch(_getSaga().getActuator().getTaskEvent(tid1)){
case(?(event)){
switch(event.result.1){
case(?(#DRC20(#drc20_executeTransfer(#ok(_txid))))){ txid1 := _txid };
case(_){};
};
};
case(_){ return (oid, await _getSaga().run(oid)); /* blocking */};
};
task := _buildTask(null, tokenA_canister, #DRC20(#drc20_executeTransfer(#ManualFill(txid1), #sendAll, null, null, null, null)), []);
let _tid4 =_getSaga().push(oid, task, null, null);
var txid2 : Blob = Blob.fromArray([]);
switch(_getSaga().getActuator().getTaskEvent(tid2)){
case(?(event)){
switch(event.result.1){
case(?(#DRC20(#drc20_executeTransfer(#ok(_txid))))){ txid2 := _txid };
case(_){};
};
};
case(_){ return (oid, await _getSaga().run(oid)); /* blocking */};
};
task := _buildTask(null, tokenB_canister, #DRC20(#drc20_executeTransfer(#ManualFill(txid2), #sendAll, null, null, null, null)), []);
let _tid5 =_getSaga().push(oid, task, null, null);
_getSaga().close(oid);
return (oid, await _getSaga().run(oid));
};
/**
* ICTC Transaction Explorer Interface
* (Optional) Implement the following interface, which allows you to browse transaction records and execute compensation transactions through a UI interface.
* https://cmqwp-uiaaa-aaaaj-aihzq-cai.raw.ic0.app/
*/
// ICTC: management functions
private stable var ictc_admins: [Principal] = [];
private func _onlyIctcAdmin(_caller: Principal) : Bool {
return Option.isSome(Array.find(ictc_admins, func (t: Principal): Bool{ t == _caller }));
};
private func _onlyBlocking(_toid: Nat) : Bool{
/// Saga
switch(_getSaga().status(_toid)){
case(?(status)){ return status == #Blocking };
case(_){ return false; };
};
/// 2PC
// switch(_getTPC().status(_toid)){
// case(?(status)){ return status == #Blocking };
// case(_){ return false; };
// };
};
public query func ictc_getAdmins() : async [Principal]{
return ictc_admins;
};
public shared(msg) func ictc_addAdmin(_admin: Principal) : async (){
assert(_onlyIctcAdmin(msg.caller));
if (Option.isNull(Array.find(ictc_admins, func (t: Principal): Bool{ t == _admin }))){
ictc_admins := TA.arrayAppend(ictc_admins, [_admin]);
};
};
public shared(msg) func ictc_removeAdmin(_admin: Principal) : async (){
assert(_onlyIctcAdmin(msg.caller));
ictc_admins := Array.filter(ictc_admins, func (t: Principal): Bool{ t != _admin });
};
// SagaTM Scan
public query func ictc_TM() : async Text{
return "Saga";
};
/// Saga
public query func ictc_getTOCount() : async Nat{
return _getSaga().count();
};
public query func ictc_getTO(_toid: SagaTM.Toid) : async ?SagaTM.Order<CustomCallType>{
return _getSaga().getOrder(_toid);
};
public query func ictc_getTOs(_page: Nat, _size: Nat) : async {data: [(SagaTM.Toid, SagaTM.Order<CustomCallType>)]; totalPage: Nat; total: Nat}{
return _getSaga().getOrders(_page, _size);
};
public query func ictc_getPool() : async {toPool: {total: Nat; items: [(SagaTM.Toid, ?SagaTM.Order<CustomCallType>)]}; ttPool: {total: Nat; items: [(SagaTM.Ttid, Task)]}}{
let tos = _getSaga().getAliveOrders();
let tts = _getSaga().getActuator().getTaskPool();
return {
toPool = { total = tos.size(); items = slice(tos, 0, ?255)};
ttPool = { total = tts.size(); items = slice(tts, 0, ?255)};
};
};
public query func ictc_getTOPool() : async [(SagaTM.Toid, ?SagaTM.Order<CustomCallType>)]{
return _getSaga().getAliveOrders();
};
public query func ictc_getTT(_ttid: SagaTM.Ttid) : async ?SagaTM.TaskEvent<CustomCallType>{
return _getSaga().getActuator().getTaskEvent(_ttid);
};
public query func ictc_getTTByTO(_toid: SagaTM.Toid) : async [SagaTM.TaskEvent<CustomCallType>]{
return _getSaga().getTaskEvents(_toid);
};
public query func ictc_getTTs(_page: Nat, _size: Nat) : async {data: [(SagaTM.Ttid, SagaTM.TaskEvent<CustomCallType>)]; totalPage: Nat; total: Nat}{
return _getSaga().getActuator().getTaskEvents(_page, _size);
};
public query func ictc_getTTPool() : async [(SagaTM.Ttid, Task)]{
return _getSaga().getActuator().getTaskPool();
};
public query func ictc_getTTErrors(_page: Nat, _size: Nat) : async {data: [(Nat, SagaTM.ErrorLog)]; totalPage: Nat; total: Nat}{
return _getSaga().getActuator().getErrorLogs(_page, _size);
};
public query func ictc_getCalleeStatus(_callee: Principal) : async ?SagaTM.CalleeStatus{
return _getSaga().getActuator().calleeStatus(_callee);
};
// Transaction Governance
public shared(msg) func ictc_clearLog(_expiration: ?Int, _delForced: Bool) : async (){ // Warning: Execute this method with caution
assert(_onlyIctcAdmin(msg.caller));
_getSaga().clear(_expiration, _delForced);
};
public shared(msg) func ictc_clearTTPool() : async (){ // Warning: Execute this method with caution
assert(_onlyIctcAdmin(msg.caller));
_getSaga().getActuator().clearTasks();
};
public shared(msg) func ictc_blockTO(_toid: SagaTM.Toid) : async ?SagaTM.Toid{
assert(_onlyIctcAdmin(msg.caller));
assert(not(_onlyBlocking(_toid)));
let saga = _getSaga();
return saga.block(_toid);
};
// public shared(msg) func ictc_removeTT(_toid: SagaTM.Toid, _ttid: SagaTM.Ttid) : async ?SagaTM.Ttid{ // Warning: Execute this method with caution
// assert(_onlyIctcAdmin(msg.caller));
// assert(_onlyBlocking(_toid));
// let saga = _getSaga();
// saga.open(_toid);
// let ttid = saga.remove(_toid, _ttid);
// saga.close(_toid);
// return ttid;
// };
public shared(msg) func ictc_appendTT(_businessId: ?Blob, _toid: SagaTM.Toid, _forTtid: ?SagaTM.Ttid, _callee: Principal, _callType: SagaTM.CallType<CustomCallType>, _preTtids: [SagaTM.Ttid]) : async SagaTM.Ttid{
// Governance or manual compensation (operation allowed only when a transaction order is in blocking status).
assert(_onlyIctcAdmin(msg.caller));
assert(_onlyBlocking(_toid));
let saga = _getSaga();
saga.open(_toid);
let taskRequest = _buildTask(_businessId, _callee, _callType, _preTtids);
//let ttid = saga.append(_toid, taskRequest, null, null);
let ttid = saga.appendComp(_toid, Option.get(_forTtid, 0), taskRequest, null);
return ttid;
};
/// Try the task again
public shared(msg) func ictc_redoTT(_toid: SagaTM.Toid, _ttid: SagaTM.Ttid) : async ?SagaTM.Ttid{
// Warning: proceed with caution!
assert(_onlyIctcAdmin(msg.caller));
let saga = _getSaga();
let ttid = saga.redo(_toid, _ttid);
let _r = await saga.run(_toid);
return ttid;
};
/// set status of pending task
public shared(msg) func ictc_doneTT(_toid: SagaTM.Toid, _ttid: SagaTM.Ttid, _toCallback: Bool) : async ?SagaTM.Ttid{
// Warning: proceed with caution!
assert(_onlyIctcAdmin(msg.caller));
let saga = _getSaga();
try{
let ttid = await* saga.taskDone(_toid, _ttid, _toCallback);
return ttid;
}catch(e){
throw Error.reject("420: internal call error: "# Error.message(e));
};
};
/// set status of pending order
public shared(msg) func ictc_doneTO(_toid: SagaTM.Toid, _status: SagaTM.OrderStatus, _toCallback: Bool) : async Bool{
// Warning: proceed with caution!
assert(_onlyIctcAdmin(msg.caller));
let saga = _getSaga();
try{
let res = await* saga.done(_toid, _status, _toCallback);
return res;
}catch(e){
throw Error.reject("420: internal call error: "# Error.message(e));
};
};
/// Complete blocking order
public shared(msg) func ictc_completeTO(_toid: SagaTM.Toid, _status: SagaTM.OrderStatus) : async Bool{
// After governance or manual compensations, this method needs to be called to complete the transaction order.
assert(_onlyIctcAdmin(msg.caller));
assert(_onlyBlocking(_toid));
let saga = _getSaga();
saga.close(_toid);
let _r = await saga.run(_toid);
try{
let r = await* _getSaga().complete(_toid, _status);
return r;
}catch(e){
throw Error.reject("430: ICTC error: "# Error.message(e));
};
};
public shared(msg) func ictc_runTO(_toid: SagaTM.Toid) : async ?SagaTM.OrderStatus{
assert(_onlyIctcAdmin(msg.caller));
let saga = _getSaga();
saga.close(_toid);
// await* _ictcSagaRun(_toid, true);
try{
let r = await saga.run(_toid);
return r;
}catch(e){
throw Error.reject("430: ICTC error: "# Error.message(e));
};
};
public shared(msg) func ictc_runTT() : async Bool{
// There is no need to call it normally, but can be called if you want to execute tasks in time when a TO is in the Doing state.
assert(_onlyIctcAdmin(msg.caller));
let _r = await _getSaga().run(0);
return true;
};
/**
* End: ICTC Transaction Explorer Interface
*/
// upgrade
/// Saga
private stable var __sagaDataNew: ?SagaTM.Data<CustomCallType> = null;
system func preupgrade() {
let data = _getSaga().getData();
__sagaDataNew := ?data;
// assert(List.size(data.actuator.tasks.0) == 0 and List.size(data.actuator.tasks.1) == 0);
};
system func postupgrade() {
switch(__sagaDataNew){
case(?(data)){
_getSaga().setData(data);
__sagaDataNew := null;
};
case(_){};
};
};
};