Skip to content

Commit

Permalink
Merge pull request #13 from CirclesUBI/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
jaensen authored May 17, 2024
2 parents 013e612 + 05310c4 commit 7055380
Show file tree
Hide file tree
Showing 13 changed files with 85 additions and 96 deletions.
23 changes: 12 additions & 11 deletions Circles.Index.CirclesV2/DatabaseSchema.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Numerics;
using Circles.Index.Common;
using Nethermind.Core.Crypto;

Expand Down Expand Up @@ -178,9 +179,9 @@ public DatabaseSchema()
{ "logIndex", e => e.LogIndex },
{ "transactionHash", e => e.TransactionHash },
{ "human", e => e.Human },
{ "amount", e => e.Amount },
{ "startPeriod", e => e.StartPeriod },
{ "endPeriod", e => e.EndPeriod }
{ "amount", e => (BigInteger)e.Amount },
{ "startPeriod", e => (BigInteger)e.StartPeriod },
{ "endPeriod", e => (BigInteger)e.EndPeriod }
});

EventDtoTableMap.Add<RegisterGroup>(("CrcV2", "RegisterGroup"));
Expand Down Expand Up @@ -247,7 +248,7 @@ public DatabaseSchema()
{ "transactionHash", e => e.TransactionHash },
{ "truster", e => e.Truster },
{ "trustee", e => e.Trustee },
{ "expiryTime", e => e.ExpiryTime }
{ "expiryTime", e => (BigInteger)e.ExpiryTime }
});

EventDtoTableMap.Add<ApprovalForAll>(("CrcV2", "ApprovalForAll"));
Expand Down Expand Up @@ -276,8 +277,8 @@ public DatabaseSchema()
{ "operator", e => e.Operator },
{ "from", e => e.From },
{ "to", e => e.To },
{ "id", e => e.Id },
{ "value", e => e.Value }
{ "id", e => (BigInteger)e.Id },
{ "value", e => (BigInteger)e.Value }
});

EventDtoTableMap.Add<TransferBatch>(("CrcV2", "TransferBatch"));
Expand All @@ -292,8 +293,8 @@ public DatabaseSchema()
{ "operator", e => e.Operator },
{ "from", e => e.From },
{ "to", e => e.To },
{ "id", e => e.Id },
{ "value", e => e.Value }
{ "id", e => (BigInteger)e.Id },
{ "value", e => (BigInteger)e.Value }
});

EventDtoTableMap.Add<URI>(("CrcV2", "URI"));
Expand All @@ -306,7 +307,7 @@ public DatabaseSchema()
{ "logIndex", e => e.LogIndex },
{ "transactionHash", e => e.TransactionHash },
{ "value", e => e.Value },
{ "id", e => e.Id }
{ "id", e => (BigInteger)e.Id }
});

