Skip to content

Commit

Permalink
Cleanup and fix tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
MilkywayPirate committed Sep 21, 2023
1 parent 60d5611 commit a398ef3
Show file tree
Hide file tree
Showing 34 changed files with 182 additions and 231 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,8 @@
import com.concordium.sdk.transactions.Signature;
import com.concordium.sdk.transactions.TransferPayload;
import com.concordium.sdk.transactions.TransferWithMemoPayload;
import com.concordium.sdk.transactions.UpdateContractPayload;
import com.concordium.sdk.transactions.*;
import com.concordium.sdk.transactions.UpdateContract;
import com.concordium.sdk.transactions.smartcontracts.WasmModule;
import com.concordium.sdk.transactions.smartcontracts.WasmModuleVersion;
import com.concordium.sdk.types.Timestamp;
Expand Down Expand Up @@ -644,7 +644,8 @@ static AccountTransaction to(com.concordium.grpc.v2.AccountTransaction transacti
.builderBlockItem()
.header(to(transaction.getHeader(), deployModulePayload.getBytes().length))
.signature(to(transaction.getSignature()))
.payload(deployModulePayload)
.payload(DeployModule.builder().module(deployModulePayload).build())
.maxEnergyCost(UInt64.from(transaction.getHeader().getEnergyAmount().getValue()))
.build();
}
case INIT_CONTRACT: {
Expand All @@ -653,10 +654,11 @@ static AccountTransaction to(com.concordium.grpc.v2.AccountTransaction transacti
.header(to(transaction.getHeader(), initContractPayload.getBytes().length))
.signature(to(transaction.getSignature()))
.payload(initContractPayload)
.maxEnergyCost(UInt64.from(transaction.getHeader().getEnergyAmount().getValue()))
.build();
}
case UPDATE_CONTRACT:
final UpdateContractPayload updateContractPayload = to(payload.getUpdateContract());
final UpdateContract updateContractPayload = to(payload.getUpdateContract());
return UpdateContractTransaction.builderAccountTransactionBlockItem()
.header(to(transaction.getHeader(), updateContractPayload.getRawPayloadBytes().length))
.signature(to(transaction.getSignature()))
Expand Down Expand Up @@ -719,8 +721,8 @@ static Data to(RegisteredData registerData) {
return Data.from(registerData.getValue().toByteArray());
}

static UpdateContractPayload to(com.concordium.grpc.v2.UpdateContractPayload updateContract) {
return UpdateContractPayload.from(
static UpdateContract to(com.concordium.grpc.v2.UpdateContractPayload updateContract) {
return UpdateContract.from(
to(updateContract.getAmount()),
to(updateContract.getAddress()),
ReceiveName.parse(updateContract.getReceiveName().getValue()),
Expand Down Expand Up @@ -871,7 +873,7 @@ static SendBlockItemRequest to(AccountTransaction accountTransaction) {
.setAccountTransaction(com.concordium.grpc.v2.AccountTransaction.newBuilder()
.setHeader(to(accountTransaction.getHeader()))
.setPayload(AccountTransactionPayload.newBuilder()
.setRawPayload(ByteString.copyFrom(accountTransaction.getPayload().getRawPayloadBytes()))
.setRawPayload(ByteString.copyFrom(accountTransaction.getPayload().getBytes()))
.build())
.setSignature(to(accountTransaction.getSignature()))
.build())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,14 @@ public class AccountTransaction extends BlockItem {
*/
private final Payload payload;

/**
* Constructor serializing an account transaction
* @param sender sender of the transaction
* @param nonce account nonce
* @param expiry the expiry of the transaction
* @param signer the {@link Signer} of the transaction
* @param payload the payload of the transaction
*/
AccountTransaction(
@NonNull final AccountAddress sender,
@NonNull final AccountNonce nonce,
Expand All @@ -40,6 +48,12 @@ public class AccountTransaction extends BlockItem {
.signWith(signer));
}

/**
* Constructor for deserializing a transaction
* @param signature the signature
* @param header the header
* @param payload the payload
*/
public AccountTransaction(
@NonNull final TransactionSignature signature,
@NonNull final TransactionHeader header,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public TransactionType getTransactionType() {
}

@Override
public byte[] getRawPayloadBytes() {
protected byte[] getRawPayloadBytes() {
return payload.getBytes();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public TransactionType getTransactionType() {
}

@Override
public byte[] getRawPayloadBytes() {
protected byte[] getRawPayloadBytes() {
return payload.getBytes();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,33 +3,34 @@
import com.concordium.sdk.transactions.smartcontracts.WasmModule;
import com.concordium.sdk.types.UInt64;
import lombok.Builder;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.NonNull;

@Getter
@EqualsAndHashCode(callSuper = true)
public class DeployModule extends Payload {
/**
* A compiled Smart Contract Module in WASM with source and version.
*/
private final WasmModule module;

private final UInt64 maxEnergyCost;
private UInt64 maxEnergyCost;

private DeployModule(@NonNull final WasmModule module, @NonNull final UInt64 maxEnergyCost) {

private DeployModule(@NonNull final WasmModule module) {
this.module = module;
this.maxEnergyCost = maxEnergyCost;
}

/**
* It creates a new instance of the DeployModule class.
*
* @param module The module to be deployed.
* @param maxEnergyCost The maximum amount of energy that can be consumed by the contract.
* @return A new DeployModule object.
*/
@Builder
static DeployModule createNew(final WasmModule module, final UInt64 maxEnergyCost) {
return new DeployModule(module, maxEnergyCost);
static DeployModule createNew(final WasmModule module) {
return new DeployModule(module);
}

@Override
Expand All @@ -43,7 +44,12 @@ public TransactionType getTransactionType() {
}

@Override
public byte[] getRawPayloadBytes() {
protected byte[] getRawPayloadBytes() {
return module.getBytes();
}

public DeployModule withMaxEnergyCost(UInt64 maxEnergyCost) {
this.maxEnergyCost = maxEnergyCost;
return this;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,15 @@ private DeployModuleTransaction(
@NonNull final TransactionSigner signer,
@NonNull final WasmModule module,
@NonNull final UInt64 maxEnergyCost) {
super(sender, nonce, expiry, signer, DeployModule.createNew(module, maxEnergyCost));
super(sender, nonce, expiry, signer, DeployModule.createNew(module).withMaxEnergyCost(maxEnergyCost));
}

private DeployModuleTransaction(
final @NonNull TransactionHeader header,
final @NonNull TransactionSignature signature,
final @NonNull WasmModule payload,
final @NonNull DeployModule payload,
final @NonNull UInt64 maxEnergyCost) {
super(header, signature, DeployModule.createNew(payload, maxEnergyCost));
super(header, signature, payload.withMaxEnergyCost(maxEnergyCost));
}

/**
Expand Down Expand Up @@ -71,7 +71,7 @@ public static DeployModuleTransaction from(final AccountAddress sender,
static DeployModuleTransaction from(
final TransactionHeader header,
final TransactionSignature signature,
final WasmModule payload,
final DeployModule payload,
final UInt64 maxEnergyCost) {
try {
return new DeployModuleTransaction(header, signature, payload, maxEnergyCost);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public TransactionType getTransactionType() {
}

@Override
public byte[] getRawPayloadBytes() {
protected byte[] getRawPayloadBytes() {
val toAddress = receiver.getBytes();
byte[] dataBytes = data.getBytes();
val buffer = ByteBuffer.allocate(toAddress.length + dataBytes.length);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public TransactionType getTransactionType() {
}

@Override
public byte[] getRawPayloadBytes() {
protected byte[] getRawPayloadBytes() {
return payload.getBytes();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public TransactionType getTransactionType() {
}

@Override
public byte[] getRawPayloadBytes() {
protected byte[] getRawPayloadBytes() {
return payload.getBytes();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.concordium.sdk.types.UInt16;
import com.fasterxml.jackson.annotation.JsonCreator;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.ToString;
import lombok.val;
Expand All @@ -16,6 +17,7 @@
*/
@Getter
@ToString
@EqualsAndHashCode
public final class InitName {
/**
* Name of the contract
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.concordium.sdk.types.UInt16;
import com.fasterxml.jackson.annotation.JsonCreator;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.ToString;
import lombok.val;
Expand All @@ -18,6 +19,7 @@

@Getter
@ToString
@EqualsAndHashCode
public final class Parameter {
public static final Parameter EMPTY = Parameter.from(new byte[0]);
private final byte[] bytes;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,13 @@ public boolean equals(Object obj) {
}

/**
* Get the bytes representation of the payload
* Get the serialized payload.
* The actual payload is prepended with a tag (a byte) which
* indicates the {@link TransactionType}
*
* @return byte[]
* @return the serialized payload (including the type tag).
*/
final byte[] getBytes() {
public final byte[] getBytes() {
val payloadBytes = getRawPayloadBytes();
val buffer = ByteBuffer.allocate(TransactionType.BYTES + payloadBytes.length);
buffer.put(getTransactionType().getValue());
Expand Down Expand Up @@ -114,6 +116,11 @@ private static UInt64 calculateEnergyCost(int noOfSignatures,

public abstract TransactionType getTransactionType();

public abstract byte[] getRawPayloadBytes();
/**
* This must return the raw payload i.e., the
* payload only. The tag will be prepended by {@link Payload#getBytes()}
* @return the raw serialized payload.
*/
protected abstract byte[] getRawPayloadBytes();

}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import java.util.Arrays;

@ToString
@EqualsAndHashCode(callSuper = true)
@EqualsAndHashCode(callSuper = false)
public class RawPayload extends Payload {

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.concordium.sdk.types.UInt16;
import com.fasterxml.jackson.annotation.JsonCreator;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.ToString;
import lombok.val;
Expand All @@ -16,6 +17,7 @@

@Getter
@ToString
@EqualsAndHashCode
public final class ReceiveName {
/**
* The name of the contract.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public TransactionType getTransactionType() {
}

@Override
public byte[] getRawPayloadBytes() {
protected byte[] getRawPayloadBytes() {
return data.getBytes();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public TransactionType getTransactionType() {
}

@Override
public byte[] getRawPayloadBytes() {
protected byte[] getRawPayloadBytes() {
return payload.getBytes();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public TransactionType getTransactionType() {
}

@Override
public byte[] getRawPayloadBytes() {
protected byte[] getRawPayloadBytes() {
val scheduleLen = amount.length;
val scheduleBufferSize = UInt64.BYTES * scheduleLen * 2;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public TransactionType getTransactionType() {
}

@Override
public byte[] getRawPayloadBytes() {
protected byte[] getRawPayloadBytes() {
val schedule_len = amount.length;
val schedule_buffer_size = UInt64.BYTES * schedule_len * 2;
val buffer = ByteBuffer.allocate(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public TransactionType getTransactionType() {
}

@Override
public byte[] getRawPayloadBytes() {
protected byte[] getRawPayloadBytes() {
return amount.getBytes();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public TransactionType getTransactionType() {
}

@Override
public byte[] getRawPayloadBytes() {
protected byte[] getRawPayloadBytes() {
val proofBytes = this.proof.getBytes();
val remainingAmountBytes = this.remainingAmount.getBytes();
val buffer = ByteBuffer.allocate(remainingAmountBytes.length
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public TransactionType getTransactionType() {
}

@Override
public byte[] getRawPayloadBytes() {
protected byte[] getRawPayloadBytes() {
return payload.getBytes();
}
}
Loading

0 comments on commit a398ef3

Please sign in to comment.