qortal/tests/test/SignatureTests.java

68 lines
2.5 KiB
Java
Raw Normal View History

package test;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
import java.math.BigDecimal;
import data.block.BlockData;
import qora.account.PrivateKeyAccount;
import qora.block.Block;
import qora.block.GenesisBlock;
import repository.DataException;
import repository.Repository;
import repository.RepositoryManager;
import utils.Base58;
import utils.NTP;
public class SignatureTests extends Common {
@Test
public void testGenesisBlockSignature() throws DataException {
String expected58 = "6pHMBFif7jXFG654joT8GPaymau1fMtaxacRyqSrnAwQMQDvqRuLpHpfFyqX4gWVvj4pF1mwQhFgqWAvjVvPJUjmBZQvL751dM9cEcQBTaUcxtNLuWZCVUAtbnWN9f7FsLppHhkPbxwpoodL3UJYRGt3EZrG17mhv1RJbmq8j6rr7Mk";
try (final Repository repository = RepositoryManager.getRepository()) {
GenesisBlock block = GenesisBlock.getInstance(repository);
BlockData blockData = block.getBlockData();
System.out
.println("Generator: " + block.getGenerator().getAddress() + ", generator signature: " + Base58.encode(blockData.getGeneratorSignature()));
assertEquals(expected58, Base58.encode(block.getSignature()));
}
}
@Test
public void testBlockSignature() throws DataException {
2018-11-02 10:30:51 +00:00
try (final Repository repository = RepositoryManager.getRepository()) {
PrivateKeyAccount generator = new PrivateKeyAccount(repository,
new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31 });
2018-11-02 10:30:51 +00:00
int version = 3;
2018-11-02 10:30:51 +00:00
byte[] reference = Base58.decode(
"BSfgEr6r1rXGGJCv8criR5NcBWfpHdJnm9x5unPwxvojEKCESv1wH1zJm7yvCeC48wshymYtARbHdUojbqWCCWW7h2UTc8g5oEx59C9M41dM7H48My8gVkcEZdxR1of3VgpE5UcowFp3kFC12hVcD9hUttJ2i2nZWMwprbFtUGyVv1U");
2018-11-02 10:30:51 +00:00
int transactionCount = 0;
BigDecimal totalFees = BigDecimal.ZERO.setScale(8);
byte[] transactionsSignature = null;
int height = 0;
long timestamp = NTP.getTime() - 5000;
BigDecimal generatingBalance = BigDecimal.valueOf(10_000_000L).setScale(8);
byte[] generatorPublicKey = generator.getPublicKey();
byte[] generatorSignature = null;
int atCount = 0;
BigDecimal atFees = BigDecimal.valueOf(10_000_000L).setScale(8);
2018-11-02 10:30:51 +00:00
BlockData blockData = new BlockData(version, reference, transactionCount, totalFees, transactionsSignature, height, timestamp, generatingBalance,
generatorPublicKey, generatorSignature, atCount, atFees);
2018-11-02 10:30:51 +00:00
Block block = new Block(repository, blockData, generator);
Progess on block and transaction processing + tidying up * Code added for calculating an account's generating balance. (CIYAM AT support yet to be added). * Added associated code in Block for calculating next block's timestamp, generating balance, base target, etc. * ValidationResult enum added to Block, mostly to aid debugging. * Block.isValid() now returns ValidationResult instead of boolean. * Block.isValid() now has added proof-of-stake tests. * Some blockchain-related constants, like feature release heights/timestamps, moved from Block to BlockChain. * Added better Block constructor for use when creating a new block. * Added helpful 'navigation' methods to Block to get to block's parent (or child). * Changed visibility of block's individual signature calculators to protected, in favour of public sign() method. * Added asset existence check to Payment.isValid. * All current transaction objects (qora.transaction.*) now have private subclassed transaction variable to save multiple casts in various methods. * Also added to above: * isInvolved(Account) : boolean * getRecipients() : List<Account> * getAmount(Account) : BigDecimal * Added BlockRepository.getLastBlock() to fetch highest block in blockchain. * Added diagnostics to HSQLDBRepository.close() to alert if there are any uncommitted changes during closure. (Currently under suspicion due to possible HSQLDB bug!) * Old "TransactionTests" renamed to "SerializationTests" as that's what they really are. * New "TransactionTests" added to test processing of transactions. (Currently only a PaymentTransaction). * PaymentTransformer.toBytes() detects and skips null signature. This was causing issues with Transaction.toBytesLessSignature(). Needs rolling out to other transaction types if acceptable.
2018-06-15 16:16:44 +00:00
block.sign();
assertTrue(block.isSignatureValid());
}
}
}