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

Link inner tx to results #6527

Closed
wants to merge 11 commits into from
4 changes: 2 additions & 2 deletions dblookupext/disabled/nilHistoryRepository.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ func (nhr *nilHistoryRepository) OnNotarizedBlocks(_ uint32, _ []data.HeaderHand
}

// GetMiniblockMetadataByTxHash does nothing
func (nhr *nilHistoryRepository) GetMiniblockMetadataByTxHash(_ []byte) (*dblookupext.MiniblockMetadata, error) {
return nil, nil
func (nhr *nilHistoryRepository) GetMiniblockMetadataByTxHash(_ []byte) (*dblookupext.MiniblockMetadata, []byte, error) {
return nil, nil, nil
}

// GetEpochByHash returns a not implemented error
Expand Down
66 changes: 36 additions & 30 deletions dblookupext/historyRepository.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,29 +170,7 @@ func (hr *historyRepository) RecordBlock(blockHeaderHash []byte,
}
}

for txHash, tx := range txResultsFromPool {
innerTxs := tx.GetUserTransactions()
if len(innerTxs) == 0 {
continue
}

for _, innerTx := range innerTxs {
innerTxHash, errCalculateHash := core.CalculateHash(hr.marshalizer, hr.hasher, innerTx)
if errCalculateHash != nil {
logging.LogErrAsWarnExceptAsDebugIfClosingError(log, errCalculateHash, "CalculateHash for inner tx",
"txHash", txHash, "err", errCalculateHash)
continue
}

relayedHashPrefix := []byte{byte(RelayedTxHash)}
relayedTxHash := append(relayedHashPrefix, []byte(txHash)...)
errPut := hr.miniblockHashByTxHashIndex.Put(innerTxHash, relayedTxHash)
if errPut != nil {
logging.LogErrAsWarnExceptAsDebugIfClosingError(log, errPut, "miniblockHashByTxHashIndex.Put() innerTxHash-relayedTxHash pair",
"txHash", txHash, "innerTxHash", innerTxHash, "err", errPut)
}
}
}
hr.recordInnerTxs(txResultsFromPool)

