github.com/koko1123/flow-go-1@v0.29.6/engine/access/rest/scripts.go (about)

     1  package rest
     2  
     3  import (
     4  	"github.com/koko1123/flow-go-1/engine/access/rest/models"
     5  	"github.com/koko1123/flow-go-1/engine/access/rest/request"
     6  	"github.com/koko1123/flow-go-1/model/flow"
     7  
     8  	"github.com/koko1123/flow-go-1/access"
     9  )
    10  
    11  // ExecuteScript handler sends the script from the request to be executed.
    12  func ExecuteScript(r *request.Request, backend access.API, _ models.LinkGenerator) (interface{}, error) {
    13  	req, err := r.GetScriptRequest()
    14  	if err != nil {
    15  		return nil, NewBadRequestError(err)
    16  	}
    17  
    18  	if req.BlockID != flow.ZeroID {
    19  		return backend.ExecuteScriptAtBlockID(r.Context(), req.BlockID, req.Script.Source, req.Script.Args)
    20  	}
    21  
    22  	// default to sealed height
    23  	if req.BlockHeight == request.SealedHeight || req.BlockHeight == request.EmptyHeight {
    24  		return backend.ExecuteScriptAtLatestBlock(r.Context(), req.Script.Source, req.Script.Args)
    25  	}
    26  
    27  	if req.BlockHeight == request.FinalHeight {
    28  		finalBlock, _, err := backend.GetLatestBlockHeader(r.Context(), false)
    29  		if err != nil {
    30  			return nil, err
    31  		}
    32  		req.BlockHeight = finalBlock.Height
    33  	}
    34  
    35  	return backend.ExecuteScriptAtBlockHeight(r.Context(), req.BlockHeight, req.Script.Source, req.Script.Args)
    36  }