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

Migrate to pg #214

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
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
3 changes: 3 additions & 0 deletions src/Blockcore.Indexer.Core/Blockcore.Indexer.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,17 @@
<ItemGroup>
<PackageReference Include="Blockcore.Core" Version="1.1.*" />
<PackageReference Include="Microsoft.AspNet.WebApi.Client" Version="6.0.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="8.0.6" />
<PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.20.1" />
<PackageReference Include="Microsoft.Extensions.PlatformAbstractions" Version="1.1.0" />
<PackageReference Include="MongoDB.Driver" Version="2.24.0" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="8.0.4" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.5.0" />
<PackageReference Include="Swashbuckle.AspNetCore.Annotations" Version="6.5.0" />
<PackageReference Include="Swashbuckle.AspNetCore.Newtonsoft" Version="6.5.0" />
<PackageReference Include="Swashbuckle.AspNetCore.Swagger" Version="6.5.0" />
<PackageReference Include="Z.EntityFramework.Extensions.EFCore" Version="8.102.3" />
</ItemGroup>
<ItemGroup>
<PackageReference Update="Microsoft.SourceLink.GitHub" Version="8.0.0" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ public abstract class StorageBatch
public abstract int GetInputCount();
public abstract int GetTransactionCount();
public abstract long GetBatchSize();

public abstract IEnumerable<long> GetBlockSizes();
public abstract bool ValidateBatch(string prevBlockHash);
}
Expand Down
10 changes: 9 additions & 1 deletion src/Blockcore.Indexer.Core/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc.ApplicationModels;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.PlatformAbstractions;
Expand Down Expand Up @@ -47,6 +48,8 @@ public static void AddIndexerServices(IServiceCollection services, IConfiguratio
default: throw new InvalidOperationException();
}

services.AddDbContext<PostgresDbContext>();

// services.AddSingleton<QueryHandler>();
services.AddSingleton<StatsHandler>();
services.AddSingleton<CommandHandler>();
Expand Down Expand Up @@ -135,7 +138,7 @@ public static void AddIndexerServices(IServiceCollection services, IConfiguratio
services.AddTransient<IBlockRewindOperation, BlockRewindOperation>();
}

