Skip to content

Commit

Permalink
Refactoring: enable nullability, use latest lang version, etc.
Browse files Browse the repository at this point in the history
  • Loading branch information
Groxan committed Mar 19, 2023
1 parent fafa00b commit 552c9c8
Show file tree
Hide file tree
Showing 198 changed files with 1,207 additions and 2,134 deletions.
4 changes: 2 additions & 2 deletions Netezos.Ledger/TezosLedgerClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ namespace Netezos.Ledger
{
public class TezosLedgerClient : LedgerClientBase
{
byte TezosWalletCLA = 0x80;
byte[] KeyPath;
readonly byte TezosWalletCLA = 0x80;
readonly byte[] KeyPath;

enum Instruction { // taken from https://github.com/obsidiansystems/ledger-app-tezos/blob/master/APDUs.md
InsVersion = 0x00,
Expand Down
2 changes: 1 addition & 1 deletion Netezos.Tests/Encoding/MichelineEncodingTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ static string CreateDeepPair(int depth)
return $@"{{""prim"":""pair"",""args"":[{{""prim"":""unit""}},{CreateDeepPair(depth - 1)}],""annots"":[""%d{depth}""]}}";
}

var json2 = CreateDeepPair(12_000);
var json2 = CreateDeepPair(10_000);
var m2 = Micheline.FromJson(json2);
var b2 = m2.ToBytes();
Assert.NotNull(m2);
Expand Down
8 changes: 6 additions & 2 deletions Netezos.Tests/Rpc/SettingsFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace Netezos.Tests.Rpc
{
public class SettingsFixture : IDisposable
{
static readonly object Crit = new object();
static readonly object Crit = new();

public TezosRpc Rpc { get; }
public string TestContract { get; }
Expand All @@ -32,6 +32,10 @@ public SettingsFixture()
}
}

public void Dispose() => Rpc.Dispose();
public void Dispose()
{
Rpc.Dispose();
GC.SuppressFinalize(this);
}
}
}
9 changes: 3 additions & 6 deletions Netezos/Contracts/ContractParameter.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.Json;
using System.Text.Json;
using Netezos.Encoding;

namespace Netezos.Contracts
Expand All @@ -14,10 +11,10 @@ public class ContractParameter

