@@ -9,6 +9,7 @@ use std::time::Instant;
9
9
10
10
use anyhow:: anyhow;
11
11
use anyhow:: Error ;
12
+ use graph:: components:: store:: GetScope ;
12
13
use graph:: slog:: SendSyncRefUnwindSafeKV ;
13
14
use never:: Never ;
14
15
use semver:: Version ;
@@ -535,6 +536,13 @@ impl<C: Blockchain> WasmInstance<C> {
535
536
id,
536
537
field
537
538
) ;
539
+ link ! (
540
+ "store.get_in_block" ,
541
+ store_get_in_block,
542
+ "host_export_store_get_in_block" ,
543
+ entity,
544
+ id
545
+ ) ;
538
546
link ! (
539
547
"store.set" ,
540
548
store_set,
@@ -910,6 +918,71 @@ impl<C: Blockchain> WasmInstanceContext<C> {
910
918
experimental_features,
911
919
} )
912
920
}
921
+
922
+ fn store_get_scoped (
923
+ & mut self ,
924
+ gas : & GasCounter ,
925
+ entity_ptr : AscPtr < AscString > ,
926
+ id_ptr : AscPtr < AscString > ,
927
+ scope : GetScope ,
928
+ ) -> Result < AscPtr < AscEntity > , HostExportError > {
929
+ let _timer = self
930
+ . host_metrics
931
+ . cheap_clone ( )
932
+ . time_host_fn_execution_region ( "store_get" ) ;
933
+
934
+ let entity_type: String = asc_get ( self , entity_ptr, gas) ?;
935
+ let id: String = asc_get ( self , id_ptr, gas) ?;
936
+ let entity_option = self . ctx . host_exports . store_get (
937
+ & mut self . ctx . state ,
938
+ entity_type. clone ( ) ,
939
+ id. clone ( ) ,
940
+ gas,
941
+ scope,
942
+ ) ?;
943
+
944
+ if self . ctx . instrument {
945
+ debug ! ( self . ctx. logger, "store_get" ;
946
+ "type" => & entity_type,
947
+ "id" => & id,
948
+ "found" => entity_option. is_some( ) ) ;
949
+ }
950
+
951
+ let ret = match entity_option {
952
+ Some ( entity) => {
953
+ let _section = self
954
+ . host_metrics
955
+ . stopwatch
956
+ . start_section ( "store_get_asc_new" ) ;
957
+ asc_new ( self , & entity. sorted ( ) , gas) ?
958
+ }
959
+ None => match & self . ctx . debug_fork {
960
+ Some ( fork) => {
961
+ let entity_option = fork. fetch ( entity_type, id) . map_err ( |e| {
962
+ HostExportError :: Unknown ( anyhow ! (
963
+ "store_get: failed to fetch entity from the debug fork: {}" ,
964
+ e
965
+ ) )
966
+ } ) ?;
967
+ match entity_option {
968
+ Some ( entity) => {
969
+ let _section = self
970
+ . host_metrics
971
+ . stopwatch
972
+ . start_section ( "store_get_asc_new" ) ;
973
+ let entity = asc_new ( self , & entity. sorted ( ) , gas) ?;
974
+ self . store_set ( gas, entity_ptr, id_ptr, entity) ?;
975
+ entity
976
+ }
977
+ None => AscPtr :: null ( ) ,
978
+ }
979
+ }
980
+ None => AscPtr :: null ( ) ,
981
+ } ,
982
+ } ;
983
+
984
+ Ok ( ret)
985
+ }
913
986
}
914
987
915
988
// Implementation of externals.
@@ -1012,59 +1085,17 @@ impl<C: Blockchain> WasmInstanceContext<C> {
1012
1085
entity_ptr : AscPtr < AscString > ,
1013
1086
id_ptr : AscPtr < AscString > ,
1014
1087
) -> Result < AscPtr < AscEntity > , HostExportError > {
1015
- let _timer = self
1016
- . host_metrics
1017
- . cheap_clone ( )
1018
- . time_host_fn_execution_region ( "store_get" ) ;
1019
-
1020
- let entity_type: String = asc_get ( self , entity_ptr, gas) ?;
1021
- let id: String = asc_get ( self , id_ptr, gas) ?;
1022
- let entity_option = self . ctx . host_exports . store_get (
1023
- & mut self . ctx . state ,
1024
- entity_type. clone ( ) ,
1025
- id. clone ( ) ,
1026
- gas,
1027
- ) ?;
1028
- if self . ctx . instrument {
1029
- debug ! ( self . ctx. logger, "store_get" ;
1030
- "type" => & entity_type,
1031
- "id" => & id,
1032
- "found" => entity_option. is_some( ) ) ;
1033
- }
1034
- let ret = match entity_option {
1035
- Some ( entity) => {
1036
- let _section = self
1037
- . host_metrics
1038
- . stopwatch
1039
- . start_section ( "store_get_asc_new" ) ;
1040
- asc_new ( self , & entity. sorted ( ) , gas) ?
1041
- }
1042
- None => match & self . ctx . debug_fork {
1043
- Some ( fork) => {
1044
- let entity_option = fork. fetch ( entity_type, id) . map_err ( |e| {
1045
- HostExportError :: Unknown ( anyhow ! (
1046
- "store_get: failed to fetch entity from the debug fork: {}" ,
1047
- e
1048
- ) )
1049
- } ) ?;
1050
- match entity_option {
1051
- Some ( entity) => {
1052
- let _section = self
1053
- . host_metrics
1054
- . stopwatch
1055
- . start_section ( "store_get_asc_new" ) ;
1056
- let entity = asc_new ( self , & entity. sorted ( ) , gas) ?;
1057
- self . store_set ( gas, entity_ptr, id_ptr, entity) ?;
1058
- entity
1059
- }
1060
- None => AscPtr :: null ( ) ,
1061
- }
1062
- }
1063
- None => AscPtr :: null ( ) ,
1064
- } ,
1065
- } ;
1088
+ self . store_get_scoped ( gas, entity_ptr, id_ptr, GetScope :: Store )
1089
+ }
1066
1090
1067
- Ok ( ret)
1091
+ /// function store.get_in_block(entity: string, id: string): Entity | null
1092
+ pub fn store_get_in_block (
1093
+ & mut self ,
1094
+ gas : & GasCounter ,
1095
+ entity_ptr : AscPtr < AscString > ,
1096
+ id_ptr : AscPtr < AscString > ,
1097
+ ) -> Result < AscPtr < AscEntity > , HostExportError > {
1098
+ self . store_get_scoped ( gas, entity_ptr, id_ptr, GetScope :: InBlock )
1068
1099
}
1069
1100
1070
1101
/// function store.loadRelated(entity_type: string, id: string, field: string): Array<Entity>
0 commit comments