forked from 0xPolygonHermez/zkevm-node
-
Notifications
You must be signed in to change notification settings - Fork 0
/
profitabilitychecker.go
91 lines (78 loc) · 2.89 KB
/
profitabilitychecker.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
package aggregator
import (
"context"
"fmt"
"math/big"
"time"
)
// TxProfitabilityCheckerType checks profitability of batch validation
type TxProfitabilityCheckerType string
const (
// ProfitabilityBase checks matic collateral with min reward
ProfitabilityBase = "base"
// ProfitabilityAcceptAll validate batch anyway and don't check anything
ProfitabilityAcceptAll = "acceptall"
)
// TxProfitabilityCheckerBase checks matic collateral with min reward
type TxProfitabilityCheckerBase struct {
State stateInterface
IntervalAfterWhichBatchSentAnyway time.Duration
MinReward *big.Int
}
// NewTxProfitabilityCheckerBase init base tx profitability checker
func NewTxProfitabilityCheckerBase(state stateInterface, interval time.Duration, minReward *big.Int) *TxProfitabilityCheckerBase {
return &TxProfitabilityCheckerBase{
State: state,
IntervalAfterWhichBatchSentAnyway: interval,
MinReward: minReward,
}
}
// IsProfitable checks matic collateral with min reward
func (pc *TxProfitabilityCheckerBase) IsProfitable(ctx context.Context, maticCollateral *big.Int) (bool, error) {
if pc.IntervalAfterWhichBatchSentAnyway != 0 {
ok, err := isConsolidatedBatchAppeared(ctx, pc.State, pc.IntervalAfterWhichBatchSentAnyway)
if err != nil {
return false, err
}
if ok {
return true, nil
}
}
return maticCollateral.Cmp(pc.MinReward) >= 0, nil
}
// TxProfitabilityCheckerAcceptAll validate batch anyway and don't check anything
type TxProfitabilityCheckerAcceptAll struct {
State stateInterface
IntervalAfterWhichBatchSentAnyway time.Duration
}
// NewTxProfitabilityCheckerAcceptAll init tx profitability checker that accept all txs
func NewTxProfitabilityCheckerAcceptAll(state stateInterface, interval time.Duration) *TxProfitabilityCheckerAcceptAll {
return &TxProfitabilityCheckerAcceptAll{
State: state,
IntervalAfterWhichBatchSentAnyway: interval,
}
}
// IsProfitable validate batch anyway and don't check anything
func (pc *TxProfitabilityCheckerAcceptAll) IsProfitable(ctx context.Context, maticCollateral *big.Int) (bool, error) {
if pc.IntervalAfterWhichBatchSentAnyway != 0 {
ok, err := isConsolidatedBatchAppeared(ctx, pc.State, pc.IntervalAfterWhichBatchSentAnyway)
if err != nil {
return false, err
}
if ok {
return true, nil
}
}
return true, nil
}
func isConsolidatedBatchAppeared(ctx context.Context, state stateInterface, intervalAfterWhichBatchConsolidatedAnyway time.Duration) (bool, error) {
batch, err := state.GetLastBatch(ctx, false, "")
if err != nil {
return false, fmt.Errorf("failed to get last consolidated batch, err: %v", err)
}
interval := intervalAfterWhichBatchConsolidatedAnyway * time.Minute
if batch.ConsolidatedAt.Before(time.Now().Add(-interval)) {
return true, nil
}
return false, err
}