Repository navigation
Expand file tree
/
Copy pathwait-query-order.go.txt
More file actions
189 lines (183 loc) · 5.71 KB
/
Copy pathwait-query-order.go.txt
File metadata and controls
189 lines (183 loc) · 5.71 KB
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
package production
import (
"fmt"
"cloud-clicker/server/decimal"
"cloud-clicker/server/economy"
"cloud-clicker/server/save"
)
// Manual chronological query, not an adopted search API or live implementation.
func queryWaitCellsResearch(state *save.State, catalog *economy.Catalog, mode EvaluationMode, horizon int64, mask AblationMask, dependencies SimulationDependencies, quote decimal.Decimal, budget int, point func(int64) (decimal.Decimal, error)) (int64, *waitCellsResearch, error) {
return queryWaitContextResearch(state, catalog, mode, horizon, mask, dependencies, quote, budget, point, waitSourceContextResearch{}, nil)
}
// Internal proof component, not an arbitrary accrual-hook registration API.
type waitRangeGuardResearch interface {
certify(int64, int64) (bool, error)
}
// Optional concrete proof preparation, not a new runtime hook API. A guard may
// use producer bounds only when their complete global certificate succeeded.
type waitProducerGuardResearch interface {
bind(*waitCellsResearch, bool) error
}
func queryWaitContextResearch(state *save.State, catalog *economy.Catalog, mode EvaluationMode, horizon int64, mask AblationMask, dependencies SimulationDependencies, quote decimal.Decimal, budget int, point func(int64) (decimal.Decimal, error), context waitSourceContextResearch, guard waitRangeGuardResearch) (int64, *waitCellsResearch, error) {
if state == nil || state.Ledger == nil || !quote.IsStateValue() || quote.Lt(decimal.Zero) {
return -1, nil, fmt.Errorf("invalid research query")
}
if context.fromMS < 0 || context.fromMS > 0 && context.fromMS > horizon {
return -1, nil, fmt.Errorf("invalid research query floor")
}
before, exists := state.Ledger.Balance("company.cash")
if !exists {
return -1, nil, ErrInvalidEngineState
}
// Match the actual caller: an available balance needs no wait evaluation.
if context.fromMS == 0 && before.Gte(quote) {
return 0, nil, nil
}
if horizon == 0 {
return -1, nil, nil // No remaining endpoint can change the known balance.
}
if budget <= 0 || point == nil {
return -1, nil, fmt.Errorf("invalid research query budget/point")
}
price, err := cellRepresented(quote)
if err != nil {
return -1, nil, err
}
factory, err := buildWaitCellContextSourcesResearch(state, catalog, mode, horizon, mask, dependencies, "", true, context)
if err != nil {
return -1, factory, err
}
factory.work.QueryRanges++
producerDefined := certifyWaitCellsResearch(factory, state) == nil
if prepared, ok := guard.(waitProducerGuardResearch); ok {
if err := prepared.bind(factory, producerDefined); err != nil {
return -1, factory, err
}
}
guardDefined := true
timeDefined := !context.preHookEncoding
if !timeDefined {
hi := int64(0)
if len(factory.cells) > 0 {
hi = factory.cells[len(factory.cells)-1].hi
}
if hi < context.fromMS {
hi = context.fromMS
}
factory.work.QueryTimeGuards++
timeDefined = certifyWaitTimeResearch(state.EvaluatedThrough, context.fromMS, hi)
}
if guard != nil {
hi := int64(0)
if len(factory.cells) > 0 {
hi = factory.cells[len(factory.cells)-1].hi
}
if hi < context.fromMS {
hi = context.fromMS
}
factory.work.QueryGuards++
guardDefined, err = guard.certify(context.fromMS, hi)
if err != nil {
return -1, factory, err
}
}
charge := func() error {
if factory.work.QueryNodes >= budget {
return fmt.Errorf("research query node budget exhausted")
}
factory.work.QueryNodes++
return nil
}
actual := func(at int64) (int64, error) {
factory.work.Points++
value, err := point(at)
if err != nil {
return -1, err
}
if !value.IsStateValue() {
return -1, ErrInvalidEngineState
}
if value.Gte(quote) {
return at, nil
}
return -1, nil
}
var visit func(waitCellResearch, int64, int64) (int64, error)
visit = func(cell waitCellResearch, lo, hi int64) (int64, error) {
if err := charge(); err != nil {
return -1, err
}
producerSafe := producerDefined
if !producerSafe {
window := *factory
cell.lo, cell.hi = lo, hi
cell.partials, cell.aggregates = nil, nil
window.cells = []waitCellResearch{cell}
window.work.QueryRanges++
producerSafe = certifyWaitCellsResearch(&window, state) == nil
factory.work = window.work // Include failed/repeated preparation too.
}
guardSafe := guardDefined
if !guardSafe {
factory.work.QueryGuards++
var guardErr error
guardSafe, guardErr = guard.certify(lo, hi)
if guardErr != nil {
return -1, guardErr
}
}
timeSafe := timeDefined
if !timeSafe {
factory.work.QueryTimeGuards++
timeSafe = certifyWaitTimeResearch(state.EvaluatedThrough, lo, hi)
}
defined := producerSafe && guardSafe && timeSafe
if defined {
bound, err := factory.upper(cell, hi)
if err == nil && bound.Cmp(price) < 0 {
return -1, nil
}
}
if lo == hi {
return actual(lo)
}
if !defined {
factory.work.QueryRefinements++
}
mid := lo + (hi-lo)/2
found, err := visit(cell, lo, mid)
if err != nil || found >= 0 {
return found, err
}
return visit(cell, mid+1, hi)
}
for _, cell := range factory.cells {
lo := cell.lo
if lo < context.fromMS {
lo = context.fromMS
}
if lo > cell.hi {
continue
}
found, err := visit(cell, lo, cell.hi)
if err != nil || found >= 0 {
return found, factory, err
}
}
if factory.stopErr != nil {
if err := charge(); err != nil {
return -1, factory, err
}
factory.work.QueryStops++
factory.work.Points++
stopAt := factory.stopAt
if stopAt < context.fromMS {
stopAt = context.fromMS
}
if _, err := point(stopAt); err != nil {
return -1, factory, err // Return the real endpoint error, not a surrogate.
}
return -1, factory, fmt.Errorf("research source-stop witness did not fail at%d: %v", stopAt, factory.stopErr)
}
return -1, factory, nil
}