batch-encoding.ts 11 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432
import zlib from 'zlib'

import { parse, serialize, Transaction } from '@ethersproject/transactions'
import { Struct, BufferWriter, BufferReader } from 'bufio'
import { id } from '@ethersproject/hash'

import { remove0x } from '../common'

export interface BatchContext {
  numSequencedTransactions: number
  numSubsequentQueueTransactions: number
  timestamp: number
  blockNumber: number
}

export enum BatchType {
  LEGACY = -1,
  ZLIB = 0,
}

export interface AppendSequencerBatchParams {
  shouldStartAtElement: number // 5 bytes -- starts at batch
  totalElementsToAppend: number // 3 bytes -- total_elements_to_append
  contexts: BatchContext[] // total_elements[fixed_size[]]
  transactions: string[] // total_size_bytes[],total_size_bytes[]
  type?: BatchType
}

const APPEND_SEQUENCER_BATCH_METHOD_ID = 'appendSequencerBatch()'
const FOUR_BYTE_APPEND_SEQUENCER_BATCH = Buffer.from(
  id(APPEND_SEQUENCER_BATCH_METHOD_ID).slice(2, 10),
  'hex'
)

// Legacy support
// This function returns the serialized batch
// without the 4 byte selector and without the
// 0x prefix
export const encodeAppendSequencerBatch = (
  b: AppendSequencerBatchParams
): string => {
  for (const tx of b.transactions) {
    if (tx.length % 2 !== 0) {
      throw new Error('Unexpected uneven hex string value!')
    }
  }
  const batch = sequencerBatch.encode(b)
  const fnSelector = batch.slice(2, 10)
  if (fnSelector !== FOUR_BYTE_APPEND_SEQUENCER_BATCH.toString('hex')) {
    throw new Error(`Incorrect function signature`)
  }
  return batch.slice(10)
}

// Legacy support
// This function assumes there is no 4byte selector
// as part of the input data
export const decodeAppendSequencerBatch = (
  b: string
): AppendSequencerBatchParams => {
  const calldata =
    '0x' + FOUR_BYTE_APPEND_SEQUENCER_BATCH.toString('hex') + remove0x(b)
  return sequencerBatch.decode(calldata)
}

// Legacy support
export const sequencerBatch = {
  encode: (params: AppendSequencerBatchParams): string => {
    const batch = new SequencerBatch({
      shouldStartAtElement: params.shouldStartAtElement,
      totalElementsToAppend: params.totalElementsToAppend,
      contexts: params.contexts.map((c) => new Context(c)),
      transactions: params.transactions.map((t) =>
        BatchedTx.fromTransaction(t)
      ),
      type: params.type,
    })
    return batch.toHex()
  },
  decode: (b: string): AppendSequencerBatchParams => {
    const buf = Buffer.from(remove0x(b), 'hex')
    const fnSelector = buf.slice(0, 4)
    if (Buffer.compare(fnSelector, FOUR_BYTE_APPEND_SEQUENCER_BATCH) !== 0) {
      throw new Error(`Incorrect function signature`)
    }

    const batch = SequencerBatch.decode<SequencerBatch>(buf)
    const params: AppendSequencerBatchParams = {
      shouldStartAtElement: batch.shouldStartAtElement,
      totalElementsToAppend: batch.totalElementsToAppend,
      contexts: batch.contexts.map((c) => ({
        numSequencedTransactions: c.numSequencedTransactions,
        numSubsequentQueueTransactions: c.numSubsequentQueueTransactions,
        timestamp: c.timestamp,
        blockNumber: c.blockNumber,
      })),
      transactions: batch.transactions.map((t) => t.toHexTransaction()),
      type: batch.type,
    }

    return params
  },
}

export class Context extends Struct {
  // 3 bytes
  public numSequencedTransactions: number = 0
  // 3 bytes
  public numSubsequentQueueTransactions: number = 0
  // 5 bytes
  public timestamp: number = 0
  // 5 bytes
  public blockNumber: number = 0

