Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Access] Add check if tx payer has sufficient amount of flow to pay fee #6004

Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
Show all changes
44 commits
Select commit Hold shift + click to select a range
7124cb4
Add check if tx payer has sufficient amount of flow to pay fee
illia-malachyn May 30, 2024
f34cc31
add skeleton code
illia-malachyn Jun 5, 2024
47f50da
skeleton code with extending VM interface
illia-malachyn Jun 5, 2024
5d0a37e
add env
illia-malachyn Jun 12, 2024
b72cbb8
Extend validator with check if payer has balance to pay for tx
illia-malachyn Jun 12, 2024
6835088
Add option to enable check if tx payer has sufficient balance
illia-malachyn Jun 17, 2024
b344d98
add option to enable payer balance check
illia-malachyn Jun 18, 2024
f45ca34
look up sealed header to execute script on
illia-malachyn Jun 18, 2024
fd820b0
Merge branch 'master' into illia-malachyn/5823-payer-has-enough-flow-…
illia-malachyn Jun 18, 2024
83ff795
fix tx validator creation
illia-malachyn Jun 18, 2024
577e146
set checkPayerBalance default value to false
illia-malachyn Jun 18, 2024
8ba0b63
remove unnecessary ctx passing for validator in CN ingestion engine
illia-malachyn Jun 19, 2024
cfbfd3c
replace templates in insecure package
illia-malachyn Jun 19, 2024
3ec0447
replace templates in integration/ folder
illia-malachyn Jun 19, 2024
3253784
add integration test skeleton
illia-malachyn Jun 20, 2024
fbdcf0e
update flow-core-contracts commit to latest
illia-malachyn Jun 24, 2024
cd3633a
Merge branch 'master' into illia-malachyn/5823-payer-has-enough-flow-…
illia-malachyn Jun 24, 2024
4863c42
add code to pay for tx
illia-malachyn Jun 27, 2024
47c5d8c
replace integration test with simple unit test
illia-malachyn Jul 2, 2024
6f39b25
Merge branch 'master' into illia-malachyn/5823-payer-has-enough-flow-…
illia-malachyn Jul 3, 2024
5c67d99
fix args types for script in tx validator
illia-malachyn Jul 4, 2024
f34875b
remove mock for access/blocks
illia-malachyn Jul 4, 2024
5960ea2
fix integration's go.mod and account creatation fees
illia-malachyn Jul 4, 2024
140009d
remove tx fees in localnet
illia-malachyn Jul 4, 2024
09ab561
move cadence utils to access directory
illia-malachyn Jul 4, 2024
5509392
return mock blocks as it is used in unit test
illia-malachyn Jul 4, 2024
3f0e13f
run goimports
illia-malachyn Jul 5, 2024
6619460
replace emulator dep
illia-malachyn Jul 5, 2024
8cbb685
add unit tests for balance check
illia-malachyn Jul 8, 2024
6e57f41
remove reduntant space
illia-malachyn Jul 8, 2024
b8a98c2
Merge branch 'master' into illia-malachyn/5823-payer-has-enough-flow-…
illia-malachyn Jul 8, 2024
8af686f
run goimport
illia-malachyn Jul 8, 2024
3e56296
remove querying of payer actual balance
illia-malachyn Jul 11, 2024
4442af2
Merge branch 'master' into illia-malachyn/5823-payer-has-enough-flow-…
illia-malachyn Jul 11, 2024
891c3ec
run go mod tidy
illia-malachyn Jul 11, 2024
807d5f3
update core contracts version
illia-malachyn Jul 16, 2024
817db93
Merge branch 'master' into illia-malachyn/5823-payer-has-enough-flow-…
illia-malachyn Jul 16, 2024
5866b5c
update replace statement with correct version
illia-malachyn Jul 16, 2024
f7d331c
update flow-emulator replace
illia-malachyn Jul 16, 2024
5b4abda
update flow-emulator replace
illia-malachyn Jul 16, 2024
58850e2
Merge branch 'master' into illia-malachyn/5823-payer-has-enough-flow-…
illia-malachyn Jul 18, 2024
5fc2b1d
update emulator replace
illia-malachyn Jul 18, 2024
69d112a
run go mod tidy
illia-malachyn Jul 18, 2024
aeee394
update to merged version of emulator
peterargue Jul 18, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 45 additions & 3 deletions access/validator.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
package access

