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

Print debug messages in integration tests #434

Merged
merged 3 commits into from
Oct 17, 2024
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
45 changes: 42 additions & 3 deletions packages/web3/src/contract/contract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,9 @@ import {
WebCrypto,
hexToBinUnsafe,
isDevnet,
HexString
HexString,
isHexString,
hexToString
} from '../utils'
import { contractIdFromAddress, groupOfAddress, addressFromContractId, subContractId } from '../address'
import { getCurrentNodeProvider } from '../global'
Expand Down Expand Up @@ -405,7 +407,7 @@ export class Contract extends Artifact {
printDebugMessages(funcName: string, messages: DebugMessage[]) {
if (isContractDebugMessageEnabled() && messages.length != 0) {
console.log(`Testing ${this.name}.${funcName}:`)
messages.forEach((m) => console.log(`> Contract @ ${m.contractAddress} - ${m.message}`))
messages.forEach((m) => printDebugMessage(m))
}
}

Expand Down Expand Up @@ -503,6 +505,7 @@ export class Contract extends Artifact {
fieldNames: ['address'],
fieldTypes: ['Address']
}
static DebugEventIndex = -3

static fromApiEvent(
event: node.ContractEventByTxId,
Expand Down Expand Up @@ -1494,6 +1497,38 @@ export async function testMethod<
} as TestContractResult<R, M>
}

function printDebugMessage(m: node.DebugMessage) {
console.log(`> Contract @ ${m.contractAddress} - ${m.message}`)
}

export async function getDebugMessagesFromTx(txId: HexString, provider?: NodeProvider) {
if (isHexString(txId) && txId.length === 64) {
const nodeProvider = provider ?? getCurrentNodeProvider()
const events = await nodeProvider.events.getEventsTxIdTxid(txId)
return events.events
.filter((e) => e.eventIndex === Contract.DebugEventIndex)
.map((e) => {
if (e.fields.length === 1 && e.fields[0].type === 'ByteVec') {
return {
contractAddress: e.contractAddress,
message: hexToString(e.fields[0].value as string)
}
} else {
throw new Error(`Invalid debug log: ${JSON.stringify(e.fields)}`)
}
})
} else {
throw new Error(`Invalid tx id: ${txId}`)
}
}

export async function printDebugMessagesFromTx(txId: HexString, provider?: NodeProvider) {
const messages = await getDebugMessagesFromTx(txId, provider)
if (messages.length > 0) {
messages.forEach((m) => printDebugMessage(m))
}
}

export class RalphMap<K extends Val, V extends Val> {
private readonly groupIndex: number
constructor(
Expand Down Expand Up @@ -1841,7 +1876,11 @@ export async function signExecuteMethod<I extends ContractInstance, F extends Fi
gasPrice: params.gasPrice
}

return await signer.signAndSubmitExecuteScriptTx(signerParams)
const result = await signer.signAndSubmitExecuteScriptTx(signerParams)
if (isContractDebugMessageEnabled() && (await contract.contract.isDevnet(signer))) {
await printDebugMessagesFromTx(result.txId, signer.nodeProvider)
}
return result
}

function getBytecodeTemplate(
Expand Down
13 changes: 12 additions & 1 deletion test/contract.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@ import {
u256Val,
ZERO_ADDRESS,
MINIMAL_CONTRACT_DEPOSIT,
ContractStateWithMaps
ContractStateWithMaps,
getDebugMessagesFromTx,
printDebugMessagesFromTx
} from '../packages/web3'
import { Contract, Script, getContractIdFromUnsignedTx } from '../packages/web3'
import {
Expand Down Expand Up @@ -310,6 +312,15 @@ describe('contract', function () {
expect(result.debugMessages[0].message).toEqual(`Hello, ${nullContractAddress}!`)
})

it('should get debug messages from tx', async () => {
const deployResult = await Debug.deploy(signer, { initialFields: {} })
const txResult = await deployResult.contractInstance.transact.debug({ signer })
const messages = await getDebugMessagesFromTx(txResult.txId)
expect(messages).toEqual([
{ contractAddress: deployResult.contractInstance.address, message: `Hello, ${ZERO_ADDRESS}!` }
])
})

it('should test assert!', async () => {
expect(Numbers.C).toEqual((1n << 256n) - 1n)

Expand Down
Loading