Skip to content

1403 Adjust Validate Transaction for Checking Balance #1404

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

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
12 changes: 6 additions & 6 deletions api/service/transactionApiService_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,17 @@ package service
import (
"database/sql"
"errors"
"github.com/zoobc/zoobc-core/common/crypto"
"github.com/zoobc/zoobc-core/common/feedbacksystem"
"github.com/zoobc/zoobc-core/common/storage"
"reflect"
"testing"

"github.com/DATA-DOG/go-sqlmock"
"github.com/sirupsen/logrus"
"github.com/zoobc/zoobc-core/common/chaintype"
"github.com/zoobc/zoobc-core/common/crypto"
"github.com/zoobc/zoobc-core/common/feedbacksystem"
"github.com/zoobc/zoobc-core/common/model"
"github.com/zoobc/zoobc-core/common/query"
"github.com/zoobc/zoobc-core/common/storage"
"github.com/zoobc/zoobc-core/common/transaction"
"github.com/zoobc/zoobc-core/core/service"
"github.com/zoobc/zoobc-core/observer"
Expand Down Expand Up @@ -95,15 +95,15 @@ func (*mockTypeSwitcherSuccess) GetTransactionType(tx *model.Transaction) (trans
return &mockTxTypeSuccess{}, nil
}

func (*mockTxTypeValidateFail) Validate(bool) error {
func (*mockTxTypeValidateFail) Validate(bool, bool) error {
return errors.New("mockError:validateFail")
}

func (*mockTxTypeApplyUnconfirmedFail) Validate(bool) error {
func (*mockTxTypeApplyUnconfirmedFail) Validate(bool, bool) error {
return nil
}

func (*mockTxTypeSuccess) Validate(bool) error {
func (*mockTxTypeSuccess) Validate(bool, bool) error {
return nil
}

Expand Down
17 changes: 17 additions & 0 deletions common/transaction/accountBalanceHelper.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ type (
blockTimestamp uint64) error
GetBalanceByAccountAddress(accountBalance *model.AccountBalance, address []byte, dbTx bool) error
HasEnoughSpendableBalance(dbTX bool, address []byte, compareBalance int64) (enough bool, err error)
HasEnoughBalance(dbTX bool, address []byte, compareBalance int64) (enough bool, err error)
}
// AccountBalanceHelper fields for AccountBalanceHelperInterface for transaction helper
AccountBalanceHelper struct {
Expand Down Expand Up @@ -141,3 +142,19 @@ func (abh *AccountBalanceHelper) HasEnoughSpendableBalance(dbTX bool, address []
abh.accountBalance = accountBalance
return accountBalance.GetSpendableBalance() >= compareBalance, nil
}

// check if account has enough balance
func (abh *AccountBalanceHelper) HasEnoughBalance(dbTX bool, address []byte, compareBalance int64) (enough bool, err error) {
if bytes.Equal(abh.accountBalance.GetAccountAddress(), address) {
return abh.accountBalance.GetBalance() >= compareBalance, nil
}
var (
accountBalance model.AccountBalance
)
err = abh.GetBalanceByAccountAddress(&accountBalance, address, dbTX)
if err != nil {
return false, err
}
abh.accountBalance = accountBalance
return accountBalance.GetBalance() >= compareBalance, nil
}
14 changes: 9 additions & 5 deletions common/transaction/approvalEscrowTransaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ type (
EscrowApplyConfirmed(blockTimestamp int64) error
EscrowApplyUnconfirmed() error
EscrowUndoApplyUnconfirmed() error
EscrowValidate(dbTx bool) error
EscrowValidate(dbTx, checkOnSpendableBalance bool) error
// EscrowApproval handle approval an escrow transaction, execute tasks that was skipped on EscrowApplyConfirmed.
EscrowApproval(
blockTimestamp int64,
Expand Down Expand Up @@ -125,7 +125,7 @@ func (tx *ApprovalEscrowTransaction) ParseBodyBytes(
Validate is func that for validating to Transaction type.
Check transaction fields, spendable balance and more
*/
func (tx *ApprovalEscrowTransaction) Validate(dbTx bool) error {
func (tx *ApprovalEscrowTransaction) Validate(dbTx, checkOnSpendableBalance bool) error {
var (
err error
enough bool
Expand All @@ -135,8 +135,12 @@ func (tx *ApprovalEscrowTransaction) Validate(dbTx bool) error {
return err
}
// check existing account & balance

enough, err = tx.AccountBalanceHelper.HasEnoughSpendableBalance(dbTx, tx.SenderAddress, tx.Fee)
// checkOnSpendableBalance will check to the spendable balance of the sender otherwise will check the actual balance
if checkOnSpendableBalance {
enough, err = tx.AccountBalanceHelper.HasEnoughSpendableBalance(dbTx, tx.SenderAddress, tx.Fee)
} else {
enough, err = tx.AccountBalanceHelper.HasEnoughBalance(dbTx, tx.SenderAddress, tx.Fee)
}
if err != nil {
if err != sql.ErrNoRows {
return err
Expand Down Expand Up @@ -295,7 +299,7 @@ func (tx *ApprovalEscrowTransaction) Escrowable() (EscrowTypeAction, bool) {
}

// EscrowValidate special validation for escrow's transaction
func (tx *ApprovalEscrowTransaction) EscrowValidate(dbTx bool) error {
func (tx *ApprovalEscrowTransaction) EscrowValidate(dbTx, checkOnSpendableBalance bool) error {

return nil
}
Expand Down
13 changes: 7 additions & 6 deletions common/transaction/approvalEscrowTransaction_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,8 @@ func TestApprovalEscrowTransaction_Validate(t *testing.T) {
AccountBalanceHelper AccountBalanceHelperInterface
}
type args struct {
dbTx bool
dbTx bool
checkOnSpendableBalance bool
}
tests := []struct {
name string
Expand All @@ -260,7 +261,7 @@ func TestApprovalEscrowTransaction_Validate(t *testing.T) {
QueryExecutor: &mockQueryExecutorValidateNotFound{},
EscrowQuery: query.NewEscrowTransactionQuery(),
},
args: args{dbTx: false},
args: args{dbTx: false, checkOnSpendableBalance: true},
wantErr: true,
},
{
Expand All @@ -278,7 +279,7 @@ func TestApprovalEscrowTransaction_Validate(t *testing.T) {
EscrowQuery: query.NewEscrowTransactionQuery(),
TransactionQuery: nil,
},
args: args{dbTx: false},
args: args{dbTx: false, checkOnSpendableBalance: true},
wantErr: true,
},
{
Expand All @@ -296,7 +297,7 @@ func TestApprovalEscrowTransaction_Validate(t *testing.T) {
EscrowQuery: query.NewEscrowTransactionQuery(),
AccountBalanceHelper: &mockAccountApprovalEscrowTransactionAccountBalanceHelperAccountBalanceNotFound{},
},
args: args{dbTx: false},
args: args{dbTx: false, checkOnSpendableBalance: true},
wantErr: true,
},
{
Expand All @@ -314,7 +315,7 @@ func TestApprovalEscrowTransaction_Validate(t *testing.T) {
EscrowQuery: query.NewEscrowTransactionQuery(),
AccountBalanceHelper: &mockAccountBalanceApprovalEscrowTransactionAccountBalanceHelperWantSuccess{},
},
args: args{dbTx: false},
args: args{dbTx: false, checkOnSpendableBalance: true},
},
}
for _, tt := range tests {
Expand All @@ -332,7 +333,7 @@ func TestApprovalEscrowTransaction_Validate(t *testing.T) {
TypeActionSwitcher: tt.fields.TypeActionSwitcher,
AccountBalanceHelper: tt.fields.AccountBalanceHelper,
}
if err := tx.Validate(tt.args.dbTx); (err != nil) != tt.wantErr {
if err := tx.Validate(tt.args.dbTx, tt.args.checkOnSpendableBalance); (err != nil) != tt.wantErr {
t.Errorf("Validate() error = %v, wantErr %v", err, tt.wantErr)
}
})
Expand Down
2 changes: 1 addition & 1 deletion common/transaction/empty.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func (tx *TXEmpty) ApplyUnconfirmed() error {
func (tx *TXEmpty) UndoApplyUnconfirmed() error {
return nil
}
func (tx *TXEmpty) Validate(bool) error {
func (tx *TXEmpty) Validate(bool, bool) error {
return nil
}

Expand Down
21 changes: 15 additions & 6 deletions common/transaction/feeVoteCommit.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ func (tx *FeeVoteCommitTransaction) UndoApplyUnconfirmed() error {
/*
Validate to validating Transaction FeeVoteCommitTransaction type
*/
func (tx *FeeVoteCommitTransaction) Validate(dbTx bool) error {
func (tx *FeeVoteCommitTransaction) Validate(dbTx, checkOnSpendableBalance bool) error {
var (
row *sql.Row
err error
Expand Down Expand Up @@ -143,7 +143,12 @@ func (tx *FeeVoteCommitTransaction) Validate(dbTx bool) error {
}

// check existing & balance account sender
enough, err = tx.AccountBalanceHelper.HasEnoughSpendableBalance(dbTx, tx.SenderAddress, tx.Fee)
// checkOnSpendableBalance will check to the spendable balance of the sender otherwise will check the actual balance
if checkOnSpendableBalance {
enough, err = tx.AccountBalanceHelper.HasEnoughSpendableBalance(dbTx, tx.SenderAddress, tx.Fee)
} else {
enough, err = tx.AccountBalanceHelper.HasEnoughBalance(dbTx, tx.SenderAddress, tx.Fee)
}
if err != nil {
if err != sql.ErrNoRows {
return err
Expand Down Expand Up @@ -321,20 +326,24 @@ func (tx *FeeVoteCommitTransaction) EscrowUndoApplyUnconfirmed() error {
return tx.AccountBalanceHelper.AddAccountSpendableBalance(tx.SenderAddress, tx.Fee+tx.Escrow.GetCommission())
}

func (tx *FeeVoteCommitTransaction) EscrowValidate(dbTx bool) (err error) {
func (tx *FeeVoteCommitTransaction) EscrowValidate(dbTx, checkOnSpendableBalance bool) (err error) {
if tx.Escrow.GetApproverAddress() == nil || bytes.Equal(tx.Escrow.GetApproverAddress(), []byte{}) {
return blocker.NewBlocker(blocker.ValidationErr, "ApproverAddressRequired")
}
if tx.Escrow.GetTimeout() > uint64(constant.MinRollbackBlocks) {
return blocker.NewBlocker(blocker.ValidationErr, "TimeoutLimitExceeded")
}
err = tx.Validate(dbTx)
err = tx.Validate(dbTx, checkOnSpendableBalance)
if err != nil {
return err
}

// checkOnSpendableBalance will check to the spendable balance of the sender otherwise will check the actual balance
var enough bool
enough, err = tx.AccountBalanceHelper.HasEnoughSpendableBalance(dbTx, tx.SenderAddress, tx.Fee+tx.Escrow.GetCommission())
if checkOnSpendableBalance {
enough, err = tx.AccountBalanceHelper.HasEnoughSpendableBalance(dbTx, tx.SenderAddress, tx.Fee+tx.Escrow.GetCommission())
} else {
enough, err = tx.AccountBalanceHelper.HasEnoughBalance(dbTx, tx.SenderAddress, tx.Fee+tx.Escrow.GetCommission())
}
if err != nil {
if err != sql.ErrNoRows {
return err
Expand Down
38 changes: 25 additions & 13 deletions common/transaction/feeVoteCommit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -574,7 +574,8 @@ func TestFeeVoteCommitTransaction_Validate(t *testing.T) {
QueryExecutor query.ExecutorInterface
}
type args struct {
dbTx bool
dbTx bool
checkOnSpendableBalance bool
}
tests := []struct {
name string
Expand All @@ -601,7 +602,8 @@ func TestFeeVoteCommitTransaction_Validate(t *testing.T) {
QueryExecutor: &mockQueryExecutorFeeVoteCommitValidateSuccess{},
},
args: args{
dbTx: false,
dbTx: false,
checkOnSpendableBalance: true,
},
wantErr: true,
},
Expand All @@ -614,7 +616,8 @@ func TestFeeVoteCommitTransaction_Validate(t *testing.T) {
FeeScaleService: &mockFeeScaleServiceValidateFail{},
},
args: args{
dbTx: false,
dbTx: false,
checkOnSpendableBalance: true,
},
wantErr: true,
},
Expand All @@ -627,7 +630,8 @@ func TestFeeVoteCommitTransaction_Validate(t *testing.T) {
FeeScaleService: &mockFeeScaleServiceValidateSuccess{},
},
args: args{
dbTx: false,
dbTx: false,
checkOnSpendableBalance: true,
},
wantErr: true,
},
Expand All @@ -641,7 +645,8 @@ func TestFeeVoteCommitTransaction_Validate(t *testing.T) {
QueryExecutor: &mockQueryExecutorFeeVoteCommitValidateSuccess{},
},
args: args{
dbTx: false,
dbTx: false,
checkOnSpendableBalance: true,
},
wantErr: true,
},
Expand All @@ -655,7 +660,8 @@ func TestFeeVoteCommitTransaction_Validate(t *testing.T) {
BlockQuery: &mockBlockQueryGetBlockHeightFeeVoteCommitValidateDuplicated{},
},
args: args{
dbTx: false,
dbTx: false,
checkOnSpendableBalance: true,
},
wantErr: true,
},
Expand All @@ -670,7 +676,8 @@ func TestFeeVoteCommitTransaction_Validate(t *testing.T) {
NodeRegistrationQuery: &mockNodeRegistrationQueryFeeVoteCommitValidateFail{},
},
args: args{
dbTx: false,
dbTx: false,
checkOnSpendableBalance: true,
},
wantErr: true,
},
Expand All @@ -685,7 +692,8 @@ func TestFeeVoteCommitTransaction_Validate(t *testing.T) {
NodeRegistrationQuery: &mockNodeRegistrationQueryFeeVoteCommitValidateFailErrNoRow{},
},
args: args{
dbTx: false,
dbTx: false,
checkOnSpendableBalance: true,
},
wantErr: true,
},
Expand All @@ -700,7 +708,8 @@ func TestFeeVoteCommitTransaction_Validate(t *testing.T) {
NodeRegistrationQuery: &mockNodeRegistrationQueryFeeVoteCommitValidateFailErrNoRow{},
},
args: args{
dbTx: false,
dbTx: false,
checkOnSpendableBalance: true,
},
wantErr: true,
},
Expand All @@ -716,7 +725,8 @@ func TestFeeVoteCommitTransaction_Validate(t *testing.T) {
AccountBalanceHelper: &mockAccountBalanceHelperFeeVoteCommitValidateFail{},
},
args: args{
dbTx: false,
dbTx: false,
checkOnSpendableBalance: true,
},
wantErr: true,
},
Expand All @@ -733,7 +743,8 @@ func TestFeeVoteCommitTransaction_Validate(t *testing.T) {
FeeScaleService: &mockFeeScaleServiceValidateSuccess{},
},
args: args{
dbTx: false,
dbTx: false,
checkOnSpendableBalance: true,
},
wantErr: true,
},
Expand All @@ -750,7 +761,8 @@ func TestFeeVoteCommitTransaction_Validate(t *testing.T) {
FeeScaleService: &mockFeeScaleServiceValidateSuccess{},
},
args: args{
dbTx: false,
dbTx: false,
checkOnSpendableBalance: true,
},
wantErr: false,
},
Expand All @@ -770,7 +782,7 @@ func TestFeeVoteCommitTransaction_Validate(t *testing.T) {
AccountBalanceHelper: tt.fields.AccountBalanceHelper,
QueryExecutor: tt.fields.QueryExecutor,
}
if err := tx.Validate(tt.args.dbTx); (err != nil) != tt.wantErr {
if err := tx.Validate(tt.args.dbTx, tt.args.checkOnSpendableBalance); (err != nil) != tt.wantErr {
t.Errorf("FeeVoteCommitTransaction.Validate() error = %v, wantErr %v", err, tt.wantErr)
}
})
Expand Down
Loading