-
Notifications
You must be signed in to change notification settings - Fork 199
/
txValidator.go
100 lines (85 loc) · 2.74 KB
/
txValidator.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
package dataValidators
import (
"encoding/hex"
"fmt"
"github.com/ElrondNetwork/elrond-go/core/logger"
"github.com/ElrondNetwork/elrond-go/data/state"
"github.com/ElrondNetwork/elrond-go/process"
"github.com/ElrondNetwork/elrond-go/sharding"
)
var log = logger.DefaultLogger()
// txValidator represents a tx handler validator that doesn't check the validity of provided txHandler
type txValidator struct {
accounts state.AccountsAdapter
shardCoordinator sharding.Coordinator
rejectedTxs uint64
maxNonceDeltaAllowed int
}
// NewTxValidator creates a new nil tx handler validator instance
func NewTxValidator(
accounts state.AccountsAdapter,
shardCoordinator sharding.Coordinator,
maxNonceDeltaAllowed int,
) (*txValidator, error) {
if accounts == nil || accounts.IsInterfaceNil() {
return nil, process.ErrNilAccountsAdapter
}
if shardCoordinator == nil || shardCoordinator.IsInterfaceNil() {
return nil, process.ErrNilShardCoordinator
}
return &txValidator{
accounts: accounts,
shardCoordinator: shardCoordinator,
rejectedTxs: uint64(0),
maxNonceDeltaAllowed: maxNonceDeltaAllowed,
}, nil
}
// IsTxValidForProcessing will filter transactions that needs to be added in pools
func (tv *txValidator) IsTxValidForProcessing(interceptedTx process.TxValidatorHandler) bool {
shardId := tv.shardCoordinator.SelfId()
txShardId := interceptedTx.SenderShardId()
senderIsInAnotherShard := shardId != txShardId
if senderIsInAnotherShard {
return true
}
sndAddr := interceptedTx.SenderAddress()
accountHandler, err := tv.accounts.GetExistingAccount(sndAddr)
if err != nil {
log.Debug(fmt.Sprintf("Transaction's sender address %s does not exist in current shard %d", sndAddr, shardId))
tv.rejectedTxs++
return false
}
accountNonce := accountHandler.GetNonce()
txNonce := interceptedTx.Nonce()
lowerNonceInTx := txNonce < accountNonce
veryHighNonceInTx := txNonce > accountNonce+uint64(tv.maxNonceDeltaAllowed)
isTxRejected := lowerNonceInTx || veryHighNonceInTx
if isTxRejected {
tv.rejectedTxs++
return false
}
account, ok := accountHandler.(*state.Account)
if !ok {
hexSenderAddr := hex.EncodeToString(sndAddr.Bytes())
log.Error(fmt.Sprintf("Cannot convert account handler in a state.Account %s", hexSenderAddr))
return false
}
accountBalance := account.Balance
txTotalValue := interceptedTx.TotalValue()
if accountBalance.Cmp(txTotalValue) < 0 {
tv.rejectedTxs++
return false
}
return true
}
// NumRejectedTxs will return number of rejected transaction
func (tv *txValidator) NumRejectedTxs() uint64 {
return tv.rejectedTxs
}
// IsInterfaceNil returns true if there is no value under the interface
func (tv *txValidator) IsInterfaceNil() bool {
if tv == nil {
return true
}
return false
}