Skip to content

Commit

Permalink
Fixing receipts inbound problems occuered when nodes not connected
Browse files Browse the repository at this point in the history
  • Loading branch information
PanagiotisDrakatos committed Oct 24, 2023
1 parent ec74cfb commit c5b4903
Show file tree
Hide file tree
Showing 5 changed files with 108 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public void onEvent(AbstractBlockEvent blockEvent, long l, boolean b) throws Int
Map<Receipt.ReceiptBlock, List<Receipt>> zone_1 = inner_receipts.get(keyset.toArray()[0]);
ServiceSubmit(zone_1);
} catch (Exception e) {
// e.printStackTrace();
e.printStackTrace();
} finally {
latch.countDown();
}
Expand Down Expand Up @@ -120,52 +120,71 @@ public void ServiceSubmit(Map<Receipt.ReceiptBlock, List<Receipt>> zone) {

//find validator position in structure map
Integer my_pos = blockIndex.getPublicKeyIndex(CachedZoneIndex.getInstance().getZoneIndex(), CachedBLSKeyPair.getInstance().getPublicKey());
// get first zone index from inner receipts and search in foor loop in which zone index of structure map belongs
Integer receiptZoneIndex = inner_receipts.keySet().stream().findFirst().get();
String IP = "";
for (Integer BlockZoneIndex : committeeBlock.getStructureMap().keySet()) {
if (BlockZoneIndex == receiptZoneIndex) {

//Fill in the list with auto increment positions of Linkdhasamp ip where zone index belong
List<Integer> searchable_list = IntStream
.range(0, 0 + committeeBlock.getStructureMap().get(BlockZoneIndex).size())
.boxed().collect(Collectors.toList());

// Find the closest value of ip in order to get this ip from linkdnHashmap
//and look for value
int target = searchable_list.stream()
.min(Comparator.comparingInt(i -> Math.abs(i - my_pos)))
.orElseThrow(() -> new NoSuchElementException("No value present"));
IP = blockIndex.getIpValue(BlockZoneIndex, blockIndex.getPublicKeyByIndex(BlockZoneIndex, target));
break;
boolean bError = false;
Integer receiptZoneIndex=0;
List<TransactionBlock> current=null;
do {
try {
// get first zone index from inner receipts and search in foor loop in which zone index of structure map belongs
receiptZoneIndex = inner_receipts.keySet().stream().findFirst().get();
String IP = "";
for (Integer BlockZoneIndex : committeeBlock.getStructureMap().keySet()) {
if (BlockZoneIndex == receiptZoneIndex) {

//Fill in the list with auto increment positions of Linkdhasamp ip where zone index belong
List<Integer> searchable_list = IntStream
.range(0, 0 + committeeBlock.getStructureMap().get(BlockZoneIndex).size())
.boxed().collect(Collectors.toList());

// Find the closest value of ip in order to get this ip from linkdnHashmap
//and look for value
Integer finalMy_pos = my_pos;
int target = searchable_list.stream()
.min(Comparator.comparingInt(i -> Math.abs(i - finalMy_pos)))
.orElseThrow(() -> new NoSuchElementException("No value present"));
IP = blockIndex.getIpValue(BlockZoneIndex, blockIndex.getPublicKeyByIndex(BlockZoneIndex, target));
break;
}
}
if (IP.equals("")) {
LOG.info("Cross zone Verification failed not valid IP");
transactionBlock.setStatustype(StatusType.ABORT);
return;
}
ArrayList<String> to_search = new ArrayList<>();
for (Receipt.ReceiptBlock receiptBlock : zone.keySet()) {
to_search.add(String.valueOf(receiptBlock.getHeight()));
}
RpcAdrestusClient<TransactionBlock> client = new RpcAdrestusClient<TransactionBlock>(new TransactionBlock(), IP, ZoneDatabaseFactory.getDatabaseRPCPort(blockIndex.getZone(IP)),400, CachedEventLoop.getInstance().getEventloop());
client.connect();

current = client.getBlock(to_search);
bError = false;
} catch (IllegalArgumentException e) {
bError=true;
if(my_pos==committeeBlock.getStructureMap().get(receiptZoneIndex).size()-1){
my_pos=0;
}
else {
my_pos=my_pos+1;
}
}
}
if (IP.equals("")) {
LOG.info("Cross zone Verification failed not valid IP");
transactionBlock.setStatustype(StatusType.ABORT);
return;
}
ArrayList<String> to_search = new ArrayList<>();
for (Receipt.ReceiptBlock receiptBlock : zone.keySet()) {
to_search.add(String.valueOf(receiptBlock.getHeight()));
}
RpcAdrestusClient<TransactionBlock> client = new RpcAdrestusClient<TransactionBlock>(new TransactionBlock(), IP, ZoneDatabaseFactory.getDatabaseRPCPort(blockIndex.getZone(IP)), CachedEventLoop.getInstance().getEventloop());
client.connect();
List<TransactionBlock> current = client.getBlock(to_search);
} while (bError);
int position = -1;
for (Map.Entry<Receipt.ReceiptBlock, List<Receipt>> entry : zone.entrySet()) {
position++;
int finalPosition = position;
List<TransactionBlock> finalCurrent = current;
entry.getValue().stream().forEach(receipt -> {
int index = Collections.binarySearch(current.get(finalPosition).getTransactionList(), receipt.getTransaction());
int index = Collections.binarySearch(finalCurrent.get(finalPosition).getTransactionList(), receipt.getTransaction());
if (index < 0) {
LOG.info("Cannot find transaction in Transaction Block");
transactionBlock.setStatustype(StatusType.ABORT);
return;
}
Transaction transaction = current.get(finalPosition).getTransactionList().get(index);
boolean check = PreConditionsChecks(receipt, entry.getKey(), current.get(finalPosition), transaction, index);
boolean cross_check = CrossZoneConditionsChecks(current.get(finalPosition), entry.getKey());
Transaction transaction = finalCurrent.get(finalPosition).getTransactionList().get(index);
boolean check = PreConditionsChecks(receipt, entry.getKey(), finalCurrent.get(finalPosition), transaction, index);
boolean cross_check = CrossZoneConditionsChecks(finalCurrent.get(finalPosition), entry.getKey());
if (!check || !cross_check)
atomicInteger.decrementAndGet();
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -204,24 +204,27 @@ public void myCdownload2() throws Exception {
try {
IDatabase<String, TransactionBlock> database = new DatabaseFactory(String.class, TransactionBlock.class).getDatabase(DatabaseType.ROCKS_DB, DatabaseInstance.ZONE_1_TRANSACTION_BLOCK);
TransactionBlock transactionBlock = new TransactionBlock();
TransactionBlock transactionBlock2 = new TransactionBlock();
String hash = HashUtil.sha256_bytetoString(encode.encode(transactionBlock));
transactionBlock.setHash(hash);
transactionBlock2.setHash("2");

database.save(transactionBlock.getHash(), transactionBlock);

database.save(transactionBlock2.getHash(), transactionBlock2);

RpcAdrestusServer<AbstractBlock> example = new RpcAdrestusServer<AbstractBlock>(new TransactionBlock(), DatabaseInstance.ZONE_1_TRANSACTION_BLOCK, "localhost", 8085, eventloop);
new Thread(example).start();
RpcAdrestusClient<AbstractBlock> client = new RpcAdrestusClient<AbstractBlock>(new TransactionBlock(), "localhost", 8085, eventloop);
client.connect();
ArrayList<String> list = new ArrayList<>();
list.add(transactionBlock.getHash());
list.add("2");
List<AbstractBlock> blocks = client.getBlock(list);
if (blocks.isEmpty()) {
System.out.println("error");
}
assertEquals(transactionBlock, blocks.get(0));

assertEquals(transactionBlock2, blocks.get(1));

client.close();
example.close();
Expand Down
31 changes: 25 additions & 6 deletions adrestus-core/src/test/java/io/Adrestus/core/ReceiptsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,14 @@ public class ReceiptsTest {
private static TransactionBlock transactionBlock;
private static BLSPrivateKey sk2;
private static BLSPublicKey vk2;
private static BLSPrivateKey sk4;
private static BLSPublicKey vk4;

private static BLSPrivateKey sk5;
private static BLSPublicKey vk5;

private static BLSPrivateKey sk6;
private static BLSPublicKey vk6;
private static SerializationUtil<AbstractBlock> serenc;
private static ArrayList<MerkleNode> merkleNodeArrayList;
private static MerkleTreeImp tree;
Expand All @@ -71,9 +79,18 @@ public static void setup() throws Exception {
sk2 = new BLSPrivateKey(2);
vk2 = new BLSPublicKey(sk2);

sk3 = new BLSPrivateKey(2);
sk3 = new BLSPrivateKey(3);
vk3 = new BLSPublicKey(sk3);

sk4 = new BLSPrivateKey(4);
vk4 = new BLSPublicKey(sk4);

sk5 = new BLSPrivateKey(5);
vk5 = new BLSPublicKey(sk5);

sk6= new BLSPrivateKey(6);
vk6 = new BLSPublicKey(sk6);

List<SerializationUtil.Mapping> list = new ArrayList<>();
list.add(new SerializationUtil.Mapping(ECP.class, ctx -> new ECPmapper()));
list.add(new SerializationUtil.Mapping(ECP2.class, ctx -> new ECP2mapper()));
Expand Down Expand Up @@ -158,10 +175,12 @@ public static void setup() throws Exception {

CommitteeBlock committeeBlock = new CommitteeBlock();
committeeBlock.getHeaderData().setTimestamp("2022-11-18 15:01:29.304");
committeeBlock.getStructureMap().get(0).put(vk1, "192.168.1.106");
committeeBlock.getStructureMap().get(0).put(vk3, "192.168.1.112");
committeeBlock.getStructureMap().get(0).put(vk1, IPFinder.getLocal_address());
committeeBlock.getStructureMap().get(0).put(vk2, "192.168.1.112");
committeeBlock.getStructureMap().get(0).put(vk3, "192.168.1.114");
committeeBlock.getStructureMap().get(1).put(vk2, "192.168.1.116");
committeeBlock.getStructureMap().get(1).put(vk4, "192.168.1.116");
committeeBlock.getStructureMap().get(1).put(vk5, "192.168.1.117");
committeeBlock.getStructureMap().get(1).put(vk6, "192.168.1.118");
CachedLatestBlocks.getInstance().setCommitteeBlock(committeeBlock);
CachedZoneIndex.getInstance().setZoneIndexInternalIP();
tree = new MerkleTreeImp();
Expand Down Expand Up @@ -280,8 +299,8 @@ public void inbound_test() throws Exception {

BlockEventPublisher publisher = new BlockEventPublisher(1024);
CachedZoneIndex.getInstance().setZoneIndex(1);
CachedBLSKeyPair.getInstance().setPublicKey(vk1);
CachedBLSKeyPair.getInstance().setPrivateKey(sk1);
CachedBLSKeyPair.getInstance().setPublicKey(vk5);
CachedBLSKeyPair.getInstance().setPrivateKey(sk5);
String OriginalRootHash = transactionBlock.getMerkleRoot();
Receipt.ReceiptBlock receiptBlock = new Receipt.ReceiptBlock(transactionBlock.getHash(), transactionBlock.getHeight(), transactionBlock.getGeneration(), transactionBlock.getMerkleRoot());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
public class RpcAdrestusClient<T> {
private static Logger LOG = LoggerFactory.getLogger(RpcAdrestusClient.class);

private static final int TIMEOUT = 4000;
private int TIMEOUT = 4000;

private SerializationUtil<ListBlockResponse> serializationUtil;
private SerializationUtil<PatriciaTreeResponse> serializationUtil2;
Expand Down Expand Up @@ -94,6 +94,24 @@ public RpcAdrestusClient(T typeParameterClass, String host, int port, Eventloop
this.valueMapper = new SerializationUtil(typeParameterClass.getClass(), list, true);
this.valueMapper2 = new SerializationUtil(typeParameterClass.getClass(), list, true);
}
public RpcAdrestusClient(T typeParameterClass, String host, int port,int timeout, Eventloop eventloop) {
this.rpc_serialize = SerializerBuilder.create();
this.typeParameterClass = typeParameterClass;
this.host = host;
this.port = port;
this.eventloop = eventloop;
this.TIMEOUT=timeout;
List<SerializationUtil.Mapping> list = new ArrayList<>();
list.add(new SerializationUtil.Mapping(ECP.class, ctx -> new ECPmapper()));
list.add(new SerializationUtil.Mapping(ECP2.class, ctx -> new ECP2mapper()));
list.add(new SerializationUtil.Mapping(BigInteger.class, ctx -> new BigIntegerSerializer()));
list.add(new SerializationUtil.Mapping(TreeMap.class, ctx -> new CustomSerializerTreeMap()));
list.add(new SerializationUtil.Mapping(MemoryTreePool.class, ctx -> new MemoryTreePoolSerializer()));
this.serializationUtil2 = new SerializationUtil<PatriciaTreeResponse>(PatriciaTreeResponse.class, list);
this.serializationUtil = new SerializationUtil<ListBlockResponse>(ListBlockResponse.class, list);
this.valueMapper = new SerializationUtil(typeParameterClass.getClass(), list, true);
this.valueMapper2 = new SerializationUtil(typeParameterClass.getClass(), list, true);
}

public RpcAdrestusClient(T typeParameterClass, List<InetSocketAddress> inetSocketAddresses) {
this.rpc_serialize = SerializerBuilder.create();
Expand Down
4 changes: 4 additions & 0 deletions jitpack.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
jdk:
- openjdk11
install:
- mvn install -DskipTests

0 comments on commit c5b4903

Please sign in to comment.