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

merge with development #1

Merged
merged 10 commits into from
Sep 21, 2018
32 changes: 27 additions & 5 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,45 @@ language: csharp
os:
- linux
dist: trusty
dotnet: 2.1.402
dotnet: 2.1.300
mono: none
sudo: required

env:
global:
- secure: "HYye9FxO66bBgpwphNYRY38y9ZS5tu3VCOyTTL9+bI18Zm5X52lSDAxRKbpg2MlF2twVxt/N1U/ckCM9/CVKr8SVdQ1kHiNLr/H0Mj53OFPyLmxDUxffsYNWqKtVAU1oCj1hl/f+Q8MWZ1P+z0x/k/sG+K6G6UTHK1xei7g9rkMVzO2SZpDw1RQoCnfpEI4H3azvhr8lu5RxAGbib2rX59zSix4YltmAYSYYI4eHFNoNec4QaErRkGe8bRbNGr3LF8iHWrijR5XE9jMm5ykkJN0p6Ca8f49X/YRnlA78QnHVgryN71I4drxSsfNlf6YhQqbLlGag0YWfnQW8C7/IRYNiIL/GqrCwJQCS5j5L2f6FAJeFJ6ISEx70HDrXAecckqorbLYyNcglN1yYxF8k0ZuQwM0zZKHIDpZIG18T2GXFJ23+P/A9PAeXEV9J94OO7eLaHeotX5KK2HbsVENFewp+8hwsLskAlQWu5DggMZErnHrMyUZnU7SV4YDzhJ14HtClbSl11mVi5OuaSwGHsFV9yeXy+D1leii5Tnxs8SinMT5Jkt7XrbOJgAZwTEOmgHhM9FGRJItYqq37FYtygAteCtqB9uEjt7qsgRlkiKwxxt4pL0qI9fo46AmT/UI6Yvai9xnXUUCocfrGx0Lk2Qh5hPjW/2ZwSeyrcxaBpm0="
- secure: "DarDapxHcxWOP52OmXUtcj7vMM/YmYXOCa9dfjYy8hvuTWmcIvHByd1TjGF4MyM6WH6EZJCcgLHI4mmtjXPUMWqW6Q8aahDGWclEjbnGJMQ8bLO7nBwITbV+wIieezDYNGA+VWfGsWNgXObFose/Y0PWq9BPoBAlSQv+sld/4aFPDarGKfOaLHn3AHhvj2ajgNNZJeV47TIlIJIfDJveNxj5RlaMPFiuSoUscepA9rmAn95YxogfHZauzk69suWHI8/pnuVF7GO9Ah2sxSV+5Q/VFAB+jJklvPHhxdso+hdhqOJ8MXDbTADg9oFgg+QyzRCMktFfP2kaOXEwzBh6CcqPtZdQ7aoZ0awUetyfSAHb6dJNw3JbXVtL9qDhZEHz7f2XpQ3yM0dDVeARpRHF+gdmuLRBNv9+IXm2rZ09gBWoSlfoe/8TxdAXw0D+dmQUBDmqYUoV4ZfvK1TYOHPq0U1jxj5kb6uKqGRlMDlypn/AT5rkTR/l6uJEsyHyjuYN4DnOWSmPz+1NiK3A0V5Q7zG4h8nJdxQR7YzXRAtkrjv8N6ePHckjiBKaUMRHeXnye3QuApsJR5e/N69n+3FQGlOPQhpNFfrjOCt/h1NYcoxF09//n3r6Lx3Ix7R4ncI40IzDV6mI8xHWtzKk/woINNEUpadME9+kdDZ0KTiOXgo="

services:
- docker

install:
# build container image
- docker build -t cityofzion/neo-sharp:latest ./containers/
# Build both docker images (sdk and runtime)
- docker build --target builder -t neo-sharp_sdk ./containers/
- docker build --target runtime -t neo-sharp_runtime ./containers/

script:
# run tests
- dotnet restore
- find test -name *.csproj | xargs -I % dotnet add % package coverlet.msbuild
- dotnet test /p:CollectCoverage=true /p:CoverletOutputFormat=opencover
- docker run -p 8000:8000 -d --rm --name neo-sharp-node neo-sharp_runtime
- bash -c "sleep 10;if [ $(docker inspect -f '{{.State.Running}}' neo-sharp-node) = 'true' ]; then exit 0; else echo 'neo-sharp Node is not starting inside the clean runtime container!!' && exit 1; fi"

