Skip to content

Commit

Permalink
Merge pull request #135 from onflow/full-block-response-protobuf
Browse files Browse the repository at this point in the history
Add `fullBlockResponse` param to Access API block requests
  • Loading branch information
franklywatson authored Oct 25, 2024
2 parents 0166c83 + abffae9 commit 56be671
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ public GetBlockAccessAPIConnector(FlowAccessApi accessAPI) {

public FlowBlock getLatestSealedBlock() {
boolean isSealed = true;
FlowAccessApi.AccessApiCallResponse<FlowBlock> response = accessAPI.getLatestBlock(isSealed);
boolean fullBlockResponse = false;
FlowAccessApi.AccessApiCallResponse<FlowBlock> response = accessAPI.getLatestBlock(isSealed, fullBlockResponse);

if (response instanceof FlowAccessApi.AccessApiCallResponse.Success) {
return ((FlowAccessApi.AccessApiCallResponse.Success<FlowBlock>) response).getData();
Expand All @@ -22,7 +23,8 @@ public FlowBlock getLatestSealedBlock() {
}

public FlowBlock getBlockByID(FlowId blockID) {
FlowAccessApi.AccessApiCallResponse<FlowBlock> response = accessAPI.getBlockById(blockID);
boolean fullBlockResponse = false;
FlowAccessApi.AccessApiCallResponse<FlowBlock> response = accessAPI.getBlockById(blockID, fullBlockResponse);

if (response instanceof FlowAccessApi.AccessApiCallResponse.Success) {
return ((FlowAccessApi.AccessApiCallResponse.Success<FlowBlock>) response).getData();
Expand All @@ -33,7 +35,8 @@ public FlowBlock getBlockByID(FlowId blockID) {
}

public FlowBlock getBlockByHeight(long height) {
FlowAccessApi.AccessApiCallResponse<FlowBlock> response = accessAPI.getBlockByHeight(height);
boolean fullBlockResponse = false;
FlowAccessApi.AccessApiCallResponse<FlowBlock> response = accessAPI.getBlockByHeight(height, fullBlockResponse);

if (response instanceof FlowAccessApi.AccessApiCallResponse.Success) {
return ((FlowAccessApi.AccessApiCallResponse.Success<FlowBlock>) response).getData();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public void testCanFetchBalanceAtLatestBlock() {
public void testCanFetchBalanceAtSpecificBlockHeight() {
FlowAddress address = serviceAccount.getFlowAddress();

FlowAccessApi.AccessApiCallResponse<FlowBlock> latestBlockResponse = accessAPI.getLatestBlock(true);
FlowAccessApi.AccessApiCallResponse<FlowBlock> latestBlockResponse = accessAPI.getLatestBlock(true, false);

if (latestBlockResponse instanceof FlowAccessApi.AccessApiCallResponse.Success) {
FlowBlock latestBlock = ((FlowAccessApi.AccessApiCallResponse.Success<FlowBlock>) latestBlockResponse).getData();
Expand All @@ -58,7 +58,7 @@ public void testBalancesAtLatestBlockAndSpecificHeightShouldMatch() {
FlowAddress address = serviceAccount.getFlowAddress();

long balanceAtLatest = balanceAPIConnector.getBalanceAtLatestBlock(address);
FlowAccessApi.AccessApiCallResponse<FlowBlock> latestBlockResponse = accessAPI.getLatestBlock(true);
FlowAccessApi.AccessApiCallResponse<FlowBlock> latestBlockResponse = accessAPI.getLatestBlock(true, false);

if (latestBlockResponse instanceof FlowAccessApi.AccessApiCallResponse.Success) {
FlowBlock latestBlock = ((FlowAccessApi.AccessApiCallResponse.Success<FlowBlock>) latestBlockResponse).getData();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public void setup() {
publicKey
);

FlowAccessApi.AccessApiCallResponse<FlowBlock> response = accessAPI.getLatestBlock(true);
FlowAccessApi.AccessApiCallResponse<FlowBlock> response = accessAPI.getLatestBlock(true, false);
if (response instanceof FlowAccessApi.AccessApiCallResponse.Success) {
FlowBlock block = ((FlowAccessApi.AccessApiCallResponse.Success<FlowBlock>) response).getData();
collectionId = block.getCollectionGuarantees().get(0).getId();
Expand Down
6 changes: 3 additions & 3 deletions sdk/src/main/kotlin/org/onflow/flow/sdk/AsyncFlowAccessApi.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@ interface AsyncFlowAccessApi {

fun getBlockHeaderByHeight(height: Long): CompletableFuture<FlowAccessApi.AccessApiCallResponse<FlowBlockHeader?>>

fun getLatestBlock(sealed: Boolean = true): CompletableFuture<FlowAccessApi.AccessApiCallResponse<FlowBlock>>
fun getLatestBlock(sealed: Boolean = true, fullBlockResponse: Boolean = false): CompletableFuture<FlowAccessApi.AccessApiCallResponse<FlowBlock>>

fun getAccountBalanceAtLatestBlock(address: FlowAddress): CompletableFuture<FlowAccessApi.AccessApiCallResponse<Long>>

fun getAccountBalanceAtBlockHeight(address: FlowAddress, height: Long): CompletableFuture<FlowAccessApi.AccessApiCallResponse<Long>>

fun getBlockById(id: FlowId): CompletableFuture<FlowAccessApi.AccessApiCallResponse<FlowBlock?>>
fun getBlockById(id: FlowId, fullBlockResponse: Boolean = false): CompletableFuture<FlowAccessApi.AccessApiCallResponse<FlowBlock?>>

fun getBlockByHeight(height: Long): CompletableFuture<FlowAccessApi.AccessApiCallResponse<FlowBlock?>>
fun getBlockByHeight(height: Long, fullBlockResponse: Boolean = false): CompletableFuture<FlowAccessApi.AccessApiCallResponse<FlowBlock?>>

fun getCollectionById(id: FlowId): CompletableFuture<FlowAccessApi.AccessApiCallResponse<FlowCollection?>>

Expand Down
7 changes: 3 additions & 4 deletions sdk/src/main/kotlin/org/onflow/flow/sdk/FlowAccessApi.kt
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,14 @@ interface FlowAccessApi {

fun getBlockHeaderByHeight(height: Long): AccessApiCallResponse<FlowBlockHeader>

fun getLatestBlock(sealed: Boolean = true): AccessApiCallResponse<FlowBlock>
fun getLatestBlock(sealed: Boolean = true, fullBlockResponse: Boolean = false): AccessApiCallResponse<FlowBlock>

fun getBlockById(id: FlowId, fullBlockResponse: Boolean = false): AccessApiCallResponse<FlowBlock>
fun getAccountBalanceAtLatestBlock(address: FlowAddress): AccessApiCallResponse<Long>

fun getAccountBalanceAtBlockHeight(address: FlowAddress, height: Long): AccessApiCallResponse<Long>

fun getBlockById(id: FlowId): AccessApiCallResponse<FlowBlock>

fun getBlockByHeight(height: Long): AccessApiCallResponse<FlowBlock>
fun getBlockByHeight(height: Long, fullBlockResponse: Boolean = false): AccessApiCallResponse<FlowBlock>

fun getCollectionById(id: FlowId): AccessApiCallResponse<FlowCollection>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,14 +127,15 @@ class AsyncFlowAccessApiImpl(
}
}

override fun getLatestBlock(sealed: Boolean): CompletableFuture<FlowAccessApi.AccessApiCallResponse<FlowBlock>> {
override fun getLatestBlock(sealed: Boolean, fullBlockResponse: Boolean): CompletableFuture<FlowAccessApi.AccessApiCallResponse<FlowBlock>> {
return try {
completableFuture(
try {
api.getLatestBlock(
Access.GetLatestBlockRequest
.newBuilder()
.setIsSealed(sealed)
.setFullBlockResponse(fullBlockResponse)
.build()
)
} catch (e: Exception) {
Expand Down Expand Up @@ -203,14 +204,15 @@ class AsyncFlowAccessApiImpl(
}
}

override fun getBlockById(id: FlowId): CompletableFuture<FlowAccessApi.AccessApiCallResponse<FlowBlock?>> {
override fun getBlockById(id: FlowId, fullBlockResponse: Boolean): CompletableFuture<FlowAccessApi.AccessApiCallResponse<FlowBlock?>> {
return try {
completableFuture(
try {
api.getBlockByID(
Access.GetBlockByIDRequest
.newBuilder()
.setId(id.byteStringValue)
.setFullBlockResponse(fullBlockResponse)
.build()
)
} catch (e: Exception) {
Expand All @@ -232,14 +234,15 @@ class AsyncFlowAccessApiImpl(
}
}

override fun getBlockByHeight(height: Long): CompletableFuture<FlowAccessApi.AccessApiCallResponse<FlowBlock?>> {
override fun getBlockByHeight(height: Long, fullBlockResponse: Boolean): CompletableFuture<FlowAccessApi.AccessApiCallResponse<FlowBlock?>> {
return try {
completableFuture(
try {
api.getBlockByHeight(
Access.GetBlockByHeightRequest
.newBuilder()
.setHeight(height)
.setFullBlockResponse(fullBlockResponse)
.build()
)
} catch (e: Exception) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,12 +83,13 @@ class FlowAccessApiImpl(
FlowAccessApi.AccessApiCallResponse.Error("Failed to get block header by height", e)
}

override fun getLatestBlock(sealed: Boolean): FlowAccessApi.AccessApiCallResponse<FlowBlock> =
override fun getLatestBlock(sealed: Boolean, fullBlockResponse: Boolean): FlowAccessApi.AccessApiCallResponse<FlowBlock> =
try {
val ret = api.getLatestBlock(
Access.GetLatestBlockRequest
.newBuilder()
.setIsSealed(sealed)
.setFullBlockResponse(fullBlockResponse)
.build()
)
FlowAccessApi.AccessApiCallResponse.Success(FlowBlock.of(ret.block))
Expand Down Expand Up @@ -123,12 +124,13 @@ class FlowAccessApiImpl(
FlowAccessApi.AccessApiCallResponse.Error("Failed to get account balance at block height", e)
}

override fun getBlockById(id: FlowId): FlowAccessApi.AccessApiCallResponse<FlowBlock> =
override fun getBlockById(id: FlowId, fullBlockResponse: Boolean): FlowAccessApi.AccessApiCallResponse<FlowBlock> =
try {
val ret = api.getBlockByID(
Access.GetBlockByIDRequest
.newBuilder()
.setId(id.byteStringValue)
.setFullBlockResponse(fullBlockResponse)
.build()
)
if (ret.hasBlock()) {
Expand All @@ -140,12 +142,13 @@ class FlowAccessApiImpl(
FlowAccessApi.AccessApiCallResponse.Error("Failed to get block by ID", e)
}

override fun getBlockByHeight(height: Long): FlowAccessApi.AccessApiCallResponse<FlowBlock> =
override fun getBlockByHeight(height: Long, fullBlockResponse: Boolean): FlowAccessApi.AccessApiCallResponse<FlowBlock> =
try {
val ret = api.getBlockByHeight(
Access.GetBlockByHeightRequest
.newBuilder()
.setHeight(height)
.setFullBlockResponse(fullBlockResponse)
.build()
)
if (ret.hasBlock()) {
Expand Down

0 comments on commit 56be671

Please sign in to comment.