EventDtoTableMap.Add<DiscountCost>(("CrcV2", "DiscountCost"));
Expand All @@ -319,8 +320,8 @@ public DatabaseSchema()
{ "logIndex", e => e.LogIndex },
{ "transactionHash", e => e.TransactionHash },
{ "account", e => e.Account },
{ "id", e => e.Id },
{ "discountCost", e => e._DiscountCost }
{ "id", e => (BigInteger)e.Id },
{ "discountCost", e => (BigInteger)e._DiscountCost }
});
}
}
16 changes: 6 additions & 10 deletions Circles.Index.CirclesV2/LogParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -137,8 +137,8 @@ private TransferSingle Erc1155TransferSingle(Block block, TxReceipt receipt, Log
string operatorAddress = "0x" + log.Topics[1].ToString().Substring(Consts.AddressEmptyBytesPrefixLength);
string fromAddress = "0x" + log.Topics[2].ToString().Substring(Consts.AddressEmptyBytesPrefixLength);
string toAddress = "0x" + log.Topics[3].ToString().Substring(Consts.AddressEmptyBytesPrefixLength);
UInt256 id = new UInt256(log.Topics[4].Bytes, true);
UInt256 value = new UInt256(log.Data, true);
UInt256 id = new UInt256(log.Data.Slice(0, 32), true);
UInt256 value = new UInt256(log.Data.Slice(32), true);

return new TransferSingle(
block.Number,
Expand Down Expand Up @@ -186,7 +186,7 @@ private RegisterOrganization CrcV2RegisterOrganization(Block block, TxReceipt re
int logIndex)
{
string orgAddress = "0x" + log.Topics[1].ToString().Substring(Consts.AddressEmptyBytesPrefixLength);
string orgName = Encoding.UTF8.GetString(log.Data);
string orgName = LogDataStringDecoder.ReadStrings(log.Data)[0];

return new RegisterOrganization(
block.Number,
Expand All @@ -204,13 +204,9 @@ private RegisterGroup CrcV2RegisterGroup(Block block, TxReceipt receipt, LogEntr
string mintPolicy = "0x" + log.Topics[2].ToString().Substring(Consts.AddressEmptyBytesPrefixLength);
string treasury = "0x" + log.Topics[3].ToString().Substring(Consts.AddressEmptyBytesPrefixLength);

int nameOffset = (int)new BigInteger(log.Data.Slice(0, 32).ToArray());
int nameLength = (int)new BigInteger(log.Data.Slice(nameOffset, 32).ToArray());
string groupName = Encoding.UTF8.GetString(log.Data.Slice(nameOffset + 32, nameLength));

int symbolOffset = (int)new BigInteger(log.Data.Slice(32, 32).ToArray());
int symbolLength = (int)new BigInteger(log.Data.Slice(symbolOffset, 32).ToArray());
string groupSymbol = Encoding.UTF8.GetString(log.Data.Slice(symbolOffset + 32, symbolLength));
string[] stringData = LogDataStringDecoder.ReadStrings(log.Data);
string groupName = stringData[0];
string groupSymbol = stringData[1];

return new RegisterGroup(
block.Number,
Expand Down
1 change: 0 additions & 1 deletion Circles.Index.Common/IDatabase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ public string QuoteIdentifier(string identifier)
}

IDbDataParameter CreateParameter(string? name, object? value);
public object? Convert(object? input, ValueTypes target);
}

public interface IDatabase : IDatabaseUtils
Expand Down
46 changes: 46 additions & 0 deletions Circles.Index.Common/LogDataStringDecoder.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
using System.Text;

namespace Circles.Index.Common;

public static class LogDataStringDecoder
{
public static string[] ReadStrings(byte[] bytes)
{
var strings = new List<string>();
var offset = 0;
do
{
offset = ReadString(bytes, strings, offset, out var length, out var str);

// Align the length of 'name' to 32 bytes
length = (length + 31) / 32 * 32;
// Take the last offset and the length and determine the next string offset aligned to 32 bytes
offset = offset + 32 + length;
} while (offset < bytes.Length);

return strings.ToArray();
}

private static int ReadString(byte[] bytes, List<string> strings, int offset, out int length, out string name)
{
// The first 32 bytes contain the offset of the first string
var val1 = BitConverter.ToInt32(bytes.Skip(offset).Take(32).Reverse().ToArray(), 0);
// At the beginning of the string data, the length of the string is stored
if (offset > 0)
{
length = val1;
}
else
{
offset = val1;
length = BitConverter.ToInt32(bytes.Skip(offset).Take(32).Reverse().ToArray(), 0);
}

// The string data is stored after the length
byte[] stringData = bytes.Skip(offset + 32).Take(length).ToArray();
name = Encoding.UTF8.GetString(stringData).Split('\0')[0];
strings.Add(name);

return offset;
}
}
2 changes: 1 addition & 1 deletion Circles.Index.Common/Range.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace Circles.Index.Data;
namespace Circles.Index.Common;

public class Range<T>
{
Expand Down
7 changes: 0 additions & 7 deletions Circles.Index.Common/SortOrder.cs

This file was deleted.

45 changes: 0 additions & 45 deletions Circles.Index.Postgres/PostgresDb.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
using System.Data;
using System.Numerics;
using System.Text;
using Circles.Index.Common;
using Nethermind.Core.Crypto;
using Nethermind.Int256;
using Npgsql;
using NpgsqlTypes;

Expand Down Expand Up @@ -314,47 +312,4 @@ public async Task DeleteFromBlockOnwards(long reorgAt)
throw;
}
}

public object? Convert(object? input, ValueTypes target)
{
if (input == null)
{
return null;
}

switch (target)
{
case ValueTypes.String:
return input.ToString() ?? throw new ArgumentNullException(nameof(input));
case ValueTypes.Int:
return System.Convert.ToInt64(input.ToString());
case ValueTypes.BigInt when input is string i:
return BigInteger.Parse(i);
case ValueTypes.BigInt when input is BigInteger:
return input;
case ValueTypes.BigInt when input is UInt256:
case ValueTypes.BigInt when input is ulong:
case ValueTypes.BigInt when input is uint:
case ValueTypes.BigInt when input is long:
case ValueTypes.BigInt when input is int:
return (BigInteger)input;
case ValueTypes.BigInt:
return BigInteger.Parse(input.ToString() ?? throw new ArgumentNullException(nameof(input)));
case ValueTypes.Address when input is string i:
return i.ToLowerInvariant();
case ValueTypes.Address:
return input.ToString()?.ToLowerInvariant();
case ValueTypes.Boolean when input is bool b:
return b;
case ValueTypes.Boolean when input is string s:
return bool.Parse(s);
case ValueTypes.Boolean when input is int i:
return i != 0;
case ValueTypes.Bytes when input is byte[] b:
return b;
default:
throw new ArgumentOutOfRangeException(nameof(target), target,
$"Cannot convert input {input} (type: {input.GetType().Name}) to target type {target}.");
}
}
}
20 changes: 15 additions & 5 deletions Circles.Index.Query.Tests/Test.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,6 @@ public IDbDataParameter CreateParameter(string? name, object? value)
{
return new TestDbDataParameter(name, value);
}

public object? Convert(object? input, ValueTypes target)
{
throw new NotImplementedException();
}
}

public class TestDbDataParameter : IDbDataParameter
Expand Down Expand Up @@ -75,6 +70,21 @@ public void Setup()
{
}


[Test]
public void ParseStringsFromData()
{
var hexString =
"00000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000080000000000000000000000000000000000000000000000000000000000000000b50657465722047726f757000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000035054520000000000000000000000000000000000000000000000000000000000";

var bytes = Convert.FromHexString(hexString);

var strings = LogDataStringDecoder.ReadStrings(bytes);

Assert.That(strings[0], Is.EqualTo("Peter Group"));
Assert.That(strings[1], Is.EqualTo("PTR"));
}

[Test]
public void FilterPredicate_ToSql_Equals()
{
Expand Down
9 changes: 4 additions & 5 deletions Circles.Index/BlockIndexer.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System.Threading.Tasks.Dataflow;
using Circles.Index.Common;
using Circles.Index.Data;
using Nethermind.Blockchain;
using Nethermind.Blockchain.Receipts;
using Nethermind.Core;
Expand All @@ -16,7 +15,7 @@ public class ImportFlow(
{
private static readonly IndexPerformanceMetrics Metrics = new();

public readonly InsertBuffer<Block> BlockBuffer = new();
private readonly InsertBuffer<Block> _blockBuffer = new();

private ExecutionDataflowBlockOptions CreateOptions(
CancellationToken cancellationToken
Expand Down Expand Up @@ -127,17 +126,17 @@ public async Task<Range<long>> Run(IAsyncEnumerable<long> blocksToIndex, Cancell

private async Task AddBlock(Block block)
{
BlockBuffer.Add(block);
_blockBuffer.Add(block);

if (BlockBuffer.Length >= context.Settings.BlockBufferSize)
if (_blockBuffer.Length >= context.Settings.BlockBufferSize)
{
await FlushBlocks();
}
}

public async Task FlushBlocks()
{
var blocks = BlockBuffer.TakeSnapshot();
var blocks = _blockBuffer.TakeSnapshot();

var map = new SchemaPropertyMap();
map.Add(("System", "Block"), new Dictionary<string, Func<Block, object?>>
Expand Down
1 change: 0 additions & 1 deletion Circles.Index/Circles.Index.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
<Product>Circles</Product>
<AssemblyVersion>1.0</AssemblyVersion>
<FileVersion>1.0</FileVersion>
<OutputType>Exe</OutputType>
</PropertyGroup>


Expand Down
8 changes: 0 additions & 8 deletions Circles.Index/Program.cs

This file was deleted.

1 change: 0 additions & 1 deletion Circles.Index/StateMachine.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using Circles.Index.Common;
using Circles.Index.Data;
using Nethermind.Blockchain;
using Nethermind.Blockchain.Receipts;

Expand Down
2 changes: 1 addition & 1 deletion add-deploy-script-tp-v2-repo.sh
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ cat << 'EOF' > ./circles-contracts-v2/script/deployments/genericDeploy.sh
MIGRATION=$(deploy_and_store_details "Migration" $MIGRATION_ADDRESS_02 \
src/migration/Migration.sol:Migration \
--constructor-args $V1_HUB_ADDRESS $HUB_ADDRESS_01)
--constructor-args $V1_HUB_ADDRESS $HUB_ADDRESS_01 $INFLATION_DAY_ZERO)
NAME_REGISTRY=$(deploy_and_store_details "NameRegistry" $NAMEREGISTRY_ADDRESS_03 \
src/names/NameRegistry.sol:NameRegistry \
Expand Down

0 comments on commit 7055380

Please sign in to comment.