Post
Topic
Board Altcoin Discussion
Re: Nxt source code
by
Come-from-Beyond
on 30/11/2013, 07:41:59 UTC
The part that seems to need most work at the moment is the networking and the loading of the block chain, which is still not stable, isn't it? It is also probably not Nxt specific, so disclosing it is not likely to help someone clone Nxt. I could try to help debugging it.

Networking is being fixed now, it will be published later.


Loading the blockchain


Part of main() method:

Code:
if (bootstrappingState >= 0) {

peer = Peer.getAnyConnectedPeer();
if (peer != null) {

setBootstrappingState(0);

ByteBuffer outputBuffer = Peer.getOutputBuffer(0, Peer.PACKET_TYPE_CUMULATIVE_DIFFICULTY_REQUEST);

peer.send(outputBuffer);

}

}


Part of Peer class:

Code:
case PACKET_TYPE_CUMULATIVE_DIFFICULTY_REQUEST:
{

byte[] cumulativeDifficulty = Block.getLastBlock().cumulativeDifficulty.toByteArray();

ByteBuffer outputBuffer = getOutputBuffer(2 + cumulativeDifficulty.length, PACKET_TYPE_CUMULATIVE_DIFFICULTY_RESPONSE);

outputBuffer.putShort((short)cumulativeDifficulty.length);
outputBuffer.put(cumulativeDifficulty);

send(outputBuffer);

}
break;

case PACKET_TYPE_CUMULATIVE_DIFFICULTY_RESPONSE:
{

if (bootstrappingState == 0) {

int cumulativeDifficultyLength = inputBuffer.getShort();
byte[] cumulativeDifficulty = new byte[cumulativeDifficultyLength];
inputBuffer.get(cumulativeDifficulty);

betterCumulativeDifficulty = new BigInteger(cumulativeDifficulty);
synchronized (blocks) {

if (betterCumulativeDifficulty.compareTo(Block.getLastBlock().cumulativeDifficulty) > 0) {

setBootstrappingState(-1);
bootstrappingPeer = this;

ByteBuffer outputBuffer = Peer.getOutputBuffer(0, PACKET_TYPE_MILESTONE_BLOCKS_REQUEST);

send(outputBuffer);

} else {

setBootstrappingState(1);

}

}

}

}
break;

case PACKET_TYPE_MILESTONE_BLOCKS_REQUEST:
{

try {

LinkedList milestoneBlocks = new LinkedList<>();

Block block = Block.getLastBlock();
int jumpLength = block.height / 4093 + 1;
while (block.height > 0) {

milestoneBlocks.add(block.getId());
for (int i = 0; i < jumpLength && block.height > 0; i++) {

block = blocks.get(block.previousBlock);

}

}

ByteBuffer outputBuffer = Peer.getOutputBuffer(2 + milestoneBlocks.size() * 8, PACKET_TYPE_MILESTONE_BLOCKS_RESPONSE);

outputBuffer.putShort((short)milestoneBlocks.size());
for (int i = 0; i < milestoneBlocks.size(); i++) {

outputBuffer.putLong(milestoneBlocks.get(i));

}

send(outputBuffer);

} catch (Exception e) {

log("12: " + e.toString());

}

}
break;

case PACKET_TYPE_MILESTONE_BLOCKS_RESPONSE:
{

if (bootstrappingState < 0) {

Block commonBlock = blocks.get(genesisBlock);

int numberOfBlocks = inputBuffer.getShort();
while (numberOfBlocks-- > 0) {

long blockId = inputBuffer.getLong();
Block block = blocks.get(blockId);
if (block != null) {

commonBlock = block;

break;

}

}

try {

ByteBuffer outputBuffer = Peer.getOutputBuffer(8, PACKET_TYPE_NEXT_BLOCK_IDS_REQUEST);

lastCommonBlock = commonBlock.getId();
outputBuffer.putLong(lastCommonBlock);

send(outputBuffer);

} catch (Exception e) {

log("13: " + e.toString());

}

}

}
break;

