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

jsonrpc/usersvc: use best block height/time in view call #1094

Merged
merged 1 commit into from
Nov 26, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion common/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ type BlockContext struct {
ChainContext *ChainContext
// Height gets the height of the current block.
Height int64
// Timestamp is a timestamp of the current block.
// Timestamp is a timestamp of the current block, in seconds (UNIX epoch).
// It is set by the block proposer, and therefore may not be accurate.
// It should not be used for time-sensitive operations where incorrect
// timestamps could result in security vulnerabilities.
Expand Down
14 changes: 12 additions & 2 deletions internal/services/jsonrpc/usersvc/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -705,7 +705,6 @@ func (svc *Service) Call(ctx context.Context, req *userjson.CallRequest) (*userj
if err != nil {
// NOTE: http api needs to be able to get the error message
return nil, jsonrpc.NewError(jsonrpc.ErrorInvalidParams, "failed to convert action call: "+err.Error(), nil)

}

// Authenticate by validating the challenge was server-issued, and verify
Expand Down Expand Up @@ -752,6 +751,16 @@ func (svc *Service) Call(ctx context.Context, req *userjson.CallRequest) (*userj
}
}

chainStat, err := svc.chainClient.Status(ctx)
if err != nil {
return nil, jsonrpc.NewError(jsonrpc.ErrorNodeInternal, "failed to get chain status: "+err.Error(), nil)
}
height, stamp := chainStat.Sync.BestBlockHeight, chainStat.Sync.BestBlockTime.Unix()
if chainStat.Sync.Syncing { // don't use known stale height and time stamp if node is syncing
height = -1
stamp = -1
}

ctxExec, cancel := context.WithTimeout(ctx, svc.readTxTimeout)
defer cancel()

Expand Down Expand Up @@ -800,7 +809,8 @@ func (svc *Service) Call(ctx context.Context, req *userjson.CallRequest) (*userj
Signer: signer,
Caller: caller,
BlockContext: &common.BlockContext{
Height: -1, // cannot know the height here.
Height: height,
Timestamp: stamp,
},
Authenticator: msg.AuthType,
}, readTx, &common.ExecutionData{
Expand Down
Loading