truncate labels over 255 chars before persisting to db

This commit is contained in:
Craig Raw 2021-08-16 10:15:14 +02:00
parent 74b4f51640
commit 8f7f0d4c61
4 changed files with 26 additions and 10 deletions

View file

@ -45,16 +45,20 @@ public interface BlockTransactionDao {
Map<Sha256Hash, BlockTransaction> existing = getForTxId(txid.getBytes()); Map<Sha256Hash, BlockTransaction> existing = getForTxId(txid.getBytes());
if(existing.isEmpty() && blkTx.getId() == null) { if(existing.isEmpty() && blkTx.getId() == null) {
long id = insertBlockTransaction(txid.getBytes(), blkTx.getHash().getBytes(), blkTx.getHeight(), blkTx.getDate(), blkTx.getFee(), blkTx.getLabel(), long id = insertBlockTransaction(txid.getBytes(), blkTx.getHash().getBytes(), blkTx.getHeight(), blkTx.getDate(), blkTx.getFee(), truncate(blkTx.getLabel()),
blkTx.getTransaction() == null ? null : blkTx.getTransaction().bitcoinSerialize(), blkTx.getTransaction() == null ? null : blkTx.getTransaction().bitcoinSerialize(),
blkTx.getBlockHash() == null ? null : blkTx.getBlockHash().getBytes(), wallet.getId()); blkTx.getBlockHash() == null ? null : blkTx.getBlockHash().getBytes(), wallet.getId());
blkTx.setId(id); blkTx.setId(id);
} else { } else {
Long existingId = existing.get(txid) != null ? existing.get(txid).getId() : blkTx.getId(); Long existingId = existing.get(txid) != null ? existing.get(txid).getId() : blkTx.getId();
updateBlockTransaction(txid.getBytes(), blkTx.getHash().getBytes(), blkTx.getHeight(), blkTx.getDate(), blkTx.getFee(), blkTx.getLabel(), updateBlockTransaction(txid.getBytes(), blkTx.getHash().getBytes(), blkTx.getHeight(), blkTx.getDate(), blkTx.getFee(), truncate(blkTx.getLabel()),
blkTx.getTransaction() == null ? null : blkTx.getTransaction().bitcoinSerialize(), blkTx.getTransaction() == null ? null : blkTx.getTransaction().bitcoinSerialize(),
blkTx.getBlockHash() == null ? null : blkTx.getBlockHash().getBytes(), wallet.getId(), existingId); blkTx.getBlockHash() == null ? null : blkTx.getBlockHash().getBytes(), wallet.getId(), existingId);
blkTx.setId(existingId); blkTx.setId(existingId);
} }
} }
default String truncate(String label) {
return (label != null && label.length() > 255 ? label.substring(0, 255) : label);
}
} }

View file

@ -68,7 +68,7 @@ public interface KeystoreDao {
} }
} }
long id = insert(keystore.getLabel(), keystore.getSource().ordinal(), keystore.getWalletModel().ordinal(), long id = insert(truncate(keystore.getLabel()), keystore.getSource().ordinal(), keystore.getWalletModel().ordinal(),
keystore.hasPrivateKey() ? null : keystore.getKeyDerivation().getMasterFingerprint(), keystore.hasPrivateKey() ? null : keystore.getKeyDerivation().getMasterFingerprint(),
keystore.getKeyDerivation().getDerivationPath(), keystore.getKeyDerivation().getDerivationPath(),
keystore.hasPrivateKey() ? null : keystore.getExtendedPublicKey().toString(), keystore.hasPrivateKey() ? null : keystore.getExtendedPublicKey().toString(),
@ -99,4 +99,8 @@ public interface KeystoreDao {
} }
} }
} }
default String truncate(String label) {
return (label != null && label.length() > 255 ? label.substring(0, 255) : label);
}
} }

View file

@ -93,7 +93,7 @@ public interface WalletDao {
setSchema(schema); setSchema(schema);
createPolicyDao().addPolicy(wallet.getDefaultPolicy()); createPolicyDao().addPolicy(wallet.getDefaultPolicy());
long id = insert(wallet.getName(), wallet.getNetwork().ordinal(), wallet.getPolicyType().ordinal(), wallet.getScriptType().ordinal(), wallet.getStoredBlockHeight(), wallet.gapLimit(), wallet.getBirthDate(), wallet.getDefaultPolicy().getId()); long id = insert(truncate(wallet.getName()), wallet.getNetwork().ordinal(), wallet.getPolicyType().ordinal(), wallet.getScriptType().ordinal(), wallet.getStoredBlockHeight(), wallet.gapLimit(), wallet.getBirthDate(), wallet.getDefaultPolicy().getId());
wallet.setId(id); wallet.setId(id);
createKeystoreDao().addKeystores(wallet); createKeystoreDao().addKeystores(wallet);
@ -103,4 +103,8 @@ public interface WalletDao {
setSchema(DbPersistence.DEFAULT_SCHEMA); setSchema(DbPersistence.DEFAULT_SCHEMA);
} }
} }
default String truncate(String label) {
return (label != null && label.length() > 255 ? label.substring(0, 255) : label);
}
} }

