Skip to content

Commit

Permalink
feat(DynamicEnergy): optimize Dynamic Energy ContractState API
Browse files Browse the repository at this point in the history
  • Loading branch information
daniel-cao-byte committed Dec 27, 2022
1 parent 4145e58 commit 17bce84
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 121 deletions.
45 changes: 16 additions & 29 deletions framework/src/main/java/org/tron/core/Wallet.java
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,6 @@
import org.tron.protos.contract.ShieldContract.PedersenHash;
import org.tron.protos.contract.ShieldContract.ReceiveDescription;
import org.tron.protos.contract.ShieldContract.ShieldedTransferContract;
import org.tron.protos.contract.SmartContractOuterClass.ContractState;
import org.tron.protos.contract.SmartContractOuterClass.CreateSmartContract;
import org.tron.protos.contract.SmartContractOuterClass.SmartContract;
import org.tron.protos.contract.SmartContractOuterClass.SmartContractDataWrapper;
Expand Down Expand Up @@ -2951,33 +2950,6 @@ public SmartContract getContract(GrpcAPI.BytesMessage bytesMessage) {
return null;
}

public ContractState getContractState(GrpcAPI.BytesMessage bytesMessage) {
byte[] address = bytesMessage.getValue().toByteArray();

ContractCapsule contractCapsule = chainBaseManager.getContractStore().get(address);
if (contractCapsule == null) {
logger.error(
"Get contract state failed, the contract does not exist!");
return null;
}

ContractStateCapsule contractStateCapsule
= chainBaseManager.getContractStateStore().get(address);
if (Objects.nonNull(contractStateCapsule)) {
contractStateCapsule.catchUpToCycle(
chainBaseManager.getDynamicPropertiesStore().getCurrentCycleNumber(),
chainBaseManager.getDynamicPropertiesStore().getDynamicEnergyThreshold(),
chainBaseManager.getDynamicPropertiesStore().getDynamicEnergyIncreaseFactor(),
chainBaseManager.getDynamicPropertiesStore().getDynamicEnergyMaxFactor()
);
return contractStateCapsule.getInstance();
}

return new ContractStateCapsule(
chainBaseManager.getDynamicPropertiesStore().getCurrentCycleNumber())
.getInstance();
}

/**
* Add a wrapper for smart contract. Current additional information including runtime code for a
* smart contract.
Expand Down Expand Up @@ -3008,7 +2980,22 @@ public SmartContractDataWrapper getContractInfo(GrpcAPI.BytesMessage bytesMessag
} else {
contractCapsule.setRuntimecode(new byte[0]);
}
return contractCapsule.generateWrapper();
SmartContractDataWrapper wrapper = contractCapsule.generateWrapper();

ContractStateCapsule contractStateCapsule
= chainBaseManager.getContractStateStore().get(address);
if (Objects.nonNull(contractStateCapsule)) {
contractStateCapsule.catchUpToCycle(
chainBaseManager.getDynamicPropertiesStore().getCurrentCycleNumber(),
chainBaseManager.getDynamicPropertiesStore().getDynamicEnergyThreshold(),
chainBaseManager.getDynamicPropertiesStore().getDynamicEnergyIncreaseFactor(),
chainBaseManager.getDynamicPropertiesStore().getDynamicEnergyMaxFactor()
);
} else {
contractStateCapsule = new ContractStateCapsule(
chainBaseManager.getDynamicPropertiesStore().getCurrentCycleNumber());
}
return wrapper.toBuilder().setContractState(contractStateCapsule.getInstance()).build();
}
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,6 @@
import org.tron.protos.contract.ShieldContract.IncrementalMerkleVoucherInfo;
import org.tron.protos.contract.ShieldContract.OutputPointInfo;
import org.tron.protos.contract.SmartContractOuterClass.ClearABIContract;
import org.tron.protos.contract.SmartContractOuterClass.ContractState;
import org.tron.protos.contract.SmartContractOuterClass.CreateSmartContract;
import org.tron.protos.contract.SmartContractOuterClass.SmartContract;
import org.tron.protos.contract.SmartContractOuterClass.SmartContractDataWrapper;
Expand Down Expand Up @@ -2041,14 +2040,6 @@ public void getContract(BytesMessage request,
responseObserver.onCompleted();
}

@Override
public void getContractState(BytesMessage request,
StreamObserver<ContractState> responseObserver) {
ContractState contractState = wallet.getContractState(request);
responseObserver.onNext(contractState);
responseObserver.onCompleted();
}

@Override
public void getContractInfo(BytesMessage request,
StreamObserver<SmartContractDataWrapper> responseObserver) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,8 +138,6 @@ public class FullNodeHttpApiService implements Service {
@Autowired
private GetContractInfoServlet getContractInfoServlet;
@Autowired
private GetContractStateServlet getContractStateServlet;
@Autowired
private ClearABIServlet clearABIServlet;
@Autowired
private ProposalCreateServlet proposalCreateServlet;
Expand Down Expand Up @@ -446,7 +444,6 @@ public void start() {
"/wallet/triggerconstantcontract");
context.addServlet(new ServletHolder(getContractServlet), "/wallet/getcontract");
context.addServlet(new ServletHolder(getContractInfoServlet), "/wallet/getcontractinfo");
context.addServlet(new ServletHolder(getContractStateServlet), "/wallet/getcontractstate");
context.addServlet(new ServletHolder(clearABIServlet), "/wallet/clearabi");
context.addServlet(new ServletHolder(proposalCreateServlet), "/wallet/proposalcreate");
context.addServlet(new ServletHolder(proposalApproveServlet), "/wallet/proposalapprove");
Expand Down

This file was deleted.

3 changes: 0 additions & 3 deletions protocol/src/main/protos/api/api.proto
Original file line number Diff line number Diff line change
Expand Up @@ -471,9 +471,6 @@ service Wallet {
rpc GetContract (BytesMessage) returns (SmartContract) {
}

rpc GetContractState (BytesMessage) returns (ContractState) {
}

rpc GetContractInfo (BytesMessage) returns (SmartContractDataWrapper) {
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,4 +100,5 @@ message UpdateEnergyLimitContract {
message SmartContractDataWrapper {
SmartContract smart_contract = 1;
bytes runtimecode = 2;
ContractState contract_state = 3;
}

0 comments on commit 17bce84

Please sign in to comment.