err = hr.eventsHashesByTxHashIndex.saveResultsHashes(epoch, scrResultsFromPool, receiptsFromPool)
if err != nil {
Expand Down Expand Up @@ -262,6 +240,32 @@ func (hr *historyRepository) recordMiniblock(blockHeaderHash []byte, blockHeader
return nil
}

func (hr *historyRepository) recordInnerTxs(txResultsFromPool map[string]data.TransactionHandler) {
for txHash, tx := range txResultsFromPool {
innerTxs := tx.GetUserTransactions()
if len(innerTxs) == 0 {
continue
}

for _, innerTx := range innerTxs {
innerTxHash, errCalculateHash := core.CalculateHash(hr.marshalizer, hr.hasher, innerTx)
if errCalculateHash != nil {
logging.LogErrAsWarnExceptAsDebugIfClosingError(log, errCalculateHash, "CalculateHash for inner tx",
"txHash", txHash, "err", errCalculateHash)
continue
}

relayedHashPrefix := []byte{byte(RelayedTxHash)}
relayedTxHashPrefixed := append(relayedHashPrefix, []byte(txHash)...)
errPut := hr.miniblockHashByTxHashIndex.Put(innerTxHash, relayedTxHashPrefixed)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Thus, now we keep heterogeneous data in this storer. In a future PR, we should refactor to adjust its naming.

if errPut != nil {
logging.LogErrAsWarnExceptAsDebugIfClosingError(log, errPut, "miniblockHashByTxHashIndex.Put() innerTxHash-relayedTxHash pair",
"txHash", txHash, "innerTxHash", innerTxHash, "err", errPut)
}
}
}
}

func (hr *historyRepository) computeMiniblockHash(miniblock *block.MiniBlock) ([]byte, error) {
return core.CalculateHash(hr.marshalizer, hr.hasher, miniblock)
}
Expand All @@ -285,20 +289,21 @@ func (hr *historyRepository) markMiniblockMetadataAsRecentlyInserted(miniblockHa
}

// GetMiniblockMetadataByTxHash will return a history transaction for the given hash from storage
func (hr *historyRepository) GetMiniblockMetadataByTxHash(hash []byte) (*MiniblockMetadata, error) {
func (hr *historyRepository) GetMiniblockMetadataByTxHash(hash []byte) (*MiniblockMetadata, []byte, error) {
miniblockHash, err := hr.miniblockHashByTxHashIndex.Get(hash)
if err != nil {
return nil, err
return nil, nil, err
}

hashSize := hr.hasher.Size()
if len(miniblockHash) == hashSize {
Copy link
Contributor

Choose a reason for hiding this comment

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

this adds a previously not needed check between the hasher.Size() and the hashSize.
if we would want to change the hasher, previously it would work without any changes, with the new code if the hash size is different it will not. Just to be aware in the future about this "connection"

return hr.getMiniblockMetadataByMiniblockHash(miniblockHash)
metadata, err := hr.getMiniblockMetadataByMiniblockHash(miniblockHash)
return metadata, nil, err
}

prefixedHashSize := hashSize + 1
if len(miniblockHash) != prefixedHashSize {
return nil, ErrInvalidHashSize
return nil, nil, ErrInvalidHashSize
}

prefix := miniblockHash[0]
Expand All @@ -307,12 +312,13 @@ func (hr *historyRepository) GetMiniblockMetadataByTxHash(hash []byte) (*Miniblo
case byte(RelayedTxHash):
miniblockHash, err = hr.miniblockHashByTxHashIndex.Get(actualHash)
if err != nil {
return nil, err
return nil, nil, err
}

return hr.getMiniblockMetadataByMiniblockHash(miniblockHash)
metadata, err := hr.getMiniblockMetadataByMiniblockHash(miniblockHash)
return metadata, actualHash, err
default:
return nil, ErrInvalidHash
return nil, nil, ErrInvalidHash
}
}

Expand Down
11 changes: 6 additions & 5 deletions dblookupext/historyRepository_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,24 +204,25 @@ func TestHistoryRepository_GetMiniblockMetadata(t *testing.T) {
txResultsFromPool, nil, nil, nil, nil,
)

metadata, err := repo.GetMiniblockMetadataByTxHash([]byte("txA"))
metadata, _, err := repo.GetMiniblockMetadataByTxHash([]byte("txA"))
require.Nil(t, err)
require.Equal(t, 42, int(metadata.Epoch))
require.Equal(t, 4321, int(metadata.Round))

metadata, err = repo.GetMiniblockMetadataByTxHash([]byte("txB"))
metadata, _, err = repo.GetMiniblockMetadataByTxHash([]byte("txB"))
require.Nil(t, err)
require.Equal(t, 42, int(metadata.Epoch))
require.Equal(t, 4321, int(metadata.Round))

_, err = repo.GetMiniblockMetadataByTxHash([]byte("foobar"))
_, _, err = repo.GetMiniblockMetadataByTxHash([]byte("foobar"))
require.NotNil(t, err)

_, err = repo.GetMiniblockMetadataByTxHash(innerTxHashTooBigRelayedHash)
_, _, err = repo.GetMiniblockMetadataByTxHash(innerTxHashTooBigRelayedHash)
require.Equal(t, ErrInvalidHashSize, err)

metadata, err = repo.GetMiniblockMetadataByTxHash(innerTxHash)
metadata, parentTxHash, err := repo.GetMiniblockMetadataByTxHash(innerTxHash)
require.NoError(t, err)
require.Equal(t, relayedTxHash, parentTxHash)
require.Equal(t, 42, int(metadata.Epoch))
require.Equal(t, 4321, int(metadata.Round))
}
Expand Down
2 changes: 1 addition & 1 deletion dblookupext/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ type HistoryRepository interface {
createdIntraShardMiniBlocks []*block.MiniBlock,
logs []*data.LogData) error
OnNotarizedBlocks(shardID uint32, headers []data.HeaderHandler, headersHashes [][]byte)
GetMiniblockMetadataByTxHash(hash []byte) (*MiniblockMetadata, error)
GetMiniblockMetadataByTxHash(hash []byte) (*MiniblockMetadata, []byte, error)
GetEpochByHash(hash []byte) (uint32, error)
GetResultsHashesByTxHash(txHash []byte, epoch uint32) (*ResultsHashesByTxHash, error)
RevertBlock(blockHeader data.HeaderHandler, blockBody data.BodyHandler) error
Expand Down
1 change: 1 addition & 0 deletions factory/processing/blockProcessorCreator.go
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,7 @@ func (pcf *processComponentsFactory) newShardBlockProcessor(
TxVersionChecker: pcf.coreData.TxVersionChecker(),
TxLogsProcessor: pcf.txLogsProcessor,
RelayedTxV3Processor: relayedTxV3Processor,
InnerTxsHashesHolder: transaction.NewTxHashesHolder(),
}
transactionProcessor, err := transaction.NewTxProcessor(argsNewTxProcessor)
if err != nil {
Expand Down
1 change: 1 addition & 0 deletions factory/processing/txSimulatorProcessComponents.go
Original file line number Diff line number Diff line change
Expand Up @@ -399,6 +399,7 @@ func (pcf *processComponentsFactory) createArgsTxSimulatorProcessorShard(
GuardianChecker: pcf.bootstrapComponents.GuardedAccountHandler(),
TxLogsProcessor: txLogsProcessor,
RelayedTxV3Processor: relayedTxV3Processor,
InnerTxsHashesHolder: transaction.NewTxHashesHolder(),
}

txProcessor, err := transaction.NewTxProcessor(argsTxProcessor)
Expand Down
1 change: 1 addition & 0 deletions genesis/process/shardGenesisBlockCreator.go
Original file line number Diff line number Diff line change
Expand Up @@ -571,6 +571,7 @@ func createProcessorsForShardGenesisBlock(arg ArgsGenesisBlockCreator, enableEpo
GuardianChecker: disabledGuardian.NewDisabledGuardedAccountHandler(),
TxLogsProcessor: arg.TxLogsProcessor,
RelayedTxV3Processor: processDisabled.NewRelayedTxV3Processor(),
InnerTxsHashesHolder: transaction.NewTxHashesHolder(),
}
transactionProcessor, err := transaction.NewTxProcessor(argsNewTxProcessor)
if err != nil {
Expand Down
15 changes: 15 additions & 0 deletions integrationTests/chainSimulator/relayedTx/relayedTx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,8 @@ func TestRelayedTransactionInMultiShardEnvironmentWithChainSimulatorAndInvalidNo
result, err := cs.SendTxAndGenerateBlockTilTxIsExecuted(relayedTx, maxNumOfBlocksToGenerateWhenExecutingTx)
require.NoError(t, err)

relayedTxHash := result.Hash

providedInnerTxHashes := getInnerTxsHashes(t, relayerShardNode, innerTxs)

require.Zero(t, len(result.SmartContractResults))
Expand All @@ -265,6 +267,9 @@ func TestRelayedTransactionInMultiShardEnvironmentWithChainSimulatorAndInvalidNo
result, err = relayerShardNode.GetFacadeHandler().GetTransaction(hex.EncodeToString(innerTxHash), true)
require.NoError(t, err)

require.Equal(t, relayedTxHash, result.OriginalTransactionHash)
require.Equal(t, relayedTxHash, result.PreviousTransactionHash)

for _, scr := range result.SmartContractResults {
if len(scr.ReturnMessage) == 0 {
scrsMap["success"]++
Expand Down Expand Up @@ -378,6 +383,8 @@ func TestRelayedTransactionInMultiShardEnvironmentWithChainSimulatorScCalls(t *t
result, err := cs.SendTxAndGenerateBlockTilTxIsExecuted(relayedTx, maxNumOfBlocksToGenerateWhenExecutingTx)
require.NoError(t, err)

relayedTxHash := result.Hash

checkSum(t, scShardNodeHandler, scAddressBytes, owner.Bytes, 4)

providedInnerTxHashes := getInnerTxsHashes(t, relayerShardNode, innerTxs)
Expand All @@ -390,6 +397,9 @@ func TestRelayedTransactionInMultiShardEnvironmentWithChainSimulatorScCalls(t *t
result, err = relayerShardNode.GetFacadeHandler().GetTransaction(hex.EncodeToString(innerTxHash), true)
require.NoError(t, err)

require.Equal(t, relayedTxHash, result.OriginalTransactionHash)
require.Equal(t, relayedTxHash, result.PreviousTransactionHash)

switch idx {
case 0, 1, 3, 5: // sc calls ok
require.Equal(t, 2, len(result.SmartContractResults))
Expand Down Expand Up @@ -773,6 +783,8 @@ func TestRelayedTransactionInMultiShardEnvironmentWithChainSimulatorInnerNotExec
result, err := cs.SendTxAndGenerateBlockTilTxIsExecuted(relayedTx, maxNumOfBlocksToGenerateWhenExecutingTx)
require.NoError(t, err)

relayedTxHash := result.Hash

// generate few more blocks for the cross shard scrs to be done
err = cs.GenerateBlocks(maxNumOfBlocksToGenerateWhenExecutingTx)
require.NoError(t, err)
Expand All @@ -787,6 +799,9 @@ func TestRelayedTransactionInMultiShardEnvironmentWithChainSimulatorInnerNotExec
result, err = relayerShardNode.GetFacadeHandler().GetTransaction(hex.EncodeToString(innerTxHash), true)
require.NoError(t, err)

require.Equal(t, relayedTxHash, result.OriginalTransactionHash)
require.Equal(t, relayedTxHash, result.PreviousTransactionHash)

require.Equal(t, 1, len(result.SmartContractResults))
switch idx {
case 0:
Expand Down
1 change: 1 addition & 0 deletions integrationTests/testInitializer.go
Original file line number Diff line number Diff line change
Expand Up @@ -1066,6 +1066,7 @@ func CreateSimpleTxProcessor(accnts state.AccountsAdapter) process.TransactionPr
GuardianChecker: &guardianMocks.GuardedAccountHandlerStub{},
TxLogsProcessor: &mock.TxLogsProcessorStub{},
RelayedTxV3Processor: &processMocks.RelayedTxV3ProcessorMock{},
InnerTxsHashesHolder: &processMocks.TransactionHashesHolderMock{},
}
txProcessor, _ := txProc.NewTxProcessor(argsNewTxProcessor)

Expand Down
1 change: 1 addition & 0 deletions integrationTests/testProcessorNode.go
Original file line number Diff line number Diff line change
Expand Up @@ -1754,6 +1754,7 @@ func (tpn *TestProcessorNode) initInnerProcessors(gasMap map[string]map[string]u
TxVersionChecker: &testscommon.TxVersionCheckerStub{},
TxLogsProcessor: tpn.TransactionLogProcessor,
RelayedTxV3Processor: relayedV3TxProcessor,
InnerTxsHashesHolder: &processMocks.TransactionHashesHolderMock{},
}
tpn.TxProcessor, _ = transaction.NewTxProcessor(argsNewTxProcessor)
scheduledSCRsStorer, _ := tpn.Storage.GetStorer(dataRetriever.ScheduledSCRsUnit)
Expand Down
2 changes: 2 additions & 0 deletions integrationTests/vm/testInitializer.go
Original file line number Diff line number Diff line change
Expand Up @@ -498,6 +498,7 @@ func CreateTxProcessorWithOneSCExecutorMockVM(
GuardianChecker: guardedAccountHandler,
TxLogsProcessor: &mock.TxLogsProcessorStub{},
RelayedTxV3Processor: &processMocks.RelayedTxV3ProcessorMock{},
InnerTxsHashesHolder: &processMocks.TransactionHashesHolderMock{},
}

return transaction.NewTxProcessor(argsNewTxProcessor)
Expand Down Expand Up @@ -917,6 +918,7 @@ func CreateTxProcessorWithOneSCExecutorWithVMs(
GuardianChecker: guardianChecker,
TxLogsProcessor: logProc,
RelayedTxV3Processor: &processMocks.RelayedTxV3ProcessorMock{},
InnerTxsHashesHolder: &processMocks.TransactionHashesHolderMock{},
}
txProcessor, err := transaction.NewTxProcessor(argsNewTxProcessor)
if err != nil {
Expand Down
1 change: 1 addition & 0 deletions integrationTests/vm/wasm/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -424,6 +424,7 @@ func (context *TestContext) initTxProcessorWithOneSCExecutorWithVMs() {
GuardianChecker: &guardianMocks.GuardedAccountHandlerStub{},
TxLogsProcessor: context.TxLogsProcessor,
RelayedTxV3Processor: &processMocks.RelayedTxV3ProcessorMock{},
InnerTxsHashesHolder: &processMocks.TransactionHashesHolderMock{},
}

context.TxProcessor, err = processTransaction.NewTxProcessor(argsNewTxProcessor)
Expand Down
1 change: 1 addition & 0 deletions integrationTests/vm/wasm/wasmvm/wasmVM_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -648,6 +648,7 @@ func TestExecuteTransactionAndTimeToProcessChange(t *testing.T) {
EnableEpochsHandler: &enableEpochsHandlerMock.EnableEpochsHandlerStub{},
TxLogsProcessor: &mock.TxLogsProcessorStub{},
RelayedTxV3Processor: &processMocks.RelayedTxV3ProcessorMock{},
InnerTxsHashesHolder: &processMocks.TransactionHashesHolderMock{},
}
txProc, _ := processTransaction.NewTxProcessor(argsNewTxProcessor)

Expand Down
9 changes: 7 additions & 2 deletions node/external/transactionAPI/apiTransactionProcessor.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ func (atp *apiTransactionProcessor) GetSCRsByTxHash(txHash string, scrHash strin
return nil, fmt.Errorf("cannot return smat contract results: %w", ErrDBLookExtensionIsNotEnabled)
}

miniblockMetadata, err := atp.historyRepository.GetMiniblockMetadataByTxHash(decodedScrHash)
miniblockMetadata, _, err := atp.historyRepository.GetMiniblockMetadataByTxHash(decodedScrHash)
if err != nil {
return nil, fmt.Errorf("%s: %w", ErrTransactionNotFound.Error(), err)
}
Expand Down Expand Up @@ -514,7 +514,7 @@ func (atp *apiTransactionProcessor) computeTimestampForRound(round uint64) int64
}

func (atp *apiTransactionProcessor) lookupHistoricalTransaction(hash []byte, withResults bool) (*transaction.ApiTransactionResult, error) {
miniblockMetadata, err := atp.historyRepository.GetMiniblockMetadataByTxHash(hash)
miniblockMetadata, parentTxHash, err := atp.historyRepository.GetMiniblockMetadataByTxHash(hash)
if err != nil {
return nil, fmt.Errorf("%s: %w", ErrTransactionNotFound.Error(), err)
}
Expand All @@ -538,6 +538,11 @@ func (atp *apiTransactionProcessor) lookupHistoricalTransaction(hash []byte, wit
return nil, fmt.Errorf("%s: %w", ErrCannotRetrieveTransaction.Error(), err)
}

if len(parentTxHash) > 0 {
tx.OriginalTransactionHash = hex.EncodeToString(parentTxHash)
Copy link
Contributor

Choose a reason for hiding this comment

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

you are doing twice the same encoding

tx.PreviousTransactionHash = hex.EncodeToString(parentTxHash)
}

putMiniblockFieldsInTransaction(tx, miniblockMetadata)
tx.Timestamp = atp.computeTimestampForRound(tx.Round)
statusComputer, err := txstatus.NewStatusComputer(atp.shardCoordinator.SelfId(), atp.uint64ByteSliceConverter, atp.storageService)
Expand Down
20 changes: 10 additions & 10 deletions node/external/transactionAPI/apiTransactionProcessor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -328,8 +328,8 @@ func TestNode_GetSCRs(t *testing.T) {
}

historyRepo := &dblookupextMock.HistoryRepositoryStub{
GetMiniblockMetadataByTxHashCalled: func(hash []byte) (*dblookupext.MiniblockMetadata, error) {
return &dblookupext.MiniblockMetadata{}, nil
GetMiniblockMetadataByTxHashCalled: func(hash []byte) (*dblookupext.MiniblockMetadata, []byte, error) {
return &dblookupext.MiniblockMetadata{}, nil, nil
},
GetEventsHashesByTxHashCalled: func(hash []byte, epoch uint32) (*dblookupext.ResultsHashesByTxHash, error) {
return resultHashesByTxHash, nil
Expand Down Expand Up @@ -538,8 +538,8 @@ func TestNode_GetTransactionWithResultsFromStorage(t *testing.T) {
}

historyRepo := &dblookupextMock.HistoryRepositoryStub{
GetMiniblockMetadataByTxHashCalled: func(hash []byte) (*dblookupext.MiniblockMetadata, error) {
return &dblookupext.MiniblockMetadata{}, nil
GetMiniblockMetadataByTxHashCalled: func(hash []byte) (*dblookupext.MiniblockMetadata, []byte, error) {
return &dblookupext.MiniblockMetadata{}, nil, nil
},
GetEventsHashesByTxHashCalled: func(hash []byte, epoch uint32) (*dblookupext.ResultsHashesByTxHash, error) {
return resultHashesByTxHash, nil
Expand Down Expand Up @@ -711,8 +711,8 @@ func TestNode_lookupHistoricalTransaction(t *testing.T) {
require.Equal(t, transaction.TxStatusSuccess, actualG.Status)

// Missing transaction
historyRepo.GetMiniblockMetadataByTxHashCalled = func(hash []byte) (*dblookupext.MiniblockMetadata, error) {
return nil, fmt.Errorf("fooError")
historyRepo.GetMiniblockMetadataByTxHashCalled = func(hash []byte) (*dblookupext.MiniblockMetadata, []byte, error) {
return nil, nil, fmt.Errorf("fooError")
}
tx, err := n.GetTransaction(hex.EncodeToString([]byte("g")), false)
require.Nil(t, tx)
Expand All @@ -722,8 +722,8 @@ func TestNode_lookupHistoricalTransaction(t *testing.T) {

// Badly serialized transaction
_ = chainStorer.Transactions.Put([]byte("badly-serialized"), []byte("this isn't good"))
historyRepo.GetMiniblockMetadataByTxHashCalled = func(hash []byte) (*dblookupext.MiniblockMetadata, error) {
return &dblookupext.MiniblockMetadata{}, nil
historyRepo.GetMiniblockMetadataByTxHashCalled = func(hash []byte) (*dblookupext.MiniblockMetadata, []byte, error) {
return &dblookupext.MiniblockMetadata{}, nil, nil
}
tx, err = n.GetTransaction(hex.EncodeToString([]byte("badly-serialized")), false)
require.NotNil(t, err)
Expand Down Expand Up @@ -1178,15 +1178,15 @@ func setupGetMiniblockMetadataByTxHash(
headerHash []byte,
headerNonce uint64,
) {
historyRepo.GetMiniblockMetadataByTxHashCalled = func(hash []byte) (*dblookupext.MiniblockMetadata, error) {
historyRepo.GetMiniblockMetadataByTxHashCalled = func(hash []byte) (*dblookupext.MiniblockMetadata, []byte, error) {
return &dblookupext.MiniblockMetadata{
Type: int32(blockType),
SourceShardID: sourceShard,
DestinationShardID: destinationShard,
Epoch: epoch,
HeaderNonce: headerNonce,
HeaderHash: headerHash,
}, nil
}, nil, nil
}
}

Expand Down
Loading
Loading