Move message command validation to abstract Message class
This commit is contained in:
parent
fc8feb491c
commit
c167f1947e
|
@ -30,9 +30,7 @@ public class BlockHashesMessage extends Message {
|
|||
|
||||
private void parse() {
|
||||
RLPList paramsList = (RLPList) RLP.decode2(encoded).get(0);
|
||||
|
||||
if ((((RLPItem)paramsList.get(0)).getRLPData()[0] & 0xFF) != BLOCK_HASHES.asByte())
|
||||
throw new RuntimeException("Not a BlockHashesMessage command");
|
||||
validateMessage(paramsList, BLOCK_HASHES);
|
||||
|
||||
hashes = new ArrayList<>();
|
||||
for (int i = 1; i < paramsList.size(); ++i) {
|
||||
|
|
|
@ -7,7 +7,6 @@ import static org.ethereum.net.message.Command.BLOCKS;
|
|||
|
||||
import org.ethereum.core.Block;
|
||||
import org.ethereum.util.RLP;
|
||||
import org.ethereum.util.RLPItem;
|
||||
import org.ethereum.util.RLPList;
|
||||
|
||||
/**
|
||||
|
@ -24,11 +23,8 @@ public class BlocksMessage extends Message {
|
|||
}
|
||||
|
||||
private void parse() {
|
||||
|
||||
RLPList paramsList = (RLPList) RLP.decode2(encoded).get(0);
|
||||
|
||||
if ((((RLPItem)paramsList.get(0)).getRLPData()[0] & 0xFF) != BLOCKS.asByte())
|
||||
throw new RuntimeException("Not a BlocksMessage command");
|
||||
validateMessage(paramsList, BLOCKS);
|
||||
|
||||
blocks = new ArrayList<>();
|
||||
for (int i = 1; i < paramsList.size(); ++i) {
|
||||
|
|
|
@ -26,11 +26,8 @@ public class DisconnectMessage extends Message {
|
|||
}
|
||||
|
||||
private void parse() {
|
||||
|
||||
RLPList paramsList = (RLPList) RLP.decode2(encoded).get(0);
|
||||
|
||||
if ((((RLPItem)paramsList.get(0)).getRLPData()[0] & 0xFF) != DISCONNECT.asByte())
|
||||
throw new RuntimeException("Not a DisconnectMessage command");
|
||||
validateMessage(paramsList, DISCONNECT);
|
||||
|
||||
byte[] reasonBytes = ((RLPItem) paramsList.get(1)).getRLPData();
|
||||
if (reasonBytes == null)
|
||||
|
|
|
@ -40,9 +40,7 @@ public class GetBlockHashesMessage extends Message {
|
|||
|
||||
private void parse() {
|
||||
RLPList paramsList = (RLPList) RLP.decode2(encoded).get(0);
|
||||
|
||||
if ( (((RLPItem)paramsList.get(0)).getRLPData()[0] & 0xFF) != GET_BLOCK_HASHES.asByte())
|
||||
throw new RuntimeException("Not a GetBlockHashesMessage command");
|
||||
validateMessage(paramsList, GET_BLOCK_HASHES);
|
||||
|
||||
this.hash = ((RLPItem) paramsList.get(1)).getRLPData();
|
||||
byte[] maxBlocksBytes = ((RLPItem) paramsList.get(2)).getRLPData();
|
||||
|
|
|
@ -30,9 +30,7 @@ public class GetBlocksMessage extends Message {
|
|||
|
||||
private void parse() {
|
||||
RLPList paramsList = (RLPList) RLP.decode2(encoded).get(0);
|
||||
|
||||
if ((((RLPItem)paramsList.get(0)).getRLPData()[0] & 0xFF) != GET_BLOCKS.asByte())
|
||||
throw new RuntimeException("Not a GetBlocksMessage command");
|
||||
validateMessage(paramsList, GET_BLOCKS);
|
||||
|
||||
blockHashes = new ArrayList<>();
|
||||
for (int i = 1; i < paramsList.size() - 1; ++i) {
|
||||
|
|
|
@ -50,15 +50,12 @@ public class HelloMessage extends Message {
|
|||
}
|
||||
|
||||
private void parse() {
|
||||
|
||||
RLPList paramsList = (RLPList) RLP.decode2(encoded).get(0);
|
||||
|
||||
// TODO: find out if it can be 00. Do we need to check for this?
|
||||
// TODO: find out if it can be 0x00. Do we need to check for this?
|
||||
// The message does not distinguish between 0 and null,
|
||||
// so we check command code for null.
|
||||
if (((RLPItem)paramsList.get(0)).getRLPData() != null)
|
||||
throw new Error("HelloMessage: parsing for mal data");
|
||||
|
||||
validateMessage(paramsList, HELLO);
|
||||
|
||||
byte[] p2pVersionBytes = ((RLPItem) paramsList.get(1)).getRLPData();
|
||||
this.p2pVersion = p2pVersionBytes != null ? p2pVersionBytes[0] : 0;
|
||||
|
||||
|
|
|
@ -1,5 +1,8 @@
|
|||
package org.ethereum.net.message;
|
||||
|
||||
import org.ethereum.util.RLPItem;
|
||||
import org.ethereum.util.RLPList;
|
||||
|
||||
/**
|
||||
* Abstract message class for all messages on the Ethereum network
|
||||
*
|
||||
|
@ -18,6 +21,15 @@ public abstract class Message {
|
|||
parsed = false;
|
||||
}
|
||||
|
||||
protected void validateMessage(RLPList data, Command expectedCode) {
|
||||
RLPItem item = (RLPItem) data.get(0);
|
||||
if (item.getRLPData() == null && expectedCode == Command.HELLO)
|
||||
return;
|
||||
if ((item.getRLPData()[0] & 0xFF) == expectedCode.asByte())
|
||||
return;
|
||||
throw new RuntimeException("Expected " + expectedCode);
|
||||
}
|
||||
|
||||
public abstract Command getCommand();
|
||||
|
||||
/**
|
||||
|
|
|
@ -36,11 +36,8 @@ public class PeersMessage extends Message {
|
|||
}
|
||||
|
||||
private void parse() {
|
||||
|
||||
RLPList paramsList = (RLPList) RLP.decode2(encoded).get(0);
|
||||
|
||||
if ((((RLPItem)paramsList.get(0)).getRLPData()[0] & 0xFF) != PEERS.asByte())
|
||||
throw new RuntimeException("Not a PeersMessage command");
|
||||
validateMessage(paramsList, PEERS);
|
||||
|
||||
peers = new LinkedHashSet<>();
|
||||
for (int i = 1; i < paramsList.size(); ++i) {
|
||||
|
|
|
@ -41,14 +41,8 @@ public class StatusMessage extends Message {
|
|||
}
|
||||
|
||||
private void parse() {
|
||||
|
||||
RLPList paramsList = (RLPList) RLP.decode2(encoded).get(0);
|
||||
|
||||
/* the message does not distinguish between the 0 and null
|
||||
* so check command code for null */
|
||||
// TODO: find out if it can be 00
|
||||
if ((((RLPItem)paramsList.get(0)).getRLPData()[0] & 0xFF) != STATUS.asByte())
|
||||
throw new RuntimeException("Not a StatusMessage command");
|
||||
validateMessage(paramsList, STATUS);
|
||||
|
||||
this.protocolVersion = ((RLPItem) paramsList.get(1)).getRLPData()[0];
|
||||
byte[] networkIdBytes = ((RLPItem) paramsList.get(2)).getRLPData();
|
||||
|
|
|
@ -4,7 +4,6 @@ import static org.ethereum.net.message.Command.TRANSACTIONS;
|
|||
|
||||
import org.ethereum.core.Transaction;
|
||||
import org.ethereum.util.RLP;
|
||||
import org.ethereum.util.RLPItem;
|
||||
import org.ethereum.util.RLPList;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
@ -32,9 +31,7 @@ public class TransactionsMessage extends Message {
|
|||
|
||||
private void parse() {
|
||||
RLPList paramsList = (RLPList) RLP.decode2(encoded).get(0);
|
||||
|
||||
if ((((RLPItem)paramsList.get(0)).getRLPData()[0] & 0xFF) != TRANSACTIONS.asByte())
|
||||
throw new RuntimeException("Not a TransactionsMessage command");
|
||||
validateMessage(paramsList, TRANSACTIONS);
|
||||
|
||||
transactions = new HashSet<>();
|
||||
for (int i = 1; i < paramsList.size(); ++i) {
|
||||
|
|
Loading…
Reference in New Issue