Skip to content

Commit f6ad137

Browse files
committed
GH-1441 Only remove code from caches if created in the same block, otherwise wait for LIB
1 parent 2a890fc commit f6ad137

File tree

6 files changed

+34
-15
lines changed

6 files changed

+34
-15
lines changed

libraries/chain/controller.cpp

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4920,8 +4920,10 @@ struct controller_impl {
49204920
return wasmif;
49214921
}
49224922

4923-
void code_block_num_last_used(const digest_type& code_hash, uint8_t vm_type, uint8_t vm_version, uint32_t block_num) {
4924-
wasmif.code_block_num_last_used(code_hash, vm_type, vm_version, block_num);
4923+
void code_block_num_last_used(const digest_type& code_hash, uint8_t vm_type, uint8_t vm_version,
4924+
block_num_type first_used_block_num, block_num_type block_num_last_used)
4925+
{
4926+
wasmif.code_block_num_last_used(code_hash, vm_type, vm_version, first_used_block_num, block_num_last_used);
49254927
}
49264928

49274929
void set_node_finalizer_keys(const bls_pub_priv_key_map_t& finalizer_keys) {
@@ -6127,8 +6129,9 @@ bool controller::is_write_window() const {
61276129
return my->is_write_window();
61286130
}
61296131

6130-
void controller::code_block_num_last_used(const digest_type& code_hash, uint8_t vm_type, uint8_t vm_version, uint32_t block_num) {
6131-
return my->code_block_num_last_used(code_hash, vm_type, vm_version, block_num);
6132+
void controller::code_block_num_last_used(const digest_type& code_hash, uint8_t vm_type, uint8_t vm_version,
6133+
block_num_type first_used_block_num, block_num_type block_num_last_used) {
6134+
return my->code_block_num_last_used(code_hash, vm_type, vm_version, first_used_block_num, block_num_last_used);
61326135
}
61336136

61346137
void controller::set_node_finalizer_keys(const bls_pub_priv_key_map_t& finalizer_keys) {

libraries/chain/eosio_contract.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,8 @@ void apply_eosio_setcode(apply_context& context) {
160160
old_size = (int64_t)old_code_entry.code.size() * config::setcode_ram_bytes_multiplier;
161161
if( old_code_entry.code_ref_count == 1 ) {
162162
db.remove(old_code_entry);
163-
context.control.code_block_num_last_used(account.code_hash, account.vm_type, account.vm_version, context.control.head().block_num() + 1);
163+
context.control.code_block_num_last_used(account.code_hash, account.vm_type, account.vm_version,
164+
old_code_entry.first_block_used, context.control.head().block_num() + 1);
164165
} else {
165166
db.modify(old_code_entry, [](code_object& o) {
166167
--o.code_ref_count;

libraries/chain/include/eosio/chain/controller.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -489,7 +489,8 @@ namespace eosio::chain {
489489
void set_to_write_window();
490490
void set_to_read_window();
491491
bool is_write_window() const;
492-
void code_block_num_last_used(const digest_type& code_hash, uint8_t vm_type, uint8_t vm_version, uint32_t block_num);
492+
void code_block_num_last_used(const digest_type& code_hash, uint8_t vm_type, uint8_t vm_version,
493+
block_num_type first_used_block_num, block_num_type block_num_last_used);
493494
void set_node_finalizer_keys(const bls_pub_priv_key_map_t& finalizer_keys);
494495

495496
// is the bls key a registered finalizer key of this node, thread safe

libraries/chain/include/eosio/chain/wasm_interface.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,8 @@ namespace eosio { namespace chain {
6868
static void validate(const controller& control, const bytes& code);
6969

7070
//indicate that a particular code probably won't be used after given block_num
71-
void code_block_num_last_used(const digest_type& code_hash, const uint8_t& vm_type, const uint8_t& vm_version, const uint32_t& block_num);
71+
void code_block_num_last_used(const digest_type& code_hash, uint8_t vm_type, uint8_t vm_version,
72+
block_num_type first_used_block_num, block_num_type last_used_block_num);
7273

7374
//indicate the current LIB. evicts old cache entries
7475
void current_lib(const uint32_t lib);

libraries/chain/include/eosio/chain/wasm_interface_private.hpp

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -207,19 +207,30 @@ struct eosvmoc_tier {
207207
return it != wasm_instantiation_cache.end();
208208
}
209209

210-
void code_block_num_last_used(const digest_type& code_hash, const uint8_t& vm_type, const uint8_t& vm_version, const uint32_t& block_num) {
210+
void code_block_num_last_used(const digest_type& code_hash, uint8_t vm_type, uint8_t vm_version,
211+
block_num_type first_used_block_num, block_num_type block_num_last_used)
212+
{
211213
// The caller of this method apply_eosio_setcode has asserted that
212214
// the transaction is not read-only, implying we are
213215
// in write window. Read-only threads are not running.
214216
// Safe to update the cache without locking.
215217
wasm_cache_index::iterator it = wasm_instantiation_cache.find(boost::make_tuple(code_hash, vm_type, vm_version));
216-
if(it != wasm_instantiation_cache.end())
217-
wasm_instantiation_cache.modify(it, [block_num](wasm_cache_entry& e) {
218-
e.last_block_num_used = block_num;
219-
});
218+
if(it != wasm_instantiation_cache.end()) {
219+
if (first_used_block_num == block_num_last_used) {
220+
// First used and no longer needed in the same block, erase immediately, do not wait for LIB.
221+
// Since created and destroyed in the same block, likely will not be needed in a forked block.
222+
// Prevents many setcodes in the same block using up space in the cache.
223+
wasm_instantiation_cache.erase(it);
224+
} else {
225+
wasm_instantiation_cache.modify(it, [block_num_last_used](wasm_cache_entry& e) {
226+
e.last_block_num_used = block_num_last_used;
227+
});
228+
}
229+
}
220230

221231
#ifdef EOSIO_EOS_VM_OC_RUNTIME_ENABLED
222-
if (eosvmoc)
232+
// see comment above
233+
if (first_used_block_num == block_num_last_used && eosvmoc)
223234
eosvmoc->cc.free_code(code_hash, vm_version);
224235
#endif
225236
}

libraries/chain/wasm_interface.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,10 @@ namespace eosio { namespace chain {
7777
//Hard: Kick off instantiation in a separate thread at this location
7878
}
7979

80-
void wasm_interface::code_block_num_last_used(const digest_type& code_hash, const uint8_t& vm_type, const uint8_t& vm_version, const uint32_t& block_num) {
81-
my->code_block_num_last_used(code_hash, vm_type, vm_version, block_num);
80+
void wasm_interface::code_block_num_last_used(const digest_type& code_hash, uint8_t vm_type, uint8_t vm_version,
81+
block_num_type first_used_block_num, block_num_type block_num_last_used)
82+
{
83+
my->code_block_num_last_used(code_hash, vm_type, vm_version, first_used_block_num, block_num_last_used);
8284
}
8385

8486
void wasm_interface::current_lib(const uint32_t lib) {

0 commit comments

Comments
 (0)