Skip to content

Commit

Permalink
[release] [DFI 456] get data rest (#154)
Browse files Browse the repository at this point in the history
* [DFI-456] VM mod: getData CLI/REST middleware processing added, unnecessary path check removed from getData REST

* [DFI-456] VM mod: REST test restricting non-0x1 addr requests removed

* [DFI-456] VM mod: DS context update added to BeginBlocker (fixes invalid Middlewares data)

* [DFI-456] VM mod: double address to LibraAddr convert removed for CLI/REST
  • Loading branch information
Mikhail Kornilov authored Jul 2, 2020
1 parent 72b2772 commit 7203ecb
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 31 deletions.
15 changes: 0 additions & 15 deletions app/rest_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
package app

import (
"encoding/hex"
"fmt"
"io/ioutil"
"net/http"
Expand Down Expand Up @@ -449,20 +448,6 @@ func Test_VMRest(t *testing.T) {
req, _ := ct.RestQueryVMGetData(writeSet.Address, "non-valid-path")
req.CheckFailed(http.StatusUnprocessableEntity, nil)
}

// restricted path
{
path := writeSet.Path

pathBytes, err := hex.DecodeString(path)
require.NoError(t, err, "decoding path HEX string")

pathBytes[0] = 0x1
path = hex.EncodeToString(pathBytes)

req, _ := ct.RestQueryVMGetData(writeSet.Address, path)
req.CheckFailed(http.StatusUnprocessableEntity, nil)
}
}
}
}
Expand Down
2 changes: 2 additions & 0 deletions x/vm/abci.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import (
func BeginBlocker(ctx sdk.Context, keeper Keeper, _ abci.RequestBeginBlock) {
logger := keeper.Logger(ctx)

keeper.SetDSContext(ctx)

keeper.IterateProposalsQueue(ctx, func(id uint64, pProposal PlannedProposal) {
if !pProposal.GetPlan().ShouldExecute(ctx) {
return
Expand Down
4 changes: 1 addition & 3 deletions x/vm/client/cli/queries.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,6 @@ func GetData(queryRoute string, cdc *codec.Codec) *cobra.Command {
if err != nil {
return fmt.Errorf("can't parse address: %s\n, check address format, it could be libra hex or bech32", rawAddress)
}

address = common_vm.Bech32ToLibra(address)
}

path, err := hex.DecodeString(args[1])
Expand All @@ -115,7 +113,7 @@ func GetData(queryRoute string, cdc *codec.Codec) *cobra.Command {
}

bz, err := cdc.MarshalJSON(types.QueryAccessPath{
Address: address,
Address: common_vm.Bech32ToLibra(address),
Path: path,
})
if err != nil {
Expand Down
12 changes: 1 addition & 11 deletions x/vm/client/rest/rest.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,6 @@ func getData(cliCtx context.CLIContext) http.HandlerFunc {
)
return
}

address = common_vm.Bech32ToLibra(address)
}

path, err := hex.DecodeString(rawPath)
Expand All @@ -119,17 +117,9 @@ func getData(cliCtx context.CLIContext) http.HandlerFunc {
)
return
}
if len(path) > 0 && path[0] != 0x0 {
rest.WriteErrorResponse(
w,
http.StatusUnprocessableEntity,
fmt.Sprintf("path %q: first byte must be 0x0", rawPath),
)
return
}

bz, err := cliCtx.Codec.MarshalJSON(types.QueryAccessPath{
Address: address,
Address: common_vm.Bech32ToLibra(address),
Path: path,
})
if err != nil {
Expand Down
15 changes: 15 additions & 0 deletions x/vm/internal/keeper/keeper_vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,21 @@ func (keeper Keeper) GetValue(ctx sdk.Context, accessPath *vm_grpc.VMAccessPath)
return keeper.getValue(ctx, accessPath)
}

// GetValue with middleware context-dependant processing.
func (keeper Keeper) GetValueWithMiddlewares(ctx sdk.Context, accessPath *vm_grpc.VMAccessPath) []byte {
for _, f := range keeper.dsServer.dataMiddlewares {
data, err := f(ctx, accessPath)
if err != nil {
return nil
}
if data != nil {
return data
}
}

return keeper.GetValue(ctx, accessPath)
}

// Public set value.
func (keeper Keeper) SetValue(ctx sdk.Context, accessPath *vm_grpc.VMAccessPath, value []byte) {
keeper.setValue(ctx, accessPath, value)
Expand Down
3 changes: 1 addition & 2 deletions x/vm/querier.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,11 @@ func NewQuerier(vmKeeper Keeper) sdk.Querier {
// Processing query to get value from DS.
func queryGetValue(ctx sdk.Context, vmKeeper Keeper, req abci.RequestQuery) ([]byte, error) {
var queryAccessPath QueryAccessPath

if err := ModuleCdc.UnmarshalJSON(req.Data, &queryAccessPath); err != nil {
return nil, sdkErrors.Wrap(ErrInternal, "unknown query")
}

return vmKeeper.GetValue(ctx, &vm_grpc.VMAccessPath{
return vmKeeper.GetValueWithMiddlewares(ctx, &vm_grpc.VMAccessPath{
Address: queryAccessPath.Address,
Path: queryAccessPath.Path,
}), nil
Expand Down

0 comments on commit 7203ecb

Please sign in to comment.