case PACKET_TYPE_NEXT_BLOCK_IDS_REQUEST:
{

try {

LinkedList nextBlocks = new LinkedList<>();

Block block = blocks.get(inputBuffer.getLong());

while (block != null && nextBlocks.size() < 4093) {

block = blocks.get(block.nextBlock);
if (block != null) {

nextBlocks.add(block.getId());

}

}

ByteBuffer outputBuffer = Peer.getOutputBuffer(2 + nextBlocks.size() * 8, PACKET_TYPE_NEXT_BLOCK_IDS_RESPONSE);

outputBuffer.putShort((short)nextBlocks.size());
for (int i = 0; i < nextBlocks.size(); i++) {

outputBuffer.putLong(nextBlocks.get(i));

}

send(outputBuffer);

} catch (Exception e) {

log("14: " + e.toString());

}

}
break;

case PACKET_TYPE_NEXT_BLOCK_IDS_RESPONSE:
{

if (bootstrappingState < 0) {

int numberOfBlocks = inputBuffer.getShort();
if (numberOfBlocks == 0) {

setBootstrappingState(1);

break;

}

long blockId;
while (numberOfBlocks-- > 0) {

blockId = inputBuffer.getLong();
if (blocks.get(blockId) == null) {

break;

}

lastCommonBlock = blockId;

}

if (numberOfBlocks < 0) {

ByteBuffer outputBuffer = Peer.getOutputBuffer(8, PACKET_TYPE_NEXT_BLOCK_IDS_REQUEST);

outputBuffer.putLong(lastCommonBlock);

send(outputBuffer);

} else {

if (Block.getLastBlock().height - blocks.get(lastCommonBlock).height > 720) {

setBootstrappingState(1);

} else {

while (lastBlock != lastCommonBlock && Block.popLastBlock()) { }

if (lastBlock == lastCommonBlock) {

ByteBuffer outputBuffer = Peer.getOutputBuffer(8, PACKET_TYPE_NEXT_BLOCKS_REQUEST);

outputBuffer.putLong(lastBlock);

send(outputBuffer);

} else {

setBootstrappingState(1);

}

}

}

}

}
break;

case PACKET_TYPE_NEXT_BLOCKS_REQUEST:
{

LinkedList nextBlocks = new LinkedList<>();
int totalLength = 0;

Block block = blocks.get(inputBuffer.getLong());

while (block != null) {

block = blocks.get(block.nextBlock);
if (block != null) {

int length = 224 + block.payloadLength;
if (totalLength + length > 32768 - (8 + 2)) {

break;

}

nextBlocks.add(block);
totalLength += length;

}

}

ByteBuffer outputBuffer = Peer.getOutputBuffer(2 + totalLength, PACKET_TYPE_NEXT_BLOCKS_RESPONSE);

outputBuffer.putShort((short)nextBlocks.size());
for (int i = 0; i < nextBlocks.size(); i++) {

block = nextBlocks.get(i);
outputBuffer.put(block.getBytes());
for (int j = 0; j < block.numberOfTransactions; j++) {

outputBuffer.put(transactions.get(block.transactions[j]).getBytes());

}

}

send(outputBuffer);

}
break;

case PACKET_TYPE_NEXT_BLOCKS_RESPONSE:
{

if (bootstrappingState < 0) {

int numberOfBlocks = inputBuffer.getShort();
if (numberOfBlocks == 0) {

setBootstrappingState(1);

break;

}

while (numberOfBlocks-- > 0) {

if (!Block.pushBlock(inputBuffer)) {

break;

}

}

ByteBuffer outputBuffer = Peer.getOutputBuffer(8, PACKET_TYPE_NEXT_BLOCKS_REQUEST);

outputBuffer.putLong(lastBlock);

send(outputBuffer);

}

}
break;