-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
238 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
Tzkt.Data/Models/Operations/UpdateConsensusKeyOperation.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
using Microsoft.EntityFrameworkCore; | ||
using Tzkt.Data.Models.Base; | ||
|
||
namespace Tzkt.Data.Models | ||
{ | ||
public class UpdateConsensusKeyOperation : ManagerOperation | ||
{ | ||
public int ActivationCycle { get; set; } | ||
public string PublicKey { get; set; } | ||
public string PublicKeyHash { get; set; } | ||
} | ||
|
||
public static class UpdateConsensusKeyOperationModel | ||
{ | ||
public static void BuildUpdateConsensusKeyOperationModel(this ModelBuilder modelBuilder) | ||
{ | ||
#region keys | ||
modelBuilder.Entity<UpdateConsensusKeyOperation>() | ||
.HasKey(x => x.Id); | ||
#endregion | ||
|
||
#region props | ||
modelBuilder.Entity<UpdateConsensusKeyOperation>() | ||
.Property(x => x.OpHash) | ||
.IsFixedLength(true) | ||
.HasMaxLength(51) | ||
.IsRequired(); | ||
#endregion | ||
|
||
#region indexes | ||
modelBuilder.Entity<UpdateConsensusKeyOperation>() | ||
.HasIndex(x => x.Level); | ||
|
||
modelBuilder.Entity<UpdateConsensusKeyOperation>() | ||
.HasIndex(x => x.OpHash); | ||
|
||
modelBuilder.Entity<UpdateConsensusKeyOperation>() | ||
.HasIndex(x => x.SenderId); | ||
#endregion | ||
|
||
#region relations | ||
modelBuilder.Entity<UpdateConsensusKeyOperation>() | ||
.HasOne(x => x.Block) | ||
.WithMany(x => x.UpdateConsensusKeyOps) | ||
.HasForeignKey(x => x.Level) | ||
.HasPrincipalKey(x => x.Level); | ||
#endregion | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
132 changes: 132 additions & 0 deletions
132
Tzkt.Sync/Protocols/Handlers/Proto15/Commits/Operations/UpdateConsensusKeyCommit.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
using System; | ||
using System.Text.Json; | ||
using System.Threading.Tasks; | ||
using Netezos.Keys; | ||
using Tzkt.Data.Models; | ||
using Tzkt.Data.Models.Base; | ||
|
||
namespace Tzkt.Sync.Protocols.Proto15 | ||
{ | ||
class UpdateConsensusKeyCommit : ProtocolCommit | ||
{ | ||
public UpdateConsensusKeyCommit(ProtocolHandler protocol) : base(protocol) { } | ||
|
||
public virtual async Task Apply(Block block, JsonElement op, JsonElement content) | ||
{ | ||
#region init | ||
var sender = await Cache.Accounts.GetAsync(content.RequiredString("source")); | ||
sender.Delegate ??= Cache.Accounts.GetDelegate(sender.DelegateId); | ||
|
||
var pubKey = content.RequiredString("pk"); | ||
var pubKeyHash = PubKey.FromBase58(pubKey).Address; | ||
var result = content.Required("metadata").Required("operation_result"); | ||
var operation = new UpdateConsensusKeyOperation | ||
{ | ||
Id = Cache.AppState.NextOperationId(), | ||
OpHash = op.RequiredString("hash"), | ||
Block = block, | ||
Level = block.Level, | ||
Timestamp = block.Timestamp, | ||
BakerFee = content.RequiredInt64("fee"), | ||
Counter = content.RequiredInt32("counter"), | ||
GasLimit = content.RequiredInt32("gas_limit"), | ||
StorageLimit = content.RequiredInt32("storage_limit"), | ||
Sender = sender, | ||
ActivationCycle = block.Cycle + block.Protocol.PreservedCycles + 1, | ||
PublicKey = pubKey, | ||
PublicKeyHash = pubKeyHash, | ||
Status = result.RequiredString("status") switch | ||
{ | ||
"applied" => OperationStatus.Applied, | ||
"backtracked" => OperationStatus.Backtracked, | ||
"failed" => OperationStatus.Failed, | ||
"skipped" => OperationStatus.Skipped, | ||
_ => throw new NotImplementedException() | ||
}, | ||
Errors = result.TryGetProperty("errors", out var errors) | ||
? OperationErrors.Parse(content, errors) | ||
: null, | ||
GasUsed = (int)(((result.OptionalInt64("consumed_milligas") ?? 0) + 999) / 1000) | ||
}; | ||
#endregion | ||
|
||
#region entities | ||
var blockBaker = block.Proposer; | ||
var senderDelegate = sender.Delegate ?? sender as Data.Models.Delegate; | ||
|
||
Db.TryAttach(blockBaker); | ||
Db.TryAttach(sender); | ||
Db.TryAttach(senderDelegate); | ||
#endregion | ||
|
||
#region apply operation | ||
sender.Balance -= operation.BakerFee; | ||
if (senderDelegate != null) | ||
{ | ||
senderDelegate.StakingBalance -= operation.BakerFee; | ||
if (senderDelegate.Id != sender.Id) | ||
senderDelegate.DelegatedBalance -= operation.BakerFee; | ||
} | ||
blockBaker.Balance += operation.BakerFee; | ||
blockBaker.StakingBalance += operation.BakerFee; | ||
|
||
sender.UpdateConsensusKeyCount++; | ||
|
||
block.Operations |= Operations.UpdateConsensusKey; | ||
block.Fees += operation.BakerFee; | ||
|
||
sender.Counter = operation.Counter; | ||
#endregion | ||
|
||
#region apply result | ||
#endregion | ||
|
||
Proto.Manager.Set(operation.Sender); | ||
Db.UpdateConsensusKeyOps.Add(operation); | ||
} | ||
|
||
public virtual async Task Revert(Block block, UpdateConsensusKeyOperation operation) | ||
{ | ||
#region init | ||
operation.Block ??= block; | ||
operation.Block.Proposer ??= Cache.Accounts.GetDelegate(block.ProposerId); | ||
|
||
operation.Sender ??= await Cache.Accounts.GetAsync(operation.SenderId); | ||
operation.Sender.Delegate ??= Cache.Accounts.GetDelegate(operation.Sender.DelegateId); | ||
#endregion | ||
|
||
#region entities | ||
var blockBaker = block.Proposer; | ||
var sender = operation.Sender; | ||
var senderDelegate = sender.Delegate ?? sender as Data.Models.Delegate; | ||
|
||
Db.TryAttach(blockBaker); | ||
Db.TryAttach(sender); | ||
Db.TryAttach(senderDelegate); | ||
#endregion | ||
|
||
#region revert result | ||
#endregion | ||
|
||
#region revert operation | ||
sender.Balance += operation.BakerFee; | ||
if (senderDelegate != null) | ||
{ | ||
senderDelegate.StakingBalance += operation.BakerFee; | ||
if (senderDelegate.Id != sender.Id) | ||
senderDelegate.DelegatedBalance += operation.BakerFee; | ||
} | ||
blockBaker.Balance -= operation.BakerFee; | ||
blockBaker.StakingBalance -= operation.BakerFee; | ||
|
||
sender.UpdateConsensusKeyCount--; | ||
|
||
sender.Counter = operation.Counter - 1; | ||
#endregion | ||
|
||
Db.UpdateConsensusKeyOps.Remove(operation); | ||
Cache.AppState.ReleaseManagerCounter(); | ||
Cache.AppState.ReleaseOperationId(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters