diff --git a/DOCUMENTATION.md b/DOCUMENTATION.md index a55b4e8..e354574 100644 --- a/DOCUMENTATION.md +++ b/DOCUMENTATION.md @@ -28,18 +28,25 @@ For projects that support adding a Nuget reference you may paste the following ## SDK References 1. Bank * AccountsService + * BankConfirmationBlockService + * BlocksService * ConfigService * ConnectedBankService - * TransactionService + * InvalidBlocksService + * TransactionsService + * UpgradeNoticeService * ValidatorService - * BankConfirmationBlockService + 2. Common * 3. Validator * AccountsService + * BankBlockService * ConfigService * ConnectedBankService + * PrimaryValidatorUpdatedService * TransactionService + * UpgradeRequestService * ValidatorService * ValidatorConfirmationBlockService @@ -64,14 +71,20 @@ Description Thenewboston.Bank.Api Thenewboston.Bank.Models -## AccountsService +## AccountsService +## BankConfirmationBLockService +## BlocksService ## ConfigService +## ConfirmationService ## ConnectedBankService +## ConnectionRequestService +## InvalidBlocksService ## TransactionService +## UpgradeNoticeService ## ValidatorService -## BankConfirmationBlockService ## Bank Exceptions (BNK) -|Exception|Class|Thrown From|Description | + +|Exception|Class|Thrown From|Description| |--|--|--|--| |BNK001|AccountsService|SampleMethod()|Sample Description| |BNK002|ConfigService|SampleMethod()|Sample Description| @@ -96,9 +109,14 @@ Description Thenewboston.Validator.Api Thenewboston.Validator.Models ## AccountsService +## BankBlockService ## ConfigService +## ConfirmationService ## ConnectedBankService +## ConnectionRequestService +## PrimaryValidatorUpdatedService ## TransactionService +## UpgradeRequestService ## ValidatorService ## ValidatorConfirmationBlockService ## Validator Exceptions (VLD) diff --git a/README.md b/README.md index 0587c05..b3d4a50 100644 --- a/README.md +++ b/README.md @@ -3,7 +3,8 @@ This is a .NET Core SDK for **thenewboston**, which will be available cross-platoform. The library itself will solely contain the domain and data layers of **thenewboston**. The presentation layer will be the responsibility of the host application. ## Status -![SDK CI pipeline](https://github.com/thenewboston-developers/dotnetcore-sdk/workflows/SDK%20CI%20pipeline/badge.svg) +![SDK CI pipeline](https://github.com/thenewboston-developers/dotnetcore-sdk/workflows/SDK%20CI%20pipeline/badge.svg) +![Create and Publish package](https://github.com/thenewboston-developers/dotnetcore-sdk/workflows/Create%20and%20Publish%20package/badge.svg?branch=main) ## How to get started diff --git a/src/Thenewboston.Tests/Bank/Api/AccountsServiceTests.cs b/src/Thenewboston.Tests/Bank/Api/AccountsServiceTests.cs index c5d27ac..67064a2 100644 --- a/src/Thenewboston.Tests/Bank/Api/AccountsServiceTests.cs +++ b/src/Thenewboston.Tests/Bank/Api/AccountsServiceTests.cs @@ -11,6 +11,7 @@ using Xunit; using System.Linq; using Thenewboston.Bank.Api.Models; +using Thenewboston.Common.Api.Models; namespace Thenewboston.Tests.Bank.Api { @@ -25,8 +26,8 @@ public async void ListOfAccountsIsReturned() var accounts = await service.GetAccountsAsync(); - Assert.Equal(2, accounts.Count()); - Assert.Equal("9eca00a5-d925-454c-a8d6-ecbb26ec2f76", accounts.ElementAt(0).Id); + Assert.Equal(2, accounts.Count); + Assert.Equal("9eca00a5-d925-454c-a8d6-ecbb26ec2f76", accounts.Results.ElementAt(0).Id); } } @@ -49,35 +50,37 @@ public static AccountsService BuildAccountsServiceMock() { var requestSender = new Mock(); - var listResult = new List - { - new BankAccount - { - Id = "9eca00a5-d925-454c-a8d6-ecbb26ec2f76", - AccountNumber = "4d2ec91f37bc553bc538e91195669b666e26b2ea3e4e31507e38102a758d4f86", - Created = DateTime.Now.AddDays(-3), - Modified = DateTime.Now, - Trust = "99.73" - }, - new BankAccount - { - Id = "ae4d43b0-5c34-4e56-8266-0e3531268815", - AccountNumber = "a29baa6ba36f6db707f8f8dacfa82d5e8a28fa616e8cc96cf6d7790f551d79f2", - Created = DateTime.Now.AddDays(-3), - Modified = DateTime.Now, - Trust = "94.61" + var listResult = new PaginatedResponseModel() { + Count = 2, + Next = string.Empty, + Previous = string.Empty, + Results = new List() { + new BankAccount { + Id = "9eca00a5-d925-454c-a8d6-ecbb26ec2f76", + AccountNumber = "4d2ec91f37bc553bc538e91195669b666e26b2ea3e4e31507e38102a758d4f86", + Created = DateTime.Now.AddDays(-3), + Modified = DateTime.Now, + Trust = "99.73" + }, + new BankAccount { + Id = "ae4d43b0-5c34-4e56-8266-0e3531268815", + AccountNumber = "a29baa6ba36f6db707f8f8dacfa82d5e8a28fa616e8cc96cf6d7790f551d79f2", + Created = DateTime.Now.AddDays(-3), + Modified = DateTime.Now, + Trust = "94.61" + } } }; var getAllResponse = new HttpResponseMessage(System.Net.HttpStatusCode.OK); - getAllResponse.Content = new StringContent(JsonConvert.SerializeObject(listResult), Encoding.UTF8, "application/json"); + getAllResponse.Content = + new StringContent(JsonConvert.SerializeObject(listResult), Encoding.UTF8, "application/json"); requestSender .Setup(x => x.GetAsync(It.IsAny())) .Returns(Task.FromResult(getAllResponse)); - var updateResult = new BankAccount - { + var updateResult = new BankAccount { Id = "64426fc5-b3ac-42fb-b75b-d5ccfcdc6872", AccountNumber = "a29baa6ba36f6db707f8f8dacfa82d5e8a28fa616e8cc96cf6d7790f551d79f2", Created = DateTime.Now.AddDays(-3), @@ -86,7 +89,8 @@ public static AccountsService BuildAccountsServiceMock() }; var updateResponse = new HttpResponseMessage(System.Net.HttpStatusCode.OK); - updateResponse.Content = new StringContent(JsonConvert.SerializeObject(updateResult), Encoding.UTF8, "application/json"); + updateResponse.Content = new StringContent(JsonConvert.SerializeObject(updateResult), Encoding.UTF8, + "application/json"); requestSender .Setup(x => x.PatchAsync(It.IsAny(), It.IsAny())) @@ -96,4 +100,4 @@ public static AccountsService BuildAccountsServiceMock() return bankService; } } -} +} \ No newline at end of file diff --git a/src/Thenewboston.Tests/Bank/Api/BankConfirmationBlockServiceTests.cs b/src/Thenewboston.Tests/Bank/Api/BankConfirmationBlockServiceTests.cs index 3d0dab0..006929d 100644 --- a/src/Thenewboston.Tests/Bank/Api/BankConfirmationBlockServiceTests.cs +++ b/src/Thenewboston.Tests/Bank/Api/BankConfirmationBlockServiceTests.cs @@ -5,6 +5,7 @@ using Moq; using Newtonsoft.Json; using Thenewboston.Bank.Api; +using Thenewboston.Bank.Api.Models; using Thenewboston.Bank.Models; using Thenewboston.Common.Api.Models; using Thenewboston.Common.Http; @@ -23,17 +24,17 @@ public class BankConfirmationBlockServiceTests #region Test Models - private PaginatedResponseModel CreateMockBankConfirmationBlock() + private PaginatedResponseModel CreateMockBankConfirmationBlock() { - return new PaginatedResponseModel() + return new PaginatedResponseModel() { Count = 1, Next = string.Empty, Previous = string.Empty, Results = - new List() + new List() { - new BankConfirmationBlockResponse + new BankConfirmationBlock { Id = "e7c5c2e0-8ed1-4eb3-abd8-97fa2e5ca8db", CreatedDate = DateTime.Parse("2020-10-08T02:18:07.908635Z"), @@ -143,10 +144,10 @@ private IBankConfirmationBlockService BuildConfirmationBlockPostMock() #region Tests [Fact] - public async void BankConfiramtionBlockReturnedAsync() + public async void BankConfirmationBlockReturnedAsync() { var service = BuildConfirmationBlockGetMock(); - var returnedBankConfirmationBlock = await service.GetAllBankConfiramtionBlocksAsync(); + var returnedBankConfirmationBlock = await service.GetAllBankConfiramtionBlocksAsync(0, 10); var expectedResult = JsonConvert.SerializeObject(CreateMockBankConfirmationBlock()); var actualResult = JsonConvert.SerializeObject(returnedBankConfirmationBlock); Assert.Equal(expectedResult, actualResult); diff --git a/src/Thenewboston.Tests/Bank/Api/BlocksServiceTests.cs b/src/Thenewboston.Tests/Bank/Api/BlocksServiceTests.cs new file mode 100644 index 0000000..7767ac6 --- /dev/null +++ b/src/Thenewboston.Tests/Bank/Api/BlocksServiceTests.cs @@ -0,0 +1,121 @@ +using System; +using System.Collections.Generic; +using System.Net.Http; +using System.Text; +using Moq; +using Newtonsoft.Json; +using Thenewboston.Bank.Api; +using Thenewboston.Bank.Models; +using Thenewboston.Common.Api.Models; +using Thenewboston.Common.Http; +using Thenewboston.Common.Models; +using Xunit; +using Block = Thenewboston.Common.Models.Block; +using BlockMessage = Thenewboston.Common.Models.BlockMessage; + +namespace Thenewboston.Tests.Bank +{ + public class BlocksServiceTests + { + #region Test Models + + private PaginatedResponseModel CreateMockBlock() + { + return new PaginatedResponseModel() { + Count = 1, + Next = string.Empty, + Previous = string.Empty, + Results = + new List() { + new BankBlock { + Id = "c6fc11cf-8948-4d32-96c9-d56caa6d5b24", + Created = DateTime.Parse("2020-10-08T02:18:07.324999Z"), + Modified = DateTime.Parse("2020-10-08T02:18:07.325044Z"), + BalanceKey = "a37e2836805975f334108b55523634c995bd2a4db610062f404510617e83126f", + Sender = "a37e2836805975f334108b55523634c995bd2a4db610062f404510617e83126f", + Signature = + "a2ba346d98cb1f7ce6bf017240d674a9928796ddb564a2c8817e68ead0ea02d960e970fe581c6d3a25b9876e1873d51c882b23d843e32f511d9575ef60d2940d" + } + } + }; + } + + private Block CreateMockBlockMessage() + { + return new Block() { + AccountNumber = "0cdd4ba04456ca169baca3d66eace869520c62fe84421329086e03d91a68acdb", + Message = new BlockMessage() { + BalanceKey = "ce51f0d9facaa7d3e69657429dd3f961ce70077a8efb53dcda508c7c0a19d2e3", + Transactions = new List() { + new BlockTransaction() { + Amount = "12.5", + Recipient = "484b3176c63d5f37d808404af1a12c4b9649cd6f6769f35bdf5a816133623fbc" + }, + new BlockTransaction() { + Amount = "1", Recipient = "5e12967707909e62b2bb2036c209085a784fabbc3deccefee70052b6181c8ed8" + }, + new BlockTransaction() { + Amount = "4", Recipient = "ad1f8845c6a1abb6011a2a434a079a087c460657aad54329a84b406dce8bf314" + } + } + }, + Signature = + "ee5a2f2a2f5261c1b633e08dd61182fd0db5604c853ebd8498f6f28ce8e2ccbbc38093918610ea88a7ad47c7f3192ed955d9d1529e7e390013e43f25a5915c0f" + }; + } + + #endregion + + #region Mock Service + + private IBlocksService BuildBlockGetMock() + { + var response = new HttpResponseMessage(System.Net.HttpStatusCode.OK); + response.Content = new StringContent(JsonConvert.SerializeObject(CreateMockBlock()), Encoding.UTF8, + "application/json"); + var requestSenderMock = new Mock(); + + requestSenderMock + .Setup(s => s.GetAsync(It.IsAny())) + .ReturnsAsync(response); + + IBlocksService service = new BlocksService(requestSenderMock.Object); + return service; + } + + private IBlocksService BuildBlockPostMock() + { + var requestSenderMock = new Mock(); + requestSenderMock.Setup(s => s.PostAsync(It.IsAny(), It.IsAny())) + .ReturnsAsync(new HttpResponseMessage(System.Net.HttpStatusCode.Created)); + + IBlocksService service = new BlocksService(requestSenderMock.Object); + return service; + } + + #endregion + + #region Tests + + [Fact] + public async void BlockReturnedAsync() + { + var service = BuildBlockGetMock(); + var returnedBlock = await service.GetBlocksAsync(0, 10); + var expectedResult = JsonConvert.SerializeObject(CreateMockBlock()); + var actualResult = JsonConvert.SerializeObject(returnedBlock); + Assert.Equal(expectedResult, actualResult); + } + + //TODO :remove + //[Fact] + //public async void BlockPostedAsync() + //{ + // var service = BuildBlockPostMock(); + // var response = await service.PostBlocksAsync(CreateMockBlockMessage()); + // Assert.NotNull(response); + //} + + #endregion + } +} \ No newline at end of file diff --git a/src/Thenewboston.Tests/Bank/Api/ConnectedBanksServiceTests.cs b/src/Thenewboston.Tests/Bank/Api/ConnectedBanksServiceTests.cs index c308fdb..75cf327 100644 --- a/src/Thenewboston.Tests/Bank/Api/ConnectedBanksServiceTests.cs +++ b/src/Thenewboston.Tests/Bank/Api/ConnectedBanksServiceTests.cs @@ -20,15 +20,15 @@ public class GetBanksAsync [Fact] public async void ListOfBanksIsReturned() { - PaginatedResponseModel expectedResponseModel = new PaginatedResponseModel + var expectedResponseModel = new PaginatedResponseModel { Count = 2, Next = null, Previous = null, Results = - new List + new List { - new BankResponseModel + new BankNode { AccountNumber = "5e12967707909e62b2bb2036c209085a784fabbc3deccefee70052b6181c8ed8", IpAddress = "83.168.1.232", @@ -39,7 +39,7 @@ public async void ListOfBanksIsReturned() DefaultTransactionFee = 1, Trust = "100.00" }, - new BankResponseModel + new BankNode { AccountNumber = "db1a9ac3c356ab744ab4ad5256bb86c2f6dfaa7c1aece1f026a08dbd8c7178f2", IpAddress = "74.124.1.68", @@ -55,7 +55,7 @@ public async void ListOfBanksIsReturned() var service = BuildGetBanksAsyncConnectedBanksServiceMock(expectedResponseModel); - var banks = await service.GetBanksAsync(); + var banks = await service.GetBanksAsync(0, 10); var expectedResponseModelStr = JsonConvert.SerializeObject(expectedResponseModel); var actualResponseModelStr = JsonConvert.SerializeObject(banks); @@ -68,7 +68,7 @@ public class UpdateBankAsync [Fact] public async void UpdatedBankIsReturned() { - var expectedBank = new BankResponseModel + var expectedBank = new BankNode { AccountNumber = "5e12967707909e62b2bb2036c209085a784fabbc3deccefee70052b6181c8ed8", IpAddress = "83.168.1.232", @@ -92,7 +92,7 @@ public async void UpdatedBankIsReturned() } } - public static IConnectedBanksService BuildGetBanksAsyncConnectedBanksServiceMock(PaginatedResponseModel expectedResponseModel) + public static IConnectedBanksService BuildGetBanksAsyncConnectedBanksServiceMock(PaginatedResponseModel expectedResponseModel) { var response = new HttpResponseMessage(HttpStatusCode.OK); response.Content = new StringContent(JsonConvert.SerializeObject(expectedResponseModel), Encoding.UTF8, "application/json"); @@ -101,13 +101,13 @@ public static IConnectedBanksService BuildGetBanksAsyncConnectedBanksServiceMock IConnectedBanksService service = new ConnectedBanksService(requestSenderMock.Object); requestSenderMock - .Setup(x => x.GetAsync("/banks")) + .Setup(x => x.GetAsync(It.IsAny())) .ReturnsAsync(response); return service; } - public static IConnectedBanksService BuildUpdateBankAsyncConnectedBanksServiceMock(BankResponseModel expectedBank) + public static IConnectedBanksService BuildUpdateBankAsyncConnectedBanksServiceMock(BankNode expectedBank) { var response = new HttpResponseMessage(HttpStatusCode.OK); response.Content = new StringContent(JsonConvert.SerializeObject(expectedBank), Encoding.UTF8, "application/json"); diff --git a/src/Thenewboston.Tests/Bank/Api/ConnectionRequestServiceTest.cs b/src/Thenewboston.Tests/Bank/Api/ConnectionRequestServiceTest.cs new file mode 100644 index 0000000..f19c88b --- /dev/null +++ b/src/Thenewboston.Tests/Bank/Api/ConnectionRequestServiceTest.cs @@ -0,0 +1,73 @@ +using System; +using System.Collections.Generic; +using System.Net.Http; +using System.Text; +using System.Threading.Tasks; +using Moq; +using Thenewboston.Bank.Api; +using Thenewboston.Bank.Models; +using Thenewboston.Common.Http; +using Xunit; + +namespace Thenewboston.Tests.Bank.Api +{ + public class ConnectionRequestServiceTest + { + public ConnectionRequest CreateMockConnectionRequest() + { + return new ConnectionRequest() + { + Message = new ConnectionRequestMessage() + { + IpAddress = "192.168.1.232", + Port = "8000", + Protocol = "http" + }, + NodeIdentifier = "d5356888dc9303e44ce52b1e06c3165a7759b9df1e6a6dfbd33ee1c3df1ab4d1", + Signature = "3c88665e123e7e25a8b9d9592f3269ab4efc4bcba989a103a898e2625933261b1cccdaf2f52eca9c58d2bf033968ab6b702089bca8fc6e0c80b3b002a5e05b03" + }; + } + + public IConnectionRequestService ConnectionRequestAcceptsMock() + { + var requestSender = new Mock(); + var httpResponse = new HttpResponseMessage(System.Net.HttpStatusCode.OK); + + requestSender.Setup(s => s.PostAsync(It.IsAny(), It.IsAny())) + .ReturnsAsync(httpResponse); + + var service = new ConnectionRequestService(requestSender.Object); + return service; + } + + public IConnectionRequestService ConnectionRequestRejectsMock() + { + var requestSender = new Mock(); + var httpResponse = new HttpResponseMessage(System.Net.HttpStatusCode.BadRequest); + + requestSender.Setup(s => s.PostAsync(It.IsAny(), It.IsAny())) + .ReturnsAsync(httpResponse); + + var service = new ConnectionRequestService(requestSender.Object); + return service; + } + + [Fact] + public async Task UpgradeRequestNodeAcceptsTest() + { + var service = ConnectionRequestAcceptsMock(); + var result = await service.PostConnectionRequestAsync(CreateMockConnectionRequest()); + var expectedResult = new HttpResponseMessage(System.Net.HttpStatusCode.OK); + Assert.Equal(expectedResult.StatusCode, result.StatusCode); + } + + [Fact] + public async Task UpgradeRequestNodeRejectsTest() + { + var service = ConnectionRequestRejectsMock(); + var result = await service.PostConnectionRequestAsync(CreateMockConnectionRequest()); + var expectedResult = new HttpResponseMessage(System.Net.HttpStatusCode.BadRequest); + Assert.Equal(expectedResult.StatusCode, result.StatusCode); + } + } +} diff --git a/src/Thenewboston.Tests/Bank/Api/InvalidBlockServiceTests.cs b/src/Thenewboston.Tests/Bank/Api/InvalidBlockServiceTests.cs new file mode 100644 index 0000000..7f65dba --- /dev/null +++ b/src/Thenewboston.Tests/Bank/Api/InvalidBlockServiceTests.cs @@ -0,0 +1,119 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Net.Http; +using System.Text; +using System.Threading.Tasks; +using Moq; +using Newtonsoft.Json; +using Thenewboston.Bank.Api; +using Thenewboston.Bank.Api.Models; +using Thenewboston.Bank.Models; +using Thenewboston.Common.Api.Models; +using Thenewboston.Common.Http; +using Xunit; + +namespace Thenewboston.Tests.Bank.Api +{ + public class InvalidBlockServiceTests + { + public class GetInvalidBankBlocksAsync + { + [Fact] + public async void GetInvalidBlockWithNoPaginationModelReturnedSuccessCodeandData() + { + var service = BuildBankInvalidServiceMock(); + + var invalidBankBlocks = await service.GetInvalidBankBlocksAsync(); + + Assert.Equal(3, invalidBankBlocks.Count); + Assert.Equal("2bcd53c5-19f9-4226-ab04-3dfb17c3a1fe", invalidBankBlocks.Results.ElementAt(0).Id); + } + + [Fact] + public async void GetInvalidBlockWithPageOnlyPaginationReturnedSuccessCodeandData() + { + var service = BuildBankInvalidServiceMock(); + + var invalidBankBlocks = await service.GetInvalidBankBlocksAsync(); + + Assert.Equal(3, invalidBankBlocks.Count); + Assert.Equal("2bcd53c5-19f9-4226-ab04-3dfb17c3a1fe", invalidBankBlocks.Results.ElementAt(0).Id); + } + } + public class SendInvalidBankBlocksAsync + { + [Fact] + public async void SendInvalidBlockReturnedSuccessCodeandData() + { + var service = BuildBankInvalidServiceMock(); + var invalidBankBlocks = await service.SendInvalidBlocksToBankAsync( new BankInvalidBlockRequest()); + + Assert.Equal("2bcd53c5-19f9-4226-ab04-3dfb17c3a1fe", invalidBankBlocks.Id); + } + } + + private static List INVALID_BLOCK_TEST_DATA = new List + { + new BankInvalidBlock + { + Id = "2bcd53c5-19f9-4226-ab04-3dfb17c3a1fe", + BlockIdentifier = "65ae26192dfb9ec41f88c6d582b374a9b42ab58833e1612452d7a8f685dcd4d5", + Block = "3ff4ebb0-2b3d-429b-ba90-08133fcdee4e", + PrimaryValidator = "51461a75-dd8d-4133-81f4-543a3b054149", + ConfirmationValidator= "fcd2dce8-9e4f-4bf1-8dac-cdbaf64e5ce8", + Created = DateTime.Now.AddDays(-3), + Modified = DateTime.Now + }, + new BankInvalidBlock + { + Id = "5bcd53c5-19f9-a43b-ab04-3dfb17c3a165", + BlockIdentifier = "65ae26192dfb9ec41f88c6d582b374a9b42ab58833e1612452d7a8f685dcd4d5", + Block = "3ff4ebb0-2b3d-429b-ba90-08133fcdee4e", + PrimaryValidator = "51461a75-dd8d-4133-81f4-543a3b054149", + ConfirmationValidator= "fcd2dce8-9e4f-4bf1-8dac-cdbaf64e5ce8", + Created = DateTime.Now.AddDays(-4), + Modified = DateTime.Now + }, + new BankInvalidBlock + { + Id = "2b9e3180-cf38-4772-9d73-6318ee4113b9", + BlockIdentifier = "5e12967707909e62b2bb2036c209085a784fabbc3deccefee70052b6181c8ed8", + Block = "3ff4ebb0-2b3d-429b-ba90-08133fcdee4e", + PrimaryValidator = "51461a75-dd8d-4133-81f4-543a3b054149", + ConfirmationValidator= "fcd2dce8-9e4f-4bf1-8dac-cdbaf64e5ce8", + Created = DateTime.Now.AddDays(-5), + Modified = DateTime.Now + } + }; + + private static InvalidBlocksService BuildBankInvalidServiceMock() + { + var mockResponse = new PaginatedResponseModel() {Results = INVALID_BLOCK_TEST_DATA, Count = INVALID_BLOCK_TEST_DATA.Count}; + var requestSender = new Mock(); + + var getAllResponse = new HttpResponseMessage(System.Net.HttpStatusCode.OK); + getAllResponse.Content = new StringContent(JsonConvert.SerializeObject(mockResponse), Encoding.UTF8, "application/json"); + + requestSender + .Setup(x => x.GetAsync(It.IsAny())) + .Returns(Task.FromResult(getAllResponse)); + + var postResponse = new HttpResponseMessage(System.Net.HttpStatusCode.OK); + postResponse.Content = new StringContent(JsonConvert.SerializeObject(INVALID_BLOCK_TEST_DATA.First()), Encoding.UTF8, "application/json"); + + //TODO Setup for more Scenarios + requestSender + .Setup(x => x.PostAsync( "/invalid_blocks", It.IsAny())) + .Returns(Task.FromResult(postResponse)); + requestSender + .Setup(x => x.GetAsync(It.IsRegex(@"\/invalid_blocks\?offset=[0-9]*&limit=[0-9]*"))) + .Returns(Task.FromResult(getAllResponse)); requestSender + .Setup(x => x.GetAsync("/invalid_blocks")) + .Returns(Task.FromResult(getAllResponse)); + + var invalidBlockService = new InvalidBlocksService(requestSender.Object); + return invalidBlockService; + } + } +} \ No newline at end of file diff --git a/src/Thenewboston.Tests/Bank/Api/TransactionsServiceTests.cs b/src/Thenewboston.Tests/Bank/Api/TransactionsServiceTests.cs index b6b44dc..b93e837 100644 --- a/src/Thenewboston.Tests/Bank/Api/TransactionsServiceTests.cs +++ b/src/Thenewboston.Tests/Bank/Api/TransactionsServiceTests.cs @@ -8,6 +8,7 @@ using Newtonsoft.Json; using Thenewboston.Bank.Api; using Thenewboston.Bank.Models; +using Thenewboston.Common.Api.Models; using Thenewboston.Common.Http; using Xunit; @@ -15,20 +16,17 @@ namespace Thenewboston.Tests.Bank.Api { public class TransactionsServiceTests { - public class GetAllTransactionsAsync { [Fact] - public async void ListOfTransactionsIsReturned() { var service = BuildBankTransactionMock(); var transactions = await service.GetAllTransactionsAsync(); - Assert.Equal(2, transactions.Count()); - Assert.Equal("2484fbf3-2054-48a4-a1ce-66333cd15470", transactions.ElementAt(0).Id); - + Assert.Equal(2, transactions.Count); + Assert.Equal("2484fbf3-2054-48a4-a1ce-66333cd15470", transactions.Results.ElementAt(0).Id); } } @@ -36,43 +34,45 @@ public static TransactionsService BuildBankTransactionMock() { var requestSender = new Mock(); - var listResult = new List - { - new BankTransaction - { - Id = "2484fbf3-2054-48a4-a1ce-66333cd15470", - Block = new BankBlock - { - Id = "10cc3cec-eef6-453b-8667-f5cb19e6120d", - Created = DateTime.Now.AddDays(-3), - Modified = DateTime.Now, - BalanceKey = "4abccf4280ef61aad6f176a103933a42ed7dbf90d55e7912f404a704ede06f41", - Sender = "4abccf4280ef61aad6f176a103933a42ed7dbf90d55e7912f404a704ede06f41", - Signature = "c4a7696abc0f1e760a0c87d2a8db1faec060f99a77bf5f669a69dfdb301150db8fdaa2863a5d4a4e75be626ff56aecdbfea034283b1eb5a8e0196fd35f541b0b" + var listResult = new PaginatedResponseModel() { + Count = 2, + Next = string.Empty, + Previous = String.Empty, + Results = new List() { + new BankTransaction { + Id = "2484fbf3-2054-48a4-a1ce-66333cd15470", + Block = new BankBlock { + Id = "10cc3cec-eef6-453b-8667-f5cb19e6120d", + Created = DateTime.Now.AddDays(-3), + Modified = DateTime.Now, + BalanceKey = "4abccf4280ef61aad6f176a103933a42ed7dbf90d55e7912f404a704ede06f41", + Sender = "4abccf4280ef61aad6f176a103933a42ed7dbf90d55e7912f404a704ede06f41", + Signature = + "c4a7696abc0f1e760a0c87d2a8db1faec060f99a77bf5f669a69dfdb301150db8fdaa2863a5d4a4e75be626ff56aecdbfea034283b1eb5a8e0196fd35f541b0b" + }, + Amount = 1, + Recipient = "2e86f48216567302527b69eae6c6a188097ed3a9741f43cc3723e570cf47644c" }, - Amount = 1, - Recipient = "2e86f48216567302527b69eae6c6a188097ed3a9741f43cc3723e570cf47644c" - }, - new BankTransaction - { - Id = "2484fbf3-2054-48a4-a1ce-66333cd15470", - Block = new BankBlock - { - Id = "6b9bc4b1-76a7-4105-951f-b729b4befdef", - Created = DateTime.Now.AddDays(-3), - Modified = DateTime.Now, - BalanceKey = "4abccf4280ef61aad6f176a103933a42ed7dbf90d55e7912f404a704ede06f41", - Sender = "4abccf4280ef61aad6f176a103933a42ed7dbf90d55e7912f404a704ede06f41", - Signature = "c4a7696abc0f1e760a0c87d2a8db1faec060f99a77bf5f669a69dfdb301150db8fdaa2863a5d4a4e75be626ff56aecdbfea034283b1eb5a8e0196fd35f541b0b" - }, - Amount = 498, - Recipient = "802f34851bdfa1572f11fc4e58e44be3c01e92399bc4ba8d81e98b02254a0106" - + new BankTransaction { + Id = "2484fbf3-2054-48a4-a1ce-66333cd15470", + Block = new BankBlock { + Id = "6b9bc4b1-76a7-4105-951f-b729b4befdef", + Created = DateTime.Now.AddDays(-3), + Modified = DateTime.Now, + BalanceKey = "4abccf4280ef61aad6f176a103933a42ed7dbf90d55e7912f404a704ede06f41", + Sender = "4abccf4280ef61aad6f176a103933a42ed7dbf90d55e7912f404a704ede06f41", + Signature = + "c4a7696abc0f1e760a0c87d2a8db1faec060f99a77bf5f669a69dfdb301150db8fdaa2863a5d4a4e75be626ff56aecdbfea034283b1eb5a8e0196fd35f541b0b" + }, + Amount = 498, + Recipient = "802f34851bdfa1572f11fc4e58e44be3c01e92399bc4ba8d81e98b02254a0106" + } } }; var getAllResponse = new HttpResponseMessage(System.Net.HttpStatusCode.OK); - getAllResponse.Content = new StringContent(JsonConvert.SerializeObject(listResult), Encoding.UTF8, "application/json"); + getAllResponse.Content = + new StringContent(JsonConvert.SerializeObject(listResult), Encoding.UTF8, "application/json"); requestSender .Setup(x => x.GetAsync(It.IsAny())) @@ -80,7 +80,6 @@ public static TransactionsService BuildBankTransactionMock() var bankTransactionService = new TransactionsService(requestSender.Object); return bankTransactionService; - } } } \ No newline at end of file diff --git a/src/Thenewboston.Tests/Bank/Api/UpgradeNoticeTests.cs b/src/Thenewboston.Tests/Bank/Api/UpgradeNoticeTests.cs new file mode 100644 index 0000000..746013b --- /dev/null +++ b/src/Thenewboston.Tests/Bank/Api/UpgradeNoticeTests.cs @@ -0,0 +1,87 @@ +using System.Net.Http; +using System.Threading.Tasks; +using Moq; +using Newtonsoft.Json; +using Thenewboston.Bank.Api; +using Thenewboston.Bank.Models; +using Thenewboston.Common.Http; +using Xunit; + +namespace Thenewboston.Tests.Bank.Api +{ + public class UpgradeNoticeTests + { + // *************************************** + // Bank Upgrade Notice Tests * + // Created 11/27/2020 based on available * + // documentation * + // *************************************** + + #region Mock Test Models + + private UpgradeNotice CreateUpgradeNoticeModelMock() + { + return new UpgradeNotice() + { + Message = new UpgradeNoticeMessage() + { + BankNodeIdentifier = "d5356888dc9303e44ce52b1e06c3165a7759b9df1e6a6dfbd33ee1c3df1ab4d1" + }, + NodeIdentifier = "59479a31c3b91d96bb7a0b3e07f18d4bf301f1bb0bde05f8d36d9611dcbe7cbf", + Signature = "e9862cf176523449417b5f3426cb7bf0a3813ef04fae3330faa50b6468d9da9c55967a24c40040eaa6bc33843804bda9a0307bffe906ed8c7b2a55c15dabaa0d" + }; + } + + #endregion + + #region Mock Services + + public IUpgradeNoticeService BuildUpgradeNoticeServiceServerAcceptsMock() + { + var requestSender = new Mock(); + var serializedContent = new StringContent(JsonConvert.SerializeObject(CreateUpgradeNoticeModelMock())); + var response = new HttpResponseMessage(System.Net.HttpStatusCode.OK); + + requestSender.Setup(s => s.PostAsync(It.IsAny(), It.IsAny())) + .ReturnsAsync(response); + + return new UpgradeNoticeService(requestSender.Object); + } + + public IUpgradeNoticeService BuildUpgradeNoticeServiceServerRejectsMock() + { + var requestSender = new Mock(); + var serializedContent = new StringContent(JsonConvert.SerializeObject(CreateUpgradeNoticeModelMock())); + var response = new HttpResponseMessage(System.Net.HttpStatusCode.BadRequest); + + requestSender.Setup(s => s.PostAsync(It.IsAny(), It.IsAny())) + .ReturnsAsync(response); + + return new UpgradeNoticeService(requestSender.Object); + } + + #endregion + + #region Tests + + [Fact] + public async Task UpgradeNoticeBankAcceptsTest() + { + var service = BuildUpgradeNoticeServiceServerAcceptsMock(); + var result = await service.PostUpgradeNoticeAsync(CreateUpgradeNoticeModelMock()); + var expectedResult = new HttpResponseMessage(System.Net.HttpStatusCode.OK); + Assert.Equal(expectedResult.StatusCode, result.StatusCode); + } + + [Fact] + public async Task UpgradeNoticeBankRejectsTest() + { + var service = BuildUpgradeNoticeServiceServerRejectsMock(); + var result = await service.PostUpgradeNoticeAsync(CreateUpgradeNoticeModelMock()); + var expectedResult = new HttpResponseMessage(System.Net.HttpStatusCode.BadRequest); + Assert.Equal(expectedResult.StatusCode, result.StatusCode); + } + + #endregion + } +} diff --git a/src/Thenewboston.Tests/Bank/Api/UpgradeRequestTests.cs b/src/Thenewboston.Tests/Bank/Api/UpgradeRequestTests.cs new file mode 100644 index 0000000..ef1a1e0 --- /dev/null +++ b/src/Thenewboston.Tests/Bank/Api/UpgradeRequestTests.cs @@ -0,0 +1,88 @@ +using System.Net.Http; +using System.Threading.Tasks; +using Moq; +using Thenewboston.Common.Http; +using Thenewboston.Validator.Api; +using Thenewboston.Validator.Api.Models; +using Thenewboston.Validator.Models; +using Xunit; + +namespace Thenewboston.Tests.Bank.Api +{ + public class UpgradeRequestTests + { + + // *************************************** + // Validator Upgrade Request Tests * + // Created 11/27/2020 based on available * + // documentation * + // *************************************** + + #region Upgrade Request Mock Models + + public UpgradeRequest CreateMockUpgradeRequest() + { + return new UpgradeRequest() + { + Message = new UpgradeRequestMessage() + { + ValidatorNodeIdentifier = "59479a31c3b91d96bb7a0b3e07f18d4bf301f1bb0bde05f8d36d9611dcbe7cbf" + }, + NodeIdentifier = "d5356888dc9303e44ce52b1e06c3165a7759b9df1e6a6dfbd33ee1c3df1ab4d1", + Signature = "90a365d1950b1765b973d3d21d763f9bea7eb1f9d1f33d6e9c3f8eb4803022f97ad3173474707b4786e556cccf4ca0a0e81d18edb655b090967c96c22c40140a" + }; + } + + #endregion + + #region Upgrade Request Mock Services + + public IUpgradeRequestService BuildUpgradeRequestServiceNodeAcceptsMock() + { + var requestSender = new Mock(); + var httpResponse = new HttpResponseMessage(System.Net.HttpStatusCode.OK); + + requestSender.Setup(s => s.PostAsync(It.IsAny(), It.IsAny())) + .ReturnsAsync(httpResponse); + + var service = new UpgradeRequestService(requestSender.Object); + return service; + } + + public IUpgradeRequestService BuildUpgradeRequestServiceNodeRejectsMock() + { + var requestSender = new Mock(); + var httpResponse = new HttpResponseMessage(System.Net.HttpStatusCode.BadRequest); + + requestSender.Setup(s => s.PostAsync(It.IsAny(), It.IsAny())) + .ReturnsAsync(httpResponse); + + var service = new UpgradeRequestService(requestSender.Object); + return service; + } + + #endregion + + #region Tests + + [Fact] + public async Task UpgradeRequestNodeAcceptsTest() + { + var service = BuildUpgradeRequestServiceNodeAcceptsMock(); + var result = await service.PostUpgradeRequestAsync(CreateMockUpgradeRequest()); + var expectedResult = new HttpResponseMessage(System.Net.HttpStatusCode.OK); + Assert.Equal(expectedResult.StatusCode, result.StatusCode); + } + + [Fact] + public async Task UpgradeRequestNodeRejectsTest() + { + var service = BuildUpgradeRequestServiceNodeRejectsMock(); + var result = await service.PostUpgradeRequestAsync(CreateMockUpgradeRequest()); + var expectedResult = new HttpResponseMessage(System.Net.HttpStatusCode.BadRequest); + Assert.Equal(expectedResult.StatusCode, result.StatusCode); + } + + #endregion + } +} diff --git a/src/Thenewboston.Tests/Bank/Api/ValidatorConfirmationServiceTests.cs b/src/Thenewboston.Tests/Bank/Api/ValidatorConfirmationServiceTests.cs new file mode 100644 index 0000000..548c1c2 --- /dev/null +++ b/src/Thenewboston.Tests/Bank/Api/ValidatorConfirmationServiceTests.cs @@ -0,0 +1,106 @@ +using System; +using System.Collections.Generic; +using System.Net.Http; +using System.Text; +using Moq; +using Newtonsoft.Json; +using Thenewboston.Bank.Api; +using Thenewboston.Bank.Api.Models; +using Thenewboston.Common.Api.Models; +using Thenewboston.Common.Http; +using Thenewboston.Common.Models; +using Xunit; + +namespace Thenewboston.Tests.Bank.Api +{ + public class ValidatorConfirmationServiceTests + { + #region Test Models + + private PaginatedResponseModel CreateMockResponse() + { + return new PaginatedResponseModel() { + Count = 1, + Next = string.Empty, + Previous = string.Empty, + Results = + new List() { + new Thenewboston.Bank.Api.Models.ValidatorConfirmation() { + Id = "be9fbc3b-d4df-43d5-9bea-9882a6dd27f6", + Created = DateTime.Parse("2020-10-08T02:18:07.324999Z"), + Modified = DateTime.Parse("2020-10-08T02:18:07.325044Z"), + End = DateTime.Parse("2020-08-09T22:10:24Z"), + Start = DateTime.Parse("2020-07-09T22:10:25Z"), + Validator = "51461a75-dd8d-4133-81f4-543a3b054149" + } + } + }; + } + + private BankValidatorConfirmationService CreateMockValidatorConfirmationServiceMessage() + { + return new BankValidatorConfirmationService() { + Message = new ValidatorConfirmationServiceMessage() { + End = DateTime.Parse("2020-07-09T22:10:25Z"), Start = DateTime.Parse("2020-08-09T22:10:25Z") + }, + NodeIdentifier = "59479a31c3b91d96bb7a0b3e07f18d4bf301f1bb0bde05f8d36d9611dcbe7cbf", + Signature = + "2a4b90e97566d4c46cb302e8297841ebe0b9f5ce6f30217721dedb4bfdc48944d14f46032e33246b6a60a942bc48fd9541057b7b1c635d4346436deab9f4bf01" + }; + } + + #endregion + + #region Mock Service + + private IValidatorConfirmationService BuildValidatorConfirmationServiceGetMock() + { + var response = new HttpResponseMessage(System.Net.HttpStatusCode.OK); + response.Content = new StringContent(JsonConvert.SerializeObject(CreateMockResponse()), Encoding.UTF8, + "application/json"); + var requestSenderMock = new Mock(); + + requestSenderMock + .Setup(s => s.GetAsync(It.IsAny())) + .ReturnsAsync(response); + + IValidatorConfirmationService service = new Thenewboston.Bank.Api.ValidatorConfirmationService(requestSenderMock.Object); + return service; + } + + private IValidatorConfirmationService BuildValidatorConfirmationServicePostMock() + { + var requestSenderMock = new Mock(); + requestSenderMock.Setup(s => s.PostAsync(It.IsAny(), It.IsAny())) + .ReturnsAsync(new HttpResponseMessage(System.Net.HttpStatusCode.Created)); + + IValidatorConfirmationService service = new Thenewboston.Bank.Api.ValidatorConfirmationService(requestSenderMock.Object); + return service; + } + + #endregion + + #region Tests + + [Fact] + public async void ConfirmationServiceReturnedAsync() + { + var service = BuildValidatorConfirmationServiceGetMock(); + var returnedBlock = await service.GetValidatorConfirmationServicesAsync(0, 10); + var expectedResult = JsonConvert.SerializeObject(CreateMockResponse()); + var actualResult = JsonConvert.SerializeObject(returnedBlock); + Assert.Equal(expectedResult, actualResult); + } + + [Fact] + public async void BlockPostedAsync() + { + var service = BuildValidatorConfirmationServicePostMock(); + var response = + await service.PostValidatorConfirmationServiceAsync(CreateMockValidatorConfirmationServiceMessage()); + Assert.True(response.StatusCode == System.Net.HttpStatusCode.Created); + } + + #endregion + } +} \ No newline at end of file diff --git a/src/Thenewboston.Tests/Bank/Api/ValidatorServiceTests.cs b/src/Thenewboston.Tests/Bank/Api/ValidatorServiceTests.cs index 81a9173..b937c77 100644 --- a/src/Thenewboston.Tests/Bank/Api/ValidatorServiceTests.cs +++ b/src/Thenewboston.Tests/Bank/Api/ValidatorServiceTests.cs @@ -24,7 +24,7 @@ public class GetAllValidatorsAsync [Fact] public async void ListOfValidatorsIsReturned() { - PaginatedResponseModel expectedResponseModel = new PaginatedResponseModel + var expectedResponseModel = new PaginatedResponseModel { Count = 2, Next = null, @@ -67,7 +67,7 @@ public async void ListOfValidatorsIsReturned() var service = BuildBankValidatorMock(expectedResponseModel); - var validators = await service.GetAllValidatorsAsync(); + var validators = await service.GetAllValidatorsAsync(0, 10); var expectedResponseModelStr = JsonConvert.SerializeObject(expectedResponseModel); var actualResponseModelStr = JsonConvert.SerializeObject(validators); @@ -116,7 +116,7 @@ public async void ValidatorIsPatched() } } - public static IValidatorService BuildBankValidatorMock(PaginatedResponseModel expectedResponseModel) + public static IValidatorService BuildBankValidatorMock(PaginatedResponseModel expectedResponseModel) { var requestSender = new Mock(); diff --git a/src/Thenewboston.Tests/Validator/Api/AccountsServiceTests.cs b/src/Thenewboston.Tests/Validator/Api/AccountsServiceTests.cs index b00e5e1..34a8bec 100644 --- a/src/Thenewboston.Tests/Validator/Api/AccountsServiceTests.cs +++ b/src/Thenewboston.Tests/Validator/Api/AccountsServiceTests.cs @@ -6,6 +6,7 @@ using System.Threading.Tasks; using Moq; using Newtonsoft.Json; +using Thenewboston.Common.Api.Models; using Thenewboston.Common.Http; using Thenewboston.Validator.Api; using Thenewboston.Validator.Models; @@ -24,8 +25,8 @@ public async void ListOfAccountsIsReturned() var accounts = await service.GetAccountsAsync(); - Assert.Equal(2, accounts.Count()); - Assert.Equal("9eca00a5-d925-454c-a8d6-ecbb26ec2f76", accounts.ElementAt(0).Id); + Assert.Equal(2, accounts.Count); + Assert.Equal("9eca00a5-d925-454c-a8d6-ecbb26ec2f76", accounts.Results.ElementAt(0).Id); } } @@ -59,8 +60,12 @@ public static AccountsService BuildValidatorServiceMock() { var requestSender = new Mock(); - var listResult = new List + var listResult = new PaginatedResponseModel() { + Count = 2, + Next = string.Empty, + Previous = string.Empty, + Results = new List(){ new ValidatorAccount { Id = "9eca00a5-d925-454c-a8d6-ecbb26ec2f76", @@ -75,6 +80,7 @@ public static AccountsService BuildValidatorServiceMock() Balance = "175.0000000000000000", BalanceLock = "484b3176c63d5f37d808404af1a12c4b9649cd6f6769f35bdf5a816133623fbc" } + } }; var getAllResponse = new HttpResponseMessage(System.Net.HttpStatusCode.OK); @@ -84,7 +90,7 @@ public static AccountsService BuildValidatorServiceMock() getBalanceResponse.Content = new StringContent(JsonConvert.SerializeObject( new ValidatorAccountBalance { - Balance = listResult[0].Balance + Balance = listResult.Results.ElementAt(0).Balance }), Encoding.UTF8, "application/json"); @@ -93,7 +99,7 @@ public static AccountsService BuildValidatorServiceMock() getBalanceLockResponse.Content = new StringContent(JsonConvert.SerializeObject( new ValidatorAccountBalanceLock { - BalanceLock = listResult[0].BalanceLock + BalanceLock = listResult.Results.ElementAt(0).BalanceLock }), Encoding.UTF8, "application/json"); diff --git a/src/Thenewboston.Tests/Validator/Api/BankBlockServiceTest.cs b/src/Thenewboston.Tests/Validator/Api/BankBlockServiceTest.cs new file mode 100644 index 0000000..3a33f10 --- /dev/null +++ b/src/Thenewboston.Tests/Validator/Api/BankBlockServiceTest.cs @@ -0,0 +1,77 @@ +using System.Collections.Generic; +using System.Net.Http; +using Moq; +using Thenewboston.Common.Http; +using Thenewboston.Common.Models; +using Thenewboston.Validator.Api; +using Thenewboston.Validator.Models; +using Xunit; + +namespace Thenewboston.Tests.Validator.Api +{ + public class BankBlockServiceTest + { + #region Test Models + private ValidatorBankBlock CreateMockBankBlock() + { + return new ValidatorBankBlock() + { + BankBlock = new Block() + { + AccountNumber = "0cdd4ba04456ca169baca3d66eace869520c62fe84421329086e03d91a68acdb", + Message = new BlockMessage() + { + BalanceKey = "ee7a6d21feb2905605f9af446566e003decec3de2f55a6eff9815d41fcde59e0", + Transactions = new List() + { + new BlockTransaction() { + Amount = "4.125", + Recipient = "484b3176c63d5f37d808404af1a12c4b9649cd6f6769f35bdf5a816133623fbc" + }, + new BlockTransaction() { + Amount = "1", + Recipient = "5e12967707909e62b2bb2036c209085a784fabbc3deccefee70052b6181c8ed8" + }, + new BlockTransaction() { + Amount = "4", + Recipient = "ad1f8845c6a1abb6011a2a434a079a087c460657aad54329a84b406dce8bf314" + } + } + }, + Signature = "e8b5215193cd5f91029b2a7a9ff2426b0a9fc0032f5a0f74ec0e6cc2f3ac9f20acd170f90e1557e561c85d34daa37d0cec90901f3d4c9579700847f67de22a05" + }, + NodeIdentifier = "d5356888dc9303e44ce52b1e06c3165a7759b9df1e6a6dfbd33ee1c3df1ab4d1", + Signature = "4f019d36d362f09960399fe51dd67d7ddbee27d73994558cf4015bb13260957f861afaa1694d40fd0397ed2a889834d8a20bff4c3417bbde383e2cd4e219cb0f" + }; + } + + #endregion + + #region Mock Service + + private IBankBlockService BuildBankBlockPostMock() + { + var requestSenderMock = new Mock(); + + requestSenderMock.Setup(s => s.PostAsync(It.IsAny(), It.IsAny())) + .ReturnsAsync(new HttpResponseMessage(System.Net.HttpStatusCode.Created)); + + IBankBlockService service = new BankBlockService(requestSenderMock.Object); + return service; + } + + #endregion + + #region Tests + + [Fact] + public async void BankConfirmationBlockMessagePostedAsync() + { + var service = BuildBankBlockPostMock(); + var response = await service.PostBankBlockAsync(CreateMockBankBlock()); + Assert.True(response.StatusCode == System.Net.HttpStatusCode.Created); + } + + #endregion + } +} \ No newline at end of file diff --git a/src/Thenewboston.Tests/Validator/Api/BankConfirmationServiceTests.cs b/src/Thenewboston.Tests/Validator/Api/BankConfirmationServiceTests.cs new file mode 100644 index 0000000..156a4dd --- /dev/null +++ b/src/Thenewboston.Tests/Validator/Api/BankConfirmationServiceTests.cs @@ -0,0 +1,74 @@ +using System; +using System.Collections.Generic; +using System.Net.Http; +using System.Text; +using Moq; +using Newtonsoft.Json; +using Thenewboston.Common.Api.Models; +using Thenewboston.Common.Http; +using Thenewboston.Validator.Api; +using Thenewboston.Validator.Api.Models; +using Xunit; + +namespace Thenewboston.Tests.Validator.Api +{ + public class BankConfirmationServiceTests + { + #region Test Models + + private PaginatedResponseModel CreateMockResponse() + { + return new PaginatedResponseModel() { + Count = 1, + Next = string.Empty, + Previous = string.Empty, + Results = + new List() { + new BankConfirmationServiceResponse() { + Id = "09e96a28-4d71-4123-85a3-882a9bdad114", + Created = DateTime.Parse("2020-09-11T02:15:13.638227Z"), + Modified = DateTime.Parse("2020-09-11T02:15:13.638326Z"), + End = DateTime.Parse("2020-09-23T00:06:55.320993Z"), + Start = DateTime.Parse("2020-09-20T00:06:55.320993Z"), + Bank = "b58b4b8a-d8f9-4395-a0de-f9df150bb093" + } + } + }; + } + + #endregion + + #region Mock Service + + private IBankConfirmationService BuildBankConfirmationServiceGetMock() + { + var response = new HttpResponseMessage(System.Net.HttpStatusCode.OK); + response.Content = new StringContent(JsonConvert.SerializeObject(CreateMockResponse()), Encoding.UTF8, + "application/json"); + var requestSenderMock = new Mock(); + + requestSenderMock + .Setup(s => s.GetAsync(It.IsAny())) + .ReturnsAsync(response); + + IBankConfirmationService service = new BankConfirmationService(requestSenderMock.Object); + return service; + } + + #endregion + + #region Tests + + [Fact] + public async void ConfirmationServiceReturnedAsync() + { + var service = BuildBankConfirmationServiceGetMock(); + var returnedBlock = await service.GetBankConfirmationServicesAsync(0, 10); + var expectedResult = JsonConvert.SerializeObject(CreateMockResponse()); + var actualResult = JsonConvert.SerializeObject(returnedBlock); + Assert.Equal(expectedResult, actualResult); + } + + #endregion + } +} \ No newline at end of file diff --git a/src/Thenewboston.Tests/Validator/Api/ConnectedBanksServiceTests.cs b/src/Thenewboston.Tests/Validator/Api/ConnectedBanksServiceTests.cs index 906212d..c91a3b5 100644 --- a/src/Thenewboston.Tests/Validator/Api/ConnectedBanksServiceTests.cs +++ b/src/Thenewboston.Tests/Validator/Api/ConnectedBanksServiceTests.cs @@ -4,6 +4,7 @@ using System.Text; using Moq; using Newtonsoft.Json; +using Thenewboston.Bank.Models; using Thenewboston.Common.Api.Models; using Thenewboston.Common.Http; using Thenewboston.Validator.Api; @@ -19,7 +20,7 @@ public class GetBanksAsync [Fact] public async void ListOfBanksIsReturned() { - PaginatedResponseModel expectedResponseModel = new PaginatedResponseModel + var expectedResponseModel = new PaginatedResponseModel { Count = 2, Next = null, @@ -56,7 +57,7 @@ public async void ListOfBanksIsReturned() var service = BuildGetBanksAsyncValidatorServiceMock(expectedResponseModel); - var banks = await service.GetBanksAsync(); + var banks = await service.GetBanksAsync(0, 10); var expectedResponseModelStr = JsonConvert.SerializeObject(expectedResponseModel); var actualResponseModelStr = JsonConvert.SerializeObject(banks); @@ -64,7 +65,7 @@ public async void ListOfBanksIsReturned() } } - public static IConnectedBanksService BuildGetBanksAsyncValidatorServiceMock(PaginatedResponseModel expectedResponseModel) + public static IConnectedBanksService BuildGetBanksAsyncValidatorServiceMock(PaginatedResponseModel expectedResponseModel) { var response = new HttpResponseMessage(HttpStatusCode.OK); response.Content = new StringContent(JsonConvert.SerializeObject(expectedResponseModel), Encoding.UTF8, "application/json"); @@ -73,7 +74,7 @@ public static IConnectedBanksService BuildGetBanksAsyncValidatorServiceMock(Pagi IConnectedBanksService service = new ConnectedBanksService(requestSenderMock.Object); requestSenderMock - .Setup(x => x.GetAsync("/banks")) + .Setup(x => x.GetAsync(It.IsAny())) .ReturnsAsync(response); return service; diff --git a/src/Thenewboston.Tests/Validator/Api/ConnectionRequestServiceTest.cs b/src/Thenewboston.Tests/Validator/Api/ConnectionRequestServiceTest.cs new file mode 100644 index 0000000..630771d --- /dev/null +++ b/src/Thenewboston.Tests/Validator/Api/ConnectionRequestServiceTest.cs @@ -0,0 +1,73 @@ +using System; +using System.Collections.Generic; +using System.Net.Http; +using System.Text; +using System.Threading.Tasks; +using Moq; +using Thenewboston.Common.Http; +using Thenewboston.Validator.Api; +using Thenewboston.Validator.Models; +using Xunit; + +namespace Thenewboston.Tests.Validator.Api +{ + public class ConnectionRequestServiceTest + { + public ConnectionRequest CreateMockConnectionRequest() + { + return new ConnectionRequest() + { + Message = new ConnectionRequestMessage() + { + IpAddress = "192.168.1.232", + Port = "8000", + Protocol = "http" + }, + NodeIdentifier = "d5356888dc9303e44ce52b1e06c3165a7759b9df1e6a6dfbd33ee1c3df1ab4d1", + Signature = "3c88665e123e7e25a8b9d9592f3269ab4efc4bcba989a103a898e2625933261b1cccdaf2f52eca9c58d2bf033968ab6b702089bca8fc6e0c80b3b002a5e05b03" + }; + } + + public IConnectionRequestService ConnectionRequestAcceptsMock() + { + var requestSender = new Mock(); + var httpResponse = new HttpResponseMessage(System.Net.HttpStatusCode.OK); + + requestSender.Setup(s => s.PostAsync(It.IsAny(), It.IsAny())) + .ReturnsAsync(httpResponse); + + var service = new ConnectionRequestService(requestSender.Object); + return service; + } + + public IConnectionRequestService ConnectionRequestRejectsMock() + { + var requestSender = new Mock(); + var httpResponse = new HttpResponseMessage(System.Net.HttpStatusCode.BadRequest); + + requestSender.Setup(s => s.PostAsync(It.IsAny(), It.IsAny())) + .ReturnsAsync(httpResponse); + + var service = new ConnectionRequestService(requestSender.Object); + return service; + } + + [Fact] + public async Task UpgradeRequestNodeAcceptsTest() + { + var service = ConnectionRequestAcceptsMock(); + var result = await service.PostConnectionRequestAsync(CreateMockConnectionRequest()); + var expectedResult = new HttpResponseMessage(System.Net.HttpStatusCode.OK); + Assert.Equal(expectedResult.StatusCode, result.StatusCode); + } + + [Fact] + public async Task UpgradeRequestNodeRejectsTest() + { + var service = ConnectionRequestRejectsMock(); + var result = await service.PostConnectionRequestAsync(CreateMockConnectionRequest()); + var expectedResult = new HttpResponseMessage(System.Net.HttpStatusCode.BadRequest); + Assert.Equal(expectedResult.StatusCode, result.StatusCode); + } + } +} diff --git a/src/Thenewboston.Tests/Validator/Api/PrimaryValidatorUpdatedTests.cs b/src/Thenewboston.Tests/Validator/Api/PrimaryValidatorUpdatedTests.cs new file mode 100644 index 0000000..38b0fd4 --- /dev/null +++ b/src/Thenewboston.Tests/Validator/Api/PrimaryValidatorUpdatedTests.cs @@ -0,0 +1,89 @@ +using System.Net.Http; +using System.Threading.Tasks; +using Moq; +using Thenewboston.Common.Http; +using Thenewboston.Validator.Api; +using Thenewboston.Validator.Models; +using Xunit; + +namespace Thenewboston.Tests.Validator.Api +{ + public class PrimaryValidatorUpdatedTests + { + // ******************************************** + // Primary Validator Updated Tests * + // Created 11/28/2020 based on available * + // documentation * + // ******************************************** + + + #region Primary Validator Updated Mock Models + + private PrimaryValidatorUpdatedModel CreateMockPrimaryValidatorUpdatedModel() + { + return new PrimaryValidatorUpdatedModel() + { + Message = new PrimaryValidatorUpdatedMessage() + { + IPAddress = "192.168.1.20", + Port = "8000", + Protocol = "http" + }, + NodeIdentifier = "d5356888dc9303e44ce52b1e06c3165a7759b9df1e6a6dfbd33ee1c3df1ab4d1", + Signature = "ccb4c9f0669abfcd1cea330fa4e45d87689fb08d2fd26b4cf05c87b2fdb1dae543024888243a84d02fd9bb8320c41f4de45d6212747e49a02a365fd42883fd01" + }; + } + + #endregion + + #region Primary Validator Updated Mock Services + + public IPrimaryValidatorUpdatedService BuildMockPrimaryValidatorUpdatesServiceApproveUpgrade() + { + var httpRequest = new Mock(); + var returnContent = new HttpResponseMessage(System.Net.HttpStatusCode.OK); + var service = new PrimaryValidatorUpdatedService(httpRequest.Object); + + httpRequest.Setup(s => s.PostAsync(It.IsAny(), It.IsAny())) + .ReturnsAsync(returnContent); + + return service; + } + + public IPrimaryValidatorUpdatedService BuildMockPrimaryValidatorUpdatesServiceRejectUpgrade() + { + var httpRequest = new Mock(); + var returnContent = new HttpResponseMessage(System.Net.HttpStatusCode.BadRequest); + var service = new PrimaryValidatorUpdatedService(httpRequest.Object); + + httpRequest.Setup(s => s.PostAsync(It.IsAny(), It.IsAny())) + .ReturnsAsync(returnContent); + + return service; + } + + #endregion + + #region Tests + + [Fact] + public async Task PrimaryValidatorUpdatedReceivingNodeApprovesTest() + { + var service = BuildMockPrimaryValidatorUpdatesServiceApproveUpgrade(); + var expectedResult = new HttpResponseMessage(System.Net.HttpStatusCode.OK); + var result = await service.PostPrimaryValidatorUpdatedAsync(CreateMockPrimaryValidatorUpdatedModel()); + Assert.Equal(expectedResult.StatusCode, result.StatusCode); + } + + [Fact] + public async Task PrimaryValidatorUpdatedReceivingNodeRejectsTest() + { + var service = BuildMockPrimaryValidatorUpdatesServiceRejectUpgrade(); + var expectedResult = new HttpResponseMessage(System.Net.HttpStatusCode.BadRequest); + var result = await service.PostPrimaryValidatorUpdatedAsync(CreateMockPrimaryValidatorUpdatedModel()); + Assert.Equal(expectedResult.StatusCode, result.StatusCode); + } + + #endregion + } +} diff --git a/src/Thenewboston.Tests/Validator/Api/ValidatorConfirmationBlockTests.cs b/src/Thenewboston.Tests/Validator/Api/ValidatorConfirmationBlockTests.cs index 4fe52a8..ee53e38 100644 --- a/src/Thenewboston.Tests/Validator/Api/ValidatorConfirmationBlockTests.cs +++ b/src/Thenewboston.Tests/Validator/Api/ValidatorConfirmationBlockTests.cs @@ -184,7 +184,7 @@ private IValidatorConfirmationBlockService BuildValidatorConfirmationBlockServic public async Task TestConfirmationBlockPostNew() { var service = BuildValidatorConfirmationBlockServicePostMock(); - var result = await service.PostConfiramtionBlockAsync(CreateMockConfirmationBlock()); + var result = await service.PostConfirmationBlockAsync(CreateMockConfirmationBlock()); var expectedResult = CreateMockConfirmationBlockResponse(); Assert.Equal(expectedResult.BlockIdentifier, result.BlockIdentifier); Assert.Equal(expectedResult.Block.Signature, result.Block.Signature); @@ -197,7 +197,7 @@ public async Task TestConfirmationBlockPostNew() public async Task TestGetQueuedConfirmationBlockByID() { var service = BuildValidatorConfirmationBlockServiceGetMock(); - var result = await service.GetQueuedConfiramtionBlockAsync(string.Empty); + var result = await service.GetQueuedConfirmationBlockAsync(string.Empty); var expectedResult = CreateMockConfirmationBlock(); Assert.Equal(expectedResult.BlockIdentifier, result.BlockIdentifier); Assert.Equal(expectedResult.NodeIdentifier, result.NodeIdentifier); diff --git a/src/Thenewboston.Tests/Validator/Api/ValidatorsServiceTests.cs b/src/Thenewboston.Tests/Validator/Api/ValidatorsServiceTests.cs index a17c22e..3f080d8 100644 --- a/src/Thenewboston.Tests/Validator/Api/ValidatorsServiceTests.cs +++ b/src/Thenewboston.Tests/Validator/Api/ValidatorsServiceTests.cs @@ -10,6 +10,7 @@ using Thenewboston.Common.Http; using Thenewboston.Common.Models; using Thenewboston.Validator.Api; +using Thenewboston.Validator.Api.Models; using Thenewboston.Validator.Models; using Xunit; @@ -23,7 +24,7 @@ public class GetAllValidatorsAsync [Fact] public async void ListOfValidatorsIsReturned() { - PaginatedResponseModel expectedResponseModel = new PaginatedResponseModel + var expectedResponseModel = new PaginatedResponseModel { Count = 2, Next = null, @@ -66,7 +67,7 @@ public async void ListOfValidatorsIsReturned() var service = BuildBankValidatorMock(expectedResponseModel); - var validators = await service.GetAllValidatorsAsync(); + var validators = await service.GetAllValidatorsAsync(0, 10); var expectedResponseModelStr = JsonConvert.SerializeObject(expectedResponseModel); var actualResponseModelStr = JsonConvert.SerializeObject(validators); @@ -75,7 +76,7 @@ public async void ListOfValidatorsIsReturned() } } - public static IValidatorsService BuildBankValidatorMock(PaginatedResponseModel expectedResponseModel) + public static IValidatorsService BuildBankValidatorMock(PaginatedResponseModel expectedResponseModel) { var requestSender = new Mock(); diff --git a/src/Thenewboston/Bank/Api/AccountsService.cs b/src/Thenewboston/Bank/Api/AccountsService.cs index cbc19b6..284869e 100644 --- a/src/Thenewboston/Bank/Api/AccountsService.cs +++ b/src/Thenewboston/Bank/Api/AccountsService.cs @@ -1,5 +1,4 @@ using System; -using System.Collections; using System.Collections.Generic; using System.Net.Http; using System.Text; @@ -7,6 +6,7 @@ using Newtonsoft.Json; using Thenewboston.Bank.Api.Models; using Thenewboston.Bank.Models; +using Thenewboston.Common.Api.Models; using Thenewboston.Common.Http; namespace Thenewboston.Bank.Api @@ -20,9 +20,9 @@ public AccountsService(IHttpRequestSender requestSender) _requestSender = requestSender; } - public async Task> GetAccountsAsync() + public async Task> GetAccountsAsync(int offset = 0, int limit = 10) { - var response = await _requestSender.GetAsync("/accounts"); + var response = await _requestSender.GetAsync($"/accounts?offset={offset}&limit={limit}"); if (!response.IsSuccessStatusCode) { @@ -38,7 +38,7 @@ public async Task> GetAccountsAsync() throw new Exception(); } - var result = JsonConvert.DeserializeObject>(stringResult); + var result = JsonConvert.DeserializeObject>(stringResult); return result; } diff --git a/src/Thenewboston/Bank/Api/BankConfirmationBlockService.cs b/src/Thenewboston/Bank/Api/BankConfirmationBlockService.cs index 8769d57..b3739e0 100644 --- a/src/Thenewboston/Bank/Api/BankConfirmationBlockService.cs +++ b/src/Thenewboston/Bank/Api/BankConfirmationBlockService.cs @@ -3,6 +3,7 @@ using System.Text; using System.Threading.Tasks; using Newtonsoft.Json; +using Thenewboston.Bank.Api.Models; using Thenewboston.Common.Api.Models; using Thenewboston.Common.Http; using Thenewboston.Common.Models; @@ -22,9 +23,10 @@ public BankConfirmationBlockService(IHttpRequestSender requestSender) /// Retrieves confirmation blocks from the connected bank via paginated /// /// containing all bank to client confirmation blocks - public async Task GetAllBankConfiramtionBlocksAsync() + public async Task> GetAllBankConfiramtionBlocksAsync( + int offset = 0, int limit = 10) { - var response = await _requestSender.GetAsync("/confirmation_blocks"); + var response = await _requestSender.GetAsync($"/confirmation_blocks?offset={offset}&limit={limit}"); if(!response.IsSuccessStatusCode) { @@ -40,11 +42,12 @@ public async Task GetAllBankConfiramtionBlocksAsync() throw new Exception(); } - var result = JsonConvert.DeserializeObject(stringResponse); + var result = JsonConvert.DeserializeObject>(stringResponse); return result; } + /// NOTE: Not implemented in the app layer as this is functionality for the validator, therefore not needed in any apps. /// /// Provides a facility for a confirmation validator to post a new to the /// receiving bank. diff --git a/src/Thenewboston/Bank/Api/BlocksService.cs b/src/Thenewboston/Bank/Api/BlocksService.cs new file mode 100644 index 0000000..9ada805 --- /dev/null +++ b/src/Thenewboston/Bank/Api/BlocksService.cs @@ -0,0 +1,63 @@ +using System; +using System.ComponentModel; +using System.Net.Http; +using System.Text; +using System.Threading.Tasks; +using Newtonsoft.Json; +using Thenewboston.Bank.Api.Models; +using Thenewboston.Bank.Models; +using Thenewboston.Common.Api.Models; +using Thenewboston.Common.Http; + +namespace Thenewboston.Bank.Api +{ + public class BlocksService : IBlocksService + { + private IHttpRequestSender _requestSender; + + public BlocksService(IHttpRequestSender requestSender) + { + _requestSender = requestSender; + } + + public async Task> GetBlocksAsync(int offset = 0, int limit = 10) + { + var response = await _requestSender.GetAsync($"/blocks?offset={offset}&limit={limit}"); + + if (!response.IsSuccessStatusCode) + { + //TODO: create specific exception + throw new Exception(); + } + + var stringResult = await response.Content.ReadAsStringAsync(); + + if (string.IsNullOrEmpty(stringResult)) + { + //TODO: create specific exception + throw new Exception(); + } + + var result = JsonConvert.DeserializeObject>(stringResult); + + return result; + } + + public async Task PostBlocksAsync(Thenewboston.Common.Models.Block block) + { + var httpContent = new StringContent(JsonConvert.SerializeObject(block), Encoding.UTF8, "application/json"); + var request = await _requestSender.PostAsync("/blocks", httpContent); + + if(!request.IsSuccessStatusCode) + { + // TODO: Create specific exception + throw new Exception(); + } + + var response = await request.Content.ReadAsStringAsync(); + var responseContent = JsonConvert.DeserializeObject(response); + + return responseContent; + } + } +} \ No newline at end of file diff --git a/src/Thenewboston/Bank/Api/ConnectedBanksService.cs b/src/Thenewboston/Bank/Api/ConnectedBanksService.cs index 4367fdf..6f90e0b 100644 --- a/src/Thenewboston/Bank/Api/ConnectedBanksService.cs +++ b/src/Thenewboston/Bank/Api/ConnectedBanksService.cs @@ -4,7 +4,6 @@ using System.Threading.Tasks; using Newtonsoft.Json; using Thenewboston.Bank.Api.Models; -using Thenewboston.Bank.Models; using Thenewboston.Common.Api.Models; using Thenewboston.Common.Http; @@ -19,9 +18,9 @@ public ConnectedBanksService(IHttpRequestSender requestSender) _requestSender = requestSender; } - public async Task GetBanksAsync() + public async Task> GetBanksAsync(int offset=0, int limit=10) { - var response = await _requestSender.GetAsync("/banks"); + var response = await _requestSender.GetAsync($"/banks?offset={offset}&limit={limit}"); if (!response.IsSuccessStatusCode) { @@ -37,12 +36,12 @@ public async Task GetBanksAsync() throw new Exception(); } - var result = JsonConvert.DeserializeObject(stringResult); + var result = JsonConvert.DeserializeObject>(stringResult); return result; } - public async Task UpdateBankAsync(string nodeIdentifier, RequestModel payload) + public async Task UpdateBankAsync(string nodeIdentifier, RequestModel payload) { var jsonPayload = JsonConvert.SerializeObject(payload); var httpContent = new StringContent(jsonPayload, Encoding.UTF8, "application/json"); @@ -63,7 +62,7 @@ public async Task UpdateBankAsync(string nodeIdentifier, Requ throw new Exception(); } - var result = JsonConvert.DeserializeObject(stringResult); + var result = JsonConvert.DeserializeObject(stringResult); return result; } diff --git a/src/Thenewboston/Bank/Api/ConnectionRequestService.cs b/src/Thenewboston/Bank/Api/ConnectionRequestService.cs new file mode 100644 index 0000000..a5e5c83 --- /dev/null +++ b/src/Thenewboston/Bank/Api/ConnectionRequestService.cs @@ -0,0 +1,42 @@ +using System; +using System.Collections.Generic; +using System.Net.Http; +using System.Text; +using System.Threading.Tasks; +using Newtonsoft.Json; +using Thenewboston.Bank.Models; +using Thenewboston.Common.Http; + +namespace Thenewboston.Bank.Api +{ + public class ConnectionRequestService : IConnectionRequestService + { + IHttpRequestSender _requestSender; + + public ConnectionRequestService(IHttpRequestSender requestSender) + { + _requestSender = requestSender; + } + + public async Task PostConnectionRequestAsync(ConnectionRequest connectionNotice) + { + if( connectionNotice is null ) + { + // TODO: Create specific exception + throw new Exception(); + } + + var httpContent = new StringContent(JsonConvert.SerializeObject(connectionNotice)); + var jsonString = await httpContent.ReadAsStringAsync(); + + if (string.IsNullOrEmpty(jsonString)) + { + // TODO: Create specific exception + throw new Exception(); + } + + var result = await _requestSender.PostAsync("/connection_requests", httpContent); + return result; + } + } +} diff --git a/src/Thenewboston/Bank/Api/IAccountsService.cs b/src/Thenewboston/Bank/Api/IAccountsService.cs index c2055bf..6409124 100644 --- a/src/Thenewboston/Bank/Api/IAccountsService.cs +++ b/src/Thenewboston/Bank/Api/IAccountsService.cs @@ -1,14 +1,14 @@ -using System; -using System.Collections.Generic; +using System.Collections.Generic; using System.Threading.Tasks; using Thenewboston.Bank.Api.Models; using Thenewboston.Bank.Models; +using Thenewboston.Common.Api.Models; namespace Thenewboston.Bank.Api { public interface IAccountsService { - Task> GetAccountsAsync(); + Task> GetAccountsAsync(int offset, int limit); Task UpdateAccountAsync(string accountNumber, RequestModel account); } } diff --git a/src/Thenewboston/Bank/Api/IBankConfirmationBlockService.cs b/src/Thenewboston/Bank/Api/IBankConfirmationBlockService.cs index 5cb2f86..28f0c0f 100644 --- a/src/Thenewboston/Bank/Api/IBankConfirmationBlockService.cs +++ b/src/Thenewboston/Bank/Api/IBankConfirmationBlockService.cs @@ -1,5 +1,6 @@ using System.Net.Http; using System.Threading.Tasks; +using Thenewboston.Bank.Api.Models; using Thenewboston.Common.Api.Models; using Thenewboston.Common.Models; @@ -7,7 +8,7 @@ namespace Thenewboston.Bank.Api { public interface IBankConfirmationBlockService { - public Task GetAllBankConfiramtionBlocksAsync(); + public Task> GetAllBankConfiramtionBlocksAsync(int offset, int limit); public Task PostConfirmationBlockAsync(ConfirmationBlock confirmationBlock); } } diff --git a/src/Thenewboston/Bank/Api/IBlocksService.cs b/src/Thenewboston/Bank/Api/IBlocksService.cs new file mode 100644 index 0000000..c2e5b1e --- /dev/null +++ b/src/Thenewboston/Bank/Api/IBlocksService.cs @@ -0,0 +1,15 @@ +using System.Net.Http; +using System.Threading.Tasks; +using Thenewboston.Bank.Api.Models; +using Thenewboston.Bank.Models; +using Thenewboston.Common.Api.Models; + +namespace Thenewboston.Bank.Api +{ + public interface IBlocksService + { + public Task> GetBlocksAsync(int offset, int limit); + + public Task PostBlocksAsync(Thenewboston.Common.Models.Block block); + } +} \ No newline at end of file diff --git a/src/Thenewboston/Bank/Api/IConnectedBanksService.cs b/src/Thenewboston/Bank/Api/IConnectedBanksService.cs index b742af2..054d989 100644 --- a/src/Thenewboston/Bank/Api/IConnectedBanksService.cs +++ b/src/Thenewboston/Bank/Api/IConnectedBanksService.cs @@ -6,7 +6,7 @@ namespace Thenewboston.Bank.Api.Models { public interface IConnectedBanksService { - Task GetBanksAsync(); - Task UpdateBankAsync(string nodeIdentifier, RequestModel payload); + Task> GetBanksAsync(int offset, int limit); + Task UpdateBankAsync(string nodeIdentifier, RequestModel payload); } } diff --git a/src/Thenewboston/Bank/Api/IConnectionRequestService.cs b/src/Thenewboston/Bank/Api/IConnectionRequestService.cs new file mode 100644 index 0000000..9f03052 --- /dev/null +++ b/src/Thenewboston/Bank/Api/IConnectionRequestService.cs @@ -0,0 +1,14 @@ +using System; +using System.Collections.Generic; +using System.Net.Http; +using System.Text; +using System.Threading.Tasks; +using Thenewboston.Bank.Models; + +namespace Thenewboston.Bank.Api +{ + public interface IConnectionRequestService + { + Task PostConnectionRequestAsync(ConnectionRequest connectionNotice); + } +} diff --git a/src/Thenewboston/Bank/Api/IInvalidBlocksService.cs b/src/Thenewboston/Bank/Api/IInvalidBlocksService.cs new file mode 100644 index 0000000..764674f --- /dev/null +++ b/src/Thenewboston/Bank/Api/IInvalidBlocksService.cs @@ -0,0 +1,14 @@ +using System.Threading.Tasks; +using Thenewboston.Bank.Api.Models; +using Thenewboston.Bank.Models; +using Thenewboston.Common.Api.Models; + +namespace Thenewboston.Bank.Api +{ + public interface IInvalidBlocksService + { + Task> GetInvalidBankBlocksAsync(int offset, int limit); + Task SendInvalidBlocksToBankAsync(BankInvalidBlockRequest model); + + } +} \ No newline at end of file diff --git a/src/Thenewboston/Bank/Api/ITransactionsService.cs b/src/Thenewboston/Bank/Api/ITransactionsService.cs index c8a603c..38ba84a 100644 --- a/src/Thenewboston/Bank/Api/ITransactionsService.cs +++ b/src/Thenewboston/Bank/Api/ITransactionsService.cs @@ -1,11 +1,12 @@ using System.Collections.Generic; using System.Threading.Tasks; using Thenewboston.Bank.Models; +using Thenewboston.Common.Api.Models; namespace Thenewboston.Bank.Api { public interface ITransactionsService { - Task> GetAllTransactionsAsync(); + Task> GetAllTransactionsAsync(int offset, int limit); } } \ No newline at end of file diff --git a/src/Thenewboston/Bank/Api/IUpgradeNoticeService.cs b/src/Thenewboston/Bank/Api/IUpgradeNoticeService.cs new file mode 100644 index 0000000..b7019ce --- /dev/null +++ b/src/Thenewboston/Bank/Api/IUpgradeNoticeService.cs @@ -0,0 +1,11 @@ +using System.Net.Http; +using System.Threading.Tasks; +using Thenewboston.Bank.Models; + +namespace Thenewboston.Bank.Api +{ + public interface IUpgradeNoticeService + { + Task PostUpgradeNoticeAsync(UpgradeNotice upgradeNotice); + } +} diff --git a/src/Thenewboston/Bank/Api/IValidatorConfirmationService.cs b/src/Thenewboston/Bank/Api/IValidatorConfirmationService.cs new file mode 100644 index 0000000..5d59720 --- /dev/null +++ b/src/Thenewboston/Bank/Api/IValidatorConfirmationService.cs @@ -0,0 +1,18 @@ +using System.Net.Http; +using System.Threading.Tasks; +using Thenewboston.Bank.Api.Models; +using Thenewboston.Common.Api.Models; +using Thenewboston.Common.Models; + +namespace Thenewboston.Bank.Api +{ + public interface IValidatorConfirmationService + { + public Task> GetValidatorConfirmationServicesAsync( + int offset, + int limit); + + public Task PostValidatorConfirmationServiceAsync( + BankValidatorConfirmationService service); + } +} \ No newline at end of file diff --git a/src/Thenewboston/Bank/Api/IValidatorService.cs b/src/Thenewboston/Bank/Api/IValidatorService.cs index c0850c8..24b5f1b 100644 --- a/src/Thenewboston/Bank/Api/IValidatorService.cs +++ b/src/Thenewboston/Bank/Api/IValidatorService.cs @@ -1,17 +1,13 @@ -using System; -using System.Collections.Generic; -using System.Text; -using System.Threading.Tasks; +using System.Threading.Tasks; using Thenewboston.Bank.Api.Models; using Thenewboston.Bank.Models; using Thenewboston.Common.Api.Models; -using Thenewboston.Common.Models; namespace Thenewboston.Bank.Api { public interface IValidatorService { - Task GetAllValidatorsAsync(); + Task> GetAllValidatorsAsync(int offset, int limit); Task PatchValidatorAsync(string nodeIdentifier, RequestModel trust); diff --git a/src/Thenewboston/Bank/Api/InvalidBlocksService.cs b/src/Thenewboston/Bank/Api/InvalidBlocksService.cs new file mode 100644 index 0000000..6e6bf0a --- /dev/null +++ b/src/Thenewboston/Bank/Api/InvalidBlocksService.cs @@ -0,0 +1,46 @@ +using System; +using System.Net.Http; +using System.Text; +using System.Threading.Tasks; +using Newtonsoft.Json; +using Thenewboston.Bank.Api.Models; +using Thenewboston.Bank.Models; +using Thenewboston.Common.Api.Models; +using Thenewboston.Common.Http; + +namespace Thenewboston.Bank.Api +{ + public class InvalidBlocksService : IInvalidBlocksService + { + private readonly IHttpRequestSender _requestSender; + + public InvalidBlocksService(IHttpRequestSender requestSender) + { + _requestSender = requestSender; + } + + public async Task> GetInvalidBankBlocksAsync(int offset = 0, int limit = 10) + { + var response = await _requestSender.GetAsync($"/invalid_blocks?offset={offset}&limit={limit}"); + + if (!response.IsSuccessStatusCode) + { + throw new Exception(); + } + + var responseContent = await response.Content.ReadAsStringAsync(); + return JsonConvert.DeserializeObject>(responseContent); + } + + public async Task SendInvalidBlocksToBankAsync(BankInvalidBlockRequest model) + { + var content = new StringContent(JsonConvert.SerializeObject(model), Encoding.UTF8, "application/json"); + var response = await _requestSender.PostAsync("/invalid_blocks", content); + if (!response.IsSuccessStatusCode) + throw new Exception("Error Sending Invalid Block to Bank"); + + var responseContent = await response.Content.ReadAsStringAsync(); + return JsonConvert.DeserializeObject(responseContent); + } + } +} \ No newline at end of file diff --git a/src/Thenewboston/Bank/Models/BankConfirmationBlockResponse.cs b/src/Thenewboston/Bank/Api/Models/BankConfirmationBlock.cs similarity index 88% rename from src/Thenewboston/Bank/Models/BankConfirmationBlockResponse.cs rename to src/Thenewboston/Bank/Api/Models/BankConfirmationBlock.cs index e86756b..13af35b 100644 --- a/src/Thenewboston/Bank/Models/BankConfirmationBlockResponse.cs +++ b/src/Thenewboston/Bank/Api/Models/BankConfirmationBlock.cs @@ -1,9 +1,9 @@ using System; using Newtonsoft.Json; -namespace Thenewboston.Bank.Models +namespace Thenewboston.Bank.Api.Models { - public class BankConfirmationBlockResponse + public class BankConfirmationBlock { [JsonProperty(PropertyName = "Id")] public string Id { get; set; } diff --git a/src/Thenewboston/Bank/Api/Models/BankInvalidBlockRequest.cs b/src/Thenewboston/Bank/Api/Models/BankInvalidBlockRequest.cs new file mode 100644 index 0000000..065d8f4 --- /dev/null +++ b/src/Thenewboston/Bank/Api/Models/BankInvalidBlockRequest.cs @@ -0,0 +1,58 @@ +using Newtonsoft.Json; + +namespace Thenewboston.Bank.Api.Models +{ + public class BankInvalidBlockRequest + { + [JsonProperty("message")] + public InvalidBlockBankRequestMessage Message { get; set; } + + [JsonProperty("node_identifier")] + public string NodeIdentifier { get; set; } + + [JsonProperty("signature")] + public string Signature { get; set; } + } + + public class InvalidBlockBankRequestMessage + { + [JsonProperty("block")] + public Block Block { get; set; } + + [JsonProperty("block_identifier")] + public string BlockIdentifier { get; set; } + + [JsonProperty("primary_validator_node_identifier")] + public string PrimaryValidatorNodeIdentifier { get; set; } + } + + public class Block + { + [JsonProperty("account_number")] + public string AccountNumber { get; set; } + + [JsonProperty("message")] + public BlockMessage Message { get; set; } + + [JsonProperty("signature")] + public string Signature { get; set; } + } + + public class BlockMessage + { + [JsonProperty("balance_key")] + public string BalanceKey { get; set; } + + [JsonProperty("txs")] + public BlockTransactions[] Txs { get; set; } + } + + public class BlockTransactions + { + [JsonProperty("amount")] + public double Amount { get; set; } + + [JsonProperty("recipient")] + public string Recipient { get; set; } + } +} \ No newline at end of file diff --git a/src/Thenewboston/Bank/Models/BankResponseModel.cs b/src/Thenewboston/Bank/Api/Models/BankNode.cs similarity index 92% rename from src/Thenewboston/Bank/Models/BankResponseModel.cs rename to src/Thenewboston/Bank/Api/Models/BankNode.cs index c684265..465cead 100644 --- a/src/Thenewboston/Bank/Models/BankResponseModel.cs +++ b/src/Thenewboston/Bank/Api/Models/BankNode.cs @@ -3,9 +3,9 @@ using Newtonsoft.Json; using Thenewboston.Common.Models; -namespace Thenewboston.Bank.Models +namespace Thenewboston.Bank.Api.Models { - public class BankResponseModel + public class BankNode { [JsonProperty(PropertyName = "account_number")] public string AccountNumber { get; set; } diff --git a/src/Thenewboston/Bank/Api/Models/RequestModel.cs b/src/Thenewboston/Bank/Api/Models/RequestModel.cs index b7352ba..f2cd6f3 100644 --- a/src/Thenewboston/Bank/Api/Models/RequestModel.cs +++ b/src/Thenewboston/Bank/Api/Models/RequestModel.cs @@ -1,6 +1,4 @@ -using System; - -namespace Thenewboston.Bank.Api.Models +namespace Thenewboston.Bank.Api.Models { public class RequestModel { diff --git a/src/Thenewboston/Bank/Api/Models/ValidatorConfirmation.cs b/src/Thenewboston/Bank/Api/Models/ValidatorConfirmation.cs new file mode 100644 index 0000000..aabab64 --- /dev/null +++ b/src/Thenewboston/Bank/Api/Models/ValidatorConfirmation.cs @@ -0,0 +1,32 @@ +using System; +using Newtonsoft.Json; + +namespace Thenewboston.Bank.Api.Models +{ + public class ValidatorConfirmation + { + [JsonProperty(PropertyName = "id")] + + public string Id { get; set; } + + [JsonProperty(PropertyName = "created_date")] + + public DateTime Created { get; set; } + + [JsonProperty(PropertyName = "modified_date")] + + public DateTime Modified { get; set; } + + [JsonProperty(PropertyName = "start")] + + public DateTime Start { get; set; } + + [JsonProperty(PropertyName = "end")] + + public DateTime End { get; set; } + + [JsonProperty(PropertyName = "validator")] + + public string Validator { get; set; } + } +} \ No newline at end of file diff --git a/src/Thenewboston/Bank/Api/TransactionsService.cs b/src/Thenewboston/Bank/Api/TransactionsService.cs index 942e401..ea90d5c 100644 --- a/src/Thenewboston/Bank/Api/TransactionsService.cs +++ b/src/Thenewboston/Bank/Api/TransactionsService.cs @@ -1,9 +1,9 @@ using System; using System.Collections.Generic; -using System.Net; using System.Threading.Tasks; using Newtonsoft.Json; using Thenewboston.Bank.Models; +using Thenewboston.Common.Api.Models; using Thenewboston.Common.Http; namespace Thenewboston.Bank.Api @@ -17,9 +17,11 @@ public TransactionsService(IHttpRequestSender requestSender) _requestSender = requestSender; } - public async Task> GetAllTransactionsAsync() + public async Task> GetAllTransactionsAsync( + int offset = 0, + int limit = 10) { - var response = await _requestSender.GetAsync("/bank_transactions"); + var response = await _requestSender.GetAsync($"/bank_transactions?offset={offset}&limit={limit}"); var stringResult = string.Empty; if (response.IsSuccessStatusCode) @@ -37,7 +39,7 @@ public async Task> GetAllTransactionsAsync() throw new Exception(); } - var result = JsonConvert.DeserializeObject>(stringResult); + var result = JsonConvert.DeserializeObject>(stringResult); return result; } diff --git a/src/Thenewboston/Bank/Api/UpgradeNoticeService.cs b/src/Thenewboston/Bank/Api/UpgradeNoticeService.cs new file mode 100644 index 0000000..480ed67 --- /dev/null +++ b/src/Thenewboston/Bank/Api/UpgradeNoticeService.cs @@ -0,0 +1,40 @@ +using System; +using System.Net.Http; +using System.Threading.Tasks; +using Newtonsoft.Json; +using Thenewboston.Bank.Models; +using Thenewboston.Common.Http; + +namespace Thenewboston.Bank.Api +{ + public class UpgradeNoticeService : IUpgradeNoticeService + { + IHttpRequestSender _requestSender; + + public UpgradeNoticeService(IHttpRequestSender requestSender) + { + _requestSender = requestSender; + } + + public async Task PostUpgradeNoticeAsync(UpgradeNotice upgradeNotice) + { + if(upgradeNotice is null) + { + // TODO: Create specific exception + throw new Exception(); + } + + var httpContent = new StringContent(JsonConvert.SerializeObject(upgradeNotice)); + var jsonString = await httpContent.ReadAsStringAsync(); + + if(string.IsNullOrEmpty(jsonString)) + { + // TODO: Create specific exception + throw new Exception(); + } + + var result = await _requestSender.PostAsync($"/upgrade_notice/{upgradeNotice.NodeIdentifier}", httpContent); + return result; + } + } +} diff --git a/src/Thenewboston/Bank/Api/ValidatorConfirmationService.cs b/src/Thenewboston/Bank/Api/ValidatorConfirmationService.cs new file mode 100644 index 0000000..7a8392e --- /dev/null +++ b/src/Thenewboston/Bank/Api/ValidatorConfirmationService.cs @@ -0,0 +1,68 @@ +using System; +using System.Net.Http; +using System.Text; +using System.Threading.Tasks; +using Newtonsoft.Json; +using Thenewboston.Bank.Api.Models; +using Thenewboston.Common.Api.Models; +using Thenewboston.Common.Http; +using Thenewboston.Common.Models; +using Thenewboston.Validator.Models; +using Block = Thenewboston.Common.Models.Block; + +namespace Thenewboston.Bank.Api +{ + public class ValidatorConfirmationService : IValidatorConfirmationService + { + private readonly IHttpRequestSender _requestSender; + + public ValidatorConfirmationService(IHttpRequestSender requestSender) + { + _requestSender = requestSender; + } + + public async Task> GetValidatorConfirmationServicesAsync( + int offset = 0, + int limit = 10) + { + var response = await _requestSender.GetAsync($"/validator_confirmation_services?offset={offset}&limit={limit}"); + + if (!response.IsSuccessStatusCode) + { + // TODO: Create specific exception + throw new Exception(); + } + + var stringResponse = await response.Content.ReadAsStringAsync(); + + if (string.IsNullOrEmpty(stringResponse)) + { + // TODO: Create specific exception + throw new Exception(); + } + + var result = + JsonConvert.DeserializeObject>( + stringResponse); + + return result; + } + + public async Task PostValidatorConfirmationServiceAsync( + BankValidatorConfirmationService service) + { + var httpContent = + new StringContent(JsonConvert.SerializeObject(service), Encoding.UTF8, "application/json"); + var request = await _requestSender.PostAsync("/blocks", httpContent); + + if (!request.IsSuccessStatusCode) + { + // TODO: Create specific exception + throw new Exception(); + } + + var response = new HttpResponseMessage(System.Net.HttpStatusCode.Created); + return response; + } + } +} \ No newline at end of file diff --git a/src/Thenewboston/Bank/Api/ValidatorService.cs b/src/Thenewboston/Bank/Api/ValidatorService.cs index c14274c..849282e 100644 --- a/src/Thenewboston/Bank/Api/ValidatorService.cs +++ b/src/Thenewboston/Bank/Api/ValidatorService.cs @@ -1,5 +1,4 @@ using System; -using System.Collections.Generic; using System.Net.Http; using System.Text; using System.Threading.Tasks; @@ -8,7 +7,6 @@ using Thenewboston.Bank.Models; using Thenewboston.Common.Api.Models; using Thenewboston.Common.Http; -using Thenewboston.Common.Models; namespace Thenewboston.Bank.Api { @@ -21,9 +19,9 @@ public ValidatorService(IHttpRequestSender requestSender) _requestSender = requestSender; } - public async Task GetAllValidatorsAsync() + public async Task> GetAllValidatorsAsync(int offset = 0, int limit = 10) { - var response = await _requestSender.GetAsync("/validators"); + var response = await _requestSender.GetAsync($"/validators?offset={offset}&limit={limit}"); if (!response.IsSuccessStatusCode) { @@ -39,11 +37,13 @@ public async Task GetAllValidatorsAsync() throw new Exception(); } - var result = JsonConvert.DeserializeObject(stringResult); + var result = JsonConvert.DeserializeObject>(stringResult); return result; } + + //TODO: remove this public async Task PatchValidatorAsync(string nodeIdentifier, RequestModel trust) { var jsonTrust = JsonConvert.SerializeObject(trust); diff --git a/src/Thenewboston/Bank/Models/BankBlock.cs b/src/Thenewboston/Bank/Models/BankBlock.cs index 3ac4c3e..f7eb28b 100644 --- a/src/Thenewboston/Bank/Models/BankBlock.cs +++ b/src/Thenewboston/Bank/Models/BankBlock.cs @@ -1,18 +1,26 @@ using System; +using Newtonsoft.Json; + namespace Thenewboston.Bank.Models { public class BankBlock { + [JsonProperty(PropertyName ="id")] public string Id { get; set; } - public string BalanceKey { get; set; } - - public string Sender { get; set; } - - public string Signature { get; set; } - + [JsonProperty(PropertyName ="created_date")] public DateTime Created { get; set; } + [JsonProperty(PropertyName ="modified_date")] public DateTime Modified { get; set; } + + [JsonProperty(PropertyName ="balance_key")] + public string BalanceKey { get; set; } + + [JsonProperty(PropertyName ="sender")] + public string Sender { get; set; } + + [JsonProperty(PropertyName ="signature")] + public string Signature { get; set; } } } diff --git a/src/Thenewboston/Bank/Models/BankConfirmationService.cs b/src/Thenewboston/Bank/Models/BankConfirmationService.cs deleted file mode 100644 index e3475b0..0000000 --- a/src/Thenewboston/Bank/Models/BankConfirmationService.cs +++ /dev/null @@ -1,18 +0,0 @@ -using System; -namespace Thenewboston.Bank.Models -{ - public class BankConfirmationService - { - public string Id { get; set; } - - public DateTime Created { get; set; } - - public DateTime Modified { get; set; } - - public DateTime Start { get; set; } - - public DateTime End { get; set; } - - public string Validator { get; set; } - } -} diff --git a/src/Thenewboston/Bank/Models/BankInvalidBlock.cs b/src/Thenewboston/Bank/Models/BankInvalidBlock.cs new file mode 100644 index 0000000..8b28911 --- /dev/null +++ b/src/Thenewboston/Bank/Models/BankInvalidBlock.cs @@ -0,0 +1,30 @@ +using System; +using Newtonsoft.Json; + +namespace Thenewboston.Bank.Models +{ + public class BankInvalidBlock + { + [JsonProperty("id")] + public string Id { get; set; } + + [JsonProperty("created_date")] + public DateTime Created { get; set; } + + [JsonProperty("modified_date")] + public DateTime Modified { get; set; } + + [JsonProperty("block_identifier")] + public string BlockIdentifier { get; set; } + + [JsonProperty("block")] + public string Block { get; set; } + + [JsonProperty("confirmation_validator")] + public string ConfirmationValidator { get; set; } + + [JsonProperty("primary_validator")] + public string PrimaryValidator { get; set; } + } + +} \ No newline at end of file diff --git a/src/Thenewboston/Bank/Models/ConnectionRequest.cs b/src/Thenewboston/Bank/Models/ConnectionRequest.cs new file mode 100644 index 0000000..c0d0078 --- /dev/null +++ b/src/Thenewboston/Bank/Models/ConnectionRequest.cs @@ -0,0 +1,19 @@ +using System; +using System.Collections.Generic; +using System.Text; +using Newtonsoft.Json; + +namespace Thenewboston.Bank.Models +{ + public class ConnectionRequest + { + [JsonProperty(PropertyName = "message")] + public ConnectionRequestMessage Message { get; set; } + + [JsonProperty(PropertyName = "node_identifier")] + public string NodeIdentifier { get; set; } + + [JsonProperty(PropertyName = "signature")] + public string Signature { get; set; } + } +} diff --git a/src/Thenewboston/Bank/Models/ConnectionRequestMessage.cs b/src/Thenewboston/Bank/Models/ConnectionRequestMessage.cs new file mode 100644 index 0000000..4d002d2 --- /dev/null +++ b/src/Thenewboston/Bank/Models/ConnectionRequestMessage.cs @@ -0,0 +1,19 @@ +using System; +using System.Collections.Generic; +using System.Text; +using Newtonsoft.Json; + +namespace Thenewboston.Bank.Models +{ + public class ConnectionRequestMessage + { + [JsonProperty(PropertyName = "ip_address")] + public string IpAddress { get; set; } + + [JsonProperty(PropertyName = "port")] + public string Port { get; set; } + + [JsonProperty(PropertyName = "protocol")] + public string Protocol { get; set; } + } +} diff --git a/src/Thenewboston/Bank/Models/UpgradeNotice.cs b/src/Thenewboston/Bank/Models/UpgradeNotice.cs new file mode 100644 index 0000000..2029ad0 --- /dev/null +++ b/src/Thenewboston/Bank/Models/UpgradeNotice.cs @@ -0,0 +1,16 @@ +using Newtonsoft.Json; + +namespace Thenewboston.Bank.Models +{ + public class UpgradeNotice + { + [JsonProperty(PropertyName ="message")] + public UpgradeNoticeMessage Message { get; set; } + + [JsonProperty(PropertyName = "node_identifier")] + public string NodeIdentifier { get; set; } + + [JsonProperty(PropertyName ="signature")] + public string Signature { get; set; } + } +} diff --git a/src/Thenewboston/Bank/Models/UpgradeNoticeMessage.cs b/src/Thenewboston/Bank/Models/UpgradeNoticeMessage.cs new file mode 100644 index 0000000..6c38c5a --- /dev/null +++ b/src/Thenewboston/Bank/Models/UpgradeNoticeMessage.cs @@ -0,0 +1,10 @@ +using Newtonsoft.Json; + +namespace Thenewboston.Bank.Models +{ + public class UpgradeNoticeMessage + { + [JsonProperty(PropertyName = "bank_node_identifier")] + public string BankNodeIdentifier { get; set; } + } +} diff --git a/src/Thenewboston/Common/Api/Models/PaginatedResponseModel.cs b/src/Thenewboston/Common/Api/Models/PaginatedResponseModel.cs index f5f59bd..02ebca7 100644 --- a/src/Thenewboston/Common/Api/Models/PaginatedResponseModel.cs +++ b/src/Thenewboston/Common/Api/Models/PaginatedResponseModel.cs @@ -5,7 +5,7 @@ namespace Thenewboston.Common.Api.Models { - public class PaginatedResponseModel + public class PaginatedResponseModel { [JsonProperty(PropertyName = "count")] public int Count { get; set; } @@ -17,6 +17,6 @@ public class PaginatedResponseModel public string Previous { get; set; } [JsonProperty(PropertyName = "results")] - public IEnumerable Results { get; set; } + public IEnumerable Results { get; set; } } } diff --git a/src/Thenewboston/Common/Models/BankValidatorConfirmationService.cs b/src/Thenewboston/Common/Models/BankValidatorConfirmationService.cs new file mode 100644 index 0000000..46191c7 --- /dev/null +++ b/src/Thenewboston/Common/Models/BankValidatorConfirmationService.cs @@ -0,0 +1,17 @@ +using Newtonsoft.Json; +using Thenewboston.Bank.Api; + +namespace Thenewboston.Common.Models +{ + public class BankValidatorConfirmationService + { + [JsonProperty(PropertyName ="message")] + public ValidatorConfirmationServiceMessage Message { get; set; } + + [JsonProperty(PropertyName ="node_identifier")] + public string NodeIdentifier { get; set; } + + [JsonProperty(PropertyName ="signature")] + public string Signature { get; set; } + } +} \ No newline at end of file diff --git a/src/Thenewboston/Common/Models/Block.cs b/src/Thenewboston/Common/Models/Block.cs new file mode 100644 index 0000000..fcf5111 --- /dev/null +++ b/src/Thenewboston/Common/Models/Block.cs @@ -0,0 +1,16 @@ +using Newtonsoft.Json; + +namespace Thenewboston.Common.Models +{ + public class Block + { + [JsonProperty(PropertyName ="account_number")] + public string AccountNumber { get; set; } + + [JsonProperty(PropertyName ="message")] + public BlockMessage Message { get; set; } + + [JsonProperty(PropertyName ="signature")] + public string Signature { get; set; } + } +} \ No newline at end of file diff --git a/src/Thenewboston/Common/Models/BlockMessage.cs b/src/Thenewboston/Common/Models/BlockMessage.cs new file mode 100644 index 0000000..78b7398 --- /dev/null +++ b/src/Thenewboston/Common/Models/BlockMessage.cs @@ -0,0 +1,14 @@ +using System.Collections.Generic; +using Newtonsoft.Json; + +namespace Thenewboston.Common.Models +{ + public class BlockMessage + { + [JsonProperty(PropertyName ="balance_key")] + public string BalanceKey { get; set; } + + [JsonProperty(PropertyName ="txs")] + public List Transactions { get; set; } + } +} \ No newline at end of file diff --git a/src/Thenewboston/Common/Models/BlockTransaction.cs b/src/Thenewboston/Common/Models/BlockTransaction.cs new file mode 100644 index 0000000..cebb961 --- /dev/null +++ b/src/Thenewboston/Common/Models/BlockTransaction.cs @@ -0,0 +1,13 @@ +using Newtonsoft.Json; + +namespace Thenewboston.Common.Models +{ + public class BlockTransaction + { + [JsonProperty(PropertyName ="amount")] + public string Amount { get; set; } + + [JsonProperty(PropertyName ="recipient")] + public string Recipient { get; set; } + } +} \ No newline at end of file diff --git a/src/Thenewboston/Common/Models/ValidatorConfirmationServiceMessage.cs b/src/Thenewboston/Common/Models/ValidatorConfirmationServiceMessage.cs new file mode 100644 index 0000000..d26720f --- /dev/null +++ b/src/Thenewboston/Common/Models/ValidatorConfirmationServiceMessage.cs @@ -0,0 +1,16 @@ +using System; +using Newtonsoft.Json; + +namespace Thenewboston.Common.Models +{ + public class ValidatorConfirmationServiceMessage + { + [JsonProperty(PropertyName = "start")] + + public DateTime Start { get; set; } + + [JsonProperty(PropertyName = "end")] + + public DateTime End { get; set; } + } +} \ No newline at end of file diff --git a/src/Thenewboston/Nodes/Bank.cs b/src/Thenewboston/Nodes/Bank.cs new file mode 100644 index 0000000..7f7421b --- /dev/null +++ b/src/Thenewboston/Nodes/Bank.cs @@ -0,0 +1,126 @@ +using System.Collections.Generic; +using System.Runtime.CompilerServices; +using System.Threading.Tasks; +using Thenewboston.Bank.Api; +using Thenewboston.Bank.Api.Models; +using Thenewboston.Bank.Models; +using Thenewboston.Common.Api.Models; +using Thenewboston.Common.Http; + +namespace Thenewboston.Nodes +{ + public class Bank + { + private readonly IAccountsService _accountsService; + private readonly IBankConfirmationBlockService _bankConfirmationBlockService; + private readonly IBlocksService _blocksService; + private readonly IConfigService _configService; + private readonly IConnectedBanksService _banksService; + private readonly ITransactionsService _transactionsService; + private readonly IValidatorConfirmationService _validatorConfirmationService; + private readonly IValidatorService _validatorService; + + public Bank(string ip, int port) + { + var requestSender = new SimpleHttpRequestSender($"{ip}:{port}"); + _accountsService = new AccountsService(requestSender); + _bankConfirmationBlockService = new BankConfirmationBlockService(requestSender); + _blocksService = new BlocksService(requestSender); + _configService = new ConfigService(requestSender); + _banksService = new ConnectedBanksService(requestSender); + _transactionsService = new TransactionsService(requestSender); + _validatorConfirmationService = new ValidatorConfirmationService(requestSender); + _validatorService = new ValidatorService(requestSender); + } + + public async Task> GetAccounts( + int offset = 0, + int limit = 10) + { + var result = await _accountsService.GetAccountsAsync(offset, limit); + return result.Results; + } + + public async Task UpdateBankAccount( + string accountNumber, + double trust, + string nodeIdentifier, + string signature) + { + var requestModel = new RequestModel + { + Message = new Message + { + Trust = trust + }, + NodeIdentifier = nodeIdentifier, + Signature = signature + }; + + var result = await _accountsService.UpdateAccountAsync(accountNumber, requestModel); + + return result; + } + + public async Task> GetBankConfirmationBlocks( + int offset = 0, + int limit = 10) + { + var result = await _bankConfirmationBlockService.GetAllBankConfiramtionBlocksAsync(offset, limit); + return result.Results; + } + + public async Task> GetBankBlocks( + int offset = 0, + int limit = 10) + { + var result = await _blocksService.GetBlocksAsync(offset, limit); + return result.Results; + } + + public async Task CreateBlock(Common.Models.Block block) + { + var result = await _blocksService.PostBlocksAsync(block); + return result; + } + + public async Task GetBankConfig() + { + var result = await _configService.GetBankConfigAsync(); + return result; + } + + public async Task> GetConnectedBanks( + int offset = 0, + int limit = 10) + { + var result = await _banksService.GetBanksAsync(offset, limit); + return result.Results; + } + + public async Task> GetTransactions( + int offset = 0, + int limit = 10) + { + var result = await _transactionsService.GetAllTransactionsAsync(offset, limit); + return result.Results; + } + + public async Task> GetValidatorConfirmationServices( + int offset = 0, + int limit = 10) + { + var result = await _validatorConfirmationService.GetValidatorConfirmationServicesAsync(offset, limit); + return result.Results; + } + + public async Task> GetValidators( + int offset = 0, + int limit = 10) + { + var result = await _validatorService.GetAllValidatorsAsync(offset, limit); + return result.Results; + } + + } +} diff --git a/src/Thenewboston/Nodes/Validator.cs b/src/Thenewboston/Nodes/Validator.cs new file mode 100644 index 0000000..6e42f68 --- /dev/null +++ b/src/Thenewboston/Nodes/Validator.cs @@ -0,0 +1,89 @@ +using System.Collections; +using System.Collections.Generic; +using System.Threading.Tasks; +using Thenewboston.Common.Http; +using Thenewboston.Common.Models; +using Thenewboston.Validator.Api; +using Thenewboston.Validator.Api.Models; +using Thenewboston.Validator.Models; + +namespace Thenewboston.Nodes +{ + public class Validator + { + private readonly IAccountsService _accountsService; + private readonly IBankBlockService _bankBlockService; // need to ask someone + private readonly IBankConfirmationService _bankConfirmationService; + private readonly IConfigService _configService; + private readonly IConnectedBanksService _connectedBanksService; + private readonly IValidatorConfirmationBlockService _validatorConfirmationBlockService; + private readonly IValidatorsService _validatorsService; + + public Validator(string ip, int port) + { + var requestSender = new SimpleHttpRequestSender($"{ip}:{port}"); + _accountsService = new AccountsService(requestSender); + _bankBlockService = new BankBlockService(requestSender); + _bankConfirmationService = new BankConfirmationService(requestSender); + _configService = new ConfigService(requestSender); + _connectedBanksService = new ConnectedBanksService(requestSender); + _validatorConfirmationBlockService = new ValidatorConfirmationBlockService(requestSender); + _validatorsService = new ValidatorsService(requestSender); + } + + public async Task> GetAccounts(int offset = 0, int limit = 10) + { + var result = await _accountsService.GetAccountsAsync(offset, limit); + return result.Results; + } + + public async Task GetAccountBalance(string accountNumber) + { + var result = await _accountsService.GetAccountBalanceAsync(accountNumber); + return result; + } + + public async Task GetAccountBalanceLock(string accountNumber) + { + var result = await _accountsService.GetAccountBalanceLockAsync(accountNumber); + return result; + } + + public async Task> GetBankConfirmationServices(int offset, + int limit) + { + var result = await _bankConfirmationService.GetBankConfirmationServicesAsync(offset, limit); + return result.Results; + } + + public async Task GetValidatorConfig() + { + var result = await _configService.GetValidatorConfigAsync(); + return result; + } + + public async Task> GetBanks(int offset, int limit) + { + var result = await _connectedBanksService.GetBanksAsync(offset, limit); + return result.Results; + } + + public async Task GetQueuedConfirmationBlock(string blockIdentifier) + { + var result = await _validatorConfirmationBlockService.GetQueuedConfirmationBlockAsync(blockIdentifier); + return result; + } + + public async Task GetValidConfirmationBlock(string blockIdentifier) + { + var result = await _validatorConfirmationBlockService.GetValidConfirmationBlockAsync(blockIdentifier); + return result; + } + + public async Task> GetAllValidators(int offset, int limit) + { + var result = await _validatorsService.GetAllValidatorsAsync(offset, limit); + return result.Results; + } + } +} \ No newline at end of file diff --git a/src/Thenewboston/Thenewboston.csproj b/src/Thenewboston/Thenewboston.csproj index fec98d3..5294083 100644 --- a/src/Thenewboston/Thenewboston.csproj +++ b/src/Thenewboston/Thenewboston.csproj @@ -10,6 +10,8 @@ + + diff --git a/src/Thenewboston/Validator/Api/AccountsService.cs b/src/Thenewboston/Validator/Api/AccountsService.cs index 60dc867..1f2994d 100644 --- a/src/Thenewboston/Validator/Api/AccountsService.cs +++ b/src/Thenewboston/Validator/Api/AccountsService.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using System.Threading.Tasks; using Newtonsoft.Json; +using Thenewboston.Common.Api.Models; using Thenewboston.Common.Http; using Thenewboston.Validator.Models; @@ -16,9 +17,9 @@ public AccountsService(IHttpRequestSender requestSender) _requestSender = requestSender; } - public async Task> GetAccountsAsync() + public async Task> GetAccountsAsync(int offset = 0, int limit = 10) { - var response = await _requestSender.GetAsync("/accounts"); + var response = await _requestSender.GetAsync($"/accounts?offset={offset}&limit={limit}"); if (!response.IsSuccessStatusCode) { @@ -34,7 +35,7 @@ public async Task> GetAccountsAsync() throw new Exception(); } - var result = JsonConvert.DeserializeObject>(stringResult); + var result = JsonConvert.DeserializeObject>(stringResult); return result; } diff --git a/src/Thenewboston/Validator/Api/BankBlockService.cs b/src/Thenewboston/Validator/Api/BankBlockService.cs new file mode 100644 index 0000000..35d3133 --- /dev/null +++ b/src/Thenewboston/Validator/Api/BankBlockService.cs @@ -0,0 +1,36 @@ +using System; +using System.Net.Http; +using System.Text; +using System.Threading.Tasks; +using Newtonsoft.Json; +using Thenewboston.Common.Http; +using Thenewboston.Validator.Models; + + +namespace Thenewboston.Validator.Api +{ + public class BankBlockService : IBankBlockService + { + private readonly IHttpRequestSender _requestSender; + + public BankBlockService(IHttpRequestSender requestSender) + { + _requestSender = requestSender; + } + + public async Task PostBankBlockAsync(ValidatorBankBlock block) + { + var httpContent = new StringContent(JsonConvert.SerializeObject(block), Encoding.UTF8, "application/json"); + var request = await _requestSender.PostAsync("/bank_blocks", httpContent); + + if(!request.IsSuccessStatusCode) + { + // TODO: Create specific exception + throw new Exception(); + } + + var response = new HttpResponseMessage(System.Net.HttpStatusCode.Created); + return response; + } + } +} \ No newline at end of file diff --git a/src/Thenewboston/Validator/Api/BankConfirmationService.cs b/src/Thenewboston/Validator/Api/BankConfirmationService.cs new file mode 100644 index 0000000..50b7ebd --- /dev/null +++ b/src/Thenewboston/Validator/Api/BankConfirmationService.cs @@ -0,0 +1,45 @@ +using System; +using System.Threading.Tasks; +using Newtonsoft.Json; +using Thenewboston.Common.Api.Models; +using Thenewboston.Common.Http; +using Thenewboston.Validator.Api.Models; + +namespace Thenewboston.Validator.Api +{ + public class BankConfirmationService : IBankConfirmationService + { + private readonly IHttpRequestSender _requestSender; + + public BankConfirmationService(IHttpRequestSender requestSender) + { + _requestSender = requestSender; + } + + public async Task> GetBankConfirmationServicesAsync( + int offset = 0, + int limit = 10) + { + var response = await _requestSender.GetAsync($"/bank_confirmation_services?offset={offset}&limit={limit}"); + + if (!response.IsSuccessStatusCode) + { + // TODO: Create specific exception + throw new Exception(); + } + + var stringResponse = await response.Content.ReadAsStringAsync(); + + if (string.IsNullOrEmpty(stringResponse)) + { + // TODO: Create specific exception + throw new Exception(); + } + + var result = + JsonConvert.DeserializeObject>(stringResponse); + + return result; + } + } +} \ No newline at end of file diff --git a/src/Thenewboston/Validator/Api/ConnectedBanksService.cs b/src/Thenewboston/Validator/Api/ConnectedBanksService.cs index cb7e553..537a567 100644 --- a/src/Thenewboston/Validator/Api/ConnectedBanksService.cs +++ b/src/Thenewboston/Validator/Api/ConnectedBanksService.cs @@ -3,6 +3,7 @@ using Newtonsoft.Json; using Thenewboston.Common.Api.Models; using Thenewboston.Common.Http; +using Thenewboston.Validator.Models; namespace Thenewboston.Validator.Api { @@ -15,9 +16,9 @@ public ConnectedBanksService(IHttpRequestSender requestSender) _requestSender = requestSender; } - public async Task GetBanksAsync() + public async Task> GetBanksAsync(int offset = 0, int limit = 10) { - var response = await _requestSender.GetAsync("/banks"); + var response = await _requestSender.GetAsync($"/banks?offset={offset}&limit={limit}"); if (!response.IsSuccessStatusCode) { @@ -33,7 +34,7 @@ public async Task GetBanksAsync() throw new Exception(); } - var result = JsonConvert.DeserializeObject(stringResult); + var result = JsonConvert.DeserializeObject>(stringResult); return result; } diff --git a/src/Thenewboston/Validator/Api/ConnectionRequestService.cs b/src/Thenewboston/Validator/Api/ConnectionRequestService.cs new file mode 100644 index 0000000..f8d6358 --- /dev/null +++ b/src/Thenewboston/Validator/Api/ConnectionRequestService.cs @@ -0,0 +1,42 @@ +using System; +using System.Collections.Generic; +using System.Net.Http; +using System.Text; +using System.Threading.Tasks; +using Newtonsoft.Json; +using Thenewboston.Common.Http; +using Thenewboston.Validator.Models; + +namespace Thenewboston.Validator.Api +{ + public class ConnectionRequestService : IConnectionRequestService + { + IHttpRequestSender _requestSender; + + public ConnectionRequestService(IHttpRequestSender requestSender) + { + _requestSender = requestSender; + } + + public async Task PostConnectionRequestAsync(ConnectionRequest connectionNotice) + { + if (connectionNotice is null) + { + // TODO: Create specific exception + throw new Exception(); + } + + var httpContent = new StringContent(JsonConvert.SerializeObject(connectionNotice)); + var jsonString = await httpContent.ReadAsStringAsync(); + + if (string.IsNullOrEmpty(jsonString)) + { + // TODO: Create specific exception + throw new Exception(); + } + + var result = await _requestSender.PostAsync("/connection_requests", httpContent); + return result; + } + } +} diff --git a/src/Thenewboston/Validator/Api/IAccountsService.cs b/src/Thenewboston/Validator/Api/IAccountsService.cs index 79342c8..1833c1f 100644 --- a/src/Thenewboston/Validator/Api/IAccountsService.cs +++ b/src/Thenewboston/Validator/Api/IAccountsService.cs @@ -1,14 +1,13 @@ -using System; -using System.Collections.Generic; -using System.Numerics; +using System.Collections.Generic; using System.Threading.Tasks; +using Thenewboston.Common.Api.Models; using Thenewboston.Validator.Models; namespace Thenewboston.Validator.Api { public interface IAccountsService { - Task> GetAccountsAsync(); + Task> GetAccountsAsync(int offset, int limit); Task GetAccountBalanceAsync(string accountNumber); Task GetAccountBalanceLockAsync(string accountNumber); } diff --git a/src/Thenewboston/Validator/Api/IBankBlockService.cs b/src/Thenewboston/Validator/Api/IBankBlockService.cs new file mode 100644 index 0000000..25d51c2 --- /dev/null +++ b/src/Thenewboston/Validator/Api/IBankBlockService.cs @@ -0,0 +1,11 @@ +using System.Net.Http; +using System.Threading.Tasks; +using Thenewboston.Validator.Models; + +namespace Thenewboston.Validator.Api +{ + public interface IBankBlockService + { + public Task PostBankBlockAsync(ValidatorBankBlock block); + } +} \ No newline at end of file diff --git a/src/Thenewboston/Validator/Api/IBankConfirmationService.cs b/src/Thenewboston/Validator/Api/IBankConfirmationService.cs new file mode 100644 index 0000000..b2e8644 --- /dev/null +++ b/src/Thenewboston/Validator/Api/IBankConfirmationService.cs @@ -0,0 +1,13 @@ +using System.Threading.Tasks; +using Thenewboston.Common.Api.Models; +using Thenewboston.Validator.Api.Models; + +namespace Thenewboston.Validator.Api +{ + public interface IBankConfirmationService + { + public Task> GetBankConfirmationServicesAsync( + int offset, + int limit); + } +} \ No newline at end of file diff --git a/src/Thenewboston/Validator/Api/IConnectedBanksService.cs b/src/Thenewboston/Validator/Api/IConnectedBanksService.cs index f2d7c24..3bb8944 100644 --- a/src/Thenewboston/Validator/Api/IConnectedBanksService.cs +++ b/src/Thenewboston/Validator/Api/IConnectedBanksService.cs @@ -1,10 +1,11 @@ using System.Threading.Tasks; using Thenewboston.Common.Api.Models; +using Thenewboston.Validator.Models; namespace Thenewboston.Validator.Api { public interface IConnectedBanksService { - Task GetBanksAsync(); + Task> GetBanksAsync(int offset, int limit); } } diff --git a/src/Thenewboston/Validator/Api/IConnectionRequestService.cs b/src/Thenewboston/Validator/Api/IConnectionRequestService.cs new file mode 100644 index 0000000..c7ba538 --- /dev/null +++ b/src/Thenewboston/Validator/Api/IConnectionRequestService.cs @@ -0,0 +1,14 @@ +using System; +using System.Collections.Generic; +using System.Net.Http; +using System.Text; +using System.Threading.Tasks; +using Thenewboston.Validator.Models; + +namespace Thenewboston.Validator.Api +{ + public interface IConnectionRequestService + { + Task PostConnectionRequestAsync(ConnectionRequest connectionNotice); + } +} diff --git a/src/Thenewboston/Validator/Api/IPrimaryValidatorUpdatedService.cs b/src/Thenewboston/Validator/Api/IPrimaryValidatorUpdatedService.cs new file mode 100644 index 0000000..9edb051 --- /dev/null +++ b/src/Thenewboston/Validator/Api/IPrimaryValidatorUpdatedService.cs @@ -0,0 +1,11 @@ +using System.Net.Http; +using System.Threading.Tasks; +using Thenewboston.Validator.Models; + +namespace Thenewboston.Validator.Api +{ + public interface IPrimaryValidatorUpdatedService + { + Task PostPrimaryValidatorUpdatedAsync(PrimaryValidatorUpdatedModel validatorUpdatedModel); + } +} diff --git a/src/Thenewboston/Validator/Api/IUpgradeRequestService.cs b/src/Thenewboston/Validator/Api/IUpgradeRequestService.cs new file mode 100644 index 0000000..6899779 --- /dev/null +++ b/src/Thenewboston/Validator/Api/IUpgradeRequestService.cs @@ -0,0 +1,12 @@ +using System.Net.Http; +using System.Threading.Tasks; +using Thenewboston.Validator.Api.Models; +using Thenewboston.Validator.Models; + +namespace Thenewboston.Validator.Api +{ + public interface IUpgradeRequestService + { + public Task PostUpgradeRequestAsync(UpgradeRequest upgradeRequest); + } +} diff --git a/src/Thenewboston/Validator/Api/IValidatorConfirmationBlockService.cs b/src/Thenewboston/Validator/Api/IValidatorConfirmationBlockService.cs index c6f0a67..f200ac5 100644 --- a/src/Thenewboston/Validator/Api/IValidatorConfirmationBlockService.cs +++ b/src/Thenewboston/Validator/Api/IValidatorConfirmationBlockService.cs @@ -5,8 +5,8 @@ namespace Thenewboston.Validator.Api { public interface IValidatorConfirmationBlockService { - Task PostConfiramtionBlockAsync(ConfirmationBlock confirmationBlockMessage); - Task GetQueuedConfiramtionBlockAsync(string blockIdentifier); + Task PostConfirmationBlockAsync(ConfirmationBlock confirmationBlockMessage); + Task GetQueuedConfirmationBlockAsync(string blockIdentifier); Task GetValidConfirmationBlockAsync(string blockIdentifier); } } diff --git a/src/Thenewboston/Validator/Api/IValidatorsService.cs b/src/Thenewboston/Validator/Api/IValidatorsService.cs index eec9377..f3adf18 100644 --- a/src/Thenewboston/Validator/Api/IValidatorsService.cs +++ b/src/Thenewboston/Validator/Api/IValidatorsService.cs @@ -1,14 +1,14 @@ -using System; -using System.Collections.Generic; -using System.Text; -using System.Threading.Tasks; +using System.Threading.Tasks; using Thenewboston.Common.Api.Models; -using Thenewboston.Common.Models; +using Thenewboston.Validator.Api.Models; +using Thenewboston.Validator.Models; namespace Thenewboston.Validator.Api { public interface IValidatorsService { - Task GetAllValidatorsAsync(); + Task> GetAllValidatorsAsync( + int offset, + int limit); } } diff --git a/src/Thenewboston/Validator/Api/Models/BankConfirmationServiceResponse.cs b/src/Thenewboston/Validator/Api/Models/BankConfirmationServiceResponse.cs new file mode 100644 index 0000000..4fca87f --- /dev/null +++ b/src/Thenewboston/Validator/Api/Models/BankConfirmationServiceResponse.cs @@ -0,0 +1,32 @@ +using System; +using Newtonsoft.Json; + +namespace Thenewboston.Validator.Api.Models +{ + public class BankConfirmationServiceResponse + { + [JsonProperty(PropertyName = "id")] + + public string Id { get; set; } + + [JsonProperty(PropertyName = "created_date")] + + public DateTime Created { get; set; } + + [JsonProperty(PropertyName = "modified_date")] + + public DateTime Modified { get; set; } + + [JsonProperty(PropertyName = "start")] + + public DateTime Start { get; set; } + + [JsonProperty(PropertyName = "end")] + + public DateTime End { get; set; } + + [JsonProperty(PropertyName = "bank")] + + public string Bank { get; set; } + } +} \ No newline at end of file diff --git a/src/Thenewboston/Validator/Api/Models/UpgradeRequest.cs b/src/Thenewboston/Validator/Api/Models/UpgradeRequest.cs new file mode 100644 index 0000000..8d39f4b --- /dev/null +++ b/src/Thenewboston/Validator/Api/Models/UpgradeRequest.cs @@ -0,0 +1,16 @@ +using Newtonsoft.Json; + +namespace Thenewboston.Validator.Api.Models +{ + public class UpgradeRequest + { + [JsonProperty(PropertyName ="message")] + public UpgradeRequestMessage Message { get; set; } + + [JsonProperty(PropertyName ="node_identifier")] + public string NodeIdentifier { get; set; } + + [JsonProperty(PropertyName ="signature")] + public string Signature { get; set; } + } +} diff --git a/src/Thenewboston/Validator/Api/Models/UpgradeRequestMessage.cs b/src/Thenewboston/Validator/Api/Models/UpgradeRequestMessage.cs new file mode 100644 index 0000000..5111f15 --- /dev/null +++ b/src/Thenewboston/Validator/Api/Models/UpgradeRequestMessage.cs @@ -0,0 +1,10 @@ +using Newtonsoft.Json; + +namespace Thenewboston.Validator.Api.Models +{ + public class UpgradeRequestMessage + { + [JsonProperty(PropertyName = "validator_node_identifier")] + public string ValidatorNodeIdentifier { get; set; } + } +} diff --git a/src/Thenewboston/Validator/Models/ValidatorResponseModel.cs b/src/Thenewboston/Validator/Api/Models/ValidatorResponseModel.cs similarity index 97% rename from src/Thenewboston/Validator/Models/ValidatorResponseModel.cs rename to src/Thenewboston/Validator/Api/Models/ValidatorResponseModel.cs index 9c4952b..54f78e7 100644 --- a/src/Thenewboston/Validator/Models/ValidatorResponseModel.cs +++ b/src/Thenewboston/Validator/Api/Models/ValidatorResponseModel.cs @@ -3,7 +3,7 @@ using System.Text; using Newtonsoft.Json; -namespace Thenewboston.Validator.Models +namespace Thenewboston.Validator.Api.Models { public class ValidatorResponseModel { diff --git a/src/Thenewboston/Validator/Api/PrimaryValidatorUpdatedService.cs b/src/Thenewboston/Validator/Api/PrimaryValidatorUpdatedService.cs new file mode 100644 index 0000000..166c421 --- /dev/null +++ b/src/Thenewboston/Validator/Api/PrimaryValidatorUpdatedService.cs @@ -0,0 +1,46 @@ +using System; +using System.Net.Http; +using System.Threading.Tasks; +using Newtonsoft.Json; +using Thenewboston.Common.Http; +using Thenewboston.Validator.Models; + +namespace Thenewboston.Validator.Api +{ + public class PrimaryValidatorUpdatedService : IPrimaryValidatorUpdatedService + { + private readonly IHttpRequestSender _requestSender; + + public PrimaryValidatorUpdatedService(IHttpRequestSender requestSender) + { + _requestSender = requestSender; + } + + /// + /// Notifies a validator on the network that a bank has upgraded its primary validator. + /// + /// + /// 200: Ok - Validator will follow bank and sync to the new primary validator or 400: + /// - Bad Request - The validator will not follow the bank and will remain on its current network + public async Task PostPrimaryValidatorUpdatedAsync(PrimaryValidatorUpdatedModel validatorUpdatedModel) + { + if(validatorUpdatedModel is null) + { + // TODO: Create specific exception + throw new Exception(); + } + + var httpContent = new StringContent(JsonConvert.SerializeObject(validatorUpdatedModel)); + var stringContent = await httpContent.ReadAsStringAsync(); + + if(string.IsNullOrEmpty(stringContent)) + { + // TODO: Create specific exception + throw new Exception(); + } + + var response = await _requestSender.PostAsync("primary_validator_updated", httpContent); + return response; + } + } +} diff --git a/src/Thenewboston/Validator/Api/UpgradeRequestService.cs b/src/Thenewboston/Validator/Api/UpgradeRequestService.cs new file mode 100644 index 0000000..8a551fe --- /dev/null +++ b/src/Thenewboston/Validator/Api/UpgradeRequestService.cs @@ -0,0 +1,48 @@ +using System; +using System.Net.Http; +using System.Text; +using System.Threading.Tasks; +using Newtonsoft.Json; +using Thenewboston.Common.Http; +using Thenewboston.Validator.Api.Models; +using Thenewboston.Validator.Models; + +namespace Thenewboston.Validator.Api +{ + public class UpgradeRequestService : IUpgradeRequestService + { + private readonly IHttpRequestSender _requestSender; + + public UpgradeRequestService(IHttpRequestSender requestSender) + { + _requestSender = requestSender; + } + + /// + /// Posts a new upgrade request to a confirmation validator requesting that it upgrade to a Primary Validator. + /// Requesting bank will pass a + /// + /// + /// NOTE: 200: Ok - Validator has accepted the upgrade request, 400: Bad Request - Validator has rejected the upgrade + public async Task PostUpgradeRequestAsync(UpgradeRequest upgradeRequest) + { + if(upgradeRequest is null) + { + // TODO: Create specific exception + throw new Exception(); + } + + var httpContent = new StringContent(JsonConvert.SerializeObject(upgradeRequest), Encoding.UTF8, "application/json"); + var jsonString = await httpContent.ReadAsStringAsync(); + + if (string.IsNullOrEmpty(jsonString)) + { + // TODO: Create specific exception + throw new Exception(); + } + + var response = await _requestSender.PostAsync("/upgrade_request/", httpContent); + return response; + } + } +} diff --git a/src/Thenewboston/Validator/Api/ValidatorConfirmationBlockService.cs b/src/Thenewboston/Validator/Api/ValidatorConfirmationBlockService.cs index 141b4fe..d4cfc2d 100644 --- a/src/Thenewboston/Validator/Api/ValidatorConfirmationBlockService.cs +++ b/src/Thenewboston/Validator/Api/ValidatorConfirmationBlockService.cs @@ -23,7 +23,7 @@ public ValidatorConfirmationBlockService(IHttpRequestSender requestSender) /// /// Message containing the validator's confirmation block info /// - public async Task PostConfiramtionBlockAsync(ConfirmationBlock confirmationBlockMessage) + public async Task PostConfirmationBlockAsync(ConfirmationBlock confirmationBlockMessage) { var jsonConfirmationBlockMessage = JsonConvert.SerializeObject(confirmationBlockMessage); var httpContent = new StringContent(jsonConfirmationBlockMessage, Encoding.UTF8, "application/json"); @@ -56,7 +56,7 @@ public async Task PostConfiramtionBlockAsync(Confirma /// The identifier for the requested block /// /// - public async Task GetQueuedConfiramtionBlockAsync(string blockIdentifier) + public async Task GetQueuedConfirmationBlockAsync(string blockIdentifier) { var response = await _requestSender.GetAsync($"/confirmation_block/{blockIdentifier}/queued"); diff --git a/src/Thenewboston/Validator/Api/ValidatorsService.cs b/src/Thenewboston/Validator/Api/ValidatorsService.cs index 30302b6..96676ea 100644 --- a/src/Thenewboston/Validator/Api/ValidatorsService.cs +++ b/src/Thenewboston/Validator/Api/ValidatorsService.cs @@ -1,11 +1,10 @@ using System; -using System.Collections.Generic; -using System.Text; using System.Threading.Tasks; using Newtonsoft.Json; using Thenewboston.Common.Api.Models; using Thenewboston.Common.Http; -using Thenewboston.Common.Models; +using Thenewboston.Validator.Api.Models; +using Thenewboston.Validator.Models; namespace Thenewboston.Validator.Api { @@ -18,9 +17,11 @@ public ValidatorsService(IHttpRequestSender requestSender) _requestSender = requestSender; } - public async Task GetAllValidatorsAsync() + public async Task> GetAllValidatorsAsync( + int offset = 0, + int limit = 10) { - var response = await _requestSender.GetAsync("/validators"); + var response = await _requestSender.GetAsync($"/validators?offset={offset}&limit={limit}"); if (!response.IsSuccessStatusCode) { @@ -36,7 +37,7 @@ public async Task GetAllValidatorsAsync() throw new Exception(); } - var result = JsonConvert.DeserializeObject(stringResult); + var result = JsonConvert.DeserializeObject>(stringResult); return result; } diff --git a/src/Thenewboston/Validator/Models/ConnectionRequest.cs b/src/Thenewboston/Validator/Models/ConnectionRequest.cs new file mode 100644 index 0000000..e811fc1 --- /dev/null +++ b/src/Thenewboston/Validator/Models/ConnectionRequest.cs @@ -0,0 +1,19 @@ +using System; +using System.Collections.Generic; +using System.Text; +using Newtonsoft.Json; + +namespace Thenewboston.Validator.Models +{ + public class ConnectionRequest + { + [JsonProperty(PropertyName = "message")] + public ConnectionRequestMessage Message { get; set; } + + [JsonProperty(PropertyName = "node_identifier")] + public string NodeIdentifier { get; set; } + + [JsonProperty(PropertyName = "signature")] + public string Signature { get; set; } + } +} diff --git a/src/Thenewboston/Validator/Models/ConnectionRequestMessage.cs b/src/Thenewboston/Validator/Models/ConnectionRequestMessage.cs new file mode 100644 index 0000000..a48232c --- /dev/null +++ b/src/Thenewboston/Validator/Models/ConnectionRequestMessage.cs @@ -0,0 +1,19 @@ +using System; +using System.Collections.Generic; +using System.Text; +using Newtonsoft.Json; + +namespace Thenewboston.Validator.Models +{ + public class ConnectionRequestMessage + { + [JsonProperty(PropertyName = "ip_address")] + public string IpAddress { get; set; } + + [JsonProperty(PropertyName = "port")] + public string Port { get; set; } + + [JsonProperty(PropertyName = "protocol")] + public string Protocol { get; set; } + } +} diff --git a/src/Thenewboston/Validator/Models/PrimaryValidatorUpdatedMessage.cs b/src/Thenewboston/Validator/Models/PrimaryValidatorUpdatedMessage.cs new file mode 100644 index 0000000..6cea70c --- /dev/null +++ b/src/Thenewboston/Validator/Models/PrimaryValidatorUpdatedMessage.cs @@ -0,0 +1,16 @@ +using Newtonsoft.Json; + +namespace Thenewboston.Validator.Models +{ + public class PrimaryValidatorUpdatedMessage + { + [JsonProperty(PropertyName ="ip_address")] + public string IPAddress { get; set; } + + [JsonProperty(PropertyName ="port")] + public string Port { get; set; } + + [JsonProperty(PropertyName ="protocol")] + public string Protocol { get; set; } + } +} diff --git a/src/Thenewboston/Validator/Models/PrimaryValidatorUpdatedModel.cs b/src/Thenewboston/Validator/Models/PrimaryValidatorUpdatedModel.cs new file mode 100644 index 0000000..8edc7f2 --- /dev/null +++ b/src/Thenewboston/Validator/Models/PrimaryValidatorUpdatedModel.cs @@ -0,0 +1,16 @@ +using Newtonsoft.Json; + +namespace Thenewboston.Validator.Models +{ + public class PrimaryValidatorUpdatedModel + { + [JsonProperty(PropertyName ="message")] + public PrimaryValidatorUpdatedMessage Message { get; set; } + + [JsonProperty(PropertyName ="property_name")] + public string NodeIdentifier { get; set; } + + [JsonProperty(PropertyName ="signature")] + public string Signature { get; set; } + } +} diff --git a/src/Thenewboston/Validator/Models/ValidatorBankBlock.cs b/src/Thenewboston/Validator/Models/ValidatorBankBlock.cs new file mode 100644 index 0000000..a1b3ed0 --- /dev/null +++ b/src/Thenewboston/Validator/Models/ValidatorBankBlock.cs @@ -0,0 +1,17 @@ +using Newtonsoft.Json; +using Thenewboston.Common.Models; + +namespace Thenewboston.Validator.Models +{ + public class ValidatorBankBlock + { + [JsonProperty(PropertyName ="block")] + public Block BankBlock { get; set; } + + [JsonProperty(PropertyName ="node_identifier")] + public string NodeIdentifier { get; set; } + + [JsonProperty(PropertyName ="signature")] + public string Signature { get; set; } + } +} \ No newline at end of file