Skip to content

Commit

Permalink
pull request fixes.
Browse files Browse the repository at this point in the history
  • Loading branch information
PawelPawelec-RDX committed Jul 28, 2023
1 parent 0edf1a1 commit 33fe109
Show file tree
Hide file tree
Showing 21 changed files with 242 additions and 2,190 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
*/

using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;

namespace RadixDlt.CoreApiSdk.Model;
Expand All @@ -78,4 +79,30 @@ public IEnumerable<string> GetEntityAddresses()

return Enumerable.Empty<string>();
}

public bool TryGetObjectInstanceSchema([NotNullWhen(true)] out InstanceSchema instanceSchema)
{
instanceSchema = null;

if (Value.Details is ObjectTypeInfoDetails objectTypeInfoDetails && objectTypeInfoDetails.InstanceSchema != null)
{
instanceSchema = objectTypeInfoDetails.InstanceSchema;
return true;
}

return false;
}

public bool TryGetKeyValueStoreSchema([NotNullWhen(true)] out KeyValueStoreSchema keyValueStoreSchema)
{
keyValueStoreSchema = null;

if (Value.Details is KeyValueStoreTypeInfoDetails keyValueStoreTypeInfoDetails && keyValueStoreTypeInfoDetails.KeyValueStoreInfo.KvStoreSchema != null)
{
keyValueStoreSchema = keyValueStoreTypeInfoDetails.KeyValueStoreInfo.KvStoreSchema;
return true;
}

return false;
}
}
6 changes: 3 additions & 3 deletions src/RadixDlt.NetworkGateway.Abstractions/Model/Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,12 +81,12 @@ public static ObjectModuleId ToInternalModel(this CoreModel.ObjectModuleId objec
};
}

