Skip to content

Commit 6b4cbaa

Browse files
author
Serhii Yolkin
committed
Code cleanup
Update xml docs
1 parent 85d26d7 commit 6b4cbaa

File tree

12 files changed

+270
-40
lines changed

12 files changed

+270
-40
lines changed

UnityProject/Assets/LoomSDK/Source/Runtime/Address.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ private Address(byte[] publicKey, string chainId = DefaultChainId)
6161
}
6262

6363
/// <summary>
64-
/// Returns binary 20-byte array representing of the address.
64+
/// Returns binary 20-byte array representation of the address.
6565
/// </summary>
6666
/// <returns>20-byte array containing the address.</returns>
6767
public byte[] ToByteArray()
@@ -116,6 +116,12 @@ public static Address FromProtobufAddress(Protobuf.Address protobufAddress)
116116
String.IsNullOrWhiteSpace(protobufAddress.ChainId) ? DefaultChainId : protobufAddress.ChainId);
117117
}
118118

119+
/// <summary>
120+
/// Creates an Address instance from a byte array.
121+
/// </summary>
122+
/// <param name="address">binary 20-byte array representation of the address</param>
123+
/// <param name="chainId">Identifier of a DAppChain.</param>
124+
/// <returns>An address</returns>
119125
public static Address FromBytes(byte[] address, string chainId = DefaultChainId)
120126
{
121127
if (address == null)

UnityProject/Assets/LoomSDK/Source/Runtime/DAppChainClient.cs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ namespace Loom.Client
1919
/// <summary>
2020
/// Writes to & reads from a Loom DAppChain.
2121
/// </summary>
22-
public class DAppChainClient : IDisposable
22+
public class DAppChainClient : IDAppChainClientConfigurationProvider, IDisposable
2323
{
2424
private const string LogTag = "Loom.DAppChainClient";
2525

@@ -46,6 +46,9 @@ public class DAppChainClient : IDisposable
4646
/// </summary>
4747
public TxMiddleware TxMiddleware { get; set; }
4848

49+
/// <summary>
50+
/// Client options container.
51+
/// </summary>
4952
public DAppChainClientConfiguration Configuration { get; }
5053

5154
public IDAppChainClientCallExecutor CallExecutor { get; }
@@ -91,12 +94,14 @@ public event EventHandler<RawChainEventArgs> ChainEventReceived
9194
/// <param name="writeClient">RPC client to use for submitting transactions.</param>
9295
/// <param name="readClient">RPC client to use for querying DAppChain state.</param>
9396
/// <param name="configuration">Client configuration structure.</param>
94-
public DAppChainClient(IRpcClient writeClient, IRpcClient readClient, DAppChainClientConfiguration configuration = null)
97+
/// <param name="callExecutor">Blockchain call execution flow controller.</param>
98+
public DAppChainClient(IRpcClient writeClient, IRpcClient readClient, DAppChainClientConfiguration configuration = null, IDAppChainClientCallExecutor callExecutor = null)
9599
{
96100
this.writeClient = writeClient;
97101
this.readClient = readClient;
98-
Configuration = configuration ?? new DAppChainClientConfiguration();
99-
this.CallExecutor = new DefaultDAppChainClientCallExecutor(Configuration);
102+
103+
this.Configuration = configuration ?? new DAppChainClientConfiguration();
104+
this.CallExecutor = callExecutor ?? new DefaultDAppChainClientCallExecutor(this);
100105
}
101106

102107
public void Dispose()
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
using System;
2+
using UnityEngine;
3+
4+
namespace Loom.Client
5+
{
6+
/// <summary>
7+
/// Builds an instance of <see cref="DAppChainClient"/>.
8+
/// </summary>
9+
public class DAppChainClientBuilder
10+
{
11+
private IRpcClient reader;
12+
private IRpcClient writer;
13+
private ILogger logger;
14+
private DAppChainClientConfiguration configuration;
15+
private TxMiddleware txMiddleware;
16+
private IDAppChainClientCallExecutor callExecutor;
17+
18+
public DAppChainClientBuilder()
19+
{
20+
}
21+
22+
public static DAppChainClientBuilder Configure()
23+
{
24+
return new DAppChainClientBuilder();
25+
}
26+
27+
public DAppChainClientBuilder WithWriter(IRpcClient writer)
28+
{
29+
if (writer == null)
30+
throw new ArgumentNullException(nameof(writer));
31+
32+
this.writer = writer;
33+
return this;
34+
}
35+
36+
public DAppChainClientBuilder WithReader(IRpcClient reader)
37+
{
38+
if (reader == null)
39+
throw new ArgumentNullException(nameof(reader));
40+
41+
this.reader = reader;
42+
return this;
43+
}
44+
45+
public DAppChainClientBuilder WithTxMiddleware(TxMiddleware txMiddleware)
46+
{
47+
if (txMiddleware == null)
48+
throw new ArgumentNullException(nameof(txMiddleware));
49+
50+
this.txMiddleware = txMiddleware;
51+
return this;
52+
}
53+
54+
public DAppChainClientBuilder WithCallExecutor(IDAppChainClientCallExecutor callExecutor)
55+
{
56+
if (callExecutor == null)
57+
throw new ArgumentNullException(nameof(callExecutor));
58+
59+
this.callExecutor = callExecutor;
60+
return this;
61+
}
62+
63+
public DAppChainClientBuilder WithConfiguration(DAppChainClientConfiguration configuration)
64+
{
65+
this.configuration = configuration;
66+
return this;
67+
}
68+
69+
public DAppChainClientBuilder WithLogger(ILogger logger)
70+
{
71+
this.logger = logger;
72+
return this;
73+
}
74+
75+
public DAppChainClient Create()
76+
{
77+
DAppChainClientConfiguration configuration = this.configuration ?? new DAppChainClientConfiguration();
78+
IDAppChainClientCallExecutor callExecutor =
79+
this.callExecutor ?? new DefaultDAppChainClientCallExecutor(
80+
new DAppChainClientConfigurationProvider
81+
{
82+
Configuration = this.configuration
83+
});
84+
return new DAppChainClient(this.writer, this.reader, configuration, callExecutor)
85+
{
86+
TxMiddleware = this.txMiddleware,
87+
Logger = this.logger ?? NullLogger.Instance
88+
};
89+
}
90+
91+
private class DAppChainClientConfigurationProvider : IDAppChainClientConfigurationProvider
92+
{
93+
public DAppChainClientConfiguration Configuration { get; set; }
94+
}
95+
}
96+
}

UnityProject/Assets/LoomSDK/Source/Runtime/DAppChainClientBuilder.cs.meta

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

UnityProject/Assets/LoomSDK/Source/Runtime/DAppChainClientConfiguration.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,12 @@ public sealed class DAppChainClientConfiguration
2727
/// </summary>
2828
public int InvalidNonceTxRetries { get; set; } = 5;
2929
}
30+
31+
/// <summary>
32+
/// Represents an entity that stores a <see cref="DAppChainClientConfiguration"/>.
33+
/// </summary>
34+
public interface IDAppChainClientConfigurationProvider
35+
{
36+
DAppChainClientConfiguration Configuration { get; }
37+
}
3038
}

