-
Notifications
You must be signed in to change notification settings - Fork 1
/
payout.go
441 lines (383 loc) · 13.8 KB
/
payout.go
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
package payout
import (
"context"
"fmt"
"strconv"
"strings"
"sync"
"time"
"github.com/go-redis/redis/v9"
"github.com/hashicorp/go-retryablehttp"
"github.com/nightowlcasino/nightowl/erg"
"github.com/nightowlcasino/nightowl/state"
"go.uber.org/zap"
)
const (
rouletteErgoTree = "101b0400040004000402054a0e20473041c7e13b5f5947640f79f00d3c5df22fad4841191260350bb8c526f9851f040004000514052605380504050404020400040205040404050f05120406050604080509050c040a0e200ef2e4e25f93775412ac620a1da495943c55ea98e72f3e95d1a18d7ace2f676cd809d601b2a5730000d602b2db63087201730100d603b2db6501fe730200d604e4c672010404d605e4c6a70404d6069e7cb2e4c67203041a9a72047303007304d607e4c6a70504d6087e720705d6099972087206d1ed96830301938c7202017305938c7202028cb2db6308a77306000293b2b2e4c67203050c1a720400e4c67201050400c5a79597830601ed937205730795ec9072067308ed9272067309907206730a939e7206730b7208ed949e7206730c7208ec937207730d937207730eed937205730f939e720673107208eded937205731192720973129072097313ed9372057314939e720673157208eded937205731692720973179072097318ed9372057319937208720693c27201e4c6a7060e93cbc27201731a"
houseAddress = "ofgUTY7c693MfaVxfuZ1YhG7RQuQCLqa7mqFHkkZcpo9r5oPmmXaemS3raHAzfP4MXXc7DiueGDFsrZ5Hp3ZK"
minerFee = 1000000 // 0.0010 ERG
minBoxValue = 1000000 // 0.0010 ERG
)
var (
log *zap.Logger
)
type Service struct {
ctx context.Context
component string
ergNode *erg.ErgNode
ergExplorer *erg.Explorer
ns *state.NotifState
rdb *redis.Client
stop chan bool
done chan bool
wg *sync.WaitGroup
}
func NewService(rdb *redis.Client, retryClient *retryablehttp.Client, ns *state.NotifState, wg *sync.WaitGroup) (service *Service, err error) {
ctx := context.Background()
log = zap.L()
ergExplorerClient, err := erg.NewExplorer(retryClient)
if err != nil {
return nil, fmt.Errorf("failed to create erg explorer client - %s", err.Error())
}
ergNodeClient, err := erg.NewErgNode(retryClient)
if err != nil {
return nil, fmt.Errorf("failed to create erg node client - %s", err.Error())
}
service = &Service{
ctx: ctx,
component: "payout",
ergNode: ergNodeClient,
ergExplorer: ergExplorerClient,
ns: ns,
rdb: rdb,
stop: make(chan bool),
done: make(chan bool),
wg: wg,
}
return service, nil
}
func wait(sleepTime time.Duration, c chan bool) {
time.Sleep(sleepTime)
c <- true
}
func (s *Service) payoutBets(stop chan bool) {
var lastHeight int
checkbets := make(chan bool, 1)
// get last known height stored in the redis db
lastBetHeight, err := s.rdb.Get(s.ctx, "oracle:lastBetHeight").Result()
switch {
case err == redis.Nil || lastBetHeight == "":
lastHeight = 0
err := s.rdb.Set(s.ctx, "oracle:lastBetHeight", lastHeight, 0).Err()
if err != nil {
log.Error("failed to set key in redis db", zap.Error(err), zap.String("redis_key", "oracle:lastBetHeight"))
}
case err != nil:
log.Error("failed to get key from redis db", zap.Error(err), zap.String("redis_key", "oracle:lastBetHeight"))
default:
lastHeight, _ = strconv.Atoi(lastBetHeight)
}
checkbets <- true
loop:
for {
select {
case <-stop:
log.Info("stopping payoutBets() loop...")
s.wg.Done()
break loop
case <-checkbets:
// clear structs
var ergTxs = erg.ErgBoxIds{}
var ergTxsBuff = erg.ErgBoxIds{}
var ergUtxo = erg.ErgTxOutputNode{}
var isSettled, allSettled bool
var txHeight int
limit := 50
offset := 0
// Need to keep retrying if this fails
currHeight, err := s.ergNode.GetCurrenHeight()
if err != nil {
log.Error("failed to get current erg height", zap.Error(err))
}
// continuously call GetOracleTxs() until we get all txs
start := time.Now()
for {
start1 := time.Now()
ergTxsBuff, err = s.ergExplorer.GetOracleTxs(lastHeight, currHeight, limit, offset)
if err != nil {
log.Error("failed to get oracle txs",
zap.Error(err),
zap.Int64("durationMs",time.Since(start).Milliseconds()),
zap.Int("last_height", lastHeight),
zap.Int("curr_height", currHeight),
zap.Int("limit", limit),
zap.Int("offset", offset),
)
continue
}
log.Debug("received erg txs",
zap.Int("tx_count", len(ergTxsBuff.Items)),
zap.Int64("durationMs", time.Since(start1).Milliseconds()),
zap.Int("last_height", lastHeight),
zap.Int("curr_height", currHeight),
zap.Int("limit", limit),
zap.Int("offset", offset),
)
if len(ergTxsBuff.Items) == 0 {
break
}
offset += limit
ergTxs.Items = append(ergTxs.Items, ergTxsBuff.Items...)
}
log.Info("finished getting all oracle txs",
zap.Int("total_txs", len(ergTxs.Items)),
zap.Int64("durationMs", time.Since(start).Milliseconds()),
)
for _, ergTx := range ergTxs.Items {
// assume all bets are settled until proven otherwise
allSettled = true
if ergTx.Height > txHeight {
txHeight = ergTx.Height
}
// convert R4 rendered value to []string
r4 := ergTx.Outputs[0].AdditionalRegisters.R4.Value
// remove surrounding brackets [ and ]
r4 = strings.TrimPrefix(r4, "[")
r4 = strings.TrimSuffix(r4, "]")
randNumbers := strings.Split(r4, ",")
// convert R5 rendered value to [][]string
r5 := ergTx.Outputs[0].AdditionalRegisters.R5.Value
// remove surrounding brackets [ and ]
r5 = strings.TrimPrefix(r5, "[")
r5 = strings.TrimSuffix(r5, "]")
// add , to the back of string to help for the split
r5 = r5 + ","
ergBoxIdsSlices := strings.Split(r5, "],")
// remove last element because it's empty
ergBoxIdsSlices = ergBoxIdsSlices[:len(ergBoxIdsSlices)-1]
for i, ergBoxIdsSlice := range ergBoxIdsSlices {
// remove leading [
ergBoxIdsClean := strings.TrimPrefix(ergBoxIdsSlice, "[")
ergBoxIds := strings.Split(ergBoxIdsClean, ",")
if len(ergBoxIds) > 0 && ergBoxIds[0] != "" {
for j, boxId := range ergBoxIds {
// check if stop signal was triggered or we are stuck in this loop
select {
case <-stop:
log.Info("stopping payoutBets() loop...")
break loop
default:
start := time.Now()
ergUtxo, err = s.ergNode.GetErgUtxoBox(boxId)
if err != nil {
log.Error("failed to get erg utxo box",
zap.Int64("durationMs", time.Since(start).Milliseconds()),
zap.String("erg_utxo_box_id", boxId),
)
} else {
log.Debug("successfully got erg utxo box",
zap.Int64("durationMs", time.Since(start).Milliseconds()),
zap.String("erg_utxo_box_id", boxId),
)
}
// check that bet ergoTree is the roulette house contract
if ergUtxo.ErgoTree == rouletteErgoTree {
startBet := time.Now()
plyrAddr, _ := s.ergNode.ErgoTreeToAddress(ergUtxo.AdditionalRegisters.R6[4:])
// check if bet exists in redis db
bet, err := s.rdb.HGetAll(s.ctx, "roulette:"+ergUtxo.BoxId+":"+plyrAddr).Result()
switch {
case err == redis.Nil || len(bet) == 0:
isSettled = false
var randNum string
if i+1 <= len(randNumbers)-1 {
randNum = randNumbers[i+1]
}
b := make(map[string]string)
b["settled"] = "false"
b["confirmed"] = "false"
b["winnerAmt"] = strconv.Itoa(ergUtxo.Assets[0].Amount)
b["winnerAddr"] = ""
b["subgame"] = ergUtxo.AdditionalRegisters.R4
b["number"] = ergUtxo.AdditionalRegisters.R5
b["randomNum"] = randNum
// add bet to redis db
err := s.rdb.HSet(s.ctx, "roulette:"+ergUtxo.BoxId+":"+plyrAddr, b).Err()
if err != nil {
log.Error("failed to set key in redis db", zap.Error(err), zap.String("redis_key", "roulette:"+ergUtxo.BoxId+":"+plyrAddr))
}
if randNum != "" {
err := s.processBet(b, ergUtxo, ergTx, plyrAddr, i, j)
if err != nil {
log.Error("failed to process bet", zap.Error(err))
} else {
isSettled = true
}
}
case err != nil:
log.Error("failed to get key from redis db", zap.Error(err), zap.String("redis_key", "roulette:"+ergUtxo.BoxId+":"+plyrAddr))
default:
if bet["randomNum"] == "" {
if i+1 <= len(randNumbers)-1 {
bet["randomNum"] = randNumbers[i+1]
err := s.rdb.HSet(s.ctx, "roulette:"+ergUtxo.BoxId+":"+plyrAddr, "randomNum", randNumbers[i+1]).Err()
if err != nil {
log.Error("failed to set key in redis db", zap.Error(err), zap.String("redis_key", "roulette:"+ergUtxo.BoxId+":"+plyrAddr))
}
}
}
// check if settled already
isSettled, _ = strconv.ParseBool(bet["settled"])
if !isSettled && bet["randomNum"] != "" {
err := s.processBet(bet, ergUtxo, ergTx, plyrAddr, i, j)
if err != nil {
log.Error("failed to process bet", zap.Error(err))
} else {
isSettled = true
}
}
}
log.Info("finished processing roulette bet", zap.Int64("durationMs", time.Since(startBet).Milliseconds()), zap.String("erg_utxo_box_id", ergUtxo.BoxId))
}
}
}
}
if !isSettled {
allSettled = false
}
}
if allSettled {
// change lastBetHeight if we know we have successfully settled every bet which has
// a height less than lastBetHeight
if txHeight > lastHeight {
err = s.rdb.Set(s.ctx, "oracle:lastBetHeight", txHeight, 0).Err()
if err != nil {
log.Error("failed to set key in redis db", zap.Error(err), zap.String("redis_key", "oracle:lastBetHeight"))
}
lastHeight = txHeight
}
}
}
// start timer in separate go routine
go wait(2 * time.Minute, checkbets)
}
}
}
func (s *Service) Start() {
stopPayout := make(chan bool)
s.wg.Add(1)
go s.payoutBets(stopPayout)
// Wait for a "stop" message in the background to stop the service.
go func(stopPayout chan bool) {
go func() {
<-s.stop
stopPayout <- true
s.done <- true
}()
}(stopPayout)
}
func (s *Service) Stop() {
s.stop <- true
}
func (s *Service) Wait(wg *sync.WaitGroup) {
defer wg.Done()
<-s.done
}
func (s *Service) processBet(bet map[string]string, box erg.ErgTxOutputNode, tx erg.ErgTx, plyrAddr string, boxPosX, boxPosY int) error {
var winnerAddr, betKey string
betKey = fmt.Sprintf("roulette:%s:%s", box.BoxId, plyrAddr)
// figure out winner and create tx to send to result contract address
randNum, err := getRandNum(bet["randomNum"])
if err != nil {
return fmt.Errorf("failed to parse random number from key '%s' - %s", betKey, err)
} else {
serializedBetBox, err := s.ergNode.SerializeErgBox(box.BoxId)
if err != nil {
return fmt.Errorf("call to SerializeErgBox with serializedBetBox failed - %s", err.Error())
}
serializedOracleBox, _ := s.ergNode.SerializeErgBox(tx.Outputs[0].BoxId)
if err != nil {
return fmt.Errorf("call to SerializeErgBox with serializedOracleBox failed - %s", err.Error())
}
subgame, _ := strconv.Atoi(bet["subgame"][2:])
chipspot, _ := strconv.Atoi(bet["number"][2:])
sg := decodeZigZag64(uint64(subgame))
cs := decodeZigZag64(uint64(chipspot))
winner := winner(int(sg), int(cs), randNum)
if winner {
winnerAddr = plyrAddr
} else {
winnerAddr = houseAddress
}
start := time.Now()
txUnsigned, _ := buildResultSmartContractTx(box, encodeZigZag64(uint64(boxPosX)), encodeZigZag64(uint64(boxPosY)), winnerAddr, serializedBetBox, serializedOracleBox)
log.Debug("unsigned erg tx created",
zap.Int64("durationMs", time.Since(start).Milliseconds()),
zap.String("txUnsigned", string(txUnsigned)),
)
start = time.Now()
txSigned, err := s.ergNode.PostErgOracleTx(txUnsigned)
if err != nil {
log.Error("post erg tx failed", zap.Error(err), zap.Int64("durationMs", time.Since(start).Milliseconds()))
return fmt.Errorf("call to PostErgOracleTx failed - %s", err.Error())
}
log.Info("successfully sent tx to result smart contract", zap.Int64("durationMs", time.Since(start).Milliseconds()), zap.String("tx_id", string(txSigned)))
log.Debug("erg utxo box results",
zap.String("erg_utxo_box_id", box.BoxId),
zap.String("winner_addr", winnerAddr),
zap.Int("winner_amount", box.Assets[0].Amount),
zap.Int("random_number", randNum),
zap.Int("subgame", int(sg)),
zap.Int("chipspot", int(cs)),
)
// add tx id and winner address to the payout entry in redis
addons := make(map[string]interface{})
addons["txId"] = string(txSigned)
addons["winnerAddr"] = string(winnerAddr)
addons["settled"] = "true"
err = s.rdb.HSet(s.ctx, betKey, addons).Err()
if err != nil {
return fmt.Errorf("failed to set txId for key '%s' to redis db - %s", betKey, err)
}
// update NotifState with bet redis key if winner is not the house
if winnerAddr != houseAddress {
s.ns.AddNotConfirmed(betKey)
}
}
return nil
}
func buildResultSmartContractTx(betUtxo erg.ErgTxOutputNode, r4, r5 uint64, winnerAddress, betDataInput, oracleDataInput string) ([]byte, error) {
// Build Erg Tx for node to sign
var assets string
if len(betUtxo.Assets) > 0 {
// lenth of assets should only be 1 since we are only dealing with OWL tokens
assets = fmt.Sprintf(`[{"tokenId": "%s", "amount": %d}]`, betUtxo.Assets[0].TokenId, betUtxo.Assets[0].Amount)
}
txToSign := []byte(fmt.Sprintf(`{
"requests": [
{
"address": "%s",
"value": %d,
"assets": %s,
"registers": {
"R4": "04%02x",
"R5": "04%02x"
}
}
],
"fee": %d,
"inputsRaw": [
"%s"
],
"dataInputsRaw": [
"%s"
]
}`, winnerAddress, minBoxValue, assets, r4, r5, minerFee, betDataInput, oracleDataInput))
return txToSign, nil
}
func encodeZigZag64(n uint64) uint64 {
return (n << 1) ^ (n >> 63)
}
func decodeZigZag64(n uint64) uint64 {
return (n >> 1) ^ (-(n & 1))
}