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 5 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
17 changes: 16 additions & 1 deletion Centaurus.Alpha/Controllers/ConstellationController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,12 @@ public ConstellationInfo Info()
StellarNetwork = network,
Assets = assets
};
if (Global.Constellation.RequestRateLimits != null)
hawthorne-abendsen marked this conversation as resolved.
Show resolved Hide resolved
info.RequestRateLimits = new RequestRateLimitsModel
{
HourLimit = Global.Constellation.RequestRateLimits.HourLimit,
MinuteLimit = Global.Constellation.RequestRateLimits.MinuteLimit,
};
}

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

RequestRateLimits requestRateLimits = null;
if (constellationInit.RequestRateLimits != null)
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))
constellationInit.Assets.Select(a => AssetSettings.FromCode(a)),
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 RequestRateLimitsModel RequestRateLimits { get; set; }

public class Network
{
public Network(string passphrase, string horizon)
Expand Down
9 changes: 3 additions & 6 deletions Centaurus.Alpha/Models/ConstellationInitModel.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace Centaurus.Alpha
namespace Centaurus.Alpha
{
public class ConstellationInitModel
{
Expand All @@ -14,5 +9,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 int MinuteLimit { get; set; }

public int 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 int HourLimit { get; set; }
hawthorne-abendsen marked this conversation as resolved.
Show resolved Hide resolved
public int 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
9 changes: 7 additions & 2 deletions Centaurus.Domain/Constellation/ConstellationInitializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public class ConstellationInitializer
{
const int minAuditorsCount = 1;

public ConstellationInitializer(IEnumerable<KeyPair> auditors, long minAccountBalance, long minAllowedLotSize, IEnumerable<AssetSettings> assets)
public ConstellationInitializer(IEnumerable<KeyPair> auditors, long minAccountBalance, long minAllowedLotSize, IEnumerable<AssetSettings> assets, RequestRateLimits requestRateLimits)
{
Auditors = auditors.Count() >= minAuditorsCount ? auditors.ToArray() : throw new Exception($"Min auditors count is {minAuditorsCount}");

Expand All @@ -28,13 +28,17 @@ public ConstellationInitializer(IEnumerable<KeyPair> auditors, long minAccountBa
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");

RequestRateLimits = requestRateLimits;
}

public KeyPair[] Auditors { get; }
public long MinAccountBalance { get; }
public long MinAllowedLotSize { get; }
public AssetSettings[] Assets { get; }

public RequestRateLimits RequestRateLimits { get; set; }

public async Task Init()
{
if (Global.AppState.State != ApplicationState.WaitingForInit)
Expand All @@ -60,7 +64,8 @@ public async Task Init()
MinAllowedLotSize = MinAllowedLotSize,
PrevHash = new byte[] { },
Ledger = ledgerId,
VaultSequence = vaultAccountInfo.SequenceNumber
VaultSequence = vaultAccountInfo.SequenceNumber,
RequestRateLimits = RequestRateLimits
};

var envelope = initQuantum.CreateEnvelope();
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
11 changes: 5 additions & 6 deletions Centaurus.Domain/Effects/Account/NonceUpdateEffectProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,23 @@

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;
}
}
}
6 changes: 3 additions & 3 deletions Centaurus.Domain/Effects/EffectProcessorsContainer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -199,11 +199,11 @@ public void AddOrderRemoved(Orderbook orderbook, AccountStorage accountStorage,



public void AddNonceUpdate(Account account, ulong newNonce)
public void AddNonceUpdate(AccountStorage accountStorage, RawPubKey publicKey, ulong newNonce, ulong currentNonce)
{
Add(new NonceUpdateEffectProcessor(
new NonceUpdateEffect { Nonce = newNonce, PrevNonce = account.Nonce, Pubkey = account.Pubkey, Apex = Apex },
account
new NonceUpdateEffect { Nonce = newNonce, PrevNonce = currentNonce, Pubkey = publicKey, Apex = Apex },
accountStorage
));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public override void CommitEffect()

public override void RevertEffect()
{
var order = new Order { OrderId = Effect.OrderId, Price = Effect.Price, Amount = 0, Account = accountStorage.GetAccount(Effect.Pubkey) };
var order = new Order { OrderId = Effect.OrderId, Price = Effect.Price, Amount = 0, Account = accountStorage.GetAccount(Effect.Pubkey).Account };
orderbook.InsertOrder(order);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ private async Task HandleClientHandshake(AlphaWebSocketConnection connection, Me
{
if (Global.AppState.State != ApplicationState.Ready)
throw new ConnectionCloseException(WebSocketCloseStatus.ProtocolError, "Alpha is not in Ready state.");
connection.Account = Global.AccountStorage.GetAccount(connection.ClientPubKey);
connection.ConnectionState = ConnectionState.Ready;
await connection.SendMessage(envelope.CreateResult(ResultStatusCodes.Success));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ public override async Task Validate(AlphaWebSocketConnection connection, Message
if (IsAuditorOnly && !Global.Constellation.Auditors.Contains(connection.ClientPubKey))
throw new UnauthorizedException();

if (connection.Account != null && !connection.Account.RequestCounter.IncRequestCount(DateTime.UtcNow.Ticks, out string error))
hawthorne-abendsen marked this conversation as resolved.
Show resolved Hide resolved
throw new TooManyRequests(error);

await base.Validate(connection, envelope);
}
}
Expand Down
Loading