Skip to content

Commit

Permalink
Merge pull request #812 from NethermindEth/810_expired_deposit_proper…
Browse files Browse the repository at this point in the history
…ty_for_rpc

Expired property added to DepositDetailsForRpc #810
  • Loading branch information
tkstanczak authored Aug 21, 2019
2 parents 4736cbe + d5aaf54 commit 4f76065
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ public INdmConsumerServices Init(INdmServices services)
: null;
services.RequiredServices.RpcModuleProvider.Register(
new SingletonModulePool<INdmRpcConsumerModule>(new NdmRpcConsumerModule(consumerService,
depositReportService, jsonRpcNdmConsumerChannel, ethRequestService, personalBridge)));
depositReportService, jsonRpcNdmConsumerChannel, ethRequestService, personalBridge, timestamper)));

var consumerServicesBackgroundProcessor = new ConsumerServicesBackgroundProcessor(accountService,
refundClaimant, depositConfirmationService, blockProcessor, depositRepository, consumerNotifier,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ public class DepositDetailsForRpc
public uint ConfirmationTimestamp { get; set; }
public bool Confirmed { get; set; }
public bool Rejected { get; set; }
public bool Expired { get; set; }
public bool RefundClaimed { get; set; }
public Keccak ClaimedRefundTransactionHash { get; set; }
public uint ConsumedUnits { get; set; }
Expand All @@ -45,7 +46,7 @@ public DepositDetailsForRpc()
{
}

public DepositDetailsForRpc(DepositDetails deposit)
public DepositDetailsForRpc(DepositDetails deposit, uint timestamp)
{
Id = deposit.Id;
Deposit = new DepositForRpc(deposit.Deposit);
Expand All @@ -56,6 +57,7 @@ public DepositDetailsForRpc(DepositDetails deposit)
ConfirmationTimestamp = deposit.ConfirmationTimestamp;
Confirmed = deposit.Confirmed;
Rejected = deposit.Rejected;
Expired = deposit.IsExpired(timestamp);
RefundClaimed = deposit.RefundClaimed;
ClaimedRefundTransactionHash = deposit.ClaimedRefundTransactionHash;
ConsumedUnits = deposit.ConsumedUnits;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,16 +44,18 @@ public class NdmRpcConsumerModule : INdmRpcConsumerModule
private readonly IJsonRpcNdmConsumerChannel _jsonRpcNdmConsumerChannel;
private readonly IEthRequestService _ethRequestService;
private readonly IPersonalBridge _personalBridge;
private readonly ITimestamper _timestamper;

public NdmRpcConsumerModule(IConsumerService consumerService, IDepositReportService depositReportService,
IJsonRpcNdmConsumerChannel jsonRpcNdmConsumerChannel, IEthRequestService ethRequestService,
IPersonalBridge personalBridge)
IPersonalBridge personalBridge, ITimestamper timestamper)
{
_consumerService = consumerService;
_depositReportService = depositReportService;
_jsonRpcNdmConsumerChannel = jsonRpcNdmConsumerChannel;
_ethRequestService = ethRequestService;
_personalBridge = personalBridge;
_timestamper = timestamper;
}

public ResultWrapper<AccountForRpc[]> ndm_listAccounts()
Expand Down Expand Up @@ -111,22 +113,24 @@ public ResultWrapper<ConsumerSessionForRpc[]> ndm_getActiveConsumerSessions()

public async Task<ResultWrapper<PagedResult<DepositDetailsForRpc>>> ndm_getDeposits(GetDeposits query)
{
var timestamp = (uint) _timestamper.EpochSeconds;
var deposits = await _consumerService.GetDepositsAsync(query ?? new GetDeposits
{
Results = int.MaxValue
});

return ResultWrapper<PagedResult<DepositDetailsForRpc>>.Success(PagedResult<DepositDetailsForRpc>.From(
deposits, deposits.Items.Select(d => new DepositDetailsForRpc(d)).ToArray()));
deposits, deposits.Items.Select(d => new DepositDetailsForRpc(d, timestamp)).ToArray()));
}

public async Task<ResultWrapper<DepositDetailsForRpc>> ndm_getDeposit(Keccak depositId)
{
var timestamp = (uint) _timestamper.EpochSeconds;
var deposit = await _consumerService.GetDepositAsync(depositId);

return deposit == null
? ResultWrapper<DepositDetailsForRpc>.Fail($"Deposit: '{depositId}' was not found.")
: ResultWrapper<DepositDetailsForRpc>.Success(new DepositDetailsForRpc(deposit));
: ResultWrapper<DepositDetailsForRpc>.Success(new DepositDetailsForRpc(deposit, timestamp));
}

public async Task<ResultWrapper<Keccak>> ndm_makeDeposit(MakeDepositForRpc deposit)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ public class NdmRpcConsumerModuleTests
private IEthRequestService _ethRequestService;
private IPersonalBridge _personalBridge;
private INdmRpcConsumerModule _rpc;
private ITimestamper _timestamper;
private const uint DepositExpiryTime = 1546393600;
private static readonly DateTime Date = new DateTime(2019, 1, 2); //1546383600

