Skip to content

Commit

Permalink
Fixes proof-period message in chain-events. Fixes asserts in marketpl…
Browse files Browse the repository at this point in the history
…ace tests.
  • Loading branch information
benbierens committed Mar 5, 2025
1 parent 25a7a30 commit c421fab
Show file tree
Hide file tree
Showing 7 changed files with 163 additions and 44 deletions.
35 changes: 35 additions & 0 deletions Framework/Utils/EthTokenExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,41 @@ public override string ToString()

return string.Join(" + ", tokens);
}

public static Ether operator +(Ether a, Ether b)
{
return new Ether(a.Wei + b.Wei);
}

public static Ether operator -(Ether a, Ether b)
{
return new Ether(a.Wei - b.Wei);
}

public static Ether operator *(Ether a, int b)
{
return new Ether(a.Wei * b);
}

public static bool operator <(Ether a, Ether b)
{
return a.Wei < b.Wei;
}

public static bool operator >(Ether a, Ether b)
{
return a.Wei > b.Wei;
}

public static bool operator ==(Ether a, Ether b)
{
return a.Wei == b.Wei;
}

public static bool operator !=(Ether a, Ether b)
{
return a.Wei != b.Wei;
}
}

public static class TokensIntExtensions
Expand Down
23 changes: 5 additions & 18 deletions ProjectPlugins/CodexContractsPlugin/ChainMonitor/PeriodMonitor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,24 +86,11 @@ public PeriodMonitorResult(PeriodReport[] reports)

private void CalcStats()
{
IsEmpty = true;
PeriodLow = ulong.MaxValue;
PeriodHigh = ulong.MinValue;
AverageNumSlots = 0.0f;
AverageNumProofsRequired = 0.0f;
float count = Reports.Length;

foreach (var report in Reports)
{
if (report.TotalProofsRequired > 0) IsEmpty = false;
PeriodLow = Math.Min(PeriodLow, report.PeriodNumber);
PeriodHigh = Math.Min(PeriodHigh, report.PeriodNumber);
AverageNumSlots += Convert.ToSingle(report.TotalNumSlots);
AverageNumProofsRequired += Convert.ToSingle(report.TotalProofsRequired);
}

AverageNumSlots = AverageNumSlots / count;
AverageNumProofsRequired = AverageNumProofsRequired / count;
IsEmpty = Reports.Any(r => r.TotalProofsRequired > 0);
PeriodLow = Reports.Min(r => r.PeriodNumber);
PeriodHigh = Reports.Max(r => r.PeriodNumber);
AverageNumSlots = Reports.Average(r => Convert.ToSingle(r.TotalNumSlots));
AverageNumProofsRequired = Reports.Average(r => Convert.ToSingle(r.TotalProofsRequired));
}
}

Expand Down
22 changes: 11 additions & 11 deletions ProjectPlugins/CodexContractsPlugin/CodexContractsAccess.cs
Original file line number Diff line number Diff line change
Expand Up @@ -105,17 +105,6 @@ public ICodexContractsEvents GetEvents(BlockInterval blockInterval)
return new CodexContractsEvents(log, gethNode, Deployment, blockInterval);
}

public byte[] GetSlotId(Request request, decimal slotIndex)
{
var encoder = new ABIEncode();
var encoded = encoder.GetABIEncoded(
new ABIValue("bytes32", request.RequestId),
new ABIValue("uint256", slotIndex.ToBig())
);

return Sha3Keccack.Current.CalculateHash(encoded);
}

public EthAddress? GetSlotHost(Request storageRequest, decimal slotIndex)
{
var slotId = GetSlotId(storageRequest, slotIndex);
Expand Down Expand Up @@ -166,6 +155,17 @@ public ProofState GetProofState(Request storageRequest, decimal slotIndex, ulong
return new ProofState(required, missing);
}

private byte[] GetSlotId(Request request, decimal slotIndex)
{
var encoder = new ABIEncode();
var encoded = encoder.GetABIEncoded(
new ABIValue("bytes32", request.RequestId),
new ABIValue("uint256", slotIndex.ToBig())
);

return Sha3Keccack.Current.CalculateHash(encoded);
}

private bool IsProofRequired(byte[] slotId, ulong blockNumber)
{
var func = new IsProofRequiredFunction
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,25 +78,23 @@ public ICodexNodeGroup StartHosts()
return hosts;
}

public void AssertTstBalance(EthAddress address, TestToken expectedBalance, string message)
public void AssertTstBalance(ICodexNode node, TestToken expectedBalance, string message)
{
var retry = GetBalanceAssertRetry();
retry.Run(() =>
{
var balance = GetTstBalance(address);

Assert.That(balance, Is.EqualTo(expectedBalance), message);
});
AssertTstBalance(node.EthAddress, expectedBalance, message);
}

public void AssertTstBalance(ICodexNode node, TestToken expectedBalance, string message)
public void AssertTstBalance(EthAddress address, TestToken expectedBalance, string message)
{
var retry = GetBalanceAssertRetry();
retry.Run(() =>
{
var balance = GetTstBalance(node);
var balance = GetTstBalance(address);

Assert.That(balance, Is.EqualTo(expectedBalance), message);
if (balance != expectedBalance)
{
throw new Exception(nameof(AssertTstBalance) +
$" expected: {expectedBalance} but was: {balance} - message: " + message);
}
});
}

Expand All @@ -107,14 +105,18 @@ public void AssertEthBalance(ICodexNode node, Ether expectedBalance, string mess
{
var balance = GetEthBalance(node);

Assert.That(balance, Is.EqualTo(expectedBalance), message);
if (balance != expectedBalance)
{
throw new Exception(nameof(AssertEthBalance) +
$" expected: {expectedBalance} but was: {balance} - message: " + message);
}
});
}

