-
Notifications
You must be signed in to change notification settings - Fork 199
/
txValidator.go
120 lines (105 loc) · 3.38 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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
package dataValidators
import (
"fmt"
"github.com/ElrondNetwork/elrond-go/core/check"
"github.com/ElrondNetwork/elrond-go/data/state"
"github.com/ElrondNetwork/elrond-go/process"
"github.com/ElrondNetwork/elrond-go/sharding"
)
var _ process.TxValidator = (*txValidator)(nil)
// txValidator represents a tx handler validator that doesn't check the validity of provided txHandler
type txValidator struct {
accounts state.AccountsAdapter
shardCoordinator sharding.Coordinator
whiteListHandler process.WhiteListHandler
pubkeyConverter state.PubkeyConverter
maxNonceDeltaAllowed int
}
// NewTxValidator creates a new nil tx handler validator instance
func NewTxValidator(
accounts state.AccountsAdapter,
shardCoordinator sharding.Coordinator,
whiteListHandler process.WhiteListHandler,
pubkeyConverter state.PubkeyConverter,
maxNonceDeltaAllowed int,
) (*txValidator, error) {
if check.IfNil(accounts) {
return nil, process.ErrNilAccountsAdapter
}
if check.IfNil(shardCoordinator) {
return nil, process.ErrNilShardCoordinator
}
if check.IfNil(whiteListHandler) {
return nil, process.ErrNilWhiteListHandler
}
if check.IfNil(pubkeyConverter) {
return nil, fmt.Errorf("%w in NewTxValidator", process.ErrNilPubkeyConverter)
}
return &txValidator{
accounts: accounts,
shardCoordinator: shardCoordinator,
whiteListHandler: whiteListHandler,
maxNonceDeltaAllowed: maxNonceDeltaAllowed,
pubkeyConverter: pubkeyConverter,
}, nil
}
// CheckTxValidity will filter transactions that needs to be added in pools
func (txv *txValidator) CheckTxValidity(interceptedTx process.TxValidatorHandler) error {
// TODO: Refactor, extract methods.
interceptedData, ok := interceptedTx.(process.InterceptedData)
if ok {
if txv.whiteListHandler.IsWhiteListed(interceptedData) {
return nil
}
}
shardID := txv.shardCoordinator.SelfId()
txShardID := interceptedTx.SenderShardId()
senderIsInAnotherShard := shardID != txShardID
if senderIsInAnotherShard {
return nil
}
senderAddress := interceptedTx.SenderAddress()
accountHandler, err := txv.accounts.GetExistingAccount(senderAddress)
if err != nil {
return fmt.Errorf("%w for address %s and shard %d, err: %s",
process.ErrAccountNotFound,
txv.pubkeyConverter.Encode(senderAddress),
shardID,
err.Error(),
)
}
accountNonce := accountHandler.GetNonce()
txNonce := interceptedTx.Nonce()
lowerNonceInTx := txNonce < accountNonce
veryHighNonceInTx := txNonce > accountNonce+uint64(txv.maxNonceDeltaAllowed)
isTxRejected := lowerNonceInTx || veryHighNonceInTx
if isTxRejected {
return fmt.Errorf("%w lowerNonceInTx: %v, veryHighNonceInTx: %v",
process.ErrWrongTransaction,
lowerNonceInTx,
veryHighNonceInTx,
)
}
account, ok := accountHandler.(state.UserAccountHandler)
if !ok {
return fmt.Errorf("%w, account is not of type *state.Account, address: %s",
process.ErrWrongTypeAssertion,
txv.pubkeyConverter.Encode(senderAddress),
)
}
accountBalance := account.GetBalance()
txFee := interceptedTx.Fee()
if accountBalance.Cmp(txFee) < 0 {
return fmt.Errorf("%w, for address: %s, wanted %v, have %v",
process.ErrInsufficientFunds,
txv.pubkeyConverter.Encode(senderAddress),
txFee,
accountBalance,
)
}
return nil
}
// IsInterfaceNil returns true if there is no value under the interface
func (txv *txValidator) IsInterfaceNil() bool {
return txv == nil
}