Skip to content

Commit

Permalink
merge hotfix
Browse files Browse the repository at this point in the history
  • Loading branch information
tmm360 committed Oct 25, 2022
2 parents d2f84f0 + 3bb4472 commit 883fe75
Show file tree
Hide file tree
Showing 107 changed files with 75,144 additions and 531 deletions.
2 changes: 1 addition & 1 deletion src/BeehiveManager.Domain/BeehiveManager.Domain.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<ItemGroup>
<PackageReference Include="Etherna.DomainEvents" Version="1.4.0" />
<PackageReference Include="MongODM.Core" Version="0.23.1" />
<PackageReference Include="Nethereum.Accounts" Version="4.8.0" />
<PackageReference Include="Nethereum.Accounts" Version="4.9.0" />
</ItemGroup>

</Project>
1 change: 1 addition & 0 deletions src/BeehiveManager.Domain/IBeehiveDbContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ namespace Etherna.BeehiveManager.Domain
public interface IBeehiveDbContext : IDbContext
{
ICollectionRepository<BeeNode, string> BeeNodes { get; }
ICollectionRepository<EtherAddressConfig, string> EtherAddressConfigs { get; }
ICollectionRepository<NodeLogBase, string> NodeLogs { get; }

IEventDispatcher EventDispatcher { get; }
Expand Down
16 changes: 0 additions & 16 deletions src/BeehiveManager.Domain/Models/BeeNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,6 @@
// See the License for the specific language governing permissions and
// limitations under the License.

using Etherna.BeehiveManager.Domain.Events;
using Etherna.BeehiveManager.Domain.Models.BeeNodeAgg;
using Etherna.MongODM.Core.Attributes;
using System;

namespace Etherna.BeehiveManager.Domain.Models
Expand Down Expand Up @@ -45,25 +42,12 @@ protected BeeNode() { }
#pragma warning restore CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider declaring as nullable.

// Properties.
public virtual BeeNodeAddresses? Addresses { get; protected set; }
public virtual string ConnectionScheme { get; set; }
public virtual Uri BaseUrl => new($"{ConnectionScheme}://{Hostname}");
public virtual int DebugPort { get; set; }
public virtual Uri DebugUrl => new($"{ConnectionScheme}://{Hostname}:{DebugPort}");
public virtual int GatewayPort { get; set; }
public virtual Uri GatewayUrl => new($"{ConnectionScheme}://{Hostname}:{GatewayPort}");
public virtual string Hostname { get; set; }

// Methods.
[PropertyAlterer(nameof(Addresses))]
public virtual void SetAddresses(BeeNodeAddresses addresses)
{
if (Addresses is not null)
throw new InvalidOperationException("Addresses already set");

Addresses = addresses ?? throw new ArgumentNullException(nameof(addresses));

AddEvent(new SetBeeNodeAddressesEvent(this));
}
}
}
4 changes: 2 additions & 2 deletions src/BeehiveManager.Domain/Models/EntityModelBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@ namespace Etherna.BeehiveManager.Domain.Models
public abstract class EntityModelBase : ModelBase, IEntityModel
{
private DateTime _creationDateTime;
private readonly List<IDomainEvent> _events = new();
private readonly HashSet<IDomainEvent> _events = new();

// Constructors and dispose.
protected EntityModelBase()
{
_creationDateTime = DateTime.Now;
_creationDateTime = DateTime.UtcNow;
}

public virtual void DisposeForDelete() { }
Expand Down
38 changes: 38 additions & 0 deletions src/BeehiveManager.Domain/Models/EtherAddressConfig.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// Copyright 2021-present Etherna Sagl
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

using Nethereum.Util;
using System;

namespace Etherna.BeehiveManager.Domain.Models
{
public class EtherAddressConfig : EntityModelBase<string>
{
// Consturctor.
public EtherAddressConfig(string address)
{
if (!address.IsValidEthereumAddressHexFormat())
throw new ArgumentException("Is not a valid ethereum address", nameof(address));

Address = address.ConvertToEthereumChecksumAddress();
}
#pragma warning disable CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider declaring as nullable.
protected EtherAddressConfig() { }
#pragma warning restore CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider declaring as nullable.

// Properties.
public virtual string Address { get; protected set; }
public virtual BeeNode? PreferredSocNode { get; set; }
}
}
3 changes: 2 additions & 1 deletion src/BeehiveManager.Domain/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,5 @@
using System.Runtime.CompilerServices;

[assembly: CLSCompliant(false)]
[assembly: InternalsVisibleTo("BeehiveManager.Services")]
[assembly: InternalsVisibleTo("BeehiveManager.Services")]
[assembly: InternalsVisibleTo("DynamicProxyGenAssembly2")]
15 changes: 11 additions & 4 deletions src/BeehiveManager.Persistence/BeehiveDbContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,20 @@ public BeehiveDbContext(
{
IndexBuilders = new[]
{
(Builders<BeeNode>.IndexKeys.Ascending(n => n.Addresses.Ethereum), new CreateIndexOptions<BeeNode> { Sparse = true, Unique = true }),
(Builders<BeeNode>.IndexKeys.Ascending(n => n.DebugPort)
.Ascending(n => n.Hostname), new CreateIndexOptions<BeeNode> { Unique = true }),
(Builders<BeeNode>.IndexKeys.Ascending(n => n.GatewayPort)
.Ascending(n => n.Hostname), new CreateIndexOptions<BeeNode> { Unique = true })
}
});
public ICollectionRepository<EtherAddressConfig, string> EtherAddressConfigs { get; } = new DomainCollectionRepository<EtherAddressConfig, string>(
new CollectionRepositoryOptions<EtherAddressConfig>("etherAddressConfigs")
{
IndexBuilders = new[]
{
(Builders<EtherAddressConfig>.IndexKeys.Ascending(a => a.Address), new CreateIndexOptions<EtherAddressConfig> { Unique = true }),
}
});
public ICollectionRepository<NodeLogBase, string> NodeLogs { get; } = new DomainCollectionRepository<NodeLogBase, string>("nodeLogs");

//other properties
Expand All @@ -73,17 +80,17 @@ where t.GetInterfaces().Contains(typeof(IModelMapsCollector))
select Activator.CreateInstance(t) as IModelMapsCollector;

// Public methods.
public override Task SaveChangesAsync(CancellationToken cancellationToken = default)
public override async Task SaveChangesAsync(CancellationToken cancellationToken = default)
{
// Dispatch events.
foreach (var model in ChangedModelsList.Where(m => m is EntityModelBase)
.Select(m => (EntityModelBase)m))
{
EventDispatcher.DispatchAsync(model.Events);
await EventDispatcher.DispatchAsync(model.Events);
model.ClearEvents();
}

return base.SaveChangesAsync(cancellationToken);
await base.SaveChangesAsync(cancellationToken);
}

// Protected methods.
Expand Down
31 changes: 26 additions & 5 deletions src/BeehiveManager.Persistence/ModelMaps/BeeNodeMap.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
// limitations under the License.

using Etherna.BeehiveManager.Domain.Models;
using Etherna.BeehiveManager.Domain.Models.BeeNodeAgg;
using Etherna.MongoDB.Bson;
using Etherna.MongoDB.Bson.Serialization.Serializers;
using Etherna.MongODM.Core;
Expand All @@ -27,13 +26,10 @@ class BeeNodeMap : IModelMapsCollector
public void Register(IDbContext dbContext)
{
dbContext.SchemaRegistry.AddModelMapsSchema<BeeNode>("6b94df32-034f-46f9-a5c1-239905ad5d07");

// Aggregate models.
dbContext.SchemaRegistry.AddModelMapsSchema<BeeNodeAddresses>("b4fc3145-6864-43d0-8ba5-c43f36877519");
}

/// <summary>
/// A minimal serialized with only id
/// A minimal serializer with only id
/// </summary>
public static ReferenceSerializer<BeeNode, string> ReferenceSerializer(
IDbContext dbContext,
Expand All @@ -50,5 +46,30 @@ public static ReferenceSerializer<BeeNode, string> ReferenceSerializer(
});
config.AddModelMapsSchema<BeeNode>("28d5e30d-c205-4440-9ba6-80505409ef8d", mm => { });
});

