mirror of https://github.com/hyperledger/besu
Add EXTCODE* unit Tests (#7374)
Add unit tests to the EXTCODE* series operations. Also, put all EVM operations tests in the proper package. Signed-off-by: Danno Ferrin <danno@numisight.com>pull/7380/head
parent
e68a6c6a52
commit
e57c811e47
@ -0,0 +1,245 @@ |
|||||||
|
/* |
||||||
|
* Copyright contributors to Hyperledger Besu. |
||||||
|
* |
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with |
||||||
|
* the License. You may obtain a copy of the License at |
||||||
|
* |
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
* |
||||||
|
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on |
||||||
|
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the |
||||||
|
* specific language governing permissions and limitations under the License. |
||||||
|
* |
||||||
|
* SPDX-License-Identifier: Apache-2.0 |
||||||
|
*/ |
||||||
|
package org.hyperledger.besu.evm.operation; |
||||||
|
|
||||||
|
import static org.assertj.core.api.Assertions.assertThat; |
||||||
|
|
||||||
|
import org.hyperledger.besu.datatypes.Address; |
||||||
|
import org.hyperledger.besu.evm.EVM; |
||||||
|
import org.hyperledger.besu.evm.account.MutableAccount; |
||||||
|
import org.hyperledger.besu.evm.frame.MessageFrame; |
||||||
|
import org.hyperledger.besu.evm.gascalculator.PragueGasCalculator; |
||||||
|
import org.hyperledger.besu.evm.testutils.TestMessageFrameBuilder; |
||||||
|
import org.hyperledger.besu.evm.toy.ToyWorld; |
||||||
|
import org.hyperledger.besu.evm.worldstate.WorldUpdater; |
||||||
|
|
||||||
|
import java.util.Arrays; |
||||||
|
import java.util.Collection; |
||||||
|
|
||||||
|
import org.apache.tuweni.bytes.Bytes; |
||||||
|
import org.junit.jupiter.api.Test; |
||||||
|
import org.junit.jupiter.api.extension.ExtendWith; |
||||||
|
import org.junit.jupiter.params.ParameterizedTest; |
||||||
|
import org.junit.jupiter.params.provider.MethodSource; |
||||||
|
import org.mockito.Mock; |
||||||
|
import org.mockito.junit.jupiter.MockitoExtension; |
||||||
|
|
||||||
|
@ExtendWith(MockitoExtension.class) |
||||||
|
class ExtCodeCopyOperationTest { |
||||||
|
|
||||||
|
private static final Address REQUESTED_ADDRESS = Address.fromHexString("0x22222222"); |
||||||
|
|
||||||
|
private final ToyWorld toyWorld = new ToyWorld(); |
||||||
|
private final WorldUpdater worldStateUpdater = toyWorld.updater(); |
||||||
|
|
||||||
|
@Mock EVM evm; |
||||||
|
|
||||||
|
static Collection<Object[]> extCodeCopyTestVector() { |
||||||
|
return Arrays.asList( |
||||||
|
new Object[][] { |
||||||
|
{ |
||||||
|
"Copy after, no overlap", |
||||||
|
Bytes.fromHexString("0123456789abcdef000000000000000000000000000000000000000000000000"), |
||||||
|
32, |
||||||
|
0, |
||||||
|
8, |
||||||
|
Bytes.fromHexString( |
||||||
|
"00000000000000000000000000000000000000000000000000000000000000000123456789abcdef"), |
||||||
|
false, |
||||||
|
2609L |
||||||
|
}, |
||||||
|
{ |
||||||
|
"copy from uninitialized memory", |
||||||
|
Bytes.EMPTY, |
||||||
|
0, |
||||||
|
24, |
||||||
|
16, |
||||||
|
Bytes.fromHexString( |
||||||
|
"0x000000000000000000000000000000000000000000000000000000000000000000"), |
||||||
|
false, |
||||||
|
2606L |
||||||
|
}, |
||||||
|
{ |
||||||
|
"copy from initialized + uninitialized memory", |
||||||
|
Bytes.fromHexString( |
||||||
|
"0x0000000000000000000000000000000000000000000000000123456789abcdef"), |
||||||
|
64, |
||||||
|
24, |
||||||
|
16, |
||||||
|
Bytes.fromHexString( |
||||||
|
"0x000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000123456789abcdef000000000000000000000000000000000000000000000000"), |
||||||
|
false, |
||||||
|
2612L |
||||||
|
}, |
||||||
|
{ |
||||||
|
"overlapping src < dst", |
||||||
|
Bytes.fromHexString( |
||||||
|
"0x0123456789abcdef000000000000000000000000000000000000000000000000"), |
||||||
|
4, |
||||||
|
0, |
||||||
|
8, |
||||||
|
Bytes.fromHexString( |
||||||
|
"0x000000000123456789abcdef0000000000000000000000000000000000000000"), |
||||||
|
false, |
||||||
|
2606L |
||||||
|
}, |
||||||
|
{ |
||||||
|
"overlapping src > dst", |
||||||
|
Bytes.fromHexString( |
||||||
|
"0x00112233445566778899aabbccddeeff00000000000000000000000000000000"), |
||||||
|
0, |
||||||
|
4, |
||||||
|
8, |
||||||
|
Bytes.fromHexString( |
||||||
|
"0x445566778899aabb000000000000000000000000000000000000000000000000"), |
||||||
|
false, |
||||||
|
2606L |
||||||
|
}, |
||||||
|
{ |
||||||
|
"overlapping src == dst", |
||||||
|
Bytes.fromHexString( |
||||||
|
"0x00112233445566778899aabbccddeeff00000000000000000000000000000000"), |
||||||
|
4, |
||||||
|
4, |
||||||
|
8, |
||||||
|
Bytes.fromHexString( |
||||||
|
"0x00000000445566778899aabb0000000000000000000000000000000000000000"), |
||||||
|
false, |
||||||
|
2606L |
||||||
|
}, |
||||||
|
{ |
||||||
|
"EOF-reserved pre-eof", |
||||||
|
Bytes.fromHexString("0xEF009f918bf09f9fa9"), |
||||||
|
0, |
||||||
|
0, |
||||||
|
9, |
||||||
|
Bytes.fromHexString("0xEF009f918bf09f9fa9"), |
||||||
|
false, |
||||||
|
2606L |
||||||
|
}, |
||||||
|
{ |
||||||
|
"EOF-reserved post-epf", |
||||||
|
Bytes.fromHexString("0xEF009f918bf09f9fa9"), |
||||||
|
0, |
||||||
|
0, |
||||||
|
9, |
||||||
|
Bytes.fromHexString("0xEF000000000000000000"), |
||||||
|
true, |
||||||
|
2606L |
||||||
|
}, |
||||||
|
{ |
||||||
|
"EF-reserved pre-epf", |
||||||
|
Bytes.fromHexString("0xEFF09f918bf09f9fa9"), |
||||||
|
0, |
||||||
|
0, |
||||||
|
9, |
||||||
|
Bytes.fromHexString("0xEFF09f918bf09f9fa9"), |
||||||
|
false, |
||||||
|
2606L |
||||||
|
}, |
||||||
|
{ |
||||||
|
"EOF-reserved post-eof", |
||||||
|
Bytes.fromHexString("0xEFF09f918bf09f9fa9"), |
||||||
|
0, |
||||||
|
0, |
||||||
|
9, |
||||||
|
Bytes.fromHexString("0xEFF09f918bf09f9fa9"), |
||||||
|
true, |
||||||
|
2606L |
||||||
|
} |
||||||
|
}); |
||||||
|
} |
||||||
|
|
||||||
|
@SuppressWarnings("unused") |
||||||
|
@ParameterizedTest(name = "{0}") |
||||||
|
@MethodSource("extCodeCopyTestVector") |
||||||
|
void testExtCodeCopy( |
||||||
|
final String name, |
||||||
|
final Bytes code, |
||||||
|
final long dst, |
||||||
|
final long src, |
||||||
|
final long len, |
||||||
|
final Bytes expected, |
||||||
|
final boolean eof, |
||||||
|
final long gasCost) { |
||||||
|
final MutableAccount account = worldStateUpdater.getOrCreate(REQUESTED_ADDRESS); |
||||||
|
account.setCode(code); |
||||||
|
|
||||||
|
ExtCodeCopyOperation subject = new ExtCodeCopyOperation(new PragueGasCalculator(), eof); |
||||||
|
MessageFrame frame = |
||||||
|
new TestMessageFrameBuilder() |
||||||
|
.worldUpdater(worldStateUpdater) |
||||||
|
.pushStackItem(Bytes.ofUnsignedLong(len)) |
||||||
|
.pushStackItem(Bytes.ofUnsignedLong(src)) |
||||||
|
.pushStackItem(Bytes.ofUnsignedLong(dst)) |
||||||
|
.pushStackItem(REQUESTED_ADDRESS) |
||||||
|
.build(); |
||||||
|
|
||||||
|
Operation.OperationResult result = subject.execute(frame, evm); |
||||||
|
|
||||||
|
assertThat(frame.readMemory(0, expected.size())).isEqualTo(expected); |
||||||
|
assertThat(frame.memoryWordSize()).isEqualTo((expected.size() + 31) / 32); |
||||||
|
assertThat(result.getGasCost()).isEqualTo(gasCost); |
||||||
|
} |
||||||
|
|
||||||
|
@Test |
||||||
|
void testExtCodeCopyCold() { |
||||||
|
final MutableAccount account = worldStateUpdater.getOrCreate(REQUESTED_ADDRESS); |
||||||
|
Bytes code = Bytes.fromHexString("0xEFF09f918bf09f9fa9"); |
||||||
|
account.setCode(code); |
||||||
|
|
||||||
|
ExtCodeCopyOperation subject = new ExtCodeCopyOperation(new PragueGasCalculator(), false); |
||||||
|
MessageFrame frame = |
||||||
|
new TestMessageFrameBuilder() |
||||||
|
.worldUpdater(worldStateUpdater) |
||||||
|
.pushStackItem(Bytes.ofUnsignedLong(9)) |
||||||
|
.pushStackItem(Bytes.ofUnsignedLong(0)) |
||||||
|
.pushStackItem(Bytes.ofUnsignedLong(0)) |
||||||
|
.pushStackItem(REQUESTED_ADDRESS) |
||||||
|
.build(); |
||||||
|
frame.warmUpAddress(REQUESTED_ADDRESS); |
||||||
|
|
||||||
|
Operation.OperationResult result = subject.execute(frame, evm); |
||||||
|
|
||||||
|
assertThat(frame.readMemory(0, 9)).isEqualTo(code); |
||||||
|
assertThat(frame.memoryWordSize()).isEqualTo(1); |
||||||
|
assertThat(result.getGasCost()).isEqualTo(106); |
||||||
|
} |
||||||
|
|
||||||
|
@Test |
||||||
|
void testExtCodeEOFDirtyMemory() { |
||||||
|
final MutableAccount account = worldStateUpdater.getOrCreate(REQUESTED_ADDRESS); |
||||||
|
Bytes code = Bytes.fromHexString("0xEF009f918bf09f9fa9"); |
||||||
|
account.setCode(code); |
||||||
|
|
||||||
|
ExtCodeCopyOperation subject = new ExtCodeCopyOperation(new PragueGasCalculator(), true); |
||||||
|
MessageFrame frame = |
||||||
|
new TestMessageFrameBuilder() |
||||||
|
.worldUpdater(worldStateUpdater) |
||||||
|
.pushStackItem(Bytes.ofUnsignedLong(9)) |
||||||
|
.pushStackItem(Bytes.ofUnsignedLong(0)) |
||||||
|
.pushStackItem(Bytes.ofUnsignedLong(0)) |
||||||
|
.pushStackItem(REQUESTED_ADDRESS) |
||||||
|
.build(); |
||||||
|
frame.writeMemory(0, 15, Bytes.fromHexString("0x112233445566778899aabbccddeeff")); |
||||||
|
|
||||||
|
Operation.OperationResult result = subject.execute(frame, evm); |
||||||
|
|
||||||
|
assertThat(frame.readMemory(0, 16)) |
||||||
|
.isEqualTo(Bytes.fromHexString("0xEF0000000000000000aabbccddeeff00")); |
||||||
|
assertThat(frame.memoryWordSize()).isEqualTo(1); |
||||||
|
assertThat(result.getGasCost()).isEqualTo(2603); |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,183 @@ |
|||||||
|
/* |
||||||
|
* Copyright ConsenSys AG. |
||||||
|
* |
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with |
||||||
|
* the License. You may obtain a copy of the License at |
||||||
|
* |
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
* |
||||||
|
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on |
||||||
|
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the |
||||||
|
* specific language governing permissions and limitations under the License. |
||||||
|
* |
||||||
|
* SPDX-License-Identifier: Apache-2.0 |
||||||
|
*/ |
||||||
|
package org.hyperledger.besu.evm.operation; |
||||||
|
|
||||||
|
import static org.assertj.core.api.Assertions.assertThat; |
||||||
|
|
||||||
|
import org.hyperledger.besu.datatypes.Address; |
||||||
|
import org.hyperledger.besu.datatypes.Wei; |
||||||
|
import org.hyperledger.besu.evm.account.MutableAccount; |
||||||
|
import org.hyperledger.besu.evm.frame.BlockValues; |
||||||
|
import org.hyperledger.besu.evm.frame.MessageFrame; |
||||||
|
import org.hyperledger.besu.evm.gascalculator.ConstantinopleGasCalculator; |
||||||
|
import org.hyperledger.besu.evm.gascalculator.IstanbulGasCalculator; |
||||||
|
import org.hyperledger.besu.evm.gascalculator.PragueGasCalculator; |
||||||
|
import org.hyperledger.besu.evm.internal.Words; |
||||||
|
import org.hyperledger.besu.evm.operation.Operation.OperationResult; |
||||||
|
import org.hyperledger.besu.evm.testutils.FakeBlockValues; |
||||||
|
import org.hyperledger.besu.evm.testutils.TestMessageFrameBuilder; |
||||||
|
import org.hyperledger.besu.evm.toy.ToyWorld; |
||||||
|
import org.hyperledger.besu.evm.worldstate.WorldUpdater; |
||||||
|
|
||||||
|
import org.apache.tuweni.bytes.Bytes; |
||||||
|
import org.apache.tuweni.units.bigints.UInt256; |
||||||
|
import org.junit.jupiter.api.Test; |
||||||
|
|
||||||
|
class ExtCodeSizeOperationTest { |
||||||
|
|
||||||
|
private static final Address REQUESTED_ADDRESS = Address.fromHexString("0x22222222"); |
||||||
|
|
||||||
|
private final ToyWorld toyWorld = new ToyWorld(); |
||||||
|
private final WorldUpdater worldStateUpdater = toyWorld.updater(); |
||||||
|
|
||||||
|
private final ExtCodeSizeOperation operation = |
||||||
|
new ExtCodeSizeOperation(new ConstantinopleGasCalculator(), false); |
||||||
|
private final ExtCodeSizeOperation operationIstanbul = |
||||||
|
new ExtCodeSizeOperation(new IstanbulGasCalculator(), false); |
||||||
|
private final ExtCodeSizeOperation operationEOF = |
||||||
|
new ExtCodeSizeOperation(new PragueGasCalculator(), true); |
||||||
|
|
||||||
|
@Test |
||||||
|
void shouldCharge700Gas() { |
||||||
|
final OperationResult result = operation.execute(createMessageFrame(REQUESTED_ADDRESS), null); |
||||||
|
assertThat(result.getGasCost()).isEqualTo(700L); |
||||||
|
} |
||||||
|
|
||||||
|
@Test |
||||||
|
void istanbulShouldCharge700Gas() { |
||||||
|
final OperationResult result = |
||||||
|
operationIstanbul.execute(createMessageFrame(REQUESTED_ADDRESS), null); |
||||||
|
assertThat(result.getGasCost()).isEqualTo(700L); |
||||||
|
} |
||||||
|
|
||||||
|
@Test |
||||||
|
void shouldReturnZeroWhenAccountDoesNotExist() { |
||||||
|
final Bytes result = executeOperation(REQUESTED_ADDRESS); |
||||||
|
assertThat(result.trimLeadingZeros()).isEqualTo(Bytes.EMPTY); |
||||||
|
} |
||||||
|
|
||||||
|
@Test |
||||||
|
void shouldReturnSizeOfEmptyDataWhenAccountExistsButDoesNotHaveCode() { |
||||||
|
worldStateUpdater.getOrCreate(REQUESTED_ADDRESS).setBalance(Wei.of(1)); |
||||||
|
assertThat(executeOperation(REQUESTED_ADDRESS).toInt()).isZero(); |
||||||
|
} |
||||||
|
|
||||||
|
@Test |
||||||
|
void shouldReturnZeroWhenAccountExistsButIsEmpty() { |
||||||
|
worldStateUpdater.getOrCreate(REQUESTED_ADDRESS); |
||||||
|
assertThat(executeOperation(REQUESTED_ADDRESS).trimLeadingZeros()).isEqualTo(Bytes.EMPTY); |
||||||
|
} |
||||||
|
|
||||||
|
@Test |
||||||
|
void shouldReturnZeroWhenPrecompiledContractHasNoBalance() { |
||||||
|
assertThat(executeOperation(Address.ECREC).trimLeadingZeros()).isEqualTo(Bytes.EMPTY); |
||||||
|
} |
||||||
|
|
||||||
|
@Test |
||||||
|
void shouldReturnEmptyCodeSizeWhenPrecompileHasBalance() { |
||||||
|
// Sending money to a precompile causes it to exist in the world state archive.
|
||||||
|
worldStateUpdater.getOrCreate(Address.ECREC).setBalance(Wei.of(10)); |
||||||
|
assertThat(executeOperation(Address.ECREC).toInt()).isZero(); |
||||||
|
} |
||||||
|
|
||||||
|
@Test |
||||||
|
void shouldGetSizeOfAccountCodeWhenCodeIsPresent() { |
||||||
|
final Bytes code = Bytes.fromHexString("0xabcdef"); |
||||||
|
final MutableAccount account = worldStateUpdater.getOrCreate(REQUESTED_ADDRESS); |
||||||
|
account.setCode(code); |
||||||
|
assertThat(executeOperation(REQUESTED_ADDRESS).toInt()).isEqualTo(3); |
||||||
|
} |
||||||
|
|
||||||
|
@Test |
||||||
|
void shouldZeroOutLeftMostBitsToGetAddress() { |
||||||
|
// If EXTCODESIZE of A is X, then EXTCODESIZE of A + 2**160 is X.
|
||||||
|
final Bytes code = Bytes.fromHexString("0xabcdef"); |
||||||
|
final MutableAccount account = worldStateUpdater.getOrCreate(REQUESTED_ADDRESS); |
||||||
|
account.setCode(code); |
||||||
|
final UInt256 value = |
||||||
|
UInt256.fromBytes(Words.fromAddress(REQUESTED_ADDRESS)) |
||||||
|
.add(UInt256.valueOf(2).pow(UInt256.valueOf(160))); |
||||||
|
final MessageFrame frame = createMessageFrame(value); |
||||||
|
operation.execute(frame, null); |
||||||
|
assertThat(frame.getStackItem(0).toInt()).isEqualTo(3); |
||||||
|
} |
||||||
|
|
||||||
|
@Test |
||||||
|
void shouldGetNonEOFSize() { |
||||||
|
final Bytes code = Bytes.fromHexString("0xEFF09f918bf09f9fa9"); |
||||||
|
final MutableAccount account = worldStateUpdater.getOrCreate(REQUESTED_ADDRESS); |
||||||
|
account.setCode(code); |
||||||
|
final UInt256 value = |
||||||
|
UInt256.fromBytes(Words.fromAddress(REQUESTED_ADDRESS)) |
||||||
|
.add(UInt256.valueOf(2).pow(UInt256.valueOf(160))); |
||||||
|
|
||||||
|
final MessageFrame frame = createMessageFrame(value); |
||||||
|
operation.execute(frame, null); |
||||||
|
assertThat(frame.getStackItem(0).toInt()).isEqualTo(9); |
||||||
|
|
||||||
|
final MessageFrame frameIstanbul = createMessageFrame(value); |
||||||
|
operationIstanbul.execute(frameIstanbul, null); |
||||||
|
assertThat(frame.getStackItem(0).toInt()).isEqualTo(9); |
||||||
|
|
||||||
|
final MessageFrame frameEOF = createMessageFrame(value); |
||||||
|
operationEOF.execute(frameEOF, null); |
||||||
|
assertThat(frame.getStackItem(0).toInt()).isEqualTo(9); |
||||||
|
} |
||||||
|
|
||||||
|
@Test |
||||||
|
void shouldGetEOFSize() { |
||||||
|
final Bytes code = Bytes.fromHexString("0xEF009f918bf09f9fa9"); |
||||||
|
final MutableAccount account = worldStateUpdater.getOrCreate(REQUESTED_ADDRESS); |
||||||
|
account.setCode(code); |
||||||
|
final UInt256 value = |
||||||
|
UInt256.fromBytes(Words.fromAddress(REQUESTED_ADDRESS)) |
||||||
|
.add(UInt256.valueOf(2).pow(UInt256.valueOf(160))); |
||||||
|
|
||||||
|
final MessageFrame frame = createMessageFrame(value); |
||||||
|
operation.execute(frame, null); |
||||||
|
assertThat(frame.getStackItem(0).toInt()).isEqualTo(9); |
||||||
|
|
||||||
|
final MessageFrame frameIstanbul = createMessageFrame(value); |
||||||
|
operationIstanbul.execute(frameIstanbul, null); |
||||||
|
|
||||||
|
assertThat(frameIstanbul.getStackItem(0).toInt()).isEqualTo(9); |
||||||
|
final MessageFrame frameEOF = createMessageFrame(value); |
||||||
|
operationEOF.execute(frameEOF, null); |
||||||
|
assertThat(frameEOF.getStackItem(0).toInt()).isEqualTo(2); |
||||||
|
} |
||||||
|
|
||||||
|
private Bytes executeOperation(final Address requestedAddress) { |
||||||
|
final MessageFrame frame = createMessageFrame(requestedAddress); |
||||||
|
operation.execute(frame, null); |
||||||
|
return frame.getStackItem(0); |
||||||
|
} |
||||||
|
|
||||||
|
private MessageFrame createMessageFrame(final Address requestedAddress) { |
||||||
|
final UInt256 stackItem = Words.fromAddress(requestedAddress); |
||||||
|
return createMessageFrame(stackItem); |
||||||
|
} |
||||||
|
|
||||||
|
private MessageFrame createMessageFrame(final UInt256 stackItem) { |
||||||
|
final BlockValues blockValues = new FakeBlockValues(1337); |
||||||
|
final MessageFrame frame = |
||||||
|
new TestMessageFrameBuilder() |
||||||
|
.worldUpdater(worldStateUpdater) |
||||||
|
.blockValues(blockValues) |
||||||
|
.build(); |
||||||
|
|
||||||
|
frame.pushStackItem(stackItem); |
||||||
|
return frame; |
||||||
|
} |
||||||
|
} |
Loading…
Reference in new issue