예제 #1
0
        public async Task <bool> PublishBlockAsync(BeaconBlock signedBlock, CancellationToken cancellationToken)
        {
            if (!_storeProvider.TryGetStore(out IStore? retrievedStore))
            {
                throw new Exception("Beacon chain is currently syncing or waiting for genesis.");
            }

            IStore store = retrievedStore !;

            bool acceptedLocally = false;

            try
            {
                await _forkChoice.OnBlockAsync(store, signedBlock).ConfigureAwait(false);

                // TODO: validate as per honest validator spec and return true/false
                acceptedLocally = true;
            }
            catch (Exception ex)
            {
                if (_logger.IsWarn())
                {
                    Log.BlockNotAcceptedLocally(_logger, signedBlock, ex);
                }
            }

            await _networkPeering.PublishBeaconBlockAsync(signedBlock).ConfigureAwait(false);

            return(acceptedLocally);
        }
예제 #2
0
        public async Task <ApiResponse> PublishBlockAsync(SignedBeaconBlock signedBlock,
                                                          CancellationToken cancellationToken)
        {
            try
            {
                if (!_store.IsInitialized)
                {
                    return(new ApiResponse(StatusCode.CurrentlySyncing));
                }

                bool acceptedLocally = false;
                try
                {
                    await _forkChoice.OnBlockAsync(_store, signedBlock).ConfigureAwait(false);

                    // TODO: validate as per honest validator spec and return true/false
                    acceptedLocally = true;
                }
                catch (Exception ex)
                {
                    if (_logger.IsWarn())
                    {
                        Log.BlockNotAcceptedLocally(_logger, signedBlock.Message, ex);
                    }
                }

                if (_logger.IsDebug())
                {
                    LogDebug.PublishingBlockToNetwork(_logger, signedBlock.Message, null);
                }
                await _networkPeering.PublishBeaconBlockAsync(signedBlock).ConfigureAwait(false);

                if (acceptedLocally)
                {
                    return(new ApiResponse(StatusCode.Success));
                }
                else
                {
                    return(new ApiResponse(StatusCode.BroadcastButFailedValidation));
                }
            }
            catch (Exception ex)
            {
                if (_logger.IsWarn())
                {
                    Log.ApiErrorPublishBlock(_logger, ex);
                }
                throw;
            }
        }