hyperledger / besu

An enterprise-grade Java-based, Apache 2.0 licensed Ethereum client https://wiki.hyperledger.org/display/besu

Home Page:https://www.hyperledger.org/projects/besu

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

java.lang.StringIndexOutOfBoundsException when getting validators from ValidatorContract for QBFT

v0dev opened this issue · comments

I am trying to create a QBFT network with a smart contract validator set, the smart contract is simple:


pragma solidity ^0.8.20;

contract Validation {

    function getValidators() external view returns (address[] memory) {
            address[] memory vals = new address[](4);
            vals[0] = 0x68b9aDc0556f86a122b12Aa1e8Af8951362CA676;
            vals[1] = 0xdc398a72b509053A77F828067471d0f71964c7A6;
            vals[2] = 0x0B60f4774E6df60833813A3853F2F2341d8d3860;
            vals[3] = 0x08134eEA5B10594E52c11Dc0198Ab95C5E0b328e;
            return vals;
    }
    
}


Solidity Version Used: 0.8.25+commit.b61c2a91
EVM Version: Paris
Tool Used: Remix IDE
Optimizations: False

I am using the bytecode from Remix to add into the Genesis.json file:

{
  "config": {
    "chainId": 1337,
    "homesteadBlock": 0,
    "eip150Block": 0,
    "eip155Block": 0,
    "eip158Block": 0,
    "byzantiumBlock": 0,
    "constantinopleBlock": 0,
    "constantinopleFixBlock": 0,
    "istanbulBlock": 0,
    "muirGlacierBlock": 0,
    "berlinBlock": 0,
    "londonBlock": 0,
    "parisBlock": 0,
    "shanghaiTime": 1715263822,
    "cancunTime": 1715263942,
    "qbft": {
      "blockperiodseconds": 3,
      "epochlength": 30000,
      "requesttimeoutseconds": 4,
      "validatorcontractaddress": "0x0000000000000000000000000000000000007777"
    }
  },
  "nonce": "0x0",
  "timestamp": "0x663CC6F3",
  "gasLimit": "0x47b760",
  "difficulty": "0x1",
  "mixHash": "0x63746963616c2062797a616e74696e65206661756c7420746f6c6572616e6365",
  "coinbase": "0x0000000000000000000000000000000000000000",
  "alloc": {
    "68b9aDc0556f86a122b12Aa1e8Af8951362CA676": {
      "balance": "0xad78ebc5ac6200000"
    },
    "dc398a72b509053A77F828067471d0f71964c7A6": {
      "balance": "0xad78ebc5ac6200000"
    },
    "0B60f4774E6df60833813A3853F2F2341d8d3860": {
      "balance": "0xad78ebc5ac6200000"
    },
    "08134eEA5B10594E52c11Dc0198Ab95C5E0b328e": {
      "balance": "0xad78ebc5ac6200000"
    },
    "0x0000000000000000000000000000000000007777": {
      "balance": "0x0",
      "code": "6080604052348015600f57600080fd5b506103d78061001f6000396000f3fe608060405234801561001057600080fd5b506004361061002b5760003560e01c8063b7ab4db514610030575b600080fd5b61003861004e565b6040516100459190610321565b60405180910390f35b60606000600467ffffffffffffffff81111561006d5761006c610343565b5b60405190808252806020026020018201604052801561009b5781602001602082028036833780820191505090505b5090507368b9adc0556f86a122b12aa1e8af8951362ca676816000815181106100c7576100c6610372565b5b602002602001019073ffffffffffffffffffffffffffffffffffffffff16908173ffffffffffffffffffffffffffffffffffffffff168152505073dc398a72b509053a77f828067471d0f71964c7a68160018151811061012a57610129610372565b5b602002602001019073ffffffffffffffffffffffffffffffffffffffff16908173ffffffffffffffffffffffffffffffffffffffff1681525050730b60f4774e6df60833813a3853f2f2341d8d38608160028151811061018d5761018c610372565b5b602002602001019073ffffffffffffffffffffffffffffffffffffffff16908173ffffffffffffffffffffffffffffffffffffffff16815250507308134eea5b10594e52c11dc0198ab95c5e0b328e816003815181106101f0576101ef610372565b5b602002602001019073ffffffffffffffffffffffffffffffffffffffff16908173ffffffffffffffffffffffffffffffffffffffff16815250508091505090565b600081519050919050565b600082825260208201905092915050565b6000819050602082019050919050565b600073ffffffffffffffffffffffffffffffffffffffff82169050919050565b60006102888261025d565b9050919050565b6102988161027d565b82525050565b60006102aa838361028f565b60208301905092915050565b6000602082019050919050565b60006102ce82610231565b6102d8818561023c565b93506102e38361024d565b8060005b838110156103145781516102fb888261029e565b9750610306836102b6565b9250506001810190506102e7565b5085935050505092915050565b6000602082019050818103600083015261033b81846102c3565b905092915050565b7f4e487b7100000000000000000000000000000000000000000000000000000000600052604160045260246000fd5b7f4e487b7100000000000000000000000000000000000000000000000000000000600052603260045260246000fdfea2646970667358221220a230fb860ecdf01007909b03bf1994283a082268da9c1687abd35ebdfd769fba64736f6c63430008190033",
      "version": "0x01"
    }
  },
  "extraData": "0xe5a00000000000000000000000000000000000000000000000000000000000000000c0c080c0"
}

