-
Notifications
You must be signed in to change notification settings - Fork 0
/
wrappedTransaction.go
72 lines (54 loc) · 2.37 KB
/
wrappedTransaction.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
package txcache
import (
"bytes"
"github.com/bhagyaraj1208117/andes-core-go/data"
)
const processFeeFactor = float64(0.8) // 80%
// WrappedTransaction contains a transaction, its hash and extra information
type WrappedTransaction struct {
Tx data.TransactionHandler
TxHash []byte
SenderShardID uint32
ReceiverShardID uint32
Size int64
TxFeeScoreNormalized uint64
}
func (wrappedTx *WrappedTransaction) sameAs(another *WrappedTransaction) bool {
return bytes.Equal(wrappedTx.TxHash, another.TxHash)
}
// estimateTxGas returns an approximation for the necessary computation units (gas units)
func estimateTxGas(tx *WrappedTransaction) uint64 {
gasLimit := tx.Tx.GetGasLimit()
return gasLimit
}
// estimateTxFeeScore returns a normalized approximation for the cost of a transaction
func estimateTxFeeScore(tx *WrappedTransaction, txGasHandler TxGasHandler, txFeeHelper feeHelper) uint64 {
moveGas, processGas := txGasHandler.SplitTxGasInCategories(tx.Tx)
normalizedMoveGas := moveGas >> txFeeHelper.gasLimitShift()
normalizedProcessGas := processGas >> txFeeHelper.gasLimitShift()
normalizedGasPriceMove := txGasHandler.GasPriceForMove(tx.Tx) >> txFeeHelper.gasPriceShift()
normalizedGasPriceProcess := normalizeGasPriceProcessing(tx, txGasHandler, txFeeHelper)
normalizedFeeMove := normalizedMoveGas * normalizedGasPriceMove
normalizedFeeProcess := normalizedProcessGas * normalizedGasPriceProcess
adjustmentFactor := computeProcessingGasPriceAdjustment(tx, txGasHandler, txFeeHelper)
tx.TxFeeScoreNormalized = normalizedFeeMove + normalizedFeeProcess*adjustmentFactor
return tx.TxFeeScoreNormalized
}
func normalizeGasPriceProcessing(tx *WrappedTransaction, txGasHandler TxGasHandler, txFeeHelper feeHelper) uint64 {
return txGasHandler.GasPriceForProcessing(tx.Tx) >> txFeeHelper.gasPriceShift()
}
func computeProcessingGasPriceAdjustment(
tx *WrappedTransaction,
txGasHandler TxGasHandler,
txFeeHelper feeHelper,
) uint64 {
minPriceFactor := txFeeHelper.minGasPriceFactor()
if minPriceFactor <= 2 {
return 1
}
actualPriceFactor := float64(1)
if txGasHandler.MinGasPriceForProcessing() != 0 {
actualPriceFactor = float64(txGasHandler.GasPriceForProcessing(tx.Tx)) / float64(txGasHandler.MinGasPriceForProcessing())
}
return uint64(float64(txFeeHelper.minGasPriceFactor()) * processFeeFactor / actualPriceFactor)
}