From bd51806a0d8d10625a858b92935b2d064de383d5 Mon Sep 17 00:00:00 2001 From: catbref Date: Mon, 14 Dec 2020 15:05:31 +0000 Subject: [PATCH] Improve Block.getBytesForMinterSignature() --- src/main/java/org/qortal/block/Block.java | 16 ++++--------- .../transform/block/BlockTransformer.java | 23 ++++++------------- 2 files changed, 11 insertions(+), 28 deletions(-) diff --git a/src/main/java/org/qortal/block/Block.java b/src/main/java/org/qortal/block/Block.java index b977a613..52cfc197 100644 --- a/src/main/java/org/qortal/block/Block.java +++ b/src/main/java/org/qortal/block/Block.java @@ -357,12 +357,8 @@ public class Block { System.arraycopy(onlineAccountData.getSignature(), 0, onlineAccountsSignatures, i * Transformer.SIGNATURE_LENGTH, Transformer.SIGNATURE_LENGTH); } - byte[] minterSignature; - try { - minterSignature = minter.sign(BlockTransformer.getBytesForMinterSignature(parentBlockData.getMinterSignature(), minter, encodedOnlineAccounts)); - } catch (TransformationException e) { - throw new DataException("Unable to calculate next block minter signature", e); - } + byte[] minterSignature = minter.sign(BlockTransformer.getBytesForMinterSignature(parentBlockData.getMinterSignature(), + minter.getPublicKey(), encodedOnlineAccounts)); // Qortal: minter is always a reward-share, so find actual minter and get their effective minting level int minterLevel = Account.getRewardShareEffectiveMintingLevel(repository, minter.getPublicKey()); @@ -428,12 +424,8 @@ public class Block { int version = this.blockData.getVersion(); byte[] reference = this.blockData.getReference(); - byte[] minterSignature; - try { - minterSignature = minter.sign(BlockTransformer.getBytesForMinterSignature(parentBlockData.getMinterSignature(), minter, this.blockData.getEncodedOnlineAccounts())); - } catch (TransformationException e) { - throw new DataException("Unable to calculate next block's minter signature", e); - } + byte[] minterSignature = minter.sign(BlockTransformer.getBytesForMinterSignature(parentBlockData.getMinterSignature(), + minter.getPublicKey(), this.blockData.getEncodedOnlineAccounts())); // Qortal: minter is always a reward-share, so find actual minter and get their effective minting level int minterLevel = Account.getRewardShareEffectiveMintingLevel(repository, minter.getPublicKey()); diff --git a/src/main/java/org/qortal/transform/block/BlockTransformer.java b/src/main/java/org/qortal/transform/block/BlockTransformer.java index 4c960118..fcc0bcad 100644 --- a/src/main/java/org/qortal/transform/block/BlockTransformer.java +++ b/src/main/java/org/qortal/transform/block/BlockTransformer.java @@ -7,7 +7,6 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.List; -import org.qortal.account.PublicKeyAccount; import org.qortal.block.Block; import org.qortal.block.BlockChain; import org.qortal.data.at.ATStateData; @@ -23,7 +22,6 @@ import org.qortal.utils.Base58; import org.qortal.utils.Serialization; import org.qortal.utils.Triple; -import com.google.common.primitives.Bytes; import com.google.common.primitives.Ints; import com.google.common.primitives.Longs; @@ -334,27 +332,20 @@ public class BlockTransformer extends Transformer { public static byte[] getBytesForMinterSignature(BlockData blockData) throws TransformationException { byte[] minterSignature = getMinterSignatureFromReference(blockData.getReference()); - PublicKeyAccount minter = new PublicKeyAccount(null, blockData.getMinterPublicKey()); - return getBytesForMinterSignature(minterSignature, minter, blockData.getEncodedOnlineAccounts()); + return getBytesForMinterSignature(minterSignature, blockData.getMinterPublicKey(), blockData.getEncodedOnlineAccounts()); } - public static byte[] getBytesForMinterSignature(byte[] minterSignature, PublicKeyAccount minter, byte[] encodedOnlineAccounts) - throws TransformationException { - try { - ByteArrayOutputStream bytes = new ByteArrayOutputStream(MINTER_SIGNATURE_LENGTH + MINTER_PUBLIC_KEY_LENGTH + encodedOnlineAccounts.length); + public static byte[] getBytesForMinterSignature(byte[] minterSignature, byte[] minterPublicKey, byte[] encodedOnlineAccounts) { + byte[] bytes = new byte[MINTER_SIGNATURE_LENGTH + MINTER_PUBLIC_KEY_LENGTH + encodedOnlineAccounts.length]; - bytes.write(minterSignature); + System.arraycopy(minterSignature, 0, bytes, 0, MINTER_SIGNATURE_LENGTH); - // We're padding here just in case the minter is the genesis account whose public key is only 8 bytes long. - bytes.write(Bytes.ensureCapacity(minter.getPublicKey(), MINTER_PUBLIC_KEY_LENGTH, 0)); + System.arraycopy(minterPublicKey, 0, bytes, MINTER_SIGNATURE_LENGTH, MINTER_PUBLIC_KEY_LENGTH); - bytes.write(encodedOnlineAccounts); + System.arraycopy(encodedOnlineAccounts, 0, bytes, MINTER_SIGNATURE_LENGTH + MINTER_PUBLIC_KEY_LENGTH, encodedOnlineAccounts.length); - return bytes.toByteArray(); - } catch (IOException e) { - throw new TransformationException(e); - } + return bytes; } public static byte[] getBytesForTransactionsSignature(Block block) throws TransformationException {