import (
"context"
"errors"
"fmt"

"github.com/onflow/cadence"
jsoncdc "github.com/onflow/cadence/encoding/json"
"github.com/onflow/cadence/runtime/parser"
"github.com/onflow/crypto"

"github.com/onflow/flow-core-contracts/lib/go/templates"
"github.com/onflow/flow-go/fvm/evm/testutils"
"github.com/onflow/flow-go/fvm/systemcontracts"
"github.com/onflow/flow-go/model/flow"
"github.com/onflow/flow-go/module/execution"
"github.com/onflow/flow-go/state"
"github.com/onflow/flow-go/state/protocol"
)
Expand Down Expand Up @@ -78,19 +83,22 @@ type TransactionValidator struct {
options TransactionValidationOptions
serviceAccountAddress flow.Address
limiter RateLimiter
scriptExecutor execution.ScriptExecutor
}

func NewTransactionValidator(
blocks Blocks,
chain flow.Chain,
options TransactionValidationOptions,
executor execution.ScriptExecutor,
) *TransactionValidator {
illia-malachyn marked this conversation as resolved.
Show resolved Hide resolved
return &TransactionValidator{
blocks: blocks,
chain: chain,
options: options,
serviceAccountAddress: chain.ServiceAddress(),
limiter: NewNoopLimiter(),
scriptExecutor: executor,
}
}

Expand All @@ -109,7 +117,7 @@ func NewTransactionValidatorWithLimiter(
}
}

func (v *TransactionValidator) Validate(tx *flow.TransactionBody) (err error) {
func (v *TransactionValidator) Validate(ctx context.Context, tx *flow.TransactionBody) (err error) {
// rate limit transactions for specific payers.
// a short term solution to prevent attacks that send too many failed transactions
// if a transaction is from a payer that should be rate limited, all the following
Expand Down Expand Up @@ -159,6 +167,11 @@ func (v *TransactionValidator) Validate(tx *flow.TransactionBody) (err error) {
return err
}

err = v.checkSufficientFlowAmountToPayForTransaction(ctx, tx)
if err != nil {
illia-malachyn marked this conversation as resolved.
Show resolved Hide resolved
return err
}

// TODO replace checkSignatureFormat by verifying the account/payer signatures

return nil
Expand Down Expand Up @@ -346,6 +359,35 @@ func (v *TransactionValidator) checkSignatureFormat(tx *flow.TransactionBody) er
return nil
}

func (v *TransactionValidator) checkSufficientFlowAmountToPayForTransaction(ctx context.Context, tx *flow.TransactionBody) error {
illia-malachyn marked this conversation as resolved.
Show resolved Hide resolved
header, err := v.blocks.HeaderByID(tx.ID())
illia-malachyn marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
return err
illia-malachyn marked this conversation as resolved.
Show resolved Hide resolved
}

inclusionEffort := cadence.UInt64(tx.InclusionEffort())
gasLimit := cadence.UInt64(tx.GasLimit)
args := testutils.EncodeArgs([]cadence.Value{inclusionEffort, gasLimit}) //TODO: is it fine to use testutils?
illia-malachyn marked this conversation as resolved.
Show resolved Hide resolved

env := systemcontracts.SystemContractsForChain(v.chain.ChainID()).AsTemplateEnv()
script := templates.GenerateVerifyPayerBalanceForTxExecution(env)
illia-malachyn marked this conversation as resolved.
Show resolved Hide resolved

result, err := v.scriptExecutor.ExecuteAtBlockHeight(ctx, script, args, header.Height)
illia-malachyn marked this conversation as resolved.
Show resolved Hide resolved

value, err := jsoncdc.Decode(nil, result)
illia-malachyn marked this conversation as resolved.
Show resolved Hide resolved
fields := cadence.FieldsMappedByName(value.(cadence.Struct))
illia-malachyn marked this conversation as resolved.
Show resolved Hide resolved
canExecuteTransaction, ok := fields["canExecuteTransaction"].(cadence.Bool)
if !ok {
return errors.New("couldn't parse canExecuteTransaction field")
}

if bool(canExecuteTransaction) {
return nil
} else {
return errors.New("cannot execute transaction")
}
}

