Repository navigation
feat(vm): optimize resource processing (TIP-945) #7069
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
yanghang8612
wants to merge
10
commits into
feature/tip-944-voting-power
Choose a base branch
from
feature/tip-945-resource-processing
base: feature/tip-944-voting-power
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+1,390
−5
Open
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
a53fbf3
feat(vm): optimize vote validation and stake weights
yanghang8612 008bb69
test(vm): cover opcode guards and consecutive stake transfers
yanghang8612 206e0f1
fix(vm): reject internal votes with mismatched array lengths
yanghang8612 8e3da43
test(vm): preserve pre-existing global stake weight offsets
yanghang8612 1b6053c
fix(vm): persist restored energy window when usage is zero
yanghang8612 5775747
test(vm): cover zero-energy frozen energy reservation
yanghang8612 ae000d1
fix(vm): count fractional stake energy weight
yanghang8612 198963e
test(vm): cover fractional stake energy weight
yanghang8612 2851378
fix(vm): harden resource execution and key hashing
yanghang8612 6529814
fix(vm): release precompile execution references
yanghang8612 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
82 changes: 82 additions & 0 deletions
82
actuator/src/test/java/org/tron/core/vm/program/ProgramPrecompileCacheTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,82 @@ | ||
| package org.tron.core.vm.program; | ||
|
|
||
| import static org.junit.Assert.assertEquals; | ||
| import static org.junit.Assert.assertFalse; | ||
| import static org.junit.Assert.assertSame; | ||
| import static org.junit.Assert.assertTrue; | ||
| import static org.mockito.Mockito.mock; | ||
| import static org.mockito.Mockito.when; | ||
|
|
||
| import java.util.Map; | ||
| import org.apache.commons.collections4.map.LRUMap; | ||
| import org.junit.After; | ||
| import org.junit.Test; | ||
| import org.tron.common.runtime.InternalTransaction; | ||
| import org.tron.common.runtime.vm.DataWord; | ||
| import org.tron.core.vm.config.VMConfig; | ||
| import org.tron.core.vm.program.invoke.ProgramInvoke; | ||
| import org.tron.core.vm.repository.Key; | ||
| import org.tron.core.vm.repository.Repository; | ||
|
|
||
| public class ProgramPrecompileCacheTest { | ||
|
|
||
| @After | ||
| public void tearDown() { | ||
| VMConfig.clearLocalSnapshot(); | ||
| } | ||
|
|
||
| @Test | ||
| public void cacheCanRecompileAcrossProposalActivationAndRollback() { | ||
| for (boolean enabled : new boolean[]{false, true, false, true}) { | ||
| setResourceOptimization(enabled); | ||
| ProgramPrecompile compiled = program().getProgramPrecompile(); | ||
| assertTrue(compiled.hasJumpDest(0)); | ||
| assertTrue(compiled.hasJumpDest(3)); | ||
| assertFalse(compiled.hasJumpDest(1)); | ||
| assertFalse(compiled.hasJumpDest(2)); | ||
| assertFalse(compiled.hasJumpDest(4)); | ||
| assertSame(compiled, program().getProgramPrecompile()); | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| public void lruEvictsEntriesWithOldHashesWithoutExceedingCapacity() { | ||
| LRUMap<Key, Integer> cache = new LRUMap<>(4); | ||
| for (boolean enabled : new boolean[]{false, true, false, true}) { | ||
| setResourceOptimization(enabled); | ||
| // Replace every entry after each switch, including entries from the previous hash mode. | ||
| for (int value = 0; value < 16; value++) { | ||
| cache.put(cacheKey(value), value); | ||
| assertEquals(Integer.valueOf(value), cache.get(cacheKey(value))); | ||
| assertTrue(cache.size() <= 4); | ||
| } | ||
| assertEquals(4, cache.size()); | ||
| for (Map.Entry<Key, Integer> entry : cache.entrySet()) { | ||
| assertEquals(entry.getValue(), cache.get(entry.getKey(), false)); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private static Program program() { | ||
| byte[] address = new byte[21]; | ||
| address[0] = 0x41; | ||
| address[20] = 0x5a; | ||
| ProgramInvoke invoke = mock(ProgramInvoke.class); | ||
| when(invoke.getContractAddress()).thenReturn(new DataWord(address)); | ||
| when(invoke.getDeposit()).thenReturn(mock(Repository.class)); | ||
| return new Program(new byte[]{0x5b, 0x60, 0x5b, 0x5b, 0}, address, invoke, | ||
| mock(InternalTransaction.class)); | ||
| } | ||
|
|
||
| private static Key cacheKey(int value) { | ||
| byte[] bytes = new byte[53]; | ||
| bytes[52] = (byte) value; | ||
| return Key.create(bytes); | ||
| } | ||
|
|
||
| private static void setResourceOptimization(boolean enabled) { | ||
| VMConfig.Snapshot snapshot = new VMConfig.Snapshot(); | ||
| snapshot.allowTvmResourceOptimization = enabled; | ||
| VMConfig.setLocalSnapshot(snapshot); | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using operation-local before/after weights keeps sequential transfers consistent with per-account rounding.