Life Cycle of a XinFin XDC Network Transaction
Learn How a XinFin Transaction is Created and Propagated to the Network
Transactions are at the heart of the XinFin blockchain (or any blockchain for that matter). When you interact with the XinFin blockchain, you are executing transactions and updating its state. Have you ever wondered what exactly happens when you execute a transaction in XinFin? Let’s try to understand it by looking at an example transaction. In this post, we will cover the following.
- An end-to-end traversal of a XinFin transaction starting from your browser/console to the XinFin XDC Network and back to your browser/console
- Understand how transactions work when you use a plugin such as XDCPay or XinFin Web Wallet instead of running your own node
- What to do if you are too paranoid and don’t trust any plugins to execute your transaction?
This post assumes that you have a basic understanding of XinFin and its components such as accounts, gas, and contracts. If you are a developer new to XinFin, you might find this helpful. This post will make more sense if you have executed a few transactions yourself. An example of a transaction is you sending some XDC to another person or a contract. Another example is if you have interacted with a dapp. For example, if you go here and buy some tokens, that would be a transaction. If you vote for a candidate, that would be another transaction.
1. End-to-end overview of a XinFin transaction
Let’s take the following contract call as an example and traverse through the entire flow of how this function call/transaction gets executed and gets permanently stored on the XinFin blockchain. You can find the entire contract here. At a high level, it’s a voting contract where you initialize a few candidates contesting in an election and anyone can vote for them. The votes will be recorded on the XinFin blockchain.
Voting.deployed().then(function(instance) {
instance.voteForCandidate(‘Ruslan’, {gas: 140000, from: web3.eth.accounts[0]}).then(function(r) {
console.log(“Voted successfully!”)
})
})

We assume that you have a XinFin Full node running locally on your computer or server which is connected to one of the networks (Testnet or Mainnet) and you have access to the contract address and ABI to execute the transaction.
If you have built a dapp, the above code should look familiar. There is a contract called Voting which has already been deployed on to the XinFin blockchain. We instantiate that contract and execute a method called voteForCandidate and pass in the candidate name, the gas limit for this transaction, and the account that executes this transaction. As the name indicates, this function is used to vote for a candidate and the vote is recorded on the blockchain. Below we will try to deconstruct this call and understand everything that happens when you execute this javascript function.
1. Construct the raw transaction object
The voteForCandidate function call is first converted into a raw transaction (rawTxn) as shown below. XDC3js library is used to build the raw transaction object.
txnCount = xdc3.eth.getTransactionCount(xdc3.eth.accounts[0])
var rawTxn = {
nonce: xdc3.toHex(txnCount),
gasPrice: xdc3.toHex(100000000000),
gasLimit: xdc3.toHex(140000),
to: ‘xdc633296baebc20f33ac2e1c1b105d7cd1f6a0718b’,
value: xdc3.toHex(0),
data: ‘0xcc9ab24952616d6100000000000000000000000000000000000000000000000000000000’
};
Let’s try to understand all the fields in the raw transaction object and how they are set.
Nonce: Each XinFin account has a field called nonce to keep track of the total number of transactions that the account has executed. The nonce is incremented for every new transaction and this allows the network to know the order in which the transactions need to be executed. The nonce is also used for the replay protection.
GasPrice: Price per unit of gas you are willing to pay for this transaction. If you are executing your transaction on the Mainnet, you will require main-net XDC as a gas fee on XinFin XDC Network is almost zero so the gas fee will be around 0.0001 XDC. Gas prices are currently measured in GWei and range from 0.1->100+Gwei. You will learn more about gas price and its impact later in this article.
GasLimit: Maximum gas you are willing to pay for this transaction. This value ensures that in case of an issue executing your transaction
To: The address to which you are directing this function call. This is the contract address (xdc633296baebc20f33ac2e1c1b105d7cd1f6a0718b) of the Voting contract in our case.
Value: Total XDC you want to send. When we executevoteForCandidate, we are not sending any XDC and so the value is 0. If you were executing a transaction to send XDC to another person or a contract, you would set this value.
Data: Let’s see how this data field is calculated.
You first take the function signature from the ABI voteForCandidate(bytes32) and generate the hash of it.
> xdc3.sha3(‘voteForCandidate(bytes32)’)
‘0xcc9ab267dda32b80892b2ae9e21b782dbf5562ef3e8919fc17cab72aa7db9d59’
You take the first 4 bytes of that hash. So, that would be 0xcc9ab267.
You then take the argument ‘Ruslan’ and convert to bytes32 and you get 52616d6100000000000000000000000000000000000000000000000000000000
You combine the two to get the data payload.
2. Sign the Transaction
If you remember, you used xdc3.eth.accounts[0] to execute the transaction. The XinFin XDC network needs to know that you actually own that account to make sure someone else doesn’t execute this transaction on your behalf. The way to prove this to the network is by signing the transaction using the private key corresponding to that account. The signed transaction looks like this:
const privateKey = Buffer.from(‘e331b6d69882b4ab4ea581s88e0b6s4039a3de5967d88dfdcffdd2270c0fd109’, ‘hex’)const txn = new EthereumTx(rawTxn)
txn.sign(privateKey)
const serializedTxn = txn.serialize()
3. Transaction is validated locally
The signed transaction is submitted to your local XinFinnode. Your local node will then validate the signed transaction to make sure that it was really signed by this account address.
4. Transaction is broadcast to the network
The signed transaction is broadcast by your XinFin Full node to its peers who in turn broadcast to their peers and so on. Once the transaction is broadcast to the network, your local node also outputs the transaction id which you can use to track the status of your transaction. This transaction id is just the hash of the signed transaction object.
transactionId = sha3(serializedTxn)

