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

Deep VM queries: deep-history observer vs. regular observer #6005

Merged
merged 10 commits into from
Mar 5, 2024
31 changes: 27 additions & 4 deletions process/smartContract/scQueryService.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ 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 All @@ -53,6 +52,7 @@ type SCQueryService struct {
marshaller marshal.Marshalizer
hasher hashing.Hasher
uint64ByteSliceConverter typeConverters.Uint64ByteSliceConverter
latestQueriedEpoch core.OptionalUint32
}

// ArgsNewSCQueryService defines the arguments needed for the sc query service
Expand Down Expand Up @@ -103,6 +103,7 @@ func NewSCQueryService(
marshaller: args.Marshaller,
hasher: args.Hasher,
uint64ByteSliceConverter: args.Uint64ByteSliceConverter,
latestQueriedEpoch: core.OptionalUint32{},
}, nil
}

Expand Down Expand Up @@ -255,14 +256,36 @@ func (service *SCQueryService) recreateTrie(blockRootHash []byte, blockHeader da
}

accountsAdapter := service.blockChainHook.GetAccountsAdapter()
if blockHeader.GetEpoch()+epochDifferenceToConsiderHistory >= service.getCurrentEpoch() {

if service.isLatestQueriedEpoch(blockHeader.GetEpoch()) {
logQueryService.Trace("calling RecreateTrie, for recent history", "block", blockHeader.GetNonce(), "rootHash", blockRootHash)
return accountsAdapter.RecreateTrie(blockRootHash)

err := accountsAdapter.RecreateTrie(blockRootHash)
if err != nil {
return err
}

service.rememberQueriedEpoch(blockHeader.GetEpoch())
Copy link
Contributor

Choose a reason for hiding this comment

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

missing return here?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Indeed, fixed.

}

logQueryService.Trace("calling RecreateTrieFromEpoch, for older history", "block", blockHeader.GetNonce(), "rootHash", blockRootHash)
holder := holders.NewRootHashHolder(blockRootHash, core.OptionalUint32{Value: blockHeader.GetEpoch(), HasValue: true})
return accountsAdapter.RecreateTrieFromEpoch(holder)

err := accountsAdapter.RecreateTrieFromEpoch(holder)
if err != nil {
return err
}

service.rememberQueriedEpoch(blockHeader.GetEpoch())
return err
Copy link
Contributor

Choose a reason for hiding this comment

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

return nil to be more obvious

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yes, fixed.

}

func (service *SCQueryService) isLatestQueriedEpoch(epoch uint32) bool {
return service.latestQueriedEpoch.HasValue && service.latestQueriedEpoch.Value == epoch
}

func (service *SCQueryService) rememberQueriedEpoch(epoch uint32) {
service.latestQueriedEpoch = core.OptionalUint32{Value: epoch, HasValue: true}
}

func (service *SCQueryService) getCurrentEpoch() uint32 {
Expand Down