Closed Maelkum closed 3 years ago
Just as a note, we do not need to implement a separate model for that. We just change validation to infer the latest block when both are empty instead of erroring, and inject the index reader into the validator.
Got it. We could also potentially just do it in the API code.. Something like:
func (d *Data) Balance(ctx echo.Context) error {
var req BalanceRequest
err := ctx.Bind(&req)
if err != nil {
return echo.NewHTTPError(http.StatusBadRequest, invalidEncoding(invalidJSON, err))
}
// ...
rosBlockID := req.BlockID
if rosBlockID.Index == nil && rosBlockID.Hash == "" {
current, _, err := d.retrieve.Current()
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, internal(balancesRetrieval, err))
}
rosBlockID = current
}
In the rest of the function we'd just refer to the rosBlockID
instead of the req.BlockID
. That way the validator can still be agnostic of the index and I feel there's less "magic" when reading the code.
This was done in #385.
Certain endpoints specify as input the PartialBlockIdentifier. In this case, omitting the index and hash of a block is not an error, but an intent to retrieve the latest/current block.