Skip to content

Commit c5b4903

Browse files
Fixing receipts inbound problems occuered when nodes not connected
1 parent ec74cfb commit c5b4903

File tree

5 files changed

+108
-45
lines changed

5 files changed

+108
-45
lines changed

adrestus-core/src/main/java/io/Adrestus/core/RingBuffer/handler/blocks/InBoundEventHandler.java

Lines changed: 55 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ public void onEvent(AbstractBlockEvent blockEvent, long l, boolean b) throws Int
7272
Map<Receipt.ReceiptBlock, List<Receipt>> zone_1 = inner_receipts.get(keyset.toArray()[0]);
7373
ServiceSubmit(zone_1);
7474
} catch (Exception e) {
75-
// e.printStackTrace();
75+
e.printStackTrace();
7676
} finally {
7777
latch.countDown();
7878
}
@@ -120,52 +120,71 @@ public void ServiceSubmit(Map<Receipt.ReceiptBlock, List<Receipt>> zone) {
120120

121121
//find validator position in structure map
122122
Integer my_pos = blockIndex.getPublicKeyIndex(CachedZoneIndex.getInstance().getZoneIndex(), CachedBLSKeyPair.getInstance().getPublicKey());
123-
// get first zone index from inner receipts and search in foor loop in which zone index of structure map belongs
124-
Integer receiptZoneIndex = inner_receipts.keySet().stream().findFirst().get();
125-
String IP = "";
126-
for (Integer BlockZoneIndex : committeeBlock.getStructureMap().keySet()) {
127-
if (BlockZoneIndex == receiptZoneIndex) {
128-
129-
//Fill in the list with auto increment positions of Linkdhasamp ip where zone index belong
130-
List<Integer> searchable_list = IntStream
131-
.range(0, 0 + committeeBlock.getStructureMap().get(BlockZoneIndex).size())
132-
.boxed().collect(Collectors.toList());
133-
134-
// Find the closest value of ip in order to get this ip from linkdnHashmap
135-
//and look for value
136-
int target = searchable_list.stream()
137-
.min(Comparator.comparingInt(i -> Math.abs(i - my_pos)))
138-
.orElseThrow(() -> new NoSuchElementException("No value present"));
139-
IP = blockIndex.getIpValue(BlockZoneIndex, blockIndex.getPublicKeyByIndex(BlockZoneIndex, target));
140-
break;
123+
boolean bError = false;
124+
Integer receiptZoneIndex=0;
125+
List<TransactionBlock> current=null;
126+
do {
127+
try {
128+
// get first zone index from inner receipts and search in foor loop in which zone index of structure map belongs
129+
receiptZoneIndex = inner_receipts.keySet().stream().findFirst().get();
130+
String IP = "";
131+
for (Integer BlockZoneIndex : committeeBlock.getStructureMap().keySet()) {
132+
if (BlockZoneIndex == receiptZoneIndex) {
133+
134+
//Fill in the list with auto increment positions of Linkdhasamp ip where zone index belong
135+
List<Integer> searchable_list = IntStream
136+
.range(0, 0 + committeeBlock.getStructureMap().get(BlockZoneIndex).size())
137+
.boxed().collect(Collectors.toList());
138+
139+
// Find the closest value of ip in order to get this ip from linkdnHashmap
140+
//and look for value
141+
Integer finalMy_pos = my_pos;
142+
int target = searchable_list.stream()
143+
.min(Comparator.comparingInt(i -> Math.abs(i - finalMy_pos)))
144+
.orElseThrow(() -> new NoSuchElementException("No value present"));
145+
IP = blockIndex.getIpValue(BlockZoneIndex, blockIndex.getPublicKeyByIndex(BlockZoneIndex, target));
146+
break;
147+
}
148+
}
149+
if (IP.equals("")) {
150+
LOG.info("Cross zone Verification failed not valid IP");
151+
transactionBlock.setStatustype(StatusType.ABORT);
152+
return;
153+
}
154+
ArrayList<String> to_search = new ArrayList<>();
155+
for (Receipt.ReceiptBlock receiptBlock : zone.keySet()) {
156+
to_search.add(String.valueOf(receiptBlock.getHeight()));
157+
}
158+
RpcAdrestusClient<TransactionBlock> client = new RpcAdrestusClient<TransactionBlock>(new TransactionBlock(), IP, ZoneDatabaseFactory.getDatabaseRPCPort(blockIndex.getZone(IP)),400, CachedEventLoop.getInstance().getEventloop());
159+
client.connect();
160+
161+
current = client.getBlock(to_search);
162+
bError = false;
163+
} catch (IllegalArgumentException e) {
164+
bError=true;
165+
if(my_pos==committeeBlock.getStructureMap().get(receiptZoneIndex).size()-1){
166+
my_pos=0;
167+
}
168+
else {
169+
my_pos=my_pos+1;
170+
}
141171
}
142-
}
143-
if (IP.equals("")) {
144-
LOG.info("Cross zone Verification failed not valid IP");
145-
transactionBlock.setStatustype(StatusType.ABORT);
146-
return;
147-
}
148-
ArrayList<String> to_search = new ArrayList<>();
149-
for (Receipt.ReceiptBlock receiptBlock : zone.keySet()) {
150-
to_search.add(String.valueOf(receiptBlock.getHeight()));
151-
}
152-
RpcAdrestusClient<TransactionBlock> client = new RpcAdrestusClient<TransactionBlock>(new TransactionBlock(), IP, ZoneDatabaseFactory.getDatabaseRPCPort(blockIndex.getZone(IP)), CachedEventLoop.getInstance().getEventloop());
153-
client.connect();
154-
List<TransactionBlock> current = client.getBlock(to_search);
172+
} while (bError);
155173
int position = -1;
156174
for (Map.Entry<Receipt.ReceiptBlock, List<Receipt>> entry : zone.entrySet()) {
157175
position++;
158176
int finalPosition = position;
177+
List<TransactionBlock> finalCurrent = current;
159178
entry.getValue().stream().forEach(receipt -> {
160-
int index = Collections.binarySearch(current.get(finalPosition).getTransactionList(), receipt.getTransaction());
179+
int index = Collections.binarySearch(finalCurrent.get(finalPosition).getTransactionList(), receipt.getTransaction());
161180
if (index < 0) {
162181
LOG.info("Cannot find transaction in Transaction Block");
163182
transactionBlock.setStatustype(StatusType.ABORT);
164183
return;
165184
}
166-
Transaction transaction = current.get(finalPosition).getTransactionList().get(index);
167-
boolean check = PreConditionsChecks(receipt, entry.getKey(), current.get(finalPosition), transaction, index);
168-
boolean cross_check = CrossZoneConditionsChecks(current.get(finalPosition), entry.getKey());
185+
Transaction transaction = finalCurrent.get(finalPosition).getTransactionList().get(index);
186+
boolean check = PreConditionsChecks(receipt, entry.getKey(), finalCurrent.get(finalPosition), transaction, index);
187+
boolean cross_check = CrossZoneConditionsChecks(finalCurrent.get(finalPosition), entry.getKey());
169188
if (!check || !cross_check)
170189
atomicInteger.decrementAndGet();
171190
});

adrestus-core/src/test/java/io/Adrestus/core/RPCExampleTest.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -204,24 +204,27 @@ public void myCdownload2() throws Exception {
204204
try {
205205
IDatabase<String, TransactionBlock> database = new DatabaseFactory(String.class, TransactionBlock.class).getDatabase(DatabaseType.ROCKS_DB, DatabaseInstance.ZONE_1_TRANSACTION_BLOCK);
206206
TransactionBlock transactionBlock = new TransactionBlock();
207+
TransactionBlock transactionBlock2 = new TransactionBlock();
207208
String hash = HashUtil.sha256_bytetoString(encode.encode(transactionBlock));
208209
transactionBlock.setHash(hash);
210+
transactionBlock2.setHash("2");
209211

210212
database.save(transactionBlock.getHash(), transactionBlock);
211-
213+
database.save(transactionBlock2.getHash(), transactionBlock2);
212214

213215
RpcAdrestusServer<AbstractBlock> example = new RpcAdrestusServer<AbstractBlock>(new TransactionBlock(), DatabaseInstance.ZONE_1_TRANSACTION_BLOCK, "localhost", 8085, eventloop);
214216
new Thread(example).start();
215217
RpcAdrestusClient<AbstractBlock> client = new RpcAdrestusClient<AbstractBlock>(new TransactionBlock(), "localhost", 8085, eventloop);
216218
client.connect();
217219
ArrayList<String> list = new ArrayList<>();
218220
list.add(transactionBlock.getHash());
221+
list.add("2");
219222
List<AbstractBlock> blocks = client.getBlock(list);
220223
if (blocks.isEmpty()) {
221224
System.out.println("error");
222225
}
223226
assertEquals(transactionBlock, blocks.get(0));
224-
227+
assertEquals(transactionBlock2, blocks.get(1));
225228

226229
client.close();
227230
example.close();

adrestus-core/src/test/java/io/Adrestus/core/ReceiptsTest.java

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,14 @@ public class ReceiptsTest {
5858
private static TransactionBlock transactionBlock;
5959
private static BLSPrivateKey sk2;
6060
private static BLSPublicKey vk2;
61+
private static BLSPrivateKey sk4;
62+
private static BLSPublicKey vk4;
63+
64+
private static BLSPrivateKey sk5;
65+
private static BLSPublicKey vk5;
66+
67+
private static BLSPrivateKey sk6;
68+
private static BLSPublicKey vk6;
6169
private static SerializationUtil<AbstractBlock> serenc;
6270
private static ArrayList<MerkleNode> merkleNodeArrayList;
6371
private static MerkleTreeImp tree;
@@ -71,9 +79,18 @@ public static void setup() throws Exception {
7179
sk2 = new BLSPrivateKey(2);
7280
vk2 = new BLSPublicKey(sk2);
7381

74-
sk3 = new BLSPrivateKey(2);
82+
sk3 = new BLSPrivateKey(3);
7583
vk3 = new BLSPublicKey(sk3);
7684

85+
sk4 = new BLSPrivateKey(4);
86+
vk4 = new BLSPublicKey(sk4);
87+
88+
sk5 = new BLSPrivateKey(5);
89+
vk5 = new BLSPublicKey(sk5);
90+
91+
sk6= new BLSPrivateKey(6);
92+
vk6 = new BLSPublicKey(sk6);
93+
7794
List<SerializationUtil.Mapping> list = new ArrayList<>();
7895
list.add(new SerializationUtil.Mapping(ECP.class, ctx -> new ECPmapper()));
7996
list.add(new SerializationUtil.Mapping(ECP2.class, ctx -> new ECP2mapper()));
@@ -158,10 +175,12 @@ public static void setup() throws Exception {
158175

159176
CommitteeBlock committeeBlock = new CommitteeBlock();
160177
committeeBlock.getHeaderData().setTimestamp("2022-11-18 15:01:29.304");
161-
committeeBlock.getStructureMap().get(0).put(vk1, "192.168.1.106");
162-
committeeBlock.getStructureMap().get(0).put(vk3, "192.168.1.112");
178+
committeeBlock.getStructureMap().get(0).put(vk1, IPFinder.getLocal_address());
179+
committeeBlock.getStructureMap().get(0).put(vk2, "192.168.1.112");
163180
committeeBlock.getStructureMap().get(0).put(vk3, "192.168.1.114");
164-
committeeBlock.getStructureMap().get(1).put(vk2, "192.168.1.116");
181+
committeeBlock.getStructureMap().get(1).put(vk4, "192.168.1.116");
182+
committeeBlock.getStructureMap().get(1).put(vk5, "192.168.1.117");
183+
committeeBlock.getStructureMap().get(1).put(vk6, "192.168.1.118");
165184
CachedLatestBlocks.getInstance().setCommitteeBlock(committeeBlock);
166185
CachedZoneIndex.getInstance().setZoneIndexInternalIP();
167186
tree = new MerkleTreeImp();
@@ -280,8 +299,8 @@ public void inbound_test() throws Exception {
280299

281300
BlockEventPublisher publisher = new BlockEventPublisher(1024);
282301
CachedZoneIndex.getInstance().setZoneIndex(1);
283-
CachedBLSKeyPair.getInstance().setPublicKey(vk1);
284-
CachedBLSKeyPair.getInstance().setPrivateKey(sk1);
302+
CachedBLSKeyPair.getInstance().setPublicKey(vk5);
303+
CachedBLSKeyPair.getInstance().setPrivateKey(sk5);
285304
String OriginalRootHash = transactionBlock.getMerkleRoot();
286305
Receipt.ReceiptBlock receiptBlock = new Receipt.ReceiptBlock(transactionBlock.getHash(), transactionBlock.getHeight(), transactionBlock.getGeneration(), transactionBlock.getMerkleRoot());
287306

adrestus-network/src/main/java/io/Adrestus/rpc/RpcAdrestusClient.java

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
public class RpcAdrestusClient<T> {
3939
private static Logger LOG = LoggerFactory.getLogger(RpcAdrestusClient.class);
4040

41-
private static final int TIMEOUT = 4000;
41+
private int TIMEOUT = 4000;
4242

4343
private SerializationUtil<ListBlockResponse> serializationUtil;
4444
private SerializationUtil<PatriciaTreeResponse> serializationUtil2;
@@ -94,6 +94,24 @@ public RpcAdrestusClient(T typeParameterClass, String host, int port, Eventloop
9494
this.valueMapper = new SerializationUtil(typeParameterClass.getClass(), list, true);
9595
this.valueMapper2 = new SerializationUtil(typeParameterClass.getClass(), list, true);
9696
}
97+
public RpcAdrestusClient(T typeParameterClass, String host, int port,int timeout, Eventloop eventloop) {
98+
this.rpc_serialize = SerializerBuilder.create();
99+
this.typeParameterClass = typeParameterClass;
100+
this.host = host;
101+
this.port = port;
102+
this.eventloop = eventloop;
103+
this.TIMEOUT=timeout;
104+
List<SerializationUtil.Mapping> list = new ArrayList<>();
105+
list.add(new SerializationUtil.Mapping(ECP.class, ctx -> new ECPmapper()));
106+
list.add(new SerializationUtil.Mapping(ECP2.class, ctx -> new ECP2mapper()));
107+
list.add(new SerializationUtil.Mapping(BigInteger.class, ctx -> new BigIntegerSerializer()));
108+
list.add(new SerializationUtil.Mapping(TreeMap.class, ctx -> new CustomSerializerTreeMap()));
109+
list.add(new SerializationUtil.Mapping(MemoryTreePool.class, ctx -> new MemoryTreePoolSerializer()));
110+
this.serializationUtil2 = new SerializationUtil<PatriciaTreeResponse>(PatriciaTreeResponse.class, list);
111+
this.serializationUtil = new SerializationUtil<ListBlockResponse>(ListBlockResponse.class, list);
112+
this.valueMapper = new SerializationUtil(typeParameterClass.getClass(), list, true);
113+
this.valueMapper2 = new SerializationUtil(typeParameterClass.getClass(), list, true);
114+
}
97115

98116
public RpcAdrestusClient(T typeParameterClass, List<InetSocketAddress> inetSocketAddresses) {
99117
this.rpc_serialize = SerializerBuilder.create();

jitpack.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
jdk:
2+
- openjdk11
3+
install:
4+
- mvn install -DskipTests

0 commit comments

Comments
 (0)