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

Add GET_BLOCK_PARTS message #146

Merged
merged 2 commits into from
Mar 12, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions src/main/java/org/semux/core/Block.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,38 @@
* Represents a block in the blockchain.
*/
public class Block {

public enum BlockPart {
HEADER(1 << 0), TRANSACTIONS(1 << 1), RECEIPTS(1 << 2), VOTES(1 << 3);

private int code;

BlockPart(int code) {
this.code = code;
}

public static int parts(BlockPart... parts) {
int result = 0;
for (BlockPart part : parts) {
result |= part.code;
}
return result;
}

public List<BlockPart> parts(int parts) {
List<BlockPart> result = new ArrayList<>();
// NOTE: values() returns an array containing all of the values of the enum type
// in the order they are declared.
for (BlockPart bp : BlockPart.values()) {
if ((parts & bp.code) != 0) {
result.add(bp);
}
}

return result;
}
}

static final Logger logger = LoggerFactory.getLogger(Block.class);

/**
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/org/semux/net/SemuxP2pHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -450,6 +450,10 @@ protected void onSync(Message msg) {
sync.onMessage(channel, msg);
break;
}
case GET_BLOCK_PARTS:
case BLOCK_PARTS:
// TODO: add handler
break;
default:
throw new UnreachableException();
}
Expand Down
10 changes: 10 additions & 0 deletions src/main/java/org/semux/net/msg/MessageCode.java
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,16 @@ public enum MessageCode {
*/
BLOCK_HEADER(0x33),

/**
* [0x34] Request parts of a block from the peer.
*/
GET_BLOCK_PARTS(0x34),

/**
* [0x35] Response containing the block parts.
*/
BLOCK_PARTS(0x35),

// =======================================
// [0x40, 0x4f] Reserved for BFT
// =======================================
Expand Down
6 changes: 6 additions & 0 deletions src/main/java/org/semux/net/msg/MessageFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@
import org.semux.crypto.Hex;
import org.semux.net.msg.consensus.BlockHeaderMessage;
import org.semux.net.msg.consensus.BlockMessage;
import org.semux.net.msg.consensus.BlockPartsMessage;
import org.semux.net.msg.consensus.GetBlockHeaderMessage;
import org.semux.net.msg.consensus.GetBlockMessage;
import org.semux.net.msg.consensus.GetBlockPartsMessage;
import org.semux.net.msg.consensus.NewHeightMessage;
import org.semux.net.msg.consensus.NewViewMessage;
import org.semux.net.msg.consensus.ProposalMessage;
Expand Down Expand Up @@ -85,6 +87,10 @@ public Message create(byte code, byte[] body) throws MessageException {
return new GetBlockHeaderMessage(body);
case BLOCK_HEADER:
return new BlockHeaderMessage(body);
case GET_BLOCK_PARTS:
return new GetBlockPartsMessage(body);
case BLOCK_PARTS:
return new BlockPartsMessage(body);

case BFT_NEW_HEIGHT:
return new NewHeightMessage(body);
Expand Down
71 changes: 71 additions & 0 deletions src/main/java/org/semux/net/msg/consensus/BlockPartsMessage.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/**
* Copyright (c) 2017-2018 The Semux Developers
*
* Distributed under the MIT software license, see the accompanying file
* LICENSE or https://opensource.org/licenses/mit-license.php
*/
package org.semux.net.msg.consensus;

import java.util.ArrayList;
import java.util.List;

import org.semux.net.msg.Message;
import org.semux.net.msg.MessageCode;
import org.semux.util.SimpleDecoder;
import org.semux.util.SimpleEncoder;

public class BlockPartsMessage extends Message {

private final long number;
private final int parts;
private final List<byte[]> data;

public BlockPartsMessage(int number, int parts, List<byte[]> data) {
super(MessageCode.BLOCK_PARTS, null);

this.number = number;
this.parts = parts;
this.data = data;

SimpleEncoder enc = new SimpleEncoder();
enc.writeLong(number);
enc.writeInt(parts);
enc.writeInt(data.size());
for (byte[] b : data) {
enc.writeBytes(b);
}
this.body = enc.toBytes();
}

public BlockPartsMessage(byte[] body) {
super(MessageCode.BLOCK_PARTS, null);

SimpleDecoder dec = new SimpleDecoder(body);
this.number = dec.readLong();
this.parts = dec.readInt();
this.data = new ArrayList<>();
int n = dec.readInt();
for (int i = 0; i < n; i++) {
data.add(dec.readBytes());
}

this.body = body;
}

public long getNumber() {
return number;
}

public int getParts() {
return parts;
}

public List<byte[]> getData() {
return data;
}

@Override
public String toString() {
return "BlockPartsMessage [number=" + number + ", parts=" + parts + "]";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/**
* Copyright (c) 2017-2018 The Semux Developers
*
* Distributed under the MIT software license, see the accompanying file
* LICENSE or https://opensource.org/licenses/mit-license.php
*/
package org.semux.net.msg.consensus;

import org.semux.net.msg.Message;
import org.semux.net.msg.MessageCode;
import org.semux.util.SimpleDecoder;
import org.semux.util.SimpleEncoder;

public class GetBlockPartsMessage extends Message {

private final long number;
private final int parts;

public GetBlockPartsMessage(long number, int parts) {
super(MessageCode.GET_BLOCK_PARTS, BlockPartsMessage.class);

this.number = number;
this.parts = parts;

SimpleEncoder enc = new SimpleEncoder();
enc.writeLong(number);
enc.writeInt(parts);
this.body = enc.toBytes();
}

public GetBlockPartsMessage(byte[] body) {
super(MessageCode.GET_BLOCK_PARTS, BlockPartsMessage.class);

SimpleDecoder dec = new SimpleDecoder(body);
this.number = dec.readLong();
this.parts = dec.readInt();

this.body = body;
}

public long getNumber() {
return number;
}

public int getParts() {
return parts;
}

@Override
public String toString() {
return "GetBlockPartsMessage [number=" + number + ", parts = " + parts + "]";
}
}