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

Submit transaction to all miners #46

Open
Popeyef5 opened this issue Jun 16, 2020 · 3 comments
Open

Submit transaction to all miners #46

Popeyef5 opened this issue Jun 16, 2020 · 3 comments

Comments

@Popeyef5
Copy link

Shouldn't all miners get notified when a transaction is submitted? If not, it's as if "my miners" validate "my transactions" and so do everyone else's. Instead of having a separation between dealers and miners where the latter compete to validate the transaction of the former. Just wanted to know if this was intentional

@satwikkansal
Copy link
Owner

A real-world network typically will use some form of gossip protocol, where every node is connected to few other nodes and it relays the information to those, and so on, eventually syncing the entire network. This is neither simple to implement or demonstrate in a single machine set up, hence I left it out.

@satwikkansal
Copy link
Owner

But yeah, just in case if someone is interested, they can add a for loop in the mine method to relay the transaction to other nodes, and handle the logic to abandon mining the relayed block when some other node figures out the nonce first and announces it to the network.

@panchiwalashivani
Copy link

We need to clear those out and put them inside actual blocks. So, we have to create a minePendingTransactions() method. This method will not only mine a new block with all the pending transactions, it will also send a mining reward to the miner.

minePendingTransactions(miningRewardAddress) {
// Create new block with all pending transactions and mine it..
let block = new Block(Date.now(), this.pendingTransactions);
block.mineBlock(this.difficulty);

// Add the newly mined block to the chain
this.chain.push(block);

// Reset the pending transactions and send the mining reward
this.pendingTransactions = [
	new Transaction(null, miningRewardAddress, this.miningReward)
];

}

If you start mining, you can pass along your wallet address to this method. Once you successfully mined a block, the system will create a new transaction to give you your mining reward (in this case 100 coins).

One thing to note is that in this implementation we take all the pending transactions and add them to a block. In reality however that won’t work because the size of a block is limited. In Bitcoin’s case there is a block size limit of 2mb. If there are more transactions that can fit in a block, the miner gets to choose which transaction he includes and which he doesn’t (usually the ones with the highest fee wins).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants