public override async Task Apply() { #region entities var block = Endorsement.Block; var sender = Endorsement.Delegate; //Db.TryAttach(block); Db.TryAttach(sender); #endregion #region apply operation sender.Balance += Endorsement.Reward; sender.FrozenRewards += Endorsement.Reward; sender.FrozenDeposits += block.Protocol.EndorsementDeposit * Endorsement.Slots; sender.EndorsementsCount++; block.Operations |= Operations.Endorsements; block.Validations += Endorsement.Slots; var newDeactivationLevel = sender.Staked ? GracePeriod.Reset(Endorsement.Block) : GracePeriod.Init(Endorsement.Block); if (sender.DeactivationLevel < newDeactivationLevel) { if (sender.DeactivationLevel <= Endorsement.Level) { await UpdateDelegate(sender, true); } Endorsement.ResetDeactivation = sender.DeactivationLevel; sender.DeactivationLevel = newDeactivationLevel; } #endregion Db.EndorsementOps.Add(Endorsement); }
public virtual async Task Apply(Block block, JsonElement op, JsonElement content) { #region init var metadata = content.Required("metadata"); var reward = metadata .RequiredArray("balance_updates") .EnumerateArray() .FirstOrDefault(x => x.RequiredString("kind")[0] == 'f' && x.RequiredString("category")[0] == 'r'); var deposit = metadata .RequiredArray("balance_updates") .EnumerateArray() .FirstOrDefault(x => x.RequiredString("kind")[0] == 'f' && x.RequiredString("category")[0] == 'd'); var endorsement = new EndorsementOperation { Id = Cache.AppState.NextOperationId(), Block = block, Level = block.Level, Timestamp = block.Timestamp, OpHash = op.RequiredString("hash"), Slots = metadata.RequiredArray("slots").Count(), Delegate = Cache.Accounts.GetDelegate(metadata.RequiredString("delegate")), Reward = reward.ValueKind != JsonValueKind.Undefined ? reward.RequiredInt64("change") : 0, Deposit = deposit.ValueKind != JsonValueKind.Undefined ? deposit.RequiredInt64("change") : 0 }; #endregion #region entities //var block = endorsement.Block; var sender = endorsement.Delegate; //Db.TryAttach(block); Db.TryAttach(sender); #endregion #region apply operation sender.Balance += endorsement.Reward; sender.FrozenRewards += endorsement.Reward; sender.FrozenDeposits += endorsement.Deposit; sender.EndorsementsCount++; block.Operations |= Operations.Endorsements; block.Validations += endorsement.Slots; var newDeactivationLevel = sender.Staked ? GracePeriod.Reset(endorsement.Block) : GracePeriod.Init(endorsement.Block); if (sender.DeactivationLevel < newDeactivationLevel) { if (sender.DeactivationLevel <= endorsement.Level) { await UpdateDelegate(sender, true); } endorsement.ResetDeactivation = sender.DeactivationLevel; sender.DeactivationLevel = newDeactivationLevel; } #endregion Db.EndorsementOps.Add(endorsement); }
public override Task Apply() { #region entities var proto = Block.Protocol; var baker = Block.Baker; Db.TryAttach(proto); Db.TryAttach(baker); #endregion baker.Balance += Block.Reward; baker.FrozenRewards += Block.Reward; baker.FrozenDeposits += Block.Protocol.BlockDeposit; baker.BlocksCount++; var newDeactivationLevel = baker.Staked ? GracePeriod.Reset(Block) : GracePeriod.Init(Block); if (baker.DeactivationLevel < newDeactivationLevel) { Block.ResetDeactivation = baker.DeactivationLevel; baker.DeactivationLevel = newDeactivationLevel; } if (Block.Events.HasFlag(BlockEvents.ProtocolEnd)) { proto.LastLevel = Block.Level; } Db.Blocks.Add(Block); Cache.Blocks.Add(Block); return(Task.CompletedTask); }
public virtual async Task Apply(JsonElement rawBlock) { var level = rawBlock.Required("header").RequiredInt32("level"); var protocol = await Cache.Protocols.GetAsync(rawBlock.RequiredString("protocol")); var events = BlockEvents.None; var metadata = rawBlock.Required("metadata"); var reward = GetBlockReward(metadata); var deposit = GetBlockDeposit(metadata); if (level % protocol.BlocksPerCycle == 1) { events |= BlockEvents.CycleBegin; } else if (level % protocol.BlocksPerCycle == 0) { events |= BlockEvents.CycleEnd; } if (protocol.FirstLevel == level) { events |= BlockEvents.ProtocolBegin; } else if (metadata.RequiredString("protocol") != metadata.RequiredString("next_protocol")) { events |= BlockEvents.ProtocolEnd; } if (metadata.RequiredArray("deactivated").Count() > 0) { events |= BlockEvents.Deactivations; } if (level % protocol.BlocksPerSnapshot == 0) { events |= BlockEvents.BalanceSnapshot; } Block = new Block { Id = Cache.AppState.NextOperationId(), Hash = rawBlock.RequiredString("hash"), Level = level, Protocol = protocol, Timestamp = rawBlock.Required("header").RequiredDateTime("timestamp"), Priority = rawBlock.Required("header").RequiredInt32("priority"), Baker = Cache.Accounts.GetDelegate(rawBlock.Required("metadata").RequiredString("baker")), Events = events, Reward = reward.ValueKind != JsonValueKind.Undefined ? reward.RequiredInt64("change") : 0, Deposit = deposit.ValueKind != JsonValueKind.Undefined ? deposit.RequiredInt64("change") : 0 }; #region entities var proto = Block.Protocol; var baker = Block.Baker; Db.TryAttach(proto); Db.TryAttach(baker); #endregion baker.Balance += Block.Reward; baker.FrozenRewards += Block.Reward; baker.FrozenDeposits += Block.Deposit; baker.BlocksCount++; var newDeactivationLevel = baker.Staked ? GracePeriod.Reset(Block) : GracePeriod.Init(Block); if (baker.DeactivationLevel < newDeactivationLevel) { if (baker.DeactivationLevel <= Block.Level) { await UpdateDelegate(baker, true); } Block.ResetDeactivation = baker.DeactivationLevel; baker.DeactivationLevel = newDeactivationLevel; } if (Block.Events.HasFlag(BlockEvents.ProtocolEnd)) { proto.LastLevel = Block.Level; } Db.Blocks.Add(Block); Cache.Blocks.Add(Block); }