-
Notifications
You must be signed in to change notification settings - Fork 199
/
feeHandler.go
110 lines (91 loc) · 2.58 KB
/
feeHandler.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
package postprocess
import (
"math/big"
"sync"
"github.com/ElrondNetwork/elrond-go/process"
)
var _ process.TransactionFeeHandler = (*feeHandler)(nil)
type feeData struct {
cost *big.Int
devFee *big.Int
}
var zero = big.NewInt(0)
type feeHandler struct {
mut sync.RWMutex
mapHashFee map[string]*feeData
accumulatedFees *big.Int
developerFees *big.Int
}
// NewFeeAccumulator constructor for the fee accumulator
func NewFeeAccumulator() (*feeHandler, error) {
f := &feeHandler{}
f.accumulatedFees = big.NewInt(0)
f.developerFees = big.NewInt(0)
f.mapHashFee = make(map[string]*feeData)
return f, nil
}
// CreateBlockStarted does the cleanup before creating a new block
func (f *feeHandler) CreateBlockStarted() {
f.mut.Lock()
f.mapHashFee = make(map[string]*feeData)
f.accumulatedFees = big.NewInt(0)
f.developerFees = big.NewInt(0)
f.mut.Unlock()
}
// GetAccumulatedFees returns the total accumulated fees
func (f *feeHandler) GetAccumulatedFees() *big.Int {
f.mut.RLock()
accumulatedFees := big.NewInt(0).Set(f.accumulatedFees)
f.mut.RUnlock()
return accumulatedFees
}
// GetDeveloperFees returns the total accumulated developer fees
func (f *feeHandler) GetDeveloperFees() *big.Int {
f.mut.RLock()
developerFees := f.developerFees
f.mut.RUnlock()
return developerFees
}
// ProcessTransactionFee adds the tx cost to the accumulated amount
func (f *feeHandler) ProcessTransactionFee(cost *big.Int, devFee *big.Int, txHash []byte) {
if cost == nil {
log.Error("nil cost in ProcessTransactionFee", "error", process.ErrNilValue.Error())
return
}
if cost.Cmp(zero) < 0 {
log.Error("negative cost in ProcessTransactionFee", "error", process.ErrNegativeValue.Error())
return
}
f.mut.Lock()
fee, ok := f.mapHashFee[string(txHash)]
if !ok {
fee = &feeData{
cost: big.NewInt(0),
devFee: big.NewInt(0),
}
}
fee.cost.Add(fee.cost, cost)
fee.devFee.Add(fee.devFee, devFee)
f.mapHashFee[string(txHash)] = fee
f.accumulatedFees.Add(f.accumulatedFees, cost)
f.developerFees.Add(f.developerFees, devFee)
f.mut.Unlock()
}
// RevertFees reverts the accumulated fees for txHashes
func (f *feeHandler) RevertFees(txHashes [][]byte) {
f.mut.Lock()
defer f.mut.Unlock()
for _, txHash := range txHashes {
fee, ok := f.mapHashFee[string(txHash)]
if !ok {
continue
}
f.developerFees.Sub(f.developerFees, fee.devFee)
f.accumulatedFees.Sub(f.accumulatedFees, fee.cost)
delete(f.mapHashFee, string(txHash))
}
}
// IsInterfaceNil returns true if there is no value under the interface
func (f *feeHandler) IsInterfaceNil() bool {
return f == nil
}