Skip to content

Commit 6474c7d

Browse files
Merge pull request #6274 from yanghang8612/feat/optimize_energyprice_querying
func(event): optimize energy price querying method
2 parents 8201860 + 3780088 commit 6474c7d

File tree

3 files changed

+16
-13
lines changed

3 files changed

+16
-13
lines changed

framework/src/main/java/org/tron/core/services/event/BlockEventGet.java

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
import org.tron.core.exception.BadItemException;
4040
import org.tron.core.services.event.bo.BlockEvent;
4141
import org.tron.core.services.event.bo.SmartContractTrigger;
42+
import org.tron.core.services.jsonrpc.JsonRpcApiUtil;
4243
import org.tron.core.store.StoreFactory;
4344
import org.tron.protos.Protocol;
4445
import org.tron.protos.contract.SmartContractOuterClass;
@@ -373,19 +374,7 @@ public List<TransactionLogTriggerCapsule> getTransactionLogTrigger(BlockCapsule
373374

374375
public long getEnergyPrice(long blockTime) {
375376
String energyPriceHistory = manager.getDynamicPropertiesStore().getEnergyPriceHistory();
376-
377-
String[] energyPrices = energyPriceHistory.split(",");
378-
String[] lastPrice = energyPrices[energyPrices.length - 1].split(":");
379-
long energyPrice = Long.parseLong(lastPrice[1]);
380-
381-
for (int i = 1; i < energyPrices.length; i++) {
382-
long effectiveTime = Long.parseLong(energyPrices[i].split(":")[0]);
383-
if (blockTime < effectiveTime) {
384-
energyPrice = Long.parseLong(energyPrices[i - 1].split(":")[1]);
385-
break;
386-
}
387-
}
388-
return energyPrice;
377+
return JsonRpcApiUtil.parseEnergyFee(blockTime, energyPriceHistory);
389378
}
390379

391380
public List<TransactionLogTriggerCapsule> getTransactionTriggers(BlockCapsule block,

framework/src/main/resources/config.conf

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -611,6 +611,9 @@ vm = {
611611
# Indicates whether the node stores featured internal transactions, such as freeze, vote and so on
612612
# saveFeaturedInternalTx = false
613613

614+
# Indicates whether the node stores the details of the internal transactions generated by the CANCELALLUNFREEZEV2 opcode, such as bandwidth/energy/tronpower cancel amount.
615+
# saveCancelAllUnfreezeV2Details = false
616+
614617
# In rare cases, transactions that will be within the specified maximum execution time (default 10(ms)) are re-executed and packaged
615618
# longRunningTime = 10
616619

framework/src/test/java/org/tron/core/event/BlockEventGetTest.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,11 @@ public void test() throws Exception {
150150
});
151151
manager.pushBlock(blockCapsule);
152152

153+
// Set energy price history to test boundary cases
154+
manager.getDynamicPropertiesStore().saveEnergyPriceHistory(
155+
manager.getDynamicPropertiesStore().getEnergyPriceHistory()
156+
+ "," + time + ":210");
157+
153158
EventPluginConfig config = new EventPluginConfig();
154159
config.setSendQueueLength(1000);
155160
config.setBindPort(5555);
@@ -187,6 +192,12 @@ public void test() throws Exception {
187192
try {
188193
BlockEvent blockEvent = blockEventGet.getBlockEvent(1);
189194
Assert.assertNotNull(blockEvent);
195+
Assert.assertEquals(1, blockEvent.getTransactionLogTriggerCapsules().size());
196+
197+
// Here energy unit price should be 100 not 210,
198+
// cause block time is equal to 210`s effective time
199+
Assert.assertEquals(100, blockEvent.getTransactionLogTriggerCapsules()
200+
.get(0).getTransactionLogTrigger().getEnergyUnitPrice());
190201
} catch (Exception e) {
191202
Assert.fail();
192203
}

0 commit comments

Comments
 (0)