Move message command validation to abstract Message class

This commit is contained in:
nicksavers 2014-10-02 00:20:48 +02:00
parent fc8feb491c
commit c167f1947e
10 changed files with 23 additions and 39 deletions

View File

@ -30,9 +30,7 @@ public class BlockHashesMessage extends Message {
private void parse() { private void parse() {
RLPList paramsList = (RLPList) RLP.decode2(encoded).get(0); RLPList paramsList = (RLPList) RLP.decode2(encoded).get(0);
validateMessage(paramsList, BLOCK_HASHES);
if ((((RLPItem)paramsList.get(0)).getRLPData()[0] & 0xFF) != BLOCK_HASHES.asByte())
throw new RuntimeException("Not a BlockHashesMessage command");
hashes = new ArrayList<>(); hashes = new ArrayList<>();
for (int i = 1; i < paramsList.size(); ++i) { for (int i = 1; i < paramsList.size(); ++i) {

View File

@ -7,7 +7,6 @@ import static org.ethereum.net.message.Command.BLOCKS;
import org.ethereum.core.Block; import org.ethereum.core.Block;
import org.ethereum.util.RLP; import org.ethereum.util.RLP;
import org.ethereum.util.RLPItem;
import org.ethereum.util.RLPList; import org.ethereum.util.RLPList;
/** /**
@ -24,11 +23,8 @@ public class BlocksMessage extends Message {
} }
private void parse() { private void parse() {
RLPList paramsList = (RLPList) RLP.decode2(encoded).get(0); RLPList paramsList = (RLPList) RLP.decode2(encoded).get(0);
validateMessage(paramsList, BLOCKS);
if ((((RLPItem)paramsList.get(0)).getRLPData()[0] & 0xFF) != BLOCKS.asByte())
throw new RuntimeException("Not a BlocksMessage command");
blocks = new ArrayList<>(); blocks = new ArrayList<>();
for (int i = 1; i < paramsList.size(); ++i) { for (int i = 1; i < paramsList.size(); ++i) {

View File

@ -26,11 +26,8 @@ public class DisconnectMessage extends Message {
} }
private void parse() { private void parse() {
RLPList paramsList = (RLPList) RLP.decode2(encoded).get(0); RLPList paramsList = (RLPList) RLP.decode2(encoded).get(0);
validateMessage(paramsList, DISCONNECT);
if ((((RLPItem)paramsList.get(0)).getRLPData()[0] & 0xFF) != DISCONNECT.asByte())
throw new RuntimeException("Not a DisconnectMessage command");
byte[] reasonBytes = ((RLPItem) paramsList.get(1)).getRLPData(); byte[] reasonBytes = ((RLPItem) paramsList.get(1)).getRLPData();
if (reasonBytes == null) if (reasonBytes == null)

View File

@ -40,9 +40,7 @@ public class GetBlockHashesMessage extends Message {
private void parse() { private void parse() {
RLPList paramsList = (RLPList) RLP.decode2(encoded).get(0); RLPList paramsList = (RLPList) RLP.decode2(encoded).get(0);
validateMessage(paramsList, GET_BLOCK_HASHES);
if ( (((RLPItem)paramsList.get(0)).getRLPData()[0] & 0xFF) != GET_BLOCK_HASHES.asByte())
throw new RuntimeException("Not a GetBlockHashesMessage command");
this.hash = ((RLPItem) paramsList.get(1)).getRLPData(); this.hash = ((RLPItem) paramsList.get(1)).getRLPData();
byte[] maxBlocksBytes = ((RLPItem) paramsList.get(2)).getRLPData(); byte[] maxBlocksBytes = ((RLPItem) paramsList.get(2)).getRLPData();

View File

@ -30,9 +30,7 @@ public class GetBlocksMessage extends Message {
private void parse() { private void parse() {
RLPList paramsList = (RLPList) RLP.decode2(encoded).get(0); RLPList paramsList = (RLPList) RLP.decode2(encoded).get(0);
validateMessage(paramsList, GET_BLOCKS);
if ((((RLPItem)paramsList.get(0)).getRLPData()[0] & 0xFF) != GET_BLOCKS.asByte())
throw new RuntimeException("Not a GetBlocksMessage command");
blockHashes = new ArrayList<>(); blockHashes = new ArrayList<>();
for (int i = 1; i < paramsList.size() - 1; ++i) { for (int i = 1; i < paramsList.size() - 1; ++i) {

View File

@ -50,14 +50,11 @@ public class HelloMessage extends Message {
} }
private void parse() { private void parse() {
RLPList paramsList = (RLPList) RLP.decode2(encoded).get(0); RLPList paramsList = (RLPList) RLP.decode2(encoded).get(0);
// TODO: find out if it can be 0x00. Do we need to check for this?
// TODO: find out if it can be 00. Do we need to check for this?
// The message does not distinguish between 0 and null, // The message does not distinguish between 0 and null,
// so we check command code for null. // so we check command code for null.
if (((RLPItem)paramsList.get(0)).getRLPData() != null) validateMessage(paramsList, HELLO);
throw new Error("HelloMessage: parsing for mal data");
byte[] p2pVersionBytes = ((RLPItem) paramsList.get(1)).getRLPData(); byte[] p2pVersionBytes = ((RLPItem) paramsList.get(1)).getRLPData();
this.p2pVersion = p2pVersionBytes != null ? p2pVersionBytes[0] : 0; this.p2pVersion = p2pVersionBytes != null ? p2pVersionBytes[0] : 0;

View File

@ -1,5 +1,8 @@
package org.ethereum.net.message; 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 * Abstract message class for all messages on the Ethereum network
* *
@ -18,6 +21,15 @@ public abstract class Message {
parsed = false; 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(); public abstract Command getCommand();
/** /**

View File

@ -36,11 +36,8 @@ public class PeersMessage extends Message {
} }
private void parse() { private void parse() {
RLPList paramsList = (RLPList) RLP.decode2(encoded).get(0); RLPList paramsList = (RLPList) RLP.decode2(encoded).get(0);
validateMessage(paramsList, PEERS);
if ((((RLPItem)paramsList.get(0)).getRLPData()[0] & 0xFF) != PEERS.asByte())
throw new RuntimeException("Not a PeersMessage command");
peers = new LinkedHashSet<>(); peers = new LinkedHashSet<>();
for (int i = 1; i < paramsList.size(); ++i) { for (int i = 1; i < paramsList.size(); ++i) {

View File

@ -41,14 +41,8 @@ public class StatusMessage extends Message {
} }
private void parse() { private void parse() {
RLPList paramsList = (RLPList) RLP.decode2(encoded).get(0); RLPList paramsList = (RLPList) RLP.decode2(encoded).get(0);
validateMessage(paramsList, STATUS);
/* 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");
this.protocolVersion = ((RLPItem) paramsList.get(1)).getRLPData()[0]; this.protocolVersion = ((RLPItem) paramsList.get(1)).getRLPData()[0];
byte[] networkIdBytes = ((RLPItem) paramsList.get(2)).getRLPData(); byte[] networkIdBytes = ((RLPItem) paramsList.get(2)).getRLPData();

View File

@ -4,7 +4,6 @@ import static org.ethereum.net.message.Command.TRANSACTIONS;
import org.ethereum.core.Transaction; import org.ethereum.core.Transaction;
import org.ethereum.util.RLP; import org.ethereum.util.RLP;
import org.ethereum.util.RLPItem;
import org.ethereum.util.RLPList; import org.ethereum.util.RLPList;
import java.util.ArrayList; import java.util.ArrayList;
@ -32,9 +31,7 @@ public class TransactionsMessage extends Message {
private void parse() { private void parse() {
RLPList paramsList = (RLPList) RLP.decode2(encoded).get(0); RLPList paramsList = (RLPList) RLP.decode2(encoded).get(0);
validateMessage(paramsList, TRANSACTIONS);
if ((((RLPItem)paramsList.get(0)).getRLPData()[0] & 0xFF) != TRANSACTIONS.asByte())
throw new RuntimeException("Not a TransactionsMessage command");
transactions = new HashSet<>(); transactions = new HashSet<>();
for (int i = 1; i < paramsList.size(); ++i) { for (int i = 1; i < paramsList.size(); ++i) {