public ContractParameter(IMicheline parameter)
{
if ((parameter as MichelinePrim)?.Prim != PrimType.parameter)
if (parameter is not MichelinePrim { Prim: PrimType.parameter } prim)
throw new ArgumentException("Invalid micheline: expected prim parameter");

var root = new ParameterSchema(parameter as MichelinePrim);
var root = new ParameterSchema(prim);
Entrypoints = new Dictionary<string, Schema> { { "default", root.Schema } };

if (root.Field?.Length > 0)
Expand Down
11 changes: 4 additions & 7 deletions Netezos/Contracts/ContractScript.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.Json;
using System.Text.Json;
using Netezos.Encoding;

namespace Netezos.Contracts
Expand All @@ -15,13 +12,13 @@ public class ContractScript

public ContractScript(IMicheline script)
{
if (!(script is MichelineArray array) || array.Count < 3)
if (script is not MichelineArray array || array.Count < 3)
throw new FormatException("Invalid micheline");

var parameter = array.FirstOrDefault(x => (x as MichelinePrim)?.Prim == PrimType.parameter) as MichelinePrim
var parameter = array.FirstOrDefault(x => x is MichelinePrim { Prim: PrimType.parameter }) as MichelinePrim
?? throw new FormatException("Invalid micheline parameters");

var storage = array.FirstOrDefault(x => (x as MichelinePrim)?.Prim == PrimType.storage) as MichelinePrim
var storage = array.FirstOrDefault(x => x is MichelinePrim { Prim: PrimType.storage }) as MichelinePrim
?? throw new FormatException("Invalid micheline storage");

Parameter = new ContractParameter(parameter);
Expand Down
7 changes: 3 additions & 4 deletions Netezos/Contracts/ContractStorage.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using System.Text.Json;
using System.Text.Json;
using Netezos.Encoding;

namespace Netezos.Contracts
Expand All @@ -10,10 +9,10 @@ public class ContractStorage

public ContractStorage(IMicheline storage)
{
if ((storage as MichelinePrim)?.Prim != PrimType.storage)
if (storage is not MichelinePrim { Prim: PrimType.storage } prim)
throw new ArgumentException("Invalid micheline: expected prim storage");

Schema = new StorageSchema(storage as MichelinePrim).Schema;
Schema = new StorageSchema(prim).Schema;
}

public IMicheline Optimize(IMicheline value, bool immutable = true)
Expand Down
163 changes: 77 additions & 86 deletions Netezos/Contracts/Schema.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Collections;
using System.Text.Json;
using Netezos.Encoding;

Expand All @@ -11,63 +7,61 @@ namespace Netezos.Contracts
public abstract class Schema
{
#region static
public static Schema Create(MichelinePrim micheline)
{
switch (micheline.Prim)
{
case PrimType.address: return new AddressSchema(micheline);
case PrimType.bls12_381_fr: return new Bls12381FrSchema(micheline);
case PrimType.bls12_381_g1: return new Bls12381G1Schema(micheline);
case PrimType.bls12_381_g2: return new Bls12381G2Schema(micheline);
case PrimType.big_map: return new BigMapSchema(micheline);
case PrimType.@bool: return new BoolSchema(micheline);
case PrimType.bytes: return new BytesSchema(micheline);
case PrimType.chest: return new ChestSchema(micheline);
case PrimType.chest_key: return new ChestKeySchema(micheline);
case PrimType.chain_id: return new ChainIdSchema(micheline);
case PrimType.constant: return new ConstantSchema(micheline);
case PrimType.contract: return new ContractSchema(micheline);
case PrimType.@int: return new IntSchema(micheline);
case PrimType.key: return new KeySchema(micheline);
case PrimType.key_hash: return new KeyHashSchema(micheline);
case PrimType.lambda: return new LambdaSchema(micheline);
case PrimType.list: return new ListSchema(micheline);
case PrimType.map: return new MapSchema(micheline);
case PrimType.mutez: return new MutezSchema(micheline);
case PrimType.nat: return new NatSchema(micheline);
case PrimType.never: return new NeverSchema(micheline);
case PrimType.option: return new OptionSchema(micheline);
case PrimType.or: return new OrSchema(micheline);
case PrimType.pair: return new PairSchema(micheline);
case PrimType.sapling_state: return new SaplingStateSchema(micheline);
case PrimType.sapling_transaction: return new SaplingTransactionSchema(micheline);
case PrimType.sapling_transaction_deprecated: return new SaplingTransactionDeprecatedSchema(micheline);
case PrimType.set: return new SetSchema(micheline);
case PrimType.signature: return new SignatureSchema(micheline);
case PrimType.@string: return new StringSchema(micheline);
case PrimType.ticket: return new TicketSchema(micheline);
case PrimType.timestamp: return new TimestampSchema(micheline);
case PrimType.tx_rollup_l2_address: return new TxRollupL2AddressSchema(micheline);
case PrimType.unit: return new UnitSchema(micheline);
case PrimType.operation: return new OperationSchema(micheline);
case PrimType.view: return new ViewSchema(micheline);
default:
throw new NotImplementedException($"Schema for prim {micheline.Prim} is not implemented");
}
}
public static Schema Create(MichelinePrim micheline) => micheline.Prim switch
{
PrimType.address => new AddressSchema(micheline),
PrimType.big_map => new BigMapSchema(micheline),
PrimType.bls12_381_fr => new Bls12381FrSchema(micheline),
PrimType.bls12_381_g1 => new Bls12381G1Schema(micheline),
PrimType.bls12_381_g2 => new Bls12381G2Schema(micheline),
PrimType.@bool => new BoolSchema(micheline),
PrimType.bytes => new BytesSchema(micheline),
PrimType.chain_id => new ChainIdSchema(micheline),
PrimType.chest_key => new ChestKeySchema(micheline),
PrimType.chest => new ChestSchema(micheline),
PrimType.constant => new ConstantSchema(micheline),
PrimType.contract => new ContractSchema(micheline),
PrimType.@int => new IntSchema(micheline),
PrimType.key_hash => new KeyHashSchema(micheline),
PrimType.key => new KeySchema(micheline),
PrimType.lambda => new LambdaSchema(micheline),
PrimType.list => new ListSchema(micheline),
PrimType.map => new MapSchema(micheline),
PrimType.mutez => new MutezSchema(micheline),
PrimType.nat => new NatSchema(micheline),
PrimType.never => new NeverSchema(micheline),
PrimType.operation=> new OperationSchema(micheline),
PrimType.option => new OptionSchema(micheline),
PrimType.or => new OrSchema(micheline),
PrimType.pair => new PairSchema(micheline),
PrimType.parameter => new ParameterSchema(micheline),
PrimType.sapling_state => new SaplingStateSchema(micheline),
PrimType.sapling_transaction_deprecated => new SaplingTransactionDeprecatedSchema(micheline),
PrimType.sapling_transaction => new SaplingTransactionSchema(micheline),
PrimType.set => new SetSchema(micheline),
PrimType.signature => new SignatureSchema(micheline),
PrimType.storage => new StorageSchema(micheline),
PrimType.@string => new StringSchema(micheline),
PrimType.ticket => new TicketSchema(micheline),
PrimType.timestamp => new TimestampSchema(micheline),
PrimType.tx_rollup_l2_address => new TxRollupL2AddressSchema(micheline),
PrimType.unit => new UnitSchema(micheline),
PrimType.view => new ViewSchema(micheline),
_ => throw new NotImplementedException($"Schema for prim {micheline.Prim} is not implemented")
};
#endregion

public abstract PrimType Prim { get; }

public string Field { get; }
public string Type { get; }
public string? Field { get; }
public string? Type { get; }

internal int Index = -1;
internal string Annot = null;
internal string? Annot = null;

internal string Suffix => Index > -1 ? $"_{Index}" : string.Empty;

public virtual string Name => (Annot ?? Prim.ToString()) + Suffix;
public virtual string? Name => (Annot ?? Prim.ToString()) + Suffix;
public virtual string Signature => Prim.ToString();

protected Schema(MichelinePrim micheline)
Expand All @@ -87,44 +81,41 @@ public TreeView ToTreeView(IMicheline value)

public string Humanize(JsonWriterOptions options = default)
{
using (var mem = new MemoryStream())
using (var writer = new Utf8JsonWriter(mem, options))
{
writer.WriteStartObject();
writer.WritePropertyName($"schema:{Signature}");
WriteValue(writer);
writer.WriteEndObject();
writer.Flush();
using var mem = new MemoryStream();
using var writer = new Utf8JsonWriter(mem, options);

writer.WriteStartObject();
writer.WritePropertyName($"schema:{Signature}");
WriteValue(writer);
writer.WriteEndObject();
writer.Flush();

return Utf8.Convert(mem.ToArray());
}
return Utf8.Convert(mem.ToArray());
}

public string Humanize(IMicheline value, JsonWriterOptions options = default)
{
using (var mem = new MemoryStream())
using (var writer = new Utf8JsonWriter(mem, options))
{
WriteValue(writer, value);
writer.Flush();
using var mem = new MemoryStream();
using var writer = new Utf8JsonWriter(mem, options);

WriteValue(writer, value);
writer.Flush();

return Utf8.Convert(mem.ToArray());
}
return Utf8.Convert(mem.ToArray());
}

public string GetJsonSchema(JsonWriterOptions options = default)
{
using (var mem = new MemoryStream())
using (var writer = new Utf8JsonWriter(mem, options))
{
writer.WriteStartObject();
writer.WriteString("$schema", "http://json-schema.org/draft/2019-09/schema#");
WriteJsonSchema(writer);
writer.WriteEndObject();
writer.Flush();
using var mem = new MemoryStream();
using var writer = new Utf8JsonWriter(mem, options);

writer.WriteStartObject();
writer.WriteString("$schema", "http://json-schema.org/draft/2019-09/schema#");
WriteJsonSchema(writer);
writer.WriteEndObject();
writer.Flush();

return Utf8.Convert(mem.ToArray());
}
return Utf8.Convert(mem.ToArray());
}

public IMicheline ToMicheline()
Expand Down Expand Up @@ -159,7 +150,7 @@ public virtual IMicheline MapObject(object obj, bool isValue = false)
throw MapFailedException($"enumerable is empty");
return MapValue(e.Current);
case JsonElement json:
if (!json.TryGetProperty(Name, out var jsonProp))
if (!json.TryGetProperty(Name!, out var jsonProp))
throw MapFailedException($"no such property");
return MapValue(jsonProp);
default:
Expand All @@ -174,12 +165,12 @@ protected virtual IMicheline MapValue(object value)
throw new NotImplementedException();
}

protected virtual List<IMicheline> GetArgs()
protected virtual List<IMicheline>? GetArgs()
{
return null;
}

protected List<IAnnotation> GetAnnotations()
protected List<IAnnotation>? GetAnnotations()
{
if (Type != null)
{
Expand Down Expand Up @@ -208,7 +199,7 @@ internal virtual void WriteValue(Utf8JsonWriter writer)

internal virtual void WriteProperty(Utf8JsonWriter writer, IMicheline value)
{
writer.WritePropertyName(Name);
writer.WritePropertyName(Name!);
WriteValue(writer, value);
}

Expand All @@ -224,11 +215,11 @@ internal virtual void WriteJsonSchema(Utf8JsonWriter writer)
writer.WriteString("$comment", Prim.ToString());
}

internal virtual TreeView GetTreeView(TreeView parent, IMicheline value, string name = null, Schema schema = null)
internal virtual TreeView GetTreeView(TreeView? parent, IMicheline value, string? name = null, Schema? schema = null)
{
return new TreeView
{
Name = name ?? Name,
Name = name ?? Name!,
Schema = schema ?? this,
Value = value,
Parent = parent
Expand Down
24 changes: 8 additions & 16 deletions Netezos/Contracts/Schemas/AddressSchema.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,20 +84,13 @@ public string Flatten(IMicheline value)

protected override IMicheline MapValue(object value)
{
switch (value)
return value switch
{
case string str:
// TODO: validation & optimization
return new MichelineString(str);
case byte[] bytes:
// TODO: validation
return new MichelineBytes(bytes);
case JsonElement json when json.ValueKind == JsonValueKind.String:
// TODO: validation & optimization
return new MichelineString(json.GetString());
default:
throw MapFailedException("invalid value");
}
string str => new MichelineString(str),
byte[] bytes => new MichelineBytes(bytes),
JsonElement { ValueKind: JsonValueKind.String } json => new MichelineString(json.GetString()!),
_ => throw MapFailedException("invalid value"),
};
}

public override IMicheline Optimize(IMicheline value)
Expand All @@ -107,7 +100,7 @@ public override IMicheline Optimize(IMicheline value)

string address;
byte[] addressBytes;
byte[] entrypointBytes;
byte[]? entrypointBytes;

if (micheStr.Value.StartsWith("txr1"))
{
Expand Down Expand Up @@ -169,8 +162,7 @@ public override IMicheline Optimize(IMicheline value)
throw FormatException(value);
}

if (entrypointBytes != null)
entrypointBytes.CopyTo(res, 22);
entrypointBytes?.CopyTo(res, 22);

return new MichelineBytes(res);
}
Expand Down
Loading

0 comments on commit 552c9c8

Please sign in to comment.