Skip to content

Commit e0e50d6

Browse files
committed
refactor validator into own module
1 parent d5a249f commit e0e50d6

File tree

11 files changed

+80
-71
lines changed

11 files changed

+80
-71
lines changed

access/ratelimit/limiter.go

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package ratelimit
2+
3+
import (
4+
"github.com/onflow/flow-go/model/flow"
5+
)
6+
7+
// RateLimiter is an interface for checking if an address is rate limited.
8+
// By convention, the address used is the payer field of a transaction.
9+
// This rate limiter is applied when a transaction is first received by a
10+
// node, meaning that if a transaction is rate-limited it will be dropped.
11+
type RateLimiter interface {
12+
// IsRateLimited returns true if the address is rate limited
13+
IsRateLimited(address flow.Address) bool
14+
}
15+
16+
type NoopLimiter struct{}
17+
18+
func NewNoopLimiter() *NoopLimiter {
19+
return &NoopLimiter{}
20+
}
21+
22+
func (l *NoopLimiter) IsRateLimited(address flow.Address) bool {
23+
return false
24+
}

access/errors.go access/validator/errors.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package access
1+
package validator
22

33
import (
44
"errors"

access/validator.go access/validator/validator.go

+5-23
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package access
1+
package validator
22

33
import (
44
"context"
@@ -13,6 +13,7 @@ import (
1313
"github.com/onflow/crypto"
1414
"github.com/onflow/flow-core-contracts/lib/go/templates"
1515

16+
"github.com/onflow/flow-go/access/ratelimit"
1617
cadenceutils "github.com/onflow/flow-go/access/utils"
1718
"github.com/onflow/flow-go/fvm"
1819
"github.com/onflow/flow-go/fvm/systemcontracts"
@@ -81,25 +82,6 @@ func (b *ProtocolStateBlocks) IndexedHeight() (uint64, error) {
8182
return 0, IndexReporterNotInitialized
8283
}
8384

84-
// RateLimiter is an interface for checking if an address is rate limited.
85-
// By convention, the address used is the payer field of a transaction.
86-
// This rate limiter is applied when a transaction is first received by a
87-
// node, meaning that if a transaction is rate-limited it will be dropped.
88-
type RateLimiter interface {
89-
// IsRateLimited returns true if the address is rate limited
90-
IsRateLimited(address flow.Address) bool
91-
}
92-
93-
type NoopLimiter struct{}
94-
95-
func NewNoopLimiter() *NoopLimiter {
96-
return &NoopLimiter{}
97-
}
98-
99-
func (l *NoopLimiter) IsRateLimited(address flow.Address) bool {
100-
return false
101-
}
102-
10385
// PayerBalanceMode represents the mode for checking the payer's balance
10486
// when validating transactions. It controls whether and how the balance
10587
// check is performed during transaction validation.
@@ -175,7 +157,7 @@ type TransactionValidator struct {
175157
chain flow.Chain // for checking validity of addresses
176158
options TransactionValidationOptions
177159
serviceAccountAddress flow.Address
178-
limiter RateLimiter
160+
limiter ratelimit.RateLimiter
179161
scriptExecutor execution.ScriptExecutor
180162
verifyPayerBalanceScript []byte
181163
transactionValidationMetrics module.TransactionValidationMetrics
@@ -201,7 +183,7 @@ func NewTransactionValidator(
201183
chain: chain,
202184
options: options,
203185
serviceAccountAddress: chain.ServiceAddress(),
204-
limiter: NewNoopLimiter(),
186+
limiter: ratelimit.NewNoopLimiter(),
205187
scriptExecutor: executor,
206188
verifyPayerBalanceScript: templates.GenerateVerifyPayerBalanceForTxExecution(env),
207189
transactionValidationMetrics: transactionValidationMetrics,
@@ -217,7 +199,7 @@ func NewTransactionValidatorWithLimiter(
217199
chain flow.Chain,
218200
options TransactionValidationOptions,
219201
transactionValidationMetrics module.TransactionValidationMetrics,
220-
rateLimiter RateLimiter,
202+
rateLimiter ratelimit.RateLimiter,
221203
) *TransactionValidator {
222204
txValidator := &TransactionValidator{
223205
blocks: blocks,

access/validator_test.go access/validator/validator_test.go

+11-11
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package access_test
1+
package validator_test
22

33
import (
44
"context"
@@ -13,8 +13,8 @@ import (
1313

1414
jsoncdc "github.com/onflow/cadence/encoding/json"
1515

16-
"github.com/onflow/flow-go/access"
1716
accessmock "github.com/onflow/flow-go/access/mock"
17+
"github.com/onflow/flow-go/access/validator"
1818
"github.com/onflow/flow-go/fvm"
1919
"github.com/onflow/flow-go/model/flow"
2020
"github.com/onflow/flow-go/module"
@@ -32,7 +32,7 @@ type TransactionValidatorSuite struct {
3232
blocks *accessmock.Blocks
3333
header *flow.Header
3434
chain flow.Chain
35-
validatorOptions access.TransactionValidationOptions
35+
validatorOptions validator.TransactionValidationOptions
3636
metrics module.TransactionValidationMetrics
3737
}
3838

@@ -57,8 +57,8 @@ func (s *TransactionValidatorSuite) SetupTest() {
5757
Return(s.header, nil)
5858

5959
s.chain = flow.Testnet.Chain()
60-
s.validatorOptions = access.TransactionValidationOptions{
61-
CheckPayerBalanceMode: access.EnforceCheck,
60+
s.validatorOptions = validator.TransactionValidationOptions{
61+
CheckPayerBalanceMode: validator.EnforceCheck,
6262
MaxTransactionByteSize: flow.DefaultMaxTransactionByteSize,
6363
MaxCollectionByteSize: flow.DefaultMaxCollectionByteSize,
6464
}
@@ -97,7 +97,7 @@ func (s *TransactionValidatorSuite) TestTransactionValidator_ScriptExecutorInter
9797
Return(nil, errors.New("script executor internal error")).
9898
Once()
9999

100-
validator, err := access.NewTransactionValidator(s.blocks, s.chain, s.metrics, s.validatorOptions, scriptExecutor)
100+
validator, err := validator.NewTransactionValidator(s.blocks, s.chain, s.metrics, s.validatorOptions, scriptExecutor)
101101
assert.NoError(s.T(), err)
102102
assert.NotNil(s.T(), validator)
103103

@@ -128,7 +128,7 @@ func (s *TransactionValidatorSuite) TestTransactionValidator_SufficientBalance()
128128
Return(actualResponse, nil).
129129
Once()
130130

131-
validator, err := access.NewTransactionValidator(s.blocks, s.chain, s.metrics, s.validatorOptions, scriptExecutor)
131+
validator, err := validator.NewTransactionValidator(s.blocks, s.chain, s.metrics, s.validatorOptions, scriptExecutor)
132132
assert.NoError(s.T(), err)
133133
assert.NotNil(s.T(), validator)
134134

@@ -164,7 +164,7 @@ func (s *TransactionValidatorSuite) TestTransactionValidator_InsufficientBalance
164164

165165
validateTx := func() error {
166166
txBody := unittest.TransactionBodyFixture()
167-
validator, err := access.NewTransactionValidator(s.blocks, s.chain, s.metrics, s.validatorOptions, scriptExecutor)
167+
validator, err := validator.NewTransactionValidator(s.blocks, s.chain, s.metrics, s.validatorOptions, scriptExecutor)
168168
assert.NoError(s.T(), err)
169169
assert.NotNil(s.T(), validator)
170170

@@ -174,15 +174,15 @@ func (s *TransactionValidatorSuite) TestTransactionValidator_InsufficientBalance
174174
s.Run("with enforce check", func() {
175175
err := validateTx()
176176

177-
expectedError := access.InsufficientBalanceError{
177+
expectedError := validator.InsufficientBalanceError{
178178
Payer: unittest.AddressFixture(),
179179
RequiredBalance: requiredBalance,
180180
}
181181
assert.ErrorIs(s.T(), err, expectedError)
182182
})
183183

184184
s.Run("with warn check", func() {
185-
s.validatorOptions.CheckPayerBalanceMode = access.WarnCheck
185+
s.validatorOptions.CheckPayerBalanceMode = validator.WarnCheck
186186
err := validateTx()
187187
assert.NoError(s.T(), err)
188188
})
@@ -198,7 +198,7 @@ func (s *TransactionValidatorSuite) TestTransactionValidator_SealedIndexedHeight
198198
On("IndexedHeight").
199199
Return(indexedHeight, nil)
200200

201-
validator, err := access.NewTransactionValidator(s.blocks, s.chain, s.metrics, s.validatorOptions, scriptExecutor)
201+
validator, err := validator.NewTransactionValidator(s.blocks, s.chain, s.metrics, s.validatorOptions, scriptExecutor)
202202
assert.NoError(s.T(), err)
203203
assert.NotNil(s.T(), validator)
204204

cmd/access/node_builder/access_node_builder.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ import (
2727
"google.golang.org/grpc/credentials"
2828
"google.golang.org/grpc/credentials/insecure"
2929

30-
accessNode "github.com/onflow/flow-go/access"
30+
txvalidator "github.com/onflow/flow-go/access/validator"
3131
"github.com/onflow/flow-go/admin/commands"
3232
stateSyncCommands "github.com/onflow/flow-go/admin/commands/state_synchronization"
3333
storageCommands "github.com/onflow/flow-go/admin/commands/storage"
@@ -290,7 +290,7 @@ func DefaultAccessNodeConfig() *AccessNodeConfig {
290290
registerCacheType: pstorage.CacheTypeTwoQueue.String(),
291291
registerCacheSize: 0,
292292
programCacheSize: 0,
293-
checkPayerBalanceMode: accessNode.Disabled.String(),
293+
checkPayerBalanceMode: txvalidator.Disabled.String(),
294294
versionControlEnabled: true,
295295
storeTxResultErrorMessages: false,
296296
stopControlEnabled: false,
@@ -1538,7 +1538,7 @@ func (builder *FlowAccessNodeBuilder) extraFlags() {
15381538
}
15391539
}
15401540

1541-
if builder.checkPayerBalanceMode != accessNode.Disabled.String() && !builder.executionDataIndexingEnabled {
1541+
if builder.checkPayerBalanceMode != txvalidator.Disabled.String() && !builder.executionDataIndexingEnabled {
15421542
return errors.New("execution-data-indexing-enabled must be set if check-payer-balance is enabled")
15431543
}
15441544

@@ -2004,7 +2004,7 @@ func (builder *FlowAccessNodeBuilder) Build() (cmd.Node, error) {
20042004
indexReporter = builder.Reporter
20052005
}
20062006

2007-
checkPayerBalanceMode, err := accessNode.ParsePayerBalanceMode(builder.checkPayerBalanceMode)
2007+
checkPayerBalanceMode, err := txvalidator.ParsePayerBalanceMode(builder.checkPayerBalanceMode)
20082008
if err != nil {
20092009
return nil, fmt.Errorf("could not parse payer balance mode: %w", err)
20102010

engine/access/rpc/backend/backend.go

+7-6
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"github.com/rs/zerolog"
1313

1414
"github.com/onflow/flow-go/access"
15+
"github.com/onflow/flow-go/access/validator"
1516
"github.com/onflow/flow-go/cmd/build"
1617
"github.com/onflow/flow-go/engine/access/index"
1718
"github.com/onflow/flow-go/engine/access/rpc/connection"
@@ -100,7 +101,7 @@ type Params struct {
100101
TxResultCacheSize uint
101102
ScriptExecutor execution.ScriptExecutor
102103
ScriptExecutionMode IndexQueryMode
103-
CheckPayerBalanceMode access.PayerBalanceMode
104+
CheckPayerBalanceMode validator.PayerBalanceMode
104105
EventQueryMode IndexQueryMode
105106
BlockTracker subscription.BlockTracker
106107
SubscriptionHandler *subscription.SubscriptionHandler
@@ -274,13 +275,13 @@ func configureTransactionValidator(
274275
indexReporter state_synchronization.IndexReporter,
275276
transactionMetrics module.TransactionValidationMetrics,
276277
executor execution.ScriptExecutor,
277-
checkPayerBalanceMode access.PayerBalanceMode,
278-
) (*access.TransactionValidator, error) {
279-
return access.NewTransactionValidator(
280-
access.NewProtocolStateBlocks(state, indexReporter),
278+
checkPayerBalanceMode validator.PayerBalanceMode,
279+
) (*validator.TransactionValidator, error) {
280+
return validator.NewTransactionValidator(
281+
validator.NewProtocolStateBlocks(state, indexReporter),
281282
chainID.Chain(),
282283
transactionMetrics,
283-
access.TransactionValidationOptions{
284+
validator.TransactionValidationOptions{
284285
Expiry: flow.DefaultTransactionExpiry,
285286
ExpiryBuffer: flow.DefaultTransactionExpiryBuffer,
286287
AllowEmptyReferenceBlockID: false,

engine/access/rpc/backend/backend_transactions.go

+4-3
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
"google.golang.org/grpc/status"
1616

1717
"github.com/onflow/flow-go/access"
18+
"github.com/onflow/flow-go/access/validator"
1819
"github.com/onflow/flow-go/engine/access/rpc/connection"
1920
"github.com/onflow/flow-go/engine/common/rpc"
2021
commonrpc "github.com/onflow/flow-go/engine/common/rpc"
@@ -38,7 +39,7 @@ type backendTransactions struct {
3839
txResultErrorMessages storage.TransactionResultErrorMessages
3940
chainID flow.ChainID
4041
transactionMetrics module.TransactionMetrics
41-
transactionValidator *access.TransactionValidator
42+
transactionValidator *validator.TransactionValidator
4243
retry *Retry
4344
connFactory connection.ConnectionFactory
4445

@@ -352,8 +353,8 @@ func (b *backendTransactions) GetTransactionResult(
352353
// retrieveBlock function returns a block based on the input argument. The block ID lookup has the highest priority,
353354
// followed by the collection ID lookup. If both are missing, the default lookup by transaction ID is performed.
354355
func (b *backendTransactions) retrieveBlock(
355-
// the requested block or collection was not found. If looking up the block based solely on the txID returns
356-
// not found, then no error is returned.
356+
// the requested block or collection was not found. If looking up the block based solely on the txID returns
357+
// not found, then no error is returned.
357358
blockID flow.Identifier,
358359
collectionID flow.Identifier,
359360
txID flow.Identifier,

engine/collection/ingest/engine.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import (
99

1010
"github.com/rs/zerolog"
1111

12-
"github.com/onflow/flow-go/access"
12+
"github.com/onflow/flow-go/access/validator"
1313
"github.com/onflow/flow-go/engine"
1414
"github.com/onflow/flow-go/engine/common/fifoqueue"
1515
"github.com/onflow/flow-go/model/flow"
@@ -38,7 +38,7 @@ type Engine struct {
3838
pendingTransactions engine.MessageStore
3939
messageHandler *engine.MessageHandler
4040
pools *epochs.TransactionPools
41-
transactionValidator *access.TransactionValidator
41+
transactionValidator *validator.TransactionValidator
4242

4343
config Config
4444
}
@@ -60,10 +60,10 @@ func New(
6060

6161
logger := log.With().Str("engine", "ingest").Logger()
6262

63-
transactionValidator := access.NewTransactionValidatorWithLimiter(
64-
access.NewProtocolStateBlocks(state, nil),
63+
transactionValidator := validator.NewTransactionValidatorWithLimiter(
64+
validator.NewProtocolStateBlocks(state, nil),
6565
chain,
66-
access.TransactionValidationOptions{
66+
validator.TransactionValidationOptions{
6767
Expiry: flow.DefaultTransactionExpiry,
6868
ExpiryBuffer: config.ExpiryBuffer,
6969
MaxGasLimit: config.MaxGasLimit,

0 commit comments

Comments
 (0)