Search in sources :

Example 26 with FlatBufferBuilder

use of com.google.flatbuffers.FlatBufferBuilder in project nem2-sdk-java by nemtech.

the class SecretProofTransaction method generateBytes.

@Override
byte[] generateBytes() {
    FlatBufferBuilder builder = new FlatBufferBuilder();
    BigInteger deadlineBigInt = BigInteger.valueOf(getDeadline().getInstant());
    int[] fee = new int[] { 0, 0 };
    int version = (int) Long.parseLong(Integer.toHexString(getNetworkType().getValue()) + "0" + Integer.toHexString(getVersion()), 16);
    // Create Vectors
    int signatureVector = SecretProofTransactionBuffer.createSignatureVector(builder, new byte[64]);
    int signerVector = SecretProofTransactionBuffer.createSignerVector(builder, new byte[32]);
    int deadlineVector = SecretProofTransactionBuffer.createDeadlineVector(builder, UInt64.fromBigInteger(deadlineBigInt));
    int feeVector = SecretProofTransactionBuffer.createFeeVector(builder, fee);
    int secretVector = SecretProofTransactionBuffer.createSecretVector(builder, Hex.decode(secret));
    int proofVector = SecretProofTransactionBuffer.createProofVector(builder, Hex.decode(proof));
    SecretProofTransactionBuffer.startSecretProofTransactionBuffer(builder);
    SecretProofTransactionBuffer.addSize(builder, 187 + Hex.decode(proof).length);
    SecretProofTransactionBuffer.addSignature(builder, signatureVector);
    SecretProofTransactionBuffer.addSigner(builder, signerVector);
    SecretProofTransactionBuffer.addVersion(builder, version);
    SecretProofTransactionBuffer.addType(builder, getType().getValue());
    SecretProofTransactionBuffer.addFee(builder, feeVector);
    SecretProofTransactionBuffer.addDeadline(builder, deadlineVector);
    SecretProofTransactionBuffer.addHashAlgorithm(builder, hashType.getValue());
    SecretProofTransactionBuffer.addSecret(builder, secretVector);
    SecretProofTransactionBuffer.addProofSize(builder, Hex.decode(proof).length);
    SecretProofTransactionBuffer.addProof(builder, proofVector);
    int codedSecretProof = SecretProofTransactionBuffer.endSecretProofTransactionBuffer(builder);
    builder.finish(codedSecretProof);
    return schema.serialize(builder.sizedByteArray());
}
Also used : FlatBufferBuilder(com.google.flatbuffers.FlatBufferBuilder) BigInteger(java.math.BigInteger)

Example 27 with FlatBufferBuilder

use of com.google.flatbuffers.FlatBufferBuilder in project nem2-sdk-java by nemtech.

the class TransferTransaction method generateBytes.

byte[] generateBytes() {
    FlatBufferBuilder builder = new FlatBufferBuilder();
    BigInteger deadlineBigInt = BigInteger.valueOf(getDeadline().getInstant());
    int[] fee = new int[] { 0, 0 };
    int version = (int) Long.parseLong(Integer.toHexString(getNetworkType().getValue()) + "0" + Integer.toHexString(getVersion()), 16);
    // Create Message
    byte[] bytePayload = message.getPayload().getBytes(StandardCharsets.UTF_8);
    int payload = MessageBuffer.createPayloadVector(builder, bytePayload);
    MessageBuffer.startMessageBuffer(builder);
    MessageBuffer.addType(builder, message.getType());
    MessageBuffer.addPayload(builder, payload);
    int message = MessageBuffer.endMessageBuffer(builder);
    // Create Mosaics
    int[] mosaicBuffers = new int[mosaics.size()];
    for (int i = 0; i < mosaics.size(); ++i) {
        Mosaic mosaic = mosaics.get(i);
        int id = MosaicBuffer.createIdVector(builder, UInt64.fromBigInteger(mosaic.getId().getId()));
        int amount = MosaicBuffer.createAmountVector(builder, UInt64.fromBigInteger(mosaic.getAmount()));
        MosaicBuffer.startMosaicBuffer(builder);
        MosaicBuffer.addId(builder, id);
        MosaicBuffer.addAmount(builder, amount);
        mosaicBuffers[i] = MosaicBuffer.endMosaicBuffer(builder);
    }
    byte[] address = new Base32().decode(getRecipient().plain().getBytes(StandardCharsets.UTF_8));
    // Create Vectors
    int signatureVector = TransferTransactionBuffer.createSignatureVector(builder, new byte[64]);
    int signerVector = TransferTransactionBuffer.createSignerVector(builder, new byte[32]);
    int deadlineVector = TransferTransactionBuffer.createDeadlineVector(builder, UInt64.fromBigInteger(deadlineBigInt));
    int feeVector = TransferTransactionBuffer.createFeeVector(builder, fee);
    int recipientVector = TransferTransactionBuffer.createRecipientVector(builder, address);
    int mosaicsVector = TransferTransactionBuffer.createMosaicsVector(builder, mosaicBuffers);
    // replace by the all numbers sum or add a comment explaining this
    int fixSize = 149;
    TransferTransactionBuffer.startTransferTransactionBuffer(builder);
    TransferTransactionBuffer.addSize(builder, fixSize + (16 * mosaics.size()) + bytePayload.length);
    TransferTransactionBuffer.addSignature(builder, signatureVector);
    TransferTransactionBuffer.addSigner(builder, signerVector);
    TransferTransactionBuffer.addVersion(builder, version);
    TransferTransactionBuffer.addType(builder, getType().getValue());
    TransferTransactionBuffer.addFee(builder, feeVector);
    TransferTransactionBuffer.addDeadline(builder, deadlineVector);
    TransferTransactionBuffer.addRecipient(builder, recipientVector);
    TransferTransactionBuffer.addNumMosaics(builder, mosaics.size());
    TransferTransactionBuffer.addMessageSize(builder, bytePayload.length + 1);
    TransferTransactionBuffer.addMessage(builder, message);
    TransferTransactionBuffer.addMosaics(builder, mosaicsVector);
    int codedTransfer = TransferTransactionBuffer.endTransferTransactionBuffer(builder);
    builder.finish(codedTransfer);
    return schema.serialize(builder.sizedByteArray());
}
Also used : FlatBufferBuilder(com.google.flatbuffers.FlatBufferBuilder) BigInteger(java.math.BigInteger) Base32(org.apache.commons.codec.binary.Base32) Mosaic(io.nem.sdk.model.mosaic.Mosaic)

