Skip to content

Commit

Permalink
fix model names, add validation and error handling for preview-v2.
Browse files Browse the repository at this point in the history
  • Loading branch information
PawelPawelec-RDX committed Nov 11, 2024
1 parent 1a7d53f commit a4a52a3
Show file tree
Hide file tree
Showing 8 changed files with 150 additions and 58 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -217,10 +217,10 @@ public TransactionPreviewService(

private async Task<GatewayModel.TransactionPreviewV2Response> HandlePreviewV2AndCreateResponse(GatewayModel.TransactionPreviewV2Request request, CancellationToken token)
{
CoreModel.PreviewTransaction MapPreviewTransaction(GatewayModel.PreviewTransactionV2 previewTransaction)
CoreModel.PreviewTransaction MapPreviewTransaction(GatewayModel.PreviewTransaction previewTransaction)
=> previewTransaction switch
{
GatewayModel.CompiledPreviewTransactionV2 compiledPreviewTransactionV2 => new CoreModel.CompiledPreviewTransaction(compiledPreviewTransactionV2.PreviewTransactionHex),
GatewayModel.CompiledPreviewTransaction compiledPreviewTransactionV2 => new CoreModel.CompiledPreviewTransaction(compiledPreviewTransactionV2.PreviewTransactionHex),
_ => throw new ArgumentOutOfRangeException(nameof(previewTransaction)),
};

Expand All @@ -240,7 +240,7 @@ CoreModel.PreviewTransaction MapPreviewTransaction(GatewayModel.PreviewTransacti
logs: request.OptIns?.Logs ?? false
));

var result = await CoreApiErrorWrapper.ResultOrError<CoreModel.TransactionPreviewV2Response, CoreModel.BasicErrorResponse>(
var result = await CoreApiErrorWrapper.ResultOrError<CoreModel.TransactionPreviewV2Response, CoreModel.TransactionPreviewV2ErrorResponse>(
() => _coreApiProvider.TransactionApi.TransactionPreviewV2PostAsync(coreRequest, token));

if (result.Succeeded)
Expand All @@ -251,7 +251,7 @@ CoreModel.PreviewTransaction MapPreviewTransaction(GatewayModel.PreviewTransacti
radixEngineToolkitReceipt: coreResponse.RadixEngineToolkitReceipt,
atLedgerStateVersion: coreResponse.AtLedgerState.StateVersion,
receipt: coreResponse.Receipt,
logs: coreResponse.Logs.Select(l => new GatewayModel.TransactionPreviewResponseLogsInner(l.Level, l.Message)).ToList());
logs: coreResponse.Logs?.Select(l => new GatewayModel.TransactionPreviewResponseLogsInner(l.Level, l.Message)).ToList());
}

throw InvalidRequestException.FromOtherError(result.FailureResponse.Message);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
/* Copyright 2021 Radix Publishing Ltd incorporated in Jersey (Channel Islands).
*
* Licensed under the Radix License, Version 1.0 (the "License"); you may not use this
* file except in compliance with the License. You may obtain a copy of the License at:
*
* radixfoundation.org/licenses/LICENSE-v1
*
* The Licensor hereby grants permission for the Canonical version of the Work to be
* published, distributed and used under or by reference to the Licensor’s trademark
* Radix ® and use of any unregistered trade names, logos or get-up.
*
* The Licensor provides the Work (and each Contributor provides its Contributions) on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied,
* including, without limitation, any warranties or conditions of TITLE, NON-INFRINGEMENT,
* MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE.
*
* Whilst the Work is capable of being deployed, used and adopted (instantiated) to create
* a distributed ledger it is your responsibility to test and validate the code, together
* with all logic and performance of that code under all foreseeable scenarios.
*
* The Licensor does not make or purport to make and hereby excludes liability for all
* and any representation, warranty or undertaking in any form whatsoever, whether express
* or implied, to any entity or person, including any representation, warranty or
* undertaking, as to the functionality security use, value or other characteristics of
* any distributed ledger nor in respect the functioning or value of any tokens which may
* be created stored or transferred using the Work. The Licensor does not warrant that the
* Work or any use of the Work complies with any law or regulation in any territory where
* it may be implemented or used or that it will be appropriate for any specific purpose.
*
* Neither the licensor nor any current or former employees, officers, directors, partners,
* trustees, representatives, agents, advisors, contractors, or volunteers of the Licensor
* shall be liable for any direct or indirect, special, incidental, consequential or other
* losses of any kind, in tort, contract or otherwise (including but not limited to loss
* of revenue, income or profits, or loss of use or data, or loss of reputation, or loss
* of any economic or other opportunity of whatsoever nature or howsoever arising), arising
* out of or in connection with (without limitation of any use, misuse, of any ledger system
* or use made or its functionality or any performance or operation of any code or protocol
* caused by bugs or programming or logic errors or otherwise);
*
* A. any offer, purchase, holding, use, sale, exchange or transmission of any
* cryptographic keys, tokens or assets created, exchanged, stored or arising from any
* interaction with the Work;
*
* B. any failure in a transmission or loss of any token or assets keys or other digital
* artefacts due to errors in transmission;
*
* C. bugs, hacks, logic errors or faults in the Work or any communication;
*
* D. system software or apparatus including but not limited to losses caused by errors
* in holding or transmitting tokens by any third-party;
*
* E. breaches or failure of security including hacker attacks, loss or disclosure of
* password, loss of private key, unauthorised use or misuse of such passwords or keys;
*
* F. any losses including loss of anticipated savings or other benefits resulting from
* use of the Work or any changes to the Work (however implemented).
*
* You are solely responsible for; testing, validating and evaluation of all operation
* logic, functionality, security and appropriateness of using the Work for any commercial
* or non-commercial purpose and for any reproduction or redistribution by You of the
* Work. You assume all risks associated with Your use of the Work and the exercise of
* permissions under this License.
*/

