execution_log.gno
4.18 Kb · 126 lines
1package dca
2
3import (
4 "gno.land/p/gnoswap/gnsmath/v1"
5 bptree "gno.land/p/nt/bptree/v0"
6
7 "gno.land/p/gnoswap/utils/v1"
8)
9
10// ExecutionLogRoundCount is the number of consecutive rounds of an order
11// one execution log covers. A tree entry costs about 640 bytes of storage
12// whatever it holds, so covering three rounds keeps the deposit near
13// 0.02 GNOT per round at 100 ugnot per byte.
14const ExecutionLogRoundCount uint64 = 3
15
16// ExecutionLog is an order's record of up to ExecutionLogRoundCount
17// consecutive rounds, [firstRound, lastRound], and the input the order sells
18// in each. The rounds' swap results are in the DCAGroupExecuted events,
19// and accounting uses the group accumulator.
20//
21// AllocateExecutionLogs stores an order's logs in the realm's executionLogs
22// tree, keyed by ExecutionLogKey(orderID, firstRound), so the order owner
23// pays their storage deposit up front; ExecuteGroup writes none. Once every
24// round of a log executed, anyone may release it through Claim or
25// ClaimGroupOrders, and a finished order releases all of its logs; the freed
26// deposit is refunded to the transaction signer.
27//
28// The tree stores only lastRound, a primitive kept inside the B+tree leaf;
29// firstRound comes from the key and amountIn from the order.
30type ExecutionLog struct {
31 firstRound uint64
32 lastRound uint64
33 amountIn int64 // input the order sells in each round, its amountPerExecution
34}
35
36func (l *ExecutionLog) FirstRound() uint64 { return l.firstRound }
37func (l *ExecutionLog) LastRound() uint64 { return l.lastRound }
38func (l *ExecutionLog) AmountIn() int64 { return l.amountIn }
39
40// RoundCount returns the number of rounds the log covers.
41func (l *ExecutionLog) RoundCount() uint64 { return l.lastRound - l.firstRound + 1 }
42
43// AllocateExecutionLogs stores the logs of the order's unreleased rounds,
44// ExecutionLogRoundCount rounds per log, and returns how many were stored.
45func AllocateExecutionLogs(executionLogs *bptree.BPTree, order *Order) int64 {
46 allocatedCount := int64(0)
47
48 for firstRound := gnsmath.SafeAddUint64(order.releasedRound, 1); firstRound <= order.endRound; {
49 lastRound := lastRoundOfLog(firstRound, order.endRound)
50 executionLogs.Set(ExecutionLogKey(order.id, firstRound), lastRound)
51
52 allocatedCount++
53
54 if lastRound == order.endRound {
55 break
56 }
57
58 firstRound = gnsmath.SafeAddUint64(lastRound, 1)
59 }
60
61 return allocatedCount
62}
63
64// ReleaseExecutionLogs removes the order's logs whose rounds all executed by
65// executedRound, the group round, and returns how many were released.
66// A log with a round still to run stays.
67func ReleaseExecutionLogs(executionLogs *bptree.BPTree, order *Order, executedRound uint64) int64 {
68 if executedRound > order.endRound {
69 executedRound = order.endRound
70 }
71
72 releasedCount := int64(0)
73
74 for order.releasedRound < order.endRound {
75 firstRound := gnsmath.SafeAddUint64(order.releasedRound, 1)
76
77 lastRound := lastRoundOfLog(firstRound, order.endRound)
78 if lastRound > executedRound {
79 break
80 }
81
82 executionLogs.Remove(ExecutionLogKey(order.id, firstRound))
83 order.setReleasedRound(lastRound)
84
85 releasedCount++
86 }
87
88 return releasedCount
89}
90
91// ReleaseAllExecutionLogs removes every log of the order, executed or not,
92// and returns how many were released.
93func ReleaseAllExecutionLogs(executionLogs *bptree.BPTree, order *Order) int64 {
94 return ReleaseExecutionLogs(executionLogs, order, order.endRound)
95}
96
97// ExecutionLogs returns up to count of the order's unreleased logs,
98// oldest first.
99func ExecutionLogs(executionLogs *bptree.BPTree, order *Order, count int) []*ExecutionLog {
100 logs := []*ExecutionLog{}
101 if count <= 0 {
102 return logs
103 }
104
105 prefix := ExecutionLogPrefix(order.id)
106 executionLogs.Iterate(prefix, PrefixEnd(prefix), func(key string, value any) bool {
107 logs = append(logs, &ExecutionLog{
108 firstRound: utils.DecodeUint64(key[len(prefix):]),
109 lastRound: value.(uint64),
110 amountIn: order.amountPerExecution,
111 })
112
113 return len(logs) >= count
114 })
115
116 return logs
117}
118
119// lastRoundOfLog returns the last round of the log starting at firstRound.
120func lastRoundOfLog(firstRound uint64, endRound uint64) uint64 {
121 if endRound-firstRound < ExecutionLogRoundCount {
122 return endRound
123 }
124
125 return gnsmath.SafeAddUint64(firstRound, ExecutionLogRoundCount-1)
126}