Skip to content

Commit

Permalink
[#10] Set start page index = 1
Browse files Browse the repository at this point in the history
Added ignoreUtxosIfDatumHash flag
  • Loading branch information
satran004 committed Oct 15, 2021
1 parent 3349bc1 commit a824d7c
Show file tree
Hide file tree
Showing 6 changed files with 180 additions and 5 deletions.
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ dependencies {
testImplementation 'org.hamcrest:hamcrest-library:2.2'
testImplementation 'org.mockito:mockito-inline:3.7.7'
testImplementation 'org.mockito:mockito-junit-jupiter:3.7.7'
testImplementation('org.assertj:assertj-core:3.21.0')
testRuntimeOnly 'org.slf4j:slf4j-log4j12:1.7.30'
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.bloxbean.cardano.client.backend.api.helper;

import com.bloxbean.cardano.client.backend.common.OrderEnum;
import com.bloxbean.cardano.client.backend.exception.ApiException;
import com.bloxbean.cardano.client.backend.model.Utxo;

Expand All @@ -19,8 +18,24 @@ public interface UtxoSelectionStrategy {
* @param unit Unit
* @param amount Amount
* @param excludeUtxos Utxos to ignore
* @return
* @return List of Utxos
*/
public List<Utxo> selectUtxos(String address, String unit, BigInteger amount, Set<Utxo> excludeUtxos) throws ApiException;
List<Utxo> selectUtxos(String address, String unit, BigInteger amount, Set<Utxo> excludeUtxos) throws ApiException;

/**
*
* @return True if utxos with datum hash need to ignored, otherwise false
*/
default boolean ignoreUtxosWithDatumHash() {
return true;
}

/**
* Set if utxos with datum hash need to be ignored or not
* @param ignoreUtxosWithDatumHash
*/
default void setIgnoreUtxosWithDatumHash(boolean ignoreUtxosWithDatumHash) {

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,11 @@ public class DefaultUtxoSelectionStrategyImpl implements UtxoSelectionStrategy {

private UtxoService utxoService;

private boolean ignoreUtxosWithDatumHash;

public DefaultUtxoSelectionStrategyImpl(UtxoService utxoService) {
this.utxoService = utxoService;
this.ignoreUtxosWithDatumHash = true;
}

@Override
Expand All @@ -33,7 +36,7 @@ public List<Utxo> selectUtxos(String address, String unit, BigInteger amount, Se
BigInteger totalUtxoAmount = BigInteger.valueOf(0);
List<Utxo> selectedUtxos = new ArrayList<>();
boolean canContinue = true;
int i = 0;
int i = 1;

while(canContinue) {
Result<List<Utxo>> result = utxoService.getUtxos(address, getUtxoFetchSize(),
Expand All @@ -49,6 +52,9 @@ public List<Utxo> selectUtxos(String address, String unit, BigInteger amount, Se
if(excludeUtxos.contains(utxo))
continue;

if(utxo.getDataHash() != null && !utxo.getDataHash().isEmpty() && ignoreUtxosWithDatumHash())
continue;

List<Amount> utxoAmounts = utxo.getAmount();

boolean unitFound = false;
Expand Down Expand Up @@ -76,6 +82,16 @@ public List<Utxo> selectUtxos(String address, String unit, BigInteger amount, Se
return selectedUtxos;
}

@Override
public boolean ignoreUtxosWithDatumHash() {
return ignoreUtxosWithDatumHash;
}

@Override
public void setIgnoreUtxosWithDatumHash(boolean ignoreUtxosWithDatumHash) {
this.ignoreUtxosWithDatumHash = ignoreUtxosWithDatumHash;
}

protected List<Utxo> filter(List<Utxo> fetchData) {
return fetchData;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@
@JsonNaming(PropertyNamingStrategy.SnakeCaseStrategy.class)
public class Utxo {
private String txHash;
// private int txIndex;
private int outputIndex;
private List<Amount> amount;
private String dataHash;

@Override
public boolean equals(Object o) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package com.bloxbean.cardano.client.backend.api.helper.impl;

import com.bloxbean.cardano.client.backend.api.UtxoService;
import com.bloxbean.cardano.client.backend.exception.ApiException;
import com.bloxbean.cardano.client.backend.model.Result;
import com.bloxbean.cardano.client.backend.model.Utxo;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.mockito.junit.jupiter.MockitoExtension;

import java.io.IOException;
import java.math.BigInteger;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import static com.bloxbean.cardano.client.common.CardanoConstants.LOVELACE;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.*;
import static org.mockito.BDDMockito.given;

@ExtendWith(MockitoExtension.class)
class DefaultUtxoSelectionStrategyImplTest {

private static final String LIST_1 = "list1";
@Mock
UtxoService utxoService;

ObjectMapper objectMapper = new ObjectMapper();

String dataFile = "utxos-selection-strategy.json";

@BeforeEach
public void setup() {
MockitoAnnotations.openMocks(this);
}

private List<Utxo> loadUtxos(String key) throws IOException {
TypeReference<HashMap<String, List<Utxo>>> typeRef
= new TypeReference<HashMap<String, List<Utxo>>>() {};
Map<String, List<Utxo>> map = objectMapper.readValue(this.getClass().getClassLoader().getResourceAsStream(dataFile), typeRef);
return map.getOrDefault(key, Collections.emptyList());
}

@Test
void selectUtxos_returnUtxosWithoutDataHashWhenIgnoreUtxoDataHashIsTrue() throws IOException, ApiException {
String address = "addr_test1qqwpl7h3g84mhr36wpetk904p7fchx2vst0z696lxk8ujsjyruqwmlsm344gfux3nsj6njyzj3ppvrqtt36cp9xyydzqzumz82";

List<Utxo> utxos = loadUtxos(LIST_1);
given(utxoService.getUtxos(any(), anyInt(), anyInt(), any())).willReturn(Result.success(utxos.toString()).withValue(utxos).code(200));

DefaultUtxoSelectionStrategyImpl selectionStrategy = new DefaultUtxoSelectionStrategyImpl(utxoService);
List<Utxo> selectedUtxos = selectionStrategy.selectUtxos(address, LOVELACE, new BigInteger("5000"), Collections.EMPTY_SET);

List<String> txnHashList = selectedUtxos.stream().map(utxo -> utxo.getTxHash()).collect(Collectors.toList());

assertThat(txnHashList).doesNotContain("88c014d348bf1919c78a5cb87a5beed87729ff3f8a2019be040117a41a83e82e");
assertThat(txnHashList).hasSize(3);
}

@Test
void selectUtxos_returnUtxosWithoutDataHashWhenIgnoreUtxoDataHashIsFalse() throws IOException, ApiException {
String address = "addr_test1qqwpl7h3g84mhr36wpetk904p7fchx2vst0z696lxk8ujsjyruqwmlsm344gfux3nsj6njyzj3ppvrqtt36cp9xyydzqzumz82";

List<Utxo> utxos = loadUtxos(LIST_1);
given(utxoService.getUtxos(any(), anyInt(), anyInt(), any())).willReturn(Result.success(utxos.toString()).withValue(utxos).code(200));

DefaultUtxoSelectionStrategyImpl selectionStrategy = new DefaultUtxoSelectionStrategyImpl(utxoService);
selectionStrategy.setIgnoreUtxosWithDatumHash(false);

List<Utxo> selectedUtxos = selectionStrategy.selectUtxos(address, LOVELACE, new BigInteger("5000"), Collections.EMPTY_SET);

List<String> txnHashList = selectedUtxos.stream().map(utxo -> utxo.getTxHash()).collect(Collectors.toList());

assertThat(txnHashList).contains("88c014d348bf1919c78a5cb87a5beed87729ff3f8a2019be040117a41a83e82e");
assertThat(txnHashList).hasSize(3);
}
}
58 changes: 58 additions & 0 deletions src/test/resources/utxos-selection-strategy.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
{
"list1": [
{
"tx_hash": "735262c68b5fa220dee2b447d0d1dd44e0800ba6212dcea7955c561f365fb0e9",
"tx_index": 0,
"output_index": 0,
"data_hash": null,
"amount": [
{
"unit": "lovelace",
"quantity": "1000"
},
{
"unit": "6b8d07d69639e9413dd637a1a815a7323c69c86abbafb66dbfdb1aa7",
"quantity": "2"
}
],
"block": "656f65cb141fa8a37b50c60da1e9f28a2e45a1ea80e13ac4f9e1323a2ec07557"
},
{
"tx_hash": "88c014d348bf1919c78a5cb87a5beed87729ff3f8a2019be040117a41a83e82e",
"tx_index": 1,
"output_index": 1,
"data_hash": "somehashxxxxdfksdjfsdfsldfjslfjslfjslfjslfsdlfsdf",
"amount": [
{
"unit": "lovelace",
"quantity": "2000"
}
],
"block": "1a06a27cfcb9316dfc3a498e96b80a3a9427ad03170077e3a80fd41a43ec3d7b"
},
{
"tx_hash": "6674e44f0f03915b1611ce58aaff5f2e52054e1911fbcd0f17dbc205f44763b6",
"tx_index": 0,
"output_index": 0,
"amount": [
{
"unit": "lovelace",
"quantity": "3000"
}
],
"block": "eb2043df94b34d1746cb47303446237e6dca7d051100c8f4229795be8c1f5e63"
},
{
"tx_hash": "a712906ae823ecefe6cab76e5cfd427bd0b6144df10c6f89a56fbdf30fa807f4",
"tx_index": 1,
"output_index": 1,
"amount": [
{
"unit": "lovelace",
"quantity": "4000"
}
],
"block": "c2a5408a54596d52e3b0d7aed546ceb7e4d74a06ce80dca13664acd3afdea3e5"
}
]
}

0 comments on commit a824d7c

Please sign in to comment.