Skip to content

Commit

Permalink
jsonrpc/usersvc: use best block height/time in view call (#1094)
Browse files Browse the repository at this point in the history
  • Loading branch information
jchappelow authored Nov 26, 2024
1 parent d892d2e commit 19ac757
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
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

0 comments on commit 19ac757

Please sign in to comment.