Skip to content

Commit

Permalink
Merge pull request #4894 from onflow/gregor/script-execution/get-block-2
Browse files Browse the repository at this point in the history
[Access] Allow get blocks script calls
  • Loading branch information
peterargue authored Oct 30, 2023
2 parents 107fa71 + dc17022 commit d86b4f4
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
4 changes: 4 additions & 0 deletions module/execution/scripts.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"errors"
"fmt"

"github.com/onflow/flow-go/fvm/environment"

"github.com/rs/zerolog"

"github.com/onflow/flow-go/engine/execution/computation"
Expand Down Expand Up @@ -70,6 +72,8 @@ func NewScripts(
vm := fvm.NewVirtualMachine()

options := computation.DefaultFVMOptions(chainID, false, false)
blocks := environment.NewBlockFinder(header)
options = append(options, fvm.WithBlocks(blocks)) // add blocks for getBlocks calls in scripts
vmCtx := fvm.NewContext(options...)

derivedChainData, err := derived.NewDerivedChainData(derived.DefaultDerivedDataCacheSize)
Expand Down
24 changes: 24 additions & 0 deletions module/execution/scripts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,30 @@ func Test_ExecuteScript(t *testing.T) {
assert.Equal(t, number, value.(cadence.Int).Value.Int64())
})

t.Run("Get Block", func(t *testing.T) {
blockchain := unittest.BlockchainFixture(10)
first := blockchain[0]
tree := bootstrapFVM()

scripts := newScripts(
t,
newBlockHeadersStorage(blockchain),
treeToRegisterAdapter(tree),
)

code := []byte(fmt.Sprintf(`pub fun main(): UInt64 {
getBlock(at: %d)!
return getCurrentBlock().height
}`, first.Header.Height))

result, err := scripts.ExecuteAtBlockHeight(context.Background(), code, nil, first.Header.Height)
require.NoError(t, err)
val, err := jsoncdc.Decode(nil, result)
require.NoError(t, err)
// make sure that the returned block height matches the current one set
assert.Equal(t, first.Header.Height, val.(cadence.UInt64).ToGoValue())
})

t.Run("Handle not found Register", func(t *testing.T) {
blockchain := unittest.BlockchainFixture(10)
first := blockchain[0]
Expand Down

0 comments on commit d86b4f4

Please sign in to comment.