Skip to content

Commit

Permalink
feat: Add intersactFound and intersactNotFound methods to BlockChainD…
Browse files Browse the repository at this point in the history
…ataListener
  • Loading branch information
satran004 committed Sep 8, 2023
1 parent 44454da commit 0b332f8
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@
import com.bloxbean.cardano.yaci.core.model.BlockHeader;
import com.bloxbean.cardano.yaci.core.model.Era;
import com.bloxbean.cardano.yaci.core.protocol.chainsync.messages.Point;
import com.bloxbean.cardano.yaci.core.protocol.chainsync.messages.Tip;
import com.bloxbean.cardano.yaci.helper.listener.BlockChainDataListener;
import com.bloxbean.cardano.yaci.helper.model.Transaction;
import org.junit.jupiter.api.Test;

import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicLong;

Expand Down Expand Up @@ -60,7 +62,7 @@ public void onBlock(Block block) {
countDownLatch.countDown();
}

@Override
@Override
public void onTransactions(Era era, BlockHeader blockHeader, List<Transaction> transactions) {
System.out.println("# of transactions >> " + transactions.size());
noOfTxs.set(transactions.size());
Expand All @@ -72,4 +74,44 @@ public void onTransactions(Era era, BlockHeader blockHeader, List<Transaction> t
assertThat(blockNo.get()).isEqualTo(292458);
assertThat(noOfTxs.get()).isGreaterThanOrEqualTo(1);
}

@Test
void intersactNotFound() throws InterruptedException {
BlockSync blockSync = new BlockSync(node, nodePort, protocolMagic, Constants.WELL_KNOWN_PREPROD_POINT);

AtomicBoolean success = new AtomicBoolean(false);
CountDownLatch countDownLatch = new CountDownLatch(1);
blockSync.startSync(new Point(38474115, "784e0c913ce6378208f4fc1abf2ce74e817048306247e0ecffa6dac676ce8c65"),
new BlockChainDataListener() {
@Override
public void intersactNotFound(Tip tip) {
System.out.println(">>> Intersection not found");
success.set(true);
countDownLatch.countDown();
}
});

countDownLatch.await(60, TimeUnit.SECONDS);
assertThat(success.get()).isTrue();
}

@Test
void intersactFound() throws InterruptedException {
BlockSync blockSync = new BlockSync(node, nodePort, protocolMagic, Constants.WELL_KNOWN_PREPROD_POINT);

AtomicBoolean success = new AtomicBoolean(false);
CountDownLatch countDownLatch = new CountDownLatch(1);
blockSync.startSync(new Point(38481569, "cf884b8e61126190f6e59d71ad53c561f620cf65ed09aff468149b32b537a804"),
new BlockChainDataListener() {
@Override
public void intersactFound(Tip tip, Point point) {
System.out.println("Intersection found");
success.set(true);
countDownLatch.countDown();
}
});

countDownLatch.await(60, TimeUnit.SECONDS);
assertThat(success.get()).isTrue();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.bloxbean.cardano.yaci.core.model.byron.ByronEbBlock;
import com.bloxbean.cardano.yaci.core.model.byron.ByronMainBlock;
import com.bloxbean.cardano.yaci.core.protocol.chainsync.messages.Point;
import com.bloxbean.cardano.yaci.core.protocol.chainsync.messages.Tip;
import com.bloxbean.cardano.yaci.helper.model.Transaction;

import java.util.List;
Expand All @@ -20,4 +21,17 @@ default void onTransactions(Era era, BlockHeader blockHeader, List<Transaction>

default void batchDone() {}
default void noBlockFound(Point from, Point to) {}

/**
* Called when an intersection is found. This is available only for {@link com.bloxbean.cardano.yaci.helper.BlockSync} (Block sync protocol)
* @param tip
* @param point
*/
default void intersactFound(Tip tip, Point point) {}

/**
* Called when an intersection is not found. This is available only for {@link com.bloxbean.cardano.yaci.helper.BlockSync} (Block sync protocol)
* @param tip
*/
default void intersactNotFound(Tip tip) {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ public ChainSyncListenerAdapter(BlockChainDataListener blockChainDataListener) {
}

public void intersactFound(Tip tip, Point point) {

blockChainDataListener.intersactFound(tip, point);
}

public void intersactNotFound(Tip tip) {

blockChainDataListener.intersactNotFound(tip);
}

public void rollforward(Tip tip, BlockHeader blockHeader) {
Expand Down

0 comments on commit 0b332f8

Please sign in to comment.