Skip to content

Commit

Permalink
add test for checkpoint/revert
Browse files Browse the repository at this point in the history
  • Loading branch information
yann300 committed Mar 13, 2024
1 parent 6766a5d commit 0094430
Show file tree
Hide file tree
Showing 2 changed files with 143 additions and 6 deletions.
135 changes: 135 additions & 0 deletions packages/vm/test/api/runTx.spec.ts
Expand Up @@ -80,6 +80,141 @@ 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 }
)
let resBlock = await vm.runBlock({
block,
generate: true,
skipNonce: true,
skipBlockValidation: true,
skipBalance: false,
})
let res = resBlock.results[0]
const contractAddress = res.createdAddress?.toString()

/*
const code = await vm.stateManager.getContractCode(res.createdAddress as Address)
console.log('code', code)
*/

// checkpoint
await vm.evm.journal.checkpoint()

// call to "incr"
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,
})

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

// call to "number"
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]

assert.equal(
bytesToHex(res.execResult.returnValue),
'0x0000000000000000000000000000000000000000000000000000000000000000'
)
})

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

0 comments on commit 0094430

Please sign in to comment.