Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/issue 13 #37

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 18 additions & 5 deletions Centaurus.Alpha/Controllers/ConstellationController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ public ConstellationInfo Info()
MinAccountBalance = Global.Constellation.MinAccountBalance,
MinAllowedLotSize = Global.Constellation.MinAllowedLotSize,
StellarNetwork = network,
Assets = assets
Assets = assets,
RequestRateLimits = Global.Constellation.RequestRateLimits
};
}

Expand All @@ -50,11 +51,23 @@ public async Task<IActionResult> Init([FromBody] ConstellationInitModel constell
if (constellationInit == null)
return StatusCode(415);

if (constellationInit.RequestRateLimits == null)
throw new ArgumentNullException(nameof(constellationInit.RequestRateLimits), "RequestRateLimits parameter is required.");
var requestRateLimits = new RequestRateLimits
{
HourLimit = constellationInit.RequestRateLimits.HourLimit,
MinuteLimit = constellationInit.RequestRateLimits.MinuteLimit
};

var constellationInitializer = new ConstellationInitializer(
hawthorne-abendsen marked this conversation as resolved.
Show resolved Hide resolved
constellationInit.Auditors.Select(a => KeyPair.FromAccountId(a)),
constellationInit.MinAccountBalance,
constellationInit.MinAllowedLotSize,
constellationInit.Assets.Select(a => AssetSettings.FromCode(a))
new ConstellationInitInfo
{
Auditors = constellationInit.Auditors.Select(a => KeyPair.FromAccountId(a)).ToArray(),
MinAccountBalance = constellationInit.MinAccountBalance,
MinAllowedLotSize = constellationInit.MinAllowedLotSize,
Assets = constellationInit.Assets.Select(a => AssetSettings.FromCode(a)).ToArray(),
RequestRateLimits = requestRateLimits
}
);

await constellationInitializer.Init();
Expand Down
8 changes: 3 additions & 5 deletions Centaurus.Alpha/Models/ConstellationInfo.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
using Centaurus.Models;
using stellar_dotnet_sdk;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace Centaurus.Alpha
{
public class ConstellationInfo
{
public ApplicationState State{ get; set; }
public ApplicationState State { get; set; }

public string Vault { get; set; }

Expand All @@ -23,6 +19,8 @@ public class ConstellationInfo

public Asset[] Assets { get; set; }

public RequestRateLimits RequestRateLimits { get; set; }

public class Network
{
public Network(string passphrase, string horizon)
Expand Down
7 changes: 3 additions & 4 deletions Centaurus.Alpha/Models/ConstellationInitModel.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.Threading.Tasks;
using Centaurus.Models;

namespace Centaurus.Alpha
{
Expand All @@ -14,5 +11,7 @@ public class ConstellationInitModel
public long MinAllowedLotSize { get; set; }

public string[] Assets { get; set; }

public RequestRateLimitsModel RequestRateLimits { get; set; }
}
}
9 changes: 9 additions & 0 deletions Centaurus.Alpha/Models/RequestRateLimitsModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace Centaurus.Alpha
{
public class RequestRateLimitsModel
{
public uint MinuteLimit { get; set; }

public uint HourLimit { get; set; }
}
}
20 changes: 20 additions & 0 deletions Centaurus.Common/Exceptions/ClientExceptions/TooManyRequests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using System;
using System.Collections.Generic;
using System.Text;

namespace Centaurus
{
public class TooManyRequests : BaseClientException
{
public TooManyRequests()
{

}

public TooManyRequests(string message)
: base(message)
{

}
}
}
2 changes: 2 additions & 0 deletions Centaurus.DAL/DiffObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ public class Account : BaseDiffModel
public byte[] PubKey { get; set; }

public ulong Nonce { get; set; }

public RequestRateLimitsModel RequestRateLimits { get; set; }
}

public class Order : BaseDiffModel
Expand Down
3 changes: 2 additions & 1 deletion Centaurus.DAL/Models/AccountModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ public class AccountModel
{
public byte[] PubKey { get; set; }

//it stores ulong
public long Nonce { get; set; }

public RequestRateLimitsModel RequestRateLimits { get; set; }
}
}
12 changes: 12 additions & 0 deletions Centaurus.DAL/Models/RequestRateLimitModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System;
using System.Collections.Generic;
using System.Text;

namespace Centaurus.DAL.Models
{
public class RequestRateLimitsModel
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do you have two RequestRateLimitsModel models? See Centaurus.Alpha/Models/RequestRateLimitsModel.cs

Add the update method to this model (we'll need it in the counter logic).
Like this:

public void Update(uint hourLimit, uint minuteLimit)
{
   this.HourLimit = hourLimit;
   this.MinuteLimit = minuteLimit;
}

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These two models have different purposes. Alpha model is used for deserializing init request data, and no other library doesn't have reference for the Alpha project. DAL model is used as DTO between domain and data storage layers.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can't really agree on this. Why can't we reuse the same class in the Alpha project? It has the same semantics. We have too much code and repeating data entries already.

{
public uint HourLimit { get; set; }
public uint MinuteLimit { get; set; }
}
}
5 changes: 3 additions & 2 deletions Centaurus.DAL/Models/SettingsModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,11 @@ public class SettingsModel
public byte[][] Auditors { get; set; }

public long MinAccountBalance { get; set; }

public long MinAllowedLotSize { get; set; }

//it stores ulong
public long Apex { get; set; }

public RequestRateLimitsModel RequestRateLimits { get; set; }
}
}
22 changes: 19 additions & 3 deletions Centaurus.DAL/Mongo/MongoStorage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,9 @@ private WriteModel<ConstellationState>[] GetStellarDataUpdate(DiffObject.Constel
if (ledger > 0)
updateCommand = update.Set(s => s.Ledger, ledger);
if (vaultSequence > 0)
updateCommand = updateCommand.Set(s => s.VaultSequence, vaultSequence) ?? update.Set(s => s.VaultSequence, vaultSequence);
updateCommand = updateCommand == null
? update.Set(s => s.VaultSequence, vaultSequence)
: updateCommand.Set(s => s.VaultSequence, vaultSequence);
updateModel = new UpdateOneModel<ConstellationState>(filter.Empty, updateCommand);
}

