public void RepositoriesFullInsert() { DigitalSignatureUtils.AssignKeyPair("addr3"); TransactionPoolRepository tpr = new TransactionPoolRepository(); TransactionPool tp = new TransactionPool { Timestamp = DateTime.Now }; int poolId = tpr.Add(tp); Assert.AreEqual(poolId, tpr.Get(poolId).Id); TransactionRepository tr = new TransactionRepository(); Transaction t = new Transaction { PoolId = poolId, SenderId = 6, RecipientId = 1, Amount = 10, Status = TransactionStatus.confirmed.ToString(), Timestamp = DateTime.Now }; int transactionId = tr.Add(t); Assert.AreEqual(transactionId, tr.Get(transactionId).Id); BlockRepository br = new BlockRepository(); Block b = new Block { PoolId = poolId, PreviousHash = br.Get().Last().Hash, Timestamp = DateTime.Now }; b.AssignPool(); b.ComputeHash(); b.Signature = Convert.ToBase64String( DigitalSignatureUtils.SignData( Convert.FromBase64String( b.Hash ) ) ); int blockId = br.Add(b); Assert.AreEqual(blockId, br.Get(blockId).Id); }
public Blockchain() { BlockRepository = new BlockRepository(); UserRepository = new UserRepository(); Blocks = BlockRepository.Get(); Users = UserRepository.Get(); foreach (Block block in Blocks) { block.AssignPool(); } }
public void ValidateBlockchain() { BlockRepository br = new BlockRepository(); List <Block> blocks = br.Get(); bool isValid = true; foreach (Block block in blocks) { if (!block.IsValid()) { isValid = false; } } Assert.IsTrue(isValid); }
public bool IsValid(bool verbose = false) { BlockRepository blockRepo = new BlockRepository(); List <Block> blocks = blockRepo.Get(); int index = blocks.FindIndex(x => x.Id == Id); if (index != 0) { if (!PreviousHash.Equals(blocks[index - 1].Hash)) { return(false); } } return(true); }
public async Task <ActionResult <IEnumerable <Block> > > Get( SelectParameter select, SortParameter sort, OffsetParameter offset, [Range(0, 10000)] int limit = 100) { #region validate if (sort != null && !sort.Validate("id", "level", "priority", "validations", "reward", "fees")) { return(new BadRequest($"{nameof(sort)}", "Sorting by the specified field is not allowed.")); } #endregion if (select == null) { return(Ok(await Blocks.Get(sort, offset, limit))); } if (select.Values != null) { if (select.Values.Length == 1) { return(Ok(await Blocks.Get(sort, offset, limit, select.Values[0]))); } else { return(Ok(await Blocks.Get(sort, offset, limit, select.Values))); } } else { if (select.Fields.Length == 1) { return(Ok(await Blocks.Get(sort, offset, limit, select.Fields[0]))); } else { return(Ok(new SelectionResponse { Cols = select.Fields, Rows = await Blocks.Get(sort, offset, limit, select.Fields) })); } } }
public async Task <ActionResult <IEnumerable <Block> > > Get( AccountParameter baker, Int32Parameter level, DateTimeParameter timestamp, Int32Parameter priority, SelectParameter select, SortParameter sort, OffsetParameter offset, [Range(0, 10000)] int limit = 100, Symbols quote = Symbols.None) { #region validate if (baker != null) { if (baker.Eqx != null) { return(new BadRequest($"{nameof(baker)}.eqx", "This parameter doesn't support .eqx mode.")); } if (baker.Nex != null) { return(new BadRequest($"{nameof(baker)}.nex", "This parameter doesn't support .nex mode.")); } if (baker.Eq == -1 || baker.In?.Count == 0) { return(Ok(Enumerable.Empty <OriginationOperation>())); } } if (sort != null && !sort.Validate("id", "level", "priority", "validations", "reward", "fees")) { return(new BadRequest($"{nameof(sort)}", "Sorting by the specified field is not allowed.")); } #endregion if (select == null) { return(Ok(await Blocks.Get(baker, level, timestamp, priority, sort, offset, limit, quote))); } if (select.Values != null) { if (select.Values.Length == 1) { return(Ok(await Blocks.Get(baker, level, timestamp, priority, sort, offset, limit, select.Values[0], quote))); } else { return(Ok(await Blocks.Get(baker, level, timestamp, priority, sort, offset, limit, select.Values, quote))); } } else { if (select.Fields.Length == 1) { return(Ok(await Blocks.Get(baker, level, timestamp, priority, sort, offset, limit, select.Fields[0], quote))); } else { return(Ok(new SelectionResponse { Cols = select.Fields, Rows = await Blocks.Get(baker, level, timestamp, priority, sort, offset, limit, select.Fields, quote) })); } } }
public void CreateABlock() { try { var pdo = new PromptDoubleOptions("Informe a altura da parede: ") { AllowNegative = false, AllowNone = false, AllowZero = false, DefaultValue = 2.80 }; PromptDoubleResult pdr; do { pdr = _editor.GetDouble(pdo); if (pdr.Status == PromptStatus.Cancel) { return; } if (pdr.Status == PromptStatus.OK) { var altura = pdr.Value; var namesOfBlocks = Enum.GetNames(typeof(TipoBloco)); var pko = new PromptKeywordOptions("") { AllowNone = false, Message = "Enter an option " }; foreach (var name in namesOfBlocks) { pko.Keywords.Add(name); } var pr = _editor.GetKeywords(pko); if (pr.Status == PromptStatus.OK) { var blk = new BlockRepository <Entity, AttributeDefinition>(); var tipBloc = (TipoBloco)Enum.Parse(typeof(TipoBloco), pr.StringResult, true); using (var ces = new CustomEntityService()) { var blockId = blk.Get(tipBloc.ToString()); if (blockId == ObjectId.Null) { _editor.WriteMessage( $"\nBloco não existente.\nCriando bloco {tipBloc.ToString()}."); var(entities, attributes) = ces.GetEntitiesToBlock(tipBloc); blockId = blk.CreatingABlock(tipBloc.ToString(), entities, attributes, new Point3d(0, 0.07, 0)); } if (blockId != ObjectId.Null) { var pio = new PromptIntegerOptions(""); pio.Keywords.Add("1 - Inserir um bloco"); pio.Keywords.Add("2 - Inserir multiplos blocos"); var op = _editor.GetInteger(pio); if (op.Status == PromptStatus.Cancel) { return; } if (op.Status == PromptStatus.OK) { PromptPointResult pPtRes; var pPtOpts = new PromptPointOptions(""); switch (op.Value) { case 1: pPtOpts.Message = "\nInforme o ponto de inserção: "; pPtRes = _editor.GetPoint(pPtOpts); if (pPtRes.Status == PromptStatus.Cancel) { return; } blk.InsertBlock(tipBloc.ToString(), pPtRes.Value, 0, altura); break; case 2: pPtOpts.Message = "\nInforme o ponto inicial: "; pPtRes = _editor.GetPoint(pPtOpts); if (pPtRes.Status == PromptStatus.Cancel) { return; } var ptStart = pPtRes.Value; pPtOpts.Message = "\nInforme o ponto final: "; pPtOpts.UseBasePoint = true; pPtOpts.BasePoint = ptStart; pPtRes = _editor.GetPoint(pPtOpts); if (pPtRes.Status == PromptStatus.Cancel) { return; } var ptEnd = pPtRes.Value; var tamanho = CustomEntityService.GetDistanceBetweenTwoPoints(ptStart, ptEnd); var angulo = CustomEntityService.GetAngleBetweenTwoPoints(ptStart, ptEnd); var blkComprimento = (double.Parse(tipBloc.ToString() .Split(new[] { "x" }, StringSplitOptions.RemoveEmptyEntries) .LastOrDefault() ?? throw new InvalidOperationException( "Operação de conversão inválida.")) + 1) / 100; var dist = 0.0; do { var pointInsert = CustomEntityService.GetPolarPoints(ptStart, angulo, dist); blk.InsertBlock(tipBloc.ToString(), pointInsert, angulo, altura); dist += blkComprimento; } while (dist < tamanho); break; default: _editor.WriteMessage("Opção inválida."); return; } } } } } } } while (pdr.Status != PromptStatus.OK); } catch (ArgumentNullException ex) { _editor.WriteMessage(ex.Message); } catch (System.Exception ex) { _editor.WriteMessage(ex.Message); } }