Skip to content

Commit

Permalink
Merge pull request #17 from CirclesUBI/feature/index-cidv0-event
Browse files Browse the repository at this point in the history
Also index the NameRegistries' CidV0 event
  • Loading branch information
jaensen authored May 18, 2024
2 parents c12a874 + 383d2ea commit 57701ed
Show file tree
Hide file tree
Showing 8 changed files with 146 additions and 47 deletions.
20 changes: 20 additions & 0 deletions Circles.Index.CirclesV2.NameRegistry/DatabaseSchema.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ public class DatabaseSchema : IDatabaseSchema
public static readonly EventSchema UpdateMetadataDigest = EventSchema.FromSolidity("CrcV2",
"event UpdateMetadataDigest(address indexed avatar, bytes32 metadataDigest)");

public static readonly EventSchema CidV0 = EventSchema.FromSolidity("CrcV2",
"event CidV0(address indexed avatar, bytes32 cidV0Digest)");

public IDictionary<(string Namespace, string Table), EventSchema> Tables { get; } =
new Dictionary<(string Namespace, string Table), EventSchema>
{
Expand All @@ -25,6 +28,10 @@ public class DatabaseSchema : IDatabaseSchema
{
("CrcV2", "UpdateMetadataDigest"),
UpdateMetadataDigest
},
{
("CrcV2", "CidV0"),
CidV0
}
};

Expand Down Expand Up @@ -56,5 +63,18 @@ public DatabaseSchema()
{ "avatar", e => e.Avatar },
{ "metadataDigest", e => e.MetadataDigest }
});

EventDtoTableMap.Add<CidV0>(("CrcV2", "CidV0"));
SchemaPropertyMap.Add(("CrcV2", "CidV0"),
new Dictionary<string, Func<CidV0, object?>>
{
{ "blockNumber", e => e.BlockNumber },
{ "timestamp", e => e.Timestamp },
{ "transactionIndex", e => e.TransactionIndex },
{ "logIndex", e => e.LogIndex },
{ "transactionHash", e => e.TransactionHash },
{ "avatar", e => e.Avatar },
{ "cidV0Digest", e => e.CidV0Digest }
});
}
}
11 changes: 10 additions & 1 deletion Circles.Index.CirclesV2.NameRegistry/Events.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,13 @@ public record UpdateMetadataDigest(
int LogIndex,
string TransactionHash,
string Avatar,
byte[] MetadataDigest) : IIndexEvent;
byte[] MetadataDigest) : IIndexEvent;

public record CidV0(
long BlockNumber,
long Timestamp,
int TransactionIndex,
int LogIndex,
string TransactionHash,
string Avatar,
byte[] CidV0Digest) : IIndexEvent;
29 changes: 26 additions & 3 deletions Circles.Index.CirclesV2.NameRegistry/LogParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ namespace Circles.Index.CirclesV2.NameRegistry;

