示例#1
0
        public static IEnumerable <GeneralStateTest> Convert(string json)
        {
            Dictionary <string, GeneralStateTestJson> testsInFile = _serializer.Deserialize <Dictionary <string, GeneralStateTestJson> >(json);
            List <GeneralStateTest> tests = new List <GeneralStateTest>();

            foreach (KeyValuePair <string, GeneralStateTestJson> namedTest in testsInFile)
            {
                tests.AddRange(Convert(namedTest.Key, namedTest.Value));
            }

            return(tests);
        }
示例#2
0
        public void Can_unlock_test_accounts(string keyJson)
        {
            EthereumJsonSerializer serializer = new EthereumJsonSerializer();
            KeyStoreItem           item       = serializer.Deserialize <KeyStoreItem>(keyJson);

            SecureString securePassword = new SecureString();
            string       password       = "******";

            for (int i = 0; i < password.Length; i++)
            {
                securePassword.AppendChar(password[i]);
            }

            securePassword.MakeReadOnly();

            _store.StoreKey(new Address(item.Address), item);
            try
            {
                (PrivateKey key, Result result) = _store.GetKey(new Address(item.Address), securePassword);
                Assert.AreEqual(ResultType.Success, result.ResultType, item.Address + " " + result.Error);
                Assert.AreEqual(key.Address.ToString(false, false), item.Address);
            }
            finally
            {
                _store.DeleteKey(new Address(item.Address));
            }
        }
示例#3
0
        public void Same_storage_format_as_in_geth(string keyJson)
        {
            EthereumJsonSerializer serializer = new EthereumJsonSerializer();
            KeyStoreItem           item       = serializer.Deserialize <KeyStoreItem>(keyJson);

            SecureString securePassword = new SecureString();
            string       password       = "******";

            for (int i = 0; i < password.Length; i++)
            {
                securePassword.AppendChar(password[i]);
            }

            Address address = new Address(item.Address);

            _store.StoreKey(address, item);

            try
            {
                string[] files = _store.FindKeyFiles(address);
                Assert.AreEqual(1, files.Length);
                string text = File.ReadAllText(files[0]);
                Assert.AreEqual(keyJson, text, "same json");
            }
            finally
            {
                _store.DeleteKey(new Address(item.Address));
            }
        }
示例#4
0
        public void can_deserialize_valid_accessList()
        {
            TransactionForRpc deserializedTxForRpc = _serializer.Deserialize <TransactionForRpc>("{\"nonce\":\"0x0\",\"blockHash\":null,\"blockNumber\":null,\"transactionIndex\":null,\"to\":null,\"value\":\"0x0\",\"gasPrice\":\"0x0\",\"gas\":\"0x0\",\"input\":null,\"type\":\"0x01\",\"accessList\":[{\"address\":\"0xb7705ae4c6f81b66cdb323c65f4e8133690fc099\",\"storageKeys\":[\"0x1\",\"0x2\",\"0x3\",\"0x5\",\"0x8\"]},{\"address\":\"0x942921b14f1b1c385cd7e0cc2ef7abe5598c8358\",\"storageKeys\":[\"0x2a\"]}]}");

            _transaction            = new Transaction();
            _transaction.Type       = TxType.AccessList;
            _transaction.AccessList = GetTestAccessList();
            _transactionForRpc      = new TransactionForRpc(_transaction);

            deserializedTxForRpc.Should().BeEquivalentTo(_transactionForRpc);
        }
        protected void TestRoundtrip <T>(string json, JsonConverter converter = null)
        {
            EthereumJsonSerializer serializer = BuildSerializer();

            if (converter != null)
            {
                serializer.RegisterConverter(converter);
            }

            T      deserialized = serializer.Deserialize <T>(json);
            string result       = serializer.Serialize(deserialized);

            Assert.AreEqual(json, result);
        }
