|
| 1 | +/* |
| 2 | + * Copyright contributors to Hyperledger Besu. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with |
| 5 | + * the License. You may obtain a copy of the License at |
| 6 | + * |
| 7 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | + * |
| 9 | + * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on |
| 10 | + * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the |
| 11 | + * specific language governing permissions and limitations under the License. |
| 12 | + * |
| 13 | + * SPDX-License-Identifier: Apache-2.0 |
| 14 | + */ |
| 15 | +package org.hyperledger.besu.ethereum.mainnet.parallelization; |
| 16 | + |
| 17 | +import static org.hyperledger.besu.ethereum.mainnet.parallelization.ParallelBlockProcessorTestSupport.ACCOUNT_GENESIS_1; |
| 18 | +import static org.hyperledger.besu.ethereum.mainnet.parallelization.ParallelBlockProcessorTestSupport.ACCOUNT_GENESIS_1_KEYPAIR; |
| 19 | +import static org.hyperledger.besu.ethereum.mainnet.parallelization.ParallelBlockProcessorTestSupport.ACCOUNT_GENESIS_2; |
| 20 | +import static org.hyperledger.besu.ethereum.mainnet.parallelization.ParallelBlockProcessorTestSupport.ACCOUNT_GENESIS_2_KEYPAIR; |
| 21 | +import static org.hyperledger.besu.ethereum.mainnet.parallelization.ParallelBlockProcessorTestSupport.CONTRACT_ADDRESS; |
| 22 | + |
| 23 | +import org.hyperledger.besu.datatypes.Address; |
| 24 | +import org.hyperledger.besu.datatypes.Wei; |
| 25 | +import org.hyperledger.besu.ethereum.core.Transaction; |
| 26 | + |
| 27 | +import java.util.Optional; |
| 28 | + |
| 29 | +import org.junit.jupiter.api.DisplayName; |
| 30 | +import org.junit.jupiter.api.Test; |
| 31 | + |
| 32 | +/** Integration tests for contract storage operations. */ |
| 33 | +public abstract class AbstractContractStorageTest |
| 34 | + extends AbstractParallelBlockProcessorIntegrationTest { |
| 35 | + |
| 36 | + private final Address contractAddr = Address.fromHexStringStrict(CONTRACT_ADDRESS); |
| 37 | + |
| 38 | + @Test |
| 39 | + @DisplayName("Writing different storage slots from the same sender produces matching state") |
| 40 | + void writeMultipleSlotsSameSender() { |
| 41 | + final Transaction txSetSlot1 = |
| 42 | + createContractCallTransaction( |
| 43 | + 0, contractAddr, "setSlot1", ACCOUNT_GENESIS_1_KEYPAIR, Optional.of(100)); |
| 44 | + final Transaction txSetSlot2 = |
| 45 | + createContractCallTransaction( |
| 46 | + 1, contractAddr, "setSlot2", ACCOUNT_GENESIS_1_KEYPAIR, Optional.of(200)); |
| 47 | + final Transaction txSetSlot3 = |
| 48 | + createContractCallTransaction( |
| 49 | + 2, contractAddr, "setSlot3", ACCOUNT_GENESIS_1_KEYPAIR, Optional.of(300)); |
| 50 | + |
| 51 | + final ComparisonResult result = |
| 52 | + executeAndCompare(Wei.of(5), txSetSlot1, txSetSlot2, txSetSlot3); |
| 53 | + |
| 54 | + assertContractStorage(result.seqWorldState(), contractAddr, 0, 100); |
| 55 | + assertContractStorage(result.seqWorldState(), contractAddr, 1, 200); |
| 56 | + assertContractStorage(result.seqWorldState(), contractAddr, 2, 300); |
| 57 | + |
| 58 | + assertContractStorageMatches(result.seqWorldState(), result.parWorldState(), contractAddr, 0); |
| 59 | + assertContractStorageMatches(result.seqWorldState(), result.parWorldState(), contractAddr, 1); |
| 60 | + assertContractStorageMatches(result.seqWorldState(), result.parWorldState(), contractAddr, 2); |
| 61 | + assertAccountsMatch( |
| 62 | + result.seqWorldState(), |
| 63 | + result.parWorldState(), |
| 64 | + Address.fromHexStringStrict(ACCOUNT_GENESIS_1)); |
| 65 | + } |
| 66 | + |
| 67 | + @Test |
| 68 | + @DisplayName("Writing then reading the same storage slot produces matching state") |
| 69 | + void writeThenReadSameSlot() { |
| 70 | + final Transaction txSetSlot1 = |
| 71 | + createContractCallTransaction( |
| 72 | + 0, contractAddr, "setSlot1", ACCOUNT_GENESIS_1_KEYPAIR, Optional.of(999)); |
| 73 | + final Transaction txGetSlot1 = |
| 74 | + createContractCallTransaction( |
| 75 | + 0, contractAddr, "getSlot1", ACCOUNT_GENESIS_2_KEYPAIR, Optional.empty()); |
| 76 | + |
| 77 | + final ComparisonResult result = executeAndCompare(Wei.of(5), txSetSlot1, txGetSlot1); |
| 78 | + |
| 79 | + assertContractStorage(result.seqWorldState(), contractAddr, 0, 999); |
| 80 | + assertContractStorageMatches(result.seqWorldState(), result.parWorldState(), contractAddr, 0); |
| 81 | + assertAccountsMatch( |
| 82 | + result.seqWorldState(), |
| 83 | + result.parWorldState(), |
| 84 | + Address.fromHexStringStrict(ACCOUNT_GENESIS_1)); |
| 85 | + assertAccountsMatch( |
| 86 | + result.seqWorldState(), |
| 87 | + result.parWorldState(), |
| 88 | + Address.fromHexStringStrict(ACCOUNT_GENESIS_2)); |
| 89 | + } |
| 90 | + |
| 91 | + @Test |
| 92 | + @DisplayName("Reading then writing the same storage slot produces matching state") |
| 93 | + void readThenWriteSameSlot() { |
| 94 | + final Transaction txGetSlot1 = |
| 95 | + createContractCallTransaction( |
| 96 | + 0, contractAddr, "getSlot1", ACCOUNT_GENESIS_1_KEYPAIR, Optional.empty()); |
| 97 | + final Transaction txSetSlot1 = |
| 98 | + createContractCallTransaction( |
| 99 | + 0, contractAddr, "setSlot1", ACCOUNT_GENESIS_2_KEYPAIR, Optional.of(777)); |
| 100 | + |
| 101 | + final ComparisonResult result = executeAndCompare(Wei.of(5), txGetSlot1, txSetSlot1); |
| 102 | + |
| 103 | + assertContractStorage(result.seqWorldState(), contractAddr, 0, 777); |
| 104 | + assertContractStorageMatches(result.seqWorldState(), result.parWorldState(), contractAddr, 0); |
| 105 | + } |
| 106 | + |
| 107 | + @Test |
| 108 | + @DisplayName("Write-read-write across two senders produces matching state") |
| 109 | + void writeReadWriteAcrossSenders() { |
| 110 | + final Transaction txSetSlot1 = |
| 111 | + createContractCallTransaction( |
| 112 | + 0, contractAddr, "setSlot1", ACCOUNT_GENESIS_1_KEYPAIR, Optional.of(100)); |
| 113 | + final Transaction txGetSlot1 = |
| 114 | + createContractCallTransaction( |
| 115 | + 0, contractAddr, "getSlot1", ACCOUNT_GENESIS_2_KEYPAIR, Optional.empty()); |
| 116 | + final Transaction txSetSlot2 = |
| 117 | + createContractCallTransaction( |
| 118 | + 1, contractAddr, "setSlot2", ACCOUNT_GENESIS_1_KEYPAIR, Optional.of(200)); |
| 119 | + final Transaction txSetSlot3 = |
| 120 | + createContractCallTransaction( |
| 121 | + 2, contractAddr, "setSlot3", ACCOUNT_GENESIS_1_KEYPAIR, Optional.of(300)); |
| 122 | + |
| 123 | + final ComparisonResult result = |
| 124 | + executeAndCompare(Wei.of(5), txSetSlot1, txGetSlot1, txSetSlot2, txSetSlot3); |
| 125 | + |
| 126 | + assertContractStorage(result.seqWorldState(), contractAddr, 0, 100); |
| 127 | + assertContractStorage(result.seqWorldState(), contractAddr, 1, 200); |
| 128 | + assertContractStorage(result.seqWorldState(), contractAddr, 2, 300); |
| 129 | + |
| 130 | + for (int slot = 0; slot <= 2; slot++) { |
| 131 | + assertContractStorageMatches( |
| 132 | + result.seqWorldState(), result.parWorldState(), contractAddr, slot); |
| 133 | + } |
| 134 | + assertAccountsMatch( |
| 135 | + result.seqWorldState(), |
| 136 | + result.parWorldState(), |
| 137 | + Address.fromHexStringStrict(ACCOUNT_GENESIS_1)); |
| 138 | + assertAccountsMatch( |
| 139 | + result.seqWorldState(), |
| 140 | + result.parWorldState(), |
| 141 | + Address.fromHexStringStrict(ACCOUNT_GENESIS_2)); |
| 142 | + } |
| 143 | + |
| 144 | + @Test |
| 145 | + @DisplayName("Storage reads from different senders produce matching state") |
| 146 | + void readFromDifferentSenders() { |
| 147 | + final Transaction txGetSlot1A = |
| 148 | + createContractCallTransaction( |
| 149 | + 0, contractAddr, "getSlot1", ACCOUNT_GENESIS_1_KEYPAIR, Optional.empty()); |
| 150 | + final Transaction txGetSlot1B = |
| 151 | + createContractCallTransaction( |
| 152 | + 0, contractAddr, "getSlot1", ACCOUNT_GENESIS_2_KEYPAIR, Optional.empty()); |
| 153 | + |
| 154 | + final ComparisonResult result = executeAndCompare(Wei.of(5), txGetSlot1A, txGetSlot1B); |
| 155 | + |
| 156 | + assertContractStorageMatches(result.seqWorldState(), result.parWorldState(), contractAddr, 0); |
| 157 | + assertAccountsMatch( |
| 158 | + result.seqWorldState(), |
| 159 | + result.parWorldState(), |
| 160 | + Address.fromHexStringStrict(ACCOUNT_GENESIS_1)); |
| 161 | + assertAccountsMatch( |
| 162 | + result.seqWorldState(), |
| 163 | + result.parWorldState(), |
| 164 | + Address.fromHexStringStrict(ACCOUNT_GENESIS_2)); |
| 165 | + } |
| 166 | +} |
0 commit comments