using FluentValidation;
using RadixDlt.NetworkGateway.GatewayApiSdk.Model;

namespace RadixDlt.NetworkGateway.GatewayApi.Validators;

internal class TransactionPreviewV2RequestValidator : AbstractValidator<TransactionPreviewV2Request>
{
public TransactionPreviewV2RequestValidator()
{
RuleFor(x => x.PreviewTransaction)
.NotNull();

RuleFor(x => x.PreviewTransaction)
.SetInheritanceValidator(x => x.Add(new CompiledPreviewTransactionValidator()));

RuleFor(x => x.Flags)
.NotNull();
}
}

internal class CompiledPreviewTransactionValidator : AbstractValidator<CompiledPreviewTransaction>
{
public CompiledPreviewTransactionValidator()
{
RuleFor(x => x.PreviewTransactionHex).NotEmpty();
RuleFor(x => x.Type).NotEmpty().IsInEnum();
}
}
14 changes: 7 additions & 7 deletions src/RadixDlt.NetworkGateway.GatewayApi/gateway-api-schema.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3707,7 +3707,7 @@ components:
- flags
properties:
preview_transaction:
$ref: "#/components/schemas/PreviewTransactionV2"
$ref: "#/components/schemas/PreviewTransaction"
flags:
$ref: "#/components/schemas/TransactionPreviewFlags"
opt_ins:
Expand Down Expand Up @@ -3735,25 +3735,25 @@ components:
This flag controls whether the preview response will include execution logs.
If not provided, this defaults to `false` and no logs will be provided in the response.
PreviewTransactionV2Type:
PreviewTransactionType:
type: string
enum:
- Compiled
PreviewTransactionV2:
PreviewTransaction:
type: object
required:
- type
properties:
type:
$ref: "#/components/schemas/PreviewTransactionV2Type"
$ref: "#/components/schemas/PreviewTransactionType"
discriminator:
propertyName: type
mapping:
# NOTE: These need to match PreviewTransactionType
Compiled: '#/components/schemas/CompiledPreviewTransactionV2'
CompiledPreviewTransactionV2:
Compiled: '#/components/schemas/CompiledPreviewTransaction'
CompiledPreviewTransaction:
allOf:
- $ref: "#/components/schemas/PreviewTransactionV2"
- $ref: "#/components/schemas/PreviewTransaction"
- type: object
required:
- preview_transaction_hex
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,29 +91,29 @@
namespace RadixDlt.NetworkGateway.GatewayApiSdk.Model
{
/// <summary>
/// CompiledPreviewTransactionV2
/// CompiledPreviewTransaction
/// </summary>
[DataContract(Name = "CompiledPreviewTransactionV2")]
[DataContract(Name = "CompiledPreviewTransaction")]
[JsonConverter(typeof(JsonSubtypes), "type")]
[JsonSubtypes.KnownSubType(typeof(CompiledPreviewTransactionV2), "Compiled")]
public partial class CompiledPreviewTransactionV2 : PreviewTransactionV2, IEquatable<CompiledPreviewTransactionV2>
[JsonSubtypes.KnownSubType(typeof(CompiledPreviewTransaction), "Compiled")]
public partial class CompiledPreviewTransaction : PreviewTransaction, IEquatable<CompiledPreviewTransaction>
{
/// <summary>
/// Initializes a new instance of the <see cref="CompiledPreviewTransactionV2" /> class.
/// Initializes a new instance of the <see cref="CompiledPreviewTransaction" /> class.
/// </summary>
[JsonConstructorAttribute]
protected CompiledPreviewTransactionV2() { }
protected CompiledPreviewTransaction() { }
/// <summary>
/// Initializes a new instance of the <see cref="CompiledPreviewTransactionV2" /> class.
/// Initializes a new instance of the <see cref="CompiledPreviewTransaction" /> class.
/// </summary>
/// <param name="previewTransactionHex">A hex-encoded, compiled &#x60;RawPreviewTransaction&#x60;. As of Cuttlefish, only &#x60;PreviewTransactionV2&#x60; is supported. A &#x60;PreviewTransactionV2&#x60; can be created with a v2 transaction builder: * If using Rust, it can be created with a &#x60;TransactionV2Builder&#x60; using &#x60;build_preview_transaction()&#x60; and then converted to hex with &#x60;preview_transaction.to_raw().unwrap().to_hex()&#x60; * If using the toolkit, you can create this using the v2 transaction builder. Some subtleties: * Partial transactions can&#39;t be previewed. Instead, they must be wrapped inside a transaction wrapper, so that the engine knows how to yield to them appropriately. * Currently the builder assumes that the signed partial transactions have real signatures. This isn&#39;t strictly required, and we may create a builder in future which allows providing public keys when building partial transactions for use in preview. * If you don&#39;t have signatures to hand, you can simply not sign the partial transactions, and then use the &#x60;assume_all_signature_proofs&#x60; preview flag, although be advised that this may result in the fee estimate being slightly lower during preview. * We may create more ergonomic builders for PreviewTransactions which allow use of public keys to denote the signers of subintents. Let us know if this is important for your use case. (required).</param>
/// <param name="type">type (required) (default to PreviewTransactionV2Type.Compiled).</param>
public CompiledPreviewTransactionV2(string previewTransactionHex = default(string), PreviewTransactionV2Type type = PreviewTransactionV2Type.Compiled) : base(type)
/// <param name="type">type (required) (default to PreviewTransactionType.Compiled).</param>
public CompiledPreviewTransaction(string previewTransactionHex = default(string), PreviewTransactionType type = PreviewTransactionType.Compiled) : base(type)
{
// to ensure "previewTransactionHex" is required (not null)
if (previewTransactionHex == null)
{
throw new ArgumentNullException("previewTransactionHex is a required property for CompiledPreviewTransactionV2 and cannot be null");
throw new ArgumentNullException("previewTransactionHex is a required property for CompiledPreviewTransaction and cannot be null");
}
this.PreviewTransactionHex = previewTransactionHex;
}
Expand All @@ -132,7 +132,7 @@ protected CompiledPreviewTransactionV2() { }
public override string ToString()
{
StringBuilder sb = new StringBuilder();
sb.Append("class CompiledPreviewTransactionV2 {\n");
sb.Append("class CompiledPreviewTransaction {\n");
sb.Append(" ").Append(base.ToString().Replace("\n", "\n ")).Append("\n");
sb.Append(" PreviewTransactionHex: ").Append(PreviewTransactionHex).Append("\n");
sb.Append("}\n");
Expand All @@ -155,15 +155,15 @@ public override string ToJson()
/// <returns>Boolean</returns>
public override bool Equals(object input)
{
return this.Equals(input as CompiledPreviewTransactionV2);
return this.Equals(input as CompiledPreviewTransaction);
}

/// <summary>
/// Returns true if CompiledPreviewTransactionV2 instances are equal
/// Returns true if CompiledPreviewTransaction instances are equal
/// </summary>
/// <param name="input">Instance of CompiledPreviewTransactionV2 to be compared</param>
/// <param name="input">Instance of CompiledPreviewTransaction to be compared</param>
/// <returns>Boolean</returns>
public bool Equals(CompiledPreviewTransactionV2 input)
public bool Equals(CompiledPreviewTransaction input)
{
if (input == null)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,26 +90,26 @@
namespace RadixDlt.NetworkGateway.GatewayApiSdk.Model
{
/// <summary>
/// CompiledPreviewTransactionV2AllOf
/// CompiledPreviewTransactionAllOf
/// </summary>
[DataContract(Name = "CompiledPreviewTransactionV2_allOf")]
public partial class CompiledPreviewTransactionV2AllOf : IEquatable<CompiledPreviewTransactionV2AllOf>
[DataContract(Name = "CompiledPreviewTransaction_allOf")]
public partial class CompiledPreviewTransactionAllOf : IEquatable<CompiledPreviewTransactionAllOf>
{
/// <summary>
/// Initializes a new instance of the <see cref="CompiledPreviewTransactionV2AllOf" /> class.
/// Initializes a new instance of the <see cref="CompiledPreviewTransactionAllOf" /> class.
/// </summary>
[JsonConstructorAttribute]
protected CompiledPreviewTransactionV2AllOf() { }
protected CompiledPreviewTransactionAllOf() { }
/// <summary>
/// Initializes a new instance of the <see cref="CompiledPreviewTransactionV2AllOf" /> class.
/// Initializes a new instance of the <see cref="CompiledPreviewTransactionAllOf" /> class.
/// </summary>
/// <param name="previewTransactionHex">A hex-encoded, compiled &#x60;RawPreviewTransaction&#x60;. As of Cuttlefish, only &#x60;PreviewTransactionV2&#x60; is supported. A &#x60;PreviewTransactionV2&#x60; can be created with a v2 transaction builder: * If using Rust, it can be created with a &#x60;TransactionV2Builder&#x60; using &#x60;build_preview_transaction()&#x60; and then converted to hex with &#x60;preview_transaction.to_raw().unwrap().to_hex()&#x60; * If using the toolkit, you can create this using the v2 transaction builder. Some subtleties: * Partial transactions can&#39;t be previewed. Instead, they must be wrapped inside a transaction wrapper, so that the engine knows how to yield to them appropriately. * Currently the builder assumes that the signed partial transactions have real signatures. This isn&#39;t strictly required, and we may create a builder in future which allows providing public keys when building partial transactions for use in preview. * If you don&#39;t have signatures to hand, you can simply not sign the partial transactions, and then use the &#x60;assume_all_signature_proofs&#x60; preview flag, although be advised that this may result in the fee estimate being slightly lower during preview. * We may create more ergonomic builders for PreviewTransactions which allow use of public keys to denote the signers of subintents. Let us know if this is important for your use case. (required).</param>
public CompiledPreviewTransactionV2AllOf(string previewTransactionHex = default(string))
public CompiledPreviewTransactionAllOf(string previewTransactionHex = default(string))
{
// to ensure "previewTransactionHex" is required (not null)
if (previewTransactionHex == null)
{
throw new ArgumentNullException("previewTransactionHex is a required property for CompiledPreviewTransactionV2AllOf and cannot be null");
throw new ArgumentNullException("previewTransactionHex is a required property for CompiledPreviewTransactionAllOf and cannot be null");
}
this.PreviewTransactionHex = previewTransactionHex;
}
Expand All @@ -128,7 +128,7 @@ protected CompiledPreviewTransactionV2AllOf() { }
public override string ToString()
{
StringBuilder sb = new StringBuilder();
sb.Append("class CompiledPreviewTransactionV2AllOf {\n");
sb.Append("class CompiledPreviewTransactionAllOf {\n");
sb.Append(" PreviewTransactionHex: ").Append(PreviewTransactionHex).Append("\n");
sb.Append("}\n");
return sb.ToString();
Expand All @@ -150,15 +150,15 @@ public virtual string ToJson()
/// <returns>Boolean</returns>
public override bool Equals(object input)
{
return this.Equals(input as CompiledPreviewTransactionV2AllOf);
return this.Equals(input as CompiledPreviewTransactionAllOf);
}

/// <summary>
/// Returns true if CompiledPreviewTransactionV2AllOf instances are equal
/// Returns true if CompiledPreviewTransactionAllOf instances are equal
/// </summary>
/// <param name="input">Instance of CompiledPreviewTransactionV2AllOf to be compared</param>
/// <param name="input">Instance of CompiledPreviewTransactionAllOf to be compared</param>
/// <returns>Boolean</returns>
public bool Equals(CompiledPreviewTransactionV2AllOf input)
public bool Equals(CompiledPreviewTransactionAllOf input)
{
if (input == null)
{
Expand Down
Loading

0 comments on commit a4a52a3

Please sign in to comment.