  constructor(options: Partial<Context> = {}) {
    super()

    if (typeof options.numSequencedTransactions === 'number') {
      this.numSequencedTransactions = options.numSequencedTransactions
    }
    if (typeof options.numSubsequentQueueTransactions === 'number') {
      this.numSubsequentQueueTransactions =
        options.numSubsequentQueueTransactions
    }
    if (typeof options.timestamp === 'number') {
      this.timestamp = options.timestamp
    }
    if (typeof options.blockNumber === 'number') {
      this.blockNumber = options.blockNumber
    }
  }

  getSize(): number {
    return 16
  }

  write(bw: BufferWriter): BufferWriter {
    bw.writeU24BE(this.numSequencedTransactions)
    bw.writeU24BE(this.numSubsequentQueueTransactions)
    bw.writeU40BE(this.timestamp)
    bw.writeU40BE(this.blockNumber)
    return bw
  }

  read(br: BufferReader): this {
    this.numSequencedTransactions = br.readU24BE()
    this.numSubsequentQueueTransactions = br.readU24BE()
    this.timestamp = br.readU40BE()
    this.blockNumber = br.readU40BE()
    return this
  }

  toJSON() {
    return {
      numSequencedTransactions: this.numSequencedTransactions,
      numSubsequentQueueTransactions: this.numSubsequentQueueTransactions,
      timestamp: this.timestamp,
      blockNumber: this.blockNumber,
    }
  }
}

// transaction
export class BatchedTx extends Struct {
  // 3 bytes
  public txSize: number
  // rlp encoded transaction
  public raw: Buffer
  public tx: Transaction

  constructor(tx?: Transaction) {
    super()
    this.tx = tx
  }

  getSize(): number {
    if (this.raw && this.raw.length) {
      return this.raw.length + 3
    }
    const tx = serialize(
      {
        nonce: this.tx.nonce,
        gasPrice: this.tx.gasPrice,
        gasLimit: this.tx.gasLimit,
        to: this.tx.to,
        value: this.tx.value,
        data: this.tx.data,
      },
      {
        v: this.tx.v,
        r: this.tx.r,
        s: this.tx.s,
      }
    )

    // remove 0x prefix
    this.raw = Buffer.from(remove0x(tx), 'hex')
    return this.raw.length + 3
  }

  write(bw: BufferWriter): BufferWriter {
    bw.writeU24BE(this.txSize)
    bw.writeBytes(this.raw)
    return bw
  }

  read(br: BufferReader): this {
    this.txSize = br.readU24BE()
    this.raw = br.readBytes(this.txSize)
    return this
  }

  toTransaction(): Transaction {
    if (this.tx) {
      return this.tx
    }
    return parse(this.raw)
  }

  toHexTransaction(): string {
    if (this.raw) {
      return '0x' + this.raw.toString('hex')
    }
    return serialize(
      {
        nonce: this.tx.nonce,
        gasPrice: this.tx.gasPrice,
        gasLimit: this.tx.gasLimit,
        to: this.tx.to,
        value: this.tx.value,
        data: this.tx.data,
      },
      {
        v: this.tx.v,
        r: this.tx.r,
        s: this.tx.s,
      }
    )
  }

  toJSON() {
    if (!this.tx) {
      this.tx = parse(this.raw)
    }

    return {
      nonce: this.tx.nonce,
      gasPrice: this.tx.gasPrice.toString(),
      gasLimit: this.tx.gasLimit.toString(),
      to: this.tx.to,
      value: this.tx.value.toString(),
      data: this.tx.data,
      v: this.tx.v,
      r: this.tx.r,
      s: this.tx.s,
      chainId: this.tx.chainId,
      hash: this.tx.hash,
      from: this.tx.from,
    }
  }

  // TODO: inconsistent API with toTransaction
  // but unnecessary right now
  // this should be fromHexTransaction
  fromTransaction(tx: string): this {
    this.raw = Buffer.from(remove0x(tx), 'hex')
    this.txSize = this.raw.length
    return this
  }

  fromHex(s: string, extra?: object): this {
    const buffer = Buffer.from(remove0x(s), 'hex')
    return this.decode(buffer, extra)
  }

  static fromTransaction(s: string) {
    return new this().fromTransaction(s)
  }
}

