protected Task ValidateNonceRevelation(RawNonceRevelationContent revelation, RawBlock rawBlock) { if (revelation.Level % Protocol.BlocksPerCommitment != 0) { throw new ValidationException("invalid seed nonce revelation level"); } if (revelation.Metadata.BalanceUpdates.Count != 1) { throw new ValidationException("invalid seed nonce revelation balance updates count"); } if (!(revelation.Metadata.BalanceUpdates[0] is RewardsUpdate)) { throw new ValidationException("invalid seed nonce revelation balance update type"); } if (revelation.Metadata.BalanceUpdates[0].Change != Protocol.RevelationReward) { throw new ValidationException("invalid seed nonce revelation balance update amount"); } if (!Cache.Accounts.DelegateExists(revelation.Metadata.BalanceUpdates[0].Target) || revelation.Metadata.BalanceUpdates[0].Target != rawBlock.Metadata.Baker) { throw new ValidationException("invalid seed nonce revelation baker"); } return(Task.CompletedTask); }
public Task Init(Block block, RawBlock rawBlock) { Block = block; NextProtocol = rawBlock.Metadata.NextProtocol; AppState = Cache.AppState.Get(); return(Task.CompletedTask); }
public static async Task <BlockCommit> Apply(ProtocolHandler proto, RawBlock rawBlock) { var commit = new BlockCommit(proto); await commit.Init(rawBlock); await commit.Apply(); return(commit); }
protected async Task ValidateEndorsement(RawEndorsementContent endorsement, RawBlock rawBlock) { var lastBlock = await Cache.Blocks.CurrentAsync(); if (endorsement.Level != lastBlock.Level) { throw new ValidationException("invalid endorsed block level"); } if (!Cache.Accounts.DelegateExists(endorsement.Metadata.Delegate)) { throw new ValidationException("invalid endorsement delegate"); } if (endorsement.Metadata.BalanceUpdates.Count != 0 && endorsement.Metadata.BalanceUpdates.Count != (Protocol.BlockReward0 > 0 ? 3 : 2)) { throw new ValidationException("invalid endorsement balance updates count"); } if (endorsement.Metadata.BalanceUpdates.Count > 0) { var contractUpdate = endorsement.Metadata.BalanceUpdates.FirstOrDefault(x => x is ContractUpdate) as ContractUpdate ?? throw new ValidationException("invalid endorsement contract balance updates"); var depostisUpdate = endorsement.Metadata.BalanceUpdates.FirstOrDefault(x => x is DepositsUpdate) as DepositsUpdate ?? throw new ValidationException("invalid endorsement depostis balance updates"); if (contractUpdate.Contract != endorsement.Metadata.Delegate || contractUpdate.Change != -endorsement.Metadata.Slots.Count * Protocol.EndorsementDeposit) { throw new ValidationException("invalid endorsement contract update"); } if (depostisUpdate.Delegate != endorsement.Metadata.Delegate || depostisUpdate.Change != endorsement.Metadata.Slots.Count * Protocol.EndorsementDeposit) { throw new ValidationException("invalid endorsement depostis update"); } if (Cycle >= (Protocol.PreservedCycles + 2)) { var rewardsUpdate = endorsement.Metadata.BalanceUpdates.FirstOrDefault(x => x is RewardsUpdate) as RewardsUpdate ?? throw new ValidationException("invalidendorsement rewards updates"); if (rewardsUpdate.Delegate != endorsement.Metadata.Delegate || rewardsUpdate.Change != GetEndorsementReward(endorsement.Metadata.Slots.Count, lastBlock.Priority)) { throw new ValidationException("invalid endorsement rewards update"); } } } }
protected async Task ValidateReveal(RawRevealContent reveal, RawBlock rawBlock) { if (!await Cache.Accounts.ExistsAsync(reveal.Source)) { throw new ValidationException("unknown source account"); } ValidateFeeBalanceUpdates( reveal.Metadata.BalanceUpdates, rawBlock.Metadata.Baker, reveal.Source, reveal.Fee, rawBlock.Metadata.LevelInfo.Cycle); }
protected async Task ValidateTransaction(RawTransactionContent transaction, RawBlock rawBlock) { if (!await Cache.Accounts.ExistsAsync(transaction.Source)) { throw new ValidationException("unknown source account"); } ValidateFeeBalanceUpdates( transaction.Metadata.BalanceUpdates, rawBlock.Metadata.Baker, transaction.Source, transaction.Fee, rawBlock.Metadata.LevelInfo.Cycle); if (transaction.Metadata.Result.BalanceUpdates != null) { ValidateTransferBalanceUpdates( transaction.Metadata.Result.BalanceUpdates, transaction.Source, transaction.Destination, transaction.Amount, transaction.Metadata.Result.PaidStorageSizeDiff * Protocol.ByteCost); } if (transaction.Metadata.InternalResults?.Count > 0) { foreach (var internalContent in transaction.Metadata.InternalResults.Where(x => x is RawInternalTransactionResult)) { var internalTransaction = internalContent as RawInternalTransactionResult; if (!await Cache.Accounts.ExistsAsync(internalTransaction.Source, AccountType.Contract)) { throw new ValidationException("unknown source contract"); } if (internalTransaction.Result.BalanceUpdates != null) { ValidateTransferBalanceUpdates( internalTransaction.Result.BalanceUpdates, internalTransaction.Source, internalTransaction.Destination, internalTransaction.Amount, internalTransaction.Result.PaidStorageSizeDiff * Protocol.ByteCost, transaction.Source); } } } }
public async Task Init(RawBlock rawBlock) { var protocol = await Cache.Protocols.GetAsync(rawBlock.Protocol); var events = BlockEvents.None; if (rawBlock.Level % protocol.BlocksPerCycle == 1) { events |= BlockEvents.CycleBegin; } else if (rawBlock.Level % protocol.BlocksPerCycle == 0) { events |= BlockEvents.CycleEnd; } if (protocol.FirstLevel == rawBlock.Level) { events |= BlockEvents.ProtocolBegin; } else if (rawBlock.Metadata.Protocol != rawBlock.Metadata.NextProtocol) { events |= BlockEvents.ProtocolEnd; } if (rawBlock.Level % protocol.BlocksPerSnapshot == 0) { events |= BlockEvents.Snapshot; } Block = new Block { Id = Cache.AppState.NextOperationId(), Hash = rawBlock.Hash, Level = rawBlock.Level, Protocol = protocol, Timestamp = rawBlock.Header.Timestamp, Priority = rawBlock.Header.Priority, Baker = Cache.Accounts.GetDelegate(rawBlock.Metadata.Baker), Events = events, Reward = protocol.BlockReward0 }; }
protected async Task ValidateDelegation(RawDelegationContent delegation, RawBlock rawBlock) { if (!await Cache.Accounts.ExistsAsync(delegation.Source)) { throw new ValidationException("unknown source account"); } ValidateFeeBalanceUpdates( delegation.Metadata.BalanceUpdates, rawBlock.Metadata.Baker, delegation.Source, delegation.Fee, rawBlock.Metadata.LevelInfo.Cycle); if (delegation.Metadata.Result.Status == "applied" && delegation.Delegate != null) { if (delegation.Source != delegation.Delegate && !Cache.Accounts.DelegateExists(delegation.Delegate)) { throw new ValidationException("unknown delegate account"); } } }
protected async Task ValidateOrigination(RawOriginationContent origination, RawBlock rawBlock) { if (!await Cache.Accounts.ExistsAsync(origination.Source)) { throw new ValidationException("unknown source account"); } ValidateFeeBalanceUpdates( origination.Metadata.BalanceUpdates, rawBlock.Metadata.Baker, origination.Source, origination.Fee, rawBlock.Metadata.LevelInfo.Cycle); if (origination.Metadata.Result.BalanceUpdates != null) { ValidateTransferBalanceUpdates( origination.Metadata.Result.BalanceUpdates, origination.Source, origination.Metadata.Result.OriginatedContracts[0], origination.Balance, (origination.Metadata.Result.PaidStorageSizeDiff + Protocol.OriginationSize) * Protocol.ByteCost); } }
public static async Task <StateCommit> Apply(ProtocolHandler proto, Block block, RawBlock rawBlock) { var commit = new StateCommit(proto); await commit.Init(block, rawBlock); await commit.Apply(); return(commit); }