If i try to use Bytecode generated from EVM version as Shanghai or Cancun, i get the following error:

Failed validator smart contract call: ValidationResult{invalidReason=Optional[EXECUTION_HALTED], errorMessage=Optional[INVALID_OPERATION]}

If i try to use Bytecode generated from Paris (in the current genesis file) or London/Berlin etc i get the following error:

picocli.CommandLine$ExecutionException: java.lang.StringIndexOutOfBoundsException: begin 1791082552, end 1791082616, length 1966                                                     
        at org.hyperledger.besu.cli.BesuCommand.buildController(BesuCommand.java:1777)                                                                                               
        at org.hyperledger.besu.cli.BesuCommand.run(BesuCommand.java:1096)                                                                                                           
        at picocli.CommandLine.executeUserObject(CommandLine.java:2026)                                                                                                              
        at picocli.CommandLine.access$1500(CommandLine.java:148)                                                                                                                     
        at picocli.CommandLine$RunLast.executeUserObjectOfLastSubcommandWithSameParent(CommandLine.java:2461)                                                                        
        at picocli.CommandLine$RunLast.handle(CommandLine.java:2453)                                                                                                                 
        at picocli.CommandLine$RunLast.handle(CommandLine.java:2415)                                                                                                                 
        at picocli.CommandLine$AbstractParseResultHandler.execute(CommandLine.java:2273)                                                                                             
        at picocli.CommandLine$RunLast.execute(CommandLine.java:2417)                                                                                                                
        at picocli.CommandLine.execute(CommandLine.java:2170)                                                                                                                        
        at org.hyperledger.besu.cli.util.ConfigOptionSearchAndRunHandler.handle(ConfigOptionSearchAndRunHandler.java:66)                                                             
        at org.hyperledger.besu.cli.util.ConfigOptionSearchAndRunHandler.handle(ConfigOptionSearchAndRunHandler.java:33)                                                             
        at picocli.CommandLine$AbstractParseResultHandler.execute(CommandLine.java:2273)                                                                                             
        at picocli.CommandLine$RunLast.execute(CommandLine.java:2417)                                                                                                                
        at picocli.CommandLine.execute(CommandLine.java:2170)                                                                                                                        
        at org.hyperledger.besu.cli.BesuCommand.parse(BesuCommand.java:1261)                                                                                                         
        at org.hyperledger.besu.cli.BesuCommand.parse(BesuCommand.java:1051)                                                                                                         
        at org.hyperledger.besu.Besu.main(Besu.java:39)   
