Skip to content

Commit

Permalink
Feat/graalvm - metadata files (#95)
Browse files Browse the repository at this point in the history
* chore: Graalvm reachable metadata

* Implement StakePoolParamsQuery deserialization

Added functionality to deserialize StakePoolParamsQuery results, converting CBOR data items into PoolParams objects. Updated integration tests to validate the deserialization process.
  • Loading branch information
satran004 authored Oct 17, 2024
1 parent 909a147 commit 8e643ba
Show file tree
Hide file tree
Showing 9 changed files with 291 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ public PoolRegistration deserializeDI(DataItem di) {
return new PoolRegistration(poolParams);
}

private Relay deserializeRelay(DataItem relayDI) {
public static Relay deserializeRelay(DataItem relayDI) {
List<DataItem> relayItems = ((Array) relayDI).getDataItems();
int type = toInt(relayItems.get(0));

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
package com.bloxbean.cardano.yaci.core.protocol.localstate.queries;

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

@Getter
@ToString
@AllArgsConstructor
public class StakePoolParamQueryResult implements QueryResult {
private java.util.Map<String, PoolParams> poolParams;
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package com.bloxbean.cardano.yaci.core.protocol.localstate.queries;

import co.nstant.in.cbor.model.Array;
import co.nstant.in.cbor.model.ByteString;
import co.nstant.in.cbor.model.DataItem;
import co.nstant.in.cbor.model.UnsignedInteger;
import co.nstant.in.cbor.model.*;
import co.nstant.in.cbor.model.Map;
import com.bloxbean.cardano.yaci.core.model.PoolParams;
import com.bloxbean.cardano.yaci.core.model.Relay;
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;
Expand All @@ -12,7 +12,12 @@
import lombok.Getter;
import lombok.NonNull;

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

import static com.bloxbean.cardano.yaci.core.model.serializers.PoolRegistrationSerializer.deserializeRelay;
import static com.bloxbean.cardano.yaci.core.util.CborSerializationUtil.*;
import static com.bloxbean.cardano.yaci.core.util.CborSerializationUtil.toHex;

@Getter
@AllArgsConstructor
Expand Down Expand Up @@ -41,6 +46,65 @@ public DataItem serialize(AcceptVersion protocolVersion) {

@Override
public StakePoolParamQueryResult deserializeResult(AcceptVersion protocolVersion, DataItem[] di) {
throw new UnsupportedOperationException("Not implemented yet");
List<DataItem> dataItemList = extractResultArray(di[0]);
Map poolMap = (Map) dataItemList.get(0);

java.util.Map<String, PoolParams> poolParamsMap = new LinkedHashMap<>();
for (var key : poolMap.getKeys()) {
String poolId = HexUtil.encodeHexString(((ByteString) key).getBytes());
var poolParamDIList = ((Array)poolMap.get(key)).getDataItems();

String operator = toHex(poolParamDIList.get(0));
String vrfKeyHash = toHex(poolParamDIList.get(1));
BigInteger pledge = toBigInteger(poolParamDIList.get(2));
BigInteger cost = toBigInteger(poolParamDIList.get(3));
String margin = ((RationalNumber) poolParamDIList.get(4)).getNumerator() + "/" + ((RationalNumber) poolParamDIList.get(4)).getDenominator();
String rewardAccount = toHex(poolParamDIList.get(5));

//Pool Owners0
Set<String> poolOwners = new HashSet<>();
List<DataItem> poolOwnersDataItems = ((Array) poolParamDIList.get(6)).getDataItems();
for (DataItem poolOwnerDI : poolOwnersDataItems) {
if (poolOwnerDI == SimpleValue.BREAK)
continue;
poolOwners.add(toHex(poolOwnerDI));
}

//Relays
List<DataItem> relaysDataItems = ((Array) poolParamDIList.get(7)).getDataItems();
List<Relay> relays = new ArrayList<>();
for (DataItem relayDI : relaysDataItems) {
if (relayDI == SimpleValue.BREAK)
continue;
relays.add(deserializeRelay(relayDI));
}

//pool metadata
DataItem poolMetaDataDI = poolParamDIList.get(8);
String metadataUrl = null;
String metadataHash = null;
if (poolMetaDataDI != SimpleValue.NULL) {
List<DataItem> poolMetadataDataItems = ((Array) poolMetaDataDI).getDataItems();
metadataUrl = toUnicodeString(poolMetadataDataItems.get(0));
metadataHash = toHex(poolMetadataDataItems.get(1));
}

PoolParams poolParams = PoolParams.builder()
.operator(operator)
.vrfKeyHash(vrfKeyHash)
.pledge(pledge)
.cost(cost)
.margin(margin)
.rewardAccount(rewardAccount)
.poolOwners(poolOwners)
.relays(relays)
.poolMetadataUrl(metadataUrl)
.poolMetadataHash(metadataHash)
.build();

poolParamsMap.put(poolId, poolParams);
}

return new StakePoolParamQueryResult(poolParamsMap);
}
}
11 changes: 11 additions & 0 deletions core/src/main/resources/META-INF/native-image/yaci/jni-config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[
{
"name" : "io.netty.channel.kqueue.KQueueStaticallyReferencedJniMethods"
},
{
"name" : "io.netty.channel.kqueue.Native"
},
{
"name" : "io.netty.channel.kqueue.BsdSocket"
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
Args = --initialize-at-run-time=io.netty.channel.epoll.Epoll \
--initialize-at-run-time=io.netty.channel.epoll.Native \
--initialize-at-run-time=io.netty.channel.epoll.EpollEventLoop \
--initialize-at-run-time=io.netty.channel.epoll.EpollEventArray \
--initialize-at-run-time=io.netty.channel.DefaultFileRegion \
--initialize-at-run-time=io.netty.channel.kqueue.KQueueEventArray \
--initialize-at-run-time=io.netty.channel.kqueue.KQueueEventLoop \
--initialize-at-run-time=io.netty.channel.kqueue.Native \
--initialize-at-run-time=io.netty.channel.unix.Errors \
--initialize-at-run-time=io.netty.channel.unix.IovArray \
--initialize-at-run-time=io.netty.channel.unix.Limits \
--initialize-at-run-time=io.netty.util.internal.logging.Log4JLogger \
--initialize-at-run-time=io.netty.channel.kqueue.KQueue \
--initialize-at-run-time=io.netty.handler.ssl.BouncyCastleAlpnSslUtils
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
Args = --static --libc=musl --initialize-at-run-time=io.netty.channel.epoll.Epoll \
--initialize-at-run-time=io.netty.channel.epoll.Native \
--initialize-at-run-time=io.netty.channel.epoll.EpollEventLoop \
--initialize-at-run-time=io.netty.channel.epoll.EpollEventArray \
--initialize-at-run-time=io.netty.channel.DefaultFileRegion \
--initialize-at-run-time=io.netty.channel.kqueue.KQueueEventArray \
--initialize-at-run-time=io.netty.channel.kqueue.KQueueEventLoop \
--initialize-at-run-time=io.netty.channel.kqueue.Native \
--initialize-at-run-time=io.netty.channel.unix.Errors \
--initialize-at-run-time=io.netty.channel.unix.IovArray \
--initialize-at-run-time=io.netty.channel.unix.Limits \
--initialize-at-run-time=io.netty.util.internal.logging.Log4JLogger \
--initialize-at-run-time=io.netty.channel.kqueue.KQueue \
--initialize-at-run-time=io.netty.handler.ssl.BouncyCastleAlpnSslUtils
148 changes: 148 additions & 0 deletions core/src/main/resources/META-INF/native-image/yaci/reflect-config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
[
{
"name":"io.netty.buffer.AbstractByteBufAllocator",
"queryAllDeclaredMethods":true
},
{
"name":"io.netty.buffer.AbstractReferenceCountedByteBuf",
"fields":[{"name":"refCnt"}]
},
{
"name":"io.netty.buffer.PooledByteBufAllocator"
},
{
"name":"io.netty.channel.AbstractChannelHandlerContext",
"fields":[{"name":"handlerState"}]
},
{
"name":"io.netty.channel.ChannelHandlerAdapter",
"methods":[{"name":"exceptionCaught","parameterTypes":["io.netty.channel.ChannelHandlerContext","java.lang.Throwable"] }]
},
{
"name":"io.netty.channel.ChannelInboundHandlerAdapter",
"methods":[{"name":"channelActive","parameterTypes":["io.netty.channel.ChannelHandlerContext"] }, {"name":"channelInactive","parameterTypes":["io.netty.channel.ChannelHandlerContext"] }, {"name":"channelRead","parameterTypes":["io.netty.channel.ChannelHandlerContext","java.lang.Object"] }, {"name":"channelReadComplete","parameterTypes":["io.netty.channel.ChannelHandlerContext"] }, {"name":"channelRegistered","parameterTypes":["io.netty.channel.ChannelHandlerContext"] }, {"name":"channelUnregistered","parameterTypes":["io.netty.channel.ChannelHandlerContext"] }, {"name":"channelWritabilityChanged","parameterTypes":["io.netty.channel.ChannelHandlerContext"] }, {"name":"exceptionCaught","parameterTypes":["io.netty.channel.ChannelHandlerContext","java.lang.Throwable"] }, {"name":"userEventTriggered","parameterTypes":["io.netty.channel.ChannelHandlerContext","java.lang.Object"] }]
},
{
"name":"io.netty.channel.ChannelInitializer",
"methods":[{"name":"channelRegistered","parameterTypes":["io.netty.channel.ChannelHandlerContext"] }, {"name":"exceptionCaught","parameterTypes":["io.netty.channel.ChannelHandlerContext","java.lang.Throwable"] }]
},
{
"name":"io.netty.channel.ChannelOutboundBuffer",
"fields":[{"name":"totalPendingSize"}, {"name":"unwritable"}]
},
{
"name":"io.netty.channel.ChannelOutboundHandlerAdapter",
"methods":[{"name":"bind","parameterTypes":["io.netty.channel.ChannelHandlerContext","java.net.SocketAddress","io.netty.channel.ChannelPromise"] }, {"name":"close","parameterTypes":["io.netty.channel.ChannelHandlerContext","io.netty.channel.ChannelPromise"] }, {"name":"connect","parameterTypes":["io.netty.channel.ChannelHandlerContext","java.net.SocketAddress","java.net.SocketAddress","io.netty.channel.ChannelPromise"] }, {"name":"deregister","parameterTypes":["io.netty.channel.ChannelHandlerContext","io.netty.channel.ChannelPromise"] }, {"name":"disconnect","parameterTypes":["io.netty.channel.ChannelHandlerContext","io.netty.channel.ChannelPromise"] }, {"name":"flush","parameterTypes":["io.netty.channel.ChannelHandlerContext"] }, {"name":"read","parameterTypes":["io.netty.channel.ChannelHandlerContext"] }]
},
{
"name":"io.netty.channel.DefaultChannelConfig",
"fields":[{"name":"autoRead"}, {"name":"writeBufferWaterMark"}]
},
{
"name":"io.netty.channel.DefaultChannelPipeline",
"fields":[{"name":"estimatorHandle"}]
},
{
"name":"io.netty.channel.DefaultChannelPipeline$HeadContext",
"methods":[{"name":"bind","parameterTypes":["io.netty.channel.ChannelHandlerContext","java.net.SocketAddress","io.netty.channel.ChannelPromise"] }, {"name":"channelActive","parameterTypes":["io.netty.channel.ChannelHandlerContext"] }, {"name":"channelInactive","parameterTypes":["io.netty.channel.ChannelHandlerContext"] }, {"name":"channelRead","parameterTypes":["io.netty.channel.ChannelHandlerContext","java.lang.Object"] }, {"name":"channelReadComplete","parameterTypes":["io.netty.channel.ChannelHandlerContext"] }, {"name":"channelRegistered","parameterTypes":["io.netty.channel.ChannelHandlerContext"] }, {"name":"channelUnregistered","parameterTypes":["io.netty.channel.ChannelHandlerContext"] }, {"name":"channelWritabilityChanged","parameterTypes":["io.netty.channel.ChannelHandlerContext"] }, {"name":"close","parameterTypes":["io.netty.channel.ChannelHandlerContext","io.netty.channel.ChannelPromise"] }, {"name":"connect","parameterTypes":["io.netty.channel.ChannelHandlerContext","java.net.SocketAddress","java.net.SocketAddress","io.netty.channel.ChannelPromise"] }, {"name":"deregister","parameterTypes":["io.netty.channel.ChannelHandlerContext","io.netty.channel.ChannelPromise"] }, {"name":"disconnect","parameterTypes":["io.netty.channel.ChannelHandlerContext","io.netty.channel.ChannelPromise"] }, {"name":"exceptionCaught","parameterTypes":["io.netty.channel.ChannelHandlerContext","java.lang.Throwable"] }, {"name":"flush","parameterTypes":["io.netty.channel.ChannelHandlerContext"] }, {"name":"read","parameterTypes":["io.netty.channel.ChannelHandlerContext"] }, {"name":"userEventTriggered","parameterTypes":["io.netty.channel.ChannelHandlerContext","java.lang.Object"] }, {"name":"write","parameterTypes":["io.netty.channel.ChannelHandlerContext","java.lang.Object","io.netty.channel.ChannelPromise"] }]
},
{
"name":"io.netty.channel.DefaultChannelPipeline$TailContext",
"methods":[{"name":"channelActive","parameterTypes":["io.netty.channel.ChannelHandlerContext"] }, {"name":"channelInactive","parameterTypes":["io.netty.channel.ChannelHandlerContext"] }, {"name":"channelRead","parameterTypes":["io.netty.channel.ChannelHandlerContext","java.lang.Object"] }, {"name":"channelReadComplete","parameterTypes":["io.netty.channel.ChannelHandlerContext"] }, {"name":"channelRegistered","parameterTypes":["io.netty.channel.ChannelHandlerContext"] }, {"name":"channelUnregistered","parameterTypes":["io.netty.channel.ChannelHandlerContext"] }, {"name":"channelWritabilityChanged","parameterTypes":["io.netty.channel.ChannelHandlerContext"] }, {"name":"exceptionCaught","parameterTypes":["io.netty.channel.ChannelHandlerContext","java.lang.Throwable"] }, {"name":"userEventTriggered","parameterTypes":["io.netty.channel.ChannelHandlerContext","java.lang.Object"] }]
},
{
"name":"io.netty.channel.DefaultFileRegion"
},
{
"name":"io.netty.channel.kqueue.KQueueDomainSocketChannel",
"methods":[{"name":"<init>","parameterTypes":[] }]
},
{
"name":"io.netty.channel.kqueue.KQueueEventLoop",
"fields":[{"name":"wakenUp"}]
},
{
"name":"io.netty.channel.socket.nio.NioSocketChannel",
"methods":[{"name":"<init>","parameterTypes":[] }]
},
{
"name":"io.netty.channel.unix.DatagramSocketAddress"
},
{
"name":"io.netty.channel.unix.DomainDatagramSocketAddress"
},
{
"name":"io.netty.channel.unix.FileDescriptor",
"fields":[{"name":"state"}]
},
{
"name":"io.netty.channel.unix.PeerCredentials"
},
{
"name":"io.netty.handler.codec.ByteToMessageDecoder",
"methods":[{"name":"channelInactive","parameterTypes":["io.netty.channel.ChannelHandlerContext"] }, {"name":"channelRead","parameterTypes":["io.netty.channel.ChannelHandlerContext","java.lang.Object"] }, {"name":"channelReadComplete","parameterTypes":["io.netty.channel.ChannelHandlerContext"] }, {"name":"userEventTriggered","parameterTypes":["io.netty.channel.ChannelHandlerContext","java.lang.Object"] }]
},
{
"name":"io.netty.handler.codec.MessageToByteEncoder",
"methods":[{"name":"write","parameterTypes":["io.netty.channel.ChannelHandlerContext","java.lang.Object","io.netty.channel.ChannelPromise"] }]
},
{
"name":"io.netty.util.AbstractReferenceCounted",
"fields":[{"name":"refCnt"}]
},
{
"name":"io.netty.util.DefaultAttributeMap",
"fields":[{"name":"attributes"}]
},
{
"name":"io.netty.util.NettyRuntime"
},
{
"name":"io.netty.util.Recycler$DefaultHandle",
"fields":[{"name":"state"}]
},
{
"name":"io.netty.util.ReferenceCountUtil",
"queryAllDeclaredMethods":true
},
{
"name":"io.netty.util.ResourceLeakDetector$DefaultResourceLeak",
"fields":[{"name":"droppedRecords"}, {"name":"head"}]
},
{
"name":"io.netty.util.concurrent.DefaultPromise",
"fields":[{"name":"result"}]
},
{
"name":"io.netty.util.concurrent.SingleThreadEventExecutor",
"fields":[{"name":"state"}, {"name":"threadProperties"}]
},
{
"name":"io.netty.util.internal.NativeLibraryUtil",
"methods":[{"name":"loadLibrary","parameterTypes":["java.lang.String","boolean"] }]
},
{
"name":"io.netty.util.internal.shaded.org.jctools.queues.BaseMpscLinkedArrayQueueColdProducerFields",
"fields":[{"name":"producerLimit"}]
},
{
"name":"io.netty.util.internal.shaded.org.jctools.queues.BaseMpscLinkedArrayQueueConsumerFields",
"fields":[{"name":"consumerIndex"}]
},
{
"name":"io.netty.util.internal.shaded.org.jctools.queues.BaseMpscLinkedArrayQueueProducerFields",
"fields":[{"name":"producerIndex"}]
},
{
"name":"io.netty.util.internal.shaded.org.jctools.queues.MpscArrayQueueConsumerIndexField",
"fields":[{"name":"consumerIndex"}]
},
{
"name":"io.netty.util.internal.shaded.org.jctools.queues.MpscArrayQueueProducerIndexField",
"fields":[{"name":"producerIndex"}]
},
{
"name":"io.netty.util.internal.shaded.org.jctools.queues.MpscArrayQueueProducerLimitField",
"fields":[{"name":"producerLimit"}]
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"resources": {
"includes": [
{
"pattern":"\\QMETA-INF/native/libnetty_transport_native_kqueue_aarch_64.dylib\\E"
},
{
"pattern":"\\QMETA-INF/native/libnetty_transport_native_kqueue_aarch_64.jnilib\\E"
}
],
"excludes": []
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -217,11 +217,21 @@ void stakeSnapshotsQuery() {

@Test
void stakePoolParamQuery() {
Mono<StakePoolParamQueryResult> mono = localStateQueryClient.executeQuery(new StakePoolParamsQuery(List.of("032a04334a846fdf542fd5633c9b3928998691b8276e004facbc8af1",
"0a4ed3c5cc11a044cff16f7045588c9b6f6c98f7154026a3a3f55f24")));
String poolId1 = "27aa9ad499cb47f856727913abd02dfd08bbb69dbefd1b4c098c6e96";
//String poolId2 = "1c07dbd3648fe70545170b6f6d390136c1cc66ec83326f5d94a0fd3e";
Mono<StakePoolParamQueryResult> mono = localStateQueryClient.executeQuery(new StakePoolParamsQuery(List.of(poolId1)));

StakePoolParamQueryResult result = mono.block(Duration.ofSeconds(5));
System.out.println(result);
assertThat(result.getPoolParams().get(poolId1)).isNotNull();
assertThat(result.getPoolParams().get(poolId1).getOperator()).isEqualTo(poolId1);
assertThat(result.getPoolParams().get(poolId1).getVrfKeyHash()).isEqualTo("5f97dc97196c55b7eca60909b68de9db9bedee4f04a000b27c2df4b95911c5af");
assertThat(result.getPoolParams().get(poolId1).getCost()).isNotNull();

// assertThat(result.getPoolParams().get(poolId2)).isNotNull();
// assertThat(result.getPoolParams().get(poolId2).getOperator()).isEqualTo(poolId2);
// assertThat(result.getPoolParams().get(poolId2).getVrfKeyHash()).isEqualTo("322828d4f7cfe86db0f8c3fb385e317b5415a8287963aa58c967eb3643c5ee25");
// assertThat(result.getPoolParams().get(poolId2).getCost()).isNotNull();
}

@Test
Expand Down

0 comments on commit 8e643ba

Please sign in to comment.