예제 #1
0
        public void SaveTrace(Keccak transactionHash, TransactionTrace trace)
        {
            string path = Path.Combine(_baseDir, string.Concat(transactionHash.ToString(true), ".txt"));
            string text = _jsonSerializer.Serialize(trace, true);

            File.WriteAllText(path, text);
        }
예제 #2
0
        private void VerifyProof(byte[][] proof, Keccak receiptRoot)
        {
            TrieNode node = new TrieNode(NodeType.Unknown, proof.Last());

            node.ResolveNode(null);
            TxReceipt receipt = new ReceiptMessageDecoder().Decode(node.Value.AsRlpStream());

            Assert.NotNull(receipt.Bloom);

            for (int i = proof.Length; i > 0; i--)
            {
                Keccak proofHash = Keccak.Compute(proof[i - 1]);
                if (i > 1)
                {
                    if (!new Rlp(proof[i - 2]).ToString(false).Contains(proofHash.ToString(false)))
                    {
                        throw new InvalidDataException();
                    }
                }
                else
                {
                    if (proofHash != receiptRoot)
                    {
                        throw new InvalidDataException();
                    }
                }
            }
        }
예제 #3
0
        public static byte[]? Verify(byte[][] proof, Keccak root)
        {
            if (proof.Length == 0)
            {
                return(null);
            }

            TrieNode trieNode = new(NodeType.Unknown, proof.Last());

            trieNode.ResolveNode(null);
            for (int i = proof.Length; i > 0; i--)
            {
                Keccak proofHash = Keccak.Compute(proof[i - 1]);
                if (i > 1)
                {
                    if (!new Rlp(proof[i - 2]).ToString(false).Contains(proofHash.ToString(false)))
                    {
                        throw new InvalidDataException();
                    }
                }
                else
                {
                    if (proofHash != root)
                    {
                        throw new InvalidDataException();
                    }
                }
            }

            return(trieNode.Value);
        }
예제 #4
0
        public async Task <Signature> Sign(Address address, Keccak message)
        {
            Guid?keyId = await RetrieveId(address);

            if (keyId is null)
            {
                throw new KeyNotFoundException($"Account with the given address {address} could not be found");
            }

            string signature = await _vaultService.Sign(_vaultId, keyId.Value, message.ToString());

            return(new Signature(signature));
        }
예제 #5
0
 public Transaction MapTransaction(int index, long blockNumber,
                                   Keccak blockHash, Core.Transaction transaction)
 => new Transaction
 {
     transactionIndex = index,
     blockNumber      = blockNumber,
     toAddr           = transaction.To?.ToString() ?? string.Empty,
     blockHash        = blockHash.ToString(),
     nonce            = (int)transaction.Nonce,
     fromAddr         = transaction.SenderAddress?.ToString() ?? string.Empty,
     hash             = transaction.Hash.ToString(),
     gasPrice         = (long)transaction.GasPrice,
     v        = transaction.Signature.V,
     r        = transaction.Signature.R.ToString(),
     s        = transaction.Signature.S.ToString(),
     input    = transaction.Data ?? Array.Empty <byte>(),
     gas      = (long)transaction.GasLimit,
     weiValue = transaction.Value.ToString()
 };
예제 #6
0
 private static void VerifyProof(byte[][] proof, Keccak txRoot)
 {
     for (int i = proof.Length; i > 0; i--)
     {
         Keccak proofHash = Keccak.Compute(proof[i - 1]);
         if (i > 1)
         {
             if (!new Rlp(proof[i - 2]).ToString(false).Contains(proofHash.ToString(false)))
             {
                 throw new InvalidDataException();
             }
         }
         else
         {
             if (proofHash != txRoot)
             {
                 throw new InvalidDataException();
             }
         }
     }
 }
