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

Feature block generation #114

Closed
wants to merge 68 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
68 commits
Select commit Hold shift + click to select a range
1e3aaa1
modify block header structure
shiwk May 12, 2018
6ae48d4
remove redundant data in header
shiwk May 12, 2018
6aa171b
modify chain structure
shiwk May 12, 2018
d492310
change hash calculating for tx
shiwk May 12, 2018
4693d92
add block generation service
shiwk May 12, 2018
c809eaa
Merge branch 'dev' of https://github.com/AElfProject/AElf into featur…
shiwk May 12, 2018
416cde7
modify BlockGenerationService
shiwk May 12, 2018
5c8e508
change ready txs collection form
shiwk May 12, 2018
27f233a
add miner configuration
shiwk May 12, 2018
51b5579
spelling mistake
shiwk May 12, 2018
f494eae
Merge branch 'dev' of https://github.com/AElfProject/AElf into featur…
shiwk May 14, 2018
741d0c3
add Miner
shiwk May 14, 2018
de3e148
add MinerConfig
shiwk May 14, 2018
c682909
reorg
shiwk May 14, 2018
d5374bc
fix bugs
shiwk May 14, 2018
bf751cd
Merge branch 'dev' of https://github.com/AElfProject/AElf into featur…
shiwk May 14, 2018
e74ffa7
Change some methods' name
EanCuznaivy May 14, 2018
56ef97b
Add a simple swtich of database.
EanCuznaivy May 14, 2018
3c45a03
modify chain manager
shiwk May 15, 2018
fe19d68
modify miner config
shiwk May 15, 2018
579b907
modify chain class
shiwk May 15, 2018
dad3e0c
add chain to protobuf
shiwk May 15, 2018
45e31c0
BlockGenerationService
shiwk May 15, 2018
aeef515
transaction pool config
shiwk May 15, 2018
5cd3336
ready txs from pool
shiwk May 15, 2018
fc3b66b
modify TxPoolService
shiwk May 15, 2018
c3341ec
change test codes
shiwk May 15, 2018
2386b51
use ulong
shiwk May 15, 2018
442549b
chang interface invoke in service
shiwk May 15, 2018
1f21700
design Miner job
shiwk May 15, 2018
a3c8732
Tune the impl of Redis
EanCuznaivy May 15, 2018
b7653fc
Merge branch 'feature-block-generation' of https://github.com/AElfPro…
EanCuznaivy May 15, 2018
2aff6c2
Tune test cases of storage based on latest impl.
EanCuznaivy May 15, 2018
8d4acf2
clean miner structure
shiwk May 15, 2018
d6f5463
add chain, block manager to BlockGenerationService
shiwk May 15, 2018
d733457
modify ChainManager
shiwk May 15, 2018
36ac0aa
block header storage
shiwk May 15, 2018
d3db4d9
modify block manager
shiwk May 15, 2018
9a5134e
modify ChainCreationService
shiwk May 15, 2018
1b8077e
Merge branch 'feature-block-generation' of https://github.com/AElfPro…
shiwk May 15, 2018
8bd1af8
Tune impl of WorldStateManager: add OfChain method.
EanCuznaivy May 16, 2018
b36b234
modify tx validation
shiwk May 16, 2018
7156614
miner configuration
shiwk May 16, 2018
393f7bc
modify Mine method
shiwk May 16, 2018
3dd587f
tx hash calculating
shiwk May 16, 2018
40c4e0d
add Promotable to tx pool
shiwk May 16, 2018
711d818
modify TxPoolService
shiwk May 16, 2018
65eb95b
change DatabaseModule
shiwk May 16, 2018
0f095bb
smart contract executing
shiwk May 16, 2018
ab56c27
Revert "smart contract executing"
shiwk May 16, 2018
6f3c3c4
Merge branch 'feature-block-generation' of https://github.com/AElfPro…
EanCuznaivy May 16, 2018
81d6ccf
Add some checks.
EanCuznaivy May 16, 2018
981d015
Merge branch 'feature-block-generation' of git://github.com/AElfProje…
shiwk May 16, 2018
57d8aa4
add tx result path
shiwk May 16, 2018
1c0e8e3
fix bug in tx result storage
shiwk May 16, 2018
a21f3d9
remove interface for block/header/body
shiwk May 16, 2018
b3f8555
make storage single instance
shiwk May 16, 2018
2321a12
remove interfaces fo block
shiwk May 16, 2018
cbb5aa7
fix bug in chain creation
shiwk May 16, 2018
d609c49
fix bug in tx storage
shiwk May 16, 2018
d6d4563
add GetBlock
shiwk May 16, 2018
2af5237
modify test cases
shiwk May 16, 2018
86195b5
modify test cases
shiwk May 16, 2018
920d2e2
recalculate key for tx result
shiwk May 16, 2018
33ceb52
Merge branch 'feature-block-generation' of https://github.com/AElfPro…
shiwk May 16, 2018
25a4bfa
modify test case
shiwk May 16, 2018
c597ab0
fix one test case.
EanCuznaivy May 16, 2018
2b626ff
fix comments
EanCuznaivy May 16, 2018
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
4 changes: 2 additions & 2 deletions AElf.Database/DatabaseConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
{
public class DatabaseConfig
{
public string IpAddress { get; set; }
public int Port { get; set; }
public string IpAddress { get; set; } = "127.0.0.1";
public int Port { get; set; } = 6379;
}
}
15 changes: 9 additions & 6 deletions AElf.Database/RedisDatabase.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices.ComTypes;
using System.Text;
using System.Threading.Tasks;
using AElf.Database;
Expand All @@ -12,21 +13,23 @@ namespace AElf.Database
{
public class RedisDatabase : IKeyValueDatabase
{
private readonly RedisClient _client;
private readonly IRedisClient _client;

public RedisDatabase()
public RedisDatabase():this(new DatabaseConfig())
{
_client = new RedisClient("127.0.0.1", 6379);;
}

public RedisDatabase(DatabaseConfig config)
{
_client = new RedisClient(config.IpAddress, config.Port);
using (var redisManager = new RedisManagerPool($"{config.IpAddress}:{config.Port}"))
{
_client = redisManager.GetClient();
}
}

public async Task<byte[]> GetAsync(string key, Type type)
{
return await Task.FromResult(_client.Get(key));
return await Task.FromResult(_client.Get<byte[]>(key));
}

public async Task SetAsync(string key, byte[] bytes)
Expand All @@ -38,7 +41,7 @@ public bool IsConnected()
{
try
{
_client.Set("test", null);
_client.Set<byte[]>("test", null);
return true;
}
catch (Exception ex)
Expand Down
17 changes: 15 additions & 2 deletions AElf.Kernel.Modules.AutofacModule/DatabaseModule.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,26 @@
using AElf.Database;
using System;
using AElf.Database;
using Autofac;
using ServiceStack.Redis;

namespace AElf.Kernel.Modules.AutofacModule
{
public class DatabaseModule : Module
{
private readonly bool _useRedis = false;
protected override void Load(ContainerBuilder builder)
{
builder.RegisterType<KeyValueDatabase>().As<IKeyValueDatabase>();
if (_useRedis)
{
if (!new RedisDatabase().IsConnected())
return;

builder.RegisterType<RedisDatabase>().As<IKeyValueDatabase>();
}
else
{
builder.RegisterType<KeyValueDatabase>().As<IKeyValueDatabase>().SingleInstance();
}
}
}
}
32 changes: 21 additions & 11 deletions AElf.Kernel.Tests/BlockTest.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using System.Threading.Tasks;
using System.Threading;
using System.Threading.Tasks;
using AElf.Kernel.KernelAccount;
using AElf.Kernel.Managers;
using Google.Protobuf.Collections;
using Xunit;
using Xunit.Frameworks.Autofac;

Expand Down Expand Up @@ -34,19 +36,27 @@ public void GenesisBlockBuilderTest()
[Fact]
public async Task BlockManagerTest()
{
var builder = new GenesisBlockBuilder().Build(_smartContractZero.GetType());
var genesisBlock = builder.Block;

await _blockManager.AddBlockAsync(genesisBlock);
var blockHeader = await _blockManager.GetBlockHeaderAsync(genesisBlock.GetHash());

var block = new Block(genesisBlock.GetHash());

var chain = await _chainTest.CreateChainTest();
await _chainTest.AppendBlockTest(chain, block);
Assert.Equal(blockHeader, genesisBlock.Header);

var block = CreateBlock(chain.GenesisBlockHash);
await _blockManager.AddBlockAsync(block);
var b = await _blockManager.GetBlockAsync(block.GetHash());
Assert.Equal(b, block);
}

private Block CreateBlock(Hash preBlockHash = null)
{
Interlocked.CompareExchange(ref preBlockHash, Hash.Zero, null);

var block = new Block(Hash.Generate());
block.AddTransaction(Hash.Generate());
block.AddTransaction(Hash.Generate());
block.AddTransaction(Hash.Generate());
block.AddTransaction(Hash.Generate());
block.FillTxsMerkleTreeRootInHeader();
block.Header.PreviousHash = preBlockHash;
return block;
}

}
}
52 changes: 38 additions & 14 deletions AElf.Kernel.Tests/ChainTest.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Threading.Tasks;
using System.Threading;
using System.Threading.Tasks;
using AElf.Kernel.KernelAccount;
using AElf.Kernel.Managers;
using AElf.Kernel.Services;
Expand Down Expand Up @@ -38,40 +39,63 @@ public async Task<IChain> CreateChainTest()
var chainId = Hash.Generate();
var chain = await _chainCreationService.CreateNewChainAsync(chainId, _smartContractZero.GetType());

await _chainManager.AppendBlockToChainAsync(chain, new Block(Hash.Generate()));
/*// add chain to storage
var genesisBlock = new Block(Hash.Zero);
await _chainManager.AddChainAsync(chain.Id, genesisBlock.GetHash());

var block = new Block(genesisBlock.GetHash()) {Header = {PreviousHash = genesisBlock.GetHash()}};
await _chainManager.AppendBlockToChainAsync(chain.Id, block);

var address = Hash.Generate();
var accountContextService = new AccountContextService();
var worldStateManager = new WorldStateManager(_worldStateStore,
accountContextService, _changesStore, _dataStore);
var accountDataProvider = worldStateManager.GetAccountDataProvider(chainId, address);
var worldStateManager = await new WorldStateManager(_worldStateStore,
_changesStore, _dataStore).OfChain(chainId);
var accountDataProvider = worldStateManager.GetAccountDataProvider(address);

await _smartContractZero.InitializeAsync(accountDataProvider);
Assert.Equal(chain.CurrentBlockHeight, (ulong)1);
await _smartContractZero.InitializeAsync(accountDataProvider);*/
Assert.Equal(await _chainManager.GetChainCurrentHeight(chain.Id), (ulong)1);
return chain;
}