Caused by: com.google.common.util.concurrent.UncheckedExecutionException: java.lang.StringIndexOutOfBoundsException: begin 1791082552, end 1791082616, length 1966
        at com.google.common.cache.LocalCache$Segment.get(LocalCache.java:2087)
        at com.google.common.cache.LocalCache.get(LocalCache.java:4036)                                                                                                              
        at com.google.common.cache.LocalCache$LocalManualCache.get(LocalCache.java:4950)                                                                                             
        at org.hyperledger.besu.consensus.qbft.validator.TransactionValidatorProvider.getValidatorsFromContract(TransactionValidatorProvider.java:86)
        at org.hyperledger.besu.consensus.qbft.validator.TransactionValidatorProvider.getValidatorsAfterBlock(TransactionValidatorProvider.java:71)
        at org.hyperledger.besu.consensus.qbft.validator.ForkingValidatorProvider.getValidatorsAfterBlock(ForkingValidatorProvider.java:67)
        at org.hyperledger.besu.consensus.qbft.validator.ForkingValidatorProvider.getValidatorsAtHead(ForkingValidatorProvider.java:61)
        at org.hyperledger.besu.consensus.common.bft.statemachine.BftFinalState.getValidators(BftFinalState.java:98)
        at org.hyperledger.besu.consensus.common.bft.statemachine.BftFinalState.isLocalNodeValidator(BftFinalState.java:135)
        at org.hyperledger.besu.consensus.qbft.statemachine.QbftBlockHeightManagerFactory.create(QbftBlockHeightManagerFactory.java:64)
        at org.hyperledger.besu.consensus.qbft.statemachine.QbftController.createNewHeightManager(QbftController.java:118)
        at org.hyperledger.besu.consensus.common.bft.statemachine.BaseBftController.startNewHeightManager(BaseBftController.java:210)
        at org.hyperledger.besu.consensus.common.bft.statemachine.BaseBftController.start(BaseBftController.java:79)
        at org.hyperledger.besu.consensus.common.bft.blockcreation.BftMiningCoordinator.start(BftMiningCoordinator.java:99)
        at org.hyperledger.besu.controller.QbftBesuControllerBuilder.createMiningCoordinator(QbftBesuControllerBuilder.java:280)
        at org.hyperledger.besu.controller.BesuControllerBuilder.build(BesuControllerBuilder.java:757)
        at org.hyperledger.besu.cli.BesuCommand.buildController(BesuCommand.java:1775)
        ... 17 more
Caused by: java.lang.StringIndexOutOfBoundsException: begin 1791082552, end 1791082616, length 1966
        at java.base/java.lang.String.checkBoundsBeginEnd(String.java:4606)
        at java.base/java.lang.String.substring(String.java:2709)
        at org.web3j.abi.TypeDecoder.decodeUintAsInt(TypeDecoder.java:280)
        at org.web3j.abi.TypeDecoder.decodeDynamicArray(TypeDecoder.java:435)
        at org.web3j.abi.DefaultFunctionReturnDecoder.build(DefaultFunctionReturnDecoder.java:100)
        at org.web3j.abi.DefaultFunctionReturnDecoder.decodeFunctionResult(DefaultFunctionReturnDecoder.java:52)
        at org.web3j.abi.FunctionReturnDecoder.decode(FunctionReturnDecoder.java:57)
        at org.hyperledger.besu.consensus.qbft.validator.ValidatorContractController.decodeResult(ValidatorContractController.java:109)
        at org.hyperledger.besu.consensus.qbft.validator.ValidatorContractController.parseGetValidatorsResult(ValidatorContractController.java:82)
        at java.base/java.util.Optional.map(Optional.java:260)
        at org.hyperledger.besu.consensus.qbft.validator.ValidatorContractController.getValidators(ValidatorContractController.java:76)
        at org.hyperledger.besu.consensus.qbft.validator.TransactionValidatorProvider.lambda$getValidatorsFromContract$0(TransactionValidatorProvider.java:89)
        at com.google.common.cache.LocalCache$LocalManualCache$1.load(LocalCache.java:4955)
        at com.google.common.cache.LocalCache$LoadingValueReference.loadFuture(LocalCache.java:3589)
        at com.google.common.cache.LocalCache$Segment.loadSync(LocalCache.java:2328)
        at com.google.common.cache.LocalCache$Segment.lockedGetOrLoad(LocalCache.java:2187)
        at com.google.common.cache.LocalCache$Segment.get(LocalCache.java:2081)
        ... 33 more
java.lang.StringIndexOutOfBoundsException: begin 1791082552, end 1791082616, length 1966

I have tried different EVM versions, tried changing solidity versions etc but each time I get the StringIndexOutOfBoundsException

@matthew1001 are you able to take a look at this one?

Yes sure, I'll assign it to myself and take a look

Hey @matthew1001 , were you able to check this issue?

I should be able to take a look today

Could you include your config file and/or CLI args please @v0dev ?

I'm not seeing the same StringIndexOutOfBoundsException error you are seeing, but the validator contract definitely isn't working.