after_success:
# if tests ok publish CodeDev report
# After all tests OK, Send CodeDov report
- echo "Test Success - Branch($TRAVIS_BRANCH) Pull Request($TRAVIS_PULL_REQUEST) Tag($TRAVIS_TAG)"
- bash <(curl -s https://codecov.io/bash) -v

before_deploy:
# To deploy the docker images, first do login and tag
- bash -c "echo $DOCKER_PASS | docker login -u $DOCKER_USER --password-stdin"
- export TAG=`if [ "$TRAVIS_BRANCH" == "master" ]; then echo "latest"; else echo $TRAVIS_BRANCH; fi`
- docker tag neo-sharp_sdk cityofzion/neo-sharp:sdk_$TAG
- docker tag neo-sharp_runtime cityofzion/neo-sharp:$TAG

deploy:
# Push images to official docker repository
- provider: script
script: bash -c "docker push cityofzion/neo-sharp"
on:
repo: CityOfZion/neo-sharp
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public void StateCommand()
var blockStr = FormatState(_blockPool.Size);
var headStr = FormatState(_blockchain.LastBlockHeader?.Index);
var blStr = FormatState(_blockchain.CurrentBlock?.Index);
var blIndex = FormatState(0); // TODO: Change me
var blIndex = FormatState(0); // TODO #398: Change me

var numSpaces = new int[] { memStr.Length, blockStr.Length, blIndex.Length, headStr.Length, blStr.Length }.Max() + 1;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public PromptContractController(IConsoleWriter consoleWriter, IConsoleReader con
}

/*
TODO:
TODO #399: Implement contract import / invoke feature
load_run {path/to/file.avm} (test {params} {returntype} {needs_storage} {needs_dynamic_invoke} {test_params})
import contract {path/to/file.avm} {params} {returntype} {needs_storage} {needs_dynamic_invoke}
import contract_addr {contract_hash} {pubkey}
Expand Down
4 changes: 2 additions & 2 deletions src/NeoSharp.Application/Controllers/PromptDebugController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -134,15 +134,15 @@ public byte[] GetScript(byte[] scriptHash, bool isDynamicInvoke)

Contract contract = Contract.GetContract(hash);

if (contract == null /* TODO: || (isDynamicInvoke && !contract.AllowDynamicInvokes) */) return null;
if (contract == null /* TODO #400: || (isDynamicInvoke && !contract.AllowDynamicInvokes) */) return null;

return contract.Script;
}
}

DebugScriptTable _scriptTable = new DebugScriptTable();

// TODO: attach
// TODO #401: Implement test invoke with asset attachment
// testinvoke {contract hash} {params} (--attach-neo={amount}, --attach-gas={amount}) (--from-addr={addr})

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public class PromptRpcController : IPromptController
private readonly IRpcServer _rpc;
private readonly IConsoleWriter _consoleWriter;

// TODO: invoke and invokefunction => ContractParameter json serializable/deserializable acording to NEO
// TODO #402: invoke and invokefunction => ContractParameter json serializable/deserializable acording to NEO

public class SendManyParams
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ public void LoadCommand(FileInfo commandsFile)
}

/*
TODO
TODO #403: Implement additional features
notifications {block_number or address}
mem
config debug {on/off}
Expand Down
51 changes: 36 additions & 15 deletions src/NeoSharp.Application/Controllers/PromptWalletController.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
using NeoSharp.Application.Attributes;
using System.Linq;
using NeoSharp.Application.Attributes;
using NeoSharp.Application.Client;
using NeoSharp.Core.Extensions;
using NeoSharp.Core.Types;
using NeoSharp.Core.Wallet;
using NeoSharp.Core.Wallet.Helpers;

namespace NeoSharp.Application.Controllers
{
Expand Down Expand Up @@ -36,11 +39,19 @@ public void WalletCreateCommand(string fileName)
{
_walletManager.CreateWallet(fileName);
var secureString = _consoleReader.ReadPassword();
var walletAccount = _walletManager.CreateAccount(secureString);

_consoleWriter.WriteLine("");
_consoleWriter.WriteLine("Address: " + walletAccount.Address, ConsoleOutputStyle.Information);
_consoleWriter.WriteLine("Public Key: " + _walletManager.GetPublicKeyFromNep2(walletAccount.Key, secureString), ConsoleOutputStyle.Information);
_consoleWriter.WriteLine("\n", ConsoleOutputStyle.Information); //How these line breaks can be improved?
var confirmationString = _consoleReader.ReadPassword();
if (secureString.ToByteArray().SequenceEqual(confirmationString.ToByteArray()))
{
var walletAccount = _walletManager.CreateAndAddAccount(secureString);
_consoleWriter.WriteLine("\nAddress: " + walletAccount.Address, ConsoleOutputStyle.Information);
_consoleWriter.WriteLine("Public Key: " + _walletManager.GetPublicKeyFromNep2(walletAccount.Key, secureString), ConsoleOutputStyle.Information);
}
else
{
_consoleWriter.WriteLine("\nPasswords don't match.", ConsoleOutputStyle.Information);
}

}

[PromptCommand("wallet open", Category = "Wallet", Help = "Open wallet")]
Expand Down Expand Up @@ -87,21 +98,31 @@ public void WalletSaveCommand(string fileName)
public void AccountCreateCommand()
{
var secureString = _consoleReader.ReadPassword();
var walletAccount = _walletManager.CreateAccount(secureString);

_consoleWriter.WriteLine("");
_consoleWriter.WriteLine("Address: " + walletAccount.Address, ConsoleOutputStyle.Information);
_consoleWriter.WriteLine("Public Key: " + _walletManager.GetPublicKeyFromNep2(walletAccount.Key, secureString), ConsoleOutputStyle.Information);
_consoleWriter.ApplyStyle(ConsoleOutputStyle.Prompt);
_consoleWriter.WriteLine("\nConfirm your password:", ConsoleOutputStyle.Information);
var confirmationString = _consoleReader.ReadPassword();
if(secureString.ToByteArray().SequenceEqual(confirmationString.ToByteArray()))
{
var walletAccount = _walletManager.CreateAndAddAccount(secureString);
_consoleWriter.WriteLine("\nAddress: " + walletAccount.Address, ConsoleOutputStyle.Information);
_consoleWriter.WriteLine("Public Key: " + _walletManager.GetPublicKeyFromNep2(walletAccount.Key, secureString), ConsoleOutputStyle.Information);
}
else
{
_consoleWriter.WriteLine("Passwords don't match.");
}
}

[PromptCommand("account delete", Category = "Account", Help = "Deletes an account")]
public void AccountDeleteCommand(UInt160 scriptHash)
public void AccountDeleteCommand(string address)
{
_walletManager.DeleteAccount(scriptHash);
//Should we ask for a confirmation? Should we ask for the password?
_walletManager.DeleteAccount(address.ToScriptHash());
_consoleWriter.WriteLine("Account deleted.");
}

/*
TODO WALLET:
TODO #404: Implement additional wallet features
wallet delete_addr {addr}
wallet delete_token {token_contract_hash}
wallet alias {addr} {title}
Expand All @@ -115,7 +136,7 @@ export nep2 {address}
*/

