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

Proposer optimisation #6186

Merged
merged 12 commits into from
Jul 4, 2024
Merged

Proposer optimisation #6186

merged 12 commits into from
Jul 4, 2024

Conversation

AdoAdoAdo
Copy link
Contributor

@AdoAdoAdo AdoAdoAdo commented May 20, 2024

Reasoning behind the pull request

There is some degradation on the proposer during execution, which does not happen on the validators that vote the proposed block

Proposed changes

The process that takes most of this time is the management of generated smart contract results and their registration in the intermediate processors, that will be used afterwards in the case of reverts.
This will be refactored and optimized.

Testing procedure

  • run a testnet with multiple smart contract calls and monitor the processing time for proposers.
  • compare processing time between proposers and validators - these should be closer in time spent on processing the same block than it is now (some differences are still expected when there are many transactions, due to the initial filtering which happens on block proposal and not on block validation, but these should be limited to <100ms).

Pre-requisites

Based on the Contributing Guidelines the PR author and the reviewers must check the following requirements are met:

  • was the PR targeted to the correct branch?
  • if this is a larger feature that probably needs more than one PR, is there a feat branch created?
  • if this is a feat branch merging, do all satellite projects have a proper tag inside go.mod?

gabi-vuls
gabi-vuls previously approved these changes May 24, 2024
Copy link
Collaborator

@gabi-vuls gabi-vuls left a comment

Choose a reason for hiding this comment

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

Normal allin test: 1.7.11-f7efd0fc48 -> proposer-optimisation-e105bd9b53

--- Specific errors ---

block hash does not match 249
wrong nonce in block 215
miniblocks does not match 0
num miniblocks does not match 0
miniblock hash does not match 0
block bodies does not match 0
receipts hash missmatch 0

/------/

--- Statistics ---

Nr. of all ERRORS: 1
Nr. of all WARNS: 252
Nr. of new ERRORS: 1
Nr. of new WARNS: 0
Nr. of PANICS: 0

/------/

--- ERRORS ---

/------/