/// <summary>
/// A serializer with connection info to node
/// </summary>
public static ReferenceSerializer<BeeNode, string> ConnectionInfoSerializer(
IDbContext dbContext,
bool useCascadeDelete = false) =>
new(dbContext, config =>
{
config.UseCascadeDelete = useCascadeDelete;
config.AddModelMapsSchema<ModelBase>("148b3991-63da-4966-a781-30295c71fcae");
config.AddModelMapsSchema<EntityModelBase>("774d614c-2bd2-4a51-83a7-6d0df1942216", mm => { });
config.AddModelMapsSchema<EntityModelBase<string>>("959def90-ddab-48a7-9a0e-1917be419171", mm =>
{
mm.MapIdMember(n => n.Id);
mm.IdMemberMap.SetSerializer(new StringSerializer(BsonType.ObjectId));
});
config.AddModelMapsSchema<BeeNode>("a833d25f-4613-4cbc-b36a-4cdfa62501f4", mm =>
{
mm.MapMember(n => n.ConnectionScheme);
mm.MapMember(n => n.DebugPort);
mm.MapMember(n => n.GatewayPort);
mm.MapMember(n => n.Hostname);
});
});
}
}
35 changes: 35 additions & 0 deletions src/BeehiveManager.Persistence/ModelMaps/EtherAddressConfigMap.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// Copyright 2021-present Etherna Sagl
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

