Skip to content
This repository has been archived by the owner on Aug 16, 2021. It is now read-only.

Commit

Permalink
Revert "merge (#35)"
Browse files Browse the repository at this point in the history
This reverts commit d72194a.
  • Loading branch information
dangershony authored Mar 7, 2017
1 parent d72194a commit 8c9d536
Show file tree
Hide file tree
Showing 12 changed files with 76 additions and 285 deletions.
48 changes: 4 additions & 44 deletions Stratis.Bitcoin.Tests/BlockStoreTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,9 @@ public void BlockRepositoryBench()

Stopwatch stopwatch = new Stopwatch();
stopwatch.Start();

blockRepo.PutAsync(lst.Last().GetHash(), lst).GetAwaiter().GetResult();
blockRepo.PutAsync(lst.Last().GetHash(), lst, true).GetAwaiter().GetResult();
var first = stopwatch.ElapsedMilliseconds;
blockRepo.PutAsync(lst.Last().GetHash(), lst).GetAwaiter().GetResult();

blockRepo.PutAsync(lst.Last().GetHash(), lst, true).GetAwaiter().GetResult();
var second = stopwatch.ElapsedMilliseconds;

}
Expand All @@ -74,8 +72,6 @@ public void BlockRepositoryPutBatch()
{
using (var blockRepo = new BlockStore.BlockRepository(Network.Main, dir.FolderName))
{
blockRepo.SetTxIndex(true).Wait();

var lst = new List<Block>();
for (int i = 0; i < 5; i++)
{
Expand All @@ -92,9 +88,7 @@ public void BlockRepositoryPutBatch()
lst.Add(block);
}


blockRepo.PutAsync(lst.Last().GetHash(), lst).GetAwaiter().GetResult();

blockRepo.PutAsync(lst.Last().GetHash(), lst, true).GetAwaiter().GetResult();

// check each block
foreach (var block in lst)
Expand All @@ -110,9 +104,7 @@ public void BlockRepositoryPutBatch()
}

// delete

blockRepo.DeleteAsync(lst.ElementAt(2).GetHash(), new[] {lst.ElementAt(2).GetHash()}.ToList());

blockRepo.DeleteAsync(lst.ElementAt(2).GetHash(), new[] {lst.ElementAt(2).GetHash()}.ToList(), true);
var deleted = blockRepo.GetAsync(lst.ElementAt(2).GetHash()).GetAwaiter().GetResult();
Assert.Null(deleted);
}
Expand Down Expand Up @@ -226,37 +218,5 @@ public void BlockStoreCanReorg()
Class1.Eventually(() => stratisNode2.FullNode.ChainBehaviorState.HighestPersistedBlock.HashBlock == stratisNodeSync.FullNode.ChainBehaviorState.HighestPersistedBlock.HashBlock);
}
}


