Skip to content

Commit

Permalink
Refactoring: run on-chain views by run_script_view endpoint (#956)
Browse files Browse the repository at this point in the history
* Refactoring: run on-chain views by run_script_view endpoint

* Fix: Dependabot issue
  • Loading branch information
aopoltorzhicky authored Jan 7, 2023
1 parent 975ab92 commit fa7e36f
Show file tree
Hide file tree
Showing 15 changed files with 95 additions and 87 deletions.
3 changes: 1 addition & 2 deletions configs/development.yml
Original file line number Diff line number Diff line change
Expand Up @@ -71,12 +71,11 @@ api:
indexer:
project_name: indexer
sentry_enabled: false
skip_delegator_blocks: true
networks:
mainnet:
receiver_threads: 5
ghostnet:
receiver_threads: 5
receiver_threads: 10
limanet:
receiver_threads: 10
connections:
Expand Down
1 change: 0 additions & 1 deletion configs/production.yml
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@ api:
indexer:
project_name: indexer
sentry_enabled: true
skip_delegator_blocks: false
networks:
mainnet:
receiver_threads: ${MAINNET_THREADS:-1}
Expand Down
1 change: 0 additions & 1 deletion configs/sandbox.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ api:
indexer:
project_name: indexer
sentry_enabled: false
skip_delegator_blocks: false
networks:
sandboxnet:
receiver_threads: 5
Expand Down
1 change: 0 additions & 1 deletion docs/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,6 @@ Indexer service settings.
indexer:
project_name: indexer
sentry_enabled: true
skip_delegator_blocks: false
networks:
mainnet:
receiver_threads: ${MAINNET_THREADS:-1}
Expand Down
12 changes: 6 additions & 6 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ require (
github.com/swaggo/swag v1.8.4
github.com/tidwall/gjson v1.14.3
github.com/yhirose/go-peg v0.0.0-20210804202551-de25d6753cf1
golang.org/x/crypto v0.0.0-20220829220503-c86fa9a7ed90
golang.org/x/crypto v0.5.0
golang.org/x/time v0.0.0-20220722155302-e5dcc9cfc0b9
gopkg.in/yaml.v3 v3.0.1
)
Expand Down Expand Up @@ -90,13 +90,13 @@ require (
github.com/vmihailenco/msgpack/v5 v5.3.5 // indirect
github.com/vmihailenco/tagparser v0.1.2 // indirect
github.com/vmihailenco/tagparser/v2 v2.0.0 // indirect
golang.org/x/net v0.0.0-20220906165146-f3363e06e74c // indirect
golang.org/x/sys v0.0.0-20220907062415-87db552b00fd // indirect
golang.org/x/term v0.0.0-20220722155259-a9ba230a4035 // indirect
golang.org/x/text v0.3.7 // indirect
golang.org/x/net v0.5.0 // indirect
golang.org/x/sys v0.4.0 // indirect
golang.org/x/term v0.4.0 // indirect
golang.org/x/text v0.6.0 // indirect
golang.org/x/tools v0.1.12 // indirect
google.golang.org/protobuf v1.28.1 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
lukechampine.com/blake3 v1.1.6 // indirect
mellium.im/sasl v0.3.0 // indirect
mellium.im/sasl v0.3.1 // indirect
)
12 changes: 12 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,8 @@ golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5y
golang.org/x/crypto v0.0.0-20211215153901-e495a2d5b3d3/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
golang.org/x/crypto v0.0.0-20220829220503-c86fa9a7ed90 h1:Y/gsMcFOcR+6S6f3YeMKl5g+dZMEWqcz5Czj/GWYbkM=
golang.org/x/crypto v0.0.0-20220829220503-c86fa9a7ed90/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
golang.org/x/crypto v0.5.0 h1:U/0M97KRkSFvyD/3FSmdP5W5swImpNgle/EHFhOsQPE=
golang.org/x/crypto v0.5.0/go.mod h1:NK/OQwhpMQP3MwtdjgLlYHnH9ebylxKWv3e0fK+mkQU=
golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE=
golang.org/x/lint v0.0.0-20190227174305-5b3e6a55c961/go.mod h1:wehouNa3lNwaWXcvxsM5YxQ5yQlVC4a0KAMCusXpPoU=
Expand All @@ -299,6 +301,8 @@ golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2/go.mod h1:9nx3DQGgdP8bBQD5qx
golang.org/x/net v0.0.0-20220127200216-cd36cc0744dd/go.mod h1:CfG3xpIq0wQ8r1q4Su4UZFWDARRcnwPjda9FqA0JpMk=
golang.org/x/net v0.0.0-20220906165146-f3363e06e74c h1:yKufUcDwucU5urd+50/Opbt4AYpqthk7wHpHok8f1lo=
golang.org/x/net v0.0.0-20220906165146-f3363e06e74c/go.mod h1:YDH+HFinaLZZlnHAfSS6ZXJJ9M9t4Dl22yv3iI2vPwk=
golang.org/x/net v0.5.0 h1:GyT4nK/YDHSqa1c4753ouYCDajOYKTja9Xb/OHtgvSw=
golang.org/x/net v0.5.0/go.mod h1:DivGGAXEgPSlEBzxGzZI+ZLohi+xUj054jfeKui00ws=
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20181108010431-42b317875d0f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
Expand Down Expand Up @@ -332,16 +336,22 @@ golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBc
golang.org/x/sys v0.0.0-20220829200755-d48e67d00261/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220907062415-87db552b00fd h1:AZeIEzg+8RCELJYq8w+ODLVxFgLMMigSwO/ffKPEd9U=
golang.org/x/sys v0.0.0-20220907062415-87db552b00fd/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.4.0 h1:Zr2JFtRQNX3BCZ8YtxRE9hNJYC8J6I1MVbMg6owUp18=
golang.org/x/sys v0.4.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/term v0.0.0-20220722155259-a9ba230a4035 h1:Q5284mrmYTpACcm+eAKjKJH48BBwSyfJqmmGDTtT8Vc=
golang.org/x/term v0.0.0-20220722155259-a9ba230a4035/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/term v0.4.0 h1:O7UWfv5+A2qiuulQk30kVinPoMtoIPeVaKLEgLpVkvg=
golang.org/x/term v0.4.0/go.mod h1:9P2UbLfCdcvo3p/nzKvsmas4TnlujnuoV9hGgYzW1lQ=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.7 h1:olpwvP2KacW1ZWvsR7uQhoyTYvKAupfQrRGBFM352Gk=
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
golang.org/x/text v0.6.0 h1:3XmdazWV+ubf7QgHSTWeykHOci5oeekaGJBLkrkaw4k=
golang.org/x/text v0.6.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
golang.org/x/time v0.0.0-20220722155302-e5dcc9cfc0b9 h1:ftMN5LMiBFjbzleLqtoBZk7KdJwhuybIU+FckUHgoyQ=
golang.org/x/time v0.0.0-20220722155302-e5dcc9cfc0b9/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
Expand Down Expand Up @@ -407,3 +417,5 @@ lukechampine.com/blake3 v1.1.6/go.mod h1:tkKEOtDkNtklkXtLNEOGNq5tcV90tJiA1vAA12R
mellium.im/sasl v0.2.1/go.mod h1:ROaEDLQNuf9vjKqE1SrAfnsobm2YKXT1gnN1uDp1PjQ=
mellium.im/sasl v0.3.0 h1:0qoaTCTo5Py7u/g0cBIQZcMOgG/5LM71nshbXwznBh8=
mellium.im/sasl v0.3.0/go.mod h1:xm59PUYpZHhgQ9ZqoJ5QaCqzWMi8IeS49dhp6plPCzw=
mellium.im/sasl v0.3.1 h1:wE0LW6g7U83vhvxjC1IY8DnXM+EU095yeo8XClvCdfo=
mellium.im/sasl v0.3.1/go.mod h1:xm59PUYpZHhgQ9ZqoJ5QaCqzWMi8IeS49dhp6plPCzw=
1 change: 1 addition & 0 deletions internal/noderpc/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ type INode interface {
RunCode(context.Context, []byte, []byte, []byte, string, string, string, string, string, int64, int64) (RunCodeResponse, error)
RunOperation(context.Context, string, string, string, string, int64, int64, int64, int64, int64, []byte) (OperationGroup, error)
RunOperationLight(context.Context, string, string, string, string, int64, int64, int64, int64, int64, []byte) (LightOperationGroup, error)
RunScriptView(ctx context.Context, request RunScriptViewRequest) ([]byte, error)
GetCounter(context.Context, string) (int64, error)
GetBigMapType(ctx context.Context, ptr, level int64) (BigMap, error)
GetBlockMetadata(ctx context.Context, level int64) (metadata Metadata, err error)
Expand Down
15 changes: 15 additions & 0 deletions internal/noderpc/mock.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions internal/noderpc/pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -288,3 +288,12 @@ func (p Pool) GetRawScript(ctx context.Context, address string, level int64) ([]
}
return data.Interface().([]byte), nil
}

// RunScriptView -
func (p Pool) RunScriptView(ctx context.Context, request RunScriptViewRequest) ([]byte, error) {
data, err := p.call("RunScriptView", ctx, request)
if err != nil {
return nil, err
}
return data.Interface().([]byte), nil
}
21 changes: 21 additions & 0 deletions internal/noderpc/requests.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,24 @@ type runOperationItemContent struct {
Amount int64 `json:"amount,string"`
Parameters stdJSON.RawMessage `json:"parameters"`
}

// RunScriptViewRequest -
type RunScriptViewRequest struct {
ChainID string `json:"chain_id"`
Contract string `json:"contract"`
View string `json:"view"`
Input stdJSON.RawMessage `json:"input"`
Source string `json:"source,omitempty"`
Payer string `json:"payer,omitempty"`
Gas int64 `json:"gas,string,omitempty"`
UnparsingMode UnparsingMode `json:"unparsing_mode"`
}

// UnparsingMode -
type UnparsingMode string

const (
UnparsingModeReadable UnparsingMode = "Readable"
UnparsingModeOptimized UnparsingMode = "Optimized"
UnparsingModeOptimizedLegacy UnparsingMode = "Optimized_legacy"
)
5 changes: 5 additions & 0 deletions internal/noderpc/responses.go
Original file line number Diff line number Diff line change
Expand Up @@ -416,3 +416,8 @@ type TicketUpdateItem struct {
Account string `json:"account"`
Amount string `json:"amount"`
}

// RunScriptViewResponse -
type RunScriptViewResponse struct {
Data stdJSON.RawMessage `json:"data"`
}
7 changes: 7 additions & 0 deletions internal/noderpc/rpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,13 @@ func (rpc *NodeRPC) RunOperationLight(ctx context.Context, chainID, branch, sour
return
}

// RunScriptView -
func (rpc *NodeRPC) RunScriptView(ctx context.Context, request RunScriptViewRequest) ([]byte, error) {
var response RunScriptViewResponse
err := rpc.post(ctx, "chains/main/blocks/head/helpers/scripts/run_script_view", request, true, &response)
return response.Data, err
}

// GetCounter -
func (rpc *NodeRPC) GetCounter(ctx context.Context, address string) (int64, error) {
var counter string
Expand Down
39 changes: 1 addition & 38 deletions internal/tzkt/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func NewTzKT(host string, timeout time.Duration) *TzKT {
}
}

//nolint
// nolint
func (t *TzKT) request(method, endpoint string, params map[string]string, response interface{}) error {
uri := helpers.URLJoin(t.Host, endpoint)

Expand Down Expand Up @@ -98,22 +98,6 @@ func (t *TzKT) GetAccounts(kind string, page, limit int64) (resp []Account, err
return
}

// GetContractOperationBlocks -
func (t *TzKT) GetContractOperationBlocks(offset, limit int64, needSmartContracts, needDelegators bool) (resp []int64, err error) {
params := map[string]string{}
if limit == 0 {
limit = 10000
}

params["limit"] = fmt.Sprintf("%d", limit)
params["offset.cr"] = fmt.Sprintf("%d", offset)
params["smartContracts"] = fmt.Sprintf("%v", needSmartContracts)
params["delegatorContracts"] = fmt.Sprintf("%v", needDelegators)

err = t.request("GET", "blocks/levels", params, &resp)
return
}

// GetAliases - returns aliases map in format map[address]alias
func (t *TzKT) GetAliases() (map[string]string, error) {
params := map[string]string{}
Expand Down Expand Up @@ -158,27 +142,6 @@ func sanitize(alias string) string {
return strings.ReplaceAll(alias, "'", "’")
}

// GetAllContractOperationBlocks -
func (t *TzKT) GetAllContractOperationBlocks() ([]int64, error) {
offset := int64(0)
resp := make([]int64, 0)
end := false
for !end {
levels, err := t.GetContractOperationBlocks(offset, 0, true, true)
if err != nil {
return nil, err
}
for i := range levels {
resp = append(resp, levels[i])
if i == len(levels)-1 {
offset = levels[i]
}
}
end = len(levels) < 10000
}
return resp, nil
}

// GetProtocols -
func (t *TzKT) GetProtocols() (resp []Protocol, err error) {
err = t.request("GET", "protocols", nil, &resp)
Expand Down
53 changes: 16 additions & 37 deletions internal/views/on-chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,53 +45,32 @@ func (onChain *OnChain) UnmarshalJSON(data []byte) error {
return nil
}

func (onChain *OnChain) buildCode(storageType []byte) ([]byte, error) {
var script bytes.Buffer
script.WriteString(`[{"prim":"parameter","args":[{"prim":"pair","args":[`)
script.Write(onChain.Parameter)
script.WriteString(`,{"prim":"address"}]}]},{"prim":"storage","args":[{"prim":"option","args":[`)
script.Write(onChain.ReturnType)
script.WriteString(`]}]},{"prim":"code","args":[[{"prim":"CAR"},{"prim":"UNPAIR"},{"prim":"VIEW","args":[{"string":"`)
script.WriteString(onChain.ViewName())
script.WriteString(`"},`)
script.Write(onChain.ReturnType)
script.WriteString(`]},{"prim":"NIL","args":[{"prim":"operation"}]},{"prim":"PAIR"}]]}]`)
return script.Bytes(), nil
}

func (onChain *OnChain) buildParameter(address, parameter string, storageValue []byte) ([]byte, error) {
var script bytes.Buffer
script.WriteString(`{"prim":"Pair","args":[`)
script.WriteString(parameter)
script.WriteString(`,{"string":"`)
script.WriteString(address)
script.WriteString(`"}]}`)
return script.Bytes(), nil
}

// Return -
func (onChain *OnChain) Return() []byte {
return onChain.ReturnType
}

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

code, err := onChain.buildCode(nil)
response, err := rpc.RunScriptView(ctx, noderpc.RunScriptViewRequest{
ChainID: args.ChainID,
Contract: args.Contract,
View: onChain.ViewName(),
Input: stdJSON.RawMessage(args.Parameters),
Source: args.Source,
Payer: args.Initiator,
Gas: args.HardGasLimitPerOperation,
UnparsingMode: noderpc.UnparsingModeReadable,
})
if err != nil {
return nil, err
}

storage := []byte(`{"prim": "None"}`)

response, err := rpc.RunCode(ctx, code, storage, parameter, args.ChainID, args.Source, args.Initiator, "", args.Protocol, args.Amount, args.HardGasLimitPerOperation)
if err != nil {
return nil, err
}
var buf bytes.Buffer
buf.WriteString(`{"args":[`)
buf.Write(response)
buf.WriteByte(']')
buf.WriteByte('}')

return response.Storage, nil
return buf.Bytes(), nil
}
2 changes: 1 addition & 1 deletion version.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"name": "bcdhub",
"version": "4.2.1"
"version": "4.4.0"
}

0 comments on commit fa7e36f

Please sign in to comment.