Skip to content

Commit

Permalink
update from comments
Browse files Browse the repository at this point in the history
  • Loading branch information
schwartz-concordium committed Jan 30, 2024
1 parent 5d2bb62 commit 2e5125f
Show file tree
Hide file tree
Showing 11 changed files with 57 additions and 55 deletions.
2 changes: 2 additions & 0 deletions rust-bindings/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,8 @@ pub enum FFIError {
}

impl FFIError {
/// The enumeration starts a 1 since return value 0 indicating a successfull
/// FFI call.
fn to_int(&self) -> u16 {
match self {
FFIError::JsonError(_) => 1,
Expand Down
23 changes: 0 additions & 23 deletions src/Exceptions/InteropBindingException.cs

This file was deleted.

23 changes: 23 additions & 0 deletions src/Exceptions/SchemaJsonException.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using System.Text;
using Concordium.Sdk.Interop;

namespace Concordium.Sdk.Exceptions;

/// <summary>
/// Thrown when a interop call failed with possible error as message.
/// </summary>
public sealed class SchemaJsonException : Exception
{
private const string EmptyErrorMessage = "Empty error message returned";
/// <summary>
/// Type of error
/// </summary>
public SchemaJsonResult SchemaJsonResult { get; }

internal static SchemaJsonException Create(SchemaJsonResult schemaJsonResult, byte[]? message) =>
message != null ? new SchemaJsonException(schemaJsonResult, Encoding.UTF8.GetString(message)) : Empty(schemaJsonResult);

private SchemaJsonException(SchemaJsonResult schemaJsonResult, string message) : base(message) => this.SchemaJsonResult = schemaJsonResult;

private static SchemaJsonException Empty(SchemaJsonResult schemaJsonResult) => new(schemaJsonResult, EmptyErrorMessage);
}
12 changes: 6 additions & 6 deletions src/Interop/InteropBinding.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@ internal static class InteropBinding
private const string DllName = "rust_bindings";

[DllImport(DllName, CallingConvention = CallingConvention.Cdecl, EntryPoint = "schema_display")]
private static extern Result SchemaDisplay(
private static extern SchemaJsonResult SchemaDisplay(
[MarshalAs(UnmanagedType.LPArray)] byte[] schema,
int schema_size,
FfiByteOption schema_version,
[MarshalAs(UnmanagedType.FunctionPtr)] SetResultCallback callback);

[DllImport(DllName, CallingConvention = CallingConvention.Cdecl, EntryPoint = "get_receive_contract_parameter")]
private static extern Result GetReceiveContractParameter(
private static extern SchemaJsonResult GetReceiveContractParameter(
[MarshalAs(UnmanagedType.LPArray)] byte[] schema, int schema_size,
FfiByteOption schema_version,
[MarshalAs(UnmanagedType.LPUTF8Str)] string contract_name,
Expand All @@ -32,7 +32,7 @@ private static extern Result GetReceiveContractParameter(
[MarshalAs(UnmanagedType.FunctionPtr)] SetResultCallback callback);

[DllImport(DllName, CallingConvention = CallingConvention.Cdecl, EntryPoint = "get_event_contract")]
private static extern Result GetEventContract(
private static extern SchemaJsonResult GetEventContract(
[MarshalAs(UnmanagedType.LPArray)] byte[] schema,
int schema_size,
FfiByteOption schema_version,
Expand Down Expand Up @@ -68,7 +68,7 @@ internal static Utf8Json SchemaDisplay(VersionedModuleSchema schema)
return new Utf8Json(result);
}

var interopException = InteropBindingException.Create(schemaDisplay, result);
var interopException = SchemaJsonException.Create(schemaDisplay, result);
throw interopException;
}

Expand Down Expand Up @@ -102,7 +102,7 @@ internal static Utf8Json GetReceiveContractParameter(VersionedModuleSchema schem
return new Utf8Json(result);
}

var interopException = InteropBindingException.Create(receiveContractParameter, result);
var interopException = SchemaJsonException.Create(receiveContractParameter, result);
throw interopException;
}

Expand Down Expand Up @@ -132,7 +132,7 @@ internal static Utf8Json GetEventContract(VersionedModuleSchema schema, Contract
return new Utf8Json(result);
}

var interopException = InteropBindingException.Create(schemaDisplay, result);
var interopException = SchemaJsonException.Create(schemaDisplay, result);
throw interopException;
}

Expand Down
4 changes: 2 additions & 2 deletions src/Interop/Result.cs → src/Interop/SchemaJsonResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ namespace Concordium.Sdk.Interop;
/// <summary>
/// Result type which on errors hold error type information.
/// </summary>
public enum Result
public enum SchemaJsonResult
{
/// <summary>
/// No error
Expand Down Expand Up @@ -87,5 +87,5 @@ public enum Result

internal static class ErrorExtensions
{
internal static bool IsError(this Result result) => result != Result.NoError;
internal static bool IsError(this SchemaJsonResult schemaJsonResult) => schemaJsonResult != SchemaJsonResult.NoError;
}
2 changes: 1 addition & 1 deletion src/Types/ContractEvent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public sealed record ContractEvent(byte[] Bytes)
/// <param name="schema">Module schema in hexadecimal.</param>
/// <param name="contractName">Contract name.</param>
/// <returns><see cref="Bytes"/> deserialized as json uft8 encoded.</returns>
/// <exception cref="InteropBindingException">Thrown when event wasn't able to be deserialized from schema.</exception>
/// <exception cref="SchemaJsonException">Thrown when event wasn't able to be deserialized from schema.</exception>
public Utf8Json GetDeserializeEvent(
VersionedModuleSchema schema,
ContractIdentifier contractName
Expand Down
2 changes: 1 addition & 1 deletion src/Types/ContractInitializedEvent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ internal static ContractInitializedEvent From(Grpc.V2.ContractInitializedEvent i
/// </summary>
/// <param name="schema">Module schema in hexadecimal.</param>
/// <returns>List of deserialized json uft8 encoded events. Possible null if this was returned from deserialization.</returns>
/// <exception cref="InteropBindingException">Thrown if an event wasn't able to be deserialized from schema.</exception>
/// <exception cref="SchemaJsonException">Thrown if an event wasn't able to be deserialized from schema.</exception>
public IList<Utf8Json> GetDeserializedEvents(VersionedModuleSchema schema)
{
var deserialized = new List<Utf8Json>(this.Events.Count);
Expand Down
8 changes: 4 additions & 4 deletions src/Types/ContractTraceElement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public sealed record Updated(
/// </summary>
/// <param name="schema">Versioned module schema.</param>
/// <returns><see cref="Message"/> deserialized as json uft8 encoded.</returns>
/// <exception cref="InteropBindingException">Thrown when message wasn't able to be deserialized form schema.</exception>
/// <exception cref="SchemaJsonException">Thrown when message wasn't able to be deserialized form schema.</exception>
public Utf8Json GetDeserializeMessage(VersionedModuleSchema schema) =>
GetDeserializeMessage(schema, this.ReceiveName.GetContractName(), this.ReceiveName.GetEntrypoint(), this.Message);

Expand All @@ -91,7 +91,7 @@ public Utf8Json GetDeserializeMessage(VersionedModuleSchema schema) =>
/// <param name="entryPoint">Entrypoint on contract.</param>
/// <param name="message">Message to entrypoint.</param>
/// <returns><see cref="message"/> deserialized as json uft8 encoded.</returns>
/// <exception cref="InteropBindingException">Thrown when message wasn't able to be deserialized from schema.</exception>
/// <exception cref="SchemaJsonException">Thrown when message wasn't able to be deserialized from schema.</exception>
public static Utf8Json GetDeserializeMessage(
VersionedModuleSchema schema,
ContractIdentifier contractIdentifier,
Expand All @@ -105,7 +105,7 @@ Parameter message
/// </summary>
/// <param name="schema">Module schema.</param>
/// <returns>List of deserialized json uft8 encoded events. Possible null if this was returned from deserialization.</returns>
/// <exception cref="InteropBindingException">Thrown if an event wasn't able to be deserialized from schema.</exception>
/// <exception cref="SchemaJsonException">Thrown if an event wasn't able to be deserialized from schema.</exception>
public IList<Utf8Json> GetDeserializedEvents(VersionedModuleSchema schema)
{
var deserialized = new List<Utf8Json>(this.Events.Count);
Expand Down Expand Up @@ -141,7 +141,7 @@ public sealed record Interrupted(ContractAddress Address, IList<ContractEvent> E
/// <param name="schema">Module schema.</param>
/// <param name="contractName">Contract name.</param>
/// <returns>List of deserialized json uft8 encoded events. Possible null if this was returned from deserialization.</returns>
/// <exception cref="InteropBindingException">Thrown if an event wasn't able to be deserialized from schema.</exception>
/// <exception cref="SchemaJsonException">Thrown if an event wasn't able to be deserialized from schema.</exception>
public IList<Utf8Json> GetDeserializedEvents(VersionedModuleSchema schema, ContractIdentifier contractName)
{
var deserialized = new List<Utf8Json>(this.Events.Count);
Expand Down
2 changes: 1 addition & 1 deletion src/Types/RejectReason.cs
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ public sealed record RejectedReceive(int RejectReason, ContractAddress ContractA
/// </summary>
/// <param name="schema">Versioned module schema.</param>
/// <returns><see cref="Parameter"/> deserialized as json uft8 encoded.</returns>
/// <exception cref="InteropBindingException">Thrown when message wasn't able to be deserialized using the schema.</exception>
/// <exception cref="SchemaJsonException">Thrown when message wasn't able to be deserialized using the schema.</exception>
public Utf8Json GetDeserializeMessage(VersionedModuleSchema schema) =>
Updated.GetDeserializeMessage(schema, this.ReceiveName.GetContractName(), this.ReceiveName.GetEntrypoint(), this.Parameter);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Types/VersionedModuleSchema.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,6 @@ public sealed record VersionedModuleSchema(byte[] Schema, ModuleSchemaVersion Ve
/// Deserialize schema.
/// </summary>
/// <returns>Schema as json uft8 encoded.</returns>
/// <exception cref="InteropBindingException">Thrown when schema wasn't able to be deserialized.</exception>
/// <exception cref="SchemaJsonException">Thrown when schema wasn't able to be deserialized.</exception>
public Utf8Json GetDeserializedSchema() => InteropBinding.SchemaDisplay(this);
};
32 changes: 16 additions & 16 deletions tests/UnitTests/Interop/InteropBindingTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,9 @@ public async Task GivenBadSchema_WhenSchemaDisplay_ThenThrowExceptionWithParseEr
var action = () => InteropBinding.SchemaDisplay(versionedModuleSchema);

// Assert
action.Should().Throw<InteropBindingException>()
action.Should().Throw<SchemaJsonException>()
.Where(e =>
e.Result == Result.VersionedSchemaErrorParseError &&
e.SchemaJsonResult == SchemaJsonResult.VersionedSchemaErrorParseError &&
e.Message.Equals("Parse error", StringComparison.Ordinal));
}

Expand Down Expand Up @@ -101,9 +101,9 @@ public async Task GivenBadReceiveParam_WhenDisplayReceiveParam_ThenThrowExceptio
var action = () => InteropBinding.GetReceiveContractParameter(versionedModuleSchema, contractIdentifier, entryPoint, parameter);

// Assert
action.Should().Throw<InteropBindingException>()
action.Should().Throw<SchemaJsonException>()
.Where(e =>
e.Result == Result.JsonError &&
e.SchemaJsonResult == SchemaJsonResult.JsonError &&
e.Message.StartsWith("Failed to deserialize AccountAddress due to: Could not parse AccountAddress", StringComparison.InvariantCulture));
}

Expand All @@ -123,9 +123,9 @@ public async Task GivenBadContractIdentifier_WhenDisplayReceiveParam_ThenThrowEx
var action = () => InteropBinding.GetReceiveContractParameter(versionedModuleSchema, contractIdentifier, entryPoint, parameter);

// Assert
action.Should().Throw<InteropBindingException>()
action.Should().Throw<SchemaJsonException>()
.Where(e =>
e.Result == Result.VersionedSchemaErrorNoContractInModule &&
e.SchemaJsonResult == SchemaJsonResult.VersionedSchemaErrorNoContractInModule &&
e.Message.Equals("Unable to find contract schema in module schema", StringComparison.Ordinal));
}

Expand All @@ -145,9 +145,9 @@ public async Task GivenBadEntrypoint_WhenDisplayReceiveParam_ThenThrowException(
var action = () => InteropBinding.GetReceiveContractParameter(versionedModuleSchema, contractIdentifier, entryPoint, parameter);

// Assert
action.Should().Throw<InteropBindingException>()
action.Should().Throw<SchemaJsonException>()
.Where(e =>
e.Result == Result.VersionedSchemaErrorNoReceiveInContract &&
e.SchemaJsonResult == SchemaJsonResult.VersionedSchemaErrorNoReceiveInContract &&
e.Message.Equals("Receive function schema not found in contract schema", StringComparison.Ordinal));
}

Expand All @@ -167,9 +167,9 @@ public async Task GivenBadSchema_WhenDisplayReceiveParam_ThenThrowException()
var action = () => InteropBinding.GetReceiveContractParameter(versionedModuleSchema, contractIdentifier, entryPoint, parameter);

// Assert
action.Should().Throw<InteropBindingException>()
action.Should().Throw<SchemaJsonException>()
.Where(e =>
e.Result == Result.VersionedSchemaErrorMissingSchemaVersion &&
e.SchemaJsonResult == SchemaJsonResult.VersionedSchemaErrorMissingSchemaVersion &&
e.Message.Equals("Missing Schema Version", StringComparison.Ordinal));
}

Expand Down Expand Up @@ -208,9 +208,9 @@ public async Task GivenBadSchema_WhenDisplayEvent_ThenThrowException()
var action = () => InteropBinding.GetEventContract(versionedModuleSchema, contractIdentifier, contractEvent);

// Assert
action.Should().Throw<InteropBindingException>()
action.Should().Throw<SchemaJsonException>()
.Where(e =>
e.Result == Result.VersionedSchemaErrorMissingSchemaVersion &&
e.SchemaJsonResult == SchemaJsonResult.VersionedSchemaErrorMissingSchemaVersion &&
e.Message.Equals("Missing Schema Version", StringComparison.Ordinal));
}

Expand All @@ -229,9 +229,9 @@ public async Task GivenBadContractIdentifier_WhenDisplayEvent_ThenThrowException
var action = () => InteropBinding.GetEventContract(versionedModuleSchema, contractIdentifier, contractEvent);

// Assert
action.Should().Throw<InteropBindingException>()
action.Should().Throw<SchemaJsonException>()
.Where(e =>
e.Result == Result.VersionedSchemaErrorNoContractInModule &&
e.SchemaJsonResult == SchemaJsonResult.VersionedSchemaErrorNoContractInModule &&
e.Message.Equals("Unable to find contract schema in module schema", StringComparison.Ordinal));
}

Expand All @@ -250,9 +250,9 @@ public async Task GivenBadContractEvent_WhenDisplayEvent_ThenThrowException()
var action = () => InteropBinding.GetEventContract(versionedModuleSchema, contractIdentifier, contractEvent);

// Assert
action.Should().Throw<InteropBindingException>()
action.Should().Throw<SchemaJsonException>()
.Where(e =>
e.Result == Result.JsonError &&
e.SchemaJsonResult == SchemaJsonResult.JsonError &&
e.Message.StartsWith("Failed to deserialize AccountAddress due to: Could not parse"));
}

Expand Down

0 comments on commit 2e5125f

Please sign in to comment.