Search in sources :

Example 1 with FlatBufferBuilder

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

the class LockFundsTransaction 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 = LockFundsTransactionBuffer.createSignatureVector(builder, new byte[64]);
    int signerVector = LockFundsTransactionBuffer.createSignerVector(builder, new byte[32]);
    int deadlineVector = LockFundsTransactionBuffer.createDeadlineVector(builder, UInt64.fromBigInteger(deadlineBigInt));
    int feeVector = LockFundsTransactionBuffer.createFeeVector(builder, fee);
    int mosaicIdVector = LockFundsTransactionBuffer.createMosaicIdVector(builder, UInt64.fromBigInteger(mosaic.getId().getId()));
    int mosaicAmountVector = LockFundsTransactionBuffer.createMosaicAmountVector(builder, UInt64.fromBigInteger(mosaic.getAmount()));
    int durationVector = LockFundsTransactionBuffer.createDurationVector(builder, UInt64.fromBigInteger(duration));
    int hashVector = LockFundsTransactionBuffer.createHashVector(builder, Hex.decode(signedTransaction.getHash()));
    LockFundsTransactionBuffer.startLockFundsTransactionBuffer(builder);
    LockFundsTransactionBuffer.addSize(builder, 176);
    LockFundsTransactionBuffer.addSignature(builder, signatureVector);
    LockFundsTransactionBuffer.addSigner(builder, signerVector);
    LockFundsTransactionBuffer.addVersion(builder, version);
    LockFundsTransactionBuffer.addType(builder, getType().getValue());
    LockFundsTransactionBuffer.addFee(builder, feeVector);
    LockFundsTransactionBuffer.addDeadline(builder, deadlineVector);
    LockFundsTransactionBuffer.addMosaicId(builder, mosaicIdVector);
    LockFundsTransactionBuffer.addMosaicAmount(builder, mosaicAmountVector);
    LockFundsTransactionBuffer.addDuration(builder, durationVector);
    LockFundsTransactionBuffer.addHash(builder, hashVector);
    int codedLockFunds = LockFundsTransactionBuffer.endLockFundsTransactionBuffer(builder);
    builder.finish(codedLockFunds);
    return schema.serialize(builder.sizedByteArray());
}
Also used : FlatBufferBuilder(com.google.flatbuffers.FlatBufferBuilder) BigInteger(java.math.BigInteger)

Example 2 with FlatBufferBuilder

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

the class MosaicSupplyChangeTransaction 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 Vectors
    int signatureVector = MosaicSupplyChangeTransactionBuffer.createSignatureVector(builder, new byte[64]);
    int signerVector = MosaicSupplyChangeTransactionBuffer.createSignerVector(builder, new byte[32]);
    int deadlineVector = MosaicSupplyChangeTransactionBuffer.createDeadlineVector(builder, UInt64.fromBigInteger(deadlineBigInt));
    int feeVector = MosaicSupplyChangeTransactionBuffer.createFeeVector(builder, fee);
    int mosaicIdVector = MosaicSupplyChangeTransactionBuffer.createMosaicIdVector(builder, UInt64.fromBigInteger(mosaicId.getId()));
    int deltaVector = MosaicSupplyChangeTransactionBuffer.createDeltaVector(builder, UInt64.fromBigInteger(delta));
    // replace by the all numbers sum or add a comment explaining this
    int fixSize = 137;
    MosaicSupplyChangeTransactionBuffer.startMosaicSupplyChangeTransactionBuffer(builder);
    MosaicSupplyChangeTransactionBuffer.addSize(builder, fixSize);
    MosaicSupplyChangeTransactionBuffer.addSignature(builder, signatureVector);
    MosaicSupplyChangeTransactionBuffer.addSigner(builder, signerVector);
    MosaicSupplyChangeTransactionBuffer.addVersion(builder, version);
    MosaicSupplyChangeTransactionBuffer.addType(builder, getType().getValue());
    MosaicSupplyChangeTransactionBuffer.addFee(builder, feeVector);
    MosaicSupplyChangeTransactionBuffer.addDeadline(builder, deadlineVector);
    MosaicSupplyChangeTransactionBuffer.addMosaicId(builder, mosaicIdVector);
    MosaicSupplyChangeTransactionBuffer.addDirection(builder, mosaicSupplyType.getValue());
    MosaicSupplyChangeTransactionBuffer.addDelta(builder, deltaVector);
    int codedTransaction = MosaicSupplyChangeTransactionBuffer.endMosaicSupplyChangeTransactionBuffer(builder);
    builder.finish(codedTransaction);
    return schema.serialize(builder.sizedByteArray());
}
Also used : FlatBufferBuilder(com.google.flatbuffers.FlatBufferBuilder) BigInteger(java.math.BigInteger)

Example 3 with FlatBufferBuilder

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

