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

Optimized trie re-creation for VM Queries, adjust some logs #5977

Merged
merged 14 commits into from
Feb 23, 2024
22 changes: 6 additions & 16 deletions process/smartContract/scQueryService.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,15 @@ import (
"github.com/multiversx/mx-chain-go/process"
"github.com/multiversx/mx-chain-go/process/smartContract/scrCommon"
"github.com/multiversx/mx-chain-go/sharding"
logger "github.com/multiversx/mx-chain-logger-go"
vmcommon "github.com/multiversx/mx-chain-vm-common-go"
"github.com/multiversx/mx-chain-vm-common-go/parsers"
)

var _ process.SCQueryService = (*SCQueryService)(nil)

var logQueryService = logger.GetOrCreate("process/smartcontract.queryService")

// MaxGasLimitPerQuery - each unit is the equivalent of 1 nanosecond processing time
const MaxGasLimitPerQuery = 300_000_000_000

Expand All @@ -39,7 +42,6 @@ type SCQueryService struct {
blockChainHook process.BlockChainHookWithAccountsAdapter
mainBlockChain data.ChainHandler
apiBlockChain data.ChainHandler
numQueries int
gasForQuery uint64
wasmVMChangeLocker common.Locker
bootstrapper process.Bootstrapper
Expand Down Expand Up @@ -179,8 +181,7 @@ func (service *SCQueryService) shouldAllowQueriesExecution() bool {
}

func (service *SCQueryService) executeScCall(query *process.SCQuery, gasPrice uint64) (*vmcommon.VMOutput, common.BlockInfo, error) {
log.Trace("executeScCall", "function", query.FuncName, "numQueries", service.numQueries)
service.numQueries++
logQueryService.Trace("executeScCall", "address", query.ScAddress, "function", query.FuncName, "blockNonce", query.BlockNonce.Value, "blockHash", query.BlockHash)
Copy link
Contributor

Choose a reason for hiding this comment

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

Debate-able if L184 and L197 should be at debug level.


shouldEarlyExitBecauseOfSyncState := query.ShouldBeSynced && service.bootstrapper.GetNodeState() == common.NsNotSynchronized
if shouldEarlyExitBecauseOfSyncState {
Expand All @@ -193,6 +194,8 @@ func (service *SCQueryService) executeScCall(query *process.SCQuery, gasPrice ui
}

if len(blockRootHash) > 0 {
logQueryService.Trace("preparing execution for block and root hash", "block", blockHeader.GetNonce(), "rootHash", blockRootHash)
Copy link
Collaborator

@sstanculeanu sstanculeanu Feb 20, 2024

Choose a reason for hiding this comment

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

the check for nil blockHeader from L243 should also be done before this log

also needed for recreateTrie

Copy link
Collaborator Author

@andreibancioiu andreibancioiu Feb 20, 2024

Choose a reason for hiding this comment

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

Fixed (moved log in recreateTrie, as well). Could not reproduce this scenario using a localnet. VM queries are not available at genesis, though. Thus, added an extra simple unit test.


err = service.apiBlockChain.SetCurrentBlockHeaderAndRootHash(blockHeader, blockRootHash)
if err != nil {
return nil, nil, err
Expand Down Expand Up @@ -230,15 +233,6 @@ func (service *SCQueryService) executeScCall(query *process.SCQuery, gasPrice ui
return nil, nil, err
}

if service.hasRetriableExecutionError(vmOutput) {
log.Error("Retriable execution error detected. Will retry (once) executeScCall()", "returnCode", vmOutput.ReturnCode, "returnMessage", vmOutput.ReturnMessage)

vmOutput, err = vm.RunSmartContractCall(vmInput)
if err != nil {
return nil, nil, err
}
}

if query.SameScState {
err = service.checkForRootHashChanges(rootHashBeforeExecution)
if err != nil {
Expand Down Expand Up @@ -417,10 +411,6 @@ func (service *SCQueryService) createVMCallInput(query *process.SCQuery, gasPric
return vmContractCallInput
}

func (service *SCQueryService) hasRetriableExecutionError(vmOutput *vmcommon.VMOutput) bool {
return vmOutput.ReturnMessage == "allocation error"
}

// ComputeScCallGasLimit will estimate how many gas a transaction will consume
func (service *SCQueryService) ComputeScCallGasLimit(tx *transaction.Transaction) (uint64, error) {
argParser := parsers.NewCallArgsParser()
Expand Down
2 changes: 1 addition & 1 deletion process/smartContract/scQueryServiceDispatcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ func (sqsd *scQueryServiceDispatcher) Close() error {
for _, scQueryService := range sqsd.list {
err := scQueryService.Close()
if err != nil {
log.Error("error while closing inner SC query service in scQueryServiceDispatcher.Close", "error", err)
logQueryService.Error("error while closing inner SC query service in scQueryServiceDispatcher.Close", "error", err)
errFound = err
}
}
Expand Down
Loading