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

Fixed recreate trie in sc query service #5983

Merged
merged 3 commits into from
Feb 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
Copy link
Collaborator

Choose a reason for hiding this comment

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

All right to be a constant here, for all practical purposes 👍


// 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) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Assuming this is not a happy & common case, all good.

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 actually used at genesis time

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