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

feat: Implement opcode logger functionality #8241

Open
wants to merge 36 commits into
base: 8174-add-new-opcodes-endpoint
Choose a base branch
from

Conversation

konstantinabl
Copy link

@konstantinabl konstantinabl commented May 7, 2024

Description:

The main goal of this PR is to add new tracer (OpcodeTracer) in the web3 module, in order to support opcodeLogger for debugging purposes.

Main files added:

  • OpcodeTracer - tracer class, which has methods for tracing contract calls and uses the message frame to get the executed opcodes and save them
  • ContractionActionService - service that queries the DB for relevant contract actions, used in the OpcodeTracer
  • EntityService - service that finds an entity in the DB, used to find the evmAddress of an account

Related issue(s):

Fixes #8173

Notes for reviewer:

Signed-off-by: Konstantina Blazhukova <konstantina.blajukova@gmail.com>
…nto 8173-implement-opcode-logger-tracing-logic

Need changes from other branch to debug
Signed-off-by: Konstantina Blazhukova <konstantina.blajukova@gmail.com>
Copy link

sonarcloud bot commented May 14, 2024

Quality Gate Failed Quality Gate failed

Failed conditions
1 Security Hotspot

See analysis details on SonarCloud

Copy link

codecov bot commented May 14, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 92.45%. Comparing base (9f37404) to head (91499c4).
Report is 27 commits behind head on main.

Additional details and impacted files
@@             Coverage Diff              @@
##               main    #8241      +/-   ##
============================================
+ Coverage     92.28%   92.45%   +0.17%     
+ Complexity     7280     1270    -6010     
============================================
  Files           898      372     -526     
  Lines         29338    11455   -17883     
  Branches       3575     1549    -2026     
============================================
- Hits          27075    10591   -16484     
+ Misses         1446      699     -747     
+ Partials        817      165     -652     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Signed-off-by: Konstantina Blazhukova <konstantina.blajukova@gmail.com>
@konstantinabl konstantinabl changed the base branch from main to 8174-add-new-opcodes-endpoint May 15, 2024 12:47
Signed-off-by: Konstantina Blazhukova <konstantina.blajukova@gmail.com>
Signed-off-by: Konstantina Blazhukova <konstantina.blajukova@gmail.com>
Signed-off-by: Konstantina Blazhukova <konstantina.blajukova@gmail.com>
Signed-off-by: Konstantina Blazhukova <konstantina.blajukova@gmail.com>
…nto 8173-implement-opcode-logger-tracing-logic

# Conflicts:
#	hedera-mirror-web3/src/main/java/com/hedera/mirror/web3/evm/contracts/execution/traceability/OpcodeTracer.java
#	hedera-mirror-web3/src/main/java/com/hedera/mirror/web3/service/ContractCallService.java
#	hedera-mirror-web3/src/main/java/com/hedera/node/app/service/evm/contracts/execution/HederaEvmTxProcessor.java
#	hedera-mirror-web3/src/test/java/com/hedera/mirror/web3/controller/OpcodesControllerTest.java
Signed-off-by: Konstantina Blazhukova <konstantina.blajukova@gmail.com>
konstantinabl and others added 7 commits May 17, 2024 16:08
Signed-off-by: Konstantina Blazhukova <konstantina.blajukova@gmail.com>
…nto 8173-implement-opcode-logger-tracing-logic

# Conflicts:
#	hedera-mirror-web3/src/main/java/com/hedera/mirror/web3/common/ContractCallContext.java
#	hedera-mirror-web3/src/main/java/com/hedera/mirror/web3/controller/OpcodesController.java
#	hedera-mirror-web3/src/main/java/com/hedera/mirror/web3/service/ContractCallService.java
#	hedera-mirror-web3/src/test/java/com/hedera/mirror/web3/controller/OpcodesControllerTest.java
…lement-opcode-logger-tracing-logic

# Conflicts:
#	hedera-mirror-web3/src/main/java/com/hedera/mirror/web3/common/ContractCallContext.java
#	hedera-mirror-web3/src/main/java/com/hedera/mirror/web3/controller/OpcodesController.java
#	hedera-mirror-web3/src/main/java/com/hedera/mirror/web3/service/ContractCallService.java
#	hedera-mirror-web3/src/test/java/com/hedera/mirror/web3/controller/OpcodesControllerTest.java
Signed-off-by: Victor Yanev <victor.yanev@limechain.tech>
Signed-off-by: Konstantina Blazhukova <konstantina.blajukova@gmail.com>
Signed-off-by: Konstantina Blazhukova <konstantina.blajukova@gmail.com>
Signed-off-by: Konstantina Blazhukova <konstantina.blajukova@gmail.com>
Copy link

@victor-yanev victor-yanev left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice job 🔥! This has been a tough task!

I've left a few suggestions below:

Signed-off-by: Victor Yanev <victor.yanev@limechain.tech>
…cing-logic' into 8173-implement-opcode-logger-tracing-logic
Signed-off-by: Victor Yanev <victor.yanev@limechain.tech>
Signed-off-by: Victor Yanev <victor.yanev@limechain.tech>
…lement-opcode-logger-tracing-logic

