+ Fix Receive less blocks than asked, small fix for balance on contract creation

This commit is contained in:
romanman 2014-12-01 22:01:57 +01:00
parent 1f22e1e9f9
commit 2c9161dead
6 changed files with 66 additions and 33 deletions

View File

@ -516,10 +516,7 @@ public class BlockchainImpl implements Blockchain {
// CREATE NEW CONTRACT ADDRESS AND ADD TX VALUE
if(isContractCreation) {
if (isValueTx) // adding to balance also creates the account
trackTx.addBalance(receiverAddress, new BigInteger(1, tx.getValue()));
else
trackTx.createAccount(receiverAddress);
trackTx.addBalance(receiverAddress, BigInteger.ZERO); // also creates account
if(stateLogger.isDebugEnabled())
stateLogger.debug("new contract created address={}",

View File

@ -85,6 +85,10 @@ public class Miner {
while(ByteUtil.increment(testNonce) && !stop) {
if (testNonce[31] == 0 && testNonce[30] == 0){
System.out.println("mining: " + new BigInteger(1, testNonce));
}
if (testNonce[31] == 0)
sleep();
concat = Arrays.concatenate(hash, testNonce);

View File

@ -148,6 +148,13 @@ public class BlockQueue {
}
}
public void returnHashes(List<byte[]> hashes){
ListIterator iterator = hashes.listIterator(hashes.size());
while(iterator.hasPrevious())
blockHashQueue.addLast((byte[])iterator.previous());
}
public void addNewBlockHash(byte[] hash){
blockHashQueue.addFirst(hash);
}
@ -158,6 +165,7 @@ public class BlockQueue {
* @return A list of hashes for which blocks need to be retrieved.
*/
public List<byte[]> getHashes() {
List<byte[]> hashes = new ArrayList<>();
while (!blockHashQueue.isEmpty() && hashes.size() < CONFIG.maxBlocksAsk()) {
hashes.add(blockHashQueue.removeLast());
@ -205,6 +213,10 @@ public class BlockQueue {
return blockReceivedQueue.size();
}
public boolean isHashesEmpty(){
return blockHashQueue.size() == 0;
}
public void clear(){
this.blockHashQueue.clear();
this.blockReceivedQueue.clear();

View File

@ -67,6 +67,7 @@ public class EthHandler extends SimpleChannelInboundHandler<EthMessage> {
@Autowired
private WorldManager worldManager;
private List<byte[]> sentHashes;
public EthHandler(){
this.peerDiscoveryMode = false;
@ -232,7 +233,7 @@ public class EthHandler extends SimpleChannelInboundHandler<EthMessage> {
// or peer doesn't have the best hash anymore
if (receivedHashes.isEmpty()
|| !this.peerId.equals(hashRetrievalLock)) {
startGetBlockTimer(); // start getting blocks from hash queue
sendGetBlocks(); // start getting blocks from hash queue
return;
}
@ -247,7 +248,7 @@ public class EthHandler extends SimpleChannelInboundHandler<EthMessage> {
logger.trace("Catch up with the hashes until: {[]}", foundHash);
// if known hash is found, ignore the rest
startGetBlockTimer(); // start getting blocks from hash queue
sendGetBlocks(); // start getting blocks from hash queue
return;
}
}
@ -260,18 +261,26 @@ public class EthHandler extends SimpleChannelInboundHandler<EthMessage> {
List<Block> blockList = blocksMessage.getBlocks();
if (blockList.isEmpty()) return;
blockchain.getQueue().addBlocks(blockList);
blockchain.getQueue().logHashQueueSize();
// check if you got less blocks than you asked
if (blockList.size() < sentHashes.size()){
for (int i = 0; i < blockList.size(); ++i)
sentHashes.remove(0);
// If we got less blocks then we could get,
// it the correct indication that we are in sync we
// the chain from here there will be NEW_BLOCK only
// message expectation
if (blockList.size() < CONFIG.maxBlocksAsk()) {
logger.info("Got less blocks: [{}], return [{}] hashes to the queue",
blockList.size(), sentHashes.size());
blockchain.getQueue().returnHashes(sentHashes);
}
if (blockchain.getQueue().isHashesEmpty()) {
logger.info(" The peer sync process fully complete");
syncStatus = SyncSatus.SYNC_DONE;
stopGetBlocksTimer();
blockchain.getQueue().addBlocks(blockList);
blockchain.getQueue().logHashQueueSize();
} else{
if (blockList.isEmpty()) return;
blockchain.getQueue().addBlocks(blockList);
blockchain.getQueue().logHashQueueSize();
sendGetBlocks();
}
}
@ -350,6 +359,11 @@ public class EthHandler extends SimpleChannelInboundHandler<EthMessage> {
msgQueue.sendMessage(msg);
}
public void sendNewBlock(Block block){
NewBlockMessage msg = new NewBlockMessage(block, block.getDifficulty());
msgQueue.sendMessage(msg);
}
private void sendGetTransactions() {
msgQueue.sendMessage(GET_TRANSACTIONS_MESSAGE);
}
@ -366,9 +380,12 @@ public class EthHandler extends SimpleChannelInboundHandler<EthMessage> {
if (queue.size() > CONFIG.maxBlocksQueued()) return;
// retrieve list of block hashes from queue
// save them locally in case the remote peer
// will return less blocks than requested.
List<byte[]> hashes = queue.getHashes();
this.sentHashes = hashes;
if (hashes.isEmpty()) {
stopGetBlocksTimer();
return;
}
@ -413,20 +430,20 @@ public class EthHandler extends SimpleChannelInboundHandler<EthMessage> {
}, 2000, 10000);
}
public void startGetBlockTimer() {
syncStatus = SyncSatus.BLOCK_RETRIEVING;
getBlocksTimer = new Timer("GetBlocksTimer");
getBlocksTimer.scheduleAtFixedRate(new TimerTask() {
public void run() {
BlockQueue blockQueue = blockchain.getQueue();
if (blockQueue.size() > CONFIG.maxBlocksQueued()) {
logger.trace("Blocks queue too big temporary postpone blocks request");
return;
}
sendGetBlocks();
}
}, 300, 10);
}
// public void startGetBlockTimer() {
// syncStatus = SyncSatus.BLOCK_RETRIEVING;
// getBlocksTimer = new Timer("GetBlocksTimer");
// getBlocksTimer.scheduleAtFixedRate(new TimerTask() {
// public void run() {
// BlockQueue blockQueue = blockchain.getQueue();
// if (blockQueue.size() > CONFIG.maxBlocksQueued()) {
// logger.trace("Blocks queue too big temporary postpone blocks request");
// return;
// }
// sendGetBlocks();
// }
// }, 300, 10);
// }
private void stopGetBlocksTimer() {
getBlocksTimer.cancel();

View File

@ -61,10 +61,10 @@ public class Channel {
ethHandler.sendTransaction(tx);
}
public void sendBlock(Block block){
public void sendNewBlock(Block block){
// 1. check by best block send or not to send
// ethHandler.sendBlock(block);
ethHandler.sendNewBlock(block);
}

View File

@ -45,8 +45,11 @@ public class ChannelManager {
}
public void sendBlock(){
public void sendNewBlock(Block block){
// 1. Go over all channels and send the block
for (Channel channel : channels){
channel.sendNewBlock(block);
}
}
public void addChannel(Channel channel){