Skip to content

Commit

Permalink
Merge pull request stratisproject#66 from dangershony/staking-expiry
Browse files Browse the repository at this point in the history
Allow stake to expiry if its enabled
  • Loading branch information
dangershony authored Dec 12, 2019
2 parents eb7e923 + 4928b02 commit 15b5724
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -147,15 +147,18 @@ public IActionResult StopStaking([FromBody] bool corsProtection = true)
}

/// <summary>
/// Whitelist an address for staking, this only allowed if <see cref="MinerSettings.EnforceStakingFlag"/> is true.
/// Set expiration for an address for staking, this only allowed if <see cref="MinerSettings.EnforceStakingFlag"/> is true.
/// </summary>
/// <returns>An <see cref="OkResult"/> object that produces a status code 200 HTTP response.</returns>
[Route("whitelist")]
[Route("stakingExpiry")]
[HttpPost]
public IActionResult Whitelist([FromBody] WhitelistRequest request)
public IActionResult StakingExpiry([FromBody] StakingExpiryRequest request)
{
try
{
if (!this.fullNode.Network.Consensus.IsProofOfStake)
return ErrorHelpers.BuildErrorResponse(HttpStatusCode.MethodNotAllowed, "Method not allowed", "Method not available for Proof of Stake");

if (!this.minerSettings.EnforceStakingFlag)
return ErrorHelpers.BuildErrorResponse(HttpStatusCode.Forbidden, "Operation not allowed", "This operation is only allowed if EnforceStakingFlag is true");

Expand All @@ -167,7 +170,7 @@ public IActionResult Whitelist([FromBody] WhitelistRequest request)
{
if ((address.Address == request.Address) || address.Bech32Address == request.Address)
{
address.StakingEnabled = request.Whitelist;
address.StakingExpiry = request.StakingExpiry;
}
}
}
Expand All @@ -180,5 +183,41 @@ public IActionResult Whitelist([FromBody] WhitelistRequest request)
return ErrorHelpers.BuildErrorResponse(HttpStatusCode.BadRequest, e.Message, e.ToString());
}
}

[Route("getStakingNotExpired")]
[HttpGet]
public IActionResult GetStakingNotExpired([FromBody] StakingNotExpiredRequest request)
{
try
{
if (!this.fullNode.Network.Consensus.IsProofOfStake)
return ErrorHelpers.BuildErrorResponse(HttpStatusCode.MethodNotAllowed, "Method not allowed", "Method not available for Proof of Stake");

if (!this.minerSettings.EnforceStakingFlag)
return ErrorHelpers.BuildErrorResponse(HttpStatusCode.Forbidden, "Operation not allowed", "This operation is only allowed if EnforceStakingFlag is true");

Wallet.Wallet wallet = this.walletManager.GetWallet(request.WalletName);

GetStakingAddressesModel model = new GetStakingAddressesModel { Addresses = new List<string>() };

foreach (Wallet.HdAccount account in wallet.GetAccounts())
{
foreach (Wallet.HdAddress address in account.GetCombinedAddresses())
{
if (address.StakingExpiry != null && address.StakingExpiry < DateTime.UtcNow)
{
model.Addresses.Add(request.Segwit ? address.Bech32Address : address.Address);
}
}
}

return this.Json(model);
}
catch (Exception e)
{
this.logger.LogError("Exception occurred: {0}", e.ToString());
return ErrorHelpers.BuildErrorResponse(HttpStatusCode.BadRequest, e.Message, e.ToString());
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System.Collections.Generic;
using NBitcoin;
using Newtonsoft.Json;

namespace Stratis.Bitcoin.Features.Miner.Models
{
public class GetStakingAddressesModel
{
[JsonProperty(PropertyName = "Addresses")]
public IList<string> Addresses { get; set; }
}
}
31 changes: 25 additions & 6 deletions src/Stratis.Bitcoin.Features.Miner/Models/RequestModels.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using System.ComponentModel.DataAnnotations;
using System;
using System.ComponentModel.DataAnnotations;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;

namespace Stratis.Bitcoin.Features.Miner.Models
{
Expand Down Expand Up @@ -49,10 +51,15 @@ public class MiningRequest : RequestModel
}

/// <summary>
/// Model for the "generate" mining request.
/// Model for the staking request.
/// </summary>
public class WhitelistRequest : RequestModel
public class StakingExpiryRequest : RequestModel
{
public StakingExpiryRequest()
{
this.StakingExpiry = DateTime.UtcNow;
}

/// <summary>
/// Name of wallet.
/// </summary>
Expand All @@ -66,9 +73,21 @@ public class WhitelistRequest : RequestModel
public string Address { get; set; }

/// <summary>
/// Wheather to enable or not.
/// Specify whether UTXOs associated with this address is within the allowed staing time, null will disable staking.
/// </summary>
[Required(ErrorMessage = "Wheather to enable or not.")]
public bool Whitelist { get; set; }
[JsonProperty(PropertyName = "stakingExpiry", NullValueHandling = NullValueHandling.Ignore)]
[JsonConverter(typeof(IsoDateTimeConverter))]
public DateTime? StakingExpiry { get; set; }
}

public class StakingNotExpiredRequest : RequestModel
{
/// <summary>
/// Name of wallet.
/// </summary>
[Required(ErrorMessage = "Name of wallet.")]
public string WalletName { get; set; }

public bool Segwit { get; set; }
}
}
4 changes: 2 additions & 2 deletions src/Stratis.Bitcoin.Features.Miner/Staking/PosMinting.cs
Original file line number Diff line number Diff line change
Expand Up @@ -446,10 +446,10 @@ internal List<UtxoStakeDescription> GetUtxoStakeDescriptions(WalletSecret wallet
{
if (this.minerSettings.EnforceStakingFlag)
{
if (utxo.Address.StakingEnabled == null)
if (utxo.Address.StakingExpiry == null)
return false;

if (utxo.Address.StakingEnabled == false)
if (utxo.Address.StakingExpiry > this.dateTimeProvider.GetUtcNow())
return false;
}

Expand Down
6 changes: 3 additions & 3 deletions src/Stratis.Bitcoin.Features.Wallet/Wallet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -908,10 +908,10 @@ public HdAddress()
public ICollection<TransactionData> Transactions { get; set; }

/// <summary>
/// Specify whether UTXOs associated with this address can stake.
/// Specify whether UTXOs associated with this address is within the allowed staing time.
/// </summary>
[JsonProperty(PropertyName = "stakingEnabled")]
public bool? StakingEnabled { get; set; }
[JsonProperty(PropertyName = "stakingExpiry", NullValueHandling = NullValueHandling.Ignore)]
public DateTime? StakingExpiry { get; set; }

/// <summary>
/// Determines whether this is a change address or a receive address.
Expand Down

0 comments on commit 15b5724

Please sign in to comment.