예제 #7
0
        public void Call()
        {
            Keccak            blockHash = TestItem.KeccakA;
            TransactionForRpc tx        = new TransactionForRpc
            {
                From = TestItem.AddressA,
                To   = TestItem.AddressB
            };

            JsonRpcSuccessResponse response = new JsonRpcSuccessResponse
            {
                Id     = "id1",
                Result = "result",
            };

            _jsonRpcClient.Post <object>("proof_call", Arg.Any <ExpandoObject>(), blockHash.ToString())
            .Returns(_serializer.Serialize(response));

            JsValue value = _engine.Execute($"proof.call({_serializer.Serialize(tx)}, \"{blockHash}\")");

            Colorful.Console.WriteLine(_serializer.Serialize(value.ToObject(), true));
            Assert.AreNotEqual(JsValue.Null, value);
        }
예제 #8
0
        public async Task <bool> Verify(Address address, Keccak message, Signature signature)
        {
            if (address is null)
            {
                throw new ArgumentNullException(nameof(address));
            }

            if (signature is null)
            {
                throw new ArgumentNullException(nameof(signature));
            }

            Guid?keyId = await RetrieveId(address);

            if (keyId is null)
            {
                throw new KeyNotFoundException($"Account with the given address {address} could not be found");
            }

            string sig    = Convert.ToString(signature) !.Remove(0, 2);
            bool   result = await _vaultService.Verify(_vaultId, keyId.Value, message.ToString(), sig);

            return(result);
        }
예제 #9
0
        public void Scenario_traverse_branch_update_missing()
        {
            MemDb     db   = new MemDb();
            StateTree tree = new StateTree(new TrieStore(db, LimboLogs.Instance), LimboLogs.Instance);

            tree.Set(new Keccak("eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeb00000"), _account0);
            tree.Set(new Keccak("eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeb11111"), _account1);
            tree.Set(new Keccak("eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeb22222"), _account2);
            tree.UpdateRootHash();
            Keccak rootHash = tree.RootHash;

            Assert.AreEqual("0xc063af0bd3dd88320bc852ff8452049c42fbc06d1a69661567bd427572824cbf", rootHash.ToString(true));
            tree.Commit(0);
            Assert.AreEqual("0xc063af0bd3dd88320bc852ff8452049c42fbc06d1a69661567bd427572824cbf", rootHash.ToString(true));
        }
예제 #10
0
        public void Scenario_traverse_leaf_read_missing()
        {
            MemDb     db   = new MemDb();
            StateTree tree = new StateTree(new TrieStore(db, LimboLogs.Instance), LimboLogs.Instance);

            tree.Set(new Keccak("1111111111111111111111111111111111111111111111111111111111111111"), _account0);
            Account account = tree.Get(new Keccak("111111111111111111111111111111111111111111111111111111111ddddddd"));

            Assert.Null(account);
            tree.UpdateRootHash();
            Keccak rootHash = tree.RootHash;

            Assert.AreEqual("0x491fbb33aaff22c0a7ff68d5c81ec114dddf89d022ccdee838a0e9d6cd45cab4", rootHash.ToString(true));
            tree.Commit(0);
            Assert.AreEqual("0x491fbb33aaff22c0a7ff68d5c81ec114dddf89d022ccdee838a0e9d6cd45cab4", rootHash.ToString(true));
        }
예제 #11
0
        public void Scenario_traverse_leaf_update_with_extension()
        {
            MemDb     db   = new MemDb();
            StateTree tree = new StateTree(new TrieStore(db, LimboLogs.Instance), LimboLogs.Instance);

            tree.Set(new Keccak("eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeb11111111111111111111111111111111"), _account0);
            tree.Set(new Keccak("eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeb00000000000000000000000000000000"), _account1);
            tree.UpdateRootHash();
            Keccak rootHash = tree.RootHash;

            Assert.AreEqual("0x215a4bab4cf2d5ebbaa59c82ae94c9707fcf4cc0ca1fe7e18f918e46db428ef9", rootHash.ToString(true));
            tree.Commit(0);
            Assert.AreEqual("0x215a4bab4cf2d5ebbaa59c82ae94c9707fcf4cc0ca1fe7e18f918e46db428ef9", rootHash.ToString(true));
        }
예제 #12
0
        public void Scenario_traverse_leaf_read_matching_leaf()
        {
            MemDb     db   = new MemDb();
            StateTree tree = new StateTree(new TrieStore(db, LimboLogs.Instance), LimboLogs.Instance);

            tree.Set(new Keccak("1111111111111111111111111111111111111111111111111111111111111111"), _account0);
            tree.Set(new Keccak("1111111111111111111111111111111111111111111111111111111111111111"), null);
            tree.UpdateRootHash();
            Keccak rootHash = tree.RootHash;

            Assert.AreEqual("0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421", rootHash.ToString(true));
            tree.Commit(0);
            Assert.AreEqual("0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421", rootHash.ToString(true));
        }
예제 #13
0
        public void Scenario_traverse_leaf_update_new_value()
        {
            MemDb     db   = new MemDb();
            StateTree tree = new StateTree(new TrieStore(db, LimboLogs.Instance), LimboLogs.Instance);

            tree.Set(new Keccak("1111111111111111111111111111111111111111111111111111111111111111"), _account0);
            tree.Set(new Keccak("1111111111111111111111111111111111111111111111111111111111111111"), _account1);
            tree.UpdateRootHash();
            Keccak rootHash = tree.RootHash;

            Assert.AreEqual("0xaa5c248d4b4b8c27a654296a8e0cc51131eb9011d9166fa0fca56a966489e169", rootHash.ToString(true));
            tree.Commit(0);
            Assert.AreEqual("0xaa5c248d4b4b8c27a654296a8e0cc51131eb9011d9166fa0fca56a966489e169", rootHash.ToString(true));
        }
예제 #14
0
        public void Scenario_traverse_extension_create_new_extension()
        {
            MemDb     db   = new MemDb();
            StateTree tree = new StateTree(new TrieStore(db, LimboLogs.Instance), LimboLogs.Instance);

            tree.Set(new Keccak("eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeb00000000"), _account0);
            tree.Set(new Keccak("eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeb11111111"), _account1);
            tree.Set(new Keccak("eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeaaaaaaaaaaaaaaaab00000000"), _account2);
            tree.Set(new Keccak("eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeaaaaaaaaaaaaaaaab11111111"), _account3);
            Assert.AreEqual(0, db.ReadsCount);
            tree.UpdateRootHash();
            Keccak rootHash = tree.RootHash;

            Assert.AreEqual("0x0918112fc898173562441709a2c1cbedb80d1aaecaeadf2f3e9492eeaa568c67", rootHash.ToString(true));
            tree.Commit(0);
            Assert.AreEqual("0x0918112fc898173562441709a2c1cbedb80d1aaecaeadf2f3e9492eeaa568c67", rootHash.ToString(true));
        }
예제 #15
0
        public void Scenario_traverse_extension_delete_missing()
        {
            MemDb     db   = new MemDb();
            StateTree tree = new StateTree(new TrieStore(db, LimboLogs.Instance), LimboLogs.Instance);

            tree.Set(new Keccak("eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeb00000000"), _account0);
            tree.Set(new Keccak("eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeb11111111"), _account1);
            tree.Set(new Keccak("eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeddddddddddddddddddddddddd"), null);
            Assert.AreEqual(0, db.ReadsCount);
            tree.UpdateRootHash();
            Keccak rootHash = tree.RootHash;

            Assert.AreEqual("0xf99f1d3234bad8d63d818db36ff63eefc8916263e654db8b800d3bd03f6339a5", rootHash.ToString(true));
            tree.Commit(0);
            Assert.AreEqual("0xf99f1d3234bad8d63d818db36ff63eefc8916263e654db8b800d3bd03f6339a5", rootHash.ToString(true));
        }
예제 #16
0
        public void Scenario_traverse_extension_new_branching()
        {
            MemDb     db   = new MemDb();
            StateTree tree = new StateTree(new TrieStore(db, LimboLogs.Instance), LimboLogs.Instance);

            tree.Set(new Keccak("eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeb00000000"), _account0);
            tree.Set(new Keccak("eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeb11111111"), _account1);
            tree.Set(new Keccak("eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeedddddddddddddddddddddddd"), _account2);
            tree.UpdateRootHash();
            Keccak rootHash = tree.RootHash;

            Assert.AreEqual("0x543c960143a2a06b685d6b92f0c37000273e616bc23888521e7edf15ad06da46", rootHash.ToString(true));
            tree.Commit(0);
            Assert.AreEqual("0x543c960143a2a06b685d6b92f0c37000273e616bc23888521e7edf15ad06da46", rootHash.ToString(true));
        }
예제 #17
0
        public void Scenario_traverse_extension_read_missing()
        {
            MemDb     db   = new MemDb();
            StateTree tree = new StateTree(db);

            tree.Set(new Keccak("eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeb00000000"), _account0);
            tree.Set(new Keccak("eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeb11111111"), _account1);
            Account account = tree.Get(new Keccak("eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeedddddddddddddddddddddddd"));

            Assert.Null(account);
            tree.UpdateRootHash();
            Keccak rootHash = tree.RootHash;

            Assert.AreEqual("0xf99f1d3234bad8d63d818db36ff63eefc8916263e654db8b800d3bd03f6339a5", rootHash.ToString(true));
            tree.Commit();
            Assert.AreEqual("0xf99f1d3234bad8d63d818db36ff63eefc8916263e654db8b800d3bd03f6339a5", rootHash.ToString(true));
        }
예제 #18
0
        public void Scenario_traverse_branch_delete_missing()
        {
            MemDb     db   = new MemDb();
            StateTree tree = new StateTree(new TrieStore(db, LimboLogs.Instance), LimboLogs.Instance);

            tree.Set(new Keccak("eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeb00000"), _account0);
            tree.Set(new Keccak("eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeb11111"), _account1);
            tree.Set(new Keccak("eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeb22222"), null);
            tree.UpdateRootHash();
            Keccak rootHash = tree.RootHash;

            Assert.AreEqual("0x94a193704e99c219d9a21428eb37d6d2d71b3d2cea80c77ff0e201c0df70a283", rootHash.ToString(true));
            tree.Commit(0);
            Assert.AreEqual("0x94a193704e99c219d9a21428eb37d6d2d71b3d2cea80c77ff0e201c0df70a283", rootHash.ToString(true));
        }
예제 #19
0
 /// <summary>
 /// Transforms <paramref name="keccak"/> to a unified document id.
 /// </summary>
 public static string AsDocumentId(this Keccak keccak) => keccak.ToString(false);
예제 #20
0
        public void Scenario_traverse_branch_read_missing()
        {
            MemDb     db   = new MemDb();
            StateTree tree = new StateTree(db);

            tree.Set(new Keccak("eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeb00000"), _account0);
            tree.Set(new Keccak("eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeb11111"), _account1);
            Account account = tree.Get(new Keccak("eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeb22222"));

            Assert.Null(account);
            tree.UpdateRootHash();
            Keccak rootHash = tree.RootHash;

            Assert.AreEqual("0x94a193704e99c219d9a21428eb37d6d2d71b3d2cea80c77ff0e201c0df70a283", rootHash.ToString(true));
            tree.Commit();
            Assert.AreEqual("0x94a193704e99c219d9a21428eb37d6d2d71b3d2cea80c77ff0e201c0df70a283", rootHash.ToString(true));
        }
예제 #21
0
        public void Scenario_traverse_leaf_delete_matching_leaf()
        {
            MemDb     db   = new MemDb();
            StateTree tree = new StateTree(db);

            tree.Set(new Keccak("1111111111111111111111111111111111111111111111111111111111111111"), _account0);
            Account account = tree.Get(new Keccak("1111111111111111111111111111111111111111111111111111111111111111"));

            Assert.NotNull(account);
            tree.UpdateRootHash();
            Keccak rootHash = tree.RootHash;

            Assert.AreEqual("0x491fbb33aaff22c0a7ff68d5c81ec114dddf89d022ccdee838a0e9d6cd45cab4", rootHash.ToString(true));
            tree.Commit();
            Assert.AreEqual("0x491fbb33aaff22c0a7ff68d5c81ec114dddf89d022ccdee838a0e9d6cd45cab4", rootHash.ToString(true));
        }
예제 #22
0
 public override void Serialize(BsonSerializationContext context, BsonSerializationArgs args, Keccak?value)
 => context.Writer.WriteString(value?.ToString() ?? string.Empty);
예제 #23
0
 public override string ToString() => _topic.ToString();