Skip to content

Commit

Permalink
Merge pull request #70 from bloxbean/issue_68
Browse files Browse the repository at this point in the history
Implement Local Queries for Governance-Related Data
  • Loading branch information
Sotatek-HuyLe3a authored Aug 20, 2024
2 parents 6fabfa3 + 66f041a commit 64bbf5f
Show file tree
Hide file tree
Showing 13 changed files with 1,050 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package com.bloxbean.cardano.yaci.core.protocol.localstate.queries;

import co.nstant.in.cbor.model.Array;
import co.nstant.in.cbor.model.DataItem;
import co.nstant.in.cbor.model.UnsignedInteger;
import com.bloxbean.cardano.yaci.core.model.governance.Anchor;
import com.bloxbean.cardano.yaci.core.model.serializers.governance.AnchorSerializer;
import com.bloxbean.cardano.yaci.core.protocol.handshake.messages.AcceptVersion;
import com.bloxbean.cardano.yaci.core.protocol.localstate.api.Era;
import com.bloxbean.cardano.yaci.core.protocol.localstate.api.EraQuery;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NonNull;
import lombok.ToString;

import java.util.List;

@Getter
@AllArgsConstructor
@ToString
// ouroboros-consensus-cardano/src/shelley/Ouroboros/Consensus/Shelley/Ledger/Query.hs
public class ConstitutionQuery implements EraQuery<ConstitutionResult> {
@NonNull
private Era era;

public ConstitutionQuery() {
this.era = Era.Conway;
}

@Override
public DataItem serialize(AcceptVersion protocolVersion) {
Array queryArray = new Array();
queryArray.add(new UnsignedInteger(23));

return wrapWithOuterArray(queryArray);
}

@Override
public ConstitutionResult deserializeResult(AcceptVersion protocolVersion, DataItem[] di) {
List<DataItem> dataItemList = ((Array)di[0]).getDataItems();

int type = ((UnsignedInteger)dataItemList.get(0)).getValue().intValue(); //4

List<DataItem> resultDIList = ((Array)dataItemList.get(1)).getDataItems();
var items = (Array)resultDIList.get(0);

Anchor anchor = AnchorSerializer.INSTANCE.deserializeDI(items.getDataItems().get(0));
return new ConstitutionResult(anchor);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.bloxbean.cardano.yaci.core.protocol.localstate.queries;

import com.bloxbean.cardano.yaci.core.model.governance.Anchor;
import com.bloxbean.cardano.yaci.core.protocol.localstate.api.QueryResult;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.ToString;

@Getter
@AllArgsConstructor
@ToString
public class ConstitutionResult implements QueryResult {
private Anchor anchor;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package com.bloxbean.cardano.yaci.core.protocol.localstate.queries;


import co.nstant.in.cbor.model.Array;
import co.nstant.in.cbor.model.DataItem;
import co.nstant.in.cbor.model.Map;
import co.nstant.in.cbor.model.UnsignedInteger;
import com.bloxbean.cardano.yaci.core.model.Credential;
import com.bloxbean.cardano.yaci.core.model.governance.Anchor;
import com.bloxbean.cardano.yaci.core.model.serializers.governance.AnchorSerializer;
import com.bloxbean.cardano.yaci.core.protocol.handshake.messages.AcceptVersion;
import com.bloxbean.cardano.yaci.core.protocol.localstate.api.Era;
import com.bloxbean.cardano.yaci.core.protocol.localstate.api.EraQuery;
import com.bloxbean.cardano.yaci.core.protocol.localstate.queries.model.DRepState;
import com.bloxbean.cardano.yaci.core.util.CborSerializationUtil;
import lombok.AllArgsConstructor;
import lombok.Getter;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;

@Getter
@AllArgsConstructor
public class DRepStateQuery implements EraQuery<DRepStateQueryResult> {
private Era era;
private List<Credential> dRepCreds;

public DRepStateQuery(List<Credential> dRepCreds) {
this(Era.Conway, dRepCreds);
}

@Override
public DataItem serialize(AcceptVersion protocolVersion) {
Array array = new Array();
array.add(new UnsignedInteger(25));

Array dRepCredArray = new Array();

dRepCreds.forEach(dRepCred -> dRepCredArray.add(dRepCred.serialize()));

dRepCredArray.setTag(258);

array.add(dRepCredArray);

return wrapWithOuterArray(array);
}

@Override
public DRepStateQueryResult deserializeResult(AcceptVersion protocolVersion, DataItem[] di) {
DRepStateQueryResult result = new DRepStateQueryResult();
var array = (Array) di[0];
var drepStateArray = (Array) array.getDataItems().get(1);

for (var dataItem : drepStateArray.getDataItems()) {
var map = (Map)dataItem;
var keys = (List<DataItem>)map.getKeys();

if (keys.isEmpty()) {
continue;
}
for (var key : keys) {
var itemDI = (Array)key;
String dRepHash = CborSerializationUtil.toHex(itemDI.getDataItems().get(1));
var value = (Array) map.get(itemDI);
Integer expiry = CborSerializationUtil.toInt(value.getDataItems().get(0));

Anchor anchor = null;
if (((Array) value.getDataItems().get(1)).getDataItems().size() > 0) {
anchor = AnchorSerializer.INSTANCE.deserializeDI(((Array)value.getDataItems().get(1)).getDataItems().get(0));
}

BigInteger deposit = CborSerializationUtil.toBigInteger(value.getDataItems().get(2));

var dRepState = DRepState.builder()
.dRepHash(dRepHash)
.anchor(anchor)
.deposit(deposit)
.expiry(expiry)
.build();

result.addDRepState(dRepState);
}
}

return result;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.bloxbean.cardano.yaci.core.protocol.localstate.queries;

import com.bloxbean.cardano.yaci.core.protocol.localstate.api.QueryResult;
import com.bloxbean.cardano.yaci.core.protocol.localstate.queries.model.DRepState;
import lombok.Getter;
import lombok.Setter;

import java.util.ArrayList;
import java.util.List;

@Setter
@Getter
public class DRepStateQueryResult implements QueryResult {
List<DRepState> dRepStates;

public DRepStateQueryResult() {
this.dRepStates = new ArrayList<>();
}

public void addDRepState(DRepState dRepState) {
dRepStates.add(dRepState);
}
}
Loading

0 comments on commit 64bbf5f

Please sign in to comment.