diff --git a/cmd/api/handlers/views.go b/cmd/api/handlers/views.go index 697b5f222..7591a0782 100644 --- a/cmd/api/handlers/views.go +++ b/cmd/api/handlers/views.go @@ -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" @@ -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 } @@ -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{ @@ -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 } @@ -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") } diff --git a/internal/bcd/protocols.go b/internal/bcd/protocols.go index 009338284..bde132313 100644 --- a/internal/bcd/protocols.go +++ b/internal/bcd/protocols.go @@ -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" diff --git a/internal/views/michelson_storage_view.go b/internal/views/michelson_storage_view.go index ed3e16a3a..4aea73aca 100644 --- a/internal/views/michelson_storage_view.go +++ b/internal/views/michelson_storage_view.go @@ -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":[`) @@ -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 @@ -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 } diff --git a/internal/views/michelson_storage_view_test.go b/internal/views/michelson_storage_view_test.go index 172019a77..e83da145a 100644 --- a/internal/views/michelson_storage_view_test.go +++ b/internal/views/michelson_storage_view_test.go @@ -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