private Retry GetBalanceAssertRetry()
{
return new Retry("AssertBalance",
maxTimeout: TimeSpan.FromMinutes(30.0),
maxTimeout: TimeSpan.FromMinutes(10.0),
sleepAfterFail: TimeSpan.FromSeconds(10.0),
onFail: f => { });
}
Expand Down Expand Up @@ -217,7 +219,10 @@ protected void AssertHostsCollateralsAreUnchanged(ICodexNodeGroup hosts)
var retry = GetBalanceAssertRetry();
retry.Run(() =>
{
Assert.That(GetTstBalance(host), Is.GreaterThanOrEqualTo(StartingBalanceTST.Tst()));
if (GetTstBalance(host) < StartingBalanceTST.Tst())
{
throw new Exception(nameof(AssertHostsCollateralsAreUnchanged));
}
});
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
using NUnit.Framework;
using Utils;

namespace FrameworkTests.CodexContractsPlugin
{
[TestFixture]
public class TestTokenEqualityTests
{
[Test]
[Combinatorial]
public void Equal(
[Values(1, 22, 333, 4444, 55555)] int amount,
[Values(true, false)] bool isWei
)
{
var amount1 = CreateTst(amount, isWei);
var amount2 = CreateTst(amount, isWei);

Assert.That(amount1, Is.EqualTo(amount2));
Assert.That(amount1 == amount2);
Assert.That(!(amount1 != amount2));
}

[Test]
[Combinatorial]
public void NotEqual(
[Values(22, 333, 4444, 55555)] int amount,
[Values(true, false)] bool isWei,
[Values(1, 2, 10, -1, -2, -10)] int deltaWei
)
{
var amount1 = CreateTst(amount, isWei);
var amount2 = CreateTst(amount, isWei) + deltaWei.TstWei();

Assert.That(amount1, Is.Not.EqualTo(amount2));
Assert.That(amount1 != amount2);
Assert.That(!(amount1 == amount2));
}

private TestToken CreateTst(int amount, bool isWei)
{
if (isWei) return amount.TstWei();
return amount.Tst();
}
}
}
46 changes: 46 additions & 0 deletions Tests/FrameworkTests/Utils/EtherEqualityTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
using NUnit.Framework;
using Utils;

namespace FrameworkTests.Utils
{
[TestFixture]
public class EtherEqualityTests
{
[Test]
[Combinatorial]
public void Equal(
[Values(1, 22, 333, 4444, 55555)] int amount,
[Values(true, false)] bool isWei
)
{
var amount1 = CreateEth(amount, isWei);
var amount2 = CreateEth(amount, isWei);

Assert.That(amount1, Is.EqualTo(amount2));
Assert.That(amount1 == amount2);
Assert.That(!(amount1 != amount2));
}

[Test]
[Combinatorial]
public void NotEqual(
[Values(22, 333, 4444, 55555)] int amount,
[Values(true, false)] bool isWei,
[Values(1, 2, 10, -1, -2, -10)] int deltaWei
)
{
var amount1 = CreateEth(amount, isWei);
var amount2 = CreateEth(amount, isWei) + deltaWei.Wei();

Assert.That(amount1, Is.Not.EqualTo(amount2));
Assert.That(amount1 != amount2);
Assert.That(!(amount1 == amount2));
}

private Ether CreateEth(int amount, bool isWei)
{
if (isWei) return amount.Wei();
return amount.Eth();
}
}
}
2 changes: 1 addition & 1 deletion Tools/TestNetRewarder/EmojiMaps.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public class EmojiMaps
public string Failed => "❌";
public string ProofSubmitted => "🎵";
public string ProofReport => "🔎";
public string NoProofsMissed => "🏛";
public string NoProofsMissed => "🎉";
public string ManyProofsMissed => "😱";

public string StringToEmojis(string input, int outLength)
Expand Down

0 comments on commit c421fab

Please sign in to comment.