UnityProject/Assets/LoomSDK/Source/Runtime/DefaultDAppChainClientCallExecutor.cs

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,29 @@
66

77
namespace Loom.Client
88
{
9+
/// <summary>
10+
/// Default call executor provides handling for general blockchain situations.
11+
/// 1. Calls throw a <see cref="TimeoutException"/> if the calls receives no response for too long.
12+
/// 2. If the blockchain
13+
/// </summary>
914
public class DefaultDAppChainClientCallExecutor : IDAppChainClientCallExecutor
1015
{
1116
private readonly AsyncSemaphore callAsyncSemaphore = new AsyncSemaphore(1);
17+
private readonly IDAppChainClientConfigurationProvider configurationProvider;
1218

13-
public DAppChainClientConfiguration Configuration { get; }
14-
15-
public DefaultDAppChainClientCallExecutor(DAppChainClientConfiguration configuration)
19+
public DefaultDAppChainClientCallExecutor(IDAppChainClientConfigurationProvider configurationProvider)
1620
{
17-
this.Configuration = configuration;
21+
if (configurationProvider == null)
22+
throw new ArgumentNullException(nameof(configurationProvider));
23+
24+
this.configurationProvider = configurationProvider;
1825
}
1926

2027
public async Task<T> Call<T>(Func<Task<T>> taskProducer)
2128
{
2229
Task<T> task = (Task<T>) await ExecuteTaskWithRetryOnInvalidTxNonceException(
2330
() => ExecuteTaskWaitForOtherTasks(
24-
() => ExecuteTaskWithTimeout(taskProducer, this.Configuration.CallTimeout)
31+
() => ExecuteTaskWithTimeout(taskProducer, this.configurationProvider.Configuration.CallTimeout)
2532
));
2633

2734
return await task;
@@ -31,7 +38,7 @@ public async Task Call(Func<Task> taskProducer)
3138
{
3239
Task task = await ExecuteTaskWithRetryOnInvalidTxNonceException(
3340
() => ExecuteTaskWaitForOtherTasks(
34-
() => ExecuteTaskWithTimeout(taskProducer, this.Configuration.CallTimeout)
41+
() => ExecuteTaskWithTimeout(taskProducer, this.configurationProvider.Configuration.CallTimeout)
3542
));
3643

3744
await task;
@@ -40,7 +47,7 @@ public async Task Call(Func<Task> taskProducer)
4047
public async Task<T> StaticCall<T>(Func<Task<T>> taskProducer)
4148
{
4249
Task<T> task = (Task<T>) await ExecuteTaskWaitForOtherTasks(
43-
() => ExecuteTaskWithTimeout(taskProducer, this.Configuration.StaticCallTimeout)
50+
() => ExecuteTaskWithTimeout(taskProducer, this.configurationProvider.Configuration.StaticCallTimeout)
4451
);
4552

4653
return await task;
@@ -49,21 +56,21 @@ public async Task<T> StaticCall<T>(Func<Task<T>> taskProducer)
4956
public async Task StaticCall(Func<Task> taskProducer)
5057
{
5158
Task task = await ExecuteTaskWaitForOtherTasks(
52-
() => ExecuteTaskWithTimeout(taskProducer, this.Configuration.StaticCallTimeout)
59+
() => ExecuteTaskWithTimeout(taskProducer, this.configurationProvider.Configuration.StaticCallTimeout)
5360
);
5461

5562
await task;
5663
}
5764

5865
public async Task<T> NonBlockingStaticCall<T>(Func<Task<T>> taskProducer)
5966
{
60-
Task<T> task = (Task<T>) await ExecuteTaskWithTimeout(taskProducer, this.Configuration.StaticCallTimeout);
67+
Task<T> task = (Task<T>) await ExecuteTaskWithTimeout(taskProducer, this.configurationProvider.Configuration.StaticCallTimeout);
6168
return await task;
6269
}
6370

6471
public async Task NonBlockingStaticCall(Func<Task> taskProducer)
6572
{
66-
Task task = await ExecuteTaskWithTimeout(taskProducer, this.Configuration.StaticCallTimeout);
73+
Task task = await ExecuteTaskWithTimeout(taskProducer, this.configurationProvider.Configuration.StaticCallTimeout);
6774
await task;
6875
}
6976

@@ -136,7 +143,9 @@ private async Task<Task> ExecuteTaskWithRetryOnInvalidTxNonceException(Func<Task
136143
#else
137144
await Task.Delay(TimeSpan.FromSeconds(delay));
138145
#endif
139-
} while (this.Configuration.InvalidNonceTxRetries != 0 && badNonceCount <= this.Configuration.InvalidNonceTxRetries);
146+
} while (
147+
this.configurationProvider.Configuration.InvalidNonceTxRetries != 0 &&
148+
badNonceCount <= this.configurationProvider.Configuration.InvalidNonceTxRetries);
140149

141150
throw new InvalidTxNonceException(1, "sequence number does not match");
142151
}

UnityProject/Assets/LoomSDK/Source/Runtime/EvmContract.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,30 @@ public EvmContract(DAppChainClient client, Address contractAddress, Address call
3636
}
3737
}
3838

39+
/// <summary>
40+
/// Gets an instance of <see cref="EvmEvent"/> set up for working with Solidity event named <paramref name="name"/>.
41+
/// </summary>
42+
/// <param name="name">Solidity event name.</param>
3943
public EvmEvent GetEvent(string name)
4044
{
4145
return new EvmEvent(this, this.contractBuilder.GetEventBuilder(name));
4246
}
4347

48+
/// <summary>
49+
/// Gets an instance of <see cref="EvmEvent{T}"/> set up for working with Solidity event named <paramref name="name"/>.
50+
/// </summary>
51+
/// <param name="name">Solidity event name.</param>
52+
/// <typeparam name="T">Event DTO type.</typeparam>
53+
/// <see href="https://nethereum.readthedocs.io/en/latest/contracts/calling-transactions-events/"/>
54+
public EvmEvent<T> GetEvent<T>(string name) where T : new()
55+
{
56+
return new EvmEvent<T>(this, this.contractBuilder.GetEventBuilder(name));
57+
}
58+
59+
/// <summary>
60+
/// Retrieves the current block height.
61+
/// </summary>
62+
/// <returns></returns>
4463
public async Task<BigInteger> GetBlockHeight()
4564
{
4665
return await this.Client.CallExecutor.StaticCall(

0 commit comments

Comments
 (0)