public Block CreateNextBlockWithCoinbase(PubKey pubkey, Money value, DateTimeOffset now, ConsensusFactory consensusFactory) { Block block = consensusFactory.CreateBlock(); block.Header.Nonce = RandomUtils.GetUInt32(); block.Header.HashPrevBlock = this.GetHash(); block.Header.BlockTime = now; var tx = block.AddTransaction(consensusFactory.CreateTransaction()); tx.AddInput(new TxIn() { ScriptSig = new Script(Op.GetPushOp(RandomUtils.GetBytes(30))) }); tx.Outputs.Add(new TxOut() { Value = value, ScriptPubKey = PayToPubkeyHashTemplate.Instance.GenerateScriptPubKey(pubkey) }); return(block); }
public static Block ParseJson(Network network, string json) { var formatter = new BlockExplorerFormatter(); JObject block = JObject.Parse(json); JArray txs = (JArray)block["tx"]; Block blk = network.Consensus.ConsensusFactory.CreateBlock(); blk.Header.Bits = new Target((uint)block["bits"]); blk.Header.BlockTime = Utils.UnixTimeToDateTime((uint)block["time"]); blk.Header.Nonce = (uint)block["nonce"]; blk.Header.Version = (int)block["ver"]; blk.Header.HashPrevBlock = uint256.Parse((string)block["prev_block"]); blk.Header.HashMerkleRoot = uint256.Parse((string)block["mrkl_root"]); foreach (JToken tx in txs) { blk.AddTransaction(formatter.Parse((JObject)tx)); } return(blk); }
public Block CreateNextBlockWithCoinbase(BitcoinAddress address, int height, DateTimeOffset now) { if (address == null) { throw new ArgumentNullException("address"); } Block block = new Block(); block.Header.Nonce = RandomUtils.GetUInt32(); block.Header.HashPrevBlock = this.GetHash(); block.Header.BlockTime = now; var tx = block.AddTransaction(new Transaction()); tx.AddInput(new TxIn() { ScriptSig = new Script(Op.GetPushOp(RandomUtils.GetBytes(30))) }); tx.Outputs.Add(new TxOut(address.Network.GetReward(height), address) { Value = address.Network.GetReward(height) }); return(block); }
public void P2PKH_GetSender_Fails() { var failResult = GetSenderResult.CreateFailure("String error"); this.senderRetriever.Setup(x => x.GetSender(It.IsAny <Transaction>(), It.IsAny <MempoolCoinView>())) .Returns(failResult); this.senderRetriever.Setup(x => x.GetSender(It.IsAny <Transaction>(), It.IsAny <ICoinView>(), It.IsAny <IList <Transaction> >())) .Returns(failResult); Transaction transaction = this.network.CreateTransaction(); transaction.Outputs.Add(new TxOut(100, new Script(new byte[] { (byte)ScOpcodeType.OP_CREATECONTRACT }))); // Mempool check fails Assert.ThrowsAny <ConsensusErrorException>(() => this.rule.CheckTransaction(new MempoolValidationContext(transaction, new MempoolValidationState(false)))); // Block validation check fails Block block = this.network.CreateBlock(); block.AddTransaction(transaction); Assert.ThrowsAnyAsync <ConsensusErrorException>(() => this.rule.RunAsync(new RuleContext(new ValidationContext { BlockToValidate = block }, DateTimeOffset.Now))); }
public void P2PKH_GetSender_Passes() { var successResult = GetSenderResult.CreateSuccess(new uint160(0)); this.senderRetriever.Setup(x => x.GetSender(It.IsAny <Transaction>(), It.IsAny <MempoolCoinView>())) .Returns(successResult); this.senderRetriever.Setup(x => x.GetSender(It.IsAny <Transaction>(), It.IsAny <ICoinView>(), It.IsAny <IList <Transaction> >())) .Returns(successResult); Transaction transaction = this.network.CreateTransaction(); transaction.Outputs.Add(new TxOut(100, new Script(new byte[] { (byte)ScOpcodeType.OP_CREATECONTRACT }))); // Mempool check works this.rule.CheckTransaction(new MempoolValidationContext(transaction, new MempoolValidationState(false))); // Block validation check works Block block = this.network.CreateBlock(); block.AddTransaction(transaction); this.rule.RunAsync(new RuleContext(new ValidationContext { BlockToValidate = block }, DateTimeOffset.Now)); }
public static Block ParseJson(string json) { var formatter = new BlockExplorerFormatter(); var block = JObject.Parse(json); var txs = (JArray)block["tx"]; Block blk = new Block(); blk.Header.Bits = new Target((uint)block["bits"]); blk.Header.BlockTime = Utils.UnixTimeToDateTime((uint)block["time"]); blk.Header.Nonce = (uint)block["nonce"]; blk.Header.Version = (int)block["ver"]; blk.Header.HashPrevBlock = uint256.Parse((string)block["prev_block"]); blk.Header.HashMerkleRoot = uint256.Parse((string)block["mrkl_root"]); foreach (var tx in txs) { blk.AddTransaction(formatter.Parse((JObject)tx)); } return blk; }
public Block CreateNextBlockWithCoinbase(PubKey pubkey, Money value, DateTimeOffset now) { Block block = new Block(); block.Header.Nonce = RandomUtils.GetUInt32(); block.Header.HashPrevBlock = this.GetHash(); block.Header.BlockTime = now; var tx = block.AddTransaction(new Transaction()); tx.AddInput(new TxIn() { ScriptSig = new Script(Op.GetPushOp(RandomUtils.GetBytes(30))) }); tx.Outputs.Add(new TxOut() { Value = value, ScriptPubKey = PayToPubkeyHashTemplate.Instance.GenerateScriptPubKey(pubkey) }); return block; }
public Block CreateNextBlockWithCoinbase(BitcoinAddress address, int height, DateTimeOffset now) { if (address == null) throw new ArgumentNullException("address"); Block block = new Block(); block.Header.Nonce = RandomUtils.GetUInt32(); block.Header.HashPrevBlock = this.GetHash(); block.Header.BlockTime = now; var tx = block.AddTransaction(new Transaction()); tx.AddInput(new TxIn() { ScriptSig = new Script(Op.GetPushOp(RandomUtils.GetBytes(30))) }); tx.Outputs.Add(new TxOut(address.Network.GetReward(height), address) { Value = address.Network.GetReward(height) }); return block; }