@@ -32,6 +32,7 @@ import {
3232 INITIAL_BALANCE_MICRO_ALGOS ,
3333} from './avm-invoker'
3434import { asUint8Array } from './util'
35+ import { Block , gloadBytes , gloadUint64 } from '../src/impl'
3536
3637describe ( 'State op codes' , async ( ) => {
3738 const ctx = new TestExecutionContext ( )
@@ -376,4 +377,65 @@ describe('State op codes', async () => {
376377 expect ( ( ) => ctx . txn . lastGroup . getScratchSlot ( 256 ) ) . toThrow ( 'invalid scratch slot' )
377378 } )
378379 } )
380+
381+ describe ( 'gloadBytes' , async ( ) => {
382+ it ( 'should return the correct field value of the scratch slot' , async ( ) => {
383+ ctx . txn . createScope ( [ ctx . any . txn . applicationCall ( { scratchSpace : [ Uint64 ( 0 ) , Bytes ( 'hello' ) , Bytes ( 'world' ) ] } ) ] ) . execute ( ( ) => {
384+ const slot1 = gloadBytes ( 0 , 1 )
385+ const slot2 = gloadBytes ( 0 , 2 )
386+ expect ( slot1 ) . toStrictEqual ( 'hello' )
387+ expect ( slot2 ) . toStrictEqual ( 'world' )
388+ } )
389+ } )
390+ it ( 'should throw error if the scratch slot is not a bytes type' , async ( ) => {
391+ ctx . txn . createScope ( [ ctx . any . txn . applicationCall ( { scratchSpace : [ Uint64 ( 0 ) , Bytes ( 'hello' ) , Bytes ( 'world' ) ] } ) ] ) . execute ( ( ) => {
392+ expect ( ( ) => gloadBytes ( 0 , 0 ) ) . toThrow ( 'invalid scratch slot type' )
393+ } )
394+ } )
395+ it ( 'should throw error if the scratch slot is out of range' , async ( ) => {
396+ ctx . txn . createScope ( [ ctx . any . txn . applicationCall ( { scratchSpace : [ Uint64 ( 0 ) , Bytes ( 'hello' ) , Bytes ( 'world' ) ] } ) ] ) . execute ( ( ) => {
397+ expect ( ( ) => gloadBytes ( 0 , 256 ) ) . toThrow ( 'invalid scratch slot' )
398+ } )
399+ } )
400+ } )
401+
402+ describe ( 'gloadUint64' , async ( ) => {
403+ it ( 'should return the correct field value of the scratch slot' , async ( ) => {
404+ ctx . txn . createScope ( [ ctx . any . txn . applicationCall ( { scratchSpace : [ Uint64 ( 7 ) , Uint64 ( 42 ) , Bytes ( 'world' ) ] } ) ] ) . execute ( ( ) => {
405+ const slot0 = gloadUint64 ( 0 , 0 )
406+ const slot1 = gloadUint64 ( 0 , 1 )
407+ expect ( slot0 ) . toStrictEqual ( 7n )
408+ expect ( slot1 ) . toStrictEqual ( 42n )
409+ } )
410+ } )
411+ it ( 'should throw error if the scratch slot is not a uint64 type' , async ( ) => {
412+ ctx . txn . createScope ( [ ctx . any . txn . applicationCall ( { scratchSpace : [ Uint64 ( 7 ) , Uint64 ( 42 ) , Bytes ( 'world' ) ] } ) ] ) . execute ( ( ) => {
413+ expect ( ( ) => gloadUint64 ( 0 , 2 ) ) . toThrow ( 'invalid scratch slot type' )
414+ } )
415+ } )
416+ it ( 'should throw error if the scratch slot is out of range' , async ( ) => {
417+ ctx . txn . createScope ( [ ctx . any . txn . applicationCall ( { scratchSpace : [ Uint64 ( 7 ) , Uint64 ( 42 ) , Bytes ( 'world' ) ] } ) ] ) . execute ( ( ) => {
418+ expect ( ( ) => gloadUint64 ( 0 , 256 ) ) . toThrow ( 'invalid scratch slot' )
419+ } )
420+ } )
421+ } )
422+
423+ describe ( 'Block' , async ( ) => {
424+ it ( 'should return the correct field value of the block' , async ( ) => {
425+ const index = 42
426+ const seed = 123
427+ const timestamp = 1234567890
428+ ctx . ledger . setBlock ( index , seed , timestamp )
429+ const seedResult = op . btoi ( Block . blkSeed ( Uint64 ( index ) ) )
430+ const timestampResult = Block . blkTimestamp ( Uint64 ( index ) )
431+
432+ expect ( seedResult ) . toEqual ( Uint64 ( seed ) )
433+ expect ( timestampResult ) . toEqual ( Uint64 ( timestamp ) )
434+ } )
435+ it ( 'should throw error if the block is not set' , async ( ) => {
436+ const index = 42
437+ expect ( ( ) => Block . blkSeed ( Uint64 ( index ) ) ) . toThrow ( 'Block 42 not set' )
438+ expect ( ( ) => Block . blkTimestamp ( Uint64 ( index ) ) ) . toThrow ( 'Block 42 not set' )
439+ } )
440+ } )
379441} )
0 commit comments