-
Notifications
You must be signed in to change notification settings - Fork 0
/
txs.go
141 lines (119 loc) · 4.08 KB
/
txs.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
package emitter
import (
"time"
"github.com/Fantom-foundation/lachesis-base/common/bigendian"
"github.com/Fantom-foundation/lachesis-base/hash"
"github.com/Fantom-foundation/lachesis-base/inter/idx"
"github.com/Fantom-foundation/lachesis-base/inter/pos"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/Fantom-foundation/go-opera/eventcheck/gaspowercheck"
"github.com/Fantom-foundation/go-opera/inter"
"github.com/Fantom-foundation/go-opera/utils"
"github.com/Fantom-foundation/go-opera/utils/txtime"
)
const (
TxTurnPeriod = 8 * time.Second
TxTurnPeriodLatency = 1 * time.Second
TxTurnNonces = 32
)
func max64(a, b uint64) uint64 {
if a > b {
return a
}
return b
}
func (em *Emitter) maxGasPowerToUse(e *inter.MutableEventPayload) uint64 {
rules := em.world.GetRules()
maxGasToUse := rules.Economy.Gas.MaxEventGas + 10000000
if maxGasToUse > e.GasPowerLeft().Min() {
maxGasToUse = e.GasPowerLeft().Min()
}
if em.config.LimitedTpsThreshold > em.config.NoTxsThreshold {
upperThreshold := em.config.LimitedTpsThreshold
downThreshold := em.config.NoTxsThreshold
estimatedAlloc := gaspowercheck.CalcValidatorGasPower(e, e.CreationTime(), e.MedianTime(), 0, em.validators, gaspowercheck.Config{
Idx: inter.LongTermGas,
AllocPerSec: rules.Economy.LongGasPower.AllocPerSec * 4 / 5,
MaxAllocPeriod: inter.Timestamp(time.Minute) * 2,
MinEnsuredAlloc: 0,
StartupAllocPeriod: 0,
MinStartupGas: 0,
})
gasPowerLeft := e.GasPowerLeft().Min() + estimatedAlloc
if gasPowerLeft < downThreshold {
return 0
}
newGasPowerLeft := uint64(0)
if gasPowerLeft > maxGasToUse {
newGasPowerLeft = gasPowerLeft - maxGasToUse
}
var x1, x2 = newGasPowerLeft, gasPowerLeft
if x1 < downThreshold {
x1 = downThreshold
}
if x2 > upperThreshold {
x2 = upperThreshold
}
trespassingPart := uint64(0)
if x2 > x1 {
trespassingPart = x2 - x1
}
healthyPart := uint64(0)
if gasPowerLeft > x2 {
healthyPart = gasPowerLeft - x2
}
smoothGasToUse := healthyPart + trespassingPart/2
if maxGasToUse > smoothGasToUse {
maxGasToUse = smoothGasToUse
}
}
maxPendingGas := max64(max64(rules.Blocks.MaxBlockGas/3, rules.Economy.Gas.MaxEventGas), 15000000)
if maxPendingGas <= em.pendingGas {
return 0
}
if maxPendingGas < em.pendingGas+maxGasToUse {
maxGasToUse = maxPendingGas - em.pendingGas
}
threshold := em.config.NoTxsThreshold
if e.GasPowerLeft().Min() <= threshold {
return 0
} else if e.GasPowerLeft().Min() < threshold+maxGasToUse {
maxGasToUse = e.GasPowerLeft().Min() - threshold
}
return maxGasToUse
}
func getTxRoundIndex(now, txTime time.Time, validatorsNum idx.Validator) int {
passed := now.Sub(txTime)
if passed < 0 {
passed = 0
}
return int((passed / TxTurnPeriod) % time.Duration(validatorsNum))
}
// safe for concurrent use
func (em *Emitter) isMyTxTurn(txHash common.Hash, sender common.Address, accountNonce uint64, now time.Time, validators *pos.Validators, me idx.ValidatorID, epoch idx.Epoch) bool {
txTime := txtime.Of(txHash)
roundIndex := getTxRoundIndex(now, txTime, validators.Len())
if roundIndex != getTxRoundIndex(now.Add(TxTurnPeriodLatency), txTime, validators.Len()) {
// round is about to change, avoid originating the transaction to avoid racing with another validator
return false
}
roundsHash := hash.Of(sender.Bytes(), bigendian.Uint64ToBytes(accountNonce/TxTurnNonces), epoch.Bytes())
rounds := utils.WeightedPermutation(roundIndex+1, validators.SortedWeights(), roundsHash)
return validators.GetID(idx.Validator(rounds[roundIndex])) == me
}
// sort transactions by price and nonce
func (em *Emitter) addTxs(e *inter.MutableEventPayload, sorted *types.TransactionsByPriceAndNonce) {
maxGasUsed := em.maxGasPowerToUse(e)
if maxGasUsed <= e.GasPowerUsed() {
return
}
for tx := sorted.Peek(); tx != nil; tx = sorted.Peek() {
// ... (rest of the code)
// add
e.SetGasPowerUsed(e.GasPowerUsed() + tx.Gas())
e.SetGasPowerLeft(e.GasPowerLeft().Sub(tx.Gas()))
e.SetTxs(append(e.Txs(), tx))
sorted.Shift()
}
}