+ 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 // CREATE NEW CONTRACT ADDRESS AND ADD TX VALUE
if(isContractCreation) { if(isContractCreation) {
if (isValueTx) // adding to balance also creates the account trackTx.addBalance(receiverAddress, BigInteger.ZERO); // also creates account
trackTx.addBalance(receiverAddress, new BigInteger(1, tx.getValue()));
else
trackTx.createAccount(receiverAddress);
if(stateLogger.isDebugEnabled()) if(stateLogger.isDebugEnabled())
stateLogger.debug("new contract created address={}", stateLogger.debug("new contract created address={}",

View File

@ -85,6 +85,10 @@ public class Miner {
while(ByteUtil.increment(testNonce) && !stop) { while(ByteUtil.increment(testNonce) && !stop) {
if (testNonce[31] == 0 && testNonce[30] == 0){
System.out.println("mining: " + new BigInteger(1, testNonce));
}
if (testNonce[31] == 0) if (testNonce[31] == 0)
sleep(); sleep();
concat = Arrays.concatenate(hash, testNonce); 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){ public void addNewBlockHash(byte[] hash){
blockHashQueue.addFirst(hash); blockHashQueue.addFirst(hash);
} }
@ -158,6 +165,7 @@ public class BlockQueue {
* @return A list of hashes for which blocks need to be retrieved. * @return A list of hashes for which blocks need to be retrieved.
*/ */
public List<byte[]> getHashes() { public List<byte[]> getHashes() {
List<byte[]> hashes = new ArrayList<>(); List<byte[]> hashes = new ArrayList<>();
while (!blockHashQueue.isEmpty() && hashes.size() < CONFIG.maxBlocksAsk()) { while (!blockHashQueue.isEmpty() && hashes.size() < CONFIG.maxBlocksAsk()) {
hashes.add(blockHashQueue.removeLast()); hashes.add(blockHashQueue.removeLast());
@ -205,6 +213,10 @@ public class BlockQueue {
return blockReceivedQueue.size(); return blockReceivedQueue.size();
} }
public boolean isHashesEmpty(){
return blockHashQueue.size() == 0;
}
public void clear(){ public void clear(){
this.blockHashQueue.clear(); this.blockHashQueue.clear();
this.blockReceivedQueue.clear(); this.blockReceivedQueue.clear();

View File

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

View File

@ -61,10 +61,10 @@ public class Channel {
ethHandler.sendTransaction(tx); ethHandler.sendTransaction(tx);
} }
public void sendBlock(Block block){ public void sendNewBlock(Block block){
// 1. check by best block send or not to send // 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 // 1. Go over all channels and send the block
for (Channel channel : channels){
channel.sendNewBlock(block);
}
} }
public void addChannel(Channel channel){ public void addChannel(Channel channel){