Skip to content

1346 Full Cache Mempool Transaction (unsafe mempool) #1359

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 14 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
38 changes: 2 additions & 36 deletions api/service/transactionApiService.go
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,6 @@ func (ts *TransactionService) PostTransaction(
) (*model.Transaction, error) {
var (
txBytes = req.GetTransactionBytes()
txType transaction.TypeAction
tx *model.Transaction
err error
tpsProcessed,
Expand Down Expand Up @@ -277,44 +276,11 @@ func (ts *TransactionService) PostTransaction(
return nil, status.Error(codes.Internal, err.Error())
}
// Validate Tx
txType, err = ts.ActionTypeSwitcher.GetTransactionType(tx)
if err != nil {
return nil, status.Error(codes.Internal, err.Error())
}
if err = ts.MempoolService.ValidateMempoolTransaction(tx); err != nil {
return nil, status.Error(codes.Internal, err.Error())
}
// Apply Unconfirmed
err = ts.Query.BeginTx()
if err != nil {
return nil, status.Error(codes.Internal, err.Error())
}

// TODO: repetitive way
escrowable, ok := txType.Escrowable()
switch ok {
case true:
err = escrowable.EscrowApplyUnconfirmed()
default:
err = txType.ApplyUnconfirmed()
}
if err != nil {
errRollback := ts.Query.RollbackTx()
if errRollback != nil {
return nil, status.Error(codes.Internal, errRollback.Error())
}
return nil, status.Error(codes.Internal, err.Error())
}
// Save to mempool
err = ts.MempoolService.AddMempoolTransaction(tx, txBytes)
if err != nil {
errRollback := ts.Query.RollbackTx()
if errRollback != nil {
return nil, status.Error(codes.Internal, errRollback.Error())
}
return nil, status.Error(codes.Internal, err.Error())
}
err = ts.Query.CommitTx()
// process validated received transction
err = ts.MempoolService.ReceivedTransactionFromWallet(tx, txBytes)
if err != nil {
return nil, status.Error(codes.Internal, err.Error())
}
Expand Down
205 changes: 38 additions & 167 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 @@ -107,11 +107,11 @@ func (*mockTxTypeSuccess) Validate(bool) error {
return nil
}

func (*mockTxTypeApplyUnconfirmedFail) ApplyUnconfirmed() error {
func (*mockTxTypeApplyUnconfirmedFail) ApplyUnconfirmed(bool) error {
return errors.New("mockError:ApplyUnconfirmedFail")
}

func (*mockTxTypeSuccess) ApplyUnconfirmed() error {
func (*mockTxTypeSuccess) ApplyUnconfirmed(bool) error {
return nil
}

Expand All @@ -127,7 +127,10 @@ func (*mockMempoolServiceFailValidate) ValidateMempoolTransaction(mpTx *model.Tr
return errors.New("mockedError")
}

func (*mockMempoolServiceSuccess) AddMempoolTransaction(tx *model.Transaction, txBytes []byte) error {
func (*mockMempoolServiceSuccess) ReceivedTransactionFromWallet(
receivedTx *model.Transaction,
receivedTxBytes []byte,
) error {
return nil
}

Expand Down Expand Up @@ -227,7 +230,7 @@ func TestNewTransactionService(t *testing.T) {
); !reflect.DeepEqual(got, tt.want) {
t.Errorf("NewTransactionService() = %v, want %v", got, tt.want)
}
defer resetTransactionService()
resetTransactionService()
})
}
}
Expand Down Expand Up @@ -271,6 +274,9 @@ type (
mockCacheStorageAlwaysSuccess struct {
storage.CacheStorageInterface
}
mockMempoolServiceReceivedPostTransactionTransactionFromWalletFail struct {
service.MempoolService
}
)

func (*mockCacheStorageAlwaysSuccess) SetItem(key, item interface{}) error { return nil }
Expand All @@ -280,6 +286,17 @@ func (*mockCacheStorageAlwaysSuccess) RemoveItem(key interface{}) error { ret
func (*mockCacheStorageAlwaysSuccess) GetSize() int64 { return 0 }
func (*mockCacheStorageAlwaysSuccess) ClearCache() error { return nil }

func (*mockMempoolServiceReceivedPostTransactionTransactionFromWalletFail) ValidateMempoolTransaction(
mpTx *model.Transaction) error {
return nil
}

func (*mockMempoolServiceReceivedPostTransactionTransactionFromWalletFail) ReceivedTransactionFromWallet(
receivedTx *model.Transaction,
receivedTxBytes []byte,
) error {
return errors.New("mockedErr")
}
func TestTransactionService_PostTransaction(t *testing.T) {

var (
Expand All @@ -306,20 +323,6 @@ func TestTransactionService_PostTransaction(t *testing.T) {
false,
true,
)
escrowApprovalTX, escrowApprovalTXBytes := transaction.GetFixtureForSpecificTransaction(
-62373445000112233,
1581301507,
txAPISenderAccount1,
nil,
12,
model.TransactionType_ApprovalEscrowTransaction,
&model.ApprovalEscrowTransactionBody{
Approval: 0,
TransactionID: 0,
},
false,
true,
)

type fields struct {
Query query.ExecutorInterface
Expand Down Expand Up @@ -361,117 +364,11 @@ func TestTransactionService_PostTransaction(t *testing.T) {
want: nil,
},
{
name: "PostTransaction:txType.ValidateFail",
fields: fields{
Query: nil,
ActionTypeSwitcher: &mockTypeSwitcherValidateFail{},
MempoolService: &mockMempoolServiceFailValidate{},
Log: mockLog,
TransactionUtil: &transaction.Util{
MempoolCacheStorage: &mockCacheStorageAlwaysSuccess{},
},
FeedbackStrategy: &feedbacksystem.DummyFeedbackStrategy{},
},
args: args{
chaintype: &chaintype.MainChain{},
req: &model.PostTransactionRequest{
TransactionBytes: sendMoneyTxBytes,
},
},
wantErr: true,
want: nil,
},
{
name: "PostTransaction:beginTxFail",
name: "ValidateMempoolTransaction:Fail",
fields: fields{
Query: &mockTransactionExecutorFailBeginTx{},
ActionTypeSwitcher: &mockTypeSwitcherApplyUnconfirmedFail{},
Log: mockLog,
MempoolService: &mockMempoolServiceSuccess{},
TransactionUtil: &transaction.Util{
MempoolCacheStorage: &mockCacheStorageAlwaysSuccess{},
},
FeedbackStrategy: &feedbacksystem.DummyFeedbackStrategy{},
},
args: args{
chaintype: &chaintype.MainChain{},
req: &model.PostTransactionRequest{
TransactionBytes: sendMoneyTxBytes,
},
},
wantErr: true,
want: nil,
},
{
name: "PostTransaction:txType.ApplyUnconfirmedFail",
fields: fields{
Query: &mockTransactionExecutorSuccess{},
ActionTypeSwitcher: &mockTypeSwitcherApplyUnconfirmedFail{},
Log: mockLog,
MempoolService: &mockMempoolServiceSuccess{},
TransactionUtil: &transaction.Util{
MempoolCacheStorage: &mockCacheStorageAlwaysSuccess{},
},
FeedbackStrategy: &feedbacksystem.DummyFeedbackStrategy{},
},
args: args{
chaintype: &chaintype.MainChain{},
req: &model.PostTransactionRequest{
TransactionBytes: sendMoneyTxBytes,
},
},
wantErr: true,
want: nil,
},
{
name: "PostTransaction:txType.ApplyUnconfirmedFail-RollbackFail",
fields: fields{
Query: &mockTransactionExecutorRollbackFail{},
ActionTypeSwitcher: &mockTypeSwitcherApplyUnconfirmedFail{},
Log: mockLog,
MempoolService: &mockMempoolServiceSuccess{},
TransactionUtil: &transaction.Util{
MempoolCacheStorage: &mockCacheStorageAlwaysSuccess{},
},
FeedbackStrategy: &feedbacksystem.DummyFeedbackStrategy{},
},
args: args{
chaintype: &chaintype.MainChain{},
req: &model.PostTransactionRequest{
TransactionBytes: sendMoneyTxBytes,
},
},
wantErr: true,
want: nil,
},
{
name: "PostTransaction:txType.AddMempoolTransactionFail",
fields: fields{
Query: &mockTransactionExecutorSuccess{},
ActionTypeSwitcher: &mockTypeSwitcherSuccess{},
MempoolService: &mockMempoolServiceFailAdd{},
Log: mockLog,
TransactionUtil: &transaction.Util{
MempoolCacheStorage: &mockCacheStorageAlwaysSuccess{},
},
FeedbackStrategy: &feedbacksystem.DummyFeedbackStrategy{},
},
args: args{
chaintype: &chaintype.MainChain{},
req: &model.PostTransactionRequest{
TransactionBytes: sendMoneyTxBytes,
},
},
wantErr: true,
want: nil,
},
{
name: "PostTransaction:txType.AddMempoolTransactionFail-RollbackFail",
fields: fields{
Query: &mockTransactionExecutorRollbackFail{},
ActionTypeSwitcher: &mockTypeSwitcherSuccess{},
MempoolService: &mockMempoolServiceFailAdd{},
Log: mockLog,
MempoolService: &mockMempoolServiceFailValidate{},
Observer: observer.NewObserver(),
Log: mockLog,
TransactionUtil: &transaction.Util{
MempoolCacheStorage: &mockCacheStorageAlwaysSuccess{},
},
Expand All @@ -480,19 +377,18 @@ func TestTransactionService_PostTransaction(t *testing.T) {
args: args{
chaintype: &chaintype.MainChain{},
req: &model.PostTransactionRequest{
TransactionBytes: sendMoneyTxBytes,
TransactionBytes: transactionBytes,
},
},
wantErr: true,
want: nil,
},
{
name: "PostTransaction:txType.AddMempoolTransactionFail-RollbackFail",
name: "ReceivedTransactionFromWallet:Fail",
fields: fields{
Query: &mockTransactionExecutorCommitFail{},
ActionTypeSwitcher: &mockTypeSwitcherSuccess{},
MempoolService: &mockMempoolServiceSuccess{},
Log: mockLog,
MempoolService: &mockMempoolServiceReceivedPostTransactionTransactionFromWalletFail{},
Observer: observer.NewObserver(),
Log: mockLog,
TransactionUtil: &transaction.Util{
MempoolCacheStorage: &mockCacheStorageAlwaysSuccess{},
},
Expand All @@ -501,20 +397,18 @@ func TestTransactionService_PostTransaction(t *testing.T) {
args: args{
chaintype: &chaintype.MainChain{},
req: &model.PostTransactionRequest{
TransactionBytes: sendMoneyTxBytes,
TransactionBytes: transactionBytes,
},
},
wantErr: true,
want: nil,
},
{
name: "PostTransaction:txType.Success",
name: "Success",
fields: fields{
Query: &mockTransactionExecutorSuccess{},
ActionTypeSwitcher: &mockTypeSwitcherSuccess{},
MempoolService: &mockMempoolServiceSuccess{},
Observer: observer.NewObserver(),
Log: mockLog,
MempoolService: &mockMempoolServiceSuccess{},
Observer: observer.NewObserver(),
Log: mockLog,
TransactionUtil: &transaction.Util{
MempoolCacheStorage: &mockCacheStorageAlwaysSuccess{},
},
Expand All @@ -529,29 +423,6 @@ func TestTransactionService_PostTransaction(t *testing.T) {
wantErr: false,
want: txTypeSuccess,
},
{
name: "WantError:ValidateMempoolFail1",
fields: fields{
Query: &mockQueryExecutorPostApprovalEscrowTX{},
Signature: nil,
ActionTypeSwitcher: &transaction.TypeSwitcher{
Executor: &mockQueryExecutorPostApprovalEscrowTX{},
},
MempoolService: &mockMempoolServicePostApprovalEscrowTXSuccess{},
Observer: observer.NewObserver(),
TransactionUtil: &transaction.Util{
MempoolCacheStorage: &mockCacheStorageAlwaysSuccess{},
},
FeedbackStrategy: &feedbacksystem.DummyFeedbackStrategy{},
},
args: args{
chaintype: &chaintype.MainChain{},
req: &model.PostTransactionRequest{
TransactionBytes: escrowApprovalTXBytes,
},
},
want: escrowApprovalTX,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down
8 changes: 5 additions & 3 deletions cmd/block/blockGenerator.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package block

import (
"fmt"
"strings"
"time"

log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
Expand All @@ -21,8 +23,6 @@ import (
"github.com/zoobc/zoobc-core/core/smith/strategy"
coreUtil "github.com/zoobc/zoobc-core/core/util"
"github.com/zoobc/zoobc-core/observer"
"strings"
"time"
)

type (
Expand Down Expand Up @@ -119,7 +119,7 @@ func initialize(
panic(err)
}
queryExecutor = query.NewQueryExecutor(db)
mempoolStorage := storage.NewMempoolStorage()
mempoolStorage := storage.NewMempoolStorage(monitoring.TypeMempoolCacheStorage, monitoring.TypeMempoolCountCacheStorage)

actionSwitcher := &transaction.TypeSwitcher{
Executor: queryExecutor,
Expand Down Expand Up @@ -163,6 +163,8 @@ func initialize(
blocksStorage,
mempoolStorage,
nil,
nil,
nil,
)
nodeAddressInfoService := service.NewNodeAddressInfoService(
queryExecutor,
Expand Down
Loading