Skip to content

Commit

Permalink
fix ump (AcalaNetwork#242)
Browse files Browse the repository at this point in the history
  • Loading branch information
xlc authored and ns212 committed Apr 3, 2023
1 parent 6850417 commit c246bcb
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 33 deletions.
60 changes: 30 additions & 30 deletions packages/chopsticks/src/blockchain/block-builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import { Block, TaskCallResponse } from './block'
import { GenericExtrinsic } from '@polkadot/types'
import { HexString } from '@polkadot/util/types'
import { StorageLayer, StorageValueKind } from './storage-layer'
import { blake2AsHex } from '@polkadot/util-crypto'
import { compactAddLength, hexToU8a, stringToHex } from '@polkadot/util'
import { compactHex } from '../utils'
import { defaultLogger, truncate } from '../logger'
Expand Down Expand Up @@ -103,12 +102,16 @@ export const newHeader = async (head: Block) => {
return header
}

const initNewBlock = async (head: Block, header: Header, inherents: HexString[]) => {
const initNewBlock = async (head: Block, header: Header, inherents: HexString[], storageLayer?: StorageLayer) => {
const blockNumber = header.number.toNumber()
const hash: HexString = `0x${Math.round(Math.random() * 100000000)
.toString(16)
.padEnd(64, '0')}`
const newBlock = new Block(head.chain, blockNumber, hash, head, { header, extrinsics: [], storage: head.storage })
const newBlock = new Block(head.chain, blockNumber, hash, head, {
header,
extrinsics: [],
storage: storageLayer ?? head.storage,
})

{
// initialize block
Expand Down Expand Up @@ -147,24 +150,12 @@ export const buildBlock = async (
): Promise<[Block, HexString[]]> => {
const registry = await head.registry
const header = await newHeader(head)
const { block: newBlock } = await initNewBlock(head, header, inherents)

logger.info(
{
number: newBlock.number,
extrinsicsCount: extrinsics.length,
tempHash: newBlock.hash,
},
`Try building block #${newBlock.number.toLocaleString()}`
)

const pendingExtrinsics: HexString[] = []
const includedExtrinsic: HexString[] = []

let layer: StorageLayer | undefined
// apply ump via storage override hack
if (Object.keys(ump).length > 0) {
const meta = await head.meta
const layer = head.pushStorageLayer()
layer = new StorageLayer(head.storage)
for (const [paraId, upwardMessages] of Object.entries(ump)) {
const queueSize = meta.registry.createType('(u32, u32)', [
upwardMessages.length,
Expand All @@ -178,21 +169,29 @@ export const buildBlock = async (
[compactHex(meta.query.ump.relayDispatchQueues(paraId)), messages.toHex()],
[compactHex(meta.query.ump.relayDispatchQueueSize(paraId)), queueSize.toHex()],
])
}

logger.trace(
{
number: newBlock.number,
tempHash: newBlock.hash,
ump,
},
'Upward messages'
)
logger.trace({ paraId, upwardMessages: truncate(upwardMessages) }, 'Pushed UMP')
}

const needsDispatch = meta.registry.createType('Vec<u32>', Object.keys(ump))
layer.set(compactHex(meta.query.ump.needsDispatch()), needsDispatch.toHex())
}

const { block: newBlock } = await initNewBlock(head, header, inherents, layer)

logger.info(
{
number: newBlock.number,
extrinsicsCount: extrinsics.length,
umpCount: Object.keys(ump).length,
tempHash: newBlock.hash,
},
`Try building block #${newBlock.number.toLocaleString()}`
)

const pendingExtrinsics: HexString[] = []
const includedExtrinsic: HexString[] = []

// apply extrinsics
for (const extrinsic of extrinsics) {
try {
Expand Down Expand Up @@ -238,12 +237,13 @@ export const buildBlock = async (

logger.info(
{
hash: finalBlock.hash,
extrinsics: includedExtrinsic.map((x) => blake2AsHex(x, 256)),
pendingExtrinsics: pendingExtrinsics.length,
number: newBlock.number,
hash: finalBlock.hash,
extrinsics: truncate(includedExtrinsic),
pendingExtrinsicsCount: pendingExtrinsics.length,
ump: truncate(ump),
},
`Block built #${newBlock.number.toLocaleString()} hash ${finalBlock.hash}`
'Block built'
)

return [finalBlock, pendingExtrinsics]
Expand Down
15 changes: 14 additions & 1 deletion packages/chopsticks/src/blockchain/txpool.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import { EventEmitter } from 'node:stream'
import { GenericExtrinsic } from '@polkadot/types'
import { HexString } from '@polkadot/util/types'
import _ from 'lodash'

import { Blockchain } from '.'
import { Deferred, defer } from '../utils'
import { GenericExtrinsic } from '@polkadot/types'
import { InherentProvider } from './inherent'
import { buildBlock } from './block-builder'
import { defaultLogger, truncate } from '../logger'

const logger = defaultLogger.child({ name: 'txpool' })

export const APPLY_EXTRINSIC_ERROR = 'TxPool::ApplyExtrinsicError'

Expand Down Expand Up @@ -64,6 +67,8 @@ export class TxPool {
}

async submitExtrinsic(extrinsic: HexString) {
logger.debug({ extrinsic: truncate(extrinsic) }, 'submit extrinsic')

this.#pool.push({ extrinsic, signer: await this.#getSigner(extrinsic) })

this.#maybeBuildBlock()
Expand All @@ -76,6 +81,8 @@ export class TxPool {
}

submitUpwardMessages(id: number, ump: HexString[]) {
logger.debug({ id, ump: truncate(ump) }, 'submit upward messages')

if (!this.#ump[id]) {
this.#ump[id] = []
}
Expand All @@ -85,12 +92,16 @@ export class TxPool {
}

submitDownwardMessages(dmp: DownwardMessage[]) {
logger.debug({ dmp: truncate(dmp) }, 'submit downward messages')

this.#dmp.push(...dmp)

this.#maybeBuildBlock()
}

submitHorizontalMessages(id: number, hrmp: HorizontalMessage[]) {
logger.debug({ id, hrmp: truncate(hrmp) }, 'submit horizontal messages')

if (!this.#hrmp[id]) {
this.#hrmp[id] = []
}
Expand Down Expand Up @@ -177,6 +188,8 @@ export class TxPool {
}
const { params, deferred } = pending

logger.trace({ params }, 'build block')

const head = this.#chain.head
const inherents = await this.#inherentProvider.createInherents(head, params)
const [newBlock, pendingExtrinsics] = await buildBlock(
Expand Down
53 changes: 51 additions & 2 deletions packages/e2e/src/__snapshots__/xcm.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -452,6 +452,55 @@ exports[`XCM > Acala send upward messages to Polkadot > system events 2`] = `
"method": "ExtrinsicSuccess",
"section": "system",
},
{
"data": {
"amount": 100000000000,
"who": "13YMK2eYoAvStnzReuxBjMrAvPXmmdsURwZvc62PrdXimbNy",
},
"method": "Withdraw",
"section": "balances",
},
{
"data": {
"amount": "(rounded 100000000000)",
"who": "146SvjUZXoMaemdeiecyxgALeYMm8ZWh1yrGo8RtpoPfe7WL",
},
"method": "Deposit",
"section": "balances",
},
{
"data": {
"account": "146SvjUZXoMaemdeiecyxgALeYMm8ZWh1yrGo8RtpoPfe7WL",
},
"method": "NewAccount",
"section": "system",
},
{
"data": {
"account": "146SvjUZXoMaemdeiecyxgALeYMm8ZWh1yrGo8RtpoPfe7WL",
"freeBalance": "(rounded 100000000000)",
},
"method": "Endowed",
"section": "balances",
},
{
"data": {
"amount": "(rounded 410000000)",
"who": "12ZNJzjPBZUh8VV5cuJFkbbwMttFNkH39EhoeYcgGHsJd4MG",
},
"method": "Deposit",
"section": "balances",
},
{
"data": [
"(hash)",
{
"Complete": 4000000000,
},
],
"method": "ExecutedUpward",
"section": "ump",
},
{
"data": {
"dispatchInfo": {
Expand Down Expand Up @@ -520,12 +569,12 @@ exports[`XCM > Acala send upward messages to Polkadot 5`] = `
"consumers": "0",
"data": {
"feeFrozen": "0",
"free": "0",
"free": "99,591,353,032",
"miscFrozen": "0",
"reserved": "0",
},
"nonce": "0",
"providers": "0",
"providers": "1",
"sufficients": "0",
}
`;
Expand Down

0 comments on commit c246bcb

Please sign in to comment.