Skip to content

Commit

Permalink
Fix 213 : Move Invalid Transactions to transaction store. (#214)
Browse files Browse the repository at this point in the history
* chore: #213 Move InvalidTransaction to transaction store

* chore: #213 Bump yaci version
  • Loading branch information
satran004 authored Mar 7, 2024
1 parent 1729ed7 commit 179ded9
Show file tree
Hide file tree
Showing 28 changed files with 150 additions and 147 deletions.
2 changes: 1 addition & 1 deletion gradle/libs.versions.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[libraries]
yaci = "com.bloxbean.cardano:yaci:0.3.0-beta12-627cb58-SNAPSHOT"
yaci = "com.bloxbean.cardano:yaci:0.3.0-beta12"
cardano-client-lib = "com.bloxbean.cardano:cardano-client-lib:0.5.1"
cardano-client-backend = "com.bloxbean.cardano:cardano-client-backend:0.5.1"
cardano-client-backend-ogmios = "com.bloxbean.cardano:cardano-client-backend-ogmios:0.5.1"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.bloxbean.cardano.yaci.store.transaction.storage.*;
import com.bloxbean.cardano.yaci.store.transaction.storage.impl.*;
import com.bloxbean.cardano.yaci.store.transaction.storage.impl.mapper.TxnMapper;
import com.bloxbean.cardano.yaci.store.transaction.storage.impl.repository.InvalidTransactionRepository;
import com.bloxbean.cardano.yaci.store.transaction.storage.impl.repository.TxnEntityRepository;
import com.bloxbean.cardano.yaci.store.transaction.storage.impl.repository.TxnWitnessRepository;
import com.bloxbean.cardano.yaci.store.transaction.storage.impl.repository.WithdrawalRepository;
Expand Down Expand Up @@ -68,4 +69,11 @@ public WithdrawalStorage withdrawalStorage(WithdrawalRepository withdrawalReposi
public WithdrawalStorageReader withdrawalStorageReader(WithdrawalRepository withdrawalRepository, TxnMapper mapper) {
return new WithdrawalStorageReaderImpl(withdrawalRepository, mapper);
}

@Bean
@ConditionalOnMissingBean
public InvalidTransactionStorage invalidTransactionStorage(InvalidTransactionRepository invalidTransactionRepository,
TxnMapper txnMapper) {
return new InvalidTransactionStorageImpl(invalidTransactionRepository, txnMapper);
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.bloxbean.cardano.yaci.store.utxo.domain;
package com.bloxbean.cardano.yaci.store.transaction.domain;

import com.bloxbean.cardano.yaci.helper.model.Transaction;
import com.fasterxml.jackson.databind.PropertyNamingStrategies;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,13 @@
import com.bloxbean.cardano.yaci.store.common.domain.Amt;
import com.bloxbean.cardano.yaci.store.common.domain.TxOuput;
import com.bloxbean.cardano.yaci.store.common.domain.UtxoKey;
import com.bloxbean.cardano.yaci.store.events.EventMetadata;
import com.bloxbean.cardano.yaci.store.events.TransactionEvent;
import com.bloxbean.cardano.yaci.store.transaction.domain.InvalidTransaction;
import com.bloxbean.cardano.yaci.store.transaction.domain.TxWitnessType;
import com.bloxbean.cardano.yaci.store.transaction.domain.Txn;
import com.bloxbean.cardano.yaci.store.transaction.domain.TxnWitness;
import com.bloxbean.cardano.yaci.store.transaction.storage.InvalidTransactionStorage;
import com.bloxbean.cardano.yaci.store.transaction.storage.TransactionStorage;
import com.bloxbean.cardano.yaci.store.transaction.storage.TransactionWitnessStorage;
import com.fasterxml.jackson.databind.ObjectMapper;
Expand All @@ -39,6 +42,7 @@ public class TransactionProcessor {

private final TransactionStorage transactionStorage;
private final TransactionWitnessStorage transactionWitnessStorage;
private final InvalidTransactionStorage invalidTransactionStorage;
private final ObjectMapper objectMapper;

@EventListener
Expand Down Expand Up @@ -98,13 +102,27 @@ public void handleTransactionEvent(TransactionEvent event) {
.build();

txList.add(txn);

if (transaction.isInvalid())
saveInvalidTransaction(event.getMetadata(), transaction);
});

if (txList.size() > 0) {
transactionStorage.saveAll(txList);
}
}

private void saveInvalidTransaction(EventMetadata metadata, Transaction transaction) {
//insert invalid transactions
InvalidTransaction invalidTransaction = InvalidTransaction.builder()
.txHash(transaction.getTxHash())
.slot(metadata.getSlot())
.blockHash(metadata.getBlockHash())
.transaction(transaction)
.build();
invalidTransactionStorage.save(invalidTransaction);
}


@EventListener
@Transactional
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.bloxbean.cardano.yaci.store.transaction.processor;

import com.bloxbean.cardano.yaci.store.events.RollbackEvent;
import com.bloxbean.cardano.yaci.store.transaction.storage.InvalidTransactionStorage;
import com.bloxbean.cardano.yaci.store.transaction.storage.TransactionStorage;
import com.bloxbean.cardano.yaci.store.transaction.storage.TransactionWitnessStorage;
import lombok.RequiredArgsConstructor;
Expand All @@ -16,6 +17,7 @@
public class TransactionRollbackProcessor {
private final TransactionStorage transactionStorage;
private final TransactionWitnessStorage transactionWitnessStorage;
private final InvalidTransactionStorage invalidTransactionStorage;

@EventListener
@Transactional
Expand All @@ -25,6 +27,9 @@ public void handleRollbackEvent(RollbackEvent rollbackEvent) {

count = transactionWitnessStorage.deleteBySlotGreaterThan(rollbackEvent.getRollbackTo().getSlot());
log.info("Rollback -- {} transaction_witness records", count);

count = invalidTransactionStorage.deleteBySlotGreaterThan(rollbackEvent.getRollbackTo().getSlot());
log.info("Rollback -- {} invalid_transaction records", count);
}

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.bloxbean.cardano.yaci.store.utxo.storage;
package com.bloxbean.cardano.yaci.store.transaction.storage;

import com.bloxbean.cardano.yaci.store.utxo.domain.InvalidTransaction;
import com.bloxbean.cardano.yaci.store.transaction.domain.InvalidTransaction;

public interface InvalidTransactionStorage {
InvalidTransaction save(InvalidTransaction invalidTransaction);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
package com.bloxbean.cardano.yaci.store.utxo.storage.impl;
package com.bloxbean.cardano.yaci.store.transaction.storage.impl;

import com.bloxbean.cardano.yaci.store.utxo.domain.InvalidTransaction;
import com.bloxbean.cardano.yaci.store.utxo.storage.InvalidTransactionStorage;
import com.bloxbean.cardano.yaci.store.utxo.storage.impl.mapper.UtxoMapper;
import com.bloxbean.cardano.yaci.store.utxo.storage.impl.model.InvalidTransactionEntity;
import com.bloxbean.cardano.yaci.store.utxo.storage.impl.repository.InvalidTransactionRepository;
import com.bloxbean.cardano.yaci.store.transaction.domain.InvalidTransaction;
import com.bloxbean.cardano.yaci.store.transaction.storage.InvalidTransactionStorage;
import com.bloxbean.cardano.yaci.store.transaction.storage.impl.mapper.TxnMapper;
import com.bloxbean.cardano.yaci.store.transaction.storage.impl.model.InvalidTransactionEntity;
import com.bloxbean.cardano.yaci.store.transaction.storage.impl.repository.InvalidTransactionRepository;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

@RequiredArgsConstructor
@Slf4j
public class InvalidTransactionStorageImpl implements InvalidTransactionStorage {
private final InvalidTransactionRepository repository;
private final UtxoMapper mapper = UtxoMapper.INSTANCE;
private final TxnMapper mapper;

@Override
public InvalidTransaction save(InvalidTransaction invalidTransaction) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package com.bloxbean.cardano.yaci.store.transaction.storage.impl.mapper;

import com.bloxbean.cardano.yaci.store.transaction.domain.InvalidTransaction;
import com.bloxbean.cardano.yaci.store.transaction.domain.Txn;
import com.bloxbean.cardano.yaci.store.transaction.domain.TxnWitness;
import com.bloxbean.cardano.yaci.store.transaction.domain.Withdrawal;
import com.bloxbean.cardano.yaci.store.transaction.storage.impl.model.InvalidTransactionEntity;
import com.bloxbean.cardano.yaci.store.transaction.storage.impl.model.TxnEntity;
import com.bloxbean.cardano.yaci.store.transaction.storage.impl.model.TxnWitnessEntity;
import com.bloxbean.cardano.yaci.store.transaction.storage.impl.model.WithdrawalEntity;
Expand All @@ -18,4 +20,7 @@ public abstract class TxnMapper {

public abstract Withdrawal toWithdrawal(WithdrawalEntity withdrawalEntity);
public abstract WithdrawalEntity toWithdrawalEntity(Withdrawal withdrawal);

public abstract InvalidTransactionEntity toInvalidTransactionEntity(InvalidTransaction invalidTransaction);
public abstract InvalidTransaction toInvalidTransaction(InvalidTransactionEntity invalidTransactionEntity);
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.bloxbean.cardano.yaci.store.utxo.storage.impl.model;
package com.bloxbean.cardano.yaci.store.transaction.storage.impl.model;

import com.bloxbean.cardano.yaci.helper.model.Transaction;
import com.bloxbean.cardano.yaci.store.common.model.BaseEntity;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.bloxbean.cardano.yaci.store.utxo.storage.impl.repository;
package com.bloxbean.cardano.yaci.store.transaction.storage.impl.repository;

import com.bloxbean.cardano.yaci.store.utxo.storage.impl.model.InvalidTransactionEntity;
import com.bloxbean.cardano.yaci.store.transaction.storage.impl.model.InvalidTransactionEntity;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,19 @@ create table withdrawal

CREATE INDEX idx_withdrawal_slot
ON withdrawal(slot);

-- invalid_transaction
drop table if exists invalid_transaction;
create table invalid_transaction
(
tx_hash varchar(64) not null
primary key,
slot bigint not null,
block_hash varchar(64),
transaction json null,
create_datetime timestamp,
update_datetime timestamp
);

CREATE INDEX idx_invalid_transaction_slot
ON invalid_transaction(slot);
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,19 @@ create table withdrawal

CREATE INDEX idx_withdrawal_slot
ON withdrawal(slot);

-- invalid_transaction
drop table if exists invalid_transaction;
create table invalid_transaction
(
tx_hash varchar(64) not null
primary key,
slot bigint not null,
block_hash varchar(64),
transaction json null,
create_datetime timestamp,
update_datetime timestamp
);

CREATE INDEX idx_invalid_transaction_slot
ON invalid_transaction(slot);
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,19 @@ create table withdrawal

CREATE INDEX idx_withdrawal_slot
ON withdrawal(slot);

-- invalid_transaction
drop table if exists invalid_transaction;
create table invalid_transaction
(
tx_hash varchar(64) not null
primary key,
slot bigint not null,
block_hash varchar(64),
transaction jsonb,
create_datetime timestamp,
update_datetime timestamp
);

CREATE INDEX idx_invalid_transaction_slot
ON invalid_transaction(slot);
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@
import com.bloxbean.cardano.yaci.store.common.domain.UtxoKey;
import com.bloxbean.cardano.yaci.store.events.EventMetadata;
import com.bloxbean.cardano.yaci.store.events.TransactionEvent;
import com.bloxbean.cardano.yaci.store.transaction.domain.InvalidTransaction;
import com.bloxbean.cardano.yaci.store.transaction.domain.TxWitnessType;
import com.bloxbean.cardano.yaci.store.transaction.domain.Txn;
import com.bloxbean.cardano.yaci.store.transaction.domain.TxnWitness;
import com.bloxbean.cardano.yaci.store.transaction.storage.InvalidTransactionStorage;
import com.bloxbean.cardano.yaci.store.transaction.storage.TransactionStorage;
import com.bloxbean.cardano.yaci.store.transaction.storage.TransactionWitnessStorage;
import com.fasterxml.jackson.databind.ObjectMapper;
Expand Down Expand Up @@ -37,15 +39,20 @@ class TransactionProcessorTest {
private TransactionStorage transactionStorage;
@Mock
private TransactionWitnessStorage transactionWitnessStorage;
@Mock
private InvalidTransactionStorage invalidTransactionStorage;

private TransactionProcessor transactionProcessor;
@Captor
private ArgumentCaptor<List<Txn>> txnListCaptor;
@Captor
private ArgumentCaptor<List<TxnWitness>> txnWitnessesCaptor;
@Captor
private ArgumentCaptor<InvalidTransaction> invalidTxCaptor;

@BeforeEach
public void setup() {
transactionProcessor = new TransactionProcessor(transactionStorage, transactionWitnessStorage, new ObjectMapper());
transactionProcessor = new TransactionProcessor(transactionStorage, transactionWitnessStorage, invalidTransactionStorage, new ObjectMapper());
}

@Test
Expand All @@ -58,6 +65,7 @@ void givenTransactionEvent_shouldSaveTxnList() {
transactionProcessor.handleTransactionEvent(transactionEvent);

verify(transactionStorage, Mockito.times(1)).saveAll(txnListCaptor.capture());
verify(invalidTransactionStorage, Mockito.never()).save(invalidTxCaptor.capture());
List<Txn> txnList = txnListCaptor.getValue();

assertThat(txnList).hasSize(1);
Expand Down Expand Up @@ -158,6 +166,22 @@ void givenTransactionEvent_shouldSaveTxnWitnessList() {
}
}

@Test
void givenTransactionEvent_withInvalidTransaction_shouldSaveInvalidTransaction() {
var transactions = transactions();
transactions.get(0).setInvalid(true);

TransactionEvent transactionEvent = TransactionEvent.builder()
.transactions(transactions)
.metadata(eventMetadata())
.build();

transactionProcessor.handleTransactionEvent(transactionEvent);

verify(transactionStorage, Mockito.times(1)).saveAll(txnListCaptor.capture());
verify(invalidTransactionStorage, Mockito.times(1)).save(invalidTxCaptor.capture());
}

private EventMetadata eventMetadata() {
return EventMetadata.builder()
.era(Era.Babbage)
Expand Down Expand Up @@ -215,7 +239,7 @@ private List<Transaction> transactions() {
.build()
)
.utxos(utxos(txHash, transactionOutputs()))
.invalid(true)
.invalid(false)
.build();

return List.of(transaction);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.bloxbean.cardano.yaci.core.protocol.chainsync.messages.Point;
import com.bloxbean.cardano.yaci.store.events.RollbackEvent;
import com.bloxbean.cardano.yaci.store.transaction.storage.impl.repository.InvalidTransactionRepository;
import com.bloxbean.cardano.yaci.store.transaction.storage.impl.repository.TxnEntityRepository;
import com.bloxbean.cardano.yaci.store.transaction.storage.impl.repository.TxnWitnessRepository;
import org.junit.jupiter.api.Test;
Expand All @@ -22,6 +23,9 @@ class TransactionRollbackProcessorIT {
@Autowired
private TxnWitnessRepository txnWitnessRepository;

@Autowired
private InvalidTransactionRepository invalidTransactionRepository;

@Autowired
private TransactionRollbackProcessor transactionRollbackProcessor;

Expand All @@ -43,4 +47,20 @@ void handleRollBackEvent() {
assertThat(countEntity).isEqualTo(3);
assertThat(countWitness).isEqualTo(9);
}

@Test
@SqlGroup({
@Sql(value = "classpath:scripts/invalid_transaction_data.sql", executionPhase = BEFORE_TEST_METHOD)
})
void givenRollbackEvent_shouldDeleteInvalidTransactions() throws Exception {
RollbackEvent rollbackEvent = RollbackEvent.builder()
.rollbackTo(new Point(13133973, "96bb7918a219dbe0cb01d3962b78a883931da27b5a4987af7c1bd964d7ffc6ff"))
.currentPoint(new Point(13518703, "5470beb0a38e7793db667269e55ed74b339d35db57e640d8f82de831ee348ba0"))
.build();

transactionRollbackProcessor.handleRollbackEvent(rollbackEvent);

int count = invalidTransactionRepository.findAll().size();
assertThat(count).isEqualTo(12);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.bloxbean.cardano.yaci.core.protocol.chainsync.messages.Point;
import com.bloxbean.cardano.yaci.store.events.RollbackEvent;
import com.bloxbean.cardano.yaci.store.transaction.storage.InvalidTransactionStorage;
import com.bloxbean.cardano.yaci.store.transaction.storage.TransactionStorage;
import com.bloxbean.cardano.yaci.store.transaction.storage.TransactionWitnessStorage;
import org.junit.jupiter.api.Test;
Expand All @@ -20,6 +21,9 @@ class TransactionRollbackProcessorTest {
@Mock
private TransactionWitnessStorage transactionWitnessStorage;

@Mock
private InvalidTransactionStorage invalidTransactionStorage;

@InjectMocks
private TransactionRollbackProcessor transactionRollbackProcessor;

Expand All @@ -34,5 +38,6 @@ void testHandleRollbackProcessor() {

Mockito.verify(transactionStorage, Mockito.times(1)).deleteBySlotGreaterThan(rollbackEvent.getRollbackTo().getSlot());
Mockito.verify(transactionWitnessStorage, Mockito.times(1)).deleteBySlotGreaterThan(rollbackEvent.getRollbackTo().getSlot());
Mockito.verify(invalidTransactionStorage, Mockito.times(1)).deleteBySlotGreaterThan(rollbackEvent.getRollbackTo().getSlot());
}
}
Loading

0 comments on commit 179ded9

Please sign in to comment.