diff --git a/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/mainnet/blockhash/Eip7709BlockHashProcessor.java b/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/mainnet/blockhash/Eip7709BlockHashProcessor.java index 0f3ca406e29..e483da33edc 100644 --- a/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/mainnet/blockhash/Eip7709BlockHashProcessor.java +++ b/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/mainnet/blockhash/Eip7709BlockHashProcessor.java @@ -16,7 +16,7 @@ import org.hyperledger.besu.ethereum.chain.Blockchain; import org.hyperledger.besu.ethereum.core.ProcessableBlockHeader; -import org.hyperledger.besu.ethereum.vm.ContractBasedBlockHashLookup; +import org.hyperledger.besu.ethereum.vm.Eip7709BlockHashLookup; import org.hyperledger.besu.evm.blockhash.BlockHashLookup; public class Eip7709BlockHashProcessor extends PragueBlockHashProcessor { @@ -24,6 +24,6 @@ public class Eip7709BlockHashProcessor extends PragueBlockHashProcessor { @Override public BlockHashLookup createBlockHashLookup( final Blockchain blockchain, final ProcessableBlockHeader blockHeader) { - return new ContractBasedBlockHashLookup(blockHeader, historyStorageAddress, historyServeWindow); + return new Eip7709BlockHashLookup(historyStorageAddress, historyServeWindow); } } diff --git a/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/vm/ContractBasedBlockHashLookup.java b/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/vm/Eip7709BlockHashLookup.java similarity index 76% rename from ethereum/core/src/main/java/org/hyperledger/besu/ethereum/vm/ContractBasedBlockHashLookup.java rename to ethereum/core/src/main/java/org/hyperledger/besu/ethereum/vm/Eip7709BlockHashLookup.java index 8aab22d9d64..0627a416109 100644 --- a/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/vm/ContractBasedBlockHashLookup.java +++ b/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/vm/Eip7709BlockHashLookup.java @@ -18,7 +18,6 @@ import org.hyperledger.besu.datatypes.Address; import org.hyperledger.besu.datatypes.Hash; -import org.hyperledger.besu.ethereum.core.ProcessableBlockHeader; import org.hyperledger.besu.evm.account.Account; import org.hyperledger.besu.evm.blockhash.BlockHashLookup; import org.hyperledger.besu.evm.frame.MessageFrame; @@ -35,47 +34,39 @@ * Retrieves block hashes from system contract storage and caches hashes by number, used by * BLOCKHASH operation. */ -public class ContractBasedBlockHashLookup implements BlockHashLookup { - private static final Logger LOG = LoggerFactory.getLogger(ContractBasedBlockHashLookup.class); +public class Eip7709BlockHashLookup implements BlockHashLookup { + private static final Logger LOG = LoggerFactory.getLogger(Eip7709BlockHashLookup.class); private static final long BLOCKHASH_SERVE_WINDOW = 256L; - private final ProcessableBlockHeader blockHeader; private final Address contractAddress; private final long historyServeWindow; private final long blockHashServeWindow; private final HashMap hashByNumber = new HashMap<>(); /** - * Constructs a ContractBasedBlockHashLookup. + * Constructs a Eip7709BlockHashLookup. * - * @param currentBlock current block header being processed. * @param contractAddress the address of the contract storing the history. * @param historyServeWindow the number of blocks for which history should be saved. */ - public ContractBasedBlockHashLookup( - final ProcessableBlockHeader currentBlock, - final Address contractAddress, - final long historyServeWindow) { - this(currentBlock, contractAddress, historyServeWindow, BLOCKHASH_SERVE_WINDOW); + public Eip7709BlockHashLookup(final Address contractAddress, final long historyServeWindow) { + this(contractAddress, historyServeWindow, BLOCKHASH_SERVE_WINDOW); } /** - * Constructs a ContractBasedBlockHashLookup with a specified blockHashServeWindow. This - * constructor is only used for testing. + * Constructs a Eip7709BlockHashLookup with a specified blockHashServeWindow. This constructor is + * only used for testing. * - * @param currentBlock current block header being processed. * @param contractAddress the address of the contract storing the history. * @param historyServeWindow the number of blocks for which history should be saved. * @param blockHashServeWindow the number of block for which contract can serve the BLOCKHASH * opcode. */ @VisibleForTesting - ContractBasedBlockHashLookup( - final ProcessableBlockHeader currentBlock, + Eip7709BlockHashLookup( final Address contractAddress, final long historyServeWindow, final long blockHashServeWindow) { - this.blockHeader = currentBlock; this.contractAddress = contractAddress; this.historyServeWindow = historyServeWindow; this.blockHashServeWindow = blockHashServeWindow; @@ -83,10 +74,10 @@ public ContractBasedBlockHashLookup( @Override public Hash apply(final MessageFrame frame, final Long blockNumber) { - final long currentBlockNumber = blockHeader.getNumber(); + final long currentBlockNumber = frame.getBlockValues().getNumber(); final long minBlockServe = Math.max(0, currentBlockNumber - blockHashServeWindow); if (blockNumber >= currentBlockNumber || blockNumber < minBlockServe) { - LOG.debug("failed to read hash from system account for block {}", blockNumber); + LOG.trace("failed to read hash from system account for block {}", blockNumber); return ZERO; } diff --git a/ethereum/core/src/test/java/org/hyperledger/besu/ethereum/vm/ContractBasedBlockHashLookupTest.java b/ethereum/core/src/test/java/org/hyperledger/besu/ethereum/vm/Eip7709BlockHashLookupTest.java similarity index 84% rename from ethereum/core/src/test/java/org/hyperledger/besu/ethereum/vm/ContractBasedBlockHashLookupTest.java rename to ethereum/core/src/test/java/org/hyperledger/besu/ethereum/vm/Eip7709BlockHashLookupTest.java index 0b8201bbe70..d9cf91d78b8 100644 --- a/ethereum/core/src/test/java/org/hyperledger/besu/ethereum/vm/ContractBasedBlockHashLookupTest.java +++ b/ethereum/core/src/test/java/org/hyperledger/besu/ethereum/vm/Eip7709BlockHashLookupTest.java @@ -33,6 +33,7 @@ import org.hyperledger.besu.evm.blockhash.BlockHashLookup; import org.hyperledger.besu.evm.fluent.SimpleAccount; import org.hyperledger.besu.evm.fluent.SimpleWorld; +import org.hyperledger.besu.evm.frame.BlockValues; import org.hyperledger.besu.evm.frame.MessageFrame; import org.hyperledger.besu.evm.worldstate.WorldUpdater; @@ -44,7 +45,7 @@ import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; -public class ContractBasedBlockHashLookupTest { +public class Eip7709BlockHashLookupTest { private static final long BLOCKHASH_SERVE_WINDOW = 160; private static final Address STORAGE_ADDRESS = Address.fromHexString("0x0"); private static final long HISTORY_SERVE_WINDOW = 200L; @@ -57,15 +58,9 @@ public class ContractBasedBlockHashLookupTest { @BeforeEach void setUp() { headers = new ArrayList<>(); - frame = mock(MessageFrame.class); - final WorldUpdater worldUpdater = createWorldUpdater(0, CURRENT_BLOCK_NUMBER); - when(frame.getWorldUpdater()).thenReturn(worldUpdater); + frame = createMessageFrame(CURRENT_BLOCK_NUMBER, createWorldUpdater(0, CURRENT_BLOCK_NUMBER)); lookup = - new ContractBasedBlockHashLookup( - createHeader(CURRENT_BLOCK_NUMBER, headers.getLast()), - STORAGE_ADDRESS, - HISTORY_SERVE_WINDOW, - BLOCKHASH_SERVE_WINDOW); + new Eip7709BlockHashLookup(STORAGE_ADDRESS, HISTORY_SERVE_WINDOW, BLOCKHASH_SERVE_WINDOW); } private WorldUpdater createWorldUpdater(final int fromBlockNumber, final int toBlockNumber) { @@ -83,6 +78,16 @@ private WorldUpdater createWorldUpdater(final int fromBlockNumber, final int toB return worldUpdaterMock; } + private MessageFrame createMessageFrame( + final long currentBlockNumber, final WorldUpdater worldUpdater) { + final MessageFrame messageFrame = mock(MessageFrame.class); + final BlockValues blockValues = mock(BlockValues.class); + when(blockValues.getNumber()).thenReturn(currentBlockNumber); + when(messageFrame.getBlockValues()).thenReturn(blockValues); + when(messageFrame.getWorldUpdater()).thenReturn(worldUpdater); + return messageFrame; + } + @Test void shouldGetHashOfImmediateParent() { assertHashForBlockNumber(CURRENT_BLOCK_NUMBER - 1); @@ -107,15 +112,12 @@ void shouldReturnEmptyHashWhenSystemContractNotExists() { @Test void shouldReturnEmptyHashWhenParentBlockNotInContract() { - final WorldUpdater worldUpdater = - createWorldUpdater(CURRENT_BLOCK_NUMBER - 10, CURRENT_BLOCK_NUMBER); - when(frame.getWorldUpdater()).thenReturn(worldUpdater); + frame = + createMessageFrame( + CURRENT_BLOCK_NUMBER, + createWorldUpdater(CURRENT_BLOCK_NUMBER - 10, CURRENT_BLOCK_NUMBER)); lookup = - new ContractBasedBlockHashLookup( - new BlockHeaderTestFixture().number(CURRENT_BLOCK_NUMBER).buildHeader(), - STORAGE_ADDRESS, - HISTORY_SERVE_WINDOW, - BLOCKHASH_SERVE_WINDOW); + new Eip7709BlockHashLookup(STORAGE_ADDRESS, HISTORY_SERVE_WINDOW, BLOCKHASH_SERVE_WINDOW); assertHashForBlockNumber(CURRENT_BLOCK_NUMBER - 20, Hash.ZERO); } @@ -147,14 +149,9 @@ void shouldCacheBlockHashes() { @Test void shouldGetHashWhenParentIsGenesis() { - final WorldUpdater worldUpdater = createWorldUpdater(0, 1); - when(frame.getWorldUpdater()).thenReturn(worldUpdater); + frame = createMessageFrame(1, createWorldUpdater(0, 1)); lookup = - new ContractBasedBlockHashLookup( - createHeader(1, headers.getFirst()), - STORAGE_ADDRESS, - HISTORY_SERVE_WINDOW, - BLOCKHASH_SERVE_WINDOW); + new Eip7709BlockHashLookup(STORAGE_ADDRESS, HISTORY_SERVE_WINDOW, BLOCKHASH_SERVE_WINDOW); assertHashForBlockNumber(0); }