-
Notifications
You must be signed in to change notification settings - Fork 199
/
gasTracker.go
88 lines (74 loc) · 2.62 KB
/
gasTracker.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
package preprocess
import (
"github.com/multiversx/mx-chain-core-go/core"
"github.com/multiversx/mx-chain-core-go/data"
"github.com/multiversx/mx-chain-go/process"
"github.com/multiversx/mx-chain-go/sharding"
)
type gasTracker struct {
shardCoordinator sharding.Coordinator
economicsFee process.FeeHandler
gasHandler process.GasHandler
}
func (gt *gasTracker) computeGasProvided(
senderShardId uint32,
receiverShardId uint32,
tx data.TransactionHandler,
txHash []byte,
gasInfo *gasConsumedInfo,
) (uint64, error) {
gasProvidedByTxInSenderShard, gasProvidedByTxInReceiverShard, err := gt.computeGasProvidedByTx(
senderShardId,
receiverShardId,
tx,
txHash)
if err != nil {
return 0, err
}
gasProvidedByTxInSelfShard := uint64(0)
if gt.shardCoordinator.SelfId() == senderShardId {
gasProvidedByTxInSelfShard = gasProvidedByTxInSenderShard
if gasProvidedByTxInReceiverShard > gt.economicsFee.MaxGasLimitPerTx() {
return 0, process.ErrMaxGasLimitPerOneTxInReceiverShardIsReached
}
if gasInfo.gasConsumedByMiniBlockInReceiverShard+gasProvidedByTxInReceiverShard > gt.economicsFee.MaxGasLimitPerBlockForSafeCrossShard() {
return 0, process.ErrMaxGasLimitPerMiniBlockInReceiverShardIsReached
}
} else {
gasProvidedByTxInSelfShard = gasProvidedByTxInReceiverShard
}
if gasInfo.totalGasConsumedInSelfShard+gasProvidedByTxInSelfShard > gt.economicsFee.MaxGasLimitPerBlock(gt.shardCoordinator.SelfId()) {
return 0, process.ErrMaxGasLimitPerBlockInSelfShardIsReached
}
gasInfo.gasConsumedByMiniBlocksInSenderShard += gasProvidedByTxInSenderShard
gasInfo.gasConsumedByMiniBlockInReceiverShard += gasProvidedByTxInReceiverShard
gasInfo.totalGasConsumedInSelfShard += gasProvidedByTxInSelfShard
return gasProvidedByTxInSelfShard, nil
}
func (gt *gasTracker) computeGasProvidedByTx(
senderShardId uint32,
receiverShardId uint32,
tx data.TransactionHandler,
txHash []byte,
) (uint64, uint64, error) {
txGasLimitInSenderShard, txGasLimitInReceiverShard, err := gt.gasHandler.ComputeGasProvidedByTx(
senderShardId,
receiverShardId,
tx)
if err != nil {
return 0, 0, err
}
if core.IsSmartContractAddress(tx.GetRcvAddr()) {
txGasRefunded := gt.gasHandler.GasRefunded(txHash)
txGasPenalized := gt.gasHandler.GasPenalized(txHash)
txGasToBeSubtracted := txGasRefunded + txGasPenalized
if txGasLimitInReceiverShard < txGasToBeSubtracted {
return 0, 0, process.ErrInsufficientGasLimitInTx
}
if senderShardId == receiverShardId {
txGasLimitInSenderShard -= txGasToBeSubtracted
txGasLimitInReceiverShard -= txGasToBeSubtracted
}
}
return txGasLimitInSenderShard, txGasLimitInReceiverShard, nil
}