@AdoAdoAdo AdoAdoAdo marked this pull request as ready for review June 4, 2024 07:50
@BeniaminDrasovean BeniaminDrasovean self-requested a review June 5, 2024 09:03
@andreibancioiu andreibancioiu self-requested a review June 5, 2024 09:03
@@ -170,7 +171,7 @@ type TransactionCoordinator interface {
VerifyCreatedBlockTransactions(hdr data.HeaderHandler, body *block.Body) error
GetCreatedInShardMiniBlocks() []*block.MiniBlock
VerifyCreatedMiniBlocks(hdr data.HeaderHandler, body *block.Body) error
AddIntermediateTransactions(mapSCRs map[block.Type][]data.TransactionHandler) error
AddIntermediateTransactions(mapSCRs map[block.Type][]data.TransactionHandler, key []byte) error
Copy link
Collaborator

@andreibancioiu andreibancioiu Jun 5, 2024

Choose a reason for hiding this comment

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

Maybe rename key to originalTransactionHash, to be more explicit?

Later edit: comment can be ignored, since key may have a different meaning.

bpp.mapProcessedResult[key] = append(bpp.mapProcessedResult[key], txHash)
pr, ok := bpp.mapProcessedResult[string(key)]
if !ok {
return
Copy link
Collaborator

Choose a reason for hiding this comment

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

An extra confirmation - this is a valid case?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Normally should not exist, the intermediate results are added on a key that was previously registered, before starting the execution of the tx that generated the result.

Now the exception comes in case of scheduled txs, where the results are from previous block but are added on the next block and do not have any previously registered key (they don't need one as they don't need to revert).

In this case key comes as nil and it would not be found in the mapProcessedResult.

Copy link
Contributor

Choose a reason for hiding this comment

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

Wouldn't be better to check for key == nil in that case ?

@@ -28,6 +29,14 @@ type txInfo struct {
*txShardInfo
}

type processedResult struct {
Copy link
Collaborator

@andreibancioiu andreibancioiu Jun 5, 2024

Choose a reason for hiding this comment

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

Even is not exported, perhaps we can add some comments for each field, e.g. "the hash of the parent tx", "a lookup map with hashes of children txs" or something similar (since this part of the code is so important).

Copy link
Contributor

Choose a reason for hiding this comment

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

yes. a little bit more explicit names could be added.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

renamed:
parent -> parentKey
children -> childrenKeys

the keys we are currently using are indeed the hashes, but could be anything.


func (bpp *basePostProcessor) removeProcessedResultsAndLinks(key string) ([][]byte, bool) {
processedResults, ok := bpp.mapProcessedResult[key]
if !ok {
Copy link
Collaborator

Choose a reason for hiding this comment

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

This case is for transactions without results?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

right, also for blocks without miniblocks - valid case, miniblocks without transactions (hopefully we don't have this case but should be fine even if there are), or transactions without scrs - valid case.

@sasurobert sasurobert self-requested a review June 6, 2024 08:55
@@ -28,6 +29,14 @@ type txInfo struct {
*txShardInfo
}

type processedResult struct {
Copy link
Contributor

Choose a reason for hiding this comment

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

yes. a little bit more explicit names could be added.

@@ -237,7 +238,7 @@ func (irp *intermediateResultsProcessor) VerifyInterMiniBlocks(body *block.Body)
}

// AddIntermediateTransactions adds smart contract results from smart contract processing for cross-shard calls
func (irp *intermediateResultsProcessor) AddIntermediateTransactions(txs []data.TransactionHandler) error {
func (irp *intermediateResultsProcessor) AddIntermediateTransactions(txs []data.TransactionHandler, key []byte) error {
Copy link
Contributor

Choose a reason for hiding this comment

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

key is too generic - add a name here - maybe miniBlockHash ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

this can be different things, such as a txhash, a scr hash or nil in case of scheduled txs results


bpp.mapProcessedResult[string(key)] = pr

if parentKey != nil {
Copy link
Contributor

Choose a reason for hiding this comment

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

len(parentKey) > 0 ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

@@ -28,6 +29,14 @@ type txInfo struct {
*txShardInfo
}

type processedResult struct {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

renamed:
parent -> parentKey
children -> childrenKeys

the keys we are currently using are indeed the hashes, but could be anything.


func (bpp *basePostProcessor) removeProcessedResultsAndLinks(key string) ([][]byte, bool) {
processedResults, ok := bpp.mapProcessedResult[key]
if !ok {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

right, also for blocks without miniblocks - valid case, miniblocks without transactions (hopefully we don't have this case but should be fine even if there are), or transactions without scrs - valid case.


bpp.mapProcessedResult[string(key)] = pr

if parentKey != nil {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

bpp.mapProcessedResult[key] = append(bpp.mapProcessedResult[key], txHash)
pr, ok := bpp.mapProcessedResult[string(key)]
if !ok {
return
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Normally should not exist, the intermediate results are added on a key that was previously registered, before starting the execution of the tx that generated the result.

Now the exception comes in case of scheduled txs, where the results are from previous block but are added on the next block and do not have any previously registered key (they don't need one as they don't need to revert).

In this case key comes as nil and it would not be found in the mapProcessedResult.

@@ -237,7 +238,7 @@ func (irp *intermediateResultsProcessor) VerifyInterMiniBlocks(body *block.Body)
}

// AddIntermediateTransactions adds smart contract results from smart contract processing for cross-shard calls
func (irp *intermediateResultsProcessor) AddIntermediateTransactions(txs []data.TransactionHandler) error {
func (irp *intermediateResultsProcessor) AddIntermediateTransactions(txs []data.TransactionHandler, key []byte) error {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

this can be different things, such as a txhash, a scr hash or nil in case of scheduled txs results

@@ -963,7 +964,7 @@ func (txProc *txProcessor) executeFailedRelayedUserTx(
return err
}

err = txProc.scrForwarder.AddIntermediateTransactions([]data.TransactionHandler{scrForRelayer})
err = txProc.scrForwarder.AddIntermediateTransactions([]data.TransactionHandler{scrForRelayer}, originalTxHash)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

this should be verified, if the originalTxHash or the userTxHash should be used.

Copy link
Contributor

Choose a reason for hiding this comment

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

originalTxHash has to be used. as in case of revert, the used key for this is the originalTxHash.

@@ -985,7 +986,7 @@ func (txProc *txProcessor) executeFailedRelayedUserTx(
}

if txProc.enableEpochsHandler.IsFlagEnabled(common.AddFailedRelayedTxToInvalidMBsFlag) {
err = txProc.badTxForwarder.AddIntermediateTransactions([]data.TransactionHandler{originalTx})
err = txProc.badTxForwarder.AddIntermediateTransactions([]data.TransactionHandler{originalTx}, originalTxHash)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

should be checked here as well if userTxHash should be used.

Copy link
Contributor

Choose a reason for hiding this comment

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

originalTxHash has to be used. as in case of revert, the used key for this is the originalTxHash.

@@ -890,7 +891,7 @@ func (txProc *txProcessor) processUserTx(
return returnCode, nil
}

err = txProc.scrForwarder.AddIntermediateTransactions([]data.TransactionHandler{scrFromTx})
err = txProc.scrForwarder.AddIntermediateTransactions([]data.TransactionHandler{scrFromTx}, txHash)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

txhash is for relay transaction, should be checked if here we should use the user tx hash instead.

Copy link
Contributor

Choose a reason for hiding this comment

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

originalTxHash has to be used. as in case of revert, the used key for this is the originalTxHash.

Copy link
Contributor

Choose a reason for hiding this comment

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

This could be check with an integration test as well.

@AdoAdoAdo AdoAdoAdo changed the base branch from master to rc/v1.7.next1 June 14, 2024 12:40
Copy link
Collaborator

@gabi-vuls gabi-vuls left a comment

Choose a reason for hiding this comment

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

Normal allin test: v1.7.13-dev-config-aebb55e8e0 -> proposer-optimisation-6042eaaf4e

--- Specific errors ---

block hash does not match 327
wrong nonce in block 235
miniblocks does not match 0
num miniblocks does not match 0
miniblock hash does not match 0
block bodies does not match 0
receipts hash missmatch 0

/------/

--- Statistics ---

Nr. of all ERRORS: 0
Nr. of all WARNS: 247
Nr. of new ERRORS: 0
Nr. of new WARNS: 0
Nr. of PANICS: 0

/------/

--- ERRORS ---

/------/

--- WARNINGS ---

/------/

@AdoAdoAdo AdoAdoAdo merged commit 313b62e into rc/v1.7.next1 Jul 4, 2024
8 checks passed
@AdoAdoAdo AdoAdoAdo deleted the proposer-optimisation branch July 4, 2024 08:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants