Skip to content

Commit

Permalink
Add libs
Browse files Browse the repository at this point in the history
Add libs parsing if code is exotic. Via dton.io... Ironic. And dton has
broken testnet.
  • Loading branch information
1IxI1 committed Jul 19, 2024
1 parent 718f7fa commit 4aebc14
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 30 deletions.
11 changes: 8 additions & 3 deletions src/runner/runner.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { getEmulationWithStack, waitForRateLimit } from './runner';
import { AccountFromAPI } from './types';
import { linkToTx, mcSeqnoByShard, txToLinks } from './utils';

describe('Converter', () => {
describe.skip('Converter', () => {
const justHash =
'3e5f49798de239da5d8f80b4dc300204d37613e4203a3f7b877c04a88c81856b';
const toncx =
Expand Down Expand Up @@ -90,6 +90,8 @@ describe('Converter', () => {
});

describe('Runner', () => {
const withLibs =
'https://ton.cx/tx/47769590000001:TfGC2E6eG7522c/jW9AjwkTFWwPL0RhFd2mGiqLoW/Q=:EQC39c119oqPkaB-fiA8_EKfejP24_IyKCNEyKFUsvXsfIHe';
const first =
'https://ton.cx/tx/44640875000007:53bLbTDYoHiBHGJPz2/oGr1JvJ/SS7iVVeMTUi5PYpw=:EQCtJGu1Q5xptmRFuP16M2w01QValw3V8IiyxQczAf83YITE';
const txs = [
Expand All @@ -109,8 +111,11 @@ describe('Runner', () => {
'https://ton.cx/tx/46843694000021:nwjPEIK88JGyPRLFVzNYHKkBb62OyVSgZKuU6J0mLC0=:EQA--JhKKuYfb-WAw7vDWEfD4fg2WOt9AuLH6xHPvF0RTUNA',
];

// it('should emulate with libs', async () => {
// });
it('should emulate with libs', async () => {
await waitForRateLimit();
const res = await getEmulationWithStack(withLibs, false);
expect(res.stateUpdateHashOk).toBe(true);
});

it('should emulate first tx', async () => {
await waitForRateLimit();
Expand Down
72 changes: 47 additions & 25 deletions src/runner/runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
loadShardAccount,
Transaction,
loadTransaction,
Dictionary,
} from '@ton/core';
import {
loadConfigParamsAsSlice,
Expand All @@ -29,7 +30,7 @@ import {
TVMLog,
} from './types';
import { parseStack } from './stack';
import { linkToTx, mcSeqnoByShard, txToLinks } from './utils';
import { getLib, linkToTx, mcSeqnoByShard, txToLinks } from './utils';

function b64ToBigInt(b64: string): bigint {
return BigInt('0x' + Buffer.from(b64, 'base64').toString('hex'));
Expand Down Expand Up @@ -213,13 +214,37 @@ export async function getEmulationWithStack(
account = getAccountResult.account;
let initialShardAccount = createShardAccountFromAPI(account, address);

// 4.1 get account stete after all txs for (maybe) verification
const { account: accountAfter } = await clientV4.getAccount(
mcBlockSeqno,
address
const state = initialShardAccount.account?.storage.state;

// 4.1 Get libs if needed
const _libs = Dictionary.empty(
Dictionary.Keys.BigUint(256),
Dictionary.Values.Cell()
);
console.log(account.balance.coins, 'coins before');
if (isOurTxLastTx) console.log(accountAfter.balance.coins, 'coins after');
if (state?.type == 'active') {
sendStatus('Getting libs');
const code = state.state.code;
if (code instanceof Cell && code.bits.length == 256 + 8) {
const cs = code.beginParse(true);
const tag = cs.loadUint(8);
if (tag == 2) {
const libHash = cs.loadBuffer(32);
const libHashHex = libHash.toString('hex').toUpperCase();
const actualCode = await getLib(libHashHex, testnet);
_libs.set(BigInt(`0x${libHashHex}`), actualCode);
}
}
}
let libs: Cell | null = null;
if (_libs.size > 0) libs = beginCell().storeDictDirect(_libs).endCell();

// 4.2 get account state after all txs for (maybe) verification
// const { account: accountAfter } = await clientV4.getAccount(
// mcBlockSeqno,
// address
// );
// console.log(account.balance.coins, 'coins before');
// if (isOurTxLastTx) console.log(accountAfter.balance.coins, 'coins after');

// 5. prep. emulator

Expand All @@ -242,7 +267,7 @@ export async function getEmulationWithStack(

let _txRes = _executor.runTransaction({
config: blockConfig,
libs: null,
libs,
verbosity: _withStack ? 'full_location_stack_verbose' : 'short',
shardAccount: _shardAccountStr,
message: beginCell().store(storeMessage(_msg)).endCell(),
Expand Down Expand Up @@ -402,20 +427,20 @@ export async function getEmulationWithStack(
);
const endBalance = parsedShardAccount.account?.storage.balance.coins || 0n;

if (isOurTxLastTx) {
// we dont know mainnet balance if its not last tx in block
const balanceCheck = endBalance === BigInt(accountAfter.balance.coins);
if (!balanceCheck) {
console.error(
`Balance check failed, expected ${accountAfter.balance.coins} got ${endBalance}`
);
} else {
console.log('Balance check ok');
}
} else {
console.log('Balance check skipped');
console.log(endBalance, 'end balance');
}
// if (isOurTxLastTx) {
// // we dont know mainnet balance if its not last tx in block
// const balanceCheck = endBalance === BigInt(accountAfter.balance.coins);
// if (!balanceCheck) {
// console.error(
// `Balance check failed, expected ${accountAfter.balance.coins} got ${endBalance}`
// );
// } else {
// console.log('Balance check ok');
// }
// } else {
// console.log('Balance check skipped');
// console.log(endBalance, 'end balance');
// }

const theTx = loadTransaction(
Cell.fromBase64(txResCorrect.result.transaction).asSlice()
Expand Down Expand Up @@ -491,9 +516,6 @@ export async function getEmulationWithStack(
gasFees: computePhase.gasFees,
};

console.log(parsedShardAccount);
console.log(accountAfter);

return {
sender: src,
contract: dest,
Expand Down
28 changes: 26 additions & 2 deletions src/runner/utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Address } from '@ton/core';
import { Address, Cell } from '@ton/core';
import axios, { AxiosInstance, AxiosResponse } from 'axios';
import { z } from 'zod';
import {
Expand Down Expand Up @@ -67,7 +67,6 @@ export async function mcSeqnoByShard(
block.root_hash
);
}
console.log(block);
return {
mcSeqno: block.masterchain_block_ref.seqno,
randSeed: Buffer.from(block.rand_seed, 'base64'),
Expand All @@ -78,6 +77,31 @@ export async function mcSeqnoByShard(
}
}

export async function getLib(libhash: string, testnet: boolean): Promise<Cell> {
// gets a library by its hash from dton's graphql
const dtonEndpoint = `https://${testnet ? 'testnet.' : ''}dton.io/graphql`;
const graphqlQuery = {
query: `
query fetchAuthor {
get_lib(lib_hash: "${libhash}")
}
`,
variables: {},
};
try {
const res = await axios.post(dtonEndpoint, graphqlQuery, {
headers: {
'Content-Type': 'application/json',
},
});
const libB64 = res.data.data.get_lib;
return Cell.fromBase64(libB64);
} catch (error) {
console.error('Error fetching library:', error);
throw error;
}
}

export async function linkToTx(
txLink: string,
testnet: boolean
Expand Down

0 comments on commit 4aebc14

Please sign in to comment.