1- import { execute } from '../dist/Process' ;
1+ import { Context , execute , InMemoryCache } from '../dist/Process' ;
22import { readFileSync } from 'fs' ;
33
44jest . setTimeout ( 10_000 ) ;
55
66describe ( 'Process' , ( ) => {
7+ const memoryCache = new InMemoryCache ( ) ;
8+
79 it ( 'should be able to run a wasm file' , async ( ) => {
810 const wasm = readFileSync ( __dirname + '/wasm/cowsay.wasm' ) ;
911 const result = await execute ( {
@@ -13,22 +15,23 @@ describe('Process', () => {
1315 gasLimit : ( 300_000_000_000_000 ) . toString ( ) ,
1416 randomSeed : '0x012' ,
1517 timestamp : 1 ,
16- } ) ;
18+ } , memoryCache ) ;
1719
1820 expect ( result . gasUsed ) . toBe ( '26844206' ) ;
1921 expect ( result . code ) . toBe ( 0 ) ;
2022 } ) ;
2123
2224 it ( 'should throw a gas error when the gas limit has been exceeded' , async ( ) => {
2325 const wasm = readFileSync ( __dirname + '/wasm/cowsay.wasm' ) ;
26+
2427 const result = await execute ( {
2528 args : [ 'cowsay' , 'blah' ] ,
2629 binary : new Uint8Array ( wasm ) ,
2730 env : { } ,
2831 gasLimit : ( 21_000_000 ) . toString ( ) ,
2932 randomSeed : '0x012' ,
3033 timestamp : 1 ,
31- } ) ;
34+ } , memoryCache ) ;
3235
3336 expect ( result . code ) . toBe ( 1 ) ;
3437 expect ( result . logs [ 0 ] ) . toBe ( 'ERR_OUT_OF_GAS' ) ;
@@ -53,7 +56,7 @@ describe('Process', () => {
5356 gasLimit : ( 300_000_000_000_000 ) . toString ( ) ,
5457 randomSeed : '0x012' ,
5558 timestamp : new Date ( ) . getTime ( ) ,
56- } ) ;
59+ } , memoryCache ) ;
5760
5861 const outcome = JSON . parse ( result . logs [ result . logs . length - 1 ] ) ;
5962 expect ( outcome . value ) . toBe ( 'imposter' ) ;
@@ -82,11 +85,11 @@ describe('Process', () => {
8285 gasLimit : ( 300_000_000_000_000 ) . toString ( ) ,
8386 randomSeed : '0x012' ,
8487 timestamp : new Date ( ) . getTime ( ) ,
85- } ) ;
88+ } , memoryCache ) ;
8689
8790 const outcome = JSON . parse ( result . logs [ result . logs . length - 1 ] ) ;
8891
89- expect ( result . gasUsed ) . toBe ( '627358915 ' ) ;
92+ expect ( result . gasUsed ) . toBe ( '633190654 ' ) ;
9093 expect ( outcome . value ) . toBe ( '70500' ) ;
9194 } ) ;
9295
@@ -113,11 +116,11 @@ describe('Process', () => {
113116 gasLimit : ( 300_000_000_000_000 ) . toString ( ) ,
114117 randomSeed : '0x012' ,
115118 timestamp : new Date ( ) . getTime ( ) ,
116- } ) ;
119+ } , memoryCache ) ;
117120
118121 const outcome = JSON . parse ( result . logs [ result . logs . length - 1 ] ) ;
119122
120- expect ( result . gasUsed ) . toBe ( '627070273 ' ) ;
123+ expect ( result . gasUsed ) . toBe ( '632902012 ' ) ;
121124 expect ( outcome . value ) . toBe ( '705000000000000000000000000' ) ;
122125 } ) ;
123126
@@ -140,10 +143,42 @@ describe('Process', () => {
140143 gasLimit : ( 300_000_000_000_000 ) . toString ( ) ,
141144 randomSeed : '0x012' ,
142145 timestamp : new Date ( ) . getTime ( ) ,
143- } ) ;
146+ } , memoryCache ) ;
144147
145148 const outcome = JSON . parse ( result . logs [ result . logs . length - 1 ] ) ;
146149 expect ( result . gasUsed ) . toBe ( '23223018' ) ;
147150 expect ( outcome . value ) . toBe ( '493625367592069900000000000000' ) ;
148151 } ) ;
152+
153+ it ( 'should always use cache when available' , async ( ) => {
154+ const internalMemoryCache = new InMemoryCache ( ) ;
155+ const wasm = readFileSync ( __dirname + '/wasm/basic-fetch.wasm' ) ;
156+ const setSpy = jest . spyOn ( internalMemoryCache , 'set' ) ;
157+ const getSpy = jest . spyOn ( internalMemoryCache , 'get' ) ;
158+
159+ const context : Context = {
160+ args : [
161+ '0xdeadbeef' ,
162+ JSON . stringify ( [
163+ {
164+ end_point : 'https://api.coinpaprika.com/v1/coins/btc-bitcoin/ohlcv/historical?start=1630612481&end=1630612781' ,
165+ source_path : '$[0].close' ,
166+ } ,
167+ ] ) ,
168+ 'number' ,
169+ ( 10e24 ) . toString ( ) ,
170+ ] ,
171+ binary : new Uint8Array ( wasm ) ,
172+ env : { } ,
173+ gasLimit : ( 300_000_000_000_000 ) . toString ( ) ,
174+ randomSeed : '0x012' ,
175+ timestamp : new Date ( ) . getTime ( )
176+ } ;
177+
178+ await execute ( context , internalMemoryCache ) ;
179+ await execute ( context , internalMemoryCache ) ;
180+
181+ expect ( getSpy ) . toHaveBeenCalledTimes ( 2 ) ;
182+ expect ( setSpy ) . toHaveBeenCalledTimes ( 1 ) ;
183+ } ) ;
149184} ) ;
0 commit comments