func remove(s []string, r string) []string {
for i, v := range s {
if v == r {
Expand Down
3 changes: 3 additions & 0 deletions engine/access/access_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,9 @@ func (suite *Suite) TestSendAndGetTransaction() {
referenceBlock := unittest.BlockHeaderFixture()
transaction := unittest.TransactionFixture()
transaction.SetReferenceBlockID(referenceBlock.ID())
account, err := unittest.AccountFixture()
illia-malachyn marked this conversation as resolved.
Show resolved Hide resolved
assert.NoError(suite.T(), err)
transaction.Payer = account.Address

refSnapshot := new(protocol.Snapshot)

Expand Down
5 changes: 3 additions & 2 deletions engine/access/rpc/backend/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ func New(params Params) (*Backend, error) {
chainID: params.ChainID,
transactions: params.Transactions,
executionReceipts: params.ExecutionReceipts,
transactionValidator: configureTransactionValidator(params.State, params.ChainID),
transactionValidator: configureTransactionValidator(params.State, params.ChainID, params.ScriptExecutor),
transactionMetrics: params.AccessMetrics,
retry: retry,
connFactory: params.ConnFactory,
Expand Down Expand Up @@ -304,7 +304,7 @@ func identifierList(ids []string) (flow.IdentifierList, error) {
return idList, nil
}

func configureTransactionValidator(state protocol.State, chainID flow.ChainID) *access.TransactionValidator {
func configureTransactionValidator(state protocol.State, chainID flow.ChainID, executor execution.ScriptExecutor) *access.TransactionValidator {
return access.NewTransactionValidator(
access.NewProtocolStateBlocks(state),
chainID.Chain(),
Expand All @@ -318,6 +318,7 @@ func configureTransactionValidator(state protocol.State, chainID flow.ChainID) *
MaxTransactionByteSize: flow.DefaultMaxTransactionByteSize,
MaxCollectionByteSize: flow.DefaultMaxCollectionByteSize,
},
executor,
)
}

Expand Down
2 changes: 1 addition & 1 deletion engine/access/rpc/backend/backend_transactions.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func (b *backendTransactions) SendTransaction(
) error {
now := time.Now().UTC()

err := b.transactionValidator.Validate(tx)
err := b.transactionValidator.Validate(ctx, tx)
if err != nil {
return status.Errorf(codes.InvalidArgument, "invalid transaction: %s", err.Error())
}
Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -255,9 +255,7 @@ require (
github.com/nxadm/tail v1.4.8 // indirect
github.com/olekukonko/tablewriter v0.0.5 // indirect
github.com/onflow/flow-ft/lib/go/contracts v1.0.0 // indirect
github.com/onflow/flow-ft/lib/go/templates v1.0.0 // indirect
github.com/onflow/flow-nft/lib/go/contracts v1.2.1 // indirect
github.com/onflow/flow-nft/lib/go/templates v1.2.0 // indirect
github.com/onflow/sdks v0.5.1-0.20230912225508-b35402f12bba // indirect
github.com/onsi/ginkgo/v2 v2.13.2 // indirect
github.com/opencontainers/runtime-spec v1.1.0 // indirect
Expand Down Expand Up @@ -328,3 +326,5 @@ require (

// Using custom fork until https://github.com/onflow/flow-go/issues/5338 is resolved
replace github.com/ipfs/boxo => github.com/onflow/boxo v0.0.0-20240201202436-f2477b92f483

replace github.com/onflow/flow-core-contracts/lib/go/templates => github.com/The-K-R-O-K/flow-core-contracts/lib/go/templates v0.15.2-0.20240611100143-0707fa3a3718
8 changes: 2 additions & 6 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -990,6 +990,8 @@ github.com/Shopify/toxiproxy v2.1.4+incompatible/go.mod h1:OXgGpZ6Cli1/URJOF1DMx
github.com/StackExchange/wmi v0.0.0-20180116203802-5d049714c4a6/go.mod h1:3eOhrUMpNV+6aFIbp5/iudMxNCF27Vw2OZgy4xEx0Fg=
github.com/StackExchange/wmi v1.2.1 h1:VIkavFPXSjcnS+O8yTq7NI32k0R5Aj+v39y29VYDOSA=
github.com/StackExchange/wmi v1.2.1/go.mod h1:rcmrprowKIVzvc+NUiLncP2uuArMWLCbu9SBzvHz7e8=
github.com/The-K-R-O-K/flow-core-contracts/lib/go/templates v0.15.2-0.20240611100143-0707fa3a3718 h1:eJ2t5njxAvl6Na3eS5bXc1FRdUHWcJr6wAY1/2U8UdA=
github.com/The-K-R-O-K/flow-core-contracts/lib/go/templates v0.15.2-0.20240611100143-0707fa3a3718/go.mod h1:c09d6sNyF/j5/pAynK7sNPb1XKqJqk1rxZPEqEL+dUo=
github.com/VictoriaMetrics/fastcache v1.6.0/go.mod h1:0qHz5QP0GMX4pfmMA/zt5RgfNuXJrTP0zS7DqpHGGTw=
github.com/VictoriaMetrics/fastcache v1.12.1 h1:i0mICQuojGDL3KblA7wUNlY5lOK6a4bwt3uRKnkZU40=
github.com/VictoriaMetrics/fastcache v1.12.1/go.mod h1:tX04vaqcNoQeGLD+ra5pU5sWkuxnzWhEzLwhP9w653o=
Expand Down Expand Up @@ -2180,19 +2182,13 @@ github.com/onflow/flow v0.3.4 h1:FXUWVdYB90f/rjNcY0Owo30gL790tiYff9Pb/sycXYE=
github.com/onflow/flow v0.3.4/go.mod h1:lzyAYmbu1HfkZ9cfnL5/sjrrsnJiUU8fRL26CqLP7+c=
github.com/onflow/flow-core-contracts/lib/go/contracts v1.1.0 h1:AegPBm079X0qjneUYs+mRCpEUxSZ1lw5h4MbuXHlqn0=
github.com/onflow/flow-core-contracts/lib/go/contracts v1.1.0/go.mod h1:u/mkP/B+PbV33tEG3qfkhhBlydSvAKxfLZSfB4lsJHg=
github.com/onflow/flow-core-contracts/lib/go/templates v1.0.0 h1:za6bxPPW4JIsthhasUDTa1ruKjIO8DIhun9INQfj61Y=
github.com/onflow/flow-core-contracts/lib/go/templates v1.0.0/go.mod h1:NgbMOYnMh0GN48VsNKZuiwK7uyk38Wyo8jN9+C9QE30=
github.com/onflow/flow-ft/lib/go/contracts v1.0.0 h1:mToacZ5NWqtlWwk/7RgIl/jeKB/Sy/tIXdw90yKHcV0=
github.com/onflow/flow-ft/lib/go/contracts v1.0.0/go.mod h1:PwsL8fC81cjnUnTfmyL/HOIyHnyaw/JA474Wfj2tl6A=
github.com/onflow/flow-ft/lib/go/templates v1.0.0 h1:6cMS/lUJJ17HjKBfMO/eh0GGvnpElPgBXx7h5aoWJhs=
github.com/onflow/flow-ft/lib/go/templates v1.0.0/go.mod h1:uQ8XFqmMK2jxyBSVrmyuwdWjTEb+6zGjRYotfDJ5pAE=
github.com/onflow/flow-go-sdk v1.0.0-M1/go.mod h1:TDW0MNuCs4SvqYRUzkbRnRmHQL1h4X8wURsCw9P9beo=
github.com/onflow/flow-go-sdk v1.0.0-preview.35 h1:2ptBhFYFGOaYghZTRbj51BbYqTZjkyEpXDyaWDYrHwA=
github.com/onflow/flow-go-sdk v1.0.0-preview.35/go.mod h1:/G8vtAekhvgynLYVDtd6OnhixoGTzzknmhYCJB2YWWU=
github.com/onflow/flow-nft/lib/go/contracts v1.2.1 h1:woAAS5z651sDpi7ihAHll8NvRS9uFXIXkL6xR+bKFZY=
github.com/onflow/flow-nft/lib/go/contracts v1.2.1/go.mod h1:2gpbza+uzs1k7x31hkpBPlggIRkI53Suo0n2AyA2HcE=
github.com/onflow/flow-nft/lib/go/templates v1.2.0 h1:JSQyh9rg0RC+D1930BiRXN8lrtMs+ubVMK6aQPon6Yc=
github.com/onflow/flow-nft/lib/go/templates v1.2.0/go.mod h1:p+2hRvtjLUR3MW1NsoJe5Gqgr2eeH49QB6+s6ze00w0=
github.com/onflow/flow/protobuf/go/flow v0.3.2-0.20231121210617-52ee94b830c2/go.mod h1:NA2pX2nw8zuaxfKphhKsk00kWLwfd+tv8mS23YXO4Sk=
github.com/onflow/flow/protobuf/go/flow v0.4.4 h1:lD1owoZGFgLcvdLZDmP0Kc4GOuQeSU3/d7hrujFzt6k=
github.com/onflow/flow/protobuf/go/flow v0.4.4/go.mod h1:NA2pX2nw8zuaxfKphhKsk00kWLwfd+tv8mS23YXO4Sk=
Expand Down
Loading