[Fact]
public void BlockStoreIndexTx()
{
using (NodeBuilder builder = NodeBuilder.Create())
{
var stratisNode1 = builder.CreateStratisNode();
var stratisNode2 = builder.CreateStratisNode();
builder.StartAll();
stratisNode1.NotInIBD();
stratisNode2.NotInIBD();

// generate blocks and wait for the downloader to pickup
stratisNode1.SetDummyMinerSecret(new BitcoinSecret(new Key(), stratisNode1.FullNode.Network));
stratisNode2.SetDummyMinerSecret(new BitcoinSecret(new Key(), stratisNode2.FullNode.Network));
// sync both nodes
stratisNode1.CreateRPCClient().AddNode(stratisNode2.Endpoint, true);
stratisNode1.GenerateStratis(10);
Class1.Eventually(() => stratisNode1.FullNode.ChainBehaviorState.HighestPersistedBlock.Height == 10);
Class1.Eventually(() => stratisNode1.FullNode.ChainBehaviorState.HighestPersistedBlock.HashBlock == stratisNode2.FullNode.ChainBehaviorState.HighestPersistedBlock.HashBlock);

var bestBlock1 = stratisNode1.FullNode.BlockStoreManager.BlockRepository.GetAsync(stratisNode1.FullNode.Chain.Tip.HashBlock).Result;
Assert.NotNull(bestBlock1);

// get the block coinbase trx
var trx = stratisNode2.FullNode.BlockStoreManager.BlockRepository.GetTrxAsync(bestBlock1.Transactions.First().GetHash()).Result;
Assert.NotNull(trx);
Assert.Equal(bestBlock1.Transactions.First().GetHash(), trx.GetHash());
}
}

}
}
14 changes: 0 additions & 14 deletions Stratis.Bitcoin.Tests/NodeBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -146,20 +146,6 @@ public static NodeBuilder Create([CallerMemberNameAttribute] string caller = nul
bitcoind.Kill();
}
}
// kill all dotnet instances no us
//foreach (var dotnet in Process.GetProcessesByName("dotnet"))
//{
// if (Process.GetCurrentProcess().Id == dotnet.Id)
// continue;
// try
// {
// dotnet.Kill();
// }
// catch
// {
// // ignored
// }
//}
Thread.Sleep(1000);
Directory.Delete(caller, true);
}
Expand Down
86 changes: 12 additions & 74 deletions Stratis.Bitcoin/BlockStore/BlockRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,13 @@ namespace Stratis.Bitcoin.BlockStore
{
public interface IBlockRepository
{

Task PutAsync(uint256 nextBlockHash, List<Block> blocks);

Task PutAsync(uint256 nextBlockHash, List<Block> blocks, bool txIndex);

Task<Block> GetAsync(uint256 hash);

Task<Transaction> GetTrxAsync(uint256 trxid);


Task DeleteAsync(uint256 newlockHash, List<uint256> hashes);

Task DeleteAsync(uint256 newlockHash, List<uint256> hashes, bool txIndex);
}

public class BlockRepository : IDisposable, IBlockRepository
Expand Down Expand Up @@ -50,56 +46,30 @@ private Task Initialize(Block genesis)
{
if (this.LoadBlockHash() == null)
{

this.SaveBlockHash(genesis.GetHash());
this.session.Transaction.Commit();
}
if (this.LoadTxIndex() == null)
{
this.SaveTxIndex(false);

this.FlushBlockHash(genesis.GetHash());
this.session.Transaction.Commit();
}
});

return Task.WhenAll(new[] {sync, hash});

}

public Task<Transaction> GetTrxAsync(uint256 trxid)
{
if (!this.TxIndex)
return Task.FromResult(default(Transaction));

return this.session.Do(() =>
{
var blockid = this.session.Transaction.Select<byte[], uint256>("Transaction", trxid.ToBytes());
if (!blockid.Exists)
if (blockid?.Value == null)
return null;
var block = this.session.Transaction.Select<byte[], Block>("Block", blockid.Value.ToBytes());
return block?.Value?.Transactions.FirstOrDefault(t => t.GetHash() == trxid);
});
}

public Task<uint256> GetTrxBlockIdAsync(uint256 trxid)
{
if (!this.TxIndex)
return Task.FromResult(default(uint256));

return this.session.Do(() =>
{
var blockid = this.session.Transaction.Select<byte[], uint256>("Transaction", trxid.ToBytes());
return !blockid.Exists ? null : blockid.Value;
});
}

static readonly byte[] BlockHashKey = new byte[0];
static readonly byte[] TxIndexKey = new byte[1];

public uint256 BlockHash { get; private set; }
public bool TxIndex { get; private set; }

public Task PutAsync(uint256 nextBlockHash, List<Block> blocks)
public Task PutAsync(uint256 nextBlockHash, List<Block> blocks, bool txIndex)
{
// dbreeze is faster if sort ascending by key in memory before insert
// however we need to find how byte arrays are sorted in dbreeze this link can help
Expand All @@ -117,7 +87,7 @@ public Task PutAsync(uint256 nextBlockHash, List<Block> blocks)
{
this.session.Transaction.Insert<byte[], Block>("Block", blockId.ToBytes(), block);

if (this.TxIndex)
if (txIndex)
{
// index transactions
foreach (var transaction in block.Transactions)
Expand All @@ -129,30 +99,7 @@ public Task PutAsync(uint256 nextBlockHash, List<Block> blocks)
}
}

this.SaveBlockHash(nextBlockHash);
this.session.Transaction.Commit();
});
}

