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

Add Transaction Permissioning Hook to PermissioningService Interface #7952

Draft
wants to merge 28 commits into
base: main
Choose a base branch
from

Conversation

vaidikcode
Copy link
Contributor

Signed-off-by: vaidikcode vaidikbhardwaj00@gmail.com

PR description

I’ve implemented the registerTransactionPermissioningProvider method to allow custom transaction permissioning logic by adding providers to a collection. A method to iterate over these providers and check if transactions are permitted has also been included.

Currently, other methods in the PermissioningService interface (like registerNodePermissioningProvider and registerNodeMessagePermissioningProvider) are left unimplemented, as they are not part of this PR's scope.

I may need guidance on whether you would like me to proceed with implementing the other methods, and any improvements or adjustments needed to integrate this into the broader permissioning system.

Fixed Issue(s)

#7835

Thanks for sending a pull request! Have you done the following?

  • Checked out our contribution guidelines?
  • Considered documentation and added the doc-change-required label to this PR if updates are required.
  • Considered the changelog and included an update if required.
  • For database changes (e.g. KeyValueSegmentIdentifier) considered compatibility and performed forwards and backwards compatibility tests

Locally, you can run these tests to catch failures early:

  • unit tests: ./gradlew build
  • acceptance tests: ./gradlew acceptanceTest
  • integration tests: ./gradlew integrationTest
  • reference tests: ./gradlew ethereum:referenceTests:referenceTests

Signed-off-by: vaidikcode <vaidikbhardwaj00@gmail.com>
@vaidikcode vaidikcode changed the title commit Add Transaction Permissioning Hook to PermissioningService Interface Nov 27, 2024
Copy link
Contributor

@macfarla macfarla left a comment

Choose a reason for hiding this comment

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

thanks for the contrib!

a few comments. the logging would be better at info level. you can have extra logging in the plugin if needed, and/or you can use custom log4j config to turn this up for troubleshooting but don't want this permanently at info level. The one-off "registered plugin" is fine at info level.

there's also the TestPermissioningPlugin and PermissioningPluginTest - you could expand the test plugin and add a test to verify the tx permissioning callback works as expected

you'll also need to run the gradle target checkAPIChanges to calculate and update the api hash

and prob add a changelog entry

return false;
}
}
log.info("Transaction {} permitted.", transaction.getHash());
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
log.info("Transaction {} permitted.", transaction.getHash());
log.debug("Transaction {} permitted.", transaction.getHash());

Copy link
Contributor

Choose a reason for hiding this comment

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

this would be spammy at info level

import java.util.List;

