Skip to content

Commit

Permalink
Merge pull request #87 from Etherna/feature/BHM-149-import-beeturbo
Browse files Browse the repository at this point in the history
Feature/bhm 149 import beeturbo
  • Loading branch information
tmm360 authored Dec 19, 2024
2 parents f9c389f + ebe5d74 commit 4bda8eb
Show file tree
Hide file tree
Showing 60 changed files with 1,549 additions and 240 deletions.
2 changes: 1 addition & 1 deletion samples/docker-beehive-sample/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ services:
networks:
- mongo-net
ports:
- 80:80
- 1633:1633
restart: unless-stopped

mongo:
Expand Down
1 change: 1 addition & 0 deletions src/Beehive.Domain/Beehive.Domain.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Bee.Net.Core" Version="0.4.0-alpha.73" />
<PackageReference Include="Etherna.DomainEvents" Version="1.4.0" />
<PackageReference Include="MongODM.Core" Version="0.25.0-alpha.6" />
<PackageReference Include="Nethereum.Web3" Version="4.26.0" />
Expand Down
7 changes: 7 additions & 0 deletions src/Beehive.Domain/IBeehiveDbContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,22 @@

using Etherna.Beehive.Domain.Models;
using Etherna.DomainEvents;
using Etherna.MongoDB.Driver.GridFS;
using Etherna.MongODM.Core;
using Etherna.MongODM.Core.Repositories;

namespace Etherna.Beehive.Domain
{
public interface IBeehiveDbContext : IDbContext
{
// Properties.
//repositories
IRepository<BeeNode, string> BeeNodes { get; }
IRepository<UploadedChunkRef, string> ChunkPushQueue { get; }
IRepository<Chunk, string> Chunks { get; }
GridFSBucket ChunksBucket { get; }

//others
IEventDispatcher EventDispatcher { get; }
}
}
38 changes: 38 additions & 0 deletions src/Beehive.Domain/Models/Chunk.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// Copyright 2021-present Etherna SA
// This file is part of Beehive.
//
// Beehive is free software: you can redistribute it and/or modify it under the terms of the
// GNU Affero General Public License as published by the Free Software Foundation,
// either version 3 of the License, or (at your option) any later version.
//
// Beehive is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
// without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
// See the GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License along with Beehive.
// If not, see <https://www.gnu.org/licenses/>.

using Etherna.BeeNet.Models;
using System.Diagnostics.CodeAnalysis;

namespace Etherna.Beehive.Domain.Models
{
public class Chunk : EntityModelBase<string>
{
// Constructors.
public Chunk(SwarmHash hash, byte[] payload)
{
Hash = hash;
Payload = payload;
}
#pragma warning disable CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider declaring as nullable.
public Chunk() { }
#pragma warning restore CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider declaring as nullable.

// Properties.
public virtual SwarmHash Hash { get; protected set; }

[SuppressMessage("Performance", "CA1819:Properties should not return arrays")]
public virtual byte[] Payload { get; protected set; }
}
}
9 changes: 2 additions & 7 deletions src/Beehive.Domain/Models/EntityModelBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,10 @@ namespace Etherna.Beehive.Domain.Models
{
public abstract class EntityModelBase : ModelBase, IEntityModel
{
private DateTime _creationDateTime;
private DateTime _creationDateTime = DateTime.UtcNow;
private readonly HashSet<IDomainEvent> _events = new();

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

// Dispose.
public virtual void DisposeForDelete() { }

// Properties.
Expand Down
35 changes: 35 additions & 0 deletions src/Beehive.Domain/Models/UploadedChunkRef.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// Copyright 2021-present Etherna SA
// This file is part of Beehive.
//
// Beehive is free software: you can redistribute it and/or modify it under the terms of the
// GNU Affero General Public License as published by the Free Software Foundation,
// either version 3 of the License, or (at your option) any later version.
//
// Beehive is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
// without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
// See the GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License along with Beehive.
// If not, see <https://www.gnu.org/licenses/>.

using Etherna.BeeNet.Models;

namespace Etherna.Beehive.Domain.Models
{
public class UploadedChunkRef : EntityModelBase<string>
{
// Constructors.
public UploadedChunkRef(SwarmHash hash, PostageBatchId batchId)
{
Hash = hash;
BatchId = batchId;
}
#pragma warning disable CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider declaring as nullable.
protected UploadedChunkRef() { }
#pragma warning restore CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider declaring as nullable.

// Properties.
public virtual PostageBatchId BatchId { get; protected set; }
public virtual SwarmHash Hash { get; protected set; }
}
}
33 changes: 30 additions & 3 deletions src/Beehive.Persistence/BeehiveDbContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
using Etherna.Beehive.Persistence.Repositories;
using Etherna.DomainEvents;
using Etherna.MongoDB.Driver;
using Etherna.MongoDB.Driver.GridFS;
using Etherna.MongODM.Core;
using Etherna.MongODM.Core.Repositories;
using Etherna.MongODM.Core.Serialization;
Expand All @@ -31,11 +32,14 @@ namespace Etherna.Beehive.Persistence
{
public class BeehiveDbContext(
IEventDispatcher eventDispatcher,
IEnumerable<BeeNode>? seedDbBeeNodes)
BeeNode[]? seedDbBeeNodes)
: DbContext, IBeehiveDbContext, IEventDispatcherDbContext
{
// Consts.
private const string SerializersNamespace = "Etherna.Beehive.Persistence.ModelMaps";
private const string ModelMapsNamespace = "Etherna.Beehive.Persistence.ModelMaps";

// Fields.
private GridFSBucket? _chunksBucket;

// Properties.
//repositories
Expand All @@ -48,14 +52,37 @@ public class BeehiveDbContext(
.Ascending(n => n.Hostname), new CreateIndexOptions<BeeNode> { Unique = true })
]
});
public IRepository<UploadedChunkRef, string> ChunkPushQueue { get; } =
new Repository<UploadedChunkRef, string>("chunkPushQueue");
public IRepository<Chunk, string> Chunks { get; } =
new Repository<Chunk, string>(new RepositoryOptions<Chunk>("chunks")
{
IndexBuilders =
[
(Builders<Chunk>.IndexKeys.Ascending(c => c.CreationDateTime), new CreateIndexOptions<Chunk>()),
(Builders<Chunk>.IndexKeys.Ascending(c => c.Hash), new CreateIndexOptions<Chunk>())
]
});
public GridFSBucket ChunksBucket
{
get
{
return _chunksBucket ??= new GridFSBucket(Database, new GridFSBucketOptions
{
BucketName = "chunks",
WriteConcern = WriteConcern.WMajority,
ReadPreference = ReadPreference.Secondary
});
}
}