public async Task ChainStoreTest(Hash chainId)
{
await _chainManager.AddChainAsync(chainId);
await _chainManager.AddChainAsync(chainId, Hash.Generate());
Assert.NotNull(_chainManager.GetChainAsync(chainId).Result);
}

public async Task ChainContextTest()
{
var chain = await CreateChainTest();
await _chainManager.AddChainAsync(chain.Id);
await _chainManager.AddChainAsync(chain.Id, Hash.Generate());
chain = await _chainManager.GetChainAsync(chain.Id);
var context = _chainContextService.GetChainContext(chain.Id);
Assert.NotNull(context);
Assert.Equal(context.SmartContractZero, _smartContractZero);
}

public async Task AppendBlockTest(IChain chain, Block block)
[Fact]
public async Task AppendBlockTest()
{
await _chainManager.AppendBlockToChainAsync(chain, block);
Assert.Equal(chain.CurrentBlockHeight, (ulong)2);
Assert.Equal(chain.CurrentBlockHash, block.GetHash());
var chain = await CreateChainTest();

var block = CreateBlock(chain.GenesisBlockHash);
await _chainManager.AppendBlockToChainAsync(chain.Id, block);
Assert.Equal(await _chainManager.GetChainCurrentHeight(chain.Id), (ulong)2);
Assert.Equal(await _chainManager.GetChainLastBlockHash(chain.Id), block.GetHash());
Assert.Equal(block.Header.Index, (ulong)1);
}