using Etherna.BeehiveManager.Domain.Models;
using Etherna.MongODM.Core;
using Etherna.MongODM.Core.Extensions;
using Etherna.MongODM.Core.Serialization;

namespace Etherna.BeehiveManager.Persistence.ModelMaps
{
class EtherAddressConfigMap : IModelMapsCollector
{
public void Register(IDbContext dbContext)
{
dbContext.SchemaRegistry.AddModelMapsSchema<EtherAddressConfig>("e7e7bb6a-17c2-444b-bd7d-6fc84f57da3c", mm =>
{
mm.AutoMap();

// Set members with custom serializers.
mm.SetMemberSerializer(a => a.PreferredSocNode!, BeeNodeMap.ConnectionInfoSerializer(dbContext));
});
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,44 +38,70 @@ public DomainCollectionRepository(CollectionRepositoryOptions<TModel> options)
: base(options)
{ }

public override void Initialize(IDbContext dbContext)
{
if (dbContext is not IEventDispatcherDbContext)
throw new InvalidOperationException($"DbContext needs to implement {nameof(IEventDispatcherDbContext)}");

base.Initialize(dbContext);
}

// Properties.
public IEventDispatcher EventDispatcher => ((IEventDispatcherDbContext)DbContext).EventDispatcher;
public IEventDispatcher? EventDispatcher => (DbContext as IEventDispatcherDbContext)?.EventDispatcher;

// Methods.
public override async Task CreateAsync(IEnumerable<TModel> models, CancellationToken cancellationToken = default)
{
if (models is null)
throw new ArgumentNullException(nameof(models));

// Create entity.
await base.CreateAsync(models, cancellationToken);

// Dispatch created events.
if (EventDispatcher != null) //could be null in seeding
await EventDispatcher.DispatchAsync(
models.Select(m => new EntityCreatedEvent<TModel>(m)));
// Dispatch events.
if (EventDispatcher != null)
{
//created event
await EventDispatcher.DispatchAsync(models.Select(m => new EntityCreatedEvent<TModel>(m)));

//custom events
foreach (var model in models)
{
await EventDispatcher.DispatchAsync(model.Events);
model.ClearEvents();
}
}
}

public override async Task CreateAsync(TModel model, CancellationToken cancellationToken = default)
{
if (model is null)
throw new ArgumentNullException(nameof(model));

// Create entity.
await base.CreateAsync(model, cancellationToken);

// Dispatch created event.
if (EventDispatcher != null) //could be null in seeding
await EventDispatcher.DispatchAsync(
new EntityCreatedEvent<TModel>(model));
// Dispatch events.
if (EventDispatcher != null)
{
//created event
await EventDispatcher.DispatchAsync(new EntityCreatedEvent<TModel>(model));

//custom events
await EventDispatcher.DispatchAsync(model.Events);
model.ClearEvents();
}
}

public override async Task DeleteAsync(TModel model, CancellationToken cancellationToken = default)
{
if (model is null)
throw new ArgumentNullException(nameof(model));

// Dispatch custom events.
if (EventDispatcher != null)
{
await EventDispatcher.DispatchAsync(model.Events);
model.ClearEvents();
}

// Delete entity.
await base.DeleteAsync(model, cancellationToken);

// Dispatch deleted event.
if (EventDispatcher != null) //could be null in seeding
if (EventDispatcher != null)
await EventDispatcher.DispatchAsync(
new EntityDeletedEvent<TModel>(model));
}
Expand Down
2 changes: 1 addition & 1 deletion src/BeehiveManager.Services/BeehiveManager.Services.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Bee.Net" Version="0.2.0-alpha.127" />
<PackageReference Include="Bee.Net" Version="0.2.0-alpha.137" />
<PackageReference Include="Etherna.DomainEvents.AspNetCore" Version="1.4.0" />
<PackageReference Include="MongODM.Hangfire" Version="0.23.1" />
<PackageReference Include="System.Linq.Async" Version="6.0.1" />
Expand Down
Loading

0 comments on commit 883fe75

Please sign in to comment.