Skip to content

Commit

Permalink
Refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
spoto committed Dec 31, 2023
1 parent 4ab8a4f commit 302d626
Show file tree
Hide file tree
Showing 60 changed files with 268 additions and 242 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,17 @@
* The types that can be used in storage objects in blockchain.
*/
@Immutable
public interface StorageType extends Marshallable {
public interface StorageType extends Marshallable, Comparable<StorageType> {

/**
* Compares this storage type with another. Puts first basic types, in their order of
* enumeration, then class types ordered wrt class name. This method is not
* called {@code compareTo} since it would conflict with the implicit {@code compareTo()}
* method defined in the enumeration {@link io.hotmoka.beans.types.internal.BasicTypes}.
* Compares this storage type with another. Puts first basic types, by name,
* then class types ordered wrt class name.
*
* @param other the other type
* @return -1 if {@code this} comes first, 1 if {@code other} comes first, 0 if they are equal
*/
int compareAgainst(StorageType other);
@Override
int compareTo(StorageType other);

/**
* Determines if this type is eager.
Expand Down
55 changes: 34 additions & 21 deletions io-hotmoka-beans/src/main/java/io/hotmoka/beans/StorageTypes.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
import io.hotmoka.beans.api.types.BasicType;
import io.hotmoka.beans.api.types.ClassType;
import io.hotmoka.beans.api.types.StorageType;
import io.hotmoka.beans.types.internal.BasicTypes;
import io.hotmoka.beans.types.internal.BasicTypeImpl;
import io.hotmoka.beans.types.internal.ClassTypeImpl;
import io.hotmoka.constants.Constants;
import io.hotmoka.marshalling.api.UnmarshallingContext;
Expand All @@ -37,42 +37,42 @@ private StorageTypes() {}
/**
* The {@code boolean} basic type of the Takamaka language.
*/
public final static BasicType BOOLEAN = BasicTypes.BOOLEAN;
public final static BasicType BOOLEAN = new BasicTypeImpl(BasicTypeImpl.Instance.BOOLEAN);

/**
* The {@code byte} basic type of the Takamaka language.
*/
public final static BasicType BYTE = BasicTypes.BYTE;
public final static BasicType BYTE = new BasicTypeImpl(BasicTypeImpl.Instance.BYTE);

/**
* The {@code char} basic type of the Takamaka language.
*/
public final static BasicType CHAR = BasicTypes.CHAR;
public final static BasicType CHAR = new BasicTypeImpl(BasicTypeImpl.Instance.CHAR);

/**
* The {@code short} basic type of the Takamaka language.
*/
public final static BasicType SHORT = BasicTypes.SHORT;
public final static BasicType SHORT = new BasicTypeImpl(BasicTypeImpl.Instance.SHORT);

/**
* The {@code int} basic type of the Takamaka language.
*/
public final static BasicType INT = BasicTypes.INT;
public final static BasicType INT = new BasicTypeImpl(BasicTypeImpl.Instance.INT);

/**
* The {@code long} basic type of the Takamaka language.
*/
public final static BasicType LONG = BasicTypes.LONG;
public final static BasicType LONG = new BasicTypeImpl(BasicTypeImpl.Instance.LONG);

/**
* The {@code float} basic type of the Takamaka language.
*/
public final static BasicType FLOAT = BasicTypes.FLOAT;
public final static BasicType FLOAT = new BasicTypeImpl(BasicTypeImpl.Instance.FLOAT);

/**
* The {@code double} basic type of the Takamaka language.
*/
public final static BasicType DOUBLE = BasicTypes.DOUBLE;
public final static BasicType DOUBLE = new BasicTypeImpl(BasicTypeImpl.Instance.DOUBLE);

/**
* The frequently used class type for {@link java.lang.Object}.
Expand Down Expand Up @@ -406,21 +406,21 @@ public static StorageType named(String name) {
*/
public static StorageType of(Class<?> clazz) {
if (clazz == boolean.class)
return BasicTypes.BOOLEAN;
return BOOLEAN;
else if (clazz == byte.class)
return BasicTypes.BYTE;
return BYTE;
else if (clazz == char.class)
return BasicTypes.CHAR;
return CHAR;
else if (clazz == short.class)
return BasicTypes.SHORT;
return SHORT;
else if (clazz == int.class)
return BasicTypes.INT;
return INT;
else if (clazz == long.class)
return BasicTypes.LONG;
return LONG;
else if (clazz == float.class)
return BasicTypes.FLOAT;
return FLOAT;
else if (clazz == double.class)
return BasicTypes.DOUBLE;
return DOUBLE;
else
return StorageTypes.classNamed(clazz.getName());
}
Expand Down Expand Up @@ -499,11 +499,24 @@ public static StorageType from(UnmarshallingContext context) throws IOException
return StorageTypes.classNamed(Constants.IO_TAKAMAKA_CODE_UTIL_PACKAGE_NAME + context.readStringShared());
case ClassTypeImpl.SELECTOR_IO_TAKAMAKA_CODE_TOKENS:
return StorageTypes.classNamed(Constants.IO_TAKAMAKA_CODE_TOKENS_PACKAGE_NAME + context.readStringShared());
case BasicTypeImpl.BOOLEAN_SELECTOR:
return BOOLEAN;
case BasicTypeImpl.BYTE_SELECTOR:
return BYTE;
case BasicTypeImpl.CHAR_SELECTOR:
return CHAR;
case BasicTypeImpl.SHORT_SELECTOR:
return SHORT;
case BasicTypeImpl.INT_SELECTOR:
return INT;
case BasicTypeImpl.LONG_SELECTOR:
return LONG;
case BasicTypeImpl.FLOAT_SELECTOR:
return FLOAT;
case BasicTypeImpl.DOUBLE_SELECTOR:
return DOUBLE;
default:
if (selector >= 0 && selector < 8)
return BasicTypes.values()[selector];
else
throw new IOException("Unexpected type selector: " + selector);
throw new IOException("Unexpected type selector: " + selector);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

/**
* The response of a failed transaction. This means that the transaction
* could not be executed until its end. All gas provided to the
* could not be executed until its end. Instance gas provided to the
* transaction has been consumed, as a form of penalty.
*/
public interface TransactionResponseFailed {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
import io.hotmoka.beans.api.types.ClassType;
import io.hotmoka.beans.api.types.StorageType;
import io.hotmoka.beans.marshalling.BeanMarshallingContext;
import io.hotmoka.beans.types.internal.BasicTypes;
import io.hotmoka.marshalling.AbstractMarshallable;
import io.hotmoka.marshalling.api.MarshallingContext;
import io.hotmoka.marshalling.api.UnmarshallingContext;
Expand Down Expand Up @@ -87,17 +86,17 @@ public abstract class CodeSignature extends AbstractMarshallable {
/**
* The method {@code getMaxErrorLength} of the manifest.
*/
public final static MethodSignature GET_MAX_ERROR_LENGTH = new NonVoidMethodSignature(StorageTypes.MANIFEST, "getMaxErrorLength", BasicTypes.LONG);
public final static MethodSignature GET_MAX_ERROR_LENGTH = new NonVoidMethodSignature(StorageTypes.MANIFEST, "getMaxErrorLength", StorageTypes.LONG);

/**
* The method {@code getMaxDependencies} of the manifest.
*/
public final static MethodSignature GET_MAX_DEPENDENCIES = new NonVoidMethodSignature(StorageTypes.MANIFEST, "getMaxDependencies", BasicTypes.LONG);
public final static MethodSignature GET_MAX_DEPENDENCIES = new NonVoidMethodSignature(StorageTypes.MANIFEST, "getMaxDependencies", StorageTypes.LONG);

/**
* The method {@code getMaxCumulativeSizeOfDependencies} of the manifest.
*/
public final static MethodSignature GET_MAX_CUMULATIVE_SIZE_OF_DEPENDENCIES = new NonVoidMethodSignature(StorageTypes.MANIFEST, "getMaxCumulativeSizeOfDependencies", BasicTypes.LONG);
public final static MethodSignature GET_MAX_CUMULATIVE_SIZE_OF_DEPENDENCIES = new NonVoidMethodSignature(StorageTypes.MANIFEST, "getMaxCumulativeSizeOfDependencies", StorageTypes.LONG);

/**
* The method {@code getTicketForNewPoll} of the validators.
Expand Down Expand Up @@ -132,17 +131,17 @@ public abstract class CodeSignature extends AbstractMarshallable {
/**
* The method {@code allowsSelfCharged} of the manifest.
*/
public final static MethodSignature ALLOWS_SELF_CHARGED = new NonVoidMethodSignature(StorageTypes.MANIFEST, "allowsSelfCharged", BasicTypes.BOOLEAN);
public final static MethodSignature ALLOWS_SELF_CHARGED = new NonVoidMethodSignature(StorageTypes.MANIFEST, "allowsSelfCharged", StorageTypes.BOOLEAN);

/**
* The method {@code allowsUnsignedFaucet} of the manifest.
*/
public final static MethodSignature ALLOWS_UNSIGNED_FAUCET = new NonVoidMethodSignature(StorageTypes.MANIFEST, "allowsUnsignedFaucet", BasicTypes.BOOLEAN);
public final static MethodSignature ALLOWS_UNSIGNED_FAUCET = new NonVoidMethodSignature(StorageTypes.MANIFEST, "allowsUnsignedFaucet", StorageTypes.BOOLEAN);

/**
* The method {@code skipsVerification} of the manifest.
*/
public final static MethodSignature SKIPS_VERIFICATION = new NonVoidMethodSignature(StorageTypes.MANIFEST, "skipsVerification", BasicTypes.BOOLEAN);
public final static MethodSignature SKIPS_VERIFICATION = new NonVoidMethodSignature(StorageTypes.MANIFEST, "skipsVerification", StorageTypes.BOOLEAN);

/**
* The method {@code getSignature} of the manifest.
Expand Down Expand Up @@ -197,7 +196,7 @@ public abstract class CodeSignature extends AbstractMarshallable {
/**
* The method {@code getOblivion} of the gas station.
*/
public final static MethodSignature GET_OBLIVION = new NonVoidMethodSignature(StorageTypes.GAS_STATION, "getOblivion", BasicTypes.LONG);
public final static MethodSignature GET_OBLIVION = new NonVoidMethodSignature(StorageTypes.GAS_STATION, "getOblivion", StorageTypes.LONG);

/**
* The method {@code getStake} of the validators object.
Expand All @@ -207,17 +206,17 @@ public abstract class CodeSignature extends AbstractMarshallable {
/**
* The method {@code getInitialInflation} of the validators object.
*/
public final static MethodSignature GET_INITIAL_INFLATION = new NonVoidMethodSignature(StorageTypes.VALIDATORS, "getInitialInflation", BasicTypes.LONG);
public final static MethodSignature GET_INITIAL_INFLATION = new NonVoidMethodSignature(StorageTypes.VALIDATORS, "getInitialInflation", StorageTypes.LONG);

/**
* The method {@code getCurrentInflation} of the validators object.
*/
public final static MethodSignature GET_CURRENT_INFLATION = new NonVoidMethodSignature(StorageTypes.VALIDATORS, "getCurrentInflation", BasicTypes.LONG);
public final static MethodSignature GET_CURRENT_INFLATION = new NonVoidMethodSignature(StorageTypes.VALIDATORS, "getCurrentInflation", StorageTypes.LONG);

/**
* The method {@code ignoresGasPrice} of the gas station.
*/
public final static MethodSignature IGNORES_GAS_PRICE = new NonVoidMethodSignature(StorageTypes.GAS_STATION, "ignoresGasPrice", BasicTypes.BOOLEAN);
public final static MethodSignature IGNORES_GAS_PRICE = new NonVoidMethodSignature(StorageTypes.GAS_STATION, "ignoresGasPrice", StorageTypes.BOOLEAN);

/**
* The method {@code getValidators} of the manifest.
Expand All @@ -232,7 +231,7 @@ public abstract class CodeSignature extends AbstractMarshallable {
/**
* The method {@code getVerificationVersion} of the versions object.
*/
public final static MethodSignature GET_VERIFICATION_VERSION = new NonVoidMethodSignature(StorageTypes.VERSIONS, "getVerificationVersion", BasicTypes.LONG);
public final static MethodSignature GET_VERIFICATION_VERSION = new NonVoidMethodSignature(StorageTypes.VERSIONS, "getVerificationVersion", StorageTypes.LONG);

/**
* The method {@code getPolls} of the validators object.
Expand Down Expand Up @@ -277,12 +276,12 @@ public abstract class CodeSignature extends AbstractMarshallable {
/**
* The method {@code receive} of a payable contract, with an int argument.
*/
public final static MethodSignature RECEIVE_INT = new VoidMethodSignature(StorageTypes.PAYABLE_CONTRACT, "receive", BasicTypes.INT);
public final static MethodSignature RECEIVE_INT = new VoidMethodSignature(StorageTypes.PAYABLE_CONTRACT, "receive", StorageTypes.INT);

/**
* The method {@code receive} of a payable contract, with a long argument.
*/
public final static MethodSignature RECEIVE_LONG = new VoidMethodSignature(StorageTypes.PAYABLE_CONTRACT, "receive", BasicTypes.LONG);
public final static MethodSignature RECEIVE_LONG = new VoidMethodSignature(StorageTypes.PAYABLE_CONTRACT, "receive", StorageTypes.LONG);

/**
* The method {@code reward} of the validators contract.
Expand All @@ -303,7 +302,7 @@ public abstract class CodeSignature extends AbstractMarshallable {
/**
* The method {@code isVoteOver} of the Poll contract.
*/
public final static MethodSignature IS_VOTE_OVER = new NonVoidMethodSignature(StorageTypes.POLL, "isVoteOver", BasicTypes.BOOLEAN);
public final static MethodSignature IS_VOTE_OVER = new NonVoidMethodSignature(StorageTypes.POLL, "isVoteOver", StorageTypes.BOOLEAN);

/**
* The method {@code closePoll} of the Poll contract.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
import io.hotmoka.beans.api.types.ClassType;
import io.hotmoka.beans.api.types.StorageType;
import io.hotmoka.beans.marshalling.BeanMarshallingContext;
import io.hotmoka.beans.types.internal.BasicTypes;
import io.hotmoka.constants.Constants;
import io.hotmoka.marshalling.AbstractMarshallable;
import io.hotmoka.marshalling.api.MarshallingContext;
Expand Down Expand Up @@ -111,12 +110,12 @@ public final class FieldSignature extends AbstractMarshallable implements Compar
/**
* The field that holds the size of a {@code io.takamaka.code.util.StorageTreeMap.Node}.
*/
public final static FieldSignature STORAGE_TREE_MAP_NODE_SIZE_FIELD = new FieldSignature(StorageTypes.STORAGE_TREE_MAP_NODE, "size", BasicTypes.INT);
public final static FieldSignature STORAGE_TREE_MAP_NODE_SIZE_FIELD = new FieldSignature(StorageTypes.STORAGE_TREE_MAP_NODE, "size", StorageTypes.INT);

/**
* The field that holds the size of a {@code io.takamaka.code.util.StorageTreeIntMap.Node}.
*/
public final static FieldSignature STORAGE_TREE_INTMAP_NODE_SIZE_FIELD = new FieldSignature(StorageTypes.STORAGE_TREE_INTMAP_NODE, "size", BasicTypes.INT);
public final static FieldSignature STORAGE_TREE_INTMAP_NODE_SIZE_FIELD = new FieldSignature(StorageTypes.STORAGE_TREE_INTMAP_NODE, "size", StorageTypes.INT);

/**
* The field that holds the value of a {@code io.takamaka.code.util.StorageTreeIntMap.Node}.
Expand All @@ -136,7 +135,7 @@ public final class FieldSignature extends AbstractMarshallable implements Compar
/**
* The field that holds the key of a {@code io.takamaka.code.util.StorageTreeIntMap.Node}.
*/
public final static FieldSignature STORAGE_TREE_INTMAP_NODE_KEY_FIELD = new FieldSignature(StorageTypes.STORAGE_TREE_INTMAP_NODE, "key", BasicTypes.INT);
public final static FieldSignature STORAGE_TREE_INTMAP_NODE_KEY_FIELD = new FieldSignature(StorageTypes.STORAGE_TREE_INTMAP_NODE, "key", StorageTypes.INT);

/**
* The field that holds the left tree of a {@code io.takamaka.code.util.StorageTreeMap.Node}.
Expand Down Expand Up @@ -219,12 +218,12 @@ public String toString() {

@Override
public int compareTo(FieldSignature other) {
int diff = definingClass.compareAgainst(other.definingClass);
int diff = definingClass.compareTo(other.definingClass);
if (diff != 0)
return diff;

diff = name.compareTo(other.name);
return diff != 0 ? diff : type.compareAgainst(other.type);
return diff != 0 ? diff : type.compareTo(other.type);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,23 +27,40 @@
* The basic types of the Takamaka language.
*/
@Immutable
public enum BasicTypes implements BasicType {
BOOLEAN, BYTE, CHAR, SHORT, INT, LONG, FLOAT, DOUBLE;
public class BasicTypeImpl implements BasicType {
private final Instance instance;

public static enum Instance {
BOOLEAN, BYTE, CHAR, SHORT, INT, LONG, FLOAT, DOUBLE;
}

public final static int BOOLEAN_SELECTOR = 0;
public final static int BYTE_SELECTOR = 1;
public final static int CHAR_SELECTOR = 2;
public final static int SHORT_SELECTOR = 3;
public final static int INT_SELECTOR = 4;
public final static int LONG_SELECTOR = 5;
public final static int FLOAT_SELECTOR = 6;
public final static int DOUBLE_SELECTOR = 7;

public BasicTypeImpl(Instance instance) {
this.instance = instance;
}

@Override
public String toString() {
return super.toString().toLowerCase();
return instance.toString().toLowerCase();
}

@Override
public int compareAgainst(StorageType other) {
return other instanceof BasicTypes bt ? compareTo(bt)
public int compareTo(StorageType other) {
return other instanceof BasicType bt ? toString().compareTo(bt.toString())
: -1; // other instanceof ClassType
}

@Override
public void into(MarshallingContext context) throws IOException {
context.writeByte((byte) ordinal());
context.writeByte((byte) instance.ordinal());
}

@Override
Expand All @@ -53,7 +70,7 @@ public boolean isEager() {

@Override
public byte[] toByteArray() {
return new byte[] { (byte) ordinal() };
return new byte[] { (byte) instance.ordinal() };
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,10 +101,10 @@ public int hashCode() {
}

@Override
public int compareAgainst(StorageType other) {
public int compareTo(StorageType other) {
if (other instanceof ClassTypeImpl ct)
return name.compareTo(ct.name);
else // other instanceof BasicTypes
else // other instanceof BasicTypeImpl
return 1;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ public int compareTo(Update other) {
if (diff != 0)
return diff;

diff = clazz.compareAgainst(((ClassTag) other).clazz);
diff = clazz.compareTo(((ClassTag) other).clazz);
if (diff != 0)
return diff;
else
Expand Down
Loading

0 comments on commit 302d626

Please sign in to comment.