This repository has been archived by the owner on Aug 16, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 312
Make RPC methods easier to create with binders, improve testing #221
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
c94dc8a
Can generate blocks through RPC
NicolasDorier 5a3f0b9
Add compile to net462, for better debug experience and profiler
NicolasDorier efaaa1e
Make tests faster by using a preloaded test wallet
NicolasDorier 5d45cb1
Create model binders for the RPC controller
NicolasDorier File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Text; | ||
|
||
namespace Stratis.Bitcoin.IntegrationTests | ||
{ | ||
class Program | ||
{ | ||
public static void Main(string[] args) | ||
{ | ||
new WalletTests().CanSendToAddress(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
using Microsoft.AspNetCore.Mvc; | ||
using Stratis.Bitcoin.Miner; | ||
using System; | ||
using System.Linq; | ||
using System.Collections.Generic; | ||
using System.Text; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Stratis.Bitcoin.Wallet; | ||
using NBitcoin; | ||
|
||
namespace Stratis.Bitcoin.RPC.Controllers | ||
{ | ||
public class WalletRPCController : BaseRPCController | ||
{ | ||
public class UsedWallet | ||
{ | ||
public string WalletName | ||
{ | ||
get; set; | ||
} | ||
public HdAccount Account | ||
{ | ||
get; | ||
set; | ||
} | ||
} | ||
public WalletRPCController(IServiceProvider serviceProvider, IWalletManager walletManager) | ||
{ | ||
this.WalletManager = walletManager; | ||
this.serviceProvider = serviceProvider; | ||
} | ||
|
||
IServiceProvider serviceProvider; | ||
|
||
public IWalletManager WalletManager | ||
{ | ||
get; set; | ||
} | ||
|
||
|
||
[ActionName("sendtoaddress")] | ||
public uint256 SendToAddress(BitcoinAddress bitcoinAddress, Money amount) | ||
{ | ||
return uint256.Zero; | ||
} | ||
|
||
[ActionName("generate")] | ||
public List<uint256> Generate(int nBlock) | ||
{ | ||
var mining = this.serviceProvider.GetRequiredService<PowMining>(); | ||
var wallet = GetWallet(); | ||
var address = this.WalletManager.GetUnusedAddress(wallet.WalletName, wallet.Account.Name); | ||
return mining.GenerateBlocks(new ReserveScript(address.Pubkey), (ulong)nBlock, int.MaxValue); | ||
} | ||
|
||
private UsedWallet GetWallet() | ||
{ | ||
var w = this.WalletManager.GetWallets().FirstOrDefault(); | ||
if(w == null) | ||
throw new RPCServerException(NBitcoin.RPC.RPCErrorCode.RPC_INVALID_REQUEST, "No wallet found"); | ||
var account = this.WalletManager.GetAccounts(w).FirstOrDefault(); | ||
return new UsedWallet() | ||
{ | ||
Account = account, | ||
WalletName = w | ||
}; | ||
} | ||
|
||
private string GetAccountName() | ||
{ | ||
return this.WalletManager.GetAccounts(GetWalletName()).FirstOrDefault().Name; | ||
} | ||
|
||
private string GetWalletName() | ||
{ | ||
return this.WalletManager.GetWallets().FirstOrDefault(); | ||
} | ||
} | ||
} |
65 changes: 65 additions & 0 deletions
65
Stratis.Bitcoin/RPC/ModelBinders/DestinationModelBinder.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
using Microsoft.AspNetCore.Mvc.ModelBinding; | ||
using NBitcoin; | ||
using System.Reflection; | ||
using System; | ||
using System.Threading.Tasks; | ||
using Microsoft.AspNetCore.Mvc.Internal; | ||
|
||
namespace Stratis.Bitcoin.RPC.ModelBinders | ||
{ | ||
public class DestinationModelBinder : IModelBinder, IModelBinderProvider | ||
{ | ||
public DestinationModelBinder() | ||
{ | ||
|
||
} | ||
|
||
#region IModelBinder Members | ||
|
||
public Task BindModelAsync(ModelBindingContext bindingContext) | ||
{ | ||
if(!SupportType(bindingContext.ModelType)) | ||
{ | ||
return TaskCache.CompletedTask; | ||
} | ||
|
||
ValueProviderResult val = bindingContext.ValueProvider.GetValue( | ||
bindingContext.ModelName); | ||
if(val == null) | ||
{ | ||
return TaskCache.CompletedTask; | ||
} | ||
|
||
string key = val.FirstValue as string; | ||
if(key == null) | ||
{ | ||
return TaskCache.CompletedTask; | ||
} | ||
|
||
var network = (Network)bindingContext.HttpContext.RequestServices.GetService(typeof(Network)); | ||
//TODO: Use var data = Network.Parse(key, network); when NBitcoin is updated to latest version | ||
var data = BitcoinAddress.Create(key, network); | ||
if(!bindingContext.ModelType.IsInstanceOfType(data)) | ||
{ | ||
throw new FormatException("Invalid destination type"); | ||
} | ||
bindingContext.Result = ModelBindingResult.Success(data); | ||
return TaskCache.CompletedTask; | ||
} | ||
|
||
private static bool SupportType(Type type) | ||
{ | ||
return (typeof(Base58Data).GetTypeInfo().IsAssignableFrom(type) || | ||
typeof(IDestination).GetTypeInfo().IsAssignableFrom(type)); | ||
} | ||
|
||
public IModelBinder GetBinder(ModelBinderProviderContext context) | ||
{ | ||
if(SupportType(context.Metadata.ModelType)) | ||
return this; | ||
return null; | ||
} | ||
|
||
#endregion | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
using Microsoft.AspNetCore.Mvc.Internal; | ||
using Microsoft.AspNetCore.Mvc.ModelBinding; | ||
using NBitcoin; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace Stratis.Bitcoin.RPC.ModelBinders | ||
{ | ||
public class MoneyModelBinder : IModelBinder, IModelBinderProvider | ||
{ | ||
public Task BindModelAsync(ModelBindingContext bindingContext) | ||
{ | ||
if(bindingContext.ModelType != typeof(Money)) | ||
{ | ||
return TaskCache.CompletedTask; | ||
} | ||
|
||
ValueProviderResult val = bindingContext.ValueProvider.GetValue( | ||
bindingContext.ModelName); | ||
if(val == null) | ||
{ | ||
return TaskCache.CompletedTask; | ||
} | ||
|
||
string key = val.FirstValue as string; | ||
if(key == null) | ||
{ | ||
return TaskCache.CompletedTask; | ||
} | ||
return Task.FromResult(Money.Parse(key)); | ||
} | ||
|
||
public IModelBinder GetBinder(ModelBinderProviderContext context) | ||
{ | ||
if(context.Metadata.ModelType == typeof(Money)) | ||
return this; | ||
return null; | ||
} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Super duper
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
oops lol