Skip to content
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

state trie final fast pure latest #15

Closed
wants to merge 33 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
d086651
feat(trie): add state trie
halibobo1205 Mar 8, 2023
42b60fb
feat(trie): add trie prune tool
tomatoishealthy Feb 27, 2023
615331c
feat(trie): ignore some keys for properties db
halibobo1205 Mar 10, 2023
b2dea18
fix(rpc): keep json rpc return code compatible with prev version
tomatoishealthy Mar 10, 2023
297fd57
fix(trie): force set netty version to 4.1.27.Final
halibobo1205 Mar 10, 2023
a65eb7e
fix(json-rpc): fix compatibility of eth_call
tomatoishealthy Mar 13, 2023
d8d7671
feat(trie): optimize token10 API for jsonrpc
halibobo1205 Mar 15, 2023
c2fd11e
fix(trie): fix token10 API for jsonrpc
halibobo1205 Mar 15, 2023
1783ff4
feat(trie): small improvements for trie and jsonrpc
halibobo1205 Mar 16, 2023
2de5ad7
feat(trie): return assets in order
halibobo1205 Mar 18, 2023
aeb8c2c
feature(trie): reset root when launching a new archive node
tomatoishealthy Mar 22, 2023
33c0e0a
fix(trie): reset archive root on khaosdb
tomatoishealthy Mar 29, 2023
fce0f89
refactor(trie): optimize code, remove unused code
halibobo1205 Apr 3, 2023
ebf3a6f
feat(trie): make trie db configurable
halibobo1205 Apr 11, 2023
8c3d949
feat(trie): fix 10token read and write
halibobo1205 Apr 14, 2023
7f3e4eb
feat(trie): add stat for trie db
halibobo1205 Apr 17, 2023
f1d22ba
feat(trie): fix `getStorageAt` logic for `create2` contracts
tomatoishealthy Apr 17, 2023
53a2b19
refactor(trie): optimize code for TVM
halibobo1205 Apr 20, 2023
36373b9
refactor(trie): add switch to turn off services
halibobo1205 May 6, 2023
21ec94b
feat(trie): fix Zksnark init
halibobo1205 May 10, 2023
a06d40c
feat(trie): set writable false when p2p is disabled for state trie
halibobo1205 May 22, 2023
7650642
feat(trie): rocksdb update 7.7.3
halibobo1205 May 10, 2023
deb0b82
feat(trie): add cache for tire db
halibobo1205 May 29, 2023
3af2c73
feat(db): skip unnecessary check and init
halibobo1205 May 30, 2023
c298d15
feat(db): add metrics
halibobo1205 May 30, 2023
bb809a3
feat(trie): skip tran-cache init when p2p is disabled
halibobo1205 Jul 9, 2023
d064f99
feat(trie): add logs for jsonrpc
halibobo1205 Jul 9, 2023
7318583
feat(trie): merge master
halibobo1205 Jul 10, 2023
457075b
merge 4.7.3.1 (#17)
halibobo1205 Apr 1, 2024
612944f
feat(trie): merge 4.7.4 (#19)
halibobo1205 Apr 1, 2024
20e1983
merge master (#20)
halibobo1205 Apr 1, 2024
bd36349
Merge remote-tracking branch 'upstream/master' into feature/state-tri…
halibobo1205 Apr 1, 2024
0e363de
Merge pull request #21 from halibobo1205/feature/state-trie-final-fas…
halibobo1205 Apr 1, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 28 additions & 13 deletions actuator/src/main/java/org/tron/core/actuator/VMActuator.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
import org.tron.common.utils.StorageUtils;
import org.tron.common.utils.StringUtil;
import org.tron.common.utils.WalletUtil;
import org.tron.core.ChainBaseManager;
import org.tron.core.capsule.AccountCapsule;
import org.tron.core.capsule.BlockCapsule;
import org.tron.core.capsule.ContractCapsule;
Expand All @@ -35,6 +34,7 @@
import org.tron.core.db.TransactionContext;
import org.tron.core.exception.ContractExeException;
import org.tron.core.exception.ContractValidateException;
import org.tron.core.exception.HeaderNotFound;
import org.tron.core.utils.TransactionUtil;
import org.tron.core.vm.EnergyCost;
import org.tron.core.vm.LogInfoTriggerParser;
Expand All @@ -53,6 +53,7 @@
import org.tron.core.vm.program.invoke.ProgramInvokeFactory;
import org.tron.core.vm.repository.Repository;
import org.tron.core.vm.repository.RepositoryImpl;
import org.tron.core.vm.repository.RepositoryStateImpl;
import org.tron.core.vm.utils.MUtil;
import org.tron.protos.Protocol;
import org.tron.protos.Protocol.Block;
Expand Down Expand Up @@ -94,6 +95,9 @@ public class VMActuator implements Actuator2 {

private LogInfoTriggerParser logInfoTriggerParser;

private static final boolean isAllowStateRoot = CommonParameter.getInstance().getStorage()
.isAllowStateRoot();

public VMActuator(boolean isConstantCall) {
this.isConstantCall = isConstantCall;
this.maxEnergyLimit = CommonParameter.getInstance().maxEnergyLimitForConstant;
Expand All @@ -110,21 +114,38 @@ private static long getEnergyFee(long callerEnergyUsage, long callerEnergyFrozen

@Override
public void validate(Object object) throws ContractValidateException {

TransactionContext context = (TransactionContext) object;
if (Objects.isNull(context)) {
throw new RuntimeException("TransactionContext is null");
}

// Load Config
ConfigLoader.load(context.getStoreFactory());
// Warm up registry class
OperationRegistry.init();
trx = context.getTrxCap().getInstance();

// check whether is state query
boolean stateQuery = isAllowStateRoot && isConstantCall;
stateQuery = stateQuery && context.getBlockCap() != null;
try {
stateQuery = stateQuery && context.getBlockCap().getNum() < context.getStoreFactory()
.getChainBaseManager().getHead().getNum();
} catch (HeaderNotFound e) {
throw new ContractValidateException(e.getMessage());
}

//Prepare Repository
if (stateQuery) {
rootRepository = RepositoryStateImpl.createRoot(context.getBlockCap().getArchiveRoot());
} else {
rootRepository = RepositoryImpl.createRoot(context.getStoreFactory());
}
// Load Config
ConfigLoader.load(rootRepository);

// If tx`s fee limit is set, use it to calc max energy limit for constant call
if (isConstantCall && trx.getRawData().getFeeLimit() > 0) {
maxEnergyLimit = Math.min(maxEnergyLimit, trx.getRawData().getFeeLimit()
/ context.getStoreFactory().getChainBaseManager()
/ rootRepository
.getDynamicPropertiesStore().getEnergyFee());
}
blockCap = context.getBlockCap();
Expand All @@ -134,9 +155,6 @@ public void validate(Object object) throws ContractValidateException {
}
//Route Type
ContractType contractType = this.trx.getRawData().getContract(0).getType();
//Prepare Repository
rootRepository = RepositoryImpl.createRoot(context.getStoreFactory());

enableEventListener = context.isEventPluginLoaded();

//set executorType type
Expand Down Expand Up @@ -297,7 +315,6 @@ public void execute(Object object) throws ContractExeException {
String txHash = Hex.toHexString(rootInternalTx.getHash());
VMUtils.saveProgramTraceFile(txHash, traceContent);
}

}

private void create()
Expand Down Expand Up @@ -563,8 +580,7 @@ public long getAccountEnergyLimitWithFixRatio(AccountCapsule account, long feeLi
long now = rootRepository.getHeadSlot();
EnergyProcessor energyProcessor =
new EnergyProcessor(
rootRepository.getDynamicPropertiesStore(),
ChainBaseManager.getInstance().getAccountStore());
rootRepository.getDynamicPropertiesStore(), rootRepository.getAccountStore());
energyProcessor.updateUsage(account);
account.setLatestConsumeTimeForEnergy(now);
receipt.setCallerEnergyUsage(account.getEnergyUsage());
Expand Down Expand Up @@ -726,8 +742,7 @@ public long getTotalEnergyLimitWithFixRatio(AccountCapsule creator, AccountCapsu
long now = rootRepository.getHeadSlot();
EnergyProcessor energyProcessor =
new EnergyProcessor(
rootRepository.getDynamicPropertiesStore(),
ChainBaseManager.getInstance().getAccountStore());
rootRepository.getDynamicPropertiesStore(), rootRepository.getAccountStore());
energyProcessor.updateUsage(creator);
creator.setLatestConsumeTimeForEnergy(now);
receipt.setOriginEnergyUsage(creator.getEnergyUsage());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,17 @@
import lombok.extern.slf4j.Slf4j;
import org.tron.common.parameter.CommonParameter;
import org.tron.core.store.DynamicPropertiesStore;
import org.tron.core.store.StoreFactory;
import org.tron.core.vm.repository.Repository;

@Slf4j(topic = "VMConfigLoader")
public class ConfigLoader {

//only for unit test
public static boolean disable = false;

public static void load(StoreFactory storeFactory) {
public static void load(Repository repository) {
if (!disable) {
DynamicPropertiesStore ds = storeFactory.getChainBaseManager().getDynamicPropertiesStore();
DynamicPropertiesStore ds = repository.getDynamicPropertiesStore();
VMConfig.setVmTrace(CommonParameter.getInstance().isVmTrace());
if (ds != null) {
VMConfig.initVmHardFork(checkForEnergyLimit(ds));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
import org.apache.commons.lang3.ArrayUtils;
import org.tron.common.utils.DecodeUtil;
import org.tron.common.utils.StringUtil;
import org.tron.core.ChainBaseManager;
import org.tron.core.actuator.ActuatorConstant;
import org.tron.core.capsule.AccountCapsule;
import org.tron.core.capsule.DelegatedResourceAccountIndexCapsule;
Expand Down Expand Up @@ -56,7 +55,9 @@ public void validate(DelegateResourceParam param, Repository repo) throws Contra

switch (param.getResourceType()) {
case BANDWIDTH: {
BandwidthProcessor processor = new BandwidthProcessor(ChainBaseManager.getInstance());
BandwidthProcessor processor = new BandwidthProcessor(dynamicStore,
repo.getAccountStore(), repo.getAssetIssueStore(),
repo.getAssetIssueV2Store());
processor.updateUsageForDelegated(ownerCapsule);

long netUsage = (long) (ownerCapsule.getNetUsage() * TRX_PRECISION * ((double)
Expand All @@ -71,8 +72,7 @@ public void validate(DelegateResourceParam param, Repository repo) throws Contra
}
break;
case ENERGY: {
EnergyProcessor processor =
new EnergyProcessor(dynamicStore, ChainBaseManager.getInstance().getAccountStore());
EnergyProcessor processor = new EnergyProcessor(dynamicStore, repo.getAccountStore());
processor.updateUsage(ownerCapsule);

long energyUsage = (long) (ownerCapsule.getEnergyUsage() * TRX_PRECISION * ((double)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
import lombok.extern.slf4j.Slf4j;
import org.tron.common.utils.DecodeUtil;
import org.tron.common.utils.StringUtil;
import org.tron.core.ChainBaseManager;
import org.tron.core.capsule.AccountCapsule;
import org.tron.core.capsule.DelegatedResourceAccountIndexCapsule;
import org.tron.core.capsule.DelegatedResourceCapsule;
Expand Down Expand Up @@ -100,7 +99,9 @@ public void execute(UnDelegateResourceParam param, Repository repo) {
if (receiverCapsule != null) {
switch (param.getResourceType()) {
case BANDWIDTH:
BandwidthProcessor bandwidthProcessor = new BandwidthProcessor(ChainBaseManager.getInstance());
BandwidthProcessor bandwidthProcessor = new BandwidthProcessor(dynamicStore,
repo.getAccountStore(), repo.getAssetIssueStore(),
repo.getAssetIssueV2Store());
bandwidthProcessor.updateUsageForDelegated(receiverCapsule);
/* For example, in a scenario where a regular account can be upgraded to a contract
account through an interface, the account information will be cleared after the
Expand All @@ -125,8 +126,8 @@ public void execute(UnDelegateResourceParam param, Repository repo) {
receiverCapsule.setLatestConsumeTime(now);
break;
case ENERGY:
EnergyProcessor energyProcessor =
new EnergyProcessor(dynamicStore, ChainBaseManager.getInstance().getAccountStore());
EnergyProcessor energyProcessor = new EnergyProcessor(dynamicStore,
repo.getAccountStore());
energyProcessor.updateUsage(receiverCapsule);

if (receiverCapsule.getAcquiredDelegatedFrozenV2BalanceForEnergy()
Expand Down Expand Up @@ -165,7 +166,9 @@ public void execute(UnDelegateResourceParam param, Repository repo) {
ownerCapsule.addDelegatedFrozenV2BalanceForBandwidth(-unDelegateBalance);
ownerCapsule.addFrozenBalanceForBandwidthV2(unDelegateBalance);

BandwidthProcessor processor = new BandwidthProcessor(ChainBaseManager.getInstance());
BandwidthProcessor processor = new BandwidthProcessor(dynamicStore,
repo.getAccountStore(), repo.getAssetIssueStore(),
repo.getAssetIssueV2Store());
if (Objects.nonNull(receiverCapsule) && transferUsage > 0) {
processor.unDelegateIncrease(ownerCapsule, receiverCapsule,
transferUsage, BANDWIDTH, now);
Expand All @@ -179,7 +182,7 @@ public void execute(UnDelegateResourceParam param, Repository repo) {
ownerCapsule.addFrozenBalanceForEnergyV2(unDelegateBalance);

EnergyProcessor processor =
new EnergyProcessor(dynamicStore, ChainBaseManager.getInstance().getAccountStore());
new EnergyProcessor(dynamicStore, repo.getAccountStore());
if (Objects.nonNull(receiverCapsule) && transferUsage > 0) {
processor.unDelegateIncrease(ownerCapsule, receiverCapsule, transferUsage, ENERGY, now);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import org.tron.core.capsule.DelegatedResourceCapsule;
import org.tron.core.capsule.VotesCapsule;
import org.tron.core.capsule.WitnessCapsule;
import org.tron.core.store.AccountStore;
import org.tron.core.store.AssetIssueStore;
import org.tron.core.store.AssetIssueV2Store;
import org.tron.core.store.DelegationStore;
Expand Down Expand Up @@ -46,6 +47,11 @@ public AssetIssueCapsule getAssetIssue(byte[] tokenId) {
return repository.getAssetIssue(tokenId);
}

@Override
public AccountStore getAccountStore() {
return repository.getAccountStore();
}

@Override
public AssetIssueV2Store getAssetIssueV2Store() {
return repository.getAssetIssueV2Store();
Expand Down
6 changes: 3 additions & 3 deletions actuator/src/main/java/org/tron/core/vm/program/Program.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@
import org.tron.common.utils.FastByteComparisons;
import org.tron.common.utils.Utils;
import org.tron.common.utils.WalletUtil;
import org.tron.core.ChainBaseManager;
import org.tron.core.Constant;
import org.tron.core.capsule.AccountCapsule;
import org.tron.core.capsule.BlockCapsule;
Expand Down Expand Up @@ -556,7 +555,8 @@ private long transferFrozenV2BalanceToInheritor(byte[] ownerAddr, byte[] inherit
});

// merge usage
BandwidthProcessor bandwidthProcessor = new BandwidthProcessor(ChainBaseManager.getInstance());
BandwidthProcessor bandwidthProcessor = new BandwidthProcessor(repo.getDynamicPropertiesStore()
, repo.getAccountStore(), repo.getAssetIssueStore(), repo.getAssetIssueV2Store());
bandwidthProcessor.updateUsageForDelegated(ownerCapsule);
ownerCapsule.setLatestConsumeTime(now);
if (ownerCapsule.getNetUsage() > 0) {
Expand All @@ -566,7 +566,7 @@ private long transferFrozenV2BalanceToInheritor(byte[] ownerAddr, byte[] inherit

EnergyProcessor energyProcessor =
new EnergyProcessor(
repo.getDynamicPropertiesStore(), ChainBaseManager.getInstance().getAccountStore());
repo.getDynamicPropertiesStore(), repo.getAccountStore());
energyProcessor.updateUsage(ownerCapsule);
ownerCapsule.setLatestConsumeTimeForEnergy(now);
if (ownerCapsule.getEnergyUsage() > 0) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@

public interface Repository {

AccountStore getAccountStore();

AssetIssueCapsule getAssetIssue(byte[] tokenId);

AssetIssueV2Store getAssetIssueV2Store();
Expand Down
Loading