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

Add GetBalance to CoinView #995

Closed
Closed
Show file tree
Hide file tree
Changes from 24 commits
Commits
Show all changes
46 commits
Select commit Hold shift + click to select a range
7bf3c0a
Add GetBalance to CoinView
quantumagi Jun 11, 2022
4a3d8f6
Fix
quantumagi Jun 11, 2022
8a69fc6
Fix indentation
quantumagi Jun 11, 2022
230763f
Add XML comment for GetBalance method
quantumagi Jun 12, 2022
4d7951e
More XML comment updates
quantumagi Jun 12, 2022
c4b642a
Update RocksDb and add CoinView fast-forward
quantumagi Jun 12, 2022
fbf4a9b
Refactor
quantumagi Jun 12, 2022
2dea122
Reduce changes
quantumagi Jun 12, 2022
f4c8f86
Reduce changes
quantumagi Jun 12, 2022
645d6da
Reduce changes
quantumagi Jun 12, 2022
87e83e6
Reduce changes
quantumagi Jun 12, 2022
fd363c7
Flush after rebuild
quantumagi Jun 12, 2022
4f4fda3
Index only PoS
quantumagi Jun 12, 2022
2793a15
Optimize
quantumagi Jun 12, 2022
577a032
Optimize / fix tests
quantumagi Jun 13, 2022
fe2ff6b
Refactor / fix tests
quantumagi Jun 13, 2022
a577923
Use AddressIndexerCV
quantumagi Jun 13, 2022
84bd656
Support toggling indexing mode
quantumagi Jun 13, 2022
ff88244
Add logs
quantumagi Jun 14, 2022
c36eb20
Refactor
quantumagi Jun 14, 2022
1e47951
Add sync method
quantumagi Jun 14, 2022
bed3316
Add ReadWriteBatch
quantumagi Jun 15, 2022
0ba5076
Fix tests
quantumagi Jun 15, 2022
7c38975
Bug fix
quantumagi Jun 15, 2022
8b2d80c
Handle unresolvable destinations
quantumagi Jun 16, 2022
da3a657
Fix ToDiff
quantumagi Jun 16, 2022
0d65346
Restore endianness fix still required for tests
quantumagi Jun 17, 2022
1ca8668
Merge branch 'release/1.3.1.0' into getbalance
quantumagi Jun 17, 2022
4a6bdc2
Merge release/1.4.0.0
quantumagi Jul 2, 2022
06c90b3
Fix merge
quantumagi Jul 2, 2022
9800ba0
Fix merge
quantumagi Jul 2, 2022
4443a5e
Guarantee CoindDb's IScriptAddressReader dependncy
quantumagi Jul 3, 2022
984bd23
Merge
quantumagi Jul 4, 2022
d755442
Reduce changes
quantumagi Jul 4, 2022
f83cc42
Reduce changes
quantumagi Jul 4, 2022
88bf887
Remove unused code
quantumagi Jul 4, 2022
70c0512
Refactor
quantumagi Jul 6, 2022
b93c277
Add XML comments
quantumagi Jul 7, 2022
c92a9ae
Add XML comments
quantumagi Jul 7, 2022
b1c1482
Modify XML comment
quantumagi Jul 7, 2022
daba62a
Merge
quantumagi Jul 12, 2022
0083e09
Fix merge
quantumagi Jul 12, 2022
98a3ef5
Fix merge
quantumagi Jul 12, 2022
6fe610a
Reorganize code
quantumagi Jul 12, 2022
bff166b
Split code into separate files
quantumagi Jul 12, 2022
89da0c2
Remove whitespace
quantumagi Jul 12, 2022
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
using System;
using System.Collections.Generic;
using System.Linq;
using NBitcoin;
using Stratis.Bitcoin.Builder.Feature;
using Stratis.Bitcoin.Controllers.Models;
using Stratis.Bitcoin.Features.BlockStore.Models;
using Stratis.Bitcoin.Features.Consensus.CoinViews;
using Stratis.Bitcoin.Interfaces;

