Skip to content

Commit e027775

Browse files
authored
HistoryOffer Async Binding (#146)
1 parent ca624f9 commit e027775

4 files changed

Lines changed: 34 additions & 15 deletions

File tree

core/src/main/java/samba/network/history/HistoryNetwork.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -333,12 +333,12 @@ public SafeFuture<Optional<Bytes>> offer(
333333
.map(Util::addUnsignedLeb128SizeToData)
334334
.toList();
335335

336-
Optional.ofNullable(contentToOffer)
337-
.filter(list -> !list.isEmpty())
338-
.ifPresent(
339-
list ->
340-
utpManager.offerWrite(
341-
nodeRecord, accept.getConnectionId(), Bytes.concatenate(list)));
336+
if (contentToOffer != null && !contentToOffer.isEmpty()) {
337+
return utpManager
338+
.offerWrite(
339+
nodeRecord, accept.getConnectionId(), Bytes.concatenate(contentToOffer))
340+
.thenApply(__ -> Optional.of(accept.getContentKeys()));
341+
}
342342

343343
return SafeFuture.completedFuture(Optional.of(accept.getContentKeys()));
344344
})

core/src/main/java/samba/services/utp/UTPManager.java

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
import java.io.IOException;
88
import java.util.Map;
9+
import java.util.concurrent.CompletableFuture;
910
import java.util.concurrent.ConcurrentHashMap;
1011
import java.util.concurrent.ExecutionException;
1112
import java.util.concurrent.Executor;
@@ -68,19 +69,19 @@ public int acceptRead(NodeRecord nodeRecord, Consumer<Bytes> onContentReceived)
6869
return connectionId;
6970
}
7071

71-
public void offerWrite(final NodeRecord nodeRecord, final int connectionId, Bytes content) {
72-
this.runAsyncUTP(
72+
public SafeFuture<Void> offerWrite(NodeRecord nodeRecord, int connectionId, Bytes content) {
73+
return runAsyncUTPWithFuture(
7374
() -> {
74-
UTPClient utpClient = this.registerClient(nodeRecord, connectionId);
75+
UTPClient utpClient = registerClient(nodeRecord, connectionId);
7576
utpClient
7677
.connect(connectionId, new UTPAddress(nodeRecord))
77-
.thenCompose(__ -> utpClient.write(content, this.utpExecutor))
78-
.get();
78+
.thenCompose(__ -> utpClient.write(content, utpExecutor))
79+
.join();
7980
},
8081
"offerWrite",
8182
nodeRecord,
8283
connectionId,
83-
this.utpExecutor);
84+
utpExecutor);
8485
}
8586

8687
public int foundContentWrite(NodeRecord nodeRecord, Bytes content) {
@@ -195,6 +196,21 @@ private void runAsyncUTP(
195196
.exceptionally(defaultUTPErrorLog(operationName, nodeRecord, connectionId));
196197
}
197198

199+
private SafeFuture<Void> runAsyncUTPWithFuture(
200+
RunnableUTP task,
201+
String operationName,
202+
NodeRecord nodeRecord,
203+
int connectionId,
204+
Executor executor) {
205+
206+
CompletableFuture<Void> future =
207+
SafeFuture.runAsync(
208+
() -> executeWithHandling(task, operationName, nodeRecord, connectionId), executor)
209+
.exceptionally(defaultUTPErrorLog(operationName, nodeRecord, connectionId));
210+
211+
return SafeFuture.of(future);
212+
}
213+
198214
private void executeWithHandling(
199215
RunnableUTP task, String operationName, NodeRecord nodeRecord, int connectionId) {
200216
try {

core/src/test/java/samba/network/history/OfferMessageTests.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import static org.junit.jupiter.api.Assertions.assertThrows;
55
import static org.junit.jupiter.api.Assertions.assertTrue;
66
import static org.mockito.ArgumentMatchers.any;
7+
import static org.mockito.ArgumentMatchers.anyInt;
78
import static org.mockito.ArgumentMatchers.eq;
89
import static org.mockito.Mockito.mock;
910
import static org.mockito.Mockito.mockStatic;
@@ -251,7 +252,8 @@ public void sendOkOfferMessageWithEmptyContentAndGetAcceptedMessageAndAnOkRespon
251252
when(discv5Client.sendDiscv5Message(any(NodeRecord.class), any(Bytes.class), any(Bytes.class)))
252253
.thenReturn(createAcceptResponse(555, Bytes.of(1), 0));
253254
when(historyDB.get(any(ContentKey.class))).thenReturn(Optional.of(Bytes.of(0)));
254-
255+
when(utpManager.offerWrite(any(NodeRecord.class), anyInt(), any(Bytes.class)))
256+
.thenReturn(SafeFuture.completedFuture(null));
255257
Offer offer = new Offer(List.of(DefaultContent.key3));
256258

257259
Optional<Bytes> contentKeysBitList =
@@ -277,7 +279,8 @@ public void sendOkOfferMessageWithEmptyContentAndGetAcceptedMessageAndAnOkRespon
277279
any(NodeRecord.class), any(Bytes.class), any(Bytes.class)))
278280
.thenReturn(createAcceptResponse(777, Bytes.of(0), 1));
279281
when(historyDB.get(any(ContentKey.class))).thenReturn(Optional.of(Bytes.of(0)));
280-
282+
when(utpManager.offerWrite(any(NodeRecord.class), anyInt(), any(Bytes.class)))
283+
.thenReturn(SafeFuture.completedFuture(null));
281284
Offer offer = new Offer(List.of(DefaultContent.key3));
282285
Optional<Bytes> contentKeysByteList =
283286
this.historyNetwork.offer(nodeRecord, List.of(DefaultContent.value3), offer).get();

core/src/test/java/samba/util/DefaultContent.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public final class DefaultContent {
3737

3838
key3 =
3939
Bytes.fromHexString("0x0288e96d4537bea4d9c05d12549907b32561d3bf31f45aae734cdc119f13406cb6");
40-
value3 = Bytes.fromHexString("0x00");
40+
value3 = Bytes.fromHexString("0x");
4141

4242
preMergeBlockHeader =
4343
new ContentBlockHeader(

0 commit comments

Comments
 (0)