public class LogParser(Address nameRegistryAddress) : ILogParser
{
private readonly Hash256 _registerShortNameTopic = new Hash256(DatabaseSchema.RegisterShortName.Topic);
private readonly Hash256 _updateMetadataDigestTopic = new Hash256(DatabaseSchema.UpdateMetadataDigest.Topic);
private readonly Hash256 _registerShortNameTopic = new(DatabaseSchema.RegisterShortName.Topic);
private readonly Hash256 _updateMetadataDigestTopic = new(DatabaseSchema.UpdateMetadataDigest.Topic);
private readonly Hash256 _cidV0Topic = new(DatabaseSchema.CidV0.Topic);

public IEnumerable<IIndexEvent> ParseLog(Block block, TxReceipt receipt, LogEntry log, int logIndex)
{
Expand All @@ -17,12 +18,15 @@ public IEnumerable<IIndexEvent> ParseLog(Block block, TxReceipt receipt, LogEntr
yield break;
}

var topic = log.Topics[0];
if (log.LoggersAddress != nameRegistryAddress)
{
yield break;
}

Console.WriteLine($"Event from NameRegistry: {log.Topics[0]}");

var topic = log.Topics[0];

if (topic == _registerShortNameTopic)
{
yield return RegisterShortName(block, receipt, log, logIndex);
Expand All @@ -32,6 +36,11 @@ public IEnumerable<IIndexEvent> ParseLog(Block block, TxReceipt receipt, LogEntr
{
yield return UpdateMetadataDigest(block, receipt, log, logIndex);
}

if (topic == _cidV0Topic)
{
yield return CidV0(block, receipt, log, logIndex);
}
}

private UpdateMetadataDigest UpdateMetadataDigest(Block block, TxReceipt receipt, LogEntry log, int logIndex)
Expand Down Expand Up @@ -67,4 +76,18 @@ private RegisterShortName RegisterShortName(Block block, TxReceipt receipt, LogE
shortName,
nonce);
}

private CidV0 CidV0(Block block, TxReceipt receipt, LogEntry log, int logIndex)
{
string avatar = "0x" + log.Topics[1].ToString().Substring(Consts.AddressEmptyBytesPrefixLength);

return new CidV0(
block.Number,
(long)block.Timestamp,
receipt.Index,
logIndex,
receipt.TxHash!.ToString(),
avatar,
log.Data);
}
}
21 changes: 10 additions & 11 deletions Circles.Index.Common/Settings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,24 @@ public class Settings
{
public readonly Address CirclesV1HubAddress = Environment.GetEnvironmentVariable("V1_HUB_ADDRESS") != null
? new(Environment.GetEnvironmentVariable("V1_HUB_ADDRESS")!)
: new("0x29b9a7fBb8995b2423a71cC17cf9810798F6C543");
: throw new Exception("V1_HUB_ADDRESS is not set.");

public readonly Address CirclesV2HubAddress = Environment.GetEnvironmentVariable("V2_HUB_ADDRESS") != null
? new(Environment.GetEnvironmentVariable("V2_HUB_ADDRESS")!)
: new("0x29b9a7fBb8995b2423a71cC17cf9810798F6C543");
: throw new Exception("V2_HUB_ADDRESS is not set.");

public readonly Address CirclesNameRegistryAddress = Environment.GetEnvironmentVariable("V2_NAME_REGISTRY_ADDRESS") != null
? new(Environment.GetEnvironmentVariable("V2_NAME_REGISTRY_ADDRESS")!)
: throw new Exception("V2_NAME_REGISTRY_ADDRESS is not set.");

public readonly string IndexDbConnectionString =
Environment.GetEnvironmentVariable("POSTGRES_CONNECTION_STRING")
?? "Server=postgres;Port=5432;User Id=postgres;Password=postgres;Database=postgres;";
?? throw new Exception("POSTGRES_CONNECTION_STRING is not set.");

public readonly int BlockBufferSize = 20000;
public readonly int EventBufferSize = 100000;

public readonly long StartBlock = Environment.GetEnvironmentVariable("START_BLOCK") != null
? long.Parse(Environment.GetEnvironmentVariable("START_BLOCK")!)
: 12541946L;

public readonly Address CirclesV2NameRegistryAddress =
Environment.GetEnvironmentVariable("V2_NAME_REGISTRY_ADDRESS") != null
? new(Environment.GetEnvironmentVariable("V2_NAME_REGISTRY_ADDRESS")!)
: new("0x29b9a7fBb8995b2423a71cC17cf9810798F6C543");
// public readonly long StartBlock = Environment.GetEnvironmentVariable("START_BLOCK") != null
// ? long.Parse(Environment.GetEnvironmentVariable("START_BLOCK")!)
// : 12541946L;
}
1 change: 1 addition & 0 deletions Circles.Index.Postgres/PostgresDb.cs
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,7 @@ private NpgsqlDbType GetNpgsqlDbType(ValueTypes type)
ValueTypes.String => NpgsqlDbType.Text,
ValueTypes.Address => NpgsqlDbType.Text,
ValueTypes.Boolean => NpgsqlDbType.Boolean,
ValueTypes.Bytes => NpgsqlDbType.Bytea,
_ => throw new ArgumentException("Unsupported type")
};
}
Expand Down
13 changes: 11 additions & 2 deletions Circles.Index/Plugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using Circles.Index.Rpc;
using Nethermind.Api;
using Nethermind.Api.Extensions;
using Nethermind.Core.Extensions;
using Nethermind.JsonRpc.Modules;
using Nethermind.Logging;

Expand Down Expand Up @@ -35,12 +36,20 @@ public async Task Init(INethermindApi nethermindApi)

ILogger baseLogger = nethermindApi.LogManager.GetClassLogger();
ILogger pluginLogger = new LoggerWithPrefix($"{Name}: ", baseLogger);

// Log all indexed events
pluginLogger.Info("Indexing events:");
foreach (var databaseSchemaTable in databaseSchema.Tables)
{
pluginLogger.Info($" * Topic: {databaseSchemaTable.Value.Topic.ToHexString()}; Name: {databaseSchemaTable.Key.Namespace}_{databaseSchemaTable.Key.Table}");
}

Settings settings = new();
pluginLogger.Info("Index Db connection string: " + settings.IndexDbConnectionString);
pluginLogger.Info("V1 Hub address: " + settings.CirclesV1HubAddress);
pluginLogger.Info("V2 Hub address: " + settings.CirclesV2HubAddress);
pluginLogger.Info("Start index from: " + settings.StartBlock);
pluginLogger.Info("V2 Name Registry address: " + settings.CirclesNameRegistryAddress);
// pluginLogger.Info("Start index from: " + settings.StartBlock);

IDatabase database = new PostgresDb(settings.IndexDbConnectionString, databaseSchema);
database.Migrate();
Expand All @@ -59,7 +68,7 @@ public async Task Init(INethermindApi nethermindApi)
[
new CirclesV1.LogParser(settings.CirclesV1HubAddress),
new CirclesV2.LogParser(settings.CirclesV2HubAddress),
new CirclesV2.NameRegistry.LogParser(settings.CirclesV2NameRegistryAddress)
new CirclesV2.NameRegistry.LogParser(settings.CirclesNameRegistryAddress)
];