I've tried deploying the Besu sample contract https://github.com/Consensys/validator-smart-contracts/blob/main/contracts/allowlist/ValidatorSmartContractAllowList.sol which seems to work fine. If I do a test invocation of getValidators() on that contract once the node has started I correctly get back

output=0x000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000020000000000000000000000009a6d82ef3912d5ab60473124bccd2f2a640769d700000000000000000000000065463bf6268e5cc409b6501ec846487b935a1446,

which is the validator list 0x9A6d82Ef3912d5aB60473124BCCd2f2A640769D7,0x65463BF6268e5cC409b6501eC846487B935A1446.

However, invoking getValidators() on your contract after starting the node returns a large data payload (it looks like it might be returning the code of the contract itself? At least it's a data payload very similar to the bytecode of the contract:

output=0x608060405234801561001057600080fd5b506004361061002b5760003560e01c8063b7ab4db514610030575b600080fd5b61003861004e565b6040516100459190610321565b60405180910390f35b60606000600467ffffffffffffffff81111561006d5761006c610343565b5b60405190808252806020026020018201604052801561009b5781602001602082028036833780820191505090505b5090507368b9adc0556f86a122b12aa1e8af8951362ca676816000815181106100c7576100c6610372565b5b602002602001019073ffffffffffffffffffffffffffffffffffffffff16908173ffffffffffffffffffffffffffffffffffffffff168152505073dc398a72b509053a77f828067471d0f71964c7a68160018151811061012a57610129610372565b5b602002602001019073ffffffffffffffffffffffffffffffffffffffff16908173ffffffffffffffffffffffffffffffffffffffff1681525050730b60f4774e6df60833813a3853f2f2341d8d38608160028151811061018d5761018c610372565b5b602002602001019073ffffffffffffffffffffffffffffffffffffffff16908173ffffffffffffffffffffffffffffffffffffffff16815250507308134eea5b10594e52c11dc0198ab95c5e0b328e816003815181106101f0576101ef610372565b5b602002602001019073ffffffffffffffffffffffffffffffffffffffff16908173ffffffffffffffffffffffffffffffffffffffff16815250508091505090565b600081519050919050565b600082825260208201905092915050565b6000819050602082019050919050565b600073ffffffffffffffffffffffffffffffffffffffff82169050919050565b60006102888261025d565b9050919050565b6102988161027d565b82525050565b60006102aa838361028f565b60208301905092915050565b6000602082019050919050565b60006102ce82610231565b6102d8818561023c565b93506102e38361024d565b8060005b838110156103145781516102fb888261029e565b9750610306836102b6565b9250506001810190506102e7565b5085935050505092915050565b6000602082019050818103600083015261033b81846102c3565b905092915050565b7f4e487b7100000000000000000000000000000000000000000000000000000000600052604160045260246000fd5b7f4e487b7100000000000000000000000000000000000000000000000000000000600052603260045260246000fdfea2646970667358221220e6fe023edcd49585727277945693024f0af34bd4ca8dae3e5de042d9347af14764736f6c63430008140033

Your contract itself, if manually deployed and invoked, does appear to work as expected. I'm wondering if it's something specific to do with the way Besu deploys a contract from the genesis file.

Regarding the shanghai/non-shanghai difference, if I don't enable shanghai on my chain I get the same INVALID_OPERATION. If I do enable it then I see the issues described above. I think this is as expected but I notice your genesis has shanghaiTime set. Did you have shanghaiTime set when you were seeing INVALID_OPERATION? Could you confirm exactly which version of Besu this is all using?

I've made a bit more progress, in as much as if I use Hardhat to compile your contract into bytecode and use that in the genesis.json, it works perfectly fine.

If I use remix to compile your contract and put the bytecode into the genesis.json, it doesn't work.

I've tried several combinations of compiler version and EVM version in remix and cannot make your contract, or the sample contract https://github.com/Consensys/validator-smart-contracts/blob/main/contracts/allowlist/ValidatorSmartContractAllowList.sol work. Remix does have several places where it outputs bytecode. Perhaps they differ slightly?

Could you try compiling with Hardhat and see if it works for you? If it does, I think the issue may be around pulling the bytecode out of remix.

Hey @matthew1001 , I will using hardhat instead of Remix , and let you know!

I suggest closing this for now. Happy for it to be reopened if there's an issue in the way Besu is treating the bytecode.