View file

@ -58,10 +58,10 @@ public interface WalletNodeDao {
default void addWalletNodes(Wallet wallet) { default void addWalletNodes(Wallet wallet) {
for(WalletNode purposeNode : wallet.getPurposeNodes()) { for(WalletNode purposeNode : wallet.getPurposeNodes()) {
long purposeNodeId = insertWalletNode(purposeNode.getDerivationPath(), purposeNode.getLabel(), wallet.getId(), null); long purposeNodeId = insertWalletNode(purposeNode.getDerivationPath(), truncate(purposeNode.getLabel()), wallet.getId(), null);
purposeNode.setId(purposeNodeId); purposeNode.setId(purposeNodeId);
for(WalletNode addressNode : purposeNode.getChildren()) { for(WalletNode addressNode : purposeNode.getChildren()) {
long addressNodeId = insertWalletNode(addressNode.getDerivationPath(), addressNode.getLabel(), wallet.getId(), purposeNodeId); long addressNodeId = insertWalletNode(addressNode.getDerivationPath(), truncate(addressNode.getLabel()), wallet.getId(), purposeNodeId);
addressNode.setId(addressNodeId); addressNode.setId(addressNodeId);
addTransactionOutputs(addressNode); addTransactionOutputs(addressNode);
} }
@ -84,22 +84,22 @@ public interface WalletNodeDao {
if(txo.isSpent()) { if(txo.isSpent()) {
BlockTransactionHashIndex spentBy = txo.getSpentBy(); BlockTransactionHashIndex spentBy = txo.getSpentBy();
if(spentBy.getId() == null) { if(spentBy.getId() == null) {
spentById = insertBlockTransactionHashIndex(spentBy.getHash().getBytes(), spentBy.getHeight(), spentBy.getDate(), spentBy.getFee(), spentBy.getLabel(), spentBy.getIndex(), spentBy.getValue(), spentById = insertBlockTransactionHashIndex(spentBy.getHash().getBytes(), spentBy.getHeight(), spentBy.getDate(), spentBy.getFee(), truncate(spentBy.getLabel()), spentBy.getIndex(), spentBy.getValue(),
spentBy.getStatus() == null ? null : spentBy.getStatus().ordinal(), null, addressNode.getId()); spentBy.getStatus() == null ? null : spentBy.getStatus().ordinal(), null, addressNode.getId());
spentBy.setId(spentById); spentBy.setId(spentById);
} else { } else {
updateBlockTransactionHashIndex(spentBy.getHash().getBytes(), spentBy.getHeight(), spentBy.getDate(), spentBy.getFee(), spentBy.getLabel(), spentBy.getIndex(), spentBy.getValue(), updateBlockTransactionHashIndex(spentBy.getHash().getBytes(), spentBy.getHeight(), spentBy.getDate(), spentBy.getFee(), truncate(spentBy.getLabel()), spentBy.getIndex(), spentBy.getValue(),
spentBy.getStatus() == null ? null : spentBy.getStatus().ordinal(), null, addressNode.getId(), spentBy.getId()); spentBy.getStatus() == null ? null : spentBy.getStatus().ordinal(), null, addressNode.getId(), spentBy.getId());
spentById = spentBy.getId(); spentById = spentBy.getId();
} }
} }
if(txo.getId() == null) { if(txo.getId() == null) {
long txoId = insertBlockTransactionHashIndex(txo.getHash().getBytes(), txo.getHeight(), txo.getDate(), txo.getFee(), txo.getLabel(), txo.getIndex(), txo.getValue(), long txoId = insertBlockTransactionHashIndex(txo.getHash().getBytes(), txo.getHeight(), txo.getDate(), txo.getFee(), truncate(txo.getLabel()), txo.getIndex(), txo.getValue(),
txo.getStatus() == null ? null : txo.getStatus().ordinal(), spentById, addressNode.getId()); txo.getStatus() == null ? null : txo.getStatus().ordinal(), spentById, addressNode.getId());
txo.setId(txoId); txo.setId(txoId);
} else { } else {
updateBlockTransactionHashIndex(txo.getHash().getBytes(), txo.getHeight(), txo.getDate(), txo.getFee(), txo.getLabel(), txo.getIndex(), txo.getValue(), updateBlockTransactionHashIndex(txo.getHash().getBytes(), txo.getHeight(), txo.getDate(), txo.getFee(), truncate(txo.getLabel()), txo.getIndex(), txo.getValue(),
txo.getStatus() == null ? null : txo.getStatus().ordinal(), spentById, addressNode.getId(), txo.getId()); txo.getStatus() == null ? null : txo.getStatus().ordinal(), spentById, addressNode.getId(), txo.getId());
} }
} }
@ -113,4 +113,8 @@ public interface WalletNodeDao {
clearSpentHistory(wallet.getId()); clearSpentHistory(wallet.getId());
clearHistory(wallet.getId()); clearHistory(wallet.getId());
} }
default String truncate(String label) {
return (label != null && label.length() > 255 ? label.substring(0, 255) : label);
}
} }