_indexerContext = new Context(
Expand Down
95 changes: 66 additions & 29 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,6 @@ Spaceneth does not support these instructions.
evm_version = 'cancun'
```


Now you can deploy the contracts to the spaceneth node.

```bash
Expand Down Expand Up @@ -358,6 +357,8 @@ Namespaces and tables:
* `Trust`
* `UpdateMetadataDigest`
* `URI`
* `V_CrcV2`
* `Transfers` (view combining `TransferBatch` and `TransferSingle`)

#### Available filter types

Expand All @@ -379,7 +380,9 @@ You can use the combination of `blockNumber`, `transactionIndex` and `logIndex`

#### Example

Query the last two Circles signups:
##### Get the transaction history of a wallet

Query the 10 most recent Circles V2 transfers from or to an address showing the most recent first:

```shell
curl -X POST --data '{
Expand All @@ -388,28 +391,51 @@ curl -X POST --data '{
"method": "circles_query",
"params": [
{
"Namespace": "CrcV1",
"Table": "Signup",
"Limit": 2,
"Namespace": "V_CrcV2",
"Table": "Transfers",
"Limit": 10,
"Columns": [],
"Filter": [],
"Filter": [
{
"Type": "Conjunction",
"ConjunctionType": "Or",
"Predicates": [
{
"Type": "FilterPredicate",
"FilterType": "Equals",
"Column": "from",
"Value": "0xae3a29a9ff24d0e936a5579bae5c4179c4dff565"
},
{
"Type": "FilterPredicate",
"FilterType": "Equals",
"Column": "to",
"Value": "0xae3a29a9ff24d0e936a5579bae5c4179c4dff565"
}
]
}
],
"Order": [
{
"Column": "blockNumber",
"SortOrder": "ASC"
"SortOrder": "DESC"
},
{
"Column": "transactionIndex",
"SortOrder": "ASC"
"SortOrder": "DESC"
},
{
"Column": "logIndex",
"SortOrder": "ASC"
"SortOrder": "DESC"
},
{
"Column": "batchIndex",
"SortOrder": "DESC"
}
]
}
]
}' -H "Content-Type: application/json" https://localhost:8545/
}' -H "Content-Type: application/json" http://localhost:8545/
```

##### Response:
Expand All @@ -418,37 +444,48 @@ curl -X POST --data '{
{
"jsonrpc": "2.0",
"result": {
"columns": [
"Columns": [
"blockNumber",
"timestamp",
"transactionIndex",
"logIndex",
"batchIndex",
"transactionHash",
"user",
"token"
"operator",
"from",
"to",
"id",
"value"
],
"rows": [
"Rows": [
[
"0x597343",
"0x64f5aa5a",
"0x0",
"0x3",
"0xb41462160f73af912b550b27a7ed31e091d5da6c59a6325b367048ea42eef47f",
"0x4bc38a9f15508d19299a45b063556ec4bee853ff",
"0xcc724001786fcf8414747dd598e8e9383882b6d7"
9817761,
1715899520,
0,
0,
0,
"0x3b3f9cfebdd164bf53cfcb1fe4c163f388712f566576edf6ac1f2c55da95929a",
"0xae3a29a9ff24d0e936a5579bae5c4179c4dff565",
"0x0000000000000000000000000000000000000000",
"0xae3a29a9ff24d0e936a5579bae5c4179c4dff565",
"994661466795450363997821247051269595921846891877",
"4000000000000000000"
],
[
"0x597343",
"0x64f5aa5a",
"0x0",
"0x3",
"0xb41462160f73af912b550b27a7ed31e091d5da6c59a6325b367048ea42eef47f",
"0x4bc38a9f15508d19299a45b063556ec4bee853ff",
"0xcc724001786fcf8414747dd598e8e9383882b6d7"
9817761,
1715899520,
0,
0,
0,
"0x3b3f9cfebdd164bf53cfcb1fe4c163f388712f566576edf6ac1f2c55da95929a",
"0xae3a29a9ff24d0e936a5579bae5c4179c4dff565",
"0x0000000000000000000000000000000000000000",
"0xae3a29a9ff24d0e936a5579bae5c4179c4dff565",
"994661466795450363997821247051269595921846891877",
"4000000000000000000"
]
]
},
"id": 1
}

```
3 changes: 2 additions & 1 deletion docker-compose.chiado.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ services:
environment:
- V1_HUB_ADDRESS=0xdbf22d4e8962db3b2f1d9ff55be728a887e47710
- V2_HUB_ADDRESS=0xFFfbD3E62203B888bb8E09c1fcAcE58242674964
- START_BLOCK=0
- V2_NAME_REGISTRY_ADDRESS=0x0A1D308a39A6dF8972A972E586E4b4b3Dc73520f
# - START_BLOCK=0
- POSTGRES_CONNECTION_STRING=Server=postgres-chiado;Port=5432;Database=postgres;User Id=${POSTGRES_USER};Password=${POSTGRES_PASSWORD};

postgres-chiado:
Expand Down

0 comments on commit 57701ed

Please sign in to comment.