Skip to content

Commit

Permalink
Merge pull request #471 from multiversx/MX-15407-tx-notarization-checker
Browse files Browse the repository at this point in the history
Tx notarization checker
  • Loading branch information
axenteoctavian authored Nov 26, 2024
2 parents 2b86131 + 3883b5c commit 136d481
Show file tree
Hide file tree
Showing 14 changed files with 169 additions and 7 deletions.
5 changes: 5 additions & 0 deletions factory/interface.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package factory

import (
"github.com/multiversx/mx-chain-proxy-go/process"
)

// ComponentHandler defines the actions common to all component handlers
type ComponentHandler interface {
Create() error
Expand All @@ -16,6 +20,7 @@ type RunTypeComponentsHandler interface {

// RunTypeComponentsHolder holds the run type components
type RunTypeComponentsHolder interface {
TxNotarizationCheckerHandlerCreator() process.TxNotarizationCheckerHandler
Create() error
Close() error
CheckSubcomponents() error
Expand Down
8 changes: 7 additions & 1 deletion factory/runType/runTypeComponents.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
package runType

type runTypeComponents struct{}
import (
"github.com/multiversx/mx-chain-proxy-go/process"
)

type runTypeComponents struct {
txNotarizationCheckerHandlerCreator process.TxNotarizationCheckerHandler
}

// Close does nothing
func (rtc *runTypeComponents) Close() error {
Expand Down
8 changes: 7 additions & 1 deletion factory/runType/runTypeComponentsFactory.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package runType

import (
"github.com/multiversx/mx-chain-proxy-go/process/factory"
)

type runTypeComponentsFactory struct{}

// NewRunTypeComponentsFactory will return a new instance of run type components factory
Expand All @@ -9,7 +13,9 @@ func NewRunTypeComponentsFactory() *runTypeComponentsFactory {

// Create will create the run type components
func (rtcf *runTypeComponentsFactory) Create() *runTypeComponents {
return &runTypeComponents{}
return &runTypeComponents{
txNotarizationCheckerHandlerCreator: factory.NewTxNotarizationChecker(),
}
}

// IsInterfaceNil returns true if there is no value under the interface
Expand Down
16 changes: 16 additions & 0 deletions factory/runType/runTypeComponentsHandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/multiversx/mx-chain-core-go/core/check"

"github.com/multiversx/mx-chain-proxy-go/factory"
"github.com/multiversx/mx-chain-proxy-go/process"
)

const runTypeComponentsName = "managedRunTypeComponents"
Expand Down Expand Up @@ -69,9 +70,24 @@ func (mrtc *managedRunTypeComponents) CheckSubcomponents() error {
if check.IfNil(mrtc.runTypeComponents) {
return errNilRunTypeComponents
}
if check.IfNil(mrtc.txNotarizationCheckerHandlerCreator) {
return process.ErrNilTxNotarizationCheckerHandler
}
return nil
}

// TxNotarizationCheckerHandlerCreator returns tx notarization checker handler
func (mrtc *managedRunTypeComponents) TxNotarizationCheckerHandlerCreator() process.TxNotarizationCheckerHandler {
mrtc.mutRunTypeCoreComponents.RLock()
defer mrtc.mutRunTypeCoreComponents.RUnlock()

if check.IfNil(mrtc.runTypeComponents) {
return nil
}

return mrtc.runTypeComponents.txNotarizationCheckerHandlerCreator
}

// IsInterfaceNil returns true if the interface is nil
func (mrtc *managedRunTypeComponents) IsInterfaceNil() bool {
return mrtc == nil
Expand Down
5 changes: 5 additions & 0 deletions factory/runType/runTypeComponentsHandler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,13 @@ func TestManagedRunTypeComponents_Create(t *testing.T) {
managedRunTypeComponents, err := createComponents()
require.NoError(t, err)

require.Nil(t, managedRunTypeComponents.TxNotarizationCheckerHandlerCreator())

err = managedRunTypeComponents.Create()
require.NoError(t, err)

require.NotNil(t, managedRunTypeComponents.TxNotarizationCheckerHandlerCreator())

require.Equal(t, runTypeComponentsName, managedRunTypeComponents.String())
require.NoError(t, managedRunTypeComponents.Close())
})
Expand All @@ -56,6 +60,7 @@ func TestManagedRunTypeComponents_Close(t *testing.T) {
require.NoError(t, err)

require.NoError(t, managedRunTypeComponents.Close())
require.Nil(t, managedRunTypeComponents.TxNotarizationCheckerHandlerCreator())
}

func TestManagedRunTypeComponents_CheckSubcomponents(t *testing.T) {
Expand Down
8 changes: 7 additions & 1 deletion factory/runType/sovereignRunTypeComponentsFactory.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package runType

import (
"github.com/multiversx/mx-chain-proxy-go/process/factory"
)

type sovereignRunTypeComponentsFactory struct{}

// NewSovereignRunTypeComponentsFactory will return a new instance of sovereign run type components factory
Expand All @@ -9,7 +13,9 @@ func NewSovereignRunTypeComponentsFactory() *sovereignRunTypeComponentsFactory {

// Create will create the run type components
func (srtcf *sovereignRunTypeComponentsFactory) Create() *runTypeComponents {
return &runTypeComponents{}
return &runTypeComponents{
txNotarizationCheckerHandlerCreator: factory.NewSovereignTxNotarizationChecker(),
}
}

// IsInterfaceNil returns true if there is no value under the interface
Expand Down
3 changes: 3 additions & 0 deletions process/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,3 +112,6 @@ var ErrEmptyPubKey = errors.New("public key is empty")

// ErrNilHttpClient signals that a nil http client has been provided
var ErrNilHttpClient = errors.New("nil http client")

// ErrNilTxNotarizationCheckerHandler signals that nil tx notarization checker handler has been provided
var ErrNilTxNotarizationCheckerHandler = errors.New("nil tx notarization checker handler has been provided")
22 changes: 22 additions & 0 deletions process/factory/sovereignTxNotarizationChecker.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package factory

import (
"github.com/multiversx/mx-chain-core-go/data/transaction"
)

type sovereignTxNotarizationChecker struct{}

// NewSovereignTxNotarizationChecker creates a new sovereign tx notarization checker
func NewSovereignTxNotarizationChecker() *sovereignTxNotarizationChecker {
return &sovereignTxNotarizationChecker{}
}

// IsNotarized returns true
func (stnc *sovereignTxNotarizationChecker) IsNotarized(_ transaction.ApiTransactionResult) bool {
return true
}

// IsInterfaceNil returns true if there is no value under the interface
func (stnc *sovereignTxNotarizationChecker) IsInterfaceNil() bool {
return stnc == nil
}
23 changes: 23 additions & 0 deletions process/factory/sovereignTxNotarizationChecker_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package factory

import (
"testing"

"github.com/multiversx/mx-chain-core-go/data/transaction"
"github.com/stretchr/testify/require"
)

func TestSovereignTxNotarizationChecker(t *testing.T) {
t.Parallel()

tnc := NewTxNotarizationChecker()
require.False(t, tnc.IsInterfaceNil())
}

func TestSovereignTxNotarizationChecker_IsNotarized(t *testing.T) {
t.Parallel()

tnc := NewTxNotarizationChecker()
isNotarized := tnc.IsNotarized(transaction.ApiTransactionResult{})
require.True(t, isNotarized)
}
1 change: 1 addition & 0 deletions process/factory/transactionProcessorFactory.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,6 @@ func CreateTransactionProcessor(
newTxCostProcessor,
logsMerger,
allowEntireTxPoolFetch,
runTypeComponents.TxNotarizationCheckerHandlerCreator(),
)
}
22 changes: 22 additions & 0 deletions process/factory/txNotarizationChecker.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package factory

import (
"github.com/multiversx/mx-chain-core-go/data/transaction"
)

type txNotarizationChecker struct{}

// NewTxNotarizationChecker creates a new tx notarization checker
func NewTxNotarizationChecker() *txNotarizationChecker {
return &txNotarizationChecker{}
}

// IsNotarized returns if tx is notarized
func (tnc *txNotarizationChecker) IsNotarized(tx transaction.ApiTransactionResult) bool {
return tx.NotarizedAtSourceInMetaNonce > 0 && tx.NotarizedAtDestinationInMetaNonce > 0
}

// IsInterfaceNil returns true if there is no value under the interface
func (tnc *txNotarizationChecker) IsInterfaceNil() bool {
return tnc == nil
}
34 changes: 34 additions & 0 deletions process/factory/txNotarizationChecker_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package factory

import (
"testing"

"github.com/multiversx/mx-chain-core-go/data/transaction"
"github.com/stretchr/testify/require"
)

func TestTxNotarizationChecker(t *testing.T) {
t.Parallel()

tnc := NewTxNotarizationChecker()
require.False(t, tnc.IsInterfaceNil())
}

func TestTxNotarizationChecker_IsNotarized(t *testing.T) {
t.Parallel()

t.Run("tx is notarized", func(t *testing.T) {
tnc := NewTxNotarizationChecker()
tx := transaction.ApiTransactionResult{
NotarizedAtSourceInMetaNonce: 1,
NotarizedAtDestinationInMetaNonce: 1,
}
isNotarized := tnc.IsNotarized(tx)
require.True(t, isNotarized)
})
t.Run("tx is not notarized", func(t *testing.T) {
tnc := NewTxNotarizationChecker()
isNotarized := tnc.IsNotarized(transaction.ApiTransactionResult{})
require.False(t, isNotarized)
})
}
7 changes: 7 additions & 0 deletions process/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/multiversx/mx-chain-core-go/data/transaction"
"github.com/multiversx/mx-chain-core-go/data/vm"
crypto "github.com/multiversx/mx-chain-crypto-go"

"github.com/multiversx/mx-chain-proxy-go/common"
"github.com/multiversx/mx-chain-proxy-go/data"
"github.com/multiversx/mx-chain-proxy-go/observer"
Expand Down Expand Up @@ -85,3 +86,9 @@ type StatusMetricsProvider interface {
type HttpClient interface {
Do(req *http.Request) (*http.Response, error)
}

// TxNotarizationCheckerHandler defines what tx notarization checked should do
type TxNotarizationCheckerHandler interface {
IsNotarized(tx transaction.ApiTransactionResult) bool
IsInterfaceNil() bool
}
14 changes: 10 additions & 4 deletions process/transactionProcessor.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/multiversx/mx-chain-core-go/data/transaction"
"github.com/multiversx/mx-chain-core-go/hashing"
"github.com/multiversx/mx-chain-core-go/marshal"

"github.com/multiversx/mx-chain-proxy-go/api/errors"
"github.com/multiversx/mx-chain-proxy-go/data"
)
Expand Down Expand Up @@ -83,6 +84,7 @@ type TransactionProcessor struct {
newTxCostProcessor func() (TransactionCostHandler, error)
mergeLogsHandler LogsMergerHandler
shouldAllowEntireTxPoolFetch bool
txNotarizationChecker TxNotarizationCheckerHandler
}

// NewTransactionProcessor creates a new instance of TransactionProcessor
Expand All @@ -94,6 +96,7 @@ func NewTransactionProcessor(
newTxCostProcessor func() (TransactionCostHandler, error),
logsMerger LogsMergerHandler,
allowEntireTxPoolFetch bool,
txNotarizationChecker TxNotarizationCheckerHandler,
) (*TransactionProcessor, error) {
if check.IfNil(proc) {
return nil, ErrNilCoreProcessor
Expand All @@ -113,6 +116,9 @@ func NewTransactionProcessor(
if check.IfNil(logsMerger) {
return nil, ErrNilLogsMerger
}
if check.IfNil(txNotarizationChecker) {
return nil, ErrNilTxNotarizationCheckerHandler
}

// no reason to get this from configs. If we are going to change the marshaller for the relayed transaction v1,
// we will need also an enable epoch handler
Expand All @@ -126,6 +132,7 @@ func NewTransactionProcessor(
mergeLogsHandler: logsMerger,
shouldAllowEntireTxPoolFetch: allowEntireTxPoolFetch,
relayedTxsMarshaller: relayedTxsMarshaller,
txNotarizationChecker: txNotarizationChecker,
}, nil
}

Expand Down Expand Up @@ -453,7 +460,7 @@ func (tp *TransactionProcessor) computeTransactionStatus(tx *transaction.ApiTran
}
}

if checkIfMoveBalanceNotarized(tx) {
if tp.checkIfMoveBalanceNotarized(tx) {
return &data.ProcessStatusResponse{
Status: string(tx.Status),
}
Expand Down Expand Up @@ -580,9 +587,8 @@ func checkIfCompleted(logs []*transaction.ApiLogs) bool {
return found
}

func checkIfMoveBalanceNotarized(tx *transaction.ApiTransactionResult) bool {
isNotarized := tx.NotarizedAtSourceInMetaNonce > 0 && tx.NotarizedAtDestinationInMetaNonce > 0
if !isNotarized {
func (tp *TransactionProcessor) checkIfMoveBalanceNotarized(tx *transaction.ApiTransactionResult) bool {
if !tp.txNotarizationChecker.IsNotarized(*tx) {
return false
}
isMoveBalance := tx.ProcessingTypeOnSource == moveBalanceDescriptor && tx.ProcessingTypeOnDestination == moveBalanceDescriptor
Expand Down

0 comments on commit 136d481

Please sign in to comment.