示例#6
0
        public void Test_node_info()
        {
            string serialized = RpcTest.TestSerializedRequest(_adminRpcModule, "admin_nodeInfo");
            JsonRpcSuccessResponse response = _serializer.Deserialize <JsonRpcSuccessResponse>(serialized);
            JsonSerializerSettings settings = new JsonSerializerSettings();

            settings.Converters = EthereumJsonSerializer.CommonConverters.ToList();

            NodeInfo nodeInfo = ((JObject)response.Result).ToObject <NodeInfo>(JsonSerializer.Create(settings));

            nodeInfo.Enode.Should().Be(_enodeString);
            nodeInfo.Id.Should().Be("ae3623ef35c06ab49e9ae4b9f5a2b0f1983c28f85de1ccc98e2174333fdbdf1f");
            nodeInfo.Ip.Should().Be("127.0.0.1");
            nodeInfo.Name.Should().Be(ClientVersion.Description);
            nodeInfo.ListenAddress.Should().Be("127.0.0.1:30303");
            nodeInfo.Ports.Discovery.Should().Be(_networkConfig.DiscoveryPort);
            nodeInfo.Ports.Listener.Should().Be(_networkConfig.P2PPort);

            nodeInfo.Protocols.Should().HaveCount(1);
            nodeInfo.Protocols["eth"].Difficulty.Should().Be(_blockTree.Head.TotalDifficulty ?? 0);
            nodeInfo.Protocols["eth"].HeadHash.Should().Be(_blockTree.HeadHash);
            nodeInfo.Protocols["eth"].GenesisHash.Should().Be(_blockTree.GenesisHash);
            nodeInfo.Protocols["eth"].ChainId.Should().Be(_blockTree.ChainId);
        }
        protected void TestRoundtrip <T>(T item, Func <T, T, bool> equalityComparer, JsonConverter <T> converter = null, string description = null)
        {
            EthereumJsonSerializer serializer = BuildSerializer();

            if (converter != null)
            {
                serializer.RegisterConverter(converter);
            }

            string result       = serializer.Serialize(item);
            T      deserialized = serializer.Deserialize <T>(result);

            if (equalityComparer == null)
            {
                Assert.AreEqual(item, deserialized, description);
            }
            else
            {
                Assert.True(equalityComparer(item, deserialized), description);
            }
        }
示例#8
0
        public async Task CanonicalTreeIsConsistent()
        {
            IJsonSerializer jsonSerializer         = new EthereumJsonSerializer();
            int             destinationBlockNumber = 5000;
            long?           currentBlockNumber     = null;
            Keccak?         currentHash            = null;
            JsonRpcClient?  client = new($"http://127.0.0.1:8545");

            do
            {
                string?requestedBlockNumber = currentBlockNumber == null ? "latest" : currentBlockNumber.Value.ToHexString(false);
                JsonRpcResponse <JObject>?requestResponse =
                    await client.PostAsync <JObject>("eth_getBlockByNumber", new object[] { requestedBlockNumber !, false });

                BlockForRpcForTest?block = jsonSerializer.Deserialize <BlockForRpcForTest>(requestResponse.Result.ToString());
                if (currentHash != null)
                {
                    Assert.AreEqual(currentHash, block.Hash, $"incorrect block hash found {block}");
                }

                currentHash        = block.ParentHash;
                currentBlockNumber = block.Number !.Value - 1;
            } while (currentBlockNumber != destinationBlockNumber);
        public void Can_load_access_lists()
        {
            const string lists =
                "{\"accessLists\": [[{address: \"0x0001020304050607080900010203040506070809\", storageKeys: [\"0x00\", \"0x01\"]}]]}";

            EthereumJsonSerializer serializer = new EthereumJsonSerializer();
            TransactionJson        txJson     = serializer.Deserialize <TransactionJson>(lists);

            txJson.SecretKey = TestItem.PrivateKeyA.KeyBytes;
            txJson.Value     = new UInt256[1];
            txJson.GasLimit  = new long[1];
            txJson.Data      = new byte[1][];
            txJson.AccessLists.Should().NotBeNull();
            txJson.AccessLists[0][0].Address.Should()
            .BeEquivalentTo(new Address("0x0001020304050607080900010203040506070809"));
            txJson.AccessLists[0][0].StorageKeys[1][0].Should().Be((byte)1);

            Transaction tx = JsonToEthereumTest.Convert(new PostStateJson {
                Indexes = new IndexesJson()
            }, txJson);

            tx.AccessList.Should().NotBeNull();
        }