Skip to content

Commit

Permalink
Enable staking on strax (#379)
Browse files Browse the repository at this point in the history
  • Loading branch information
dangershony authored Jan 27, 2022
1 parent cc038cf commit 36f93cb
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 4 deletions.
4 changes: 2 additions & 2 deletions src/Features/Blockcore.Features.Miner/Staking/PosMinting.cs
Original file line number Diff line number Diff line change
Expand Up @@ -740,7 +740,7 @@ await Task.Run(() => Parallel.ForEach(workerContexts, cwc =>
long coinstakeOutputValue = coinstakeInput.TxOut.Value + reward;

int eventuallyStakableUtxosCount = utxoStakeDescriptions.Count;
Transaction coinstakeTx = this.PrepareCoinStakeTransactions(chainTip.Height, coinstakeContext, coinstakeOutputValue, eventuallyStakableUtxosCount, ourWeight);
Transaction coinstakeTx = this.PrepareCoinStakeTransactions(chainTip.Height, coinstakeContext, coinstakeOutputValue, eventuallyStakableUtxosCount, ourWeight, reward);

if (coinstakeTx is IPosTransactionWithTime posTrxn)
{
Expand All @@ -767,7 +767,7 @@ await Task.Run(() => Parallel.ForEach(workerContexts, cwc =>
return true;
}

internal Transaction PrepareCoinStakeTransactions(int currentChainHeight, CoinstakeContext coinstakeContext, long coinstakeOutputValue, int utxosCount, long amountStaked)
public virtual Transaction PrepareCoinStakeTransactions(int currentChainHeight, CoinstakeContext coinstakeContext, long coinstakeOutputValue, int utxosCount, long amountStaked, long reward)
{
// Split stake into SplitFactor utxos if above threshold.
bool shouldSplitStake = this.ShouldSplitStake(utxosCount, amountStaked, coinstakeOutputValue, currentChainHeight);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
<ItemGroup>
<ProjectReference Include="..\..\Features\Blockcore.Features.Consensus\Blockcore.Features.Consensus.csproj" />
<ProjectReference Include="..\..\Features\Blockcore.Features.MemoryPool\Blockcore.Features.MemoryPool.csproj" />
<ProjectReference Include="..\..\Features\Blockcore.Features.Miner\Blockcore.Features.Miner.csproj" />
</ItemGroup>

<ItemGroup>
Expand Down
64 changes: 64 additions & 0 deletions src/Networks/Blockcore.Networks.Strax/Staking/StraxMinting.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
using Blockcore.AsyncWork;
using Blockcore.Base;
using Blockcore.Consensus;
using Blockcore.Consensus.Chain;
using Blockcore.Consensus.TransactionInfo;
using Blockcore.Features.Consensus;
using Blockcore.Features.Consensus.CoinViews;
using Blockcore.Features.Consensus.Interfaces;
using Blockcore.Features.MemoryPool;
using Blockcore.Features.MemoryPool.Interfaces;
using Blockcore.Features.Miner;
using Blockcore.Features.Miner.Staking;
using Blockcore.Features.Wallet.Interfaces;
using Blockcore.Interfaces;
using Blockcore.Mining;
using Blockcore.Networks.Strax.Rules;
using Blockcore.Utilities;
using Microsoft.Extensions.Logging;


namespace Blockcore.Networks.Strax.Staking
{
public class StraxMinting : PosMinting
{
public StraxMinting(
IBlockProvider blockProvider,
IConsensusManager consensusManager,
ChainIndexer chainIndexer,
Network network,
IDateTimeProvider dateTimeProvider,
IInitialBlockDownloadState initialBlockDownloadState,
INodeLifetime nodeLifetime,
ICoinView coinView,
IStakeChain stakeChain,
IStakeValidator stakeValidator,
MempoolSchedulerLock mempoolLock,
ITxMempool mempool,
IWalletManager walletManager,
IAsyncProvider asyncProvider,
ITimeSyncBehaviorState timeSyncBehaviorState,
ILoggerFactory loggerFactory,
MinerSettings minerSettings) : base(blockProvider, consensusManager, chainIndexer, network, dateTimeProvider,
initialBlockDownloadState, nodeLifetime, coinView, stakeChain, stakeValidator, mempoolLock, mempool,
walletManager, asyncProvider, timeSyncBehaviorState, loggerFactory, minerSettings)
{
}

public override Transaction PrepareCoinStakeTransactions(int currentChainHeight, CoinstakeContext coinstakeContext, long coinstakeOutputValue, int utxosCount, long amountStaked, long reward)
{
long cirrusReward = reward * StraxCoinviewRule.CirrusRewardPercentage / 100;

coinstakeOutputValue -= cirrusReward;

// Populate the initial coinstake with the modified overall reward amount, the outputs will be split as necessary
base.PrepareCoinStakeTransactions(currentChainHeight, coinstakeContext, coinstakeOutputValue, utxosCount, amountStaked, reward);

// Now add the remaining reward into an additional output on the coinstake
var cirrusRewardOutput = new TxOut(cirrusReward, StraxCoinstakeRule.CirrusRewardScript);
coinstakeContext.CoinstakeTx.Outputs.Add(cirrusRewardOutput);

return coinstakeContext.CoinstakeTx;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,8 @@ public void Given_A_Coin_That_Does_Not_Need_Splitting_CoinstakeTx_Should_Have_On
coinstakeContext: coinStakeContext,
coinstakeOutputValue: coinstakeInputValue,
utxosCount: amounts.Count,
amountStaked: amountStaked);
amountStaked: amountStaked,
reward: (long)reward);
return (coinstakeInputValue, transaction);
}

Expand Down Expand Up @@ -194,7 +195,8 @@ private Transaction GetCoinstakeTransaction(List<Money> amounts)
coinstakeContext: coinStakeContext,
coinstakeOutputValue: coinstakeInputValue,
utxosCount: amounts.Count,
amountStaked: amounts.Sum(u => u.Satoshi));
amountStaked: amounts.Sum(u => u.Satoshi),
reward: (long)reward);
return transaction;
}

Expand Down

0 comments on commit 36f93cb

Please sign in to comment.