tronprotocol / tronweb

Javascript API Library for interacting with the TRON Network
MIT License
423 stars 271 forks source link

External call for obtaining block header information when create a transaction #413

Closed VladLypovyi closed 6 months ago

VladLypovyi commented 1 year ago

After the 5.2.0 release, it claims to 'Support build transactions locally with protobuf'.

But the createTransaction method still calls the getHeaderInfo method, which requests information about the block from the network using a request to the node via 'wallet/getblock'.

file path: tronweb/src/lib/transactionBuilder.js

function getHeaderInfo(node) {
    return node.request('wallet/getblock', { detail: false }, 'post')
        .then((data) => {
            return {
                ref_block_bytes: data.block_header.raw_data.number.toString(16).slice(-4).padStart(4, '0'),
                ref_block_hash: data.blockID.slice(16, 32),
                expiration: data.block_header.raw_data.timestamp + 60 * 1000,
                timestamp: data.block_header.raw_data.timestamp,
            };
        });
}

async function createTransaction(tronWeb, type, value, Permission_id, options = {}) {
    const metaData = await getHeaderInfo(tronWeb.fullNode);
    const tx = {
        visible: false,
        txID: '',
        raw_data_hex: '',
        raw_data: {
            contract: [{
                parameter: {
                    value,
                    type_url: `type.googleapis.com/protocol.${type}`,
                },
                type,
            }],
            ...metaData,
            ...options,
        },
    };
    if (Permission_id) {
        tx.raw_data.contract[0].Permission_id = Permission_id;
    }
    const pb = txJsonToPb(tx);
    tx.txID = txPbToTxID(pb).replace(/^0x/, '');
    tx.raw_data_hex = txPbToRawDataHex(pb).toLowerCase();
    return tx;
}

Then what was the point of abandoning transaction building with a node request as it was before 5.2.0?

Maybe it should be possible to pass headerInfo manually in transaction creation parameters?

start940315 commented 1 year ago

As we know, header info is changing as time pass, and we must obtain it from the Tron chain. There are many connections between each prop. So if you want to change any of those props, we always recommend you to use our sdk, which is tested and in most situation, it is well performed.

VladLypovyi commented 1 year ago

Then it is unfair to say that since version 5.2.0. - 'Support for building transactions locally with protobuf'.

Since it's still an external request to the network.

start940315 commented 1 year ago

But the request only fetch the header info, the main building process is handled locally. The header info can only be fetched from the chain in any way. So we integrate it in TransactionBuilder.

DmytroShalaiev commented 1 year ago

Inspiration in passing header info by ourselves - because we obtain it in parallel, and don't need to use extra network requests in building scope. 'Support for building transactions locally with protobuf'. - but under the hood network call exists it's strange

start940315 commented 6 months ago

Tronweb v5.3.2 now supports custom block header! You can pass a field named blockHeader in options parameter.

unek commented 2 months ago

Tronweb v5.3.2 now supports custom block header! You can pass a field named blockHeader in options parameter.

trx.sendTransaction still ignoring these options? not sure if intended

start940315 commented 2 months ago

sendTransaction

You need to use transactionBuilder to trigger the action.

const options = {
    blockHeader: {...},
};
const transaction = await tronWeb.transactionBuilder.sendTrx(to, amount, address, options);
const signedTransaction = await tronWeb.trx.sign(transaction);
const result = await tronWeb.trx.sendRawTransaction(signedTransaction);