public static KeyTypeKind ToInternalModel(this CoreModel.LocalTypeIndex.KindEnum indexKind)
public static SborTypeKind ToInternalModel(this CoreModel.LocalTypeIndex.KindEnum indexKind)
{
return indexKind switch
{
CoreModel.LocalTypeIndex.KindEnum.SchemaLocal => KeyTypeKind.SchemaLocal,
CoreModel.LocalTypeIndex.KindEnum.WellKnown => KeyTypeKind.WellKnown,
CoreModel.LocalTypeIndex.KindEnum.SchemaLocal => SborTypeKind.SchemaLocal,
CoreModel.LocalTypeIndex.KindEnum.WellKnown => SborTypeKind.WellKnown,
_ => throw new UnreachableException($"Didn't expect {indexKind} value"),
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@

namespace RadixDlt.NetworkGateway.Abstractions.Model;

public enum KeyTypeKind
public enum SborTypeKind
{
WellKnown,
SchemaLocal,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -897,11 +897,11 @@ components:
type: object
required:
- raw_hex
- json
- programmatic_json
properties:
raw_hex:
type: string
json:
programmatic_json:
type: object

EntityMetadataCollection:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)
modelBuilder.HasPostgresEnum<PublicKeyType>();
modelBuilder.HasPostgresEnum<ResourceType>();
modelBuilder.HasPostgresEnum<ObjectModuleId>();
modelBuilder.HasPostgresEnum<KeyTypeKind>();
modelBuilder.HasPostgresEnum<SborTypeKind>();

HookupTransactions(modelBuilder);
HookupPendingTransactions(modelBuilder);
Expand Down Expand Up @@ -362,7 +362,7 @@ private static void HookupHistory(ModelBuilder modelBuilder)
.HasIndex(e => new { e.EntityId, e.FromStateVersion });

modelBuilder.Entity<ValidatorStateHistory>()
.HasIndex(e => new { e.EntityId, e.FromStateVersion });
.HasIndex(e => new { EntityId = e.ValidatorEntityId, e.FromStateVersion });

modelBuilder.Entity<PackageBlueprintHistory>()
.HasIndex(e => new { e.PackageEntityId, e.FromStateVersion });
Expand Down Expand Up @@ -392,7 +392,7 @@ private static void HookupHistory(ModelBuilder modelBuilder)
.HasIndex(e => new { e.KeyValueStoreEntityId, e.Key, e.FromStateVersion });

modelBuilder.Entity<KeyValueStoreSchemaHistory>()
.HasIndex(e => new { e.EntityId, e.FromStateVersion });
.HasIndex(e => new { EntityId = e.KeyValueStoreEntityId, e.FromStateVersion });

modelBuilder.Entity<NonFungibleDataSchemaHistory>()
.HasIndex(e => new { e.EntityId, e.FromStateVersion });
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ public static void EnsureConfigured()
NpgsqlConnection.GlobalTypeMapper.MapEnum<PublicKeyType>();
NpgsqlConnection.GlobalTypeMapper.MapEnum<ResourceType>();
NpgsqlConnection.GlobalTypeMapper.MapEnum<ObjectModuleId>();
NpgsqlConnection.GlobalTypeMapper.MapEnum<KeyTypeKind>();
NpgsqlConnection.GlobalTypeMapper.MapEnum<SborTypeKind>();
#pragma warning restore CS0618

_configured = true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -548,25 +548,6 @@ private async Task<ExtendLedgerReport> ProcessTransactions(ReadWriteDbContext db
ledgerTransaction.CreatedTimestamp = summary.CreatedTimestamp;
ledgerTransaction.NormalizedRoundTimestamp = summary.NormalizedRoundTimestamp;
ledgerTransaction.RawPayload = committedTransaction.LedgerTransaction.GetUnwrappedPayloadBytes();

var eventsTuple = committedTransaction.Receipt.Events?
.Select(x =>
{
if (x.Type.TypePointer is not CoreModel.PackageTypePointer packageTypePointer)
{
throw new UnreachableException("All events are expected to be package type pointer.");
}
return new
{
typeIndex = packageTypePointer.LocalTypeIndex.Index,
typeKind = packageTypePointer.LocalTypeIndex.Kind.ToInternalModel(),
schemaHash = packageTypePointer.SchemaHash.ConvertFromHex(),
data = x.Data.GetDataBytes(),
};
})
.ToArray();

ledgerTransaction.EngineReceipt = new TransactionReceipt
{
StateUpdates = committedTransaction.Receipt.StateUpdates.ToJson(),
Expand All @@ -575,10 +556,18 @@ private async Task<ExtendLedgerReport> ProcessTransactions(ReadWriteDbContext db
ErrorMessage = committedTransaction.Receipt.ErrorMessage,
Output = committedTransaction.Receipt.Output != null ? JsonConvert.SerializeObject(committedTransaction.Receipt.Output) : null,
NextEpoch = committedTransaction.Receipt.NextEpoch?.ToJson(),
EventsSbor = eventsTuple?.Select(x => x.data).ToArray(),
EventsSchemaHash = eventsTuple?.Select(x => x.schemaHash).ToArray(),
EventsTypeIndex = eventsTuple?.Select(x => x.typeIndex).ToArray(),
EventsTypeKind = eventsTuple?.Select(x => x.typeKind).ToArray(),
EventsSbor = committedTransaction.Receipt.Events != null ?
committedTransaction.Receipt.Events.Select(x => x.Data.GetDataBytes()).ToArray()
: Array.Empty<byte[]>(),
EventsSchemaHash = committedTransaction.Receipt.Events != null ?
committedTransaction.Receipt.Events.Select(x => ((CoreModel.PackageTypePointer)x.Type.TypePointer).SchemaHash.ConvertFromHex()).ToArray()
: Array.Empty<byte[]>(),
EventsTypeIndex = committedTransaction.Receipt.Events != null ?
committedTransaction.Receipt.Events.Select(x => ((CoreModel.PackageTypePointer)x.Type.TypePointer).LocalTypeIndex.Index).ToArray()
: Array.Empty<int>(),
EventsSborTypeKind = committedTransaction.Receipt.Events != null ?
committedTransaction.Receipt.Events.Select(x => ((CoreModel.PackageTypePointer)x.Type.TypePointer).LocalTypeIndex.Kind.ToInternalModel()).ToArray().ToArray()
: Array.Empty<SborTypeKind>(),
};

ledgerTransactionsToAdd.Add(ledgerTransaction);
Expand Down Expand Up @@ -895,7 +884,7 @@ private async Task<ExtendLedgerReport> ProcessTransactions(ReadWriteDbContext db
{
Id = sequences.ValidatorStateHistorySequence++,
FromStateVersion = stateVersion,
EntityId = referencedEntities.Get((EntityAddress)substateId.EntityAddress).DatabaseId,
ValidatorEntityId = referencedEntities.Get((EntityAddress)substateId.EntityAddress).DatabaseId,
State = validator.Value.ToJson(),
});
}
Expand Down Expand Up @@ -1075,9 +1064,9 @@ private async Task<ExtendLedgerReport> ProcessTransactions(ReadWriteDbContext db

if (substateData is CoreModel.TypeInfoModuleFieldTypeInfoSubstate typeInfoSubstate)
{
if (typeInfoSubstate.Value.Details is CoreModel.ObjectTypeInfoDetails { InstanceSchema: not null } objectTypeInfoDetails)
if (typeInfoSubstate.TryGetObjectInstanceSchema(out var instanceSchema))
{
if (objectTypeInfoDetails.InstanceSchema.ProvidedTypes.Count != 1)
if (instanceSchema.ProvidedTypes.Count != 1)
{
throw new NotSupportedException("Expected non fungible data with only one data type entry.");
}
Expand All @@ -1087,24 +1076,24 @@ private async Task<ExtendLedgerReport> ProcessTransactions(ReadWriteDbContext db
Id = sequences.NonFungibleDataSchemaHistorySequence++,
FromStateVersion = stateVersion,
EntityId = referencedEntity.DatabaseId,
Schema = objectTypeInfoDetails.InstanceSchema.Schema.SborData.Hex.ConvertFromHex(),
TypeKind = objectTypeInfoDetails.InstanceSchema.ProvidedTypes[0].Kind.ToInternalModel(),
TypeIndex = objectTypeInfoDetails.InstanceSchema.ProvidedTypes[0].Index,
Schema = instanceSchema.Schema.SborData.Hex.ConvertFromHex(),
SborTypeKind = instanceSchema.ProvidedTypes[0].Kind.ToInternalModel(),
TypeIndex = instanceSchema.ProvidedTypes[0].Index,
});
}

if (typeInfoSubstate.Value.Details is CoreModel.KeyValueStoreTypeInfoDetails { KeyValueStoreInfo.KvStoreSchema.Schema: not null } kvStoreSchema )
if (typeInfoSubstate.TryGetKeyValueStoreSchema(out var keyValueStoreSchema))
{
keyVaulueStoreSchemaHistoryToAdd.Add(new KeyValueStoreSchemaHistory
{
Id = sequences.NonFungibleDataSchemaHistorySequence++,
FromStateVersion = stateVersion,
EntityId = referencedEntity.DatabaseId,
Schema = kvStoreSchema.KeyValueStoreInfo.KvStoreSchema.Schema.SborData.Hex.ConvertFromHex(),
KeyTypeKind = kvStoreSchema.KeyValueStoreInfo.KvStoreSchema.KeyType.Kind.ToInternalModel(),
KeyTypeIndex = kvStoreSchema.KeyValueStoreInfo.KvStoreSchema.KeyType.Index,
ValueTypeKind = kvStoreSchema.KeyValueStoreInfo.KvStoreSchema.ValueType.Kind.ToInternalModel(),
ValueTypeIndex = kvStoreSchema.KeyValueStoreInfo.KvStoreSchema.ValueType.Index,
KeyValueStoreEntityId = referencedEntity.DatabaseId,
Schema = keyValueStoreSchema.Schema.SborData.Hex.ConvertFromHex(),
KeySborTypeKind = keyValueStoreSchema.KeyType.Kind.ToInternalModel(),
KeyTypeIndex = keyValueStoreSchema.KeyType.Index,
ValueSborTypeKind = keyValueStoreSchema.ValueType.Kind.ToInternalModel(),
ValueTypeIndex = keyValueStoreSchema.ValueType.Index,
});
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ public async Task<int> CopyLedgerTransaction(ICollection<LedgerTransaction> enti
return 0;
}

await using var writer = await _connection.BeginBinaryImportAsync("COPY ledger_transactions (state_version, message, epoch, round_in_epoch, index_in_epoch, index_in_round, is_end_of_epoch, fee_paid, tip_paid, affected_global_entities, round_timestamp, created_timestamp, normalized_round_timestamp, raw_payload, receipt_state_updates, receipt_status, receipt_fee_summary, receipt_error_message, receipt_output, receipt_next_epoch, receipt_events_sbor, receipt_events_schema_hash, receipt_events_type_index, receipt_events_type_kind, discriminator, payload_hash, intent_hash, signed_intent_hash) FROM STDIN (FORMAT BINARY)", token);
await using var writer = await _connection.BeginBinaryImportAsync("COPY ledger_transactions (state_version, message, epoch, round_in_epoch, index_in_epoch, index_in_round, is_end_of_epoch, fee_paid, tip_paid, affected_global_entities, round_timestamp, created_timestamp, normalized_round_timestamp, raw_payload, receipt_state_updates, receipt_status, receipt_fee_summary, receipt_error_message, receipt_output, receipt_next_epoch, receipt_events_sbor, receipt_events_schema_hash, receipt_events_type_index, receipt_events_sbor_type_kind, discriminator, payload_hash, intent_hash, signed_intent_hash) FROM STDIN (FORMAT BINARY)", token);

foreach (var lt in entities)
{
Expand Down Expand Up @@ -210,7 +210,7 @@ public async Task<int> CopyLedgerTransaction(ICollection<LedgerTransaction> enti
await writer.WriteAsync(lt.EngineReceipt.EventsSbor, NpgsqlDbType.Array | NpgsqlDbType.Bytea, token);
await writer.WriteAsync(lt.EngineReceipt.EventsSchemaHash, NpgsqlDbType.Array | NpgsqlDbType.Bytea, token);
await writer.WriteAsync(lt.EngineReceipt.EventsTypeIndex, NpgsqlDbType.Array | NpgsqlDbType.Integer, token);
await writer.WriteAsync(lt.EngineReceipt.EventsTypeKind, "key_type_kind[]", token);
await writer.WriteAsync(lt.EngineReceipt.EventsSborTypeKind, "sbor_type_kind[]", token);
await writer.WriteAsync(discriminator, "ledger_transaction_type", token);

switch (lt)
Expand Down Expand Up @@ -431,14 +431,14 @@ public async Task<int> CopyValidatorStateHistory(ICollection<ValidatorStateHisto
return 0;
}

await using var writer = await _connection.BeginBinaryImportAsync("COPY validator_state_history (id, from_state_version, entity_id, state) FROM STDIN (FORMAT BINARY)", token);
await using var writer = await _connection.BeginBinaryImportAsync("COPY validator_state_history (id, from_state_version, validator_entity_id, state) FROM STDIN (FORMAT BINARY)", token);

foreach (var e in stateHistory)
{
await writer.StartRowAsync(token);
await writer.WriteAsync(e.Id, NpgsqlDbType.Bigint, token);
await writer.WriteAsync(e.FromStateVersion, NpgsqlDbType.Bigint, token);
await writer.WriteAsync(e.EntityId, NpgsqlDbType.Bigint, token);
await writer.WriteAsync(e.ValidatorEntityId, NpgsqlDbType.Bigint, token);
await writer.WriteAsync(e.State, NpgsqlDbType.Jsonb, token);
}

Expand Down Expand Up @@ -952,7 +952,7 @@ public async Task<int> CopyNonFungibleDataSchemaHistory(ICollection<NonFungibleD
return 0;
}

await using var writer = await _connection.BeginBinaryImportAsync("COPY non_fungible_data_schema_history (id, from_state_version, entity_id, schema, type_kind, type_index) FROM STDIN (FORMAT BINARY)", token);
await using var writer = await _connection.BeginBinaryImportAsync("COPY non_fungible_data_schema_history (id, from_state_version, entity_id, schema, sbor_type_kind, type_index) FROM STDIN (FORMAT BINARY)", token);

foreach (var e in nonFungibleDataSchemaEntries)
{
Expand All @@ -961,7 +961,7 @@ public async Task<int> CopyNonFungibleDataSchemaHistory(ICollection<NonFungibleD
await writer.WriteAsync(e.FromStateVersion, NpgsqlDbType.Bigint, token);
await writer.WriteAsync(e.EntityId, NpgsqlDbType.Bigint, token);
await writer.WriteAsync(e.Schema, NpgsqlDbType.Bytea, token);
await writer.WriteAsync(e.TypeKind, "key_type_kind", token);
await writer.WriteAsync(e.SborTypeKind, "sbor_type_kind", token);
await writer.WriteAsync(e.TypeIndex, NpgsqlDbType.Integer, token);
}

Expand All @@ -977,18 +977,18 @@ public async Task<int> CopyKeyValueStoreSchemaHistory(ICollection<KeyValueStoreS
return 0;
}

await using var writer = await _connection.BeginBinaryImportAsync("COPY key_value_store_schema_history (id, from_state_version, entity_id, schema, key_type_kind, key_type_index, value_type_kind, value_type_index) FROM STDIN (FORMAT BINARY)", token);
await using var writer = await _connection.BeginBinaryImportAsync("COPY key_value_store_schema_history (id, from_state_version, key_value_store_entity_id, schema, key_sbor_type_kind, key_type_index, value_sbor_type_kind, value_type_index) FROM STDIN (FORMAT BINARY)", token);

foreach (var e in kvSchemaHistoryEntries)
{
await writer.StartRowAsync(token);
await writer.WriteAsync(e.Id, NpgsqlDbType.Bigint, token);
await writer.WriteAsync(e.FromStateVersion, NpgsqlDbType.Bigint, token);
await writer.WriteAsync(e.EntityId, NpgsqlDbType.Bigint, token);
await writer.WriteAsync(e.KeyValueStoreEntityId, NpgsqlDbType.Bigint, token);
await writer.WriteAsync(e.Schema, NpgsqlDbType.Bytea, token);
await writer.WriteAsync(e.KeyTypeKind, "key_type_kind", token);
await writer.WriteAsync(e.KeySborTypeKind, "sbor_type_kind", token);
await writer.WriteAsync(e.KeyTypeIndex, NpgsqlDbType.Integer, token);
await writer.WriteAsync(e.ValueTypeKind, "key_type_kind", token);
await writer.WriteAsync(e.ValueSborTypeKind, "sbor_type_kind", token);
await writer.WriteAsync(e.ValueTypeIndex, NpgsqlDbType.Integer, token);
}

Expand Down
Loading

0 comments on commit 33fe109

Please sign in to comment.