//other properties
public IEventDispatcher EventDispatcher { get; } = eventDispatcher;

// Protected properties.
protected override IEnumerable<IModelMapsCollector> ModelMapsCollectors =>
from t in typeof(BeehiveDbContext).GetTypeInfo().Assembly.GetTypes()
where t.IsClass && t.Namespace == SerializersNamespace
where t.IsClass && t.Namespace == ModelMapsNamespace
where t.GetInterfaces().Contains(typeof(IModelMapsCollector))
select Activator.CreateInstance(t) as IModelMapsCollector;

Expand Down
43 changes: 43 additions & 0 deletions src/Beehive.Persistence/ModelMaps/BeeNetMap.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// Copyright 2021-present Etherna SA
// This file is part of Beehive.
//
// Beehive is free software: you can redistribute it and/or modify it under the terms of the
// GNU Affero General Public License as published by the Free Software Foundation,
// either version 3 of the License, or (at your option) any later version.
//
// Beehive is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
// without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
// See the GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License along with Beehive.
// If not, see <https://www.gnu.org/licenses/>.

using Etherna.Beehive.Persistence.Serializers;
using Etherna.BeeNet.Models;
using Etherna.MongODM.Core;
using Etherna.MongODM.Core.Serialization;

namespace Etherna.Beehive.Persistence.ModelMaps
{
internal sealed class BeeNetMap : IModelMapsCollector
{
public void Register(IDbContext dbContext)
{
dbContext.MapRegistry.AddModelMap<PostageBatchId>( //v0.4.0
"57e606db-94be-4b3f-85fc-193483020012",
customSerializer: new PostageBatchIdSerializer());

dbContext.MapRegistry.AddModelMap<SwarmAddress>( //v0.4.0
"5d4d4d6a-5448-43ca-aa74-50c6d0d82f25",
customSerializer: new SwarmAddressSerializer());

dbContext.MapRegistry.AddModelMap<SwarmHash>( //v0.4.0
"64231e24-1cfc-4b24-8c7a-20f0f708969a",
customSerializer: new SwarmHashSerializer());

dbContext.MapRegistry.AddModelMap<SwarmUri>( //v0.4.0
"99d8f98f-cfa4-4490-aeda-5dc2df7fdba8",
customSerializer: new SwarmUriSerializer());
}
}
}
35 changes: 35 additions & 0 deletions src/Beehive.Persistence/ModelMaps/ChunkMap.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// Copyright 2021-present Etherna SA
// This file is part of Beehive.
//
// Beehive is free software: you can redistribute it and/or modify it under the terms of the
// GNU Affero General Public License as published by the Free Software Foundation,
// either version 3 of the License, or (at your option) any later version.
//
// Beehive is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
// without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
// See the GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License along with Beehive.
// If not, see <https://www.gnu.org/licenses/>.

