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

Skip dataAvailabilityMode in program output #413

Merged
merged 5 commits into from
Jun 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion packages/backend/src/Application.ts
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,8 @@ export class Application {
: undefined

const perpetualCairoOutputCollector = new PerpetualCairoOutputCollector(
ethereumClient
ethereumClient,
config.starkex
)
const rollupStateRepository = new MerkleTreeRepository(
database,
Expand Down
3 changes: 2 additions & 1 deletion packages/backend/src/core/PerpetualValidiumSyncService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@ export class PerpetualValidiumSyncService implements IDataSyncService {
for (const stateTransition of stateTransitions) {
const [perpetualCairoOutput, batch] = await Promise.all([
this.perpetualCairoOutputCollector.collect(
stateTransition.transactionHash
stateTransition.transactionHash,
stateTransition.blockNumber
),
this.availabilityGatewayClient.getPerpetualBatchData(
stateTransition.batchId
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,39 @@ import {
import { Hash256 } from '@explorer/types'
import { BigNumber, utils } from 'ethers'

import { StarkexConfig } from '../../config/starkex/StarkexConfig'
import { EthereumClient } from '../../peripherals/ethereum/EthereumClient'
import { toHexData } from '../../utils/toHexData'
import { shouldSkipDataAvailabilityModeField } from './shouldSkipDataAvailabilityModeField'

const ABI = new utils.Interface([
'function updateState(uint256[] calldata programOutput, uint256[] calldata applicationData)',
])

export class PerpetualCairoOutputCollector {
constructor(private readonly ethereumClient: EthereumClient) {}
constructor(
private readonly ethereumClient: EthereumClient,
private readonly starkexConfig: StarkexConfig
) {}

async collect(transactionHash: Hash256): Promise<PerpetualCairoOutput> {
async collect(
transactionHash: Hash256,
blockNumber: number
): Promise<PerpetualCairoOutput> {
const tx = await this.ethereumClient.getTransaction(transactionHash)
if (!tx) {
throw new Error('Invalid transaction')
}
const decoded = ABI.decodeFunctionData('updateState', tx.data)
const programOutputValues = decoded.programOutput as BigNumber[]
const hexData = toHexData(programOutputValues)
return decodePerpetualCairoOutput(hexData)

const skipDataAvailabilityModeField = shouldSkipDataAvailabilityModeField(
blockNumber,
this.starkexConfig.instanceName,
this.starkexConfig.blockchain.chainId
)

return decodePerpetualCairoOutput(hexData, skipDataAvailabilityModeField)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { expect } from 'earl'

import { shouldSkipDataAvailabilityModeField } from './shouldSkipDataAvailabilityModeField'

describe('shouldSkipDataAvailabilityModeField', () => {
it('should return true when instanceName is "ApeX", chainId is 5 and blockNumber is >= 8056029', () => {
expect(shouldSkipDataAvailabilityModeField(8056028, 'ApeX', 5)).toEqual(
false
)
expect(shouldSkipDataAvailabilityModeField(8056029, 'ApeX', 5)).toEqual(
true
)
expect(shouldSkipDataAvailabilityModeField(9000000, 'ApeX', 5)).toEqual(
true
)
})

it('should return false when instanceName is "Dydx", chainId is 1 and blockNumber is >= 8056029', () => {
expect(shouldSkipDataAvailabilityModeField(8056028, 'dYdX', 1)).toEqual(
false
)
expect(shouldSkipDataAvailabilityModeField(8056029, 'dYdX', 1)).toEqual(
false
)
expect(shouldSkipDataAvailabilityModeField(9000000, 'dYdX', 1)).toEqual(
false
)
})
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { InstanceName } from '@explorer/shared'

export function shouldSkipDataAvailabilityModeField(
Copy link
Contributor

Choose a reason for hiding this comment

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

In my opinion this function should be inside PerpetualCairoOutputCollector.ts

blockNumber: number,
instanceName: InstanceName,
chainId: number
) {
// This is a fix for Apex Goerli testnet, which switches
// to a different program output (one more field) at block 8056029.
// See: https://github.com/starkware-libs/stark-perpetual/blob/eaa284683deec190407fece98b96546d10f6ad67/src/services/perpetual/cairo/output/program_output.cairo#L36
return (
instanceName === 'ApeX' &&
chainId === 5 && // it's goerli
blockNumber >= 8056029
)
}
16 changes: 16 additions & 0 deletions packages/encoding/src/decoding/decodePerpetualCairoOutput.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { expect } from 'earl'

import DECODED_EXAMPLE from '../test/data/decoded-with-dataavail-field.json'
import ENCODED_EXAMPLE from '../test/data/encoded-with-dataavail-field.json'
import { decodePerpetualCairoOutput } from './decodePerpetualCairoOutput'

describe('decodePerpetualCairoOutput', () => {
it('decodes data with skipDataAvailabilityModeField', () => {
// passing true to skipDataAvailabilityModeField
const decoded = decodePerpetualCairoOutput(ENCODED_EXAMPLE, true)
const noBigInt = JSON.parse(
JSON.stringify(decoded, (k, v) => (typeof v === 'bigint' ? Number(v) : v))
)
expect(noBigInt).toEqual(DECODED_EXAMPLE)
})
})
9 changes: 8 additions & 1 deletion packages/encoding/src/decoding/decodePerpetualCairoOutput.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,16 @@ import { readForcedActions } from './readForcedActions'
import { readModifications } from './readModifications'
import { readState } from './readState'

export function decodePerpetualCairoOutput(data: string): PerpetualCairoOutput {
export function decodePerpetualCairoOutput(
data: string,
skipDataAvailabilityModeField?: boolean
): PerpetualCairoOutput {
const reader = new ByteReader(data)

if (skipDataAvailabilityModeField) {
const _dataAvailabilityMode = reader.readBigInt(32)
torztomasz marked this conversation as resolved.
Show resolved Hide resolved
}

const configurationHash = Hash256(reader.readHex(32))
const assetConfigHashes = readAssetConfigHashes(reader)
const oldState = readState(reader)
Expand Down
Loading