private Block CreateBlock(Hash preBlockHash = null)
{
Interlocked.CompareExchange(ref preBlockHash, Hash.Zero, null);

var block = new Block(Hash.Generate());
block.AddTransaction(Hash.Generate());
block.AddTransaction(Hash.Generate());
block.AddTransaction(Hash.Generate());
block.AddTransaction(Hash.Generate());
block.FillTxsMerkleTreeRootInHeader();
block.Header.PreviousHash = preBlockHash;
return block;
}
}
}
20 changes: 8 additions & 12 deletions AElf.Kernel.Tests/DataProviderTest.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using AElf.Database;
using AElf.Kernel.Extensions;
using AElf.Kernel.Managers;
using AElf.Kernel.Services;
using AElf.Kernel.Storages;
using Google.Protobuf;
using Xunit;
using Xunit.Frameworks.Autofac;

Expand Down Expand Up @@ -34,17 +31,16 @@ public async Task SetTest()
const int count = 5;
var setList = CreateSet(count).ToList();
var keys = GenerateKeys(setList).ToList();

var chain = new Chain(Hash.Generate());
var chainManager = new ChainManager(_chainStore);
await chainManager.AddChainAsync(chain.Id);

var genesisBlockHash = Hash.Generate();
var chain = new Chain(Hash.Generate(), genesisBlockHash);
var chainManager = new ChainManager(_chainStore, _dataStore);
await chainManager.AddChainAsync(chain.Id, genesisBlockHash);

var address = Hash.Generate();
var accountContextService = new AccountContextService();
var worldStateManager = new WorldStateManager(_worldStateStore, accountContextService,
_changesStore, _dataStore);
await worldStateManager.SetWorldStateAsync(chain.Id, Hash.Generate());
var accountDataProvider = worldStateManager.GetAccountDataProvider(chain.Id, address);
var worldStateManager = await new WorldStateManager(_worldStateStore, _changesStore, _dataStore).OfChain(chain.Id);
await worldStateManager.SetWorldStateAsync(genesisBlockHash);
var accountDataProvider = worldStateManager.GetAccountDataProvider(address);
var dataProvider = accountDataProvider.GetDataProvider();

for (var i = 0; i < count; i++)
Expand Down
Loading