using Etherna.Beehive.Domain.Models;
using Etherna.MongoDB.Bson.Serialization.Serializers;
using Etherna.MongODM.Core;
using Etherna.MongODM.Core.Serialization;

namespace Etherna.Beehive.Persistence.ModelMaps
{
internal sealed class ChunkMap : IModelMapsCollector
{
public void Register(IDbContext dbContext)
{
dbContext.MapRegistry.AddModelMap<Chunk>( //v0.4.0
"06aaf593-07af-4fca-99a9-bdc3718547d8",
mm =>
{
mm.AutoMap();
mm.MapProperty(c => c.Payload).SetSerializer(ByteArraySerializer.Instance);
});
}
}
}
29 changes: 29 additions & 0 deletions src/Beehive.Persistence/ModelMaps/UploadedChunkRefMap.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Copyright 2021-present Etherna SA
// This file is part of Beehive.
//
// Beehive is free software: you can redistribute it and/or modify it under the terms of the
// GNU Affero General Public License as published by the Free Software Foundation,
// either version 3 of the License, or (at your option) any later version.
//
// Beehive is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
// without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
// See the GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License along with Beehive.
// If not, see <https://www.gnu.org/licenses/>.

using Etherna.Beehive.Domain.Models;
using Etherna.MongODM.Core;
using Etherna.MongODM.Core.Serialization;

namespace Etherna.Beehive.Persistence.ModelMaps
{
internal sealed class UploadedChunkRefMap : IModelMapsCollector
{
public void Register(IDbContext dbContext)
{
dbContext.MapRegistry.AddModelMap<UploadedChunkRef>(
"30e3473f-5d56-4821-9c66-aa8922b46942"); //v0.4.0
}
}
}
36 changes: 36 additions & 0 deletions src/Beehive.Persistence/Serializers/PostageBatchIdSerializer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// Copyright 2021-present Etherna SA
// This file is part of Beehive.
//
// Beehive is free software: you can redistribute it and/or modify it under the terms of the
// GNU Affero General Public License as published by the Free Software Foundation,
// either version 3 of the License, or (at your option) any later version.
//
// Beehive is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
// without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
// See the GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License along with Beehive.
// If not, see <https://www.gnu.org/licenses/>.

using Etherna.BeeNet.Models;
using Etherna.MongoDB.Bson.Serialization;
using Etherna.MongoDB.Bson.Serialization.Serializers;

namespace Etherna.Beehive.Persistence.Serializers
{
public class PostageBatchIdSerializer : SerializerBase<PostageBatchId>
{
private readonly StringSerializer stringSerializer = new();

public override PostageBatchId Deserialize(BsonDeserializationContext context, BsonDeserializationArgs args)
{
var batchId = stringSerializer.Deserialize(context, args);
return PostageBatchId.FromString(batchId);
}

public override void Serialize(BsonSerializationContext context, BsonSerializationArgs args, PostageBatchId value)
{
stringSerializer.Serialize(context, args, value.ToString());
}
}
}
36 changes: 36 additions & 0 deletions src/Beehive.Persistence/Serializers/SwarmAddressSerializer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// Copyright 2021-present Etherna SA
// This file is part of Beehive.
//
// Beehive is free software: you can redistribute it and/or modify it under the terms of the
// GNU Affero General Public License as published by the Free Software Foundation,
// either version 3 of the License, or (at your option) any later version.
//
// Beehive is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
// without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
// See the GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License along with Beehive.
// If not, see <https://www.gnu.org/licenses/>.

using Etherna.BeeNet.Models;
using Etherna.MongoDB.Bson.Serialization;
using Etherna.MongoDB.Bson.Serialization.Serializers;

namespace Etherna.Beehive.Persistence.Serializers
{
public class SwarmAddressSerializer : SerializerBase<SwarmAddress>
{
private readonly StringSerializer stringSerializer = new();

public override SwarmAddress Deserialize(BsonDeserializationContext context, BsonDeserializationArgs args)
{
var address = stringSerializer.Deserialize(context, args);
return SwarmAddress.FromString(address);
}

public override void Serialize(BsonSerializationContext context, BsonSerializationArgs args, SwarmAddress value)
{
stringSerializer.Serialize(context, args, value.ToString());
}
}
}
Loading

0 comments on commit 4bda8eb

Please sign in to comment.