Search code examples
transactionsblockchaincryptocurrency

Where does the first request for transaction go in a blockchain?


My question is simple. If a user on a blockchain wants to make a transaction request, he/she will send a request to a particular URL. If this URL is dynamic then how is a particular miner chosen to validate this request. Moreover, if it is a static URL, won't it just dissolve the whole point of decentralizing the network? Because if you can forge that one particular node (which accepts the request for the transaction and forwards it to all the miners)

I know that won't this also arise the question in the latter scenario that the same constraint goes there, as to how will the miners be informed from the static URL. Well I feel if the address is known, the miners will just keep sending requests for checking if a transaction request has been made on that URL

Any help is appreciated

I am fairly new to blockchain but very eagerly interested to understand this technology.
Sorry if my ideology is that of a rookie here.

EDIT: I do understand that there are wallet services for the same but I still raise the same question - "if it is a static URL, won't it just dissolve the whole point of decentralizing the network?"


Solution

  • You need to have to endpoints. /transaction/broadcast and /transaction.

    we are going to send transaction data to /transaction/broadcast on a random node in our network. first thing that will happen at this end point, we will create a new transaction from the data that sent in. So you should already have a method to create in your blockchain.

    after a new transaction is created, we have to broadcast that transaction to every node inside the network. this broadcast will be received at /transaction endpoint on all other networks.

    Every node in our blockchain has an array of other node's urls. So broadcasting means sending the new transactions to all of other nodes in the array. a small code snipped will be like this.

    const requestPromises = [];//sending transaction to each node returns a promise. so we store each promise inside the array.
        blockchain.networkNodes.forEach(networkNodeUrl => {
            const requestOptions = {
                uri: networkNodeUrl + '/transaction',
                method: 'POST',
                body: newTransaction,
                json: true
            };
    
            requestPromises.push(rp(requestOptions)); //I used request-promise library
        });
    

    Once all of those promises are resolved that means our transaction is created successfully.

    Promise.all(requestPromises)
        .then(data => {
            res.json({ note: 'Transaction created and broadcast successfully.' });
        });