-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #70 from bloxbean/issue_68
Implement Local Queries for Governance-Related Data
- Loading branch information
Showing
13 changed files
with
1,050 additions
and
4 deletions.
There are no files selected for viewing
50 changes: 50 additions & 0 deletions
50
...in/java/com/bloxbean/cardano/yaci/core/protocol/localstate/queries/ConstitutionQuery.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
...n/java/com/bloxbean/cardano/yaci/core/protocol/localstate/queries/ConstitutionResult.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
88 changes: 88 additions & 0 deletions
88
.../main/java/com/bloxbean/cardano/yaci/core/protocol/localstate/queries/DRepStateQuery.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
...java/com/bloxbean/cardano/yaci/core/protocol/localstate/queries/DRepStateQueryResult.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
Oops, something went wrong.