diff --git a/src/blockchain_utils.erl b/src/blockchain_utils.erl index 5ef88f4d32..a9a9ce07c8 100644 --- a/src/blockchain_utils.erl +++ b/src/blockchain_utils.erl @@ -878,14 +878,20 @@ get_vars(VarList, Ledger) -> VarsNonce :: non_neg_integer(), Ledger :: blockchain_ledger_v1:ledger()) -> {ok, any()} | {error, any()}. get_var_(VarName, HasAux, VarsNonce, Ledger) -> - Cache = persistent_term:get(?var_cache), - cream:cache( - Cache, - {HasAux, VarsNonce, VarName}, - fun() -> - get_var_(VarName, Ledger) - end - ). + case get('__speculative_absorb') of + true -> + %% do not update the cache + get_var_(VarName, Ledger); + _ -> + Cache = persistent_term:get(?var_cache), + cream:cache( + Cache, + {HasAux, VarsNonce, VarName}, + fun() -> + get_var_(VarName, Ledger) + end + ) + end. -spec get_var(VarName :: atom(), Ledger :: blockchain_ledger_v1:ledger()) -> {ok, any()} | {error, any()}. get_var(VarName, Ledger) -> diff --git a/src/region/blockchain_region_v1.erl b/src/region/blockchain_region_v1.erl index fbfa2559b9..a329e680c5 100644 --- a/src/region/blockchain_region_v1.erl +++ b/src/region/blockchain_region_v1.erl @@ -73,24 +73,31 @@ h3_to_region(H3, Ledger, RegionBins) -> Res = polyfill_resolution(Ledger), HasAux = blockchain_ledger_v1:has_aux(Ledger), Parent = h3:parent(H3, Res), - Cache = persistent_term:get(?region_cache), - cream:cache( - Cache, - {HasAux, VarsNonce, Parent}, - fun() -> - MaybeBins = - case RegionBins of - no_prefetch -> get_all_region_bins(Ledger); - {error, _} = Err -> Err; - B -> {ok, B} - end, - case MaybeBins of - {ok, Bins} -> - h3_to_region_(Parent, Bins); - {error, _} = Error -> Error - end - end - ). + LookupFun = fun() -> + MaybeBins = + case RegionBins of + no_prefetch -> get_all_region_bins(Ledger); + {error, _} = Err -> Err; + B -> {ok, B} + end, + case MaybeBins of + {ok, Bins} -> + h3_to_region_(Parent, Bins); + {error, _} = Error -> Error + end + end, + %% don't contaminate the cache when doing speculative absorbs + case get('__speculative_absorb') of + true -> + LookupFun(); + _ -> + Cache = persistent_term:get(?region_cache), + cream:cache( + Cache, + {HasAux, VarsNonce, Parent}, + LookupFun + ) + end. -spec h3_in_region( H3 :: h3:h3_index(), diff --git a/src/transactions/blockchain_txn_mgr.erl b/src/transactions/blockchain_txn_mgr.erl index 84802b2d5f..952674eead 100644 --- a/src/transactions/blockchain_txn_mgr.erl +++ b/src/transactions/blockchain_txn_mgr.erl @@ -206,6 +206,8 @@ init(Args) -> Tabs end, ok = blockchain_event:add_handler(self()), + %% indicate this process does speculative absorbs to determine txn validity + put('__speculative_absorb', true), {ok, #state{txn_cache = TxnCache, rejections_deferred = []}}. handle_cast({set_chain, Chain}, State=#state{chain = undefined}) -> diff --git a/src/transactions/v1/blockchain_txn_vars_v1.erl b/src/transactions/v1/blockchain_txn_vars_v1.erl index cb801a7b31..5a9ef28834 100644 --- a/src/transactions/v1/blockchain_txn_vars_v1.erl +++ b/src/transactions/v1/blockchain_txn_vars_v1.erl @@ -616,18 +616,24 @@ delayed_absorb(Txn, Ledger) -> ok = blockchain_ledger_v1:master_key(Key, Ledger) end end, - blockchain_utils:teardown_var_cache(), - case blockchain_ledger_v1:mode(Ledger) of - active -> - %% we've invalidated the region cache, so prewarm it. - spawn(fun() -> - timer:sleep(30000), - blockchain_region_v1:prewarm_cache(blockchain_ledger_v1:remove_context(Ledger)) - end); + case get('__speculative_absorb') of + true -> + ok; _ -> + %% arguably this is unnecessary because the var nonce is part of the cache key + blockchain_utils:teardown_var_cache(), + case blockchain_ledger_v1:mode(Ledger) of + active -> + %% we've invalidated the region cache, so prewarm it. + spawn(fun() -> + timer:sleep(30000), + blockchain_region_v1:prewarm_cache(blockchain_ledger_v1:remove_context(Ledger)) + end); + _ -> + ok + end, ok - end, - ok. + end. sum_higher(Target, Proplist) -> sum_higher(Target, Proplist, 0).