So currently my function looks as follow:

   * @notice Checks if it is allowed to transition between the given states
  function checkNextStates(bytes32 fromState, bytes32 toState) public view returns (bool hasNextState) {
    hasNextState = false;
    bytes32[] storage nextStates = states[fromState].nextStates;
    for (uint256 i = 0; i < nextStates.length; i++) {
      if (keccak256(abi.encodePacked(nextStates[i])) == keccak256(abi.encodePacked(toState))) {
        hasNextState = true;

So fromState is a bytes32 of multiple values but toState is always the same length == 1. I pushed this to prod, when we have a lot in fromState then it becomes extremely costly. Do you guys have any idea on how to check if in a bytes32 with multiple values there is the value of a bytes32 with a single value?

Thanks in advance

1 Answer 1


If you have an unsorted array you need somehow to apply a sorting / search algorithm;

This could be very expensive.

I would suggest to approach the problem differently, if possible (I have not clear what you are trying to achieve so this might not work):

instead of having an array of values, you could create a mapping mapping(bytes32 => bool)where you save your keccak256(abi.encodePacked(toState)) as a key (and remove it when it is not needed anymore)

this would allow you to check the existence of the key you are looking for in O(1)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.