export class SequencerBatch extends Struct {
  // 5 bytes
  public shouldStartAtElement: number
  // 3 bytes
  public totalElementsToAppend: number
  // 3 byte header for count, []Context
  public contexts: Context[]
  // []3 byte size, rlp encoded tx
  public transactions: BatchedTx[]

  // The batch type that determines how
  // it is serialized
  public type: BatchType

  constructor(options: Partial<SequencerBatch> = {}) {
    super()
    this.contexts = []
    this.transactions = []

    if (typeof options.shouldStartAtElement === 'number') {
      this.shouldStartAtElement = options.shouldStartAtElement
    }
    if (typeof options.totalElementsToAppend === 'number') {
      this.totalElementsToAppend = options.totalElementsToAppend
    }
    if (Array.isArray(options.contexts)) {
      this.contexts = options.contexts
    }
    if (Array.isArray(options.transactions)) {
      this.transactions = options.transactions
    }
    if (typeof options.type === 'number') {
      this.type = options.type
    }
  }

  write(bw: BufferWriter): BufferWriter {
    bw.writeBytes(FOUR_BYTE_APPEND_SEQUENCER_BATCH)

    bw.writeU40BE(this.shouldStartAtElement)
    bw.writeU24BE(this.totalElementsToAppend)

    const contexts = this.contexts.slice()
    if (this.type === BatchType.ZLIB) {
      contexts.unshift(
        new Context({
          blockNumber: 0,
          timestamp: 0,
          numSequencedTransactions: 0,
          numSubsequentQueueTransactions: 0,
        })
      )
    }
    bw.writeU24BE(contexts.length)

    for (const context of contexts) {
      context.write(bw)
    }

    if (this.type === BatchType.ZLIB) {
      const writer = new BufferWriter()
      for (const tx of this.transactions) {
        tx.write(writer)
      }
      const compressed = zlib.deflateSync(writer.render())
      bw.writeBytes(compressed)
    } else {
      // Legacy
      for (const tx of this.transactions) {
        tx.write(bw)
      }
    }

    return bw
  }

  read(br: BufferReader): this {
    const selector = br.readBytes(4)
    if (Buffer.compare(selector, FOUR_BYTE_APPEND_SEQUENCER_BATCH) !== 0) {
      br.seek(-4)
    }

    this.type = BatchType.LEGACY
    this.shouldStartAtElement = br.readU40BE()
    this.totalElementsToAppend = br.readU24BE()

    const contexts = br.readU24BE()
    for (let i = 0; i < contexts; i++) {
      const context = Context.read<Context>(br)
      this.contexts.push(context)
    }

    // handle typed batches
    if (this.contexts.length > 0 && this.contexts[0].timestamp === 0) {
      switch (this.contexts[0].blockNumber) {
        case 0: {
          this.type = BatchType.ZLIB
          const bytes = br.readBytes(br.left())
          const inflated = zlib.inflateSync(bytes)
          br = new BufferReader(inflated)

          // remove the dummy context
          this.contexts = this.contexts.slice(1)
          break
        }
      }
    }

    for (const context of this.contexts) {
      for (let i = 0; i < context.numSequencedTransactions; i++) {
        const tx = BatchedTx.read<BatchedTx>(br)
        this.transactions.push(tx)
      }
    }

    return this
  }

  getSize(): number {
    if (this.type === BatchType.ZLIB) {
      return -1
    }

    let size = 8 + 3 + 4
    for (const context of this.contexts) {
      size += context.getSize()
    }

    for (const tx of this.transactions) {
      size += tx.getSize()
    }
    return size
  }

  fromHex(s: string, extra?: object): this {
    const buffer = Buffer.from(remove0x(s), 'hex')
    return this.decode(buffer, extra)
  }

  toHex(): string {
    return '0x' + this.encode().toString('hex')
  }

  toJSON() {
    return {
      shouldStartAtElement: this.shouldStartAtElement,
      totalElementsToAppend: this.totalElementsToAppend,
      contexts: this.contexts.map((c) => c.toJSON()),
      transactions: this.transactions.map((tx) => tx.toJSON()),
    }
  }
}