Remove more old Qora v1 compatibility code

This commit is contained in:
catbref 2020-04-27 15:17:22 +01:00
parent bd521baade
commit cd066cf357
4 changed files with 83 additions and 145 deletions

View File

@ -1107,14 +1107,8 @@ public class Block {
if (this.ourAtStates == this.getATStates()) // Note object reference compare if (this.ourAtStates == this.getATStates()) // Note object reference compare
return ValidationResult.OK; return ValidationResult.OK;
// For old v1 CIYAM ATs we blindly accept them // Generate local AT states for comparison
if (this.blockData.getVersion() < 4) { this.executeATs();
this.ourAtStates = this.atStates;
this.ourAtFees = this.blockData.getATFees();
} else {
// Generate local AT states for comparison
this.executeATs();
}
// Check locally generated AT states against ones received from elsewhere // Check locally generated AT states against ones received from elsewhere

View File

@ -49,7 +49,6 @@ public class ArbitraryTransactionData extends TransactionData {
this.creatorPublicKey = this.senderPublicKey; this.creatorPublicKey = this.senderPublicKey;
} }
/** V3 */
public ArbitraryTransactionData(BaseTransactionData baseTransactionData, public ArbitraryTransactionData(BaseTransactionData baseTransactionData,
int version, int service, byte[] data, DataType dataType, List<PaymentData> payments) { int version, int service, byte[] data, DataType dataType, List<PaymentData> payments) {
super(TransactionType.ARBITRARY, baseTransactionData); super(TransactionType.ARBITRARY, baseTransactionData);
@ -62,12 +61,6 @@ public class ArbitraryTransactionData extends TransactionData {
this.payments = payments; this.payments = payments;
} }
/** V1 */
public ArbitraryTransactionData(BaseTransactionData baseTransactionData,
int version, int service, byte[] data, DataType dataType) {
this(baseTransactionData, version, service, data, dataType, null);
}
// Getters/Setters // Getters/Setters
public byte[] getSenderPublicKey() { public byte[] getSenderPublicKey() {

View File

@ -54,8 +54,7 @@ public class BlockTransformer extends Transformer {
protected static final int ONLINE_ACCOUNTS_TIMESTAMP_LENGTH = LONG_LENGTH; protected static final int ONLINE_ACCOUNTS_TIMESTAMP_LENGTH = LONG_LENGTH;
protected static final int ONLINE_ACCOUNTS_SIGNATURES_COUNT_LENGTH = INT_LENGTH; protected static final int ONLINE_ACCOUNTS_SIGNATURES_COUNT_LENGTH = INT_LENGTH;
protected static final int V2_AT_ENTRY_LENGTH = ADDRESS_LENGTH + MD5_LENGTH; protected static final int AT_ENTRY_LENGTH = ADDRESS_LENGTH + SHA256_LENGTH + BIG_DECIMAL_LENGTH;
protected static final int V4_AT_ENTRY_LENGTH = ADDRESS_LENGTH + SHA256_LENGTH + BIG_DECIMAL_LENGTH;
/** /**
* Extract block data and transaction data from serialized bytes. * Extract block data and transaction data from serialized bytes.
@ -86,8 +85,8 @@ public class BlockTransformer extends Transformer {
public static Triple<BlockData, List<TransactionData>, List<ATStateData>> fromByteBuffer(ByteBuffer byteBuffer) throws TransformationException { public static Triple<BlockData, List<TransactionData>, List<ATStateData>> fromByteBuffer(ByteBuffer byteBuffer) throws TransformationException {
int version = byteBuffer.getInt(); int version = byteBuffer.getInt();
if (version >= 2 && byteBuffer.remaining() < BASE_LENGTH + AT_BYTES_LENGTH - VERSION_LENGTH) if (byteBuffer.remaining() < BASE_LENGTH + AT_BYTES_LENGTH - VERSION_LENGTH)
throw new TransformationException("Byte data too short for V2+ Block"); throw new TransformationException("Byte data too short for Block");
if (byteBuffer.remaining() > BlockChain.getInstance().getMaxBlockSize()) if (byteBuffer.remaining() > BlockChain.getInstance().getMaxBlockSize())
throw new TransformationException("Byte data too long for Block"); throw new TransformationException("Byte data too long for Block");
@ -111,64 +110,39 @@ public class BlockTransformer extends Transformer {
BigDecimal atFees = BigDecimal.ZERO.setScale(8); BigDecimal atFees = BigDecimal.ZERO.setScale(8);
List<ATStateData> atStates = new ArrayList<>(); List<ATStateData> atStates = new ArrayList<>();
if (version >= 2) { int atBytesLength = byteBuffer.getInt();
int atBytesLength = byteBuffer.getInt();
if (atBytesLength > BlockChain.getInstance().getMaxBlockSize()) if (atBytesLength > BlockChain.getInstance().getMaxBlockSize())
throw new TransformationException("Byte data too long for Block's AT info"); throw new TransformationException("Byte data too long for Block's AT info");
ByteBuffer atByteBuffer = byteBuffer.slice(); ByteBuffer atByteBuffer = byteBuffer.slice();
atByteBuffer.limit(atBytesLength); atByteBuffer.limit(atBytesLength);
if (version < 4) { // Read AT-address, SHA256 hash and fees
// For versions < 4, read AT-address & MD5 pairs if (atBytesLength % AT_ENTRY_LENGTH != 0)
if (atBytesLength % V2_AT_ENTRY_LENGTH != 0) throw new TransformationException("AT byte data not a multiple of AT entry length");
throw new TransformationException("AT byte data not a multiple of version 2+ entries");
while (atByteBuffer.hasRemaining()) { while (atByteBuffer.hasRemaining()) {
byte[] atAddressBytes = new byte[ADDRESS_LENGTH]; byte[] atAddressBytes = new byte[ADDRESS_LENGTH];
atByteBuffer.get(atAddressBytes); atByteBuffer.get(atAddressBytes);
String atAddress = Base58.encode(atAddressBytes); String atAddress = Base58.encode(atAddressBytes);
byte[] stateHash = new byte[MD5_LENGTH]; byte[] stateHash = new byte[SHA256_LENGTH];
atByteBuffer.get(stateHash); atByteBuffer.get(stateHash);
atStates.add(new ATStateData(atAddress, stateHash)); BigDecimal fees = Serialization.deserializeBigDecimal(atByteBuffer);
} // Add this AT's fees to our total
atFees = atFees.add(fees);
// Bump byteBuffer over AT states just read in slice atStates.add(new ATStateData(atAddress, stateHash, fees));
byteBuffer.position(byteBuffer.position() + atBytesLength);
// AT fees follow in versions < 4
atFees = Serialization.deserializeBigDecimal(byteBuffer);
} else {
// For block versions >= 4, read AT-address, SHA256 hash and fees
if (atBytesLength % V4_AT_ENTRY_LENGTH != 0)
throw new TransformationException("AT byte data not a multiple of version 4+ entries");
while (atByteBuffer.hasRemaining()) {
byte[] atAddressBytes = new byte[ADDRESS_LENGTH];
atByteBuffer.get(atAddressBytes);
String atAddress = Base58.encode(atAddressBytes);
byte[] stateHash = new byte[SHA256_LENGTH];
atByteBuffer.get(stateHash);
BigDecimal fees = Serialization.deserializeBigDecimal(atByteBuffer);
// Add this AT's fees to our total
atFees = atFees.add(fees);
atStates.add(new ATStateData(atAddress, stateHash, fees));
}
}
// AT count to reflect the number of states we have
atCount = atStates.size();
// Add AT fees to totalFees
totalFees = totalFees.add(atFees);
} }
// AT count to reflect the number of states we have
atCount = atStates.size();
// Add AT fees to totalFees
totalFees = totalFees.add(atFees);
int transactionCount = byteBuffer.getInt(); int transactionCount = byteBuffer.getInt();
// Parse transactions now, compared to deferred parsing in Gen1, so we can throw ParseException if need be. // Parse transactions now, compared to deferred parsing in Gen1, so we can throw ParseException if need be.
@ -201,39 +175,37 @@ public class BlockTransformer extends Transformer {
byte[] onlineAccountsSignatures = null; byte[] onlineAccountsSignatures = null;
Long onlineAccountsTimestamp = null; Long onlineAccountsTimestamp = null;
if (version >= 4) { onlineAccountsCount = byteBuffer.getInt();
onlineAccountsCount = byteBuffer.getInt();
int conciseSetLength = byteBuffer.getInt(); int conciseSetLength = byteBuffer.getInt();
if (conciseSetLength > BlockChain.getInstance().getMaxBlockSize()) if (conciseSetLength > BlockChain.getInstance().getMaxBlockSize())
throw new TransformationException("Byte data too long for online account info"); throw new TransformationException("Byte data too long for online account info");
if ((conciseSetLength & 3) != 0) if ((conciseSetLength & 3) != 0)
throw new TransformationException("Byte data length not multiple of 4 for online account info"); throw new TransformationException("Byte data length not multiple of 4 for online account info");
encodedOnlineAccounts = new byte[conciseSetLength]; encodedOnlineAccounts = new byte[conciseSetLength];
byteBuffer.get(encodedOnlineAccounts); byteBuffer.get(encodedOnlineAccounts);
// Try to decode to ConciseSet // Try to decode to ConciseSet
ConciseSet accountsIndexes = BlockTransformer.decodeOnlineAccounts(encodedOnlineAccounts); ConciseSet accountsIndexes = BlockTransformer.decodeOnlineAccounts(encodedOnlineAccounts);
if (accountsIndexes.size() != onlineAccountsCount) if (accountsIndexes.size() != onlineAccountsCount)
throw new TransformationException("Block's online account data malformed"); throw new TransformationException("Block's online account data malformed");
// Note: number of signatures, not byte length // Note: number of signatures, not byte length
int onlineAccountsSignaturesCount = byteBuffer.getInt(); int onlineAccountsSignaturesCount = byteBuffer.getInt();
if (onlineAccountsSignaturesCount > 0) { if (onlineAccountsSignaturesCount > 0) {
// Online accounts timestamp is only present if there are also signatures // Online accounts timestamp is only present if there are also signatures
onlineAccountsTimestamp = byteBuffer.getLong(); onlineAccountsTimestamp = byteBuffer.getLong();
final int signaturesByteLength = onlineAccountsSignaturesCount * Transformer.SIGNATURE_LENGTH; final int signaturesByteLength = onlineAccountsSignaturesCount * Transformer.SIGNATURE_LENGTH;
if (signaturesByteLength > BlockChain.getInstance().getMaxBlockSize()) if (signaturesByteLength > BlockChain.getInstance().getMaxBlockSize())
throw new TransformationException("Byte data too long for online accounts signatures"); throw new TransformationException("Byte data too long for online accounts signatures");
onlineAccountsSignatures = new byte[signaturesByteLength]; onlineAccountsSignatures = new byte[signaturesByteLength];
byteBuffer.get(onlineAccountsSignatures); byteBuffer.get(onlineAccountsSignatures);
}
} }
if (byteBuffer.hasRemaining()) if (byteBuffer.hasRemaining())
@ -251,16 +223,13 @@ public class BlockTransformer extends Transformer {
BlockData blockData = block.getBlockData(); BlockData blockData = block.getBlockData();
int blockLength = BASE_LENGTH; int blockLength = BASE_LENGTH;
if (blockData.getVersion() >= 4) { blockLength += AT_BYTES_LENGTH + blockData.getATCount() * AT_ENTRY_LENGTH;
blockLength += AT_BYTES_LENGTH + blockData.getATCount() * V4_AT_ENTRY_LENGTH; blockLength += ONLINE_ACCOUNTS_COUNT_LENGTH + ONLINE_ACCOUNTS_SIZE_LENGTH + blockData.getEncodedOnlineAccounts().length;
blockLength += ONLINE_ACCOUNTS_COUNT_LENGTH + ONLINE_ACCOUNTS_SIZE_LENGTH + blockData.getEncodedOnlineAccounts().length; blockLength += ONLINE_ACCOUNTS_SIGNATURES_COUNT_LENGTH;
blockLength += ONLINE_ACCOUNTS_SIGNATURES_COUNT_LENGTH;
byte[] onlineAccountsSignatures = blockData.getOnlineAccountsSignatures(); byte[] onlineAccountsSignatures = blockData.getOnlineAccountsSignatures();
if (onlineAccountsSignatures != null && onlineAccountsSignatures.length > 0) if (onlineAccountsSignatures != null && onlineAccountsSignatures.length > 0)
blockLength += ONLINE_ACCOUNTS_TIMESTAMP_LENGTH + blockData.getOnlineAccountsSignatures().length; blockLength += ONLINE_ACCOUNTS_TIMESTAMP_LENGTH + blockData.getOnlineAccountsSignatures().length;
} else if (blockData.getVersion() >= 2)
blockLength += AT_FEES_LENGTH + AT_BYTES_LENGTH + blockData.getATCount() * V2_AT_ENTRY_LENGTH;
try { try {
// Short cut for no transactions // Short cut for no transactions
@ -290,29 +259,13 @@ public class BlockTransformer extends Transformer {
bytes.write(blockData.getTransactionsSignature()); bytes.write(blockData.getTransactionsSignature());
bytes.write(blockData.getMinterSignature()); bytes.write(blockData.getMinterSignature());
if (blockData.getVersion() >= 4) { int atBytesLength = blockData.getATCount() * AT_ENTRY_LENGTH;
int atBytesLength = blockData.getATCount() * V4_AT_ENTRY_LENGTH; bytes.write(Ints.toByteArray(atBytesLength));
bytes.write(Ints.toByteArray(atBytesLength));
for (ATStateData atStateData : block.getATStates()) { for (ATStateData atStateData : block.getATStates()) {
bytes.write(Base58.decode(atStateData.getATAddress())); bytes.write(Base58.decode(atStateData.getATAddress()));
bytes.write(atStateData.getStateHash()); bytes.write(atStateData.getStateHash());
Serialization.serializeBigDecimal(bytes, atStateData.getFees()); Serialization.serializeBigDecimal(bytes, atStateData.getFees());
}
} else if (blockData.getVersion() >= 2) {
int atBytesLength = blockData.getATCount() * V2_AT_ENTRY_LENGTH;
bytes.write(Ints.toByteArray(atBytesLength));
for (ATStateData atStateData : block.getATStates()) {
bytes.write(Base58.decode(atStateData.getATAddress()));
bytes.write(atStateData.getStateHash());
}
if (blockData.getATFees() != null)
// NOTE: atFees serialized as long value, not as BigDecimal, for historic compatibility
bytes.write(Longs.toByteArray(blockData.getATFees().longValue()));
else
bytes.write(Longs.toByteArray(0));
} }
// Transactions // Transactions
@ -325,33 +278,31 @@ public class BlockTransformer extends Transformer {
} }
// Online account info // Online account info
if (blockData.getVersion() >= 4) { byte[] encodedOnlineAccounts = blockData.getEncodedOnlineAccounts();
byte[] encodedOnlineAccounts = blockData.getEncodedOnlineAccounts();
if (encodedOnlineAccounts != null) { if (encodedOnlineAccounts != null) {
bytes.write(Ints.toByteArray(blockData.getOnlineAccountsCount())); bytes.write(Ints.toByteArray(blockData.getOnlineAccountsCount()));
bytes.write(Ints.toByteArray(encodedOnlineAccounts.length)); bytes.write(Ints.toByteArray(encodedOnlineAccounts.length));
bytes.write(encodedOnlineAccounts); bytes.write(encodedOnlineAccounts);
} else { } else {
bytes.write(Ints.toByteArray(0)); // onlineAccountsCount bytes.write(Ints.toByteArray(0)); // onlineAccountsCount
bytes.write(Ints.toByteArray(0)); // encodedOnlineAccounts length bytes.write(Ints.toByteArray(0)); // encodedOnlineAccounts length
} }
byte[] onlineAccountsSignatures = blockData.getOnlineAccountsSignatures(); byte[] onlineAccountsSignatures = blockData.getOnlineAccountsSignatures();
if (onlineAccountsSignatures != null && onlineAccountsSignatures.length > 0) { if (onlineAccountsSignatures != null && onlineAccountsSignatures.length > 0) {
// Note: we write the number of signatures, not the number of bytes // Note: we write the number of signatures, not the number of bytes
bytes.write(Ints.toByteArray(onlineAccountsSignatures.length / Transformer.SIGNATURE_LENGTH)); bytes.write(Ints.toByteArray(onlineAccountsSignatures.length / Transformer.SIGNATURE_LENGTH));
// We only write online accounts timestamp if we have signatures // We only write online accounts timestamp if we have signatures
bytes.write(Longs.toByteArray(blockData.getOnlineAccountsTimestamp())); bytes.write(Longs.toByteArray(blockData.getOnlineAccountsTimestamp()));
bytes.write(onlineAccountsSignatures); bytes.write(onlineAccountsSignatures);
} else { } else {
// Zero online accounts signatures (timestamp omitted also) // Zero online accounts signatures (timestamp omitted also)
bytes.write(Ints.toByteArray(0)); bytes.write(Ints.toByteArray(0));
}
} }
return bytes.toByteArray(); return bytes.toByteArray();

View File

@ -67,7 +67,7 @@ public class ArbitraryTransactionTransformer extends TransactionTransformer {
byte[] senderPublicKey = Serialization.deserializePublicKey(byteBuffer); byte[] senderPublicKey = Serialization.deserializePublicKey(byteBuffer);
// V3+ allows payments but always return a list of payments, even if empty // Always return a list of payments, even if empty
List<PaymentData> payments = new ArrayList<>(); List<PaymentData> payments = new ArrayList<>();
if (version != 1) { if (version != 1) {
int paymentsCount = byteBuffer.getInt(); int paymentsCount = byteBuffer.getInt();