github.com/onflow/flow-go@v0.35.7-crescendo-preview.23-atree-inlining/engine/execution/scripts/engine.go (about)

     1  package scripts
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  
     7  	"github.com/rs/zerolog"
     8  
     9  	"github.com/onflow/flow-go/engine"
    10  	"github.com/onflow/flow-go/engine/execution"
    11  	"github.com/onflow/flow-go/engine/execution/computation/query"
    12  	"github.com/onflow/flow-go/engine/execution/state"
    13  	"github.com/onflow/flow-go/model/flow"
    14  )
    15  
    16  type Engine struct {
    17  	unit          *engine.Unit
    18  	log           zerolog.Logger
    19  	queryExecutor query.Executor
    20  	execState     state.ScriptExecutionState
    21  }
    22  
    23  var _ execution.ScriptExecutor = (*Engine)(nil)
    24  
    25  func New(
    26  	logger zerolog.Logger,
    27  	queryExecutor query.Executor,
    28  	execState state.ScriptExecutionState,
    29  ) *Engine {
    30  	return &Engine{
    31  		unit:          engine.NewUnit(),
    32  		log:           logger.With().Str("engine", "scripts").Logger(),
    33  		execState:     execState,
    34  		queryExecutor: queryExecutor,
    35  	}
    36  }
    37  
    38  func (e *Engine) Ready() <-chan struct{} {
    39  	return e.unit.Ready()
    40  }
    41  
    42  func (e *Engine) Done() <-chan struct{} {
    43  	return e.unit.Done()
    44  }
    45  
    46  func (e *Engine) ExecuteScriptAtBlockID(
    47  	ctx context.Context,
    48  	script []byte,
    49  	arguments [][]byte,
    50  	blockID flow.Identifier,
    51  ) ([]byte, uint64, error) {
    52  
    53  	blockSnapshot, header, err := e.execState.CreateStorageSnapshot(blockID)
    54  	if err != nil {
    55  		return nil, 0, fmt.Errorf("failed to create storage snapshot: %w", err)
    56  	}
    57  
    58  	return e.queryExecutor.ExecuteScript(
    59  		ctx,
    60  		script,
    61  		arguments,
    62  		header,
    63  		blockSnapshot)
    64  }
    65  
    66  func (e *Engine) GetRegisterAtBlockID(
    67  	ctx context.Context,
    68  	owner, key []byte,
    69  	blockID flow.Identifier,
    70  ) ([]byte, error) {
    71  
    72  	blockSnapshot, _, err := e.execState.CreateStorageSnapshot(blockID)
    73  	if err != nil {
    74  		return nil, fmt.Errorf("failed to create storage snapshot: %w", err)
    75  	}
    76  
    77  	id := flow.NewRegisterID(flow.BytesToAddress(owner), string(key))
    78  	data, err := blockSnapshot.Get(id)
    79  	if err != nil {
    80  		return nil, fmt.Errorf("failed to get the register (%s): %w", id, err)
    81  	}
    82  
    83  	return data, nil
    84  }
    85  
    86  func (e *Engine) GetAccount(
    87  	ctx context.Context,
    88  	addr flow.Address,
    89  	blockID flow.Identifier,
    90  ) (*flow.Account, error) {
    91  	blockSnapshot, header, err := e.execState.CreateStorageSnapshot(blockID)
    92  	if err != nil {
    93  		return nil, fmt.Errorf("failed to create storage snapshot: %w", err)
    94  	}
    95  
    96  	return e.queryExecutor.GetAccount(ctx, addr, header, blockSnapshot)
    97  }