Expand All @@ -302,11 +304,25 @@ private WriteModel<AccountModel>[] GetAccountUpdates(List<DiffObject.Account> ac
var pubKey = acc.PubKey;
var currentAccFilter = filter.Eq(a => a.PubKey, pubKey);
if (acc.IsInserted)
updates[i] = new InsertOneModel<AccountModel>(new AccountModel { Nonce = (long)acc.Nonce, PubKey = pubKey });
updates[i] = new InsertOneModel<AccountModel>(new AccountModel
{
Nonce = (long)acc.Nonce,
PubKey = pubKey,
RequestRateLimits = acc.RequestRateLimits
});
else if (acc.IsDeleted)
updates[i] = new DeleteOneModel<AccountModel>(currentAccFilter);
else
updates[i] = new UpdateOneModel<AccountModel>(currentAccFilter, update.Set(a => a.Nonce, (long)acc.Nonce));
{
UpdateDefinition<AccountModel> currentUpdate = null;
if (acc.Nonce != 0)
currentUpdate = update.Set(a => a.Nonce, (long)acc.Nonce);
if (acc.RequestRateLimits != null)
currentUpdate = currentUpdate == null
? update.Set(a => a.RequestRateLimits, acc.RequestRateLimits)
: currentUpdate.Set(a => a.RequestRateLimits, acc.RequestRateLimits);
updates[i] = new UpdateOneModel<AccountModel>(currentAccFilter, currentUpdate);
}
}
return updates;
}
Expand Down
62 changes: 39 additions & 23 deletions Centaurus.Domain/Constellation/ConstellationInitializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,15 @@