If you execute the transaction on a public XinFin XDC network, the best way to track the status of your transaction is on XinFin Explorer In the picture above, if you notice there are a couple of nodes marked as XinFin Explorer nodes. The folks at XinFin run a few nodes and they have a nice frontend webapp connected to it. When your transaction is picked up by their nodes, you can see your transaction on their website.
5. XinFin Master Node accepts the transaction
On the XinFin XDPoS, only masternodes can produce and validate blocks. Once coin holders deposit 10 million XDC to the Smart Contract, they are listed as masternode candidates in the DApp. Masternodes that work consistently within the system creating and verifying blocks are incentivized with XDC. XinFin Network engineers take responsibility for designing this fair, explicit, automated, and accountable reward mechanism.
Given that the XinFin masternode is a full node, it stores a copy of the blockchain, produces blocks, and keeps the chain consistent. These nodes are controlled by consortium members and come with a number of caveats. As previously noted, full nodes must purchase (and hold) fixed equity of XDC (more than 10,000,000) to be able to host the full XDC protocol. This design introduces the advantage that no Full Node or groups of Full Nodes have control over the network. For node holders to possess more than 51% hashing power, a full node must acquire more and more XDC. With increased demand, XDC prices rise, making it financially impossible for a Full Node cartel to control the XinFin Network.
The XinFin Network also employs Double Validation complemented with a Randomization mechanism to prevent unscrupulous characters from gaining control of the network. With these techniques, the network reduces the probability of having invalid blocks in the blockchain, ultimately reducing its effectiveness. These enhancements and the components of the XinFin Network.
6. XinFin Master Node finds a valid block and broadcasts to the network
XinFin Masternode eventually picks our transaction to include in the block along with other transactions. The Master Node can only pick so many transactions to include in the block because XinFin has set a block gas limit. i.e, the sum of all the gas limits on the transactions can not exceed the block gas limit. You can see the current gas limit at XinFin stats.
Once the XinFin Masternode selects the transactions to include in the block, the transactions are validated and included in a pending block and the XinFin Delegated proof of stake. One of the XinFin Master nodes eventually finds a valid block (by solving the XDPoS puzzle) and adds that block to the XinFin blockchain. Just like the raw transaction was broadcast by your local node which was received by all other nodes, the XinFin Master node broadcasts this valid block to other nodes.
7. Loca XinFinl Node receives/syncs the new block
Eventually, your local node will receive this new block and syncs it’s the local copy of the XinFin blockchain. Upon receiving the new block, the local node executes all the transactions in the block.
2. Using XDCPay instead of the local node

If you install the XDCPay browser plugin, you can manage your accounts in your browser. The keys are stored only on your browser, so you are the only one who has access to your account and the private key. When you execute a transaction in your browser, the plugin takes care of converting your function call into a raw transaction and sign the transaction using your private key. XDCPay runs their own Full nodes which they use to broadcast your transaction This way, you don’t even have to run your own XinFin full node.
3. Offline signing
What if you are not comfortable using a plugin or if you are worried that your local XinFin Full node might be compromised? There is a secure solution to solve that issue.
If you notice, the first 2 steps don’t require you to be online at all. If you want to absolutely make sure your transaction isn’t tampered with, you can use a computer that is offline to convert the function call into a raw transaction and use your private key to sign the transaction. You can then copy the signed transaction string and use a computer that is online to broadcast it to the network. For more details, you can check out https://medium.com/xinfin/send-offline-functionality-sof-a-secure-way-to-transact-xdc-coins-3734eaa81365
Another safe solution is to use Yodaplus multi-signature wallet or Hardware wallet such as Trezor, Decent, or Ellipall These wallets store your private key, and the code to sign the transaction is programmed into the hardware itself. They need to be connected to the internet only to publish your signed transaction.
Hopefully, this gave you a better idea of how XinFin transactions work.
For support, let’s connect via XDC Network’s Social media communities on Slack, Twitter, and Discord.