Skip to content

Commit

Permalink
Merge pull request #5983 from multiversx/fix-recreate-trie-in-sc-quer…
Browse files Browse the repository at this point in the history
…y-service

Fixed recreate trie in sc query service
  • Loading branch information
andreibancioiu committed Feb 20, 2024
2 parents 13bc2e4 + 9f3d010 commit a929406
Show file tree
Hide file tree
Showing 5 changed files with 386 additions and 16 deletions.
27 changes: 23 additions & 4 deletions process/smartContract/scQueryService.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ var logQueryService = logger.GetOrCreate("process/smartcontract.queryService")

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

// SCQueryService can execute Get functions over SC to fetch stored values
type SCQueryService struct {
Expand Down Expand Up @@ -201,10 +202,7 @@ func (service *SCQueryService) executeScCall(query *process.SCQuery, gasPrice ui
return nil, nil, err
}

accountsAdapter := service.blockChainHook.GetAccountsAdapter()

holder := holders.NewRootHashHolder(blockRootHash, core.OptionalUint32{Value: blockHeader.GetEpoch(), HasValue: true})
err = accountsAdapter.RecreateTrieFromEpoch(holder)
err = service.recreateTrie(blockRootHash, blockHeader)
if err != nil {
return nil, nil, err
}
Expand Down Expand Up @@ -253,6 +251,27 @@ func (service *SCQueryService) executeScCall(query *process.SCQuery, gasPrice ui
return vmOutput, blockInfo, nil
}

func (service *SCQueryService) recreateTrie(blockRootHash []byte, blockHeader data.HeaderHandler) error {
accountsAdapter := service.blockChainHook.GetAccountsAdapter()
if blockHeader.GetEpoch()+epochDifferenceToConsiderHistory >= service.getCurrentEpoch() {
// recent history
return accountsAdapter.RecreateTrie(blockRootHash)
}

// old history, this will take a little longer
holder := holders.NewRootHashHolder(blockRootHash, core.OptionalUint32{Value: blockHeader.GetEpoch(), HasValue: true})
return accountsAdapter.RecreateTrieFromEpoch(holder)
}

func (service *SCQueryService) getCurrentEpoch() uint32 {
header := service.mainBlockChain.GetCurrentBlockHeader()
if check.IfNil(header) {
return 0
}

return header.GetEpoch()
}

// TODO: extract duplicated code with nodeBlocks.go
func (service *SCQueryService) extractBlockHeaderAndRootHash(query *process.SCQuery) (data.HeaderHandler, []byte, error) {
if len(query.BlockHash) > 0 {
Expand Down

0 comments on commit a929406

Please sign in to comment.