namespace Stratis.Bitcoin.Features.BlockStore.AddressIndexing
{
public class AddressIndexerCV : IAddressIndexer
{
private readonly Network network;
private readonly ICoinView coinView;
private readonly ChainIndexer chainIndexer;
private readonly IScriptAddressReader scriptAddressReader;

public ChainedHeader IndexerTip => GetTip();

public IFullNodeFeature InitializingFeature { set; private get; }

public AddressIndexerCV(Network network, ChainIndexer chainIndexer, IScriptAddressReader scriptAddressReader, ICoinView coinView)
{
this.network = network;
this.coinView = coinView;
this.chainIndexer = chainIndexer;
this.scriptAddressReader = scriptAddressReader;
}

private ChainedHeader GetTip()
{
this.coinView.Sync(this.chainIndexer);

return this.chainIndexer[this.coinView.GetTipHash().Hash];
}

public void Initialize()
{
}

private TxDestination AddressToDestination(string address)
{
var bitcoinAddress = BitcoinAddress.Create(address, this.network);
return this.scriptAddressReader.GetDestinationFromScriptPubKey(this.network, bitcoinAddress.ScriptPubKey).Single();
}

public AddressBalancesResult GetAddressBalances(string[] addresses, int minConfirmations = 0)
{
return new AddressBalancesResult()
{
Balances = addresses.Select(address => new AddressBalanceResult()
{
Address = address,
Balance = new Money(this.coinView.GetBalance(AddressToDestination(address)).First().satoshis)
zeptin marked this conversation as resolved.
Show resolved Hide resolved
}).ToList()
};
}

public LastBalanceDecreaseTransactionModel GetLastBalanceDecreaseTransaction(string address)
{
throw new NotImplementedException();
}

private IEnumerable<AddressBalanceChange> ToDiff(List<AddressBalanceChange> addressBalanceChanges)
{
for (int i = addressBalanceChanges.Count - 1; i > 0; i--)
{
yield return new AddressBalanceChange() {
BalanceChangedHeight = addressBalanceChanges[i].BalanceChangedHeight,
Deposited = addressBalanceChanges[i].Satoshi < addressBalanceChanges[i - 1].Satoshi,
Satoshi = Math.Abs(addressBalanceChanges[i].Satoshi - addressBalanceChanges[i - 1].Satoshi)
};
}
}

/// <inheritdoc/>
public VerboseAddressBalancesResult GetAddressIndexerState(string[] addresses)
{
// If the containing feature is not initialized then wait a bit.
this.InitializingFeature?.WaitInitialized();

return new VerboseAddressBalancesResult(this.IndexerTip.Height)
{
BalancesData = addresses.Select(address => new AddressIndexerData()
{
Address = address,
BalanceChanges = ToDiff(this.coinView.GetBalance(AddressToDestination(address)).Select(b => new AddressBalanceChange()
{
BalanceChangedHeight = (int)b.height,
Deposited = b.satoshis >= 0,
Satoshi = Math.Abs(b.satoshis)
}).ToList()).ToList()
}).ToList()
};
}

public void Dispose()
{
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ public static IFullNodeBuilder UseBlockStore(this IFullNodeBuilder fullNodeBuild

services.AddSingleton<StoreSettings>();
services.AddSingleton<IBlockStoreQueueFlushCondition, BlockStoreQueueFlushCondition>();
services.AddSingleton<IAddressIndexer, AddressIndexer>();
services.AddSingleton<IAddressIndexer, AddressIndexerCV>();
services.AddSingleton<IUtxoIndexer, UtxoIndexer>();
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using NBitcoin;
using Stratis.Bitcoin.Consensus;
using Stratis.Bitcoin.Features.Wallet.Interfaces;
using Stratis.Bitcoin.Interfaces;

namespace Stratis.Bitcoin.Features.ColdStaking
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public CoinviewTests()
this.nodeStats = new NodeStats(this.dateTimeProvider, NodeSettings.Default(this.network), new Mock<IVersionProvider>().Object);

this.coindb = new DBreezeCoindb(this.network, this.dataFolder, this.dateTimeProvider, this.loggerFactory, this.nodeStats, new DBreezeSerializer(this.network.Consensus.ConsensusFactory));
this.coindb.Initialize(new ChainedHeader(this.network.GetGenesis().Header, this.network.GenesisHash, 0));
this.coindb.Initialize(false);

this.chainIndexer = new ChainIndexer(this.network);
this.stakeChainStore = new StakeChainStore(this.network, this.chainIndexer, (IStakedb)this.coindb, this.loggerFactory);
Expand Down
Loading