namespace Centaurus.Domain
{
public class ConstellationInitInfo
{
public KeyPair[] Auditors { get; set; }
public long MinAccountBalance { get; set; }
public long MinAllowedLotSize { get; set; }
public AssetSettings[] Assets { get; set; }
public RequestRateLimits RequestRateLimits { get; set; }
}

/// <summary>
/// Initializes the application.
/// It can only be called from the Alpha, and only when it is in the waiting for initialization state.
Expand All @@ -17,23 +26,30 @@ public class ConstellationInitializer
{
const int minAuditorsCount = 1;

public ConstellationInitializer(IEnumerable<KeyPair> auditors, long minAccountBalance, long minAllowedLotSize, IEnumerable<AssetSettings> assets)
ConstellationInitInfo constellationInitInfo { get; }

public ConstellationInitializer(ConstellationInitInfo constellationInitInfo)
{
Auditors = auditors.Count() >= minAuditorsCount ? auditors.ToArray() : throw new Exception($"Min auditors count is {minAuditorsCount}");
if (constellationInitInfo.Auditors == null || constellationInitInfo.Auditors.Count() < minAuditorsCount)
throw new ArgumentException($"Min auditors count is {minAuditorsCount}");

MinAccountBalance = minAccountBalance > 0 ? minAccountBalance : throw new ArgumentException("Minimal account balance is less then 0");
if (constellationInitInfo.MinAccountBalance < 1)
throw new ArgumentException("Minimal account balance is less then 0");

MinAllowedLotSize = minAllowedLotSize > 0 ? minAllowedLotSize : throw new ArgumentException("Minimal allowed lot size is less then 0");
if (constellationInitInfo.MinAllowedLotSize < 1)
throw new ArgumentException("Minimal allowed lot size is less then 0");

Assets = !assets.GroupBy(a => a.ToString()).Any(g => g.Count() > 1)
? assets.Where(a => !a.IsXlm).ToArray() //skip XLM, it's supported by default
: throw new ArgumentException("All asset values should be unique");
}
if (constellationInitInfo.Assets.GroupBy(a => a.ToString()).Any(g => g.Count() > 1))
throw new ArgumentException("All asset values should be unique");

public KeyPair[] Auditors { get; }
public long MinAccountBalance { get; }
public long MinAllowedLotSize { get; }
public AssetSettings[] Assets { get; }
if (constellationInitInfo.Assets.Any(a => a.IsXlm))
throw new ArgumentException("Specify only custom assets. Native assets are supported by default.");

if (constellationInitInfo.RequestRateLimits == null || constellationInitInfo.RequestRateLimits.HourLimit < 1 || constellationInitInfo.RequestRateLimits.MinuteLimit < 1)
throw new ArgumentException("Request rate limit values should be greater than 0");

this.constellationInitInfo = constellationInitInfo;
}

public async Task Init()
{
Expand All @@ -48,19 +64,19 @@ public async Task Init()

SetIdToAssets();


var vaultAccountInfo = await Global.StellarNetwork.Server.Accounts.Account(Global.Settings.KeyPair.AccountId);

var initQuantum = new ConstellationInitQuantum
{
Assets = Assets.ToList(),
Auditors = Auditors.Select(key => (RawPubKey)key.PublicKey).ToList(),
Assets = constellationInitInfo.Assets.ToList(),
Auditors = constellationInitInfo.Auditors.Select(key => (RawPubKey)key.PublicKey).ToList(),
Vault = Global.Settings.KeyPair.PublicKey,
MinAccountBalance = MinAccountBalance,
MinAllowedLotSize = MinAllowedLotSize,
MinAccountBalance = constellationInitInfo.MinAccountBalance,
MinAllowedLotSize = constellationInitInfo.MinAllowedLotSize,
PrevHash = new byte[] { },
Ledger = ledgerId,
VaultSequence = vaultAccountInfo.SequenceNumber
VaultSequence = vaultAccountInfo.SequenceNumber,
RequestRateLimits = constellationInitInfo.RequestRateLimits
};

var envelope = initQuantum.CreateEnvelope();
Expand All @@ -71,9 +87,9 @@ public async Task Init()
private void SetIdToAssets()
{
//start from 1, 0 is reserved by XLM
for (var i = 1; i <= Assets.Length; i++)
for (var i = 1; i <= constellationInitInfo.Assets.Length; i++)
{
Assets[i - 1].Id = i;
constellationInitInfo.Assets[i - 1].Id = i;
}
}

Expand All @@ -83,7 +99,7 @@ private void SetIdToAssets()
/// <returns>Ledger id</returns>
private async Task<long> BuildAndConfigureVault(stellar_dotnet_sdk.responses.AccountResponse vaultAccount)
{
var majority = MajorityHelper.GetMajorityCount(Auditors.Count());
var majority = MajorityHelper.GetMajorityCount(constellationInitInfo.Auditors.Count());

var sourceAccount = await Global.StellarNetwork.Server.Accounts.Account(Global.Settings.KeyPair.AccountId);

Expand All @@ -94,7 +110,7 @@ private async Task<long> BuildAndConfigureVault(stellar_dotnet_sdk.responses.Acc
.Where(b => b.Asset is stellar_dotnet_sdk.AssetTypeCreditAlphaNum)
.Select(b => b.Asset)
.Cast<stellar_dotnet_sdk.AssetTypeCreditAlphaNum>();
foreach (var a in Assets)
foreach (var a in constellationInitInfo.Assets)
{
var asset = a.ToAsset() as stellar_dotnet_sdk.AssetTypeCreditAlphaNum;

Expand All @@ -114,7 +130,7 @@ private async Task<long> BuildAndConfigureVault(stellar_dotnet_sdk.responses.Acc
.SetMediumThreshold(majority)
.SetHighThreshold(majority);

foreach (var signer in Auditors)
foreach (var signer in constellationInitInfo.Auditors)
optionOperationBuilder.SetSigner(Signer.Ed25519PublicKey(signer), 1);

transactionBuilder.AddOperation(optionOperationBuilder.Build());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public Account Account
get
{
if (account == null)
account = accountStorage.GetAccount(Effect.Pubkey);
account = accountStorage.GetAccount(Effect.Pubkey).Account;
return account;
}
}
Expand Down
13 changes: 5 additions & 8 deletions Centaurus.Domain/Effects/Account/NonceUpdateEffectProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,21 @@

namespace Centaurus.Domain
{
public class NonceUpdateEffectProcessor : EffectProcessor<NonceUpdateEffect>
public class NonceUpdateEffectProcessor : BaseAccountEffectProcessor<NonceUpdateEffect>
{
private Account account;

public NonceUpdateEffectProcessor(NonceUpdateEffect effect, Account account)
: base(effect)
public NonceUpdateEffectProcessor(NonceUpdateEffect effect, AccountStorage accountStorage)
: base(effect, accountStorage)
{
this.account = account;
}

public override void CommitEffect()
{
account.Nonce = Effect.Nonce;
Account.Nonce = Effect.Nonce;
hawthorne-abendsen marked this conversation as resolved.
Show resolved Hide resolved
}

public override void RevertEffect()
{
account.Nonce = Effect.PrevNonce;
Account.Nonce = Effect.PrevNonce;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using Centaurus.Models;
using System;
using System.Collections.Generic;
using System.Text;

namespace Centaurus.Domain
{
public class RequestRateLimitUpdateEffectProcessor : BaseAccountEffectProcessor<RequestRateLimitUpdateEffect>
{
public RequestRateLimitUpdateEffectProcessor(RequestRateLimitUpdateEffect effect, AccountStorage accountStorage)
: base(effect, accountStorage)
{

}

public override void CommitEffect()
{
Account.RequestRateLimits = Effect.RequestRateLimits;
}

public override void RevertEffect()
{
Account.RequestRateLimits = Effect.PrevRequestRateLimits;
}
}
}
Loading