# Conflicts:
#	hedera-mirror-common/src/main/java/com/hedera/mirror/common/domain/transaction/Opcode.java
#	hedera-mirror-web3/src/main/java/com/hedera/mirror/web3/controller/OpcodesController.java
#	hedera-mirror-web3/src/main/java/com/hedera/mirror/web3/service/ContractCallService.java
#	hedera-mirror-web3/src/test/java/com/hedera/mirror/web3/controller/OpcodesControllerTest.java
Signed-off-by: Victor Yanev <victor.yanev@limechain.tech>
Signed-off-by: Victor Yanev <victor.yanev@limechain.tech>
Signed-off-by: Victor Yanev <victor.yanev@limechain.tech>
@victor-yanev victor-yanev added enhancement Type: New feature web3 Area: Web3 API limechain Work planned for the LimeChain team labels May 22, 2024
@victor-yanev victor-yanev changed the title Drafts opcode logger functionality Implement opcode logger functionality May 22, 2024
@victor-yanev victor-yanev changed the title Implement opcode logger functionality feat: Implement opcode logger functionality May 22, 2024
@victor-yanev victor-yanev marked this pull request as ready for review May 22, 2024 10:29
…lement-opcode-logger-tracing-logic

# Conflicts:
#	hedera-mirror-web3/src/main/java/com/hedera/mirror/web3/controller/OpcodesController.java
#	hedera-mirror-web3/src/test/java/com/hedera/mirror/web3/controller/OpcodesControllerTest.java
Signed-off-by: Victor Yanev <victor.yanev@limechain.tech>
Signed-off-by: Victor Yanev <victor.yanev@limechain.tech>
…Exception when transaction cannot be found

Signed-off-by: Victor Yanev <victor.yanev@limechain.tech>
victor-yanev and others added 6 commits May 23, 2024 17:58
Signed-off-by: Victor Yanev <victor.yanev@limechain.tech>
Signed-off-by: Victor Yanev <victor.yanev@limechain.tech>
Signed-off-by: Victor Yanev <victor.yanev@limechain.tech>
Signed-off-by: Victor Yanev <victor.yanev@limechain.tech>
Signed-off-by: Konstantina Blazhukova <konstantina.blajukova@gmail.com>
Signed-off-by: Konstantina Blazhukova <konstantina.blajukova@gmail.com>

@Override
public List<ContractAction> findFromTransaction(@NonNull @Valid TransactionIdOrHashParameter transactionIdOrHash) {
Assert.isTrue(transactionIdOrHash.isValid(), "Invalid transactionIdOrHash");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't the @Valid annotation doing this check automatically?

@@ -88,20 +97,9 @@ public String processCall(final CallServiceParameters params) {
if (params.isEstimate()) {
// eth_estimateGas initialization - historical timestamp is Optional.empty()
ctx.initializeStackFrames(store.getStackedStateFrames());
result = estimateGas(params);
result = estimateGas(params, ctx);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it would be cleaner if we have a dedicated ContractCallDebugService, which would be dedicated only for all existing and future debug related RPC calls. The workflow there is different - we replay an already executed transaction, while in ContractCallService we simulate a hypothetical transaction. So both services would have different responsibilities and needs.

If some components seem to be common for both services, they can be extracted in a parent Service class.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree
Will do.

} else {
BlockType block = params.getBlock();
// if we have historical call then set corresponding file record
if (block != BlockType.LATEST) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the case of debug_traceTransaction we won't have a defined block explicitly. We should find the record_file corresponding to the timestamp for the transactionHash that is sent as an input, so that the ContractCallContext contains proper data.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is already done when constructing the call service parameters, and the block number is passed to the ContractCallService.

// if the call is historical
ctx.initializeStackFrames(store.getStackedStateFrames());
final var ethCallTxnResult = doProcessCall(params, params.getGas(), true);
final var ethCallTxnResult = callContract(params, TracerType.OPERATION, ctx);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's a symptom of why we should split the logic in 2 different services. We shouldn't use any tracing in the case of eth_call, eth_estimateGas, so that we don't make this operations more CPU and memory intensive.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree, but the other operation tracer was used previously as well, we haven’t introduced any changes to the other methods.

Previously, the MirrorOperationTracer was injected directly into the processor and now we inject a Map<TracerType, HederaEvmTracer> instead, and thats why we need to pass the tracer type here, in order to get the right tracer that we want to use.

If we don’t want to do any tracing in eth_call and eth_estimateGas, we can create a new TracerType.NONE that’s used by default and doesn’t invoke any tracers when processing the call.

What’s your opinion here?


public enum TracerType {
OPCODE,
OPERATION
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For OPERATION we use directly the json-rpc-relay implementation, which utilises the /actions endpoint. The REST API in the web3 module should be used only for OPCODE tracing. I think we should delete the OPERATION type.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Service
@RequiredArgsConstructor
public class EntityServiceImpl implements EntityService {
private final EntityRepository entityRepository;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can directly use the EntityDatabaseAccessor for the moment and delete this Service definition.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's used in the StackedStateFrames component, but could be used as a service layer abstraction over the EntityRepository.

Signed-off-by: Konstantina Blazhukova <konstantina.blajukova@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement Type: New feature limechain Work planned for the LimeChain team web3 Area: Web3 API
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Implement opcode logger tracing logic
3 participants