public static void Configure(IApplicationBuilder app, IWebHostEnvironment env)
public static void Configure(IApplicationBuilder app, IWebHostEnvironment env, PostgresDbContext db)
{
app.UseExceptionHandler("/error");

Expand All @@ -145,6 +148,11 @@ public static void Configure(IApplicationBuilder app, IWebHostEnvironment env)
app.UseResponseCompression();

//app.UseMvc();
db.Database.Migrate();

using (var client = new PostgresDbContext()){
client.Database.EnsureCreated();
}

app.UseDefaultFiles();

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using System.Collections.Generic;
using Blockcore.Indexer.Core.Client.Types;
using Blockcore.Indexer.Core.Storage.Postgres.Types;
using Blockcore.Indexer.Core.Storage.Types;

namespace Blockcore.Indexer.Core.Storage
{
public interface IMapPgBlockToStorageBlock
{
SyncBlockInfo Map(Block block);
Block Map(BlockInfo blockInfo);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
using System.Collections.Generic;
using System.Linq;
using System.Net.Http.Headers;
using Blockcore.Indexer.Core.Client.Types;
using Blockcore.Indexer.Core.Storage.Postgres.Types;
using Blockcore.Indexer.Core.Storage.Types;

namespace Blockcore.Indexer.Core.Storage
{
public class MapPgBlockToStorageBlock : IMapPgBlockToStorageBlock
{
public SyncBlockInfo Map(Block block) => new SyncBlockInfo
{
BlockIndex = block.BlockIndex,
BlockSize = block.BlockSize,
BlockHash = block.BlockHash,
BlockTime = block.BlockTime,
NextBlockHash = block.NextBlockHash,
PreviousBlockHash = block.PreviousBlockHash,
TransactionCount = block.TransactionCount,
Nonce = block.Nonce,
ChainWork = block.ChainWork,
Difficulty = block.Difficulty,
Merkleroot = block.Merkleroot,
PosModifierv2 = block.PosModifierv2,
PosHashProof = block.PosHashProof,
PosFlags = block.PosFlags,
PosChainTrust = block.PosChainTrust,
PosBlockTrust = block.PosBlockTrust,
PosBlockSignature = block.PosBlockSignature,
Confirmations = block.Confirmations,
Bits = block.Bits,
Version = block.Version,
SyncComplete = block.SyncComplete
};


public Block Map(BlockInfo block, List<Transaction> transactions) =>

new Block
{
BlockIndex = block.Height,
BlockHash = block.Hash,
BlockSize = block.Size,
BlockTime = block.Time,
NextBlockHash = block.NextBlockHash,
PreviousBlockHash = block.PreviousBlockHash,
TransactionCount = block.Transactions.Count(),
Bits = block.Bits,
Confirmations = block.Confirmations,
Merkleroot = block.Merkleroot,
Nonce = block.Nonce,
ChainWork = block.ChainWork,
Difficulty = block.Difficulty,
PosBlockSignature = block.PosBlockSignature,
PosBlockTrust = block.PosBlockTrust,
PosChainTrust = block.PosChainTrust,
PosFlags = block.PosFlags,
PosHashProof = block.PosHashProof,
PosModifierv2 = block.PosModifierv2,
Version = block.Version,
SyncComplete = false,
Transactions = transactions
};
public Block Map(BlockInfo block)
{
Block output = new Block
{
BlockIndex = block.Height,
BlockHash = block.Hash,
BlockSize = block.Size,
BlockTime = block.Time,
NextBlockHash = block.NextBlockHash,
PreviousBlockHash = block.PreviousBlockHash,
TransactionCount = block.Transactions.Count(),
Bits = block.Bits,
Confirmations = block.Confirmations,
Merkleroot = block.Merkleroot,
Nonce = block.Nonce,
ChainWork = block.ChainWork,
Difficulty = block.Difficulty,
PosBlockSignature = block.PosBlockSignature,
PosBlockTrust = block.PosBlockTrust,
PosChainTrust = block.PosChainTrust,
PosFlags = block.PosFlags,
PosHashProof = block.PosHashProof,
PosModifierv2 = block.PosModifierv2,
Version = block.Version,
SyncComplete = false,
Transactions = [],
};
return output;
}
}
}
99 changes: 99 additions & 0 deletions src/Blockcore.Indexer.Core/Storage/Postgres/PostgresDBContext.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
using System.Globalization;
using Blockcore.Indexer.Core.Storage.Postgres.Types;
using Blockcore.NBitcoin.Protocol;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Options;

public class PostgresDbContext : DbContext
{
public PostgresDbContext() : base()
{

}
protected override void OnConfiguring(DbContextOptionsBuilder options)
{
options.UseNpgsql("Host=127.0.0.1;Port=5432;Database=IndexerBenchmark;Username=postgres;Password=drb;");
}

public DbSet<Block> Blocks { get; set; }
public DbSet<Transaction> Transactions { get; set; }
public DbSet<Input> Inputs { get; set; }
public DbSet<Output> Outputs { get; set; }
public DbSet<MempoolTransaction> mempoolTransactions { get; set; }
public DbSet<MempoolInput> mempoolInputs { get; set; }
public DbSet<MempoolOutput> mempoolOutputs { get; set; }
public DbSet<UnspentOutput> unspentOutputs { get; set; }

protected override void OnModelCreating(ModelBuilder modelBuilder)
{

modelBuilder.Entity<Block>()
.HasKey(b => b.BlockHash);

modelBuilder.Entity<Transaction>()
.HasKey(t => t.Txid);

modelBuilder.Entity<Input>()
.HasKey(i => new { i.Txid, i.Vout });

modelBuilder.Entity<Output>()
.HasKey(o => new { o.outpoint.Txid, o.outpoint.Vout });

modelBuilder.Entity<MempoolTransaction>()
.HasKey(t => t.Txid);

modelBuilder.Entity<MempoolInput>()
.HasKey(i => new { i.Txid, i.Vout });

modelBuilder.Entity<MempoolOutput>()
.HasKey(o => new { o.outpoint.Txid, o.outpoint.Vout });

modelBuilder.Entity<UnspentOutput>()
.HasKey(uo => new { uo.Outpoint.Txid, uo.Outpoint.Vout });


modelBuilder.Entity<Transaction>()
.HasOne(t => t.Block)
.WithMany(b => b.Transactions)
.HasForeignKey(t => t.BlockIndex);

modelBuilder.Entity<Input>()
.HasOne<Transaction>(i => i.Transaction)
.WithMany(t => t.Inputs)
.HasForeignKey(i => i.Txid);

modelBuilder.Entity<Input>()
.HasOne<Transaction>(i => i.Transaction)
.WithMany(t => t.Inputs)
.HasForeignKey(i => i.outpoint.Txid);

modelBuilder.Entity<Output>()
.HasOne<Transaction>(o => o.Transaction)
.WithMany(t => t.Outputs)
.HasForeignKey(o => o.outpoint.Txid);

modelBuilder.Entity<MempoolInput>()
.HasOne<MempoolTransaction>(i => i.Transaction)
.WithMany(t => t.Inputs)
.HasForeignKey(i => i.Txid);

modelBuilder.Entity<MempoolOutput>()
.HasOne<MempoolTransaction>(o => o.Transaction)
.WithMany(t => t.Outputs)
.HasForeignKey(o => o.outpoint.Txid);

// modelBuilder.Entity<UnspentOutput>() .HasOne(t => t.)

modelBuilder.Entity<Block>()
.HasIndex(b => b.BlockHash)
.HasMethod("hash");

modelBuilder.Entity<Block>()
.HasIndex(b => b.BlockIndex)
.IsDescending();

modelBuilder.Entity<Transaction>()
.HasIndex(t => t.Txid)
.HasMethod("hash");
}
}
Loading
Loading