the class RegisterNamespaceTransaction 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 Vectors
    int signatureVector = ProvisionNamespaceTransactionBuffer.createSignatureVector(builder, new byte[64]);
    int signerVector = ProvisionNamespaceTransactionBuffer.createSignerVector(builder, new byte[32]);
    int deadlineVector = ProvisionNamespaceTransactionBuffer.createDeadlineVector(builder, UInt64.fromBigInteger(deadlineBigInt));
    int feeVector = ProvisionNamespaceTransactionBuffer.createFeeVector(builder, fee);
    int namespaceIdVector = ProvisionNamespaceTransactionBuffer.createNamespaceIdVector(builder, UInt64.fromBigInteger(namespaceId.getId()));
    int durationParentIdVector = ProvisionNamespaceTransactionBuffer.createDurationParentIdVector(builder, getNamespaceType() == NamespaceType.RootNamespace ? UInt64.fromBigInteger(duration.get()) : UInt64.fromBigInteger(parentId.get().getId()));
    // replace by the all numbers sum or add a comment explaining this
    int fixSize = 138;
    int name = builder.createString(namespaceName);
    ProvisionNamespaceTransactionBuffer.startProvisionNamespaceTransactionBuffer(builder);
    ProvisionNamespaceTransactionBuffer.addSize(builder, fixSize + namespaceName.length());
    ProvisionNamespaceTransactionBuffer.addSignature(builder, signatureVector);
    ProvisionNamespaceTransactionBuffer.addSigner(builder, signerVector);
    ProvisionNamespaceTransactionBuffer.addVersion(builder, version);
    ProvisionNamespaceTransactionBuffer.addType(builder, getType().getValue());
    ProvisionNamespaceTransactionBuffer.addFee(builder, feeVector);
    ProvisionNamespaceTransactionBuffer.addDeadline(builder, deadlineVector);
    ProvisionNamespaceTransactionBuffer.addNamespaceType(builder, getNamespaceType().getValue());
    ProvisionNamespaceTransactionBuffer.addDurationParentId(builder, durationParentIdVector);
    ProvisionNamespaceTransactionBuffer.addNamespaceId(builder, namespaceIdVector);
    ProvisionNamespaceTransactionBuffer.addNamespaceNameSize(builder, namespaceName.length());
    ProvisionNamespaceTransactionBuffer.addNamespaceName(builder, name);
    int codedTransaction = ProvisionNamespaceTransactionBuffer.endProvisionNamespaceTransactionBuffer(builder);
    builder.finish(codedTransaction);
    return schema.serialize(builder.sizedByteArray());
}
Also used : FlatBufferBuilder(com.google.flatbuffers.FlatBufferBuilder) BigInteger(java.math.BigInteger)

Example 4 with FlatBufferBuilder

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

the class SecretLockTransaction 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 = SecretLockTransactionBuffer.createSignatureVector(builder, new byte[64]);
    int signerVector = SecretLockTransactionBuffer.createSignerVector(builder, new byte[32]);
    int deadlineVector = SecretLockTransactionBuffer.createDeadlineVector(builder, UInt64.fromBigInteger(deadlineBigInt));
    int feeVector = SecretLockTransactionBuffer.createFeeVector(builder, fee);
    int mosaicIdVector = SecretLockTransactionBuffer.createMosaicIdVector(builder, UInt64.fromBigInteger(mosaic.getId().getId()));
    int mosaicAmountVector = SecretLockTransactionBuffer.createMosaicAmountVector(builder, UInt64.fromBigInteger(mosaic.getAmount()));
    int durationVector = SecretLockTransactionBuffer.createDurationVector(builder, UInt64.fromBigInteger(duration));
    int secretVector = SecretLockTransactionBuffer.createSecretVector(builder, Hex.decode(secret));
    byte[] address = new Base32().decode(getRecipient().plain().getBytes(StandardCharsets.UTF_8));
    int recipientVector = SecretLockTransactionBuffer.createRecipientVector(builder, address);
    SecretLockTransactionBuffer.startSecretLockTransactionBuffer(builder);
    SecretLockTransactionBuffer.addSize(builder, 234);
    SecretLockTransactionBuffer.addSignature(builder, signatureVector);
    SecretLockTransactionBuffer.addSigner(builder, signerVector);
    SecretLockTransactionBuffer.addVersion(builder, version);
    SecretLockTransactionBuffer.addType(builder, getType().getValue());
    SecretLockTransactionBuffer.addFee(builder, feeVector);
    SecretLockTransactionBuffer.addDeadline(builder, deadlineVector);
    SecretLockTransactionBuffer.addMosaicId(builder, mosaicIdVector);
    SecretLockTransactionBuffer.addMosaicAmount(builder, mosaicAmountVector);
    SecretLockTransactionBuffer.addDuration(builder, durationVector);
    SecretLockTransactionBuffer.addHashAlgorithm(builder, hashType.getValue());
    SecretLockTransactionBuffer.addSecret(builder, secretVector);
    SecretLockTransactionBuffer.addRecipient(builder, recipientVector);
    int codedSecretLock = SecretLockTransactionBuffer.endSecretLockTransactionBuffer(builder);
    builder.finish(codedSecretLock);
    return schema.serialize(builder.sizedByteArray());
}
Also used : FlatBufferBuilder(com.google.flatbuffers.FlatBufferBuilder) BigInteger(java.math.BigInteger) Base32(org.apache.commons.codec.binary.Base32)

