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

Bug-fix outport data provider #5043

Merged
merged 4 commits into from
Mar 3, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
7 changes: 6 additions & 1 deletion outport/process/transactionsfee/transactionsFeeProcessor.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/multiversx/mx-chain-core-go/marshal"
"github.com/multiversx/mx-chain-go/sharding"
"github.com/multiversx/mx-chain-go/storage"
logger "github.com/multiversx/mx-chain-logger-go"
)

// ArgTransactionsFeeProcessor holds the arguments needed for creating a new instance of transactionsFeeProcessor
Expand All @@ -24,11 +25,13 @@ type transactionsFeeProcessor struct {
txGetter transactionGetter
txFeeCalculator FeesProcessorHandler
shardCoordinator sharding.Coordinator
log logger.Logger
}

// NewTransactionsFeeProcessor will create a new instance of transactionsFeeProcessor
func NewTransactionsFeeProcessor(arg ArgTransactionsFeeProcessor) (*transactionsFeeProcessor, error) {
err := checkArg(arg)

if err != nil {
return nil, err
}
Expand All @@ -37,6 +40,7 @@ func NewTransactionsFeeProcessor(arg ArgTransactionsFeeProcessor) (*transactions
txFeeCalculator: arg.TxFeeCalculator,
shardCoordinator: arg.ShardCoordinator,
txGetter: newTxGetter(arg.TransactionsStorer, arg.Marshaller),
log: logger.GetOrCreate("outport/process/transactionsfee"),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we can either have the constant string defined as a constant or the log instance declared as a var to comply with the rest of the project.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added a const for the logger name

}, nil
}

Expand Down Expand Up @@ -160,7 +164,8 @@ func (tep *transactionsFeeProcessor) prepareScrsNoTx(transactionsAndScrs *transa

txFromStorage, err := tep.txGetter.GetTxByHash(scr.OriginalTxHash)
if err != nil {
return err
tep.log.Trace("transactionsFeeProcessor.prepareScrsNoTx: cannot find transaction in storage", "hash", scr.OriginalTxHash, "error", err.Error())
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the only clean way to test that the log.Trace function was called 👍

continue
}

gasUsed, fee := tep.txFeeCalculator.ComputeGasUsedAndFeeBasedOnRefundValue(txFromStorage, scr.Value)
Expand Down
49 changes: 49 additions & 0 deletions outport/process/transactionsfee/transactionsFeeProcessor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/multiversx/mx-chain-go/outport/mock"
"github.com/multiversx/mx-chain-go/testscommon"
"github.com/multiversx/mx-chain-go/testscommon/genericMocks"
logger "github.com/multiversx/mx-chain-logger-go"
"github.com/stretchr/testify/require"
)

Expand Down Expand Up @@ -298,3 +299,51 @@ func TestPutFeeAndGasUsedWrongRelayedTx(t *testing.T) {
require.Equal(t, uint64(550000000), initialTx.GetGasUsed())
require.Equal(t, "6103405000000000", initialTx.GetInitialPaidFee().String())
}

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

_ = logger.SetLogLevel("*:TRACE")

txHash := []byte("relayedTx")
scrWithRefund := []byte("scrWithRefund")

refundValueBig, _ := big.NewInt(0).SetString("226498540000000", 10)

scr := outportcore.NewTransactionHandlerWithGasAndFee(&smartContractResult.SmartContractResult{
Nonce: 3,
SndAddr: []byte("erd1qqqqqqqqqqqqqpgq3dswlnnlkfd3gqrcv3dhzgnvh8ryf27g5rfsecnn2s"),
RcvAddr: []byte("erd1k7j6ewjsla4zsgv8v6f6fe3dvrkgv3d0d9jerczw45hzedhyed8sh2u34u"),
PrevTxHash: []byte("f639cb7a0231191e04ec19dcb1359bd93a03fe8dc4a28a80d00835c5d1c988f8"),
OriginalTxHash: txHash,
Value: refundValueBig,
Data: []byte("@ok"),
}, 0, big.NewInt(0))

pool := &outportcore.Pool{
Scrs: map[string]coreData.TransactionHandlerWithGasUsedAndFee{
"wrong": outportcore.NewTransactionHandlerWithGasAndFee(&transaction.Transaction{}, 0, big.NewInt(0)),
string(scrWithRefund): scr,
},
}

arg := prepareMockArg()

wasCalled := false
txsFeeProc, err := NewTransactionsFeeProcessor(arg)
txsFeeProc.log = &testscommon.LoggerStub{
TraceCalled: func(message string, args ...interface{}) {
wasCalled = true
require.Equal(t, "transactionsFeeProcessor.prepareScrsNoTx: cannot find transaction in storage", message)
},
}

require.NotNil(t, txsFeeProc)
require.Nil(t, err)

err = txsFeeProc.PutFeeAndGasUsed(pool)
require.Nil(t, err)
require.Equal(t, big.NewInt(0), scr.GetFee())
require.Equal(t, uint64(0), scr.GetGasUsed())
require.True(t, wasCalled)
}
2 changes: 1 addition & 1 deletion process/block/metablock.go
Original file line number Diff line number Diff line change
Expand Up @@ -616,7 +616,7 @@ func (mp *metaProcessor) indexBlock(
PreviousHeader: lastMetaBlock,
})
if err != nil {
log.Warn("metaProcessor.indexBlock cannot prepare argSaveBlock", "error", err.Error())
log.Error("metaProcessor.indexBlock cannot prepare argSaveBlock", "error", err.Error())
return
}
mp.outportHandler.SaveBlock(argSaveBlock)
Expand Down
2 changes: 1 addition & 1 deletion process/block/shardblock.go
Original file line number Diff line number Diff line change
Expand Up @@ -596,7 +596,7 @@ func (sp *shardProcessor) indexBlockIfNeeded(
PreviousHeader: lastBlockHeader,
})
if err != nil {
log.Warn("shardProcessor.indexBlockIfNeeded cannot prepare argSaveBlock", "error", err.Error())
log.Error("shardProcessor.indexBlockIfNeeded cannot prepare argSaveBlock", "error", err.Error())
return
}
sp.outportHandler.SaveBlock(argSaveBlock)
Expand Down