/// <summary> /// Adds the coin. /// </summary> /// <returns>The coin.</returns> /// <param name="coin">Coin.</param> public async Task <byte[]> AddCoin(CoinProto coin) { if (coin == null) { throw new ArgumentNullException(nameof(coin)); } try { var coinHasElements = coin.Validate().Any(); if (!coinHasElements) { var blockIDExist = await unitOfWork.BlockID.HasCoin(coin.Stamp, coin.Version); if (blockIDExist) { return(null); } var blockGraphExist = await unitOfWork.BlockGraph.GetFirstOrDefault(x => x.Block.Hash.Equals(coin.Stamp) && x.Block.SignedBlock.Coin.Version.Equals(coin.Version) && x.Block.Node.Equals(httpService.NodeIdentity)); if (blockGraphExist != null) { return(null); } var blockGraph = new BlockGraphProto { Block = new BlockIDProto { Node = httpService.NodeIdentity, Hash = coin.Stamp, SignedBlock = new BlockProto { Coin = coin, Key = coin.Stamp } }, Deps = new List <DepProto>() }; var graphProto = await blockGraphService.SetBlockGraph(blockGraph); if (graphProto == null) { return(null); } var block = Util.SerializeProto(graphProto.Block); return(block); } } catch (Exception ex) { logger.LogError($"<<< CoinService.AddCoin >>>: {ex.ToString()}"); } return(null); }
public async Task <IActionResult> AddBlock([FromBody] byte[] blockGraph) { try { var blockGrpahProto = Util.DeserializeProto <BlockGraphProto>(blockGraph); var block = await blockGraphService.SetBlockGraph(blockGrpahProto); return(new ObjectResult(new { protobuf = Util.SerializeProto(block) })); } catch (Exception ex) { logger.LogError($"<<< AddBlock - Controller >>>: {ex.ToString()}"); } return(new StatusCodeResult(StatusCodes.Status500InternalServerError)); }