optakt / flow-dps

Flow Data Provisioning Service
Apache License 2.0
29 stars 13 forks source link

Rosetta: Block identifier can omit both index and hash #379

Closed Maelkum closed 3 years ago

Maelkum commented 3 years ago

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.

awfm9 commented 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.

Maelkum commented 3 years ago

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.

awfm9 commented 3 years ago

This was done in #385.