From 55be07d711aaff8aa369d9d1089891a9a2f5ccaa Mon Sep 17 00:00:00 2001 From: benbierens Date: Wed, 20 Dec 2023 11:34:23 +0100 Subject: [PATCH] Implements getting slot host address --- .../CodexContractsAccess.cs | 31 +++++++++++++++++-- .../Marketplace/Customizations.cs | 13 ++++++++ ProjectPlugins/GethPlugin/EthAddress.cs | 11 +++++++ Tests/CodexTests/BasicTests/ExampleTests.cs | 5 ++- 4 files changed, 57 insertions(+), 3 deletions(-) create mode 100644 ProjectPlugins/CodexContractsPlugin/Marketplace/Customizations.cs diff --git a/ProjectPlugins/CodexContractsPlugin/CodexContractsAccess.cs b/ProjectPlugins/CodexContractsPlugin/CodexContractsAccess.cs index d6813b47..b5c920ff 100644 --- a/ProjectPlugins/CodexContractsPlugin/CodexContractsAccess.cs +++ b/ProjectPlugins/CodexContractsPlugin/CodexContractsAccess.cs @@ -1,6 +1,9 @@ using CodexContractsPlugin.Marketplace; using GethPlugin; using Logging; +using Nethereum.ABI; +using Nethereum.Util; +using NethereumWorkflow; using Utils; namespace CodexContractsPlugin @@ -16,6 +19,7 @@ namespace CodexContractsPlugin TestToken GetTestTokenBalance(EthAddress ethAddress); Request[] GetStorageRequests(TimeRange range); + EthAddress GetSlotHost(Request storageRequest, decimal slotIndex); } public class CodexContractsAccess : ICodexContracts @@ -62,12 +66,35 @@ namespace CodexContractsPlugin { var events = gethNode.GetEvents(Deployment.MarketplaceAddress, timeRange); var i = StartInteraction(); + return events - .Select(e => i.GetRequest(Deployment.MarketplaceAddress, e.Event.RequestId)) - .Select(r => r.ReturnValue1) + .Select(e => + { + var requestEvent = i.GetRequest(Deployment.MarketplaceAddress, e.Event.RequestId); + var request = requestEvent.ReturnValue1; + request.RequestId = e.Event.RequestId; + return request; + }) .ToArray(); } + public EthAddress GetSlotHost(Request storageRequest, decimal slotIndex) + { + var encoder = new ABIEncode(); + var encoded = encoder.GetABIEncoded( + new ABIValue("bytes32", storageRequest.RequestId), + new ABIValue("uint256", slotIndex.ToBig()) + ); + + var hashed = Sha3Keccack.Current.CalculateHash(encoded); + + var func = new GetHostFunction + { + SlotId = hashed + }; + return new EthAddress(gethNode.Call(Deployment.MarketplaceAddress, func)); + } + private ContractInteractions StartInteraction() { return new ContractInteractions(log, gethNode); diff --git a/ProjectPlugins/CodexContractsPlugin/Marketplace/Customizations.cs b/ProjectPlugins/CodexContractsPlugin/Marketplace/Customizations.cs new file mode 100644 index 00000000..d75d7129 --- /dev/null +++ b/ProjectPlugins/CodexContractsPlugin/Marketplace/Customizations.cs @@ -0,0 +1,13 @@ +#pragma warning disable CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider declaring as nullable. +using GethPlugin; + +namespace CodexContractsPlugin.Marketplace +{ + public partial class Request : RequestBase + { + public byte[] RequestId { get; set; } + + public EthAddress ClientAddress { get { return new EthAddress(Client); } } + } +} +#pragma warning restore CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider declaring as nullable. diff --git a/ProjectPlugins/GethPlugin/EthAddress.cs b/ProjectPlugins/GethPlugin/EthAddress.cs index 2954c488..bc660b6b 100644 --- a/ProjectPlugins/GethPlugin/EthAddress.cs +++ b/ProjectPlugins/GethPlugin/EthAddress.cs @@ -14,6 +14,17 @@ public string Address { get; } + public override bool Equals(object? obj) + { + return obj is EthAddress address && + Address == address.Address; + } + + public override int GetHashCode() + { + return HashCode.Combine(Address); + } + public override string ToString() { return Address; diff --git a/Tests/CodexTests/BasicTests/ExampleTests.cs b/Tests/CodexTests/BasicTests/ExampleTests.cs index 9b39af3a..5fe34efc 100644 --- a/Tests/CodexTests/BasicTests/ExampleTests.cs +++ b/Tests/CodexTests/BasicTests/ExampleTests.cs @@ -91,11 +91,14 @@ namespace CodexTests.BasicTests var requests = contracts.GetStorageRequests(GetTestRunTimeRange()); Assert.That(requests.Length, Is.EqualTo(1)); var request = requests.Single(); - Assert.That(request.Client, Is.EqualTo(buyer.EthAddress.Address)); + Assert.That(request.ClientAddress, Is.EqualTo(buyer.EthAddress)); Assert.That(request.Ask.Slots, Is.EqualTo(1)); AssertBalance(contracts, seller, Is.LessThan(sellerInitialBalance), "Collateral was not placed."); + var slotHost = contracts.GetSlotHost(request, 0); + Assert.That(slotHost, Is.EqualTo(seller.EthAddress)); + purchaseContract.WaitForStorageContractFinished(); AssertBalance(contracts, seller, Is.GreaterThan(sellerInitialBalance), "Seller was not paid for storage.");