Skip to content

Commit

Permalink
issues/262 - Push up common code in to AbstractStoredVersionedContract
Browse files Browse the repository at this point in the history
  • Loading branch information
meywood committed Apr 24, 2024
1 parent 09ed5dd commit 81d62cc
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 93 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package com.casper.sdk.model.deploy.executabledeploy;

import com.casper.sdk.exception.NoSuchTypeException;
import com.casper.sdk.model.clvalue.AbstractCLValue;
import com.casper.sdk.model.clvalue.CLValueOption;
import com.casper.sdk.model.clvalue.CLValueU32;
import com.casper.sdk.model.clvalue.serde.Target;
import com.casper.sdk.model.deploy.NamedArg;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
import dev.oak3.sbs4j.SerializerBuffer;
import dev.oak3.sbs4j.exception.ValueSerializationException;
import lombok.*;

import java.util.List;
import java.util.Optional;

/**
* Base class for versioned contracts.
*
* @author [email protected]
*/
@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
abstract class AbstractStoredVersionedContract extends ExecutableDeployItemWithEntryPoint {

/** contract version */
@JsonProperty("version")
private Long version;

/** Entry Point */
@JsonProperty("entry_point")
private String entryPoint;

/** @see NamedArg */
private List<NamedArg<?>> args;

@JsonIgnore
public Optional<Long> getVersion() {
return Optional.ofNullable(version);
}

@Override
public void serialize(final SerializerBuffer ser, final Target target) throws NoSuchTypeException, ValueSerializationException {
ser.writeU8(getOrder());
serializeCustom(ser);

final Optional<AbstractCLValue<?, ?>> innerValue;
if (version == null) {
innerValue = Optional.empty();
} else {
innerValue = Optional.of(new CLValueU32(version));
}
new CLValueOption(innerValue).serialize(ser, Target.JSON);

ser.writeString(getEntryPoint());
serializeNamedArgs(ser, target);
}

protected abstract void serializeCustom(final SerializerBuffer ser);
}
Original file line number Diff line number Diff line change
@@ -1,23 +1,12 @@
package com.casper.sdk.model.deploy.executabledeploy;

import com.casper.sdk.exception.NoSuchTypeException;
import com.casper.sdk.model.clvalue.AbstractCLValue;
import com.casper.sdk.model.clvalue.CLValueOption;
import com.casper.sdk.model.clvalue.CLValueU32;
import com.casper.sdk.model.clvalue.serde.Target;
import com.casper.sdk.model.deploy.NamedArg;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonTypeName;
import dev.oak3.sbs4j.SerializerBuffer;
import dev.oak3.sbs4j.exception.ValueSerializationException;
import lombok.*;

import java.util.List;
import java.util.Optional;

/**
* Abstract Executable Deploy Item containing the StoredVersionedContractByHash.
* Executable Deploy Item containing the StoredVersionedContractByHash.
*
* @author Alexandre Carvalho
* @author Andre Bertolace
Expand All @@ -30,51 +19,21 @@
@AllArgsConstructor
@NoArgsConstructor
@JsonTypeName("StoredVersionedContractByHash")
public class StoredVersionedContractByHash extends ExecutableDeployItemWithEntryPoint {
public class StoredVersionedContractByHash extends AbstractStoredVersionedContract {

/** Hex-encoded Hash */
@JsonProperty("hash")
private String hash;

/** contract version */
@JsonProperty("version")
private Long version;

/** Entry Point */
@JsonProperty("entry_point")
private String entryPoint;

/** @see NamedArg */
private List<NamedArg<?>> args;

/** {@link ExecutableDeployItem} order 3 */
@Override
public byte getOrder() {
return 0x3;
}

@JsonIgnore
public Optional<Long> getVersion() {
return Optional.ofNullable(version);
}

/**
* Implements the StoredVersionedContractByHash encoder
*/
@Override
public void serialize(final SerializerBuffer ser, final Target target) throws NoSuchTypeException, ValueSerializationException {
ser.writeU8(getOrder());
protected void serializeCustom(final SerializerBuffer ser) {
ser.writeString(getHash());

final Optional<AbstractCLValue<?, ?>> innerValue;
if (version == null) {
innerValue = Optional.empty();
} else {
innerValue = Optional.of(new CLValueU32(version));
}
new CLValueOption(innerValue).serialize(ser, Target.JSON);

ser.writeString(getEntryPoint());
serializeNamedArgs(ser, target);
}
}
Original file line number Diff line number Diff line change
@@ -1,23 +1,11 @@
package com.casper.sdk.model.deploy.executabledeploy;

import com.casper.sdk.exception.NoSuchTypeException;
import com.casper.sdk.model.clvalue.AbstractCLValue;
import com.casper.sdk.model.clvalue.CLValueOption;
import com.casper.sdk.model.clvalue.CLValueU32;
import com.casper.sdk.model.clvalue.serde.Target;
import com.casper.sdk.model.deploy.NamedArg;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonTypeName;
import dev.oak3.sbs4j.SerializerBuffer;
import dev.oak3.sbs4j.exception.ValueSerializationException;
import lombok.*;

import java.util.List;
import java.util.Optional;

/**
* Abstract Executable Deploy Item containing the StoredVersionedContractByName.
* Executable Deploy Item containing the StoredVersionedContractByName.
*
* @author Alexandre Carvalho
* @author Andre Bertolace
Expand All @@ -30,51 +18,19 @@
@AllArgsConstructor
@NoArgsConstructor
@JsonTypeName("StoredVersionedContractByName")
public class StoredVersionedContractByName extends ExecutableDeployItemWithEntryPoint {
public class StoredVersionedContractByName extends AbstractStoredVersionedContract {

/** * Contract Name */
/** Contract Name */
private String name;

/** * contract version */
@JsonProperty("version")
private Long version;

/** * Entry Point */
@JsonProperty("entry_point")
private String entryPoint;

/** * List of @see NamedArg */
private List<NamedArg<?>> args;

/** * {@link ExecutableDeployItem} order 4 */
/** {@link ExecutableDeployItem} order 4 */
@Override
public byte getOrder() {
return 0x4;
}

@JsonIgnore
public Optional<Long> getVersion() {
return Optional.ofNullable(version);
}

/**
* Implements the StoredVersionedContractName encoder
*/
@Override
public void serialize(final SerializerBuffer ser, final Target target) throws ValueSerializationException, NoSuchTypeException {
ser.writeU8(getOrder());
protected void serializeCustom(final SerializerBuffer ser) {
ser.writeString(getName());

//noinspection DuplicatedCode
final Optional<AbstractCLValue<?, ?>> innerValue;
if (version == null) {
innerValue = Optional.empty();
} else {
innerValue = Optional.of(new CLValueU32(version));
}
new CLValueOption(innerValue).serialize(ser, Target.JSON);

ser.writeString(getEntryPoint());
serializeNamedArgs(ser, target);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
import static org.hamcrest.core.IsNull.notNullValue;

/**
* Unit tests for the StoredVersionedContractByHash class.
*
* @author [email protected]
*/
class StoredVersionedContractByHashTest {
Expand Down

0 comments on commit 81d62cc

Please sign in to comment.