Migrated new lite node message types to new format.

This commit is contained in:
CalDescent 2022-04-30 15:22:50 +01:00
parent 5e0bde226a
commit 52904db413
12 changed files with 232 additions and 250 deletions

View File

@ -1666,7 +1666,9 @@ public class Controller extends Thread {
}
} catch (DataException e) {
LOGGER.error(String.format("Repository issue while send transactions for account %s %d to peer %s", address, peer), e);
LOGGER.error(String.format("Repository issue while sending transactions for account %s %d to peer %s", address, peer), e);
} catch (MessageException e) {
LOGGER.error(String.format("Message serialization issue while sending transactions for account %s %d to peer %s", address, peer), e);
}
}

View File

@ -13,8 +13,7 @@ import org.qortal.network.message.*;
import java.security.SecureRandom;
import java.util.*;
import static org.qortal.network.message.Message.MessageType;
import static org.qortal.network.message.Message.MessageType.*;
import static org.qortal.network.message.MessageType.*;
public class LiteNode {

View File

@ -7,19 +7,34 @@ import org.qortal.utils.Base58;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.nio.ByteBuffer;
public class AccountBalanceMessage extends Message {
private static final int ADDRESS_LENGTH = Transformer.ADDRESS_LENGTH;
private final AccountBalanceData accountBalanceData;
private AccountBalanceData accountBalanceData;
public AccountBalanceMessage(AccountBalanceData accountBalanceData) {
super(MessageType.ACCOUNT_BALANCE);
this.accountBalanceData = accountBalanceData;
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
try {
// Send raw address instead of base58 encoded
byte[] address = Base58.decode(accountBalanceData.getAddress());
bytes.write(address);
bytes.write(Longs.toByteArray(accountBalanceData.getAssetId()));
bytes.write(Longs.toByteArray(accountBalanceData.getBalance()));
} catch (IOException e) {
throw new AssertionError("IOException shouldn't occur with ByteArrayOutputStream");
}
this.dataBytes = bytes.toByteArray();
this.checksumBytes = Message.generateChecksum(this.dataBytes);
}
public AccountBalanceMessage(int id, AccountBalanceData accountBalanceData) {
@ -33,7 +48,7 @@ public class AccountBalanceMessage extends Message {
}
public static Message fromByteBuffer(int id, ByteBuffer byteBuffer) throws UnsupportedEncodingException {
public static Message fromByteBuffer(int id, ByteBuffer byteBuffer) {
byte[] addressBytes = new byte[ADDRESS_LENGTH];
byteBuffer.get(addressBytes);
String address = Base58.encode(addressBytes);
@ -46,29 +61,6 @@ public class AccountBalanceMessage extends Message {
return new AccountBalanceMessage(id, accountBalanceData);
}
@Override
protected byte[] toData() {
if (this.accountBalanceData == null) {
return null;
}
try {
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
// Send raw address instead of base58 encoded
byte[] address = Base58.decode(this.accountBalanceData.getAddress());
bytes.write(address);
bytes.write(Longs.toByteArray(this.accountBalanceData.getAssetId()));
bytes.write(Longs.toByteArray(this.accountBalanceData.getBalance()));
return bytes.toByteArray();
} catch (IOException e) {
return null;
}
}
public AccountBalanceMessage cloneWithNewId(int newId) {
AccountBalanceMessage clone = new AccountBalanceMessage(this.accountBalanceData);
clone.setId(newId);

View File

@ -7,7 +7,6 @@ import org.qortal.utils.Base58;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.nio.ByteBuffer;
public class AccountMessage extends Message {
@ -16,12 +15,38 @@ public class AccountMessage extends Message {
private static final int REFERENCE_LENGTH = Transformer.SIGNATURE_LENGTH;
private static final int PUBLIC_KEY_LENGTH = Transformer.PUBLIC_KEY_LENGTH;
private final AccountData accountData;
private AccountData accountData;
public AccountMessage(AccountData accountData) {
super(MessageType.ACCOUNT);
this.accountData = accountData;
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
try {
// Send raw address instead of base58 encoded
byte[] address = Base58.decode(accountData.getAddress());
bytes.write(address);
bytes.write(accountData.getReference());
bytes.write(accountData.getPublicKey());
bytes.write(Ints.toByteArray(accountData.getDefaultGroupId()));
bytes.write(Ints.toByteArray(accountData.getFlags()));
bytes.write(Ints.toByteArray(accountData.getLevel()));
bytes.write(Ints.toByteArray(accountData.getBlocksMinted()));
bytes.write(Ints.toByteArray(accountData.getBlocksMintedAdjustment()));
} catch (IOException e) {
throw new AssertionError("IOException shouldn't occur with ByteArrayOutputStream");
}
this.dataBytes = bytes.toByteArray();
this.checksumBytes = Message.generateChecksum(this.dataBytes);
}
public AccountMessage(int id, AccountData accountData) {
@ -34,7 +59,7 @@ public class AccountMessage extends Message {
return this.accountData;
}
public static Message fromByteBuffer(int id, ByteBuffer byteBuffer) throws UnsupportedEncodingException {
public static Message fromByteBuffer(int id, ByteBuffer byteBuffer) {
byte[] addressBytes = new byte[ADDRESS_LENGTH];
byteBuffer.get(addressBytes);
String address = Base58.encode(addressBytes);
@ -59,39 +84,6 @@ public class AccountMessage extends Message {
return new AccountMessage(id, accountData);
}
@Override
protected byte[] toData() {
if (this.accountData == null) {
return null;
}
try {
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
// Send raw address instead of base58 encoded
byte[] address = Base58.decode(accountData.getAddress());
bytes.write(address);
bytes.write(accountData.getReference());
bytes.write(accountData.getPublicKey());
bytes.write(Ints.toByteArray(accountData.getDefaultGroupId()));
bytes.write(Ints.toByteArray(accountData.getFlags()));
bytes.write(Ints.toByteArray(accountData.getLevel()));
bytes.write(Ints.toByteArray(accountData.getBlocksMinted()));
bytes.write(Ints.toByteArray(accountData.getBlocksMintedAdjustment()));
return bytes.toByteArray();
} catch (IOException e) {
return null;
}
}
public AccountMessage cloneWithNewId(int newId) {
AccountMessage clone = new AccountMessage(this.accountData);
clone.setId(newId);

View File

@ -6,7 +6,6 @@ import org.qortal.utils.Base58;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.nio.ByteBuffer;
public class GetAccountBalanceMessage extends Message {
@ -17,7 +16,23 @@ public class GetAccountBalanceMessage extends Message {
private long assetId;
public GetAccountBalanceMessage(String address, long assetId) {
this(-1, address, assetId);
super(MessageType.GET_ACCOUNT_BALANCE);
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
try {
// Send raw address instead of base58 encoded
byte[] addressBytes = Base58.decode(address);
bytes.write(addressBytes);
bytes.write(Longs.toByteArray(assetId));
} catch (IOException e) {
throw new AssertionError("IOException shouldn't occur with ByteArrayOutputStream");
}
this.dataBytes = bytes.toByteArray();
this.checksumBytes = Message.generateChecksum(this.dataBytes);
}
private GetAccountBalanceMessage(int id, String address, long assetId) {
@ -35,7 +50,7 @@ public class GetAccountBalanceMessage extends Message {
return this.assetId;
}
public static Message fromByteBuffer(int id, ByteBuffer bytes) throws UnsupportedEncodingException {
public static Message fromByteBuffer(int id, ByteBuffer bytes) {
byte[] addressBytes = new byte[ADDRESS_LENGTH];
bytes.get(addressBytes);
String address = Base58.encode(addressBytes);
@ -45,21 +60,4 @@ public class GetAccountBalanceMessage extends Message {
return new GetAccountBalanceMessage(id, address, assetId);
}
@Override
protected byte[] toData() {
try {
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
// Send raw address instead of base58 encoded
byte[] address = Base58.decode(this.address);
bytes.write(address);
bytes.write(Longs.toByteArray(this.assetId));
return bytes.toByteArray();
} catch (IOException e) {
return null;
}
}
}

View File

@ -5,7 +5,7 @@ import org.qortal.utils.Base58;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.nio.BufferUnderflowException;
import java.nio.ByteBuffer;
public class GetAccountMessage extends Message {
@ -15,7 +15,21 @@ public class GetAccountMessage extends Message {
private String address;
public GetAccountMessage(String address) {
this(-1, address);
super(MessageType.GET_ACCOUNT);
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
try {
// Send raw address instead of base58 encoded
byte[] addressBytes = Base58.decode(address);
bytes.write(addressBytes);
} catch (IOException e) {
throw new AssertionError("IOException shouldn't occur with ByteArrayOutputStream");
}
this.dataBytes = bytes.toByteArray();
this.checksumBytes = Message.generateChecksum(this.dataBytes);
}
private GetAccountMessage(int id, String address) {
@ -28,9 +42,9 @@ public class GetAccountMessage extends Message {
return this.address;
}
public static Message fromByteBuffer(int id, ByteBuffer bytes) throws UnsupportedEncodingException {
public static Message fromByteBuffer(int id, ByteBuffer bytes) {
if (bytes.remaining() != ADDRESS_LENGTH)
return null;
throw new BufferUnderflowException();
byte[] addressBytes = new byte[ADDRESS_LENGTH];
bytes.get(addressBytes);
@ -39,19 +53,4 @@ public class GetAccountMessage extends Message {
return new GetAccountMessage(id, address);
}
@Override
protected byte[] toData() {
try {
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
// Send raw address instead of base58 encoded
byte[] address = Base58.decode(this.address);
bytes.write(address);
return bytes.toByteArray();
} catch (IOException e) {
return null;
}
}
}

View File

@ -5,7 +5,6 @@ import org.qortal.utils.Base58;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.nio.ByteBuffer;
public class GetAccountNamesMessage extends Message {
@ -15,7 +14,21 @@ public class GetAccountNamesMessage extends Message {
private String address;
public GetAccountNamesMessage(String address) {
this(-1, address);
super(MessageType.GET_ACCOUNT_NAMES);
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
try {
// Send raw address instead of base58 encoded
byte[] addressBytes = Base58.decode(address);
bytes.write(addressBytes);
} catch (IOException e) {
throw new AssertionError("IOException shouldn't occur with ByteArrayOutputStream");
}
this.dataBytes = bytes.toByteArray();
this.checksumBytes = Message.generateChecksum(this.dataBytes);
}
private GetAccountNamesMessage(int id, String address) {
@ -29,7 +42,7 @@ public class GetAccountNamesMessage extends Message {
}
public static Message fromByteBuffer(int id, ByteBuffer bytes) throws UnsupportedEncodingException {
public static Message fromByteBuffer(int id, ByteBuffer bytes) {
byte[] addressBytes = new byte[ADDRESS_LENGTH];
bytes.get(addressBytes);
String address = Base58.encode(addressBytes);
@ -37,19 +50,4 @@ public class GetAccountNamesMessage extends Message {
return new GetAccountNamesMessage(id, address);
}
@Override
protected byte[] toData() {
try {
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
// Send raw address instead of base58 encoded
byte[] address = Base58.decode(this.address);
bytes.write(address);
return bytes.toByteArray();
} catch (IOException e) {
return null;
}
}
}

View File

@ -6,7 +6,6 @@ import org.qortal.utils.Base58;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.nio.ByteBuffer;
public class GetAccountTransactionsMessage extends Message {
@ -18,7 +17,25 @@ public class GetAccountTransactionsMessage extends Message {
private int offset;
public GetAccountTransactionsMessage(String address, int limit, int offset) {
this(-1, address, limit, offset);
super(MessageType.GET_ACCOUNT_TRANSACTIONS);
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
try {
// Send raw address instead of base58 encoded
byte[] addressBytes = Base58.decode(address);
bytes.write(addressBytes);
bytes.write(Ints.toByteArray(limit));
bytes.write(Ints.toByteArray(offset));
} catch (IOException e) {
throw new AssertionError("IOException shouldn't occur with ByteArrayOutputStream");
}
this.dataBytes = bytes.toByteArray();
this.checksumBytes = Message.generateChecksum(this.dataBytes);
}
private GetAccountTransactionsMessage(int id, String address, int limit, int offset) {
@ -37,7 +54,7 @@ public class GetAccountTransactionsMessage extends Message {
public int getOffset() { return this.offset; }
public static Message fromByteBuffer(int id, ByteBuffer bytes) throws UnsupportedEncodingException {
public static Message fromByteBuffer(int id, ByteBuffer bytes) {
byte[] addressBytes = new byte[ADDRESS_LENGTH];
bytes.get(addressBytes);
String address = Base58.encode(addressBytes);
@ -49,23 +66,4 @@ public class GetAccountTransactionsMessage extends Message {
return new GetAccountTransactionsMessage(id, address, limit, offset);
}
@Override
protected byte[] toData() {
try {
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
// Send raw address instead of base58 encoded
byte[] address = Base58.decode(this.address);
bytes.write(address);
bytes.write(Ints.toByteArray(this.limit));
bytes.write(Ints.toByteArray(this.offset));
return bytes.toByteArray();
} catch (IOException e) {
return null;
}
}
}

View File

@ -2,12 +2,10 @@ package org.qortal.network.message;
import org.qortal.naming.Name;
import org.qortal.transform.TransformationException;
import org.qortal.transform.Transformer;
import org.qortal.utils.Serialization;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.nio.ByteBuffer;
public class GetNameMessage extends Message {
@ -15,7 +13,19 @@ public class GetNameMessage extends Message {
private String name;
public GetNameMessage(String address) {
this(-1, address);
super(MessageType.GET_NAME);
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
try {
Serialization.serializeSizedStringV2(bytes, name);
} catch (IOException e) {
throw new AssertionError("IOException shouldn't occur with ByteArrayOutputStream");
}
this.dataBytes = bytes.toByteArray();
this.checksumBytes = Message.generateChecksum(this.dataBytes);
}
private GetNameMessage(int id, String name) {
@ -29,26 +39,14 @@ public class GetNameMessage extends Message {
}
public static Message fromByteBuffer(int id, ByteBuffer bytes) throws UnsupportedEncodingException {
public static Message fromByteBuffer(int id, ByteBuffer bytes) throws MessageException {
try {
String name = Serialization.deserializeSizedStringV2(bytes, Name.MAX_NAME_SIZE);
return new GetNameMessage(id, name);
} catch (TransformationException e) {
return null;
}
}
@Override
protected byte[] toData() {
try {
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
Serialization.serializeSizedStringV2(bytes, this.name);
return bytes.toByteArray();
} catch (IOException e) {
return null;
throw new MessageException(e.getMessage(), e);
}
}

View File

@ -61,7 +61,21 @@ public enum MessageType {
GET_TRADE_PRESENCES(141, GetTradePresencesMessage::fromByteBuffer),
ARBITRARY_METADATA(150, ArbitraryMetadataMessage::fromByteBuffer),
GET_ARBITRARY_METADATA(151, GetArbitraryMetadataMessage::fromByteBuffer);
GET_ARBITRARY_METADATA(151, GetArbitraryMetadataMessage::fromByteBuffer),
// Lite node support
ACCOUNT(160, AccountMessage::fromByteBuffer),
GET_ACCOUNT(161, GetAccountMessage::fromByteBuffer),
ACCOUNT_BALANCE(170, AccountBalanceMessage::fromByteBuffer),
GET_ACCOUNT_BALANCE(171, GetAccountBalanceMessage::fromByteBuffer),
NAMES(180, NamesMessage::fromByteBuffer),
GET_ACCOUNT_NAMES(181, GetAccountNamesMessage::fromByteBuffer),
GET_NAME(182, GetNameMessage::fromByteBuffer),
TRANSACTIONS(190, TransactionsMessage::fromByteBuffer),
GET_ACCOUNT_TRANSACTIONS(191, GetAccountTransactionsMessage::fromByteBuffer);
public final int value;
public final MessageProducer fromByteBufferMethod;

View File

@ -10,7 +10,7 @@ import org.qortal.utils.Serialization;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.nio.BufferUnderflowException;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.List;
@ -19,12 +19,55 @@ public class NamesMessage extends Message {
private static final int SIGNATURE_LENGTH = Transformer.SIGNATURE_LENGTH;
private final List<NameData> nameDataList;
private List<NameData> nameDataList;
public NamesMessage(List<NameData> nameDataList) {
super(MessageType.NAMES);
this.nameDataList = nameDataList;
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
try {
bytes.write(Ints.toByteArray(nameDataList.size()));
for (int i = 0; i < nameDataList.size(); ++i) {
NameData nameData = nameDataList.get(i);
Serialization.serializeSizedStringV2(bytes, nameData.getName());
Serialization.serializeSizedStringV2(bytes, nameData.getReducedName());
Serialization.serializeAddress(bytes, nameData.getOwner());
Serialization.serializeSizedStringV2(bytes, nameData.getData());
bytes.write(Longs.toByteArray(nameData.getRegistered()));
Long updated = nameData.getUpdated();
int wasUpdated = (updated != null) ? 1 : 0;
bytes.write(Ints.toByteArray(wasUpdated));
if (updated != null) {
bytes.write(Longs.toByteArray(nameData.getUpdated()));
}
int isForSale = nameData.isForSale() ? 1 : 0;
bytes.write(Ints.toByteArray(isForSale));
if (nameData.isForSale()) {
bytes.write(Longs.toByteArray(nameData.getSalePrice()));
}
bytes.write(nameData.getReference());
bytes.write(Ints.toByteArray(nameData.getCreationGroupId()));
}
} catch (IOException e) {
throw new AssertionError("IOException shouldn't occur with ByteArrayOutputStream");
}
this.dataBytes = bytes.toByteArray();
this.checksumBytes = Message.generateChecksum(this.dataBytes);
}
public NamesMessage(int id, List<NameData> nameDataList) {
@ -38,7 +81,7 @@ public class NamesMessage extends Message {
}
public static Message fromByteBuffer(int id, ByteBuffer bytes) throws UnsupportedEncodingException {
public static Message fromByteBuffer(int id, ByteBuffer bytes) throws MessageException {
try {
final int nameCount = bytes.getInt();
@ -80,63 +123,13 @@ public class NamesMessage extends Message {
}
if (bytes.hasRemaining()) {
return null;
throw new BufferUnderflowException();
}
return new NamesMessage(id, nameDataList);
} catch (TransformationException e) {
return null;
}
}
@Override
protected byte[] toData() {
if (this.nameDataList == null) {
return null;
}
try {
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
bytes.write(Ints.toByteArray(this.nameDataList.size()));
for (int i = 0; i < this.nameDataList.size(); ++i) {
NameData nameData = this.nameDataList.get(i);
Serialization.serializeSizedStringV2(bytes, nameData.getName());
Serialization.serializeSizedStringV2(bytes, nameData.getReducedName());
Serialization.serializeAddress(bytes, nameData.getOwner());
Serialization.serializeSizedStringV2(bytes, nameData.getData());
bytes.write(Longs.toByteArray(nameData.getRegistered()));
Long updated = nameData.getUpdated();
int wasUpdated = (updated != null) ? 1 : 0;
bytes.write(Ints.toByteArray(wasUpdated));
if (updated != null) {
bytes.write(Longs.toByteArray(nameData.getUpdated()));
}
int isForSale = nameData.isForSale() ? 1 : 0;
bytes.write(Ints.toByteArray(isForSale));
if (nameData.isForSale()) {
bytes.write(Longs.toByteArray(nameData.getSalePrice()));
}
bytes.write(nameData.getReference());
bytes.write(Ints.toByteArray(nameData.getCreationGroupId()));
}
return bytes.toByteArray();
} catch (IOException e) {
return null;
throw new MessageException(e.getMessage(), e);
}
}

View File

@ -7,7 +7,7 @@ import org.qortal.transform.transaction.TransactionTransformer;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.nio.BufferUnderflowException;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.List;
@ -16,8 +16,29 @@ public class TransactionsMessage extends Message {
private List<TransactionData> transactions;
public TransactionsMessage(List<TransactionData> transactions) {
this(-1, transactions);
public TransactionsMessage(List<TransactionData> transactions) throws MessageException {
super(MessageType.TRANSACTIONS);
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
try {
bytes.write(Ints.toByteArray(transactions.size()));
for (int i = 0; i < transactions.size(); ++i) {
TransactionData transactionData = transactions.get(i);
byte[] serializedTransactionData = TransactionTransformer.toBytes(transactionData);
bytes.write(serializedTransactionData);
}
} catch (IOException e) {
throw new AssertionError("IOException shouldn't occur with ByteArrayOutputStream");
} catch (TransformationException e) {
throw new MessageException(e.getMessage(), e);
}
this.dataBytes = bytes.toByteArray();
this.checksumBytes = Message.generateChecksum(this.dataBytes);
}
private TransactionsMessage(int id, List<TransactionData> transactions) {
@ -30,7 +51,7 @@ public class TransactionsMessage extends Message {
return this.transactions;
}
public static Message fromByteBuffer(int id, ByteBuffer byteBuffer) throws UnsupportedEncodingException {
public static Message fromByteBuffer(int id, ByteBuffer byteBuffer) throws MessageException {
try {
final int transactionCount = byteBuffer.getInt();
@ -42,35 +63,13 @@ public class TransactionsMessage extends Message {
}
if (byteBuffer.hasRemaining()) {
return null;
throw new BufferUnderflowException();
}
return new TransactionsMessage(id, transactions);
} catch (TransformationException e) {
return null;
}
}
@Override
protected byte[] toData() {
if (this.transactions == null)
return null;
try {
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
bytes.write(Ints.toByteArray(this.transactions.size()));
for (int i = 0; i < this.transactions.size(); ++i) {
TransactionData transactionData = this.transactions.get(i);
byte[] serializedTransactionData = TransactionTransformer.toBytes(transactionData);
bytes.write(serializedTransactionData);
}
return bytes.toByteArray();
} catch (TransformationException | IOException e) {
return null;
throw new MessageException(e.getMessage(), e);
}
}