public class PermissionServiceImpl implements PermissioningService {
private static final Logger log = LoggerFactory.getLogger(PermissionServiceImpl.class);
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
private static final Logger log = LoggerFactory.getLogger(PermissionServiceImpl.class);
private static final Logger LOG = LoggerFactory.getLogger(PermissionServiceImpl.class);

Copy link
Contributor

Choose a reason for hiding this comment

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

all upper case naming convention for static fields

public boolean isTransactionPermitted(Transaction transaction) {
for (TransactionPermissioningProvider provider : transactionPermissioningProviders) {
if (!provider.isPermitted(transaction)) {
log.info("Transaction {} not permitted by one of the providers.", transaction.getHash());
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
log.info("Transaction {} not permitted by one of the providers.", transaction.getHash());
log.debug("Transaction {} not permitted by one of the providers.", transaction.getHash());

@macfarla macfarla self-assigned this Nov 27, 2024
@macfarla macfarla added the doc-change-required Indicates an issue or PR that requires doc to be updated label Nov 28, 2024
Signed-off-by: vaidikcode <vaidikbhardwaj00@gmail.com>
Signed-off-by: vaidikcode <vaidikbhardwaj00@gmail.com>
…hen it already exists

Signed-off-by: vaidikcode <vaidikbhardwaj00@gmail.com>
Signed-off-by: vaidikcode <vaidikbhardwaj00@gmail.com>
Signed-off-by: vaidikcode <vaidikbhardwaj00@gmail.com>
Signed-off-by: vaidikcode <vaidikbhardwaj00@gmail.com>
Signed-off-by: vaidikcode <vaidikbhardwaj00@gmail.com>
…er#7835

# Conflicts:
#	acceptance-tests/tests/src/test/java/org/hyperledger/besu/tests/acceptance/plugins/PermissioningPluginTest.java
Signed-off-by: vaidikcode <vaidikbhardwaj00@gmail.com>
Copy link
Contributor

@macfarla macfarla left a comment

Choose a reason for hiding this comment

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

will need some refactoring to move this TransactionPermissioningProvider interface into the plugin package.

/**
* Gets transaction rules.
*
* @return if the transaction is valid
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
* @return if the transaction is valid
* @return whether the transaction is permitted

CHANGELOG.md Outdated
@@ -32,6 +32,7 @@
- Fast Sync

### Additions and Improvements
- Add support for registeringTransactionPermissionProvider in Plugin API to define and validate transaction rules [#7952](https://github.com/hyperledger/besu/pull/7952)
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
- Add support for registeringTransactionPermissionProvider in Plugin API to define and validate transaction rules [#7952](https://github.com/hyperledger/besu/pull/7952)
- Add support for transaction permissioning rules in Plugin API [#7952](https://github.com/hyperledger/besu/pull/7952)

@@ -14,6 +14,7 @@
*/
package org.hyperledger.besu.plugin.services;

import org.hyperledger.besu.ethereum.permissioning.account.TransactionPermissioningProvider;
Copy link
Contributor

Choose a reason for hiding this comment

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

will need some refactoring to move this TransactionPermissioningProvider interface into the plugin package.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thank you for your feedback! Are you referring to this location: plugin-api/src/main/java/org/hyperledger/besu/plugin/services/permissioning/?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I have a quick question: Since the TransactionPermissioningProvider is implemented by both the AccountLocalConfigPermissioningController and the TransactionSmartContractPermissioningController, what would be the best way to move forward with this? Your guidance would be greatly appreciated!

Copy link
Contributor

Choose a reason for hiding this comment

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

I would have a look at how NodeMessagePermissioningProvider (functional interface) is used, since that's already exposed to plugins and used elsewhere in besu. It's a common pattern in besu to have an interface in the plugin-api package which is imported and implemented by classes where needed. BlockHeader is another good example but you can find lots of examples in the plugin-api package.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

There seems to be a circular dependency issue caused by importing the Transaction class into the TransactionPermissioningProvider interface. introducing a lightweight abstraction (like a DTO or a subset of transaction properties) for permissioning logic might be a good solution. wdyt?

Copy link
Contributor

Choose a reason for hiding this comment

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

does it help if the TransactionPermissioningProvider uses the org.hyperledger.besu.datatypes.Transaction interface instead

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thank you very much. It works perfectly!.

Signed-off-by: vaidikcode <vaidikbhardwaj00@gmail.com>
Signed-off-by: vaidikcode <vaidikbhardwaj00@gmail.com>
Signed-off-by: vaidikcode <vaidikbhardwaj00@gmail.com>
Signed-off-by: Vaidik <vaidikbhardwaj00@gmail.com>
Signed-off-by: vaidikcode <vaidikbhardwaj00@gmail.com>
Signed-off-by: Vaidik <vaidikbhardwaj00@gmail.com>
@macfarla
Copy link
Contributor

@vaidikcode spotless!

@macfarla
Copy link
Contributor

macfarla commented Dec 10, 2024

@vaidikcode still getting compile errors - can you run compile and test tasks locally, your feedback loop will be waaaay faster

/Users/sm/workspace/b2/ethereum/permissioning/src/main/java/org/hyperledger/besu/ethereum/permissioning/TransactionSmartContractPermissioningController.java:205: warning: [rawtypes] found raw type: BaseUInt256Value
            .map(BaseUInt256Value::toBytes)
                 ^
  missing type arguments for generic class BaseUInt256Value<T>
  where T is a type-variable:
    T extends UInt256Value<T> declared in class BaseUInt256Value
error: warnings found and -Werror specified
1 error
1 warning

@macfarla macfarla marked this pull request as draft December 11, 2024 05:17
@macfarla
Copy link
Contributor

converting to draft as a signal it's not ready to review

Signed-off-by: vaidikcode <vaidikbhardwaj00@gmail.com>
@vaidikcode vaidikcode marked this pull request as ready for review December 11, 2024 09:33
macfarla and others added 4 commits December 12, 2024 06:52
Signed-off-by: vaidikcode <vaidikbhardwaj00@gmail.com>
Signed-off-by: vaidikcode <vaidikbhardwaj00@gmail.com>
@vaidikcode vaidikcode marked this pull request as draft December 11, 2024 21:35
@vaidikcode vaidikcode marked this pull request as ready for review December 11, 2024 21:59
Signed-off-by: Sally MacFarlane <macfarla.github@gmail.com>
Copy link
Contributor

@macfarla macfarla left a comment

Choose a reason for hiding this comment

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

couple of comments

also this test is failing when I run locally - PermissioningPluginTest.testGasLimitLogic

@@ -57,6 +57,7 @@
- Fast Sync

### Additions and Improvements
- Add support for transaction permissioning rules in Plugin API [#7952](https://github.com/hyperledger/besu/pull/7952)
Copy link
Contributor

Choose a reason for hiding this comment

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

needs to move to "Unreleased" section

encodeAddress(transaction.getTo()),
transaction.getValue(),
transaction.getGasPrice().map(BaseUInt256Value::toBytes).orElse(Bytes32.ZERO),
encodeAddress(transaction.getTo().map(Address.class::cast)),
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't love this line - surely there is a cleaner way todo this

@@ -231,4 +237,16 @@ private static Bytes encodeBytes(final Bytes value) {
final Bytes padding = Bytes.wrap(new byte[(32 - (value.size() % 32))]);
return Bytes.concatenate(dynamicParameterOffset, length, value, padding);
}

// Convert the Quantity value to Bytes
private static Bytes convertQuantityToBytes(final Quantity quantity) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
private static Bytes convertQuantityToBytes(final Quantity quantity) {
private static Bytes encodeQuantity(final Quantity quantity) {

@macfarla macfarla marked this pull request as draft December 11, 2024 23:29
@macfarla
Copy link
Contributor

@vaidikcode couple of comments - it's getting there - but tests are still failing. Can you please run tests locally before saying it's ready to review? Your feedback loop will be like 100x faster

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
doc-change-required Indicates an issue or PR that requires doc to be updated
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants