public void TransactionsWithInconsistentGenesisHashes()
        {
            var key = PrivateKey.FromString(
                "2ed05de0b35d93e4ae801ae40c8bb4257a771ff67c1e5d1754562e4191953710"
                );
            var differentGenesisHash = BlockHash.FromString(
                "76942b42f99c28da02ed916ebd2fadb189415e8288a4bd87f9ae3594127b79e6"
                );
            var txWithDifferentGenesis = new Transaction <Arithmetic>(
                nonce: 0L,
                signer: key.ToAddress(),
                publicKey: key.PublicKey,
                genesisHash: differentGenesisHash,
                updatedAddresses: ImmutableHashSet <Address> .Empty,
                timestamp: new DateTimeOffset(2021, 9, 7, 12, 1, 12, 345, TimeSpan.FromHours(9)),
                actions: Array.Empty <Arithmetic>(),
                signature: ByteUtil.ParseHex(
                    "304402202027a31e4298c685daaa944b1d120b4e6894f3bfffa13563331c0a7071a04b" +
                    "310220167507575e982d47d7c6753b782a5f1beb6415af96e7db3ccaf83b516d5133d1"
                    )
                );
            BlockContent <Arithmetic> block = Block1.Copy();

            Transaction <Arithmetic>[] inconsistentTxs =
                block.Transactions.Append(txWithDifferentGenesis).ToArray();
            InvalidTxGenesisHashException e = Assert.Throws <InvalidTxGenesisHashException>(
                () => block.Transactions = inconsistentTxs
                );

            Assert.Equal(Block1.Transactions[0].GenesisHash, e.ExpectedGenesisHash);
            Assert.Equal(differentGenesisHash, e.ImproperGenesisHash);
            Assert.Equal(Block1.Transactions, block.Transactions);
        }
        public void TransactionsWithDuplicateNonce()
        {
            var dupTx1 = new Transaction <Arithmetic>(
                nonce: 1L,
                signer: Tx1InBlock1.Signer,
                publicKey: Tx1InBlock1.PublicKey,
                genesisHash: GenesisHash,
                updatedAddresses: ImmutableHashSet <Address> .Empty.Add(Tx1InBlock1.Signer),
                timestamp: Tx1InBlock1.Timestamp,
                actions: Array.Empty <Arithmetic>(),
                signature: ByteUtil.ParseHex(
                    "304502210099e580e8599acf0b26ad0a80315f2d488703ffde01e9449b4bf399593b8cc" +
                    "e63022002feb21bf0e4d76d25d17c8c1c4fbb3dfbda986e0693f984fbb302183ab7ece1"
                    )
                );
            var block = Block1.Copy();
            InvalidTxNonceException e = Assert.Throws <InvalidTxNonceException>(
                () => block.Transactions = new[] { Tx0InBlock1, Tx1InBlock1, dupTx1 }
                );

            Assert.Equal(dupTx1.Id, e.TxId);
            Assert.Equal(2L, e.ExpectedNonce);
            Assert.Equal(dupTx1.Nonce, e.ImproperNonce);
            Assert.Equal(Block1.Transactions, block.Transactions);
        }
        public void TransactionsWithMissingNonce()
        {
            var dupTx1 = new Transaction <Arithmetic>(
                nonce: 3L,
                signer: Tx1InBlock1.Signer,
                publicKey: Tx1InBlock1.PublicKey,
                genesisHash: GenesisHash,
                updatedAddresses: ImmutableHashSet <Address> .Empty.Add(Tx1InBlock1.Signer),
                timestamp: Tx1InBlock1.Timestamp,
                actions: Array.Empty <Arithmetic>(),
                signature: ByteUtil.ParseHex(
                    "3045022100bfdf79427028efea9449ad46fbf46d5a806694aa5bbab1a01f4c76b21acd" +
                    "cb16022057c851a01dd74797121385ccfc81e7b33842941189154b4d46d05e891a28e3eb"
                    )
                );
            var block = Block1.Copy();
            InvalidTxNonceException e = Assert.Throws <InvalidTxNonceException>(
                () => block.Transactions = new[] { Tx0InBlock1, Tx1InBlock1, dupTx1 }
                );

            Assert.Equal(dupTx1.Id, e.TxId);
            Assert.Equal(2L, e.ExpectedNonce);
            Assert.Equal(dupTx1.Nonce, e.ImproperNonce);
            Assert.Equal(Block1.Transactions, block.Transactions);
        }
        public void TotalDifficulty()
        {
            BlockMetadata a = BlockMetadata1.Copy();

            a.TotalDifficulty = BlockMetadata1.TotalDifficulty + 10;
            Assert.Equal(BlockMetadata1.TotalDifficulty + 10, a.TotalDifficulty);
            Assert.Equal(BlockMetadata1.Difficulty, a.Difficulty);

            BlockMetadata b = Block1.Copy();
            InvalidBlockTotalDifficultyException e =
                Assert.Throws <InvalidBlockTotalDifficultyException>(() => b.TotalDifficulty = -1);

            Assert.Equal(BlockMetadata1.TotalDifficulty, b.TotalDifficulty);
            Assert.Equal(BlockMetadata1.Difficulty, b.Difficulty);
            Assert.Equal(b.Difficulty, e.Difficulty);
            Assert.Equal(-1, e.TotalDifficulty);

            e = Assert.Throws <InvalidBlockTotalDifficultyException>(
                () => b.TotalDifficulty = b.Difficulty - 1L
                );
            Assert.Equal(BlockMetadata1.TotalDifficulty, b.TotalDifficulty);
            Assert.Equal(BlockMetadata1.Difficulty, b.Difficulty);
            Assert.Equal(b.Difficulty, e.Difficulty);
            Assert.Equal(b.Difficulty - 1L, e.TotalDifficulty);
        }
Exemple #5
0
 public void Copy()
 {
     AssertBlockContentsEqual(Genesis, Genesis.Copy());
     AssertBlockContentsEqual(Block1, Block1.Copy());
     AssertBlockContentsEqual(BlockPv0, BlockPv0.Copy());
 }