Skip to content

Commit

Permalink
BlockchainDataListener - Merge onBlock and onTransaction callbacks (#24)
Browse files Browse the repository at this point in the history
* feat!: Merged onBlock() and onTransactions() of BlockChainDataListener

* chore: Bump snapshot version
  • Loading branch information
satran004 authored Sep 13, 2023
1 parent ca3b4d2 commit 9647437
Show file tree
Hide file tree
Showing 6 changed files with 14 additions and 23 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
group = com.bloxbean.cardano
artifactId = yaci
version = 0.2.3.1-SNAPSHOT
version = 0.2.3.2-SNAPSHOT
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package com.bloxbean.cardano.yaci.helper;

import com.bloxbean.cardano.yaci.core.model.Block;
import com.bloxbean.cardano.yaci.core.model.Era;
import com.bloxbean.cardano.yaci.core.protocol.chainsync.messages.Point;
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.ArrayList;
Expand All @@ -21,7 +23,7 @@ void fetch() throws InterruptedException {
BlockRangeSync blockRangeSync = new BlockRangeSync(node, nodePort, protocolMagic);
blockRangeSync.start(new BlockChainDataListener() {
@Override
public void onBlock(Block block) {
public void onBlock(Era era, Block block, List<Transaction> transactions) {
System.out.println(block.getHeader().getHeaderBody().getBlockNumber());
blocks.add(block);
countDownLatch.countDown();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import com.bloxbean.cardano.yaci.core.common.Constants;
import com.bloxbean.cardano.yaci.core.model.Block;
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;
Expand All @@ -28,17 +27,14 @@ void syncFromTip() throws InterruptedException {
AtomicLong blockNo = new AtomicLong();
CountDownLatch countDownLatch = new CountDownLatch(1);
blockSync.startSyncFromTip(new BlockChainDataListener() {
@Override
public void onBlock(Block block) {

public void onBlock(Era era, Block block, List<Transaction> transactions) {
System.out.println(block.getHeader().getHeaderBody().getBlockNumber());
blockNo.set(block.getHeader().getHeaderBody().getBlockNumber());
System.out.println("# of transactions >> " + transactions.size());
countDownLatch.countDown();
}

@Override
public void onTransactions(Era era, BlockHeader blockHeader, List<Transaction> transactions) {
System.out.println("# of transactions >> " + transactions.size());
}
});

countDownLatch.await(60, TimeUnit.SECONDS);
Expand All @@ -51,19 +47,14 @@ void syncFromPoint() throws InterruptedException {

AtomicLong blockNo = new AtomicLong();
AtomicInteger noOfTxs = new AtomicInteger();
CountDownLatch countDownLatch = new CountDownLatch(4);
CountDownLatch countDownLatch = new CountDownLatch(2);
blockSync.startSync(new Point(13107195, "ad2ceec67a07069d6e9295ed2144015860602c29f42505dc6ea2f55b9fc0dd93"),
new BlockChainDataListener() {
@Override
public void onBlock(Block block) {
public void onBlock(Era era, Block block, List<Transaction> transactions) {
System.out.println(block.getHeader().getHeaderBody().getBlockNumber());
if (blockNo.get() == 0)
blockNo.set(block.getHeader().getHeaderBody().getBlockNumber());
countDownLatch.countDown();
}

@Override
public void onTransactions(Era era, BlockHeader blockHeader, List<Transaction> transactions) {
System.out.println("# of transactions >> " + transactions.size());
noOfTxs.set(transactions.size());
countDownLatch.countDown();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@
import com.bloxbean.cardano.yaci.core.model.Block;
import com.bloxbean.cardano.yaci.core.model.Era;
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.helper.listener.BlockChainDataListener;
import com.bloxbean.cardano.yaci.helper.model.StartPoint;
import com.bloxbean.cardano.yaci.helper.model.Transaction;

import java.util.List;
import java.util.Optional;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
Expand All @@ -32,7 +33,7 @@ public void onByronEbBlock(ByronEbBlock byronEbBlock) {
}

@Override
public void onBlock(Block block) {
public void onBlock(Era era, Block block, List<Transaction> transactions) {
if (block.getHeader().getHeaderBody().getBlockNumber() == 0) {
startPoint.setFirstBlock(new Point(block.getHeader().getHeaderBody().getSlot(), block.getHeader().getHeaderBody().getBlockHash()));
startPoint.setFirstBlockEra(block.getEra());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.bloxbean.cardano.yaci.helper.listener;

import com.bloxbean.cardano.yaci.core.model.Block;
import com.bloxbean.cardano.yaci.core.model.BlockHeader;
import com.bloxbean.cardano.yaci.core.model.Era;
import com.bloxbean.cardano.yaci.core.model.byron.ByronEbBlock;
import com.bloxbean.cardano.yaci.core.model.byron.ByronMainBlock;
Expand All @@ -15,9 +14,8 @@ public interface BlockChainDataListener {
default void onByronBlock(ByronMainBlock byronBlock) {}
default void onByronEbBlock(ByronEbBlock byronEbBlock) {}

default void onBlock(Block block) {}
default void onBlock(Era era, Block block, List<Transaction> transactions) {}
default void onRollback(Point point) {}
default void onTransactions(Era era, BlockHeader blockHeader, List<Transaction> transactions) {}

default void batchDone() {}
default void noBlockFound(Point from, Point to) {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,7 @@ public void blockFound(Block block) {
}


blockChainDataListener.onBlock(block);
blockChainDataListener.onTransactions(block.getEra(), block.getHeader(), transactionEvents);
blockChainDataListener.onBlock(block.getEra(), block, transactionEvents);
}

private List<Utxo> getUtxosFromOutput(TransactionBody txBody) {
Expand Down

0 comments on commit 9647437

Please sign in to comment.