Skip to content

Commit

Permalink
Refactoring: remove script request from view execution (#999)
Browse files Browse the repository at this point in the history
* Refactoring: remove script request from view execution

* Fix: storage type
  • Loading branch information
aopoltorzhicky authored Oct 29, 2023
1 parent e9cfc9c commit f7cc284
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 36 deletions.
30 changes: 21 additions & 9 deletions cmd/api/handlers/views.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"net/http"
"time"

"github.com/baking-bad/bcdhub/internal/bcd"
"github.com/baking-bad/bcdhub/internal/bcd/ast"
"github.com/baking-bad/bcdhub/internal/bcd/base"
"github.com/baking-bad/bcdhub/internal/bcd/consts"
Expand Down Expand Up @@ -211,7 +212,7 @@ func ExecuteView() gin.HandlerFunc {
return
}

view, parameters, err := getViewForExecute(c.Request.Context(), ctx.Contracts, ctx.Blocks, req.Address, execView)
view, parameters, err := getViewForExecute(c.Request.Context(), ctx, req.Address, execView)
if handleError(c, ctx.Storage, err, 0) {
return
}
Expand All @@ -221,7 +222,7 @@ func ExecuteView() gin.HandlerFunc {
return
}

timeoutContext, cancel := context.WithTimeout(c, 10*time.Second)
timeoutContext, cancel := context.WithTimeout(c, 20*time.Second)
defer cancel()

response, err := view.Execute(timeoutContext, ctx.RPC, views.Args{
Expand Down Expand Up @@ -267,14 +268,15 @@ func ExecuteView() gin.HandlerFunc {
}
}

func getViewForExecute(ctx context.Context, contracts contract.Repository, blocks block.Repository, address string, req executeViewRequest) (views.View, []byte, error) {
func getViewForExecute(ctx context.Context, networkContext *config.Context, address string, req executeViewRequest) (views.View, []byte, error) {
symLink, err := bcd.SymLink()
if err != nil {
return nil, nil, err
}

switch req.Kind {
case OnchainView:
block, err := blocks.Last(ctx)
if err != nil {
return nil, nil, err
}
rawViews, err := contracts.ScriptPart(ctx, address, block.Protocol.SymLink, consts.VIEWS)
rawViews, err := networkContext.Contracts.ScriptPart(ctx, address, symLink, consts.VIEWS)
if err != nil {
return nil, nil, err
}
Expand Down Expand Up @@ -328,7 +330,17 @@ func getViewForExecute(ctx context.Context, contracts contract.Repository, block
return nil, nil, err
}

return views.NewMichelsonStorageView(*req.View, req.Name), parameters, nil
storageType, err := networkContext.Contracts.ScriptPart(ctx, address, symLink, consts.STORAGE)
if err != nil {
return nil, nil, err
}

storageValue, err := getDeffattedStorage(ctx, networkContext, address, 0)
if err != nil {
return nil, nil, err
}

return views.NewMichelsonStorageView(*req.View, req.Name, storageType, storageValue), parameters, nil
default:
return nil, nil, errors.New("invalid view kind")
}
Expand Down
5 changes: 5 additions & 0 deletions internal/bcd/protocols.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,11 @@ func GetCurrentProtocol() string {
return "PtNairobiyssHuh87hEhfVBGCVrK3WnS8Z2FT4ymB5tAa4r1nQf"
}

// SymLink - returns last sym link
func SymLink() (string, error) {
return GetProtoSymLink(GetCurrentProtocol())
}

// Symbolic links
const (
SymLinkAlpha = "alpha"
Expand Down
43 changes: 21 additions & 22 deletions internal/views/michelson_storage_view.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,34 +14,42 @@ type MichelsonStorageView struct {
Code []byte
ReturnType []byte
Name string

storageType []byte
storageValue []byte
}

// NewMichelsonStorageView -
func NewMichelsonStorageView(impl contract.ViewImplementation, name string) *MichelsonStorageView {
func NewMichelsonStorageView(impl contract.ViewImplementation, name string, storageType, storageValue []byte) *MichelsonStorageView {
var parameter []byte
if !impl.MichelsonStorageView.IsParameterEmpty() {
parameter = impl.MichelsonStorageView.Parameter
}
if storageType[0] == '[' {
storageType = storageType[1 : len(storageType)-1]
}
return &MichelsonStorageView{
Parameter: parameter,
ReturnType: impl.MichelsonStorageView.ReturnType,
Code: impl.MichelsonStorageView.Code,
Name: name,
Parameter: parameter,
ReturnType: impl.MichelsonStorageView.ReturnType,
Code: impl.MichelsonStorageView.Code,
Name: name,
storageType: storageType,
storageValue: storageValue,
}
}

func (msv *MichelsonStorageView) buildCode(storageType []byte) ([]byte, error) {
func (msv *MichelsonStorageView) buildCode() ([]byte, error) {
var script bytes.Buffer
script.WriteString(`[{"prim":"parameter","args":[`)
if msv.Parameter != nil {
script.WriteString(`{"prim":"pair","args":[`)
script.Write(msv.Parameter)
script.WriteString(",")
if _, err := script.Write(storageType); err != nil {
if _, err := script.Write(msv.storageType); err != nil {
return nil, err
}
script.WriteString("]}")
} else if _, err := script.Write(storageType); err != nil {
} else if _, err := script.Write(msv.storageType); err != nil {
return nil, err
}
script.WriteString(`]},{"prim":"storage","args":[{"prim":"option","args":[`)
Expand All @@ -52,17 +60,17 @@ func (msv *MichelsonStorageView) buildCode(storageType []byte) ([]byte, error) {
return script.Bytes(), nil
}

func (msv *MichelsonStorageView) buildParameter(_, parameter string, storageValue []byte) ([]byte, error) {
func (msv *MichelsonStorageView) buildParameter(_, parameter string) ([]byte, error) {
var script bytes.Buffer
if msv.Parameter != nil {
script.WriteString(`{"prim":"Pair","args":[`)
script.WriteString(parameter)
script.WriteString(",")
if _, err := script.Write(storageValue); err != nil {
if _, err := script.Write(msv.storageValue); err != nil {
return nil, err
}
script.WriteString(`]}`)
} else if _, err := script.Write(storageValue); err != nil {
} else if _, err := script.Write(msv.storageValue); err != nil {
return nil, err
}
return script.Bytes(), nil
Expand All @@ -75,21 +83,12 @@ func (msv *MichelsonStorageView) Return() []byte {

// Execute -
func (msv *MichelsonStorageView) Execute(ctx context.Context, rpc noderpc.INode, args Args) ([]byte, error) {
script, err := rpc.GetScriptJSON(ctx, args.Contract, 0)
parameter, err := msv.buildParameter(args.Contract, args.Parameters)
if err != nil {
return nil, err
}

parameter, err := msv.buildParameter(args.Contract, args.Parameters, script.Storage)
if err != nil {
return nil, err
}

storageType, err := json.Marshal(script.Code.Storage[0])
if err != nil {
return nil, err
}
code, err := msv.buildCode(storageType)
code, err := msv.buildCode()
if err != nil {
return nil, err
}
Expand Down
11 changes: 6 additions & 5 deletions internal/views/michelson_storage_view_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,13 @@ func TestMichelsonStorageView_GetCode(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
msv := &MichelsonStorageView{
Parameter: tt.fields.Parameter,
Code: tt.fields.Code,
ReturnType: tt.fields.ReturnType,
Name: tt.fields.Name,
Parameter: tt.fields.Parameter,
Code: tt.fields.Code,
ReturnType: tt.fields.ReturnType,
Name: tt.fields.Name,
storageType: tt.storageType,
}
got, err := msv.buildCode(tt.storageType)
got, err := msv.buildCode()
if (err != nil) != tt.wantErr {
t.Errorf("MichelsonStorageView.GetCode() error = %v, wantErr %v", err, tt.wantErr)
return
Expand Down

0 comments on commit f7cc284

Please sign in to comment.