/*
TODO TRANSACTION MANAGER:
TODO #405: Implement additional transaction manager features
wallet rebuild {start block}
wallet claim
wallet tkn_send {token symbol} {address_from} {address to} {amount}
Expand Down
2 changes: 1 addition & 1 deletion src/NeoSharp.Application/DI/VMModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public IExecutionEngine Create(ExecutionEngineArgs args)

public void Register(IContainerBuilder containerBuilder)
{
// TODO: Inject here HyperVM Nuget package
// TODO #406: Inject here HyperVM Nuget package

containerBuilder.RegisterSingleton<IVMFactory, NullVM>();
//containerBuilder.RegisterSingleton<IVMFactory, NeoVM>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public BinaryDictionarySerializer(Type type, Type keyType, IBinaryCustomSerializ

if (_type.IsInterface)
{
// TODO: Compose a Dictionary<T> type
// TODO #394: Compose a Dictionary<T> type
}

_keyType = keyType;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public BinaryHashSetSerializer(Type typeSet, IBinaryCustomSerializable serialize

if (_type.IsInterface)
{
// TODO: Compose a HashTag<T> type
// TODO #393: Compose a HashTag<T> type
}

MaxLength = maxLength;
Expand Down
6 changes: 3 additions & 3 deletions src/NeoSharp.Core/Blockchain/Blockchain.cs
Original file line number Diff line number Diff line change
Expand Up @@ -183,11 +183,11 @@ public async Task<Transaction> GetTransaction(UInt256 hash)
return await _repository.GetTransaction(hash);
}

// TODO [AboimPinto] Async methods cannot have out parameters. Method not used for now.
// TODO #390 [AboimPinto] Async methods cannot have out parameters. Method not used for now.
///// <inheritdoc />
//public async Task<Transaction> GetTransaction(UInt256 hash, out int height)
//{
// // TODO: How to get the height?
// // TODO #390: How to get the height?

// height = 0;
// return await _repository.GetTransaction(hash);
Expand All @@ -211,7 +211,7 @@ public async Task<IEnumerable<Transaction>> GetTransactions(IReadOnlyCollection<
/// <inheritdoc />
public async Task<bool> ContainsTransaction(UInt256 hash)
{
// TODO: Optimize this
// TODO #389: Optimize this
return await _repository.GetTransaction(hash) != null;
}

Expand Down
4 changes: 2 additions & 2 deletions src/NeoSharp.Core/Blockchain/CoinIndex.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@ public async Task IndexBlock(Block block)
throw new ArgumentException(
$"Unable to index block: Requires next block to be {currentHeight + 1} but received block {block.Index}");

// TODO: Index Spendable
// TODO: Index Claimable
// TODO #387: Index Spendable
// TODO #388: Index Claimable
await _repository.SetIndexHeight(currentHeight + 1);
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/NeoSharp.Core/Blockchain/GenesisAssets.cs
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ public static MinerTransaction GenesisMinerTransaction()
}

/// <summary>
/// TODO: Check with team how to document this
/// TODO #386: Check with team how to document this
/// </summary>
/// <returns>The witness.</returns>
public static Witness GenesisWitness()
Expand Down
2 changes: 1 addition & 1 deletion src/NeoSharp.Core/Blockchain/IBlockchain.cs
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ public interface IBlockchain
/// <returns></returns>
Task<Transaction> GetTransaction(UInt256 hash);

// TODO [AboimPinto] Async methods cannot have out parameters. Method not used for now.
// TODO #385 [AboimPinto] Async methods cannot have out parameters. Method not used for now.
///// <summary>
///// Return the corresponding transaction information and the height of the block where the transaction is located according to the specified hash value
///// </summary>
Expand Down
20 changes: 15 additions & 5 deletions src/NeoSharp.Core/Blockchain/Processing/BlockHeaderPersister.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using NeoSharp.Core.Extensions;
Expand Down Expand Up @@ -28,11 +29,20 @@ public async Task Persist(params BlockHeader[] blockHeaders)
{
if (blockHeaders == null) throw new ArgumentNullException(nameof(blockHeaders));

var blockHeadersToPersist = blockHeaders
.Where(bh => bh != null && bh.Index > LastBlockHeader?.Index)
.Distinct(bh => bh.Index)
.OrderBy(bh => bh.Index)
.ToList();
var blockHeadersToPersist = new List<BlockHeader>();
if (this.LastBlockHeader == null)
{
// Persisting the Genesis block
blockHeadersToPersist = blockHeaders.ToList();
}
else
{
blockHeadersToPersist = blockHeaders
.Where(bh => bh != null && bh.Index > LastBlockHeader?.Index)
.Distinct(bh => bh.Index)
.OrderBy(bh => bh.Index)
.ToList();
}

foreach (var blockHeader in blockHeadersToPersist)
{
Expand Down
94 changes: 94 additions & 0 deletions src/NeoSharp.Core/Blockchain/Processing/BlockPersister.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
using System;
using System.Threading.Tasks;
using NeoSharp.Core.Models;
using NeoSharp.Core.Persistence;

namespace NeoSharp.Core.Blockchain.Processing
{
public class BlockPersister : IBlockPersister
{
#region Private Fields
private readonly IRepository _repository;
private readonly IBlockHeaderPersister _blockHeaderPersister;
private readonly ITransactionPersister<Transaction> _transactionPersister;
private readonly ITransactionPool _transactionPool;
#endregion

#region Constructor
public BlockPersister(
IRepository repository,
IBlockHeaderPersister blockHeaderPersister,
ITransactionPersister<Transaction> transactionPersister,
ITransactionPool transactionPool)
{
this._repository = repository;
this._blockHeaderPersister = blockHeaderPersister;
this._transactionPersister = transactionPersister;
this._transactionPool = transactionPool;
}
#endregion

#region IBlockPersister Implementation
public Block LastPersistedBlock { get; private set; }

public event EventHandler<BlockHeader[]> OnBlockHeadersPersisted;

public async Task Persist(params Block[] blocks)
{
foreach (var block in blocks)
{
this.LastPersistedBlock = block;

var blockHeader = await this._repository.GetBlockHeader(block.Hash);
if (blockHeader == null)
{
await this._blockHeaderPersister.Persist(block.GetBlockHeader());
}

foreach (var transaction in block.Transactions)
{
await this._transactionPersister.Persist(transaction);
this._transactionPool.Remove(transaction.Hash);
}
}
}

public async Task Persist(params BlockHeader[] blockHeaders)
{
try
{
this._blockHeaderPersister.OnBlockHeadersPersisted += this.HandleBlockHandlePersisted;
await this._blockHeaderPersister.Persist(blockHeaders);
}
finally
{
this._blockHeaderPersister.OnBlockHeadersPersisted -= this.HandleBlockHandlePersisted;
}
}

public async Task<bool> IsBlockPersisted(Block block)
{
var blockHeader = await this._repository.GetBlockHeader(block.Hash);

if (blockHeader?.Type == HeaderType.Extended)
{
throw new InvalidOperationException($"The block \"{block.Hash.ToString(true)}\" exists already on the blockchain.");
}

if (blockHeader != null && blockHeader.Hash != block.Hash)
{
throw new InvalidOperationException($"The block \"{block.Hash.ToString(true)}\" has an invalid hash.");
}

return false;
}
#endregion

#region Private Method
private void HandleBlockHandlePersisted(object sender, BlockHeader[] e)
{
this.OnBlockHeadersPersisted?.Invoke(sender, e);
}
#endregion
}
}
Loading