Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
luke7211 committed Jan 6, 2024
1 parent 30f51ca commit a99bbd6
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 55 deletions.
2 changes: 2 additions & 0 deletions packages/optimism-decoder/src/analyze.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const ctcMapping: Record<string, string | undefined> = {
'0x56a76bcC92361f6DF8D75476feD8843EdC70e1C9': 'Metis',
'0x6A1DB7d799FBA381F2a518cA859ED30cB8E1d41a': 'Metis 2.0',
'0xfBd2541e316948B259264c02f370eD088E04c3Db': 'Boba Network',
'0x5f7f7f6DB967F0ef10BdA0678964DBA185d16c50': 'Lyra',
}

export async function analyzeTransaction(
Expand All @@ -14,6 +15,7 @@ export async function analyzeTransaction(
) {
const tx = await provider.getTransaction(txHash)
const project = ctcMapping[tx.to ?? ''] ?? 'Unknown'
console.log('Tx submits data to', tx.to, 'hence it is', project)

return {
data: tx.data,
Expand Down
139 changes: 84 additions & 55 deletions packages/optimism-decoder/src/decode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,70 +24,99 @@ export async function decodeSequencerBatch(
data: string,
fourBytesApi: FourBytesApi,
): Promise<AppendSequencerBatchParams> {
console.log('Decoding', kind, 'L1 Sequencer transaction batch...')
console.log('Decoding', kind, 'L1 Sequencer transaction batch ...')
let reader = new BufferReader(Buffer.from(data.slice(2), 'hex'))

const methodName = reader.readBytes(4).toString('hex')
console.log('MethodName:', methodName)
if (kind === 'Lyra') {
const version = reader.readBytes(1).toString('hex')
console.log('Version:', version)
const channelId = reader.readBytes(16).toString('hex')
console.log('ChannelId:', channelId)
const frame_number = reader.readU16BE()
console.log('Frame Number:', frame_number)
const frame_data_length = reader.readU32BE()
console.log('Frame Data Length:', frame_data_length)
console.log(reader.left())
const bytes = reader.readBytes(reader.left() - 1)
const inflated = zlib.inflateSync(bytes)
reader = new BufferReader(inflated)
const decompressedBytes = reader.readBytes(reader.left())
console.log(add0x(decompressedBytes.toString('hex')))
const decoded = ethers.utils.RLP.decode(
// TODO: why this is failing ????
add0x(decompressedBytes.toString('hex')),
)
console.log(decoded)
} else {
const methodName = reader.readBytes(4).toString('hex')
console.log('MethodName:', methodName)

if (kind === 'Metis' || kind === 'Metis 2.0') {
const chainId = reader.readBytes(32).toString('hex')
console.log('ChainId:', chainId)
}
const shouldStartAtElement = reader.readU40BE()
const totalElementsToAppend = reader.readU24BE()
const contextCount = reader.readU24BE()
if (kind === 'Metis' || kind === 'Metis 2.0') {
const chainId = reader.readBytes(32).toString('hex')
console.log('ChainId:', chainId)
}
const shouldStartAtElement = reader.readU40BE()
const totalElementsToAppend = reader.readU24BE()
const contextCount = reader.readU24BE()

console.log('Should start at Element:', shouldStartAtElement)
console.log('Total Elements to Append:', totalElementsToAppend)
console.log('contextCount:', contextCount)
console.log('Should start at Element:', shouldStartAtElement)
console.log('Total Elements to Append:', totalElementsToAppend)
console.log('contextCount:', contextCount)

const contexts = []
for (let i = 0; i < contextCount; i++) {
const sequencerTxCount = reader.readU24BE()
const queueTxCount = reader.readU24BE()
const timestamp = reader.readU40BE()
const blockNumber = reader.readU40BE()
contexts.push({
sequencerTxCount,
queueTxCount,
timestamp,
blockNumber,
})
console.log(sequencerTxCount, queueTxCount, timestamp, blockNumber)
}
const contexts = []
for (let i = 0; i < contextCount; i++) {
const sequencerTxCount = reader.readU24BE()
const queueTxCount = reader.readU24BE()
const timestamp = reader.readU40BE()
const blockNumber = reader.readU40BE()
contexts.push({
sequencerTxCount,
queueTxCount,
timestamp,
blockNumber,
})
console.log(sequencerTxCount, queueTxCount, timestamp, blockNumber)
}

if (contexts[0].blockNumber === 0 && kind === 'Optimism OVM 2.0') {
console.log(
'Block number = 0 ? Transactions are compressed, nice.... Decompressing....',
)
contexts.slice(1) // remove dummy context that indicates compressed transaction data
const bytes = reader.readBytes(reader.left())
const inflated = zlib.inflateSync(bytes)
reader = new BufferReader(inflated)
}
if (contexts[0].blockNumber === 0 && kind === 'Optimism OVM 2.0') {
console.log(
'Block number = 0 ? Transactions are compressed, nice.... Decompressing....',
)
contexts.slice(1) // remove dummy context that indicates compressed transaction data
const bytes = reader.readBytes(reader.left())
const inflated = zlib.inflateSync(bytes)
reader = new BufferReader(inflated)
}

const transactions = []
for (const context of contexts) {
console.log('Block:', context.blockNumber, 'Timestamp:', context.timestamp)
for (let i = 0; i < context.sequencerTxCount; i++) {
const size = reader.readU24BE()
const raw = reader.readBytes(size).toString('hex')
const parsed = ethers.utils.parseTransaction(add0x(raw))
const methodHash = parsed.data.slice(0, 10)
const methodSignature = await fourBytesApi.getMethodSignature(methodHash)
transactions.push(add0x(raw))
console.log(' ', trimLong(add0x(raw)), methodHash, methodSignature)
const transactions = []
for (const context of contexts) {
console.log(
'Block:',
context.blockNumber,
'Timestamp:',
context.timestamp,
)
for (let i = 0; i < context.sequencerTxCount; i++) {
const size = reader.readU24BE()
const raw = reader.readBytes(size).toString('hex')
const parsed = ethers.utils.parseTransaction(add0x(raw))
const methodHash = parsed.data.slice(0, 10)
const methodSignature = await fourBytesApi.getMethodSignature(
methodHash,
)
transactions.push(add0x(raw))
console.log(' ', trimLong(add0x(raw)), methodHash, methodSignature)
}
}
}

console.log('Decoded', transactions.length, 'transactions')
console.log('Done decoding...')
console.log('Decoded', transactions.length, 'transactions')
console.log('Done decoding...')

return {
shouldStartAtElement,
totalElementsToAppend,
contexts,
transactions,
return {
shouldStartAtElement,
totalElementsToAppend,
contexts,
transactions,
}
}
}

0 comments on commit a99bbd6

Please sign in to comment.