-
Notifications
You must be signed in to change notification settings - Fork 179
/
scripts.go
71 lines (60 loc) · 1.76 KB
/
scripts.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package rest
import (
"fmt"
"github.com/onflow/flow-go/access"
"github.com/onflow/flow-go/engine/access/rest/generated"
)
// executeScript handler sends the script from the request to be executed.
func executeScript(r *request, backend access.API, _ LinkGenerator) (interface{}, error) {
blockID := r.getQueryParam("block_id")
blockHeight := r.getQueryParam("block_height")
// validate block ID and block height is provided
if blockID == "" && blockHeight == "" {
return nil, NewBadRequestError(fmt.Errorf("either block ID or block height must be provided"))
}
if blockID != "" && blockHeight != "" {
return nil, NewBadRequestError(fmt.Errorf("can not provide both block ID and block height"))
}
// parse body to script
var scriptBody generated.ScriptsBody
err := r.bodyAs(&scriptBody)
if err != nil {
return nil, err
}
args, err := toScriptArgs(scriptBody)
if err != nil {
return nil, NewBadRequestError(err)
}
code, err := toScriptSource(scriptBody)
if err != nil {
return nil, NewBadRequestError(err)
}
if blockID != "" {
id, err := toID(blockID)
if err != nil {
return nil, NewBadRequestError(err)
}
return backend.ExecuteScriptAtBlockID(r.Context(), id, code, args)
}
if blockHeight == sealedHeightQueryParam {
result, err := backend.ExecuteScriptAtLatestBlock(r.Context(), code, args)
if err != nil {
return nil, err
}
return result, nil
}
var height uint64
if blockHeight == finalHeightQueryParam {
finalBlock, err := backend.GetLatestBlockHeader(r.Context(), false)
if err != nil {
return nil, err
}
height = finalBlock.Height
} else {
height, err = toHeight(blockHeight)
if err != nil {
return nil, NewBadRequestError(err)
}
}
return backend.ExecuteScriptAtBlockHeight(r.Context(), height, code, args)
}