Example 28 with FlatBufferBuilder

use of com.google.flatbuffers.FlatBufferBuilder in project nd4j by deeplearning4j.

the class ByteOrderTests method testScalarEncoding.

@Test
public void testScalarEncoding() {
    val scalar = Nd4j.trueScalar(2.0f);
    FlatBufferBuilder bufferBuilder = new FlatBufferBuilder(0);
    val fb = scalar.toFlatArray(bufferBuilder);
    bufferBuilder.finish(fb);
    val db = bufferBuilder.dataBuffer();
    val flat = FlatArray.getRootAsFlatArray(db);
    val restored = Nd4j.createFromFlatArray(flat);
    assertEquals(scalar, restored);
}
Also used : lombok.val(lombok.val) FlatBufferBuilder(com.google.flatbuffers.FlatBufferBuilder) Test(org.junit.Test) BaseNd4jTest(org.nd4j.linalg.BaseNd4jTest)

Example 29 with FlatBufferBuilder

use of com.google.flatbuffers.FlatBufferBuilder in project nd4j by deeplearning4j.

the class ByteOrderTests method testByteArrayOrder3.

@Test
public void testByteArrayOrder3() {
    val original = Nd4j.linspace(1, 25, 25).reshape('f', 5, 5);
    val bufferBuilder = new FlatBufferBuilder(0);
    int array = original.toFlatArray(bufferBuilder);
    bufferBuilder.finish(array);
    val flatArray = FlatArray.getRootAsFlatArray(bufferBuilder.dataBuffer());
    val restored = Nd4j.createFromFlatArray(flatArray);
    assertEquals(original, restored);
}
Also used : lombok.val(lombok.val) FlatBufferBuilder(com.google.flatbuffers.FlatBufferBuilder) Test(org.junit.Test) BaseNd4jTest(org.nd4j.linalg.BaseNd4jTest)

Example 30 with FlatBufferBuilder

use of com.google.flatbuffers.FlatBufferBuilder in project nd4j by deeplearning4j.

the class ByteOrderTests method testByteArrayOrder2.

@Test
public void testByteArrayOrder2() {
    val original = Nd4j.linspace(1, 25, 25).reshape(5, 5);
    val bufferBuilder = new FlatBufferBuilder(0);
    int array = original.toFlatArray(bufferBuilder);
    bufferBuilder.finish(array);
    val flatArray = FlatArray.getRootAsFlatArray(bufferBuilder.dataBuffer());
    val restored = Nd4j.createFromFlatArray(flatArray);
    assertEquals(original, restored);
}
Also used : lombok.val(lombok.val) FlatBufferBuilder(com.google.flatbuffers.FlatBufferBuilder) Test(org.junit.Test) BaseNd4jTest(org.nd4j.linalg.BaseNd4jTest)

Aggregations

FlatBufferBuilder (com.google.flatbuffers.FlatBufferBuilder)51 DisplayName (org.junit.jupiter.api.DisplayName)14 Test (org.junit.jupiter.api.Test)14 BigInteger (java.math.BigInteger)10 A6ConfigRequest (org.apache.apisix.plugin.runner.A6ConfigRequest)9 HttpRequest (org.apache.apisix.plugin.runner.HttpRequest)9 ByteBuffer (java.nio.ByteBuffer)8 EmbeddedChannel (io.netty.channel.embedded.EmbeddedChannel)7 A6ConfigResponse (org.apache.apisix.plugin.runner.A6ConfigResponse)7 Test (org.junit.Test)7 Req (io.github.api7.A6.PrepareConf.Req)5 A6Conf (org.apache.apisix.plugin.runner.A6Conf)5 ByteStringAccess (com.google.protobuf.ByteStringAccess)3 TIntArrayList (gnu.trove.list.array.TIntArrayList)3 UncheckedDeephavenException (io.deephaven.UncheckedDeephavenException)3 ChunkType (io.deephaven.chunk.ChunkType)3 Map (java.util.Map)3 lombok.val (lombok.val)3 ExtraInfoRequest (org.apache.apisix.plugin.runner.ExtraInfoRequest)3 BaseNd4jTest (org.nd4j.linalg.BaseNd4jTest)3