Example 5 with FlatBufferBuilder

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

the class SameDiff method asFlatBuffers.

/**
 * This method exports given SameDiff instance into FlatBuffers
 *
 * @param configuration - ExecutorConfiguration to be embedded into serialized graph
 * @return
 */
public ByteBuffer asFlatBuffers(@NonNull ExecutorConfiguration configuration) {
    Nd4j.getExecutioner().commit();
    FlatBufferBuilder bufferBuilder = new FlatBufferBuilder(1024);
    val idCounter = new AtomicInteger(0);
    val flatVariables = new ArrayList<Integer>();
    val flatOffsets = new ArrayList<Integer>();
    val flatNodes = new ArrayList<Integer>();
    // first of all we build VariableSpace dump
    List<SDVariable> variableList = new ArrayList<>(variables());
    val reverseMap = new LinkedHashMap<String, Integer>();
    val forwardMap = new LinkedHashMap<String, Integer>();
    val framesMap = new LinkedHashMap<String, Integer>();
    int idx = 0;
    for (val variable : variables()) {
        log.debug("Exporting variable: [{}]", variable.getVarName());
        if (variable.getArr() == null || variable.getShape() == null) {
            // addAsPlaceHolder(variable.getVarName());
            continue;
        }
        val pair = parseVariable(variable.getVarName());
        reverseMap.put(pair.getFirst(), idCounter.incrementAndGet());
        log.debug("Adding [{}] as [{}]", pair.getFirst(), idCounter.get());
        val arr = variable.getArr();
        int name = bufferBuilder.createString(variable.getVarName());
        int array = arr.toFlatArray(bufferBuilder);
        int id = IntPair.createIntPair(bufferBuilder, idCounter.get(), 0);
        int flatVariable = FlatVariable.createFlatVariable(bufferBuilder, id, name, 0, array, -1);
        flatVariables.add(flatVariable);
    }
    // add functions
    for (val func : functionInstancesById.values()) {
        flatNodes.add(asFlatNode(func, bufferBuilder, variableList, reverseMap, forwardMap, framesMap, idCounter));
    }
    // we're dumping scopes now
    for (val scope : sameDiffFunctionInstances.entrySet()) {
        flatNodes.add(asFlatNode(scope.getKey(), scope.getValue(), bufferBuilder));
        val currVarList = new ArrayList<SDVariable>(scope.getValue().variables());
        // converting all ops from node
        for (val node : scope.getValue().variables()) {
            INDArray arr = node.getArr();
            if (arr == null) {
                // arr = otherArr;
                continue;
            }
            int name = bufferBuilder.createString(node.getVarName());
            int array = arr.toFlatArray(bufferBuilder);
            int id = IntPair.createIntPair(bufferBuilder, ++idx, 0);
            val pair = parseVariable(node.getVarName());
            reverseMap.put(pair.getFirst(), idx);
            log.debug("Adding [{}] as [{}]", pair.getFirst(), idx);
            int flatVariable = FlatVariable.createFlatVariable(bufferBuilder, id, name, 0, array, -1);
            flatVariables.add(flatVariable);
        }
        // add functions
        for (val func : scope.getValue().functionInstancesById.values()) {
            flatNodes.add(asFlatNode(func, bufferBuilder, currVarList, reverseMap, forwardMap, framesMap, idCounter));
        }
    }
    int outputsOffset = FlatGraph.createVariablesVector(bufferBuilder, Ints.toArray(flatOffsets));
    int variablesOffset = FlatGraph.createVariablesVector(bufferBuilder, Ints.toArray(flatVariables));
    int nodesOffset = FlatGraph.createNodesVector(bufferBuilder, Ints.toArray(flatNodes));
    int fg = FlatGraph.createFlatGraph(bufferBuilder, 119, variablesOffset, nodesOffset, outputsOffset, configuration.getFlatConfiguration(bufferBuilder));
    bufferBuilder.finish(fg);
    return bufferBuilder.dataBuffer();
}
Also used : INDArray(org.nd4j.linalg.api.ndarray.INDArray) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) FlatBufferBuilder(com.google.flatbuffers.FlatBufferBuilder)

Aggregations

FlatBufferBuilder (com.google.flatbuffers.FlatBufferBuilder)18 BigInteger (java.math.BigInteger)9 Test (org.junit.Test)6 lombok.val (lombok.val)4 BaseNd4jTest (org.nd4j.linalg.BaseNd4jTest)4 ByteBuffer (java.nio.ByteBuffer)2 Base32 (org.apache.commons.codec.binary.Base32)2 Nullable (androidx.annotation.Nullable)1 SgEpisode2Numbers (com.battlelancer.seriesguide.provider.SgEpisode2Numbers)1 Mosaic (io.nem.sdk.model.mosaic.Mosaic)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 INDArray (org.nd4j.linalg.api.ndarray.INDArray)1