Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add test for checkpoint/revert #3320

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
173 changes: 173 additions & 0 deletions packages/vm/test/api/runTx.spec.ts
Expand Up @@ -80,6 +80,179 @@ describe('runTx() -> successful API parameter usage', async () => {
await simpleRun(vm, 'goerli (PoA), london HF, default SM - should run without errors')
})

it('simple run and revert', async () => {
const common = new Common({ chain: Chain.Mainnet, hardfork: Hardfork.London })
const vm = await VM.create({
common,
blockchain: await Blockchain.create({ validateConsensus: false, validateBlocks: false }),
})
/*
pragma solidity >=0.8.2 <0.9.0;
contract Storage {

uint256 public number;

function incr() public {
number++;
}
}
*/
const bytecode =
'0x608060405234801561001057600080fd5b50610164806100206000396000f3fe608060405234801561001057600080fd5b50600436106100365760003560e01c8063119fbbd41461003b5780638381f58a14610045575b600080fd5b610043610063565b005b61004d61007c565b60405161005a9190610091565b60405180910390f35b600080815480929190610075906100b6565b9190505550565b60005481565b61008b816100ac565b82525050565b60006020820190506100a66000830184610082565b92915050565b6000819050919050565b60006100c1826100ac565b91507fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff8214156100f4576100f36100ff565b5b600182019050919050565b7f4e487b7100000000000000000000000000000000000000000000000000000000600052601160045260246000fdfea26469706673582212209406edd37c93969bf043c9479e8dd9d97cb54f94deb6c31567a70e62288f9df764736f6c63430008070033'
let tx = getTransaction(
vm.common,
TransactionType.FeeMarketEIP1559,
true,
'',
true,
0,
bytecode,
undefined,
200000,
1
)

const caller = tx.getSenderAddress()
const acc = createAccount(BigInt(0), BigInt(1000000000))
await vm.stateManager.putAccount(caller, acc)

let block = Block.fromBlockData(
{
header: {
number: 0,
gasLimit: 8000000,
baseFeePerGas: '0x1',
},
transactions: [tx],
},
{ common }
)
// 1 - contract deployment
let resBlock = await vm.runBlock({
block,
generate: true,
skipNonce: true,
skipBlockValidation: true,
skipBalance: false,
})
let res = resBlock.results[0]
const contractAddress = res.createdAddress?.toString()

// 2 - checkpoint
// at time of checkpoint, "number" is 0
await vm.evm.journal.checkpoint()

// 3 - call to "incr"
// after this tx, number is 1
tx = getTransaction(
vm.common,
TransactionType.FeeMarketEIP1559,
true,
'',
false,
1,
'0x119fbbd4',
contractAddress
)
block = Block.fromBlockData(
{
header: {
number: 1,
gasLimit: 8000000,
baseFeePerGas: '0x1',
},
transactions: [tx],
},
{ common }
)
resBlock = await vm.runBlock({
block,
generate: true,
skipNonce: true,
skipBlockValidation: true,
skipBalance: false,
})

// 4 - call to "number"
// this should return 1
tx = getTransaction(
vm.common,
TransactionType.FeeMarketEIP1559,
true,
'',
false,
1,
'0x8381f58a',
contractAddress
)
block = Block.fromBlockData(
{
header: {
number: 2,
gasLimit: 8000000,
baseFeePerGas: '0x1',
},
transactions: [tx],
},
{ common }
)
resBlock = await vm.runBlock({
block,
generate: true,
skipNonce: true,
skipBlockValidation: true,
skipBalance: false,
})
res = resBlock.results[0]

// return value should be '0x0000000000000000000000000000000000000000000000000000000000000001', because incr has been called.
assert.equal(
bytesToHex(res.execResult.returnValue),
'0x0000000000000000000000000000000000000000000000000000000000000001'
)

// 5 -revert
await vm.evm.journal.revert()

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would expect some assert() here (at least additionally, before doing another thing (runBlock() again) since it is the revert() what you seem to want to test?

// 6 - call to "number"
// this should return 1
tx = getTransaction(
vm.common,
TransactionType.FeeMarketEIP1559,
true,
'',
false,
1,
'0x8381f58a',
contractAddress
)
block = Block.fromBlockData(
{
header: {
number: 2,
gasLimit: 8000000,
baseFeePerGas: '0x1',
},
transactions: [tx],
},
{ common }
)
resBlock = await vm.runBlock({
block,
generate: true,
skipNonce: true,
skipBlockValidation: true,
skipBalance: false,
})
res = resBlock.results[0]

// return value should be "0x0000000000000000000000000000000000000000000000000000000000000000" as the state is at this point reverted.
assert.equal(
bytesToHex(res.execResult.returnValue),
'0x0000000000000000000000000000000000000000000000000000000000000000'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you state by adding a "should do x..." comment along assert() what you actually want to test here?

)
})

it('test successful hardfork matching', async () => {
const common = new Common({ chain: Chain.Mainnet, hardfork: Hardfork.London })
const vm = await VM.create({
Expand Down
14 changes: 8 additions & 6 deletions packages/vm/test/api/utils.ts
Expand Up @@ -53,21 +53,23 @@ export function getTransaction(
sign = false,
value = '0x00',
createContract = false,
nonce = 0
nonce = 0,
data = '0x7f7465737432000000000000000000000000000000000000000000000000000000600057',
to: string | undefined = '0x0000000000000000000000000000000000000000',
gasLimit: number = 90000,
gasPrice: number = 100
) {
let to: string | undefined = '0x0000000000000000000000000000000000000000'
let data = '0x7f7465737432000000000000000000000000000000000000000000000000000000600057'

if (createContract) {
to = undefined
data =
data ||
'0x6080604052348015600f57600080fd5b50603e80601d6000396000f3fe6080604052600080fdfea265627a7a723158204aed884a44fd1747efccba1447a2aa2d9a4b06dd6021c4a3bbb993021e0a909e64736f6c634300050f0032'
}

const txParams: any = {
nonce,
gasPrice: 100,
gasLimit: 90000,
gasPrice,
gasLimit,
to,
value,
data,
Expand Down