Skip to content

Commit

Permalink
Clean-up of the marshalling classes
Browse files Browse the repository at this point in the history
  • Loading branch information
spoto committed Nov 26, 2023
1 parent 7540fc0 commit aa5515f
Show file tree
Hide file tree
Showing 61 changed files with 339 additions and 437 deletions.
2 changes: 1 addition & 1 deletion dockerfiles/tendermint-node/init
Original file line number Diff line number Diff line change
Expand Up @@ -89,4 +89,4 @@ sed -i '/timeout_commit = /s/".*"/"'${TIMEOUT_COMMIT}s'"/' node0/config/config.t
# invoke moka in a way that deletes the tendermint configuration that was initially
# created into node0, so that we do not leave garbage around;
# in any case, it has been copied inside the chain directory
moka init-tendermint ${INITIAL_SUPPLY} --delta-supply=${DELTA_SUPPLY} --interactive=false --open-unsigned-faucet=${OPEN_UNSIGNED_FAUCET} --allow-mint-burn-from-gamete=${ALLOW_MINT_BURN_FROM_GAMETE} --key-of-gamete=${KEY_OF_GAMETE} --takamaka-code /modules/explicit/io-takamaka-code-1.0.13.jar --tendermint-config=node0 --delete-tendermint-config --bind-validators --max-gas-per-view ${MAX_GAS_PER_VIEW} --oblivion ${OBLIVION} --inflation ${INFLATION} --initial-gas-price ${INITIAL_GAS_PRICE} --percent-staked ${PERCENT_STAKED} --buyer-surcharge ${BUYER_SURCHARGE} --slashing-for-misbehaving ${SLASHING_FOR_MISBEHAVING} --slashing-for-not-behaving ${SLASHING_FOR_NOT_BEHAVING}
moka init-tendermint ${INITIAL_SUPPLY} --delta-supply=${DELTA_SUPPLY} --interactive=false --open-unsigned-faucet=${OPEN_UNSIGNED_FAUCET} --allow-mint-burn-from-gamete=${ALLOW_MINT_BURN_FROM_GAMETE} --key-of-gamete=${KEY_OF_GAMETE} --takamaka-code /modules/explicit/io-takamaka-code-1.0.14.jar --tendermint-config=node0 --delete-tendermint-config --bind-validators --max-gas-per-view ${MAX_GAS_PER_VIEW} --oblivion ${OBLIVION} --inflation ${INFLATION} --initial-gas-price ${INITIAL_GAS_PRICE} --percent-staked ${PERCENT_STAKED} --buyer-surcharge ${BUYER_SURCHARGE} --slashing-for-misbehaving ${SLASHING_FOR_MISBEHAVING} --slashing-for-not-behaving ${SLASHING_FOR_NOT_BEHAVING}
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
/*
Copyright 2021 Fausto Spoto
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package io.hotmoka.beans.marshalling.internal;

import java.io.IOException;
Expand Down Expand Up @@ -33,7 +49,7 @@ public void write(FieldSignature field, MarshallingContext context) throws IOExc
else {
int next = memory.size();
if (next == Integer.MAX_VALUE) // irrealistic
throw new IllegalStateException("too many field signatures in the same context");
throw new IllegalStateException("Too many field signatures in the same context");

memory.put(field, next);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
/*
Copyright 2021 Fausto Spoto
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package io.hotmoka.beans.marshalling.internal;

import java.io.IOException;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
/*
Copyright 2021 Fausto Spoto
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package io.hotmoka.beans.marshalling.internal;

import java.io.IOException;
Expand Down Expand Up @@ -33,12 +49,12 @@ public void write(TransactionReference transaction, MarshallingContext context)
else {
int next = memory.size();
if (next == Integer.MAX_VALUE) // irrealistic
throw new IllegalStateException("too many transaction references in the same context");
throw new IllegalStateException("Too many transaction references in the same context");

memory.put(transaction, next);

context.writeByte(255);
context.write(transaction.getHashAsBytes());
context.writeBytes(transaction.getHashAsBytes());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

package io.hotmoka.beans.nodes;

import java.util.Objects;

/**
* Node-specific information about a Hotmoka node.
*/
Expand Down Expand Up @@ -44,14 +46,9 @@ public class NodeInfo {
* @param ID the identifier of the node inside its network, if any. Otherwise the empty string
*/
public NodeInfo(String type, String version, String ID) {
if (type == null)
throw new NullPointerException("type cannot be null");

if (version == null)
throw new NullPointerException("version cannot be null");

if (ID == null)
throw new NullPointerException("ID cannot be null");
Objects.requireNonNull(type, "type cannot be null");
Objects.requireNonNull(version, "version cannot be null");
Objects.requireNonNull(ID, "ID cannot be null");

this.type = type;
this.version = version;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import java.io.IOException;
import java.math.BigInteger;
import java.util.Objects;

import io.hotmoka.annotations.Immutable;
import io.hotmoka.beans.references.TransactionReference;
Expand Down Expand Up @@ -52,9 +53,7 @@ public abstract class AbstractInstanceMethodCallTransactionRequest extends Metho
protected AbstractInstanceMethodCallTransactionRequest(StorageReference caller, BigInteger nonce, BigInteger gasLimit, BigInteger gasPrice, TransactionReference classpath, MethodSignature method, StorageReference receiver, StorageValue... actuals) {
super(caller, nonce, gasLimit, gasPrice, classpath, method, actuals);

if (receiver == null)
throw new IllegalArgumentException("receiver cannot be null");

Objects.requireNonNull(receiver, "receiver cannot be null");
this.receiver = receiver;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import java.io.IOException;
import java.math.BigInteger;
import java.util.Arrays;
import java.util.Objects;
import java.util.stream.Stream;

import io.hotmoka.annotations.Immutable;
Expand Down Expand Up @@ -50,12 +51,8 @@ public abstract class CodeExecutionTransactionRequest<R extends CodeExecutionTra
protected CodeExecutionTransactionRequest(StorageReference caller, BigInteger nonce, BigInteger gasLimit, BigInteger gasPrice, TransactionReference classpath, StorageValue... actuals) {
super(caller, nonce, gasLimit, gasPrice, classpath);

if (actuals == null)
throw new IllegalArgumentException("actuals cannot be null");

for (StorageValue actual: actuals)
if (actual == null)
throw new IllegalArgumentException("actuals cannot hold null");
Objects.requireNonNull(actuals, "actuals cannot be null");
Stream.of(actuals).forEach(actual -> Objects.requireNonNull(actual, "actuals cannot hold null"));

this.actuals = actuals;
}
Expand Down Expand Up @@ -89,6 +86,6 @@ public int hashCode() {
@Override
protected void intoWithoutSignature(MarshallingContext context) throws IOException {
super.intoWithoutSignature(context);
intoArray(actuals, context);
context.writeLengthAndArray(actuals);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.security.InvalidKeyException;
import java.security.SignatureException;
import java.util.Arrays;
import java.util.Objects;
import java.util.stream.Collectors;

import io.hotmoka.annotations.Immutable;
Expand Down Expand Up @@ -73,14 +74,11 @@ public class ConstructorCallTransactionRequest extends CodeExecutionTransactionR
public ConstructorCallTransactionRequest(Signer<? super ConstructorCallTransactionRequest> signer, StorageReference caller, BigInteger nonce, String chainId, BigInteger gasLimit, BigInteger gasPrice, TransactionReference classpath, ConstructorSignature constructor, StorageValue... actuals) throws InvalidKeyException, SignatureException {
super(caller, nonce, gasLimit, gasPrice, classpath, actuals);

if (constructor == null)
throw new IllegalArgumentException("constructor cannot be null");
Objects.requireNonNull(constructor, "constructor cannot be null");
Objects.requireNonNull(chainId, "chainId cannot be null");

if (constructor.formals().count() != actuals.length)
throw new IllegalArgumentException("argument count mismatch between formals and actuals");

if (chainId == null)
throw new IllegalArgumentException("chainId cannot be null");
throw new IllegalArgumentException("Argument count mismatch between formals and actuals");

this.constructor = constructor;
this.chainId = chainId;
Expand Down Expand Up @@ -190,9 +188,9 @@ public static ConstructorCallTransactionRequest from(UnmarshallingContext contex
var gasPrice = context.readBigInteger();
var classpath = TransactionReference.from(context);
var nonce = context.readBigInteger();
StorageValue[] actuals = context.readArray(StorageValue::from, StorageValue[]::new);
StorageValue[] actuals = context.readLengthAndArray(StorageValue::from, StorageValue[]::new);
ConstructorSignature constructor = (ConstructorSignature) CodeSignature.from(context);
byte[] signature = unmarshallSignature(context);
byte[] signature = context.readLengthAndBytes("Signature length mismatch in request");

return new ConstructorCallTransactionRequest(signature, caller, nonce, chainId, gasLimit, gasPrice, classpath, constructor, actuals);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import java.io.IOException;
import java.math.BigInteger;
import java.util.Objects;

import io.hotmoka.annotations.Immutable;
import io.hotmoka.beans.references.TransactionReference;
Expand Down Expand Up @@ -66,24 +67,17 @@ public class GameteCreationTransactionRequest extends InitialTransactionRequest<
* @param publicKey the Base64-encoded public key that will be assigned to the gamete
*/
public GameteCreationTransactionRequest(TransactionReference classpath, BigInteger initialAmount, BigInteger redInitialAmount, String publicKey) {
if (classpath == null)
throw new IllegalArgumentException("classpath cannot be null");

if (initialAmount == null)
throw new IllegalArgumentException("initialAmount cannot be null");
Objects.requireNonNull(classpath, "classpath cannot be null");
Objects.requireNonNull(initialAmount, "initialAmount cannot be null");
Objects.requireNonNull(redInitialAmount, "redInitialAmount cannot be null");
Objects.requireNonNull(publicKey, "publicKey cannot be null");

if (initialAmount.signum() < 0)
throw new IllegalArgumentException("initialAmount cannot be negative");

if (redInitialAmount == null)
throw new IllegalArgumentException("redInitialAmount cannot be null");

if (redInitialAmount.signum() < 0)
throw new IllegalArgumentException("redInitialAmount cannot be negative");

if (publicKey == null)
throw new IllegalArgumentException("publicKey cannot be null");

this.classpath = classpath;
this.initialAmount = initialAmount;
this.redInitialAmount = redInitialAmount;
Expand All @@ -102,7 +96,7 @@ public String toString() {
@Override
public boolean equals(Object other) {
if (other instanceof GameteCreationTransactionRequest) {
GameteCreationTransactionRequest otherCast = (GameteCreationTransactionRequest) other;
var otherCast = (GameteCreationTransactionRequest) other;
return classpath.equals(otherCast.classpath) && initialAmount.equals(otherCast.initialAmount) && redInitialAmount.equals(otherCast.redInitialAmount)
&& publicKey.equals(otherCast.publicKey);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package io.hotmoka.beans.requests;

import java.io.IOException;
import java.util.Objects;

import io.hotmoka.annotations.Immutable;
import io.hotmoka.beans.references.TransactionReference;
Expand Down Expand Up @@ -53,27 +54,22 @@ public class InitializationTransactionRequest extends InitialTransactionRequest<
* @param manifest the storage reference that must be set as manifest
*/
public InitializationTransactionRequest(TransactionReference classpath, StorageReference manifest) {
if (classpath == null)
throw new IllegalArgumentException("classpath cannot be null");

if (manifest == null)
throw new IllegalArgumentException("manifest cannot be null");
Objects.requireNonNull(classpath, "classpath cannot be null");
Objects.requireNonNull(manifest, "manifest cannot be null");

this.classpath = classpath;
this.manifest = manifest;
}

@Override
public String toString() {
return getClass().getSimpleName() + ":\n"
+ " class path: " + classpath + "\n"
+ " manifest: " + manifest;
return getClass().getSimpleName() + ":\n" + " class path: " + classpath + "\n" + " manifest: " + manifest;
}

@Override
public boolean equals(Object other) {
if (other instanceof InitializationTransactionRequest) {
InitializationTransactionRequest otherCast = (InitializationTransactionRequest) other;
var otherCast = (InitializationTransactionRequest) other;
return classpath.equals(otherCast.classpath) && manifest.equals(otherCast.manifest);
}
else
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import java.security.InvalidKeyException;
import java.security.SignatureException;
import java.util.Arrays;
import java.util.Objects;

import io.hotmoka.annotations.Immutable;
import io.hotmoka.beans.references.TransactionReference;
Expand Down Expand Up @@ -78,8 +79,7 @@ public class InstanceMethodCallTransactionRequest extends AbstractInstanceMethod
public InstanceMethodCallTransactionRequest(Signer<? super InstanceMethodCallTransactionRequest> signer, StorageReference caller, BigInteger nonce, String chainId, BigInteger gasLimit, BigInteger gasPrice, TransactionReference classpath, MethodSignature method, StorageReference receiver, StorageValue... actuals) throws InvalidKeyException, SignatureException {
super(caller, nonce, gasLimit, gasPrice, classpath, method, receiver, actuals);

if (chainId == null)
throw new IllegalArgumentException("chainId cannot be null");
Objects.requireNonNull(chainId, "chainId cannot be null");

this.chainId = chainId;
this.signature = signer.sign(this);
Expand All @@ -102,12 +102,8 @@ public InstanceMethodCallTransactionRequest(Signer<? super InstanceMethodCallTra
public InstanceMethodCallTransactionRequest(byte[] signature, StorageReference caller, BigInteger nonce, String chainId, BigInteger gasLimit, BigInteger gasPrice, TransactionReference classpath, MethodSignature method, StorageReference receiver, StorageValue... actuals) {
super(caller, nonce, gasLimit, gasPrice, classpath, method, receiver, actuals);

if (chainId == null)
throw new IllegalArgumentException("chainId cannot be null");

if (signature == null)
throw new IllegalArgumentException("signature cannot be null");

Objects.requireNonNull(chainId, "chainId cannot be null");
Objects.requireNonNull(signature, "signature cannot be null");
this.chainId = chainId;
this.signature = signature;
}
Expand All @@ -131,15 +127,12 @@ public InstanceMethodCallTransactionRequest(StorageReference caller, BigInteger
@Override
public final void into(MarshallingContext context) throws IOException {
intoWithoutSignature(context);
// we add the signature
context.writeLengthAndBytes(signature);
context.writeLengthAndBytes(signature); // we add the signature
}

@Override
public String toString() {
return super.toString()
+ "\n chainId: " + chainId
+ "\n signature: " + bytesToHex(signature);
return super.toString() + "\n chainId: " + chainId + "\n signature: " + bytesToHex(signature);
}

@Override
Expand Down Expand Up @@ -217,16 +210,16 @@ else if (receiveLong)
*/
public static InstanceMethodCallTransactionRequest from(UnmarshallingContext context, byte selector) throws IOException {
if (selector == SELECTOR) {
String chainId = context.readStringUnshared();
StorageReference caller = StorageReference.from(context);
BigInteger gasLimit = context.readBigInteger();
BigInteger gasPrice = context.readBigInteger();
TransactionReference classpath = TransactionReference.from(context);
BigInteger nonce = context.readBigInteger();
StorageValue[] actuals = context.readArray(StorageValue::from, StorageValue[]::new);
MethodSignature method = (MethodSignature) CodeSignature.from(context);
StorageReference receiver = StorageReference.from(context);
byte[] signature = unmarshallSignature(context);
var chainId = context.readStringUnshared();
var caller = StorageReference.from(context);
var gasLimit = context.readBigInteger();
var gasPrice = context.readBigInteger();
var classpath = TransactionReference.from(context);
var nonce = context.readBigInteger();
StorageValue[] actuals = context.readLengthAndArray(StorageValue::from, StorageValue[]::new);
var method = (MethodSignature) CodeSignature.from(context);
var receiver = StorageReference.from(context);
byte[] signature = context.readLengthAndBytes("Signature length mismatch in request");

return new InstanceMethodCallTransactionRequest(signature, caller, nonce, chainId, gasLimit, gasPrice, classpath, method, receiver, actuals);
}
Expand All @@ -241,19 +234,19 @@ else if (selector == SELECTOR_TRANSFER_INT || selector == SELECTOR_TRANSFER_LONG

if (selector == SELECTOR_TRANSFER_INT) {
int howMuch = context.readInt();
byte[] signature = unmarshallSignature(context);
byte[] signature = context.readLengthAndBytes("Signature length mismatch in request");

return new InstanceMethodCallTransactionRequest(signature, caller, nonce, chainId, gasLimit, gasPrice, classpath, CodeSignature.RECEIVE_INT, receiver, new IntValue(howMuch));
}
else if (selector == SELECTOR_TRANSFER_LONG) {
long howMuch = context.readLong();
byte[] signature = unmarshallSignature(context);
byte[] signature = context.readLengthAndBytes("Signature length mismatch in request");

return new InstanceMethodCallTransactionRequest(signature, caller, nonce, chainId, gasLimit, gasPrice, classpath, CodeSignature.RECEIVE_LONG, receiver, new LongValue(howMuch));
}
else {
BigInteger howMuch = context.readBigInteger();
byte[] signature = unmarshallSignature(context);
byte[] signature = context.readLengthAndBytes("Signature length mismatch in request");

return new InstanceMethodCallTransactionRequest(signature, caller, nonce, chainId, gasLimit, gasPrice, classpath, CodeSignature.RECEIVE_BIG_INTEGER, receiver, new BigIntegerValue(howMuch));
}
Expand Down
Loading

0 comments on commit aa5515f

Please sign in to comment.