private bool? LoadTxIndex()
{
var item = this.session.Transaction.Select<byte[], bool>("Common", TxIndexKey);
if (!item.Exists)
return null;
this.TxIndex = item.Value;
return item.Value;
}
private void SaveTxIndex(bool txIndex)
{
this.TxIndex = txIndex;
this.session.Transaction.Insert<byte[], bool>("Common", TxIndexKey, txIndex);
}

public Task SetTxIndex(bool txIndex)
{
return this.session.Do(() =>
{
this.SaveTxIndex(txIndex);
this.FlushBlockHash(nextBlockHash);
this.session.Transaction.Commit();
});
}
Expand All @@ -167,15 +114,12 @@ public Task SetBlockHash(uint256 nextBlockHash)
{
return this.session.Do(() =>
{
this.SaveBlockHash(nextBlockHash);

this.FlushBlockHash(nextBlockHash);
this.session.Transaction.Commit();
});
}


private void SaveBlockHash(uint256 nextBlockHash)

private void FlushBlockHash(uint256 nextBlockHash)
{
this.BlockHash = nextBlockHash;
this.session.Transaction.Insert<byte[], uint256>("Common", BlockHashKey, nextBlockHash);
Expand All @@ -201,9 +145,7 @@ public Task<bool> ExistAsync(uint256 hash)
});
}


public Task DeleteAsync(uint256 newlockHash, List<uint256> hashes)

public Task DeleteAsync(uint256 newlockHash, List<uint256> hashes, bool txIndex)
{
return this.session.Do(() =>
{
Expand All @@ -212,9 +154,7 @@ public Task DeleteAsync(uint256 newlockHash, List<uint256> hashes)
// if the block is already in store don't write it again
var key = hash.ToBytes();


if (this.TxIndex)

if (txIndex)
{
var block = this.session.Transaction.Select<byte[], Block>("Block", key);
if (block.Exists)
Expand All @@ -225,9 +165,7 @@ public Task DeleteAsync(uint256 newlockHash, List<uint256> hashes)
this.session.Transaction.RemoveKey<byte[]>("Block", key);
}


this.SaveBlockHash(newlockHash);

this.FlushBlockHash(newlockHash);
this.session.Transaction.Commit();
});
}
Expand Down
14 changes: 3 additions & 11 deletions Stratis.Bitcoin/BlockStore/BlockStoreBehavior.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,6 @@ public class BlockStoreBehavior : NodeBehavior
private readonly ConcurrentChain chain;
private readonly BlockRepository blockRepository;

private readonly BlockStoreCache blockStoreCache;


public bool CanRespondToGetBlocksPayload { get; set; }

public bool CanRespondeToGetDataPayload { get; set; }
Expand All @@ -34,13 +31,10 @@ public class BlockStoreBehavior : NodeBehavior
public bool PreferHeaders; // public for testing
private bool preferHeaderAndIDs;


public BlockStoreBehavior(ConcurrentChain chain, BlockRepository blockRepository, BlockStoreCache blockStoreCache)
public BlockStoreBehavior(ConcurrentChain chain, BlockRepository blockRepository)
{
this.chain = chain;
this.blockRepository = blockRepository;
this.blockStoreCache = blockStoreCache;


this.CanRespondToGetBlocksPayload = false;
this.CanRespondeToGetDataPayload = true;
Expand Down Expand Up @@ -120,7 +114,7 @@ private async Task ProcessGetDataAsync(Node node, GetDataPayload getDataPayload)
{
// TODO: check if we need to add support for "not found"

var block = await this.blockStoreCache.GetBlockAsync(item.Hash).ConfigureAwait(false);
var block = await this.blockRepository.GetAsync(item.Hash).ConfigureAwait(false);


if (block != null)
Expand Down Expand Up @@ -277,9 +271,7 @@ public Task AnnounceBlocks(List<uint256> blockHashesToAnnounce)

public override object Clone()
{

return new BlockStoreBehavior(this.chain, this.blockRepository, this.blockStoreCache)

return new BlockStoreBehavior(this.chain, this.blockRepository)
{
CanRespondToGetBlocksPayload = this.CanRespondToGetBlocksPayload,
CanRespondeToGetDataPayload = this.CanRespondeToGetDataPayload
Expand Down
Loading

0 comments on commit 8c9d536

Please sign in to comment.