[SetUp]
public void Setup()
Expand All @@ -65,8 +68,9 @@ public void Setup()
_jsonRpcNdmConsumerChannel = Substitute.For<IJsonRpcNdmConsumerChannel>();
_ethRequestService = Substitute.For<IEthRequestService>();
_personalBridge = Substitute.For<IPersonalBridge>();
_timestamper = new Timestamper(Date);
_rpc = new NdmRpcConsumerModule(_consumerService, _depositReportService, _jsonRpcNdmConsumerChannel,
_ethRequestService, _personalBridge);
_ethRequestService, _personalBridge, _timestamper);
}

[Test]
Expand All @@ -93,7 +97,7 @@ public void given_null_personal_bridge_list_accounts_should_not_return_accounts(
{
_personalBridge = null;
_rpc = new NdmRpcConsumerModule(_consumerService, _depositReportService, _jsonRpcNdmConsumerChannel,
_ethRequestService, _personalBridge);
_ethRequestService, _personalBridge, _timestamper);
var result = _rpc.ndm_listAccounts();
result.Data.Should().BeEmpty();
}
Expand Down Expand Up @@ -497,10 +501,11 @@ private static void VerifyDepositDetails(DepositDetailsForRpc deposit)
deposit.Deposit.Id.Should().Be(Keccak.OfAnEmptyString);
deposit.Deposit.Units.Should().Be(1);
deposit.Deposit.Value.Should().Be(1);
deposit.Deposit.ExpiryTime.Should().Be(1);
deposit.Deposit.ExpiryTime.Should().Be(DepositExpiryTime);
deposit.Timestamp.Should().Be(1);
deposit.TransactionHash.Should().Be(TestItem.KeccakA);
deposit.Confirmed.Should().Be(false);
deposit.Expired.Should().Be(false);
deposit.RefundClaimed.Should().Be(false);
deposit.ClaimedRefundTransactionHash.Should().BeNull();
deposit.ConsumedUnits.Should().Be(0);
Expand Down Expand Up @@ -543,12 +548,12 @@ private static DataAsset GetDataAsset()
new DataAssetProvider(Address.Zero, "test"));

private static DepositDetails GetDepositDetails()
=> new DepositDetails(new Deposit(Keccak.OfAnEmptyString, 1, 1, 1),
=> new DepositDetails(new Deposit(Keccak.OfAnEmptyString, 1, DepositExpiryTime, 1),
GetDataAsset(), TestItem.AddressB, Array.Empty<byte>(), 1, TestItem.KeccakA);

private static DepositReportItem GetDepositReportItem()
=> new DepositReportItem(Keccak.Zero, TestItem.KeccakA, "test", TestItem.AddressA,
"test", 1, 1, TestItem.AddressB, 1, 1, false, TestItem.KeccakA,
"test", 1, 1, TestItem.AddressB, 1, DepositExpiryTime, false, TestItem.KeccakA,
1, 1, 1, true, false, TestItem.KeccakB, false, 1, new[]
{
new DataDeliveryReceiptReportItem(Keccak.Zero, 1, TestItem.KeccakC, TestItem.PublicKeyA,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,7 @@ public class DepositDetails : IEquatable<DepositDetails>
public string Kyc { get; private set; }
public uint Confirmations { get; private set; }
public uint RequiredConfirmations { get; private set; }

public bool IsExpired(uint timestamp) => timestamp >= Deposit.ExpiryTime;


public DepositDetails(Deposit deposit, DataAsset dataAsset, Address consumer, byte[] pepper, uint timestamp,
Keccak transactionHash, uint confirmationTimestamp = 0, bool rejected = false,
EarlyRefundTicket earlyRefundTicket = null, Keccak claimedRefundTransactionHash = null,
Expand All @@ -66,6 +64,8 @@ public DepositDetails(Deposit deposit, DataAsset dataAsset, Address consumer, by
Confirmations = confirmations;
RequiredConfirmations = requiredConfirmations;
}

public bool IsExpired(uint timestamp) => timestamp >= Deposit.ExpiryTime;

public void SetConfirmationTimestamp(uint timestamp)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,14 +90,14 @@ public async Task<DepositsReport> GetAsync(GetDepositsReport query)
results = 10;
}

var now = _timestamper.EpochSeconds;
var timestamp = (uint)_timestamper.EpochSeconds;
var skip = (page - 1) * results;
var items = new List<DepositReportItem>();
foreach (var (_, deposit) in foundDeposits.OrderByDescending(d => d.Value.Timestamp).Skip(skip)
.Take(results))
{
depositsReceipts.TryGetValue(deposit.Id, out var depositReceipts);
var expired = now >= deposit.Deposit.ExpiryTime;
var expired = deposit.IsExpired(timestamp);
var receiptItems = depositReceipts?.Select(r => new DataDeliveryReceiptReportItem(r.Id, r.Number,
r.SessionId, r.ConsumerNodeId, r.Request, r.Receipt, r.Timestamp, r.IsMerged, r.IsClaimed));
var sessions = await _sessionRepository.BrowseAsync(new GetConsumerSessions
Expand Down

0 comments on commit 4f76065

Please sign in to comment.