From 92dd3d13af93a01370df1c5de71507e1c0f92cda Mon Sep 17 00:00:00 2001 From: Pana Date: Thu, 12 Sep 2024 11:25:40 +0800 Subject: [PATCH 1/7] add eth api --- Cargo.lock | 385 ++++++++++++++++-- Cargo.toml | 2 +- .../src/pos/common/time-service/Cargo.toml | 2 +- crates/rpc/rpc-eth-api/Cargo.toml | 19 + crates/rpc/rpc-eth-api/src/eth.rs | 329 +++++++++++++++ crates/rpc/rpc-eth-api/src/lib.rs | 1 + crates/rpc/rpc-eth-types/src/block.rs | 4 +- crates/rpc/rpc-eth-types/src/receipt.rs | 2 +- crates/rpc/rpc-eth-types/src/sync.rs | 4 +- crates/util/solidity-abi-derive/Cargo.toml | 2 +- 10 files changed, 702 insertions(+), 48 deletions(-) create mode 100644 crates/rpc/rpc-eth-api/Cargo.toml create mode 100644 crates/rpc/rpc-eth-api/src/eth.rs create mode 100644 crates/rpc/rpc-eth-api/src/lib.rs diff --git a/Cargo.lock b/Cargo.lock index 7480ee47bf..d74c33bc5a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -191,7 +191,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5277af0cbcc483ee6ad2c1e818090b5928d27f04fd6580680f31c1cf8068bcc2" dependencies = [ "alloy-rlp", - "bytes 1.4.0", + "bytes 1.7.1", "cfg-if 1.0.0", "const-hex", "derive_more", @@ -214,7 +214,7 @@ checksum = "8d58d9f5da7b40e9bfff0b7e7816700be4019db97d4b6359fe7f94a9e22e42ac" dependencies = [ "alloy-rlp-derive", "arrayvec 0.7.4", - "bytes 1.4.0", + "bytes 1.7.1", ] [[package]] @@ -571,6 +571,12 @@ version = "0.3.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c9ff149ed9780025acfdb36862d35b28856bb693ceb451259a7164442f22fdc3" +[[package]] +name = "atomic-waker" +version = "1.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1505bd5d3d116872e7271a6d4e16d81d0c8570876c8de68093a09ac269d8aac0" + [[package]] name = "attohttpc" version = "0.10.1" @@ -663,6 +669,12 @@ version = "0.21.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a4a4ddaa51a5bc52a6948f74c06d20aaaddb71924eab79b8c97a8c556e942d6a" +[[package]] +name = "base64" +version = "0.22.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "72b3254f16251a8381aa12e40e3c4d2f0199f8c6508fbecb9d91f575e0fbb8c6" + [[package]] name = "base64ct" version = "1.1.1" @@ -714,7 +726,7 @@ dependencies = [ "proc-macro2 1.0.85", "quote 1.0.36", "regex", - "rustc-hash", + "rustc-hash 1.1.0", "shlex", "syn 1.0.109", ] @@ -1008,9 +1020,9 @@ checksum = "0e4cec68f03f32e44924783795810fa50a7035d8c8ebe78580ad7e6c703fba38" [[package]] name = "bytes" -version = "1.4.0" +version = "1.7.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "89b2fd2a0dcf38d7971e2194b6b6eebab45ae01067456a7fd93d5547a61b70be" +checksum = "8318a53db07bb3f8dca91a600466bdb3f2eaadeedfdbcf02e1accbad9271ba50" dependencies = [ "serde", ] @@ -1674,7 +1686,7 @@ dependencies = [ "serde", "serde_derive", "serde_json", - "smallvec 1.10.0", + "smallvec 1.13.2", "tempdir", "time 0.1.45", "tiny-keccak 1.5.0", @@ -2523,7 +2535,7 @@ dependencies = [ "bitvec 1.0.1", "bls-signatures", "byteorder", - "bytes 1.4.0", + "bytes 1.7.1", "cfx-types", "cfxkey", "criterion", @@ -2778,7 +2790,7 @@ dependencies = [ "aes-gcm", "anyhow", "bcs", - "bytes 1.4.0", + "bytes 1.7.1", "cfx-types", "chrono", "diem-crypto", @@ -3311,7 +3323,7 @@ checksum = "139834ddba373bbdd213dffe02c8d110508dcf1726c2be27e8d1f7d7e1856418" dependencies = [ "arrayvec 0.7.4", "auto_impl", - "bytes 1.4.0", + "bytes 1.7.1", ] [[package]] @@ -3785,7 +3797,7 @@ version = "0.3.16" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5be7b54589b581f624f566bf5d8eb2bab1db736c51528720b6bd36b96b55924d" dependencies = [ - "bytes 1.4.0", + "bytes 1.7.1", "fnv", "futures-core", "futures-sink", @@ -3798,6 +3810,25 @@ dependencies = [ "tracing", ] +[[package]] +name = "h2" +version = "0.4.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "524e8ac6999421f49a846c2d4411f337e53497d8ec55d67753beffa43c5d9205" +dependencies = [ + "atomic-waker", + "bytes 1.7.1", + "fnv", + "futures-core", + "futures-sink", + "http 1.1.0", + "indexmap 2.2.5", + "slab", + "tokio 1.26.0", + "tokio-util", + "tracing", +] + [[package]] name = "half" version = "1.8.2" @@ -4009,7 +4040,18 @@ version = "0.2.9" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "bd6effc99afb63425aff9b05836f029929e345a6148a14b7ecd5ab67af944482" dependencies = [ - "bytes 1.4.0", + "bytes 1.7.1", + "fnv", + "itoa 1.0.6", +] + +[[package]] +name = "http" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "21b9ddb458710bc376481b842f5da65cdf31522de232c1ca8146abce2a358258" +dependencies = [ + "bytes 1.7.1", "fnv", "itoa 1.0.6", ] @@ -4032,11 +4074,34 @@ version = "0.4.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d5f38f16d184e36f2408a55281cd658ecbd3ca05cce6d6510a176eca393e26d1" dependencies = [ - "bytes 1.4.0", + "bytes 1.7.1", "http 0.2.9", "pin-project-lite 0.2.9", ] +[[package]] +name = "http-body" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1efedce1fb8e6913f23e0c92de8e62cd5b772a67e7b3946df930a62566c93184" +dependencies = [ + "bytes 1.7.1", + "http 1.1.0", +] + +[[package]] +name = "http-body-util" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "793429d76616a256bcb62c2a2ec2bed781c8307e797e2598c50010f2bee2544f" +dependencies = [ + "bytes 1.7.1", + "futures-util", + "http 1.1.0", + "http-body 1.0.1", + "pin-project-lite 0.2.9", +] + [[package]] name = "httparse" version = "1.8.0" @@ -4100,7 +4165,7 @@ version = "0.14.25" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "cc5e554ff619822309ffd57d8734d77cd5ce6238bc956f037ea06c58238c9899" dependencies = [ - "bytes 1.4.0", + "bytes 1.7.1", "futures-channel", "futures-core", "futures-util", @@ -4118,19 +4183,56 @@ dependencies = [ "want 0.3.0", ] +[[package]] +name = "hyper" +version = "1.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "50dfd22e0e76d0f662d429a5f80fcaf3855009297eab6a0a9f8543834744ba05" +dependencies = [ + "bytes 1.7.1", + "futures-channel", + "futures-util", + "h2 0.4.6", + "http 1.1.0", + "http-body 1.0.1", + "httparse", + "httpdate", + "itoa 1.0.6", + "pin-project-lite 0.2.9", + "smallvec 1.13.2", + "tokio 1.26.0", +] + [[package]] name = "hyper-tls" version = "0.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d6183ddfa99b85da61a140bea0efc93fdf56ceaa041b37d553518030827f9905" dependencies = [ - "bytes 1.4.0", + "bytes 1.7.1", "hyper 0.14.25", "native-tls", "tokio 1.26.0", "tokio-native-tls", ] +[[package]] +name = "hyper-util" +version = "0.1.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "da62f120a8a37763efb0cf8fdf264b884c7b8b9ac8660b900c8661030c00e6ba" +dependencies = [ + "bytes 1.7.1", + "futures-util", + "http 1.1.0", + "http-body 1.0.1", + "hyper 1.4.1", + "pin-project-lite 0.2.9", + "tokio 1.26.0", + "tower", + "tower-service", +] + [[package]] name = "iana-time-zone" version = "0.1.54" @@ -4323,7 +4425,7 @@ version = "0.5.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d2ef03268010ccf98c178eed83aa7377b7f6531f8ec8d43a256902c24cadac60" dependencies = [ - "bytes 1.4.0", + "bytes 1.7.1", "futures 0.3.27", "reqwest", "serde", @@ -4589,6 +4691,95 @@ dependencies = [ "slab", ] +[[package]] +name = "jsonrpsee" +version = "0.24.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8fd1ead9fb95614e8dc5556d12a8681c2f6d352d0c1d3efc8708c7ccbba47bc6" +dependencies = [ + "jsonrpsee-core", + "jsonrpsee-proc-macros", + "jsonrpsee-server", + "jsonrpsee-types", + "tokio 1.26.0", + "tracing", +] + +[[package]] +name = "jsonrpsee-core" +version = "0.24.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ff79651479f69ada7bda604ef2acf3f1aa50755d97cc36d25ff04c2664f9d96f" +dependencies = [ + "async-trait", + "bytes 1.7.1", + "futures-util", + "http 1.1.0", + "http-body 1.0.1", + "http-body-util", + "jsonrpsee-types", + "parking_lot 0.12.1", + "rand 0.8.5", + "rustc-hash 2.0.0", + "serde", + "serde_json", + "thiserror", + "tokio 1.26.0", + "tracing", +] + +[[package]] +name = "jsonrpsee-proc-macros" +version = "0.24.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a0d4c6bec4909c966f59f52db3655c0e9d4685faae8b49185973d9d7389bb884" +dependencies = [ + "heck 0.5.0", + "proc-macro-crate 3.2.0", + "proc-macro2 1.0.85", + "quote 1.0.36", + "syn 2.0.66", +] + +[[package]] +name = "jsonrpsee-server" +version = "0.24.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ebe2198e5fd96cf2153ecc123364f699b6e2151317ea09c7bf799c43c2fe1415" +dependencies = [ + "futures-util", + "http 1.1.0", + "http-body 1.0.1", + "http-body-util", + "hyper 1.4.1", + "hyper-util", + "jsonrpsee-core", + "jsonrpsee-types", + "pin-project", + "route-recognizer", + "serde", + "serde_json", + "soketto", + "thiserror", + "tokio 1.26.0", + "tokio-stream", + "tokio-util", + "tower", + "tracing", +] + +[[package]] +name = "jsonrpsee-types" +version = "0.24.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "531e386460425e49679587871a056f2895a47dade21457324ad1262cd78ef6d9" +dependencies = [ + "http 1.1.0", + "serde", + "serde_json", + "thiserror", +] + [[package]] name = "k256" version = "0.13.3" @@ -4649,7 +4840,7 @@ checksum = "03080afe6f42cd996da9f568d6add5d7fb5ee2ea7fb7802d2d2cbd836958fd87" dependencies = [ "parity-bytes", "parity-util-mem", - "smallvec 1.10.0", + "smallvec 1.13.2", ] [[package]] @@ -4935,7 +5126,7 @@ dependencies = [ "jemallocator", "parking_lot 0.11.2", "slab", - "smallvec 1.10.0", + "smallvec 1.13.2", "winapi 0.3.9", ] @@ -5629,7 +5820,7 @@ version = "3.6.9" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "be30eaf4b0a9fba5336683b38de57bb86d179a35862ba6bfcf57625d006bde5b" dependencies = [ - "proc-macro-crate 2.0.2", + "proc-macro-crate 2.0.0", "proc-macro2 1.0.85", "quote 1.0.36", "syn 1.0.109", @@ -5774,7 +5965,7 @@ dependencies = [ "cloudabi", "libc", "redox_syscall 0.1.57", - "smallvec 1.10.0", + "smallvec 1.13.2", "winapi 0.3.9", ] @@ -5790,7 +5981,7 @@ dependencies = [ "libc", "petgraph", "redox_syscall 0.2.16", - "smallvec 1.10.0", + "smallvec 1.13.2", "thread-id", "winapi 0.3.9", ] @@ -5804,7 +5995,7 @@ dependencies = [ "cfg-if 1.0.0", "libc", "redox_syscall 0.2.16", - "smallvec 1.10.0", + "smallvec 1.13.2", "windows-sys 0.45.0", ] @@ -5929,22 +6120,22 @@ dependencies = [ [[package]] name = "pin-project" -version = "1.0.12" +version = "1.1.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ad29a609b6bcd67fee905812e544992d216af9d755757c05ed2d0e15a74c6ecc" +checksum = "b6bf43b791c5b9e34c3d182969b4abb522f9343702850a2e57f460d00d09b4b3" dependencies = [ "pin-project-internal", ] [[package]] name = "pin-project-internal" -version = "1.0.12" +version = "1.1.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "069bdb1e05adc7a8990dce9cc75370895fbe4e3d58b9b73bf1aee56359344a55" +checksum = "2f38a4412a78282e09a2cf38d195ea5420d15ba0602cb375210efbc877243965" dependencies = [ "proc-macro2 1.0.85", "quote 1.0.36", - "syn 1.0.109", + "syn 2.0.66", ] [[package]] @@ -6260,14 +6451,22 @@ dependencies = [ [[package]] name = "proc-macro-crate" -version = "2.0.2" +version = "2.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b00f26d3400549137f92511a46ac1cd8ce37cb5598a96d382381458b992a5d24" +checksum = "7e8366a6159044a37876a2b9817124296703c586a5c92e2c53751fa06d8d43e8" dependencies = [ - "toml_datetime", "toml_edit 0.20.2", ] +[[package]] +name = "proc-macro-crate" +version = "3.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8ecf48c7ca261d60b74ab1a7b20da18bede46776b2e55535cb958eb595c5fa7b" +dependencies = [ + "toml_edit 0.22.20", +] + [[package]] name = "proc-macro-error" version = "1.0.4" @@ -6764,7 +6963,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0ba30cc2c0cd02af1222ed216ba659cdb2f879dfe3181852fe7c50b1d0005949" dependencies = [ "base64 0.21.0", - "bytes 1.4.0", + "bytes 1.7.1", "encoding_rs", "futures-core", "futures-util", @@ -6908,7 +7107,7 @@ version = "0.5.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "bb919243f34364b6bd2fc10ef797edbfa75f33c252e7998527479c6d6b47e1ec" dependencies = [ - "bytes 1.4.0", + "bytes 1.7.1", "rustc-hex", ] @@ -6931,6 +7130,12 @@ dependencies = [ "librocksdb_sys", ] +[[package]] +name = "route-recognizer" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "afab94fb28594581f62d981211a9a4d53cc8130bbcbbb89a0440d9b8e81a7746" + [[package]] name = "rpassword" version = "4.0.5" @@ -6951,6 +7156,16 @@ dependencies = [ "winapi 0.3.9", ] +[[package]] +name = "rpc-eth-api" +version = "2.4.0" +dependencies = [ + "cfx-rpc-eth-types", + "cfx-rpc-primitives", + "cfx-types", + "jsonrpsee", +] + [[package]] name = "ruint" version = "1.12.3" @@ -6960,7 +7175,7 @@ dependencies = [ "alloy-rlp", "ark-ff 0.3.0", "ark-ff 0.4.2", - "bytes 1.4.0", + "bytes 1.7.1", "fastrlp", "num-bigint 0.4.4", "num-traits", @@ -7001,6 +7216,12 @@ version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "08d43f7aa6b08d49f382cde6a7982047c3426db949b1424bc4b7ec9ae12c6ce2" +[[package]] +name = "rustc-hash" +version = "2.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "583034fd73374156e66797ed8e5b0d5690409c9226b22d87cb7f19821c05d152" + [[package]] name = "rustc-hex" version = "2.1.0" @@ -7477,6 +7698,17 @@ dependencies = [ "opaque-debug 0.2.3", ] +[[package]] +name = "sha1" +version = "0.10.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e3bf829a2d51ab4a5ddf1352d8470c140cadc8301b2ae1789db023f01cedd6ba" +dependencies = [ + "cfg-if 1.0.0", + "cpufeatures", + "digest 0.10.7", +] + [[package]] name = "sha2" version = "0.9.9" @@ -7612,9 +7844,9 @@ dependencies = [ [[package]] name = "smallvec" -version = "1.10.0" +version = "1.13.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a507befe795404456341dfab10cef66ead4c041f62b8b11bbb92bffe5d0953e0" +checksum = "3c5e1a9a646d36c3599cd173a41282daf47c44583ad367b8e6837255952e5c67" [[package]] name = "smart-default" @@ -7647,6 +7879,22 @@ dependencies = [ "winapi 0.3.9", ] +[[package]] +name = "soketto" +version = "0.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "37468c595637c10857701c990f93a40ce0e357cedb0953d1c26c8d8027f9bb53" +dependencies = [ + "base64 0.22.1", + "bytes 1.7.1", + "futures 0.3.27", + "http 1.1.0", + "httparse", + "log", + "rand 0.8.5", + "sha1", +] + [[package]] name = "solidity-abi" version = "0.1.0" @@ -7662,7 +7910,7 @@ dependencies = [ name = "solidity-abi-derive" version = "0.1.0" dependencies = [ - "proc-macro-crate 1.3.1", + "proc-macro-crate 3.2.0", "proc-macro2 1.0.85", "quote 1.0.36", "syn 1.0.109", @@ -8177,7 +8425,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "03201d01c3c27a29c8a5cee5b55a93ddae1ccf6f08f65365c2c918f8c1b76f64" dependencies = [ "autocfg 1.1.0", - "bytes 1.4.0", + "bytes 1.7.1", "libc", "memchr", "mio 0.8.6", @@ -8323,6 +8571,7 @@ dependencies = [ "futures-core", "pin-project-lite 0.2.9", "tokio 1.26.0", + "tokio-util", ] [[package]] @@ -8356,7 +8605,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "53474327ae5e166530d17f2d956afcb4f8a004de581b3cae10f12006bc8163e3" dependencies = [ "async-stream", - "bytes 1.4.0", + "bytes 1.7.1", "futures-core", "tokio 1.26.0", "tokio-stream", @@ -8430,8 +8679,9 @@ version = "0.7.7" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5427d89453009325de0d8f342c9490009f76e999cb7672d77e46267448f7e6b2" dependencies = [ - "bytes 1.4.0", + "bytes 1.7.1", "futures-core", + "futures-io", "futures-sink", "pin-project-lite 0.2.9", "tokio 1.26.0", @@ -8449,9 +8699,9 @@ dependencies = [ [[package]] name = "toml_datetime" -version = "0.6.3" +version = "0.6.8" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7cda73e2f1397b1262d6dfdcef8aafae14d1de7748d66822d3bfeeb6d03e5e4b" +checksum = "0dd7358ecb8fc2f8d014bf86f6f638ce72ba252a2c3a2572f2a795f1d23efb41" [[package]] name = "toml_edit" @@ -8475,6 +8725,39 @@ dependencies = [ "winnow 0.5.40", ] +[[package]] +name = "toml_edit" +version = "0.22.20" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "583c44c02ad26b0c3f3066fe629275e50627026c51ac2e595cca4c230ce1ce1d" +dependencies = [ + "indexmap 2.2.5", + "toml_datetime", + "winnow 0.6.18", +] + +[[package]] +name = "tower" +version = "0.4.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b8fa9be0de6cf49e536ce1851f987bd21a43b771b09473c3549a6c853db37c1c" +dependencies = [ + "futures-core", + "futures-util", + "pin-project", + "pin-project-lite 0.2.9", + "tokio 1.26.0", + "tower-layer", + "tower-service", + "tracing", +] + +[[package]] +name = "tower-layer" +version = "0.3.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "121c2a6cda46980bb0fcd1647ffaf6cd3fc79a013de288782836f6df9c48780e" + [[package]] name = "tower-service" version = "0.3.2" @@ -8488,10 +8771,23 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8ce8c33a8d48bd45d624a6e523445fd21ec13d3653cd51f681abf67418f54eb8" dependencies = [ "cfg-if 1.0.0", + "log", "pin-project-lite 0.2.9", + "tracing-attributes", "tracing-core", ] +[[package]] +name = "tracing-attributes" +version = "0.1.27" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "34704c8d6ebcbc939824180af020566b01a7c01f80641264eba0999f6c2b6be7" +dependencies = [ + "proc-macro2 1.0.85", + "quote 1.0.36", + "syn 2.0.66", +] + [[package]] name = "tracing-core" version = "0.1.30" @@ -9148,6 +9444,15 @@ dependencies = [ "memchr", ] +[[package]] +name = "winnow" +version = "0.6.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "68a9bda4691f099d435ad181000724da8e5899daa10713c2d432552b9ccd3a6f" +dependencies = [ + "memchr", +] + [[package]] name = "winreg" version = "0.10.1" diff --git a/Cargo.toml b/Cargo.toml index ba916c7831..ddd5cb09be 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -56,7 +56,7 @@ members = [ "crates/cfxcore/geth-tracer", "crates/rpc/rpc-eth-types", "crates/rpc/rpc-primitives", - "crates/rpc/rpc-cfx-types", + "crates/rpc/rpc-cfx-types", "crates/rpc/rpc-eth-api", ] resolver = "2" diff --git a/crates/cfxcore/core/src/pos/common/time-service/Cargo.toml b/crates/cfxcore/core/src/pos/common/time-service/Cargo.toml index d43966cc2a..12e5fd7a4a 100644 --- a/crates/cfxcore/core/src/pos/common/time-service/Cargo.toml +++ b/crates/cfxcore/core/src/pos/common/time-service/Cargo.toml @@ -20,7 +20,7 @@ diem-infallible = { path = "../infallible" } [dev-dependencies] futures = "0.3.12" -pin-project = "1.0.5" +pin-project = "1.1.3" tokio = { version = "1.3.0", features = ["macros", "rt-multi-thread", "time"] } tokio-test = "0.4.1" diff --git a/crates/rpc/rpc-eth-api/Cargo.toml b/crates/rpc/rpc-eth-api/Cargo.toml new file mode 100644 index 0000000000..faa0d2cf89 --- /dev/null +++ b/crates/rpc/rpc-eth-api/Cargo.toml @@ -0,0 +1,19 @@ +[package] +name = "rpc-eth-api" +edition = "2021" +version.workspace = true +authors.workspace = true +description.workspace = true +documentation.workspace = true +homepage.workspace = true +keywords.workspace = true +repository.workspace = true +license-file.workspace = true + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +cfx-rpc-eth-types = { workspace = true } +jsonrpsee = { version = "0.24.4", features = ["server", "macros"] } +cfx-types = { workspace = true } +cfx-rpc-primitives = { workspace = true } diff --git a/crates/rpc/rpc-eth-api/src/eth.rs b/crates/rpc/rpc-eth-api/src/eth.rs new file mode 100644 index 0000000000..261b39092b --- /dev/null +++ b/crates/rpc/rpc-eth-api/src/eth.rs @@ -0,0 +1,329 @@ +use jsonrpsee::proc_macros::rpc; +use jsonrpsee::core::RpcResult; +use cfx_types::{U64, H256, Address, U256, H64}; +use cfx_rpc_primitives::Bytes; +use cfx_rpc_eth_types::{BlockNumber as BlockId, SyncStatus, Block, Receipt, Transaction, TransactionRequest, Header}; + +type BlockNumberOrTag = BlockId; + +type Index = u64; + +type JsonStorageKey = U256; + +type FeeHistory = (); + +#[rpc(server, namespace = "eth")] +pub trait EthApi { + /// Returns the protocol version encoded as a string. + #[method(name = "protocolVersion")] + async fn protocol_version(&self) -> RpcResult; + + /// Returns an object with data about the sync status or false. + #[method(name = "syncing")] + fn syncing(&self) -> RpcResult; + + /// Returns the client coinbase address. + #[method(name = "coinbase")] + async fn author(&self) -> RpcResult
; + + /// Returns a list of addresses owned by client. + #[method(name = "accounts")] + fn accounts(&self) -> RpcResult>; + + /// Returns the number of most recent block. + #[method(name = "blockNumber")] + fn block_number(&self) -> RpcResult; + + /// Returns the chain ID of the current network. + #[method(name = "chainId")] + async fn chain_id(&self) -> RpcResult>; + + /// Returns information about a block by hash. + #[method(name = "getBlockByHash")] + async fn block_by_hash(&self, hash: H256, full: bool) -> RpcResult>; + + /// Returns information about a block by number. + #[method(name = "getBlockByNumber")] + async fn block_by_number(&self, number: BlockNumberOrTag, full: bool) -> RpcResult>; + + /// Returns the number of transactions in a block from a block matching the given block hash. + #[method(name = "getBlockTransactionCountByHash")] + async fn block_transaction_count_by_hash(&self, hash: H256) -> RpcResult>; + + /// Returns the number of transactions in a block matching the given block number. + #[method(name = "getBlockTransactionCountByNumber")] + async fn block_transaction_count_by_number( + &self, + number: BlockNumberOrTag, + ) -> RpcResult>; + + /// Returns the number of uncles in a block from a block matching the given block hash. + #[method(name = "getUncleCountByBlockHash")] + async fn block_uncles_count_by_hash(&self, hash: H256) -> RpcResult>; + + /// Returns the number of uncles in a block with given block number. + #[method(name = "getUncleCountByBlockNumber")] + async fn block_uncles_count_by_number( + &self, + number: BlockNumberOrTag, + ) -> RpcResult>; + + /// Returns all transaction receipts for a given block. + #[method(name = "getBlockReceipts")] + async fn block_receipts(&self, block_id: BlockId) -> RpcResult>>; + + /// Returns an uncle block of the given block and index. + #[method(name = "getUncleByBlockHashAndIndex")] + async fn uncle_by_block_hash_and_index(&self, hash: H256, index: Index) + -> RpcResult>; + + /// Returns an uncle block of the given block and index. + #[method(name = "getUncleByBlockNumberAndIndex")] + async fn uncle_by_block_number_and_index( + &self, + number: BlockNumberOrTag, + index: Index, + ) -> RpcResult>; + + /// Returns the EIP-2718 encoded transaction if it exists. + /// + /// If this is a EIP-4844 transaction that is in the pool it will include the sidecar. + #[method(name = "getRawTransactionByHash")] + async fn raw_transaction_by_hash(&self, hash: H256) -> RpcResult>; + + /// Returns the information about a transaction requested by transaction hash. + #[method(name = "getTransactionByHash")] + async fn transaction_by_hash(&self, hash: H256) -> RpcResult>; + + /// Returns information about a raw transaction by block hash and transaction index position. + #[method(name = "getRawTransactionByBlockHashAndIndex")] + async fn raw_transaction_by_block_hash_and_index( + &self, + hash: H256, + index: Index, + ) -> RpcResult>; + + /// Returns information about a transaction by block hash and transaction index position. + #[method(name = "getTransactionByBlockHashAndIndex")] + async fn transaction_by_block_hash_and_index( + &self, + hash: H256, + index: Index, + ) -> RpcResult>; + + /// Returns information about a raw transaction by block number and transaction index + /// position. + #[method(name = "getRawTransactionByBlockNumberAndIndex")] + async fn raw_transaction_by_block_number_and_index( + &self, + number: BlockNumberOrTag, + index: Index, + ) -> RpcResult>; + + /// Returns information about a transaction by block number and transaction index position. + #[method(name = "getTransactionByBlockNumberAndIndex")] + async fn transaction_by_block_number_and_index( + &self, + number: BlockNumberOrTag, + index: Index, + ) -> RpcResult>; + + /// Returns information about a transaction by sender and nonce. + #[method(name = "getTransactionBySenderAndNonce")] + async fn transaction_by_sender_and_nonce( + &self, + address: Address, + nonce: U64, + ) -> RpcResult>; + + /// Returns the receipt of a transaction by transaction hash. + #[method(name = "getTransactionReceipt")] + async fn transaction_receipt(&self, hash: H256) -> RpcResult>; + + /// Returns the balance of the account of given address. + #[method(name = "getBalance")] + async fn balance(&self, address: Address, block_number: Option) -> RpcResult; + + /// Returns the value from a storage position at a given address + #[method(name = "getStorageAt")] + async fn storage_at( + &self, + address: Address, + index: JsonStorageKey, + block_number: Option, + ) -> RpcResult; + + /// Returns the number of transactions sent from an address at given block number. + #[method(name = "getTransactionCount")] + async fn transaction_count( + &self, + address: Address, + block_number: Option, + ) -> RpcResult; + + /// Returns code at a given address at given block number. + #[method(name = "getCode")] + async fn get_code(&self, address: Address, block_number: Option) -> RpcResult; + + /// Returns the block's header at given number. + #[method(name = "getHeaderByNumber")] + async fn header_by_number(&self, hash: BlockNumberOrTag) -> RpcResult>; + + /// Returns the block's header at given hash. + #[method(name = "getHeaderByHash")] + async fn header_by_hash(&self, hash: H256) -> RpcResult>; + + /// `eth_simulateV1` executes an arbitrary number of transactions on top of the requested state. + /// The transactions are packed into individual blocks. Overrides can be provided. + // #[method(name = "simulateV1")] + // async fn simulate_v1( + // &self, + // opts: SimBlock, + // block_number: Option, + // ) -> RpcResult>; + + /// Executes a new message call immediately without creating a transaction on the block chain. + #[method(name = "call")] + async fn call( + &self, + request: TransactionRequest, + block_number: Option, + // state_overrides: Option, + // block_overrides: Option>, + ) -> RpcResult; + + /// Simulate arbitrary number of transactions at an arbitrary blockchain index, with the + /// optionality of state overrides + // #[method(name = "callMany")] + // async fn call_many( + // &self, + // bundle: Bundle, + // state_context: Option, + // state_override: Option, + // ) -> RpcResult>; + + /// Generates an access list for a transaction. + /// + /// This method creates an [EIP2930](https://eips.ethereum.org/EIPS/eip-2930) type accessList based on a given Transaction. + /// + /// An access list contains all storage slots and addresses touched by the transaction, except + /// for the sender account and the chain's precompiles. + /// + /// It returns list of addresses and storage keys used by the transaction, plus the gas + /// consumed when the access list is added. That is, it gives you the list of addresses and + /// storage keys that will be used by that transaction, plus the gas consumed if the access + /// list is included. Like eth_estimateGas, this is an estimation; the list could change + /// when the transaction is actually mined. Adding an accessList to your transaction does + /// not necessary result in lower gas usage compared to a transaction without an access + /// list. + // #[method(name = "createAccessList")] + // async fn create_access_list( + // &self, + // request: TransactionRequest, + // block_number: Option, + // ) -> RpcResult; + + /// Generates and returns an estimate of how much gas is necessary to allow the transaction to + /// complete. + #[method(name = "estimateGas")] + async fn estimate_gas( + &self, + request: TransactionRequest, + block_number: Option, + // state_override: Option, + ) -> RpcResult; + + /// Returns the current price per gas in wei. + #[method(name = "gasPrice")] + async fn gas_price(&self) -> RpcResult; + + /// Returns the account details by specifying an address and a block number/tag + // #[method(name = "getAccount")] + // async fn get_account( + // &self, + // address: Address, + // block: BlockId, + // ) -> RpcResult>; + + /// Introduced in EIP-1559, returns suggestion for the priority for dynamic fee transactions. + #[method(name = "maxPriorityFeePerGas")] + async fn max_priority_fee_per_gas(&self) -> RpcResult; + + /// Introduced in EIP-4844, returns the current blob base fee in wei. + #[method(name = "blobBaseFee")] + async fn blob_base_fee(&self) -> RpcResult; + + /// Returns the Transaction fee history + /// + /// Introduced in EIP-1559 for getting information on the appropriate priority fee to use. + /// + /// Returns transaction base fee per gas and effective priority fee per gas for the + /// requested/supported block range. The returned Fee history for the returned block range + /// can be a subsection of the requested range if not all blocks are available. + #[method(name = "feeHistory")] + async fn fee_history( + &self, + block_count: U64, + newest_block: BlockNumberOrTag, + reward_percentiles: Option>, + ) -> RpcResult; + + /// Returns whether the client is actively mining new blocks. + #[method(name = "mining")] + async fn is_mining(&self) -> RpcResult; + + /// Returns the number of hashes per second that the node is mining with. + #[method(name = "hashrate")] + async fn hashrate(&self) -> RpcResult; + + /// Returns the hash of the current block, the seedHash, and the boundary condition to be met + /// (“target”) + // #[method(name = "getWork")] + // async fn get_work(&self) -> RpcResult; + + /// Used for submitting mining hashrate. + /// + /// Can be used for remote miners to submit their hash rate. + /// It accepts the miner hash rate and an identifier which must be unique between nodes. + /// Returns `true` if the block was successfully submitted, `false` otherwise. + #[method(name = "submitHashrate")] + async fn submit_hashrate(&self, hashrate: U256, id: H256) -> RpcResult; + + /// Used for submitting a proof-of-work solution. + #[method(name = "submitWork")] + async fn submit_work(&self, nonce: H64, pow_hash: H256, mix_digest: H256) -> RpcResult; + + /// Sends transaction; will block waiting for signer to return the + /// transaction hash. + #[method(name = "sendTransaction")] + async fn send_transaction(&self, request: TransactionRequest) -> RpcResult; + + /// Sends signed transaction, returning its hash. + #[method(name = "sendRawTransaction")] + async fn send_raw_transaction(&self, bytes: Bytes) -> RpcResult; + + /// Returns an Ethereum specific signature with: sign(keccak256("\x19Ethereum Signed Message:\n" + /// + len(message) + message))). + #[method(name = "sign")] + async fn sign(&self, address: Address, message: Bytes) -> RpcResult; + + /// Signs a transaction that can be submitted to the network at a later time using with + /// `sendRawTransaction.` + #[method(name = "signTransaction")] + async fn sign_transaction(&self, transaction: TransactionRequest) -> RpcResult; + + // Signs data via [EIP-712](https://github.com/ethereum/EIPs/blob/master/EIPS/eip-712.md). + // #[method(name = "signTypedData")] + // async fn sign_typed_data(&self, address: Address, data: TypedData) -> RpcResult; + + // Returns the account and storage values of the specified account including the Merkle-proof. + // This call can be used to verify that the data you are pulling from is not tampered with. + + // #[method(name = "getProof")] + // async fn get_proof( + // &self, + // address: Address, + // keys: Vec, + // block_number: Option, + // ) -> RpcResult; +} \ No newline at end of file diff --git a/crates/rpc/rpc-eth-api/src/lib.rs b/crates/rpc/rpc-eth-api/src/lib.rs new file mode 100644 index 0000000000..cae7f269fa --- /dev/null +++ b/crates/rpc/rpc-eth-api/src/lib.rs @@ -0,0 +1 @@ +mod eth; diff --git a/crates/rpc/rpc-eth-types/src/block.rs b/crates/rpc/rpc-eth-types/src/block.rs index 4c5c27f93f..c613a7981d 100644 --- a/crates/rpc/rpc-eth-types/src/block.rs +++ b/crates/rpc/rpc-eth-types/src/block.rs @@ -32,7 +32,7 @@ const SHA3_HASH_OF_EMPTY: &str = "56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421"; /// Block Transactions -#[derive(Debug)] +#[derive(Debug, Clone)] pub enum BlockTransactions { /// Only hashes Hashes(Vec), @@ -53,7 +53,7 @@ impl Serialize for BlockTransactions { } /// Block representation -#[derive(Debug, Serialize)] +#[derive(Debug, Serialize, Clone)] #[serde(rename_all = "camelCase")] pub struct Block { /// Hash of the block diff --git a/crates/rpc/rpc-eth-types/src/receipt.rs b/crates/rpc/rpc-eth-types/src/receipt.rs index 924a7a2722..dce0100935 100644 --- a/crates/rpc/rpc-eth-types/src/receipt.rs +++ b/crates/rpc/rpc-eth-types/src/receipt.rs @@ -23,7 +23,7 @@ use cfx_types::{Bloom as H2048, H160, H256, U256, U64}; use serde::Serialize; /// Receipt -#[derive(Debug, Serialize)] +#[derive(Debug, Serialize, Clone)] #[serde(rename_all = "camelCase")] pub struct Receipt { #[serde(rename = "type", skip_serializing_if = "Option::is_none")] diff --git a/crates/rpc/rpc-eth-types/src/sync.rs b/crates/rpc/rpc-eth-types/src/sync.rs index 33e5b12356..d0868a5963 100644 --- a/crates/rpc/rpc-eth-types/src/sync.rs +++ b/crates/rpc/rpc-eth-types/src/sync.rs @@ -22,7 +22,7 @@ use cfx_types::U256; use serde::{Serialize, Serializer}; /// Sync info -#[derive(Default, Debug, Serialize, PartialEq)] +#[derive(Default, Debug, Serialize, PartialEq, Clone)] #[serde(rename_all = "camelCase")] pub struct SyncInfo { /// Starting block @@ -38,7 +38,7 @@ pub struct SyncInfo { } /// Sync status -#[derive(Debug, PartialEq)] +#[derive(Debug, PartialEq, Clone)] pub enum SyncStatus { /// Info when syncing Info(SyncInfo), diff --git a/crates/util/solidity-abi-derive/Cargo.toml b/crates/util/solidity-abi-derive/Cargo.toml index e267a11179..bab68c1ff5 100644 --- a/crates/util/solidity-abi-derive/Cargo.toml +++ b/crates/util/solidity-abi-derive/Cargo.toml @@ -10,7 +10,7 @@ edition = "2018" syn = {version = "1", features=["parsing"]} # feature "extra-traits" for debug quote = "1" proc-macro2 = "1" -proc-macro-crate = "1" +proc-macro-crate = "3" [lib] proc-macro = true From 177367005e528288bd4bac768688620e97436c21 Mon Sep 17 00:00:00 2001 From: Pana Date: Fri, 13 Sep 2024 10:26:19 +0800 Subject: [PATCH 2/7] add eth rpc api traits --- Cargo.lock | 1 + crates/rpc/rpc-eth-api/Cargo.toml | 1 + crates/rpc/rpc-eth-api/src/debug.rs | 39 ++++ crates/rpc/rpc-eth-api/src/eth.rs | 238 +++++++++++--------- crates/rpc/rpc-eth-api/src/filter.rs | 39 ++++ crates/rpc/rpc-eth-api/src/lib.rs | 10 + crates/rpc/rpc-eth-api/src/pubsub.rs | 18 ++ crates/rpc/rpc-eth-api/src/trace.rs | 24 ++ crates/rpc/rpc-eth-types/src/fee_history.rs | 2 +- crates/rpc/rpc-eth-types/src/filter.rs | 2 +- crates/rpc/rpc-eth-types/src/lib.rs | 2 + crates/rpc/rpc-eth-types/src/trace.rs | 20 +- 12 files changed, 280 insertions(+), 116 deletions(-) create mode 100644 crates/rpc/rpc-eth-api/src/debug.rs create mode 100644 crates/rpc/rpc-eth-api/src/filter.rs create mode 100644 crates/rpc/rpc-eth-api/src/pubsub.rs create mode 100644 crates/rpc/rpc-eth-api/src/trace.rs diff --git a/Cargo.lock b/Cargo.lock index 03ae79273f..5e0f7f611c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -7180,6 +7180,7 @@ dependencies = [ name = "rpc-eth-api" version = "2.4.0" dependencies = [ + "alloy-rpc-types-trace", "cfx-rpc-eth-types", "cfx-rpc-primitives", "cfx-types", diff --git a/crates/rpc/rpc-eth-api/Cargo.toml b/crates/rpc/rpc-eth-api/Cargo.toml index faa0d2cf89..bd91a7c45c 100644 --- a/crates/rpc/rpc-eth-api/Cargo.toml +++ b/crates/rpc/rpc-eth-api/Cargo.toml @@ -17,3 +17,4 @@ cfx-rpc-eth-types = { workspace = true } jsonrpsee = { version = "0.24.4", features = ["server", "macros"] } cfx-types = { workspace = true } cfx-rpc-primitives = { workspace = true } +alloy-rpc-types-trace = { workspace = true } diff --git a/crates/rpc/rpc-eth-api/src/debug.rs b/crates/rpc/rpc-eth-api/src/debug.rs new file mode 100644 index 0000000000..969a53fe67 --- /dev/null +++ b/crates/rpc/rpc-eth-api/src/debug.rs @@ -0,0 +1,39 @@ +use alloy_rpc_types_trace::geth::{ + GethDebugTracingCallOptions, GethDebugTracingOptions, GethTrace, + TraceResult, +}; +use cfx_rpc_eth_types::{BlockNumber, TransactionRequest}; +use cfx_types::H256; +use jsonrpsee::{core::RpcResult, proc_macros::rpc}; + +#[rpc(server, namespace = "debug")] +pub trait DebugApi { + #[method(name = "dbGet")] + async fn db_get(&self, key: String) -> RpcResult>; + + /// The `debug_traceTransaction` debugging method will attempt to run the + /// transaction in the exact same manner as it was executed on the + /// network. It will replay any transaction that may have been executed + /// prior to this one before it will finally attempt to execute the + /// transaction that corresponds to the given hash. + #[method(name = "traceTransaction")] + async fn debug_trace_transaction( + &self, tx_hash: H256, opts: Option, + ) -> RpcResult; + + #[method(name = "traceBlockByHash")] + async fn debug_trace_block_by_hash( + &self, block: H256, opts: Option, + ) -> RpcResult>; + + #[method(name = "traceBlockByNumber")] + async fn debug_trace_block_by_number( + &self, block: BlockNumber, opts: Option, + ) -> RpcResult>; + + #[method(name = "traceCall")] + async fn debug_trace_call( + &self, request: TransactionRequest, block_number: Option, + opts: Option, + ) -> RpcResult; +} diff --git a/crates/rpc/rpc-eth-api/src/eth.rs b/crates/rpc/rpc-eth-api/src/eth.rs index 261b39092b..345649f6ab 100644 --- a/crates/rpc/rpc-eth-api/src/eth.rs +++ b/crates/rpc/rpc-eth-api/src/eth.rs @@ -1,17 +1,15 @@ -use jsonrpsee::proc_macros::rpc; -use jsonrpsee::core::RpcResult; -use cfx_types::{U64, H256, Address, U256, H64}; -use cfx_rpc_primitives::Bytes; -use cfx_rpc_eth_types::{BlockNumber as BlockId, SyncStatus, Block, Receipt, Transaction, TransactionRequest, Header}; +use cfx_rpc_eth_types::{ + Block, BlockNumber as BlockId, FeeHistory, Header, Receipt, SyncStatus, + Transaction, TransactionRequest, +}; +use cfx_rpc_primitives::{Bytes, Index}; +use cfx_types::{Address, H256, H64, U256, U64}; +use jsonrpsee::{core::RpcResult, proc_macros::rpc}; type BlockNumberOrTag = BlockId; -type Index = u64; - type JsonStorageKey = U256; -type FeeHistory = (); - #[rpc(server, namespace = "eth")] pub trait EthApi { /// Returns the protocol version encoded as a string. @@ -40,141 +38,156 @@ pub trait EthApi { /// Returns information about a block by hash. #[method(name = "getBlockByHash")] - async fn block_by_hash(&self, hash: H256, full: bool) -> RpcResult>; + async fn block_by_hash( + &self, hash: H256, full: bool, + ) -> RpcResult>; /// Returns information about a block by number. #[method(name = "getBlockByNumber")] - async fn block_by_number(&self, number: BlockNumberOrTag, full: bool) -> RpcResult>; + async fn block_by_number( + &self, number: BlockNumberOrTag, full: bool, + ) -> RpcResult>; - /// Returns the number of transactions in a block from a block matching the given block hash. + /// Returns the number of transactions in a block from a block matching the + /// given block hash. #[method(name = "getBlockTransactionCountByHash")] - async fn block_transaction_count_by_hash(&self, hash: H256) -> RpcResult>; + async fn block_transaction_count_by_hash( + &self, hash: H256, + ) -> RpcResult>; - /// Returns the number of transactions in a block matching the given block number. + /// Returns the number of transactions in a block matching the given block + /// number. #[method(name = "getBlockTransactionCountByNumber")] async fn block_transaction_count_by_number( - &self, - number: BlockNumberOrTag, + &self, number: BlockNumberOrTag, ) -> RpcResult>; - /// Returns the number of uncles in a block from a block matching the given block hash. + /// Returns the number of uncles in a block from a block matching the given + /// block hash. #[method(name = "getUncleCountByBlockHash")] - async fn block_uncles_count_by_hash(&self, hash: H256) -> RpcResult>; + async fn block_uncles_count_by_hash( + &self, hash: H256, + ) -> RpcResult>; /// Returns the number of uncles in a block with given block number. #[method(name = "getUncleCountByBlockNumber")] async fn block_uncles_count_by_number( - &self, - number: BlockNumberOrTag, + &self, number: BlockNumberOrTag, ) -> RpcResult>; /// Returns all transaction receipts for a given block. #[method(name = "getBlockReceipts")] - async fn block_receipts(&self, block_id: BlockId) -> RpcResult>>; + async fn block_receipts( + &self, block_id: BlockId, + ) -> RpcResult>>; /// Returns an uncle block of the given block and index. #[method(name = "getUncleByBlockHashAndIndex")] - async fn uncle_by_block_hash_and_index(&self, hash: H256, index: Index) - -> RpcResult>; + async fn uncle_by_block_hash_and_index( + &self, hash: H256, index: Index, + ) -> RpcResult>; /// Returns an uncle block of the given block and index. #[method(name = "getUncleByBlockNumberAndIndex")] async fn uncle_by_block_number_and_index( - &self, - number: BlockNumberOrTag, - index: Index, + &self, number: BlockNumberOrTag, index: Index, ) -> RpcResult>; /// Returns the EIP-2718 encoded transaction if it exists. /// - /// If this is a EIP-4844 transaction that is in the pool it will include the sidecar. + /// If this is a EIP-4844 transaction that is in the pool it will include + /// the sidecar. #[method(name = "getRawTransactionByHash")] - async fn raw_transaction_by_hash(&self, hash: H256) -> RpcResult>; + async fn raw_transaction_by_hash( + &self, hash: H256, + ) -> RpcResult>; - /// Returns the information about a transaction requested by transaction hash. + /// Returns the information about a transaction requested by transaction + /// hash. #[method(name = "getTransactionByHash")] - async fn transaction_by_hash(&self, hash: H256) -> RpcResult>; + async fn transaction_by_hash( + &self, hash: H256, + ) -> RpcResult>; - /// Returns information about a raw transaction by block hash and transaction index position. + /// Returns information about a raw transaction by block hash and + /// transaction index position. #[method(name = "getRawTransactionByBlockHashAndIndex")] async fn raw_transaction_by_block_hash_and_index( - &self, - hash: H256, - index: Index, + &self, hash: H256, index: Index, ) -> RpcResult>; - /// Returns information about a transaction by block hash and transaction index position. + /// Returns information about a transaction by block hash and transaction + /// index position. #[method(name = "getTransactionByBlockHashAndIndex")] async fn transaction_by_block_hash_and_index( - &self, - hash: H256, - index: Index, + &self, hash: H256, index: Index, ) -> RpcResult>; - /// Returns information about a raw transaction by block number and transaction index - /// position. + /// Returns information about a raw transaction by block number and + /// transaction index position. #[method(name = "getRawTransactionByBlockNumberAndIndex")] async fn raw_transaction_by_block_number_and_index( - &self, - number: BlockNumberOrTag, - index: Index, + &self, number: BlockNumberOrTag, index: Index, ) -> RpcResult>; - /// Returns information about a transaction by block number and transaction index position. + /// Returns information about a transaction by block number and transaction + /// index position. #[method(name = "getTransactionByBlockNumberAndIndex")] async fn transaction_by_block_number_and_index( - &self, - number: BlockNumberOrTag, - index: Index, + &self, number: BlockNumberOrTag, index: Index, ) -> RpcResult>; /// Returns information about a transaction by sender and nonce. #[method(name = "getTransactionBySenderAndNonce")] async fn transaction_by_sender_and_nonce( - &self, - address: Address, - nonce: U64, + &self, address: Address, nonce: U64, ) -> RpcResult>; /// Returns the receipt of a transaction by transaction hash. #[method(name = "getTransactionReceipt")] - async fn transaction_receipt(&self, hash: H256) -> RpcResult>; + async fn transaction_receipt( + &self, hash: H256, + ) -> RpcResult>; /// Returns the balance of the account of given address. #[method(name = "getBalance")] - async fn balance(&self, address: Address, block_number: Option) -> RpcResult; + async fn balance( + &self, address: Address, block_number: Option, + ) -> RpcResult; /// Returns the value from a storage position at a given address #[method(name = "getStorageAt")] async fn storage_at( - &self, - address: Address, - index: JsonStorageKey, + &self, address: Address, index: JsonStorageKey, block_number: Option, ) -> RpcResult; - /// Returns the number of transactions sent from an address at given block number. + /// Returns the number of transactions sent from an address at given block + /// number. #[method(name = "getTransactionCount")] async fn transaction_count( - &self, - address: Address, - block_number: Option, + &self, address: Address, block_number: Option, ) -> RpcResult; /// Returns code at a given address at given block number. #[method(name = "getCode")] - async fn get_code(&self, address: Address, block_number: Option) -> RpcResult; + async fn get_code( + &self, address: Address, block_number: Option, + ) -> RpcResult; /// Returns the block's header at given number. #[method(name = "getHeaderByNumber")] - async fn header_by_number(&self, hash: BlockNumberOrTag) -> RpcResult>; + async fn header_by_number( + &self, hash: BlockNumberOrTag, + ) -> RpcResult>; /// Returns the block's header at given hash. #[method(name = "getHeaderByHash")] async fn header_by_hash(&self, hash: H256) -> RpcResult>; - /// `eth_simulateV1` executes an arbitrary number of transactions on top of the requested state. - /// The transactions are packed into individual blocks. Overrides can be provided. + /// `eth_simulateV1` executes an arbitrary number of transactions on top of + /// the requested state. The transactions are packed into individual + /// blocks. Overrides can be provided. // #[method(name = "simulateV1")] // async fn simulate_v1( // &self, @@ -182,7 +195,8 @@ pub trait EthApi { // block_number: Option, // ) -> RpcResult>; - /// Executes a new message call immediately without creating a transaction on the block chain. + /// Executes a new message call immediately without creating a transaction + /// on the block chain. #[method(name = "call")] async fn call( &self, @@ -192,8 +206,8 @@ pub trait EthApi { // block_overrides: Option>, ) -> RpcResult; - /// Simulate arbitrary number of transactions at an arbitrary blockchain index, with the - /// optionality of state overrides + /// Simulate arbitrary number of transactions at an arbitrary blockchain + /// index, with the optionality of state overrides // #[method(name = "callMany")] // async fn call_many( // &self, @@ -206,16 +220,18 @@ pub trait EthApi { /// /// This method creates an [EIP2930](https://eips.ethereum.org/EIPS/eip-2930) type accessList based on a given Transaction. /// - /// An access list contains all storage slots and addresses touched by the transaction, except - /// for the sender account and the chain's precompiles. + /// An access list contains all storage slots and addresses touched by the + /// transaction, except for the sender account and the chain's + /// precompiles. /// - /// It returns list of addresses and storage keys used by the transaction, plus the gas - /// consumed when the access list is added. That is, it gives you the list of addresses and - /// storage keys that will be used by that transaction, plus the gas consumed if the access - /// list is included. Like eth_estimateGas, this is an estimation; the list could change - /// when the transaction is actually mined. Adding an accessList to your transaction does - /// not necessary result in lower gas usage compared to a transaction without an access - /// list. + /// It returns list of addresses and storage keys used by the transaction, + /// plus the gas consumed when the access list is added. That is, it + /// gives you the list of addresses and storage keys that will be used + /// by that transaction, plus the gas consumed if the access + /// list is included. Like eth_estimateGas, this is an estimation; the list + /// could change when the transaction is actually mined. Adding an + /// accessList to your transaction does not necessary result in lower + /// gas usage compared to a transaction without an access list. // #[method(name = "createAccessList")] // async fn create_access_list( // &self, @@ -223,8 +239,8 @@ pub trait EthApi { // block_number: Option, // ) -> RpcResult; - /// Generates and returns an estimate of how much gas is necessary to allow the transaction to - /// complete. + /// Generates and returns an estimate of how much gas is necessary to allow + /// the transaction to complete. #[method(name = "estimateGas")] async fn estimate_gas( &self, @@ -237,7 +253,8 @@ pub trait EthApi { #[method(name = "gasPrice")] async fn gas_price(&self) -> RpcResult; - /// Returns the account details by specifying an address and a block number/tag + /// Returns the account details by specifying an address and a block + /// number/tag // #[method(name = "getAccount")] // async fn get_account( // &self, @@ -245,26 +262,27 @@ pub trait EthApi { // block: BlockId, // ) -> RpcResult>; - /// Introduced in EIP-1559, returns suggestion for the priority for dynamic fee transactions. + /// Introduced in EIP-1559, returns suggestion for the priority for dynamic + /// fee transactions. #[method(name = "maxPriorityFeePerGas")] async fn max_priority_fee_per_gas(&self) -> RpcResult; /// Introduced in EIP-4844, returns the current blob base fee in wei. - #[method(name = "blobBaseFee")] - async fn blob_base_fee(&self) -> RpcResult; + // #[method(name = "blobBaseFee")] + // async fn blob_base_fee(&self) -> RpcResult; /// Returns the Transaction fee history /// - /// Introduced in EIP-1559 for getting information on the appropriate priority fee to use. + /// Introduced in EIP-1559 for getting information on the appropriate + /// priority fee to use. /// - /// Returns transaction base fee per gas and effective priority fee per gas for the - /// requested/supported block range. The returned Fee history for the returned block range - /// can be a subsection of the requested range if not all blocks are available. + /// Returns transaction base fee per gas and effective priority fee per gas + /// for the requested/supported block range. The returned Fee history + /// for the returned block range can be a subsection of the requested + /// range if not all blocks are available. #[method(name = "feeHistory")] async fn fee_history( - &self, - block_count: U64, - newest_block: BlockNumberOrTag, + &self, block_count: U64, newest_block: BlockNumberOrTag, reward_percentiles: Option>, ) -> RpcResult; @@ -276,49 +294,61 @@ pub trait EthApi { #[method(name = "hashrate")] async fn hashrate(&self) -> RpcResult; - /// Returns the hash of the current block, the seedHash, and the boundary condition to be met - /// (“target”) + /// Returns the hash of the current block, the seedHash, and the boundary + /// condition to be met (“target”) // #[method(name = "getWork")] // async fn get_work(&self) -> RpcResult; /// Used for submitting mining hashrate. /// /// Can be used for remote miners to submit their hash rate. - /// It accepts the miner hash rate and an identifier which must be unique between nodes. - /// Returns `true` if the block was successfully submitted, `false` otherwise. + /// It accepts the miner hash rate and an identifier which must be unique + /// between nodes. Returns `true` if the block was successfully + /// submitted, `false` otherwise. #[method(name = "submitHashrate")] - async fn submit_hashrate(&self, hashrate: U256, id: H256) -> RpcResult; + async fn submit_hashrate( + &self, hashrate: U256, id: H256, + ) -> RpcResult; /// Used for submitting a proof-of-work solution. #[method(name = "submitWork")] - async fn submit_work(&self, nonce: H64, pow_hash: H256, mix_digest: H256) -> RpcResult; + async fn submit_work( + &self, nonce: H64, pow_hash: H256, mix_digest: H256, + ) -> RpcResult; /// Sends transaction; will block waiting for signer to return the /// transaction hash. #[method(name = "sendTransaction")] - async fn send_transaction(&self, request: TransactionRequest) -> RpcResult; + async fn send_transaction( + &self, request: TransactionRequest, + ) -> RpcResult; /// Sends signed transaction, returning its hash. #[method(name = "sendRawTransaction")] async fn send_raw_transaction(&self, bytes: Bytes) -> RpcResult; - /// Returns an Ethereum specific signature with: sign(keccak256("\x19Ethereum Signed Message:\n" + /// Returns an Ethereum specific signature with: + /// sign(keccak256("\x19Ethereum Signed Message:\n" /// + len(message) + message))). #[method(name = "sign")] async fn sign(&self, address: Address, message: Bytes) -> RpcResult; - /// Signs a transaction that can be submitted to the network at a later time using with - /// `sendRawTransaction.` + /// Signs a transaction that can be submitted to the network at a later time + /// using with `sendRawTransaction.` #[method(name = "signTransaction")] - async fn sign_transaction(&self, transaction: TransactionRequest) -> RpcResult; + async fn sign_transaction( + &self, transaction: TransactionRequest, + ) -> RpcResult; // Signs data via [EIP-712](https://github.com/ethereum/EIPs/blob/master/EIPS/eip-712.md). // #[method(name = "signTypedData")] - // async fn sign_typed_data(&self, address: Address, data: TypedData) -> RpcResult; + // async fn sign_typed_data(&self, address: Address, data: TypedData) -> + // RpcResult; + + // Returns the account and storage values of the specified account including + // the Merkle-proof. This call can be used to verify that the data you + // are pulling from is not tampered with. - // Returns the account and storage values of the specified account including the Merkle-proof. - // This call can be used to verify that the data you are pulling from is not tampered with. - // #[method(name = "getProof")] // async fn get_proof( // &self, @@ -326,4 +356,4 @@ pub trait EthApi { // keys: Vec, // block_number: Option, // ) -> RpcResult; -} \ No newline at end of file +} diff --git a/crates/rpc/rpc-eth-api/src/filter.rs b/crates/rpc/rpc-eth-api/src/filter.rs new file mode 100644 index 0000000000..d4732d704d --- /dev/null +++ b/crates/rpc/rpc-eth-api/src/filter.rs @@ -0,0 +1,39 @@ +use cfx_rpc_eth_types::{EthRpcLogFilter as Filter, FilterChanges, Log}; +use cfx_types::H128 as FilterId; +use jsonrpsee::{core::RpcResult, proc_macros::rpc}; + +type PendingTransactionFilterKind = (); // TODO: Implement this type + +/// Rpc Interface for poll-based ethereum filter API. +#[rpc(server, namespace = "eth")] +pub trait EthFilterApi { + /// Creates anew filter and returns its id. + #[method(name = "newFilter")] + async fn new_filter(&self, filter: Filter) -> RpcResult; + + /// Creates a new block filter and returns its id. + #[method(name = "newBlockFilter")] + async fn new_block_filter(&self) -> RpcResult; + + /// Creates a pending transaction filter and returns its id. + #[method(name = "newPendingTransactionFilter")] + async fn new_pending_transaction_filter( + &self, kind: Option, + ) -> RpcResult; + + /// Returns all filter changes since last poll. + #[method(name = "getFilterChanges")] + async fn filter_changes(&self, id: FilterId) -> RpcResult; + + /// Returns all logs matching given filter (in a range 'from' - 'to'). + #[method(name = "getFilterLogs")] + async fn filter_logs(&self, id: FilterId) -> RpcResult>; + + /// Uninstalls filter. + #[method(name = "uninstallFilter")] + async fn uninstall_filter(&self, id: FilterId) -> RpcResult; + + /// Returns logs matching given filter object. + #[method(name = "getLogs")] + async fn logs(&self, filter: Filter) -> RpcResult>; +} diff --git a/crates/rpc/rpc-eth-api/src/lib.rs b/crates/rpc/rpc-eth-api/src/lib.rs index cae7f269fa..a638f8f469 100644 --- a/crates/rpc/rpc-eth-api/src/lib.rs +++ b/crates/rpc/rpc-eth-api/src/lib.rs @@ -1 +1,11 @@ +mod debug; mod eth; +mod filter; +mod pubsub; +mod trace; + +pub use debug::DebugApiServer; +pub use eth::EthApiServer; +pub use filter::EthFilterApiServer; +pub use pubsub::EthPubSubApiServer; +pub use trace::TraceApiServer; diff --git a/crates/rpc/rpc-eth-api/src/pubsub.rs b/crates/rpc/rpc-eth-api/src/pubsub.rs new file mode 100644 index 0000000000..3d5ba03523 --- /dev/null +++ b/crates/rpc/rpc-eth-api/src/pubsub.rs @@ -0,0 +1,18 @@ +//! `eth_` RPC API for pubsub subscription. + +use cfx_rpc_eth_types::eth_pubsub::{Kind as SubscriptionKind, Params}; +use jsonrpsee::proc_macros::rpc; + +/// Ethereum pub-sub rpc interface. +#[rpc(server, namespace = "eth")] +pub trait EthPubSubApi { + /// Create an ethereum subscription for the given params + #[subscription( + name = "subscribe" => "subscription", + unsubscribe = "unsubscribe", + item = reth_rpc_types::pubsub::SubscriptionResult + )] + async fn subscribe( + &self, kind: SubscriptionKind, params: Option, + ) -> jsonrpsee::core::SubscriptionResult; +} diff --git a/crates/rpc/rpc-eth-api/src/trace.rs b/crates/rpc/rpc-eth-api/src/trace.rs new file mode 100644 index 0000000000..027f496a1c --- /dev/null +++ b/crates/rpc/rpc-eth-api/src/trace.rs @@ -0,0 +1,24 @@ +use cfx_rpc_eth_types::{BlockNumber, LocalizedTrace, TraceFilter}; +use cfx_types::H256; +use jsonrpsee::{core::RpcResult, proc_macros::rpc}; + +#[rpc(server, namespace = "trace")] +pub trait TraceApi { + /// Returns all traces produced at the given block. + #[method(name = "block")] + fn block_traces( + &self, block_number: BlockNumber, + ) -> RpcResult>>; + + /// Returns all traces matching the provided filter. + #[method(name = "filter")] + fn filter_traces( + &self, filter: TraceFilter, + ) -> RpcResult>>; + + /// Returns all traces produced at the given transaction. + #[method(name = "transaction")] + fn transaction_traces( + &self, tx_hash: H256, + ) -> RpcResult>>; +} diff --git a/crates/rpc/rpc-eth-types/src/fee_history.rs b/crates/rpc/rpc-eth-types/src/fee_history.rs index 3af5ba4608..cf1125c651 100644 --- a/crates/rpc/rpc-eth-types/src/fee_history.rs +++ b/crates/rpc/rpc-eth-types/src/fee_history.rs @@ -9,7 +9,7 @@ use cfx_rpc_cfx_types::{CfxFeeHistory, FeeHistoryCacheEntry}; use cfx_types::{Space, SpaceMap, U256}; use primitives::{transaction::SignedTransaction, BlockHeader}; -#[derive(Serialize, Debug, Default)] +#[derive(Serialize, Debug, Default, Clone)] #[serde(rename_all = "camelCase")] pub struct FeeHistory { /// Oldest Block diff --git a/crates/rpc/rpc-eth-types/src/filter.rs b/crates/rpc/rpc-eth-types/src/filter.rs index 48f0b23f4a..93e2ae600a 100644 --- a/crates/rpc/rpc-eth-types/src/filter.rs +++ b/crates/rpc/rpc-eth-types/src/filter.rs @@ -198,7 +198,7 @@ impl EthRpcLogFilter { } /// Results of the filter_changes RPC. -#[derive(Debug, PartialEq)] +#[derive(Debug, PartialEq, Clone)] pub enum FilterChanges { /// New logs. Logs(Vec), diff --git a/crates/rpc/rpc-eth-types/src/lib.rs b/crates/rpc/rpc-eth-types/src/lib.rs index 584e4d27b9..866c0bff2a 100644 --- a/crates/rpc/rpc-eth-types/src/lib.rs +++ b/crates/rpc/rpc-eth-types/src/lib.rs @@ -23,6 +23,8 @@ pub use filter::*; pub use log::Log; pub use receipt::Receipt; pub use sync::{SyncInfo, SyncStatus}; +pub use trace::*; +pub use trace_filter::TraceFilter; pub use transaction::Transaction; pub use transaction_request::{ TransactionRequest, DEFAULT_ETH_GAS_CALL_REQUEST, diff --git a/crates/rpc/rpc-eth-types/src/trace.rs b/crates/rpc/rpc-eth-types/src/trace.rs index cd65553108..73c61a18ef 100644 --- a/crates/rpc/rpc-eth-types/src/trace.rs +++ b/crates/rpc/rpc-eth-types/src/trace.rs @@ -14,7 +14,7 @@ use std::{ }; /// Create response -#[derive(Debug, Serialize)] +#[derive(Debug, Serialize, Clone)] #[serde(rename_all = "camelCase")] pub struct Create { /// Sender @@ -30,7 +30,7 @@ pub struct Create { } /// The type of the create-like instruction. -#[derive(Debug, Serialize)] +#[derive(Debug, Serialize, Clone)] #[serde(rename_all = "lowercase")] pub enum CreateType { /// Not a create @@ -52,7 +52,7 @@ impl From for CreateType { } /// Call type. -#[derive(Debug, Serialize)] +#[derive(Debug, Serialize, Clone)] #[serde(rename_all = "lowercase")] pub enum CallType { /// None @@ -80,7 +80,7 @@ impl From for CallType { } /// Call response -#[derive(Debug, Serialize)] +#[derive(Debug, Serialize, Clone)] #[serde(rename_all = "camelCase")] pub struct Call { /// Sender @@ -98,7 +98,7 @@ pub struct Call { } /// Action -#[derive(Debug)] +#[derive(Debug, Clone)] pub enum Action { /// Call Call(Call), @@ -136,7 +136,7 @@ impl TryFrom for Action { } /// Call Result -#[derive(Debug, Serialize)] +#[derive(Debug, Serialize, Clone)] #[serde(rename_all = "camelCase")] pub struct CallResult { /// Gas used @@ -146,7 +146,7 @@ pub struct CallResult { } /// Craete Result -#[derive(Debug, Serialize)] +#[derive(Debug, Serialize, Clone)] #[serde(rename_all = "camelCase")] pub struct CreateResult { /// Gas used @@ -158,7 +158,7 @@ pub struct CreateResult { } /// Response -#[derive(Debug)] +#[derive(Debug, Clone)] pub enum Res { /// Call Call(CallResult), @@ -173,7 +173,7 @@ pub enum Res { } /// Trace -#[derive(Debug)] +#[derive(Debug, Clone)] pub struct LocalizedTrace { /// Action pub action: Action, @@ -378,7 +378,7 @@ impl Serialize for Trace { } } -#[derive(Debug)] +#[derive(Debug, Clone)] pub enum TraceError { /// Execution has been reverted with REVERT instruction. Reverted, From 23d276804dd9dd6232c7f07db6dad46ed3397bcb Mon Sep 17 00:00:00 2001 From: Pana Date: Fri, 13 Sep 2024 10:36:27 +0800 Subject: [PATCH 3/7] remove repeat defined VariadicValue type --- crates/rpc/rpc-eth-types/src/filter.rs | 64 +------------------ .../rpc/rpc-primitives/src/variadic_value.rs | 20 ++++++ 2 files changed, 23 insertions(+), 61 deletions(-) diff --git a/crates/rpc/rpc-eth-types/src/filter.rs b/crates/rpc/rpc-eth-types/src/filter.rs index 93e2ae600a..42fa4c2e19 100644 --- a/crates/rpc/rpc-eth-types/src/filter.rs +++ b/crates/rpc/rpc-eth-types/src/filter.rs @@ -20,74 +20,16 @@ use crate::{BlockNumber, Error as SelfError, Log}; use cfx_rpc_cfx_types::traits::Provider; +use cfx_rpc_primitives::VariadicValue; use cfx_types::{Space, H160, H256}; use primitives::{ filter::{LogFilter as PrimitiveFilter, LogFilterParams}, EpochNumber, }; -use serde::{ - de::{DeserializeOwned, Error}, - Deserialize, Deserializer, Serialize, Serializer, -}; -use serde_json::{from_value, Value}; +use serde::{Deserialize, Serialize, Serializer}; +use serde_json::Value; use std::convert::TryInto; -/// Variadic value -#[derive(Debug, PartialEq, Eq, Clone, Hash)] -pub enum VariadicValue -where T: DeserializeOwned -{ - /// Single - Single(T), - /// List - Multiple(Vec), - /// None - Null, -} - -impl<'a, T> Deserialize<'a> for VariadicValue -where T: DeserializeOwned -{ - fn deserialize(deserializer: D) -> Result, D::Error> - where D: Deserializer<'a> { - let v: Value = Deserialize::deserialize(deserializer)?; - - if v.is_null() { - return Ok(VariadicValue::Null); - } - - from_value(v.clone()) - .map(VariadicValue::Single) - .or_else(|_| from_value(v).map(VariadicValue::Multiple)) - .map_err(|err| { - D::Error::custom(format!( - "Invalid variadic value type: {}", - err - )) - }) - } -} - -impl VariadicValue -where T: DeserializeOwned -{ - pub fn to_vec(self) -> Vec { - match self { - VariadicValue::Null => vec![], - VariadicValue::Single(x) => vec![x], - VariadicValue::Multiple(xs) => xs, - } - } - - pub fn to_opt(self) -> Option> { - match self { - VariadicValue::Null => None, - VariadicValue::Single(x) => Some(vec![x]), - VariadicValue::Multiple(xs) => Some(xs), - } - } -} - /// Filter Address pub type FilterAddress = VariadicValue; /// Topic diff --git a/crates/rpc/rpc-primitives/src/variadic_value.rs b/crates/rpc/rpc-primitives/src/variadic_value.rs index aaefcd1fd5..4395d8317b 100644 --- a/crates/rpc/rpc-primitives/src/variadic_value.rs +++ b/crates/rpc/rpc-primitives/src/variadic_value.rs @@ -24,6 +24,26 @@ impl Into>> for VariadicValue { } } +impl VariadicValue +where T: DeserializeOwned +{ + pub fn to_vec(self) -> Vec { + match self { + VariadicValue::Null => vec![], + VariadicValue::Single(x) => vec![x], + VariadicValue::Multiple(xs) => xs, + } + } + + pub fn to_opt(self) -> Option> { + match self { + VariadicValue::Null => None, + VariadicValue::Single(x) => Some(vec![x]), + VariadicValue::Multiple(xs) => Some(xs), + } + } +} + impl VariadicValue { pub fn iter<'a>(&'a self) -> Box + 'a> { match self { From 650a73c3d423ceb86ec5873b060b7d5761fbb20f Mon Sep 17 00:00:00 2001 From: Pana Date: Fri, 13 Sep 2024 10:59:55 +0800 Subject: [PATCH 4/7] use cargo workspace dependent style --- Cargo.lock | 13 ++------ Cargo.toml | 7 +++++ crates/cfxcore/core/Cargo.toml | 30 +++++++++---------- .../src/pos/consensus/executor/Cargo.toml | 10 +++---- .../pos/storage/backup/backup-cli/Cargo.toml | 2 +- .../src/pos/storage/pos-ledger-db/Cargo.toml | 8 ++--- .../src/pos/storage/scratchpad/Cargo.toml | 2 +- .../pos/storage/storage-interface/Cargo.toml | 2 +- .../pos/storage/storage-service/Cargo.toml | 2 +- crates/client/Cargo.toml | 24 +++++++-------- crates/primitives/Cargo.toml | 20 ++++++------- crates/rpc/rpc-eth-api/Cargo.toml | 2 +- crates/rpc/rpc-eth-types/Cargo.toml | 2 +- 13 files changed, 61 insertions(+), 63 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 5e0f7f611c..a7903cfc95 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1569,7 +1569,7 @@ dependencies = [ "impl-tools", "impl-trait-for-tuples 0.2.2", "io", - "itertools 0.9.0", + "itertools 0.10.5", "jsonrpc-core", "keccak-hash", "kvdb", @@ -1862,7 +1862,7 @@ dependencies = [ "futures01", "geth-tracer", "io", - "itertools 0.9.0", + "itertools 0.10.5", "jsonrpc-core", "jsonrpc-core-client", "jsonrpc-derive", @@ -4511,15 +4511,6 @@ version = "0.12.8" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "70783119ac90828aaba91eae39db32c6c1b8838deea3637e5238efa0130801ab" -[[package]] -name = "itertools" -version = "0.9.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "284f18f85651fe11e8a991b2adb42cb078325c996ed026d994719efcfca1d54b" -dependencies = [ - "either", -] - [[package]] name = "itertools" version = "0.10.5" diff --git a/Cargo.toml b/Cargo.toml index de7dd47fe4..78fea71860 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -152,6 +152,7 @@ cfx-parity-trace-types = { path = "./crates/cfxcore/parity-trace-types" } serde = { version = "1.0", features = ["derive", "alloc"] } serde_json = "1.0" serde_derive = "1.0" +fail = "0.4.0" thiserror = "1.0.63" anyhow = "1.0" chrono = "=0.4.38" @@ -163,7 +164,13 @@ strum_macros = "0.20" lazy_static = "1.4" parking_lot = "0.11" rayon = "1.10" +jsonrpsee = "0.24.4" +once_cell = "1.17.1" +rand = "0.7" +itertools = "0.10.0" +byteorder = "1.2.7" +fixed-hash = "0.5" keccak-hash = "0.5" rlp = "0.4.0" rlp_derive = { git = "https://github.com/Conflux-Chain/conflux-parity-deps.git", rev = "1597a9cab02343eb2322ca0ac58d39b64e3f42d1" } diff --git a/crates/cfxcore/core/Cargo.toml b/crates/cfxcore/core/Cargo.toml index 9f27ba6e6b..545b9ac671 100644 --- a/crates/cfxcore/core/Cargo.toml +++ b/crates/cfxcore/core/Cargo.toml @@ -9,7 +9,7 @@ edition = "2021" [dependencies] bit-set = "0.4" substrate-bn = { git = "https://github.com/paritytech/bn", default-features = false, rev="63f8c587356a67b33c7396af98e065b66fca5dda" } -byteorder = "1.0" +byteorder = { workspace = true } cfxkey = { workspace = true } cfx-addr = { workspace = true } cfx-bytes = { workspace = true } @@ -37,15 +37,15 @@ fs_extra = "1.1.0" futures = {version="0.3.3", features = ["compat"]} hashbrown = "0.7.1" hibitset = { workspace = true } -itertools = "0.9" +itertools = { workspace = true } io = { workspace = true } -jsonrpc-core = "15.1.0" +jsonrpc-core = { workspace = true } keccak-hash = { workspace = true } kvdb = "0.4" kvdb-rocksdb = { workspace = true } -lazy_static = "1.4" +lazy_static = { workspace = true } link-cut-tree = { workspace = true } -log = "0.4" +log = { workspace = true } log4rs = { version = "1.2.0", features = ["background_rotation", "gzip"] } lru_time_cache = "0.9.0" malloc_size_of = { workspace = true } @@ -63,18 +63,18 @@ primal = "0.2.3" primitives = { workspace = true } priority-send-queue = { workspace = true } prometheus = { version = "0.7.0", default-features = false } -rand = "0.7" +rand = { workspace = true } rand_08 = {package = "rand", version = "0.8"} rand_xorshift="0.2" random-crash = { workspace = true } -rayon = "1.0" +rayon = { workspace = true } rlp = { workspace = true } rlp_derive = { workspace = true } -rustc-hex = "2.1" +rustc-hex = { workspace = true } secret-store = { workspace = true } serde = { version = "1.0", features = ["rc"] } -serde_derive = "1.0" -serde_json = "1.0" +serde_derive = { workspace = true } +serde_json = { workspace = true } siphasher = "0.3" slab = "0.4" solidity-abi = { workspace = true } @@ -93,8 +93,8 @@ tokio = { version = "1.6", features = ["full"] } tokio-timer = "0.2.13" tokio-stream = "0.1.4" unexpected = { git = "https://github.com/Conflux-Chain/conflux-parity-deps.git", rev = "1597a9cab02343eb2322ca0ac58d39b64e3f42d1" } -strum = "0.20" -strum_macros = "0.20" +strum = { workspace = true } +strum_macros = { workspace = true } smart-default = "0.6.0" bls-signatures = { workspace = true } tiny-keccak = {version = "2.0", features = ["keccak"]} @@ -122,9 +122,9 @@ schemadb = { path = "src/pos/storage/schemadb" } storage-interface = { path = "src/pos/storage/storage-interface" } short-hex-str = { path = "src/pos/common/short-hex-str" } subscription-service = { path = "src/pos/common/subscription-service" } -fail = "0.4.0" -anyhow = "1.0.38" -once_cell = "1.7.2" +fail = { workspace = true } +anyhow = { workspace = true } +once_cell = { workspace = true } move-core-types = { path = "src/pos/types/move-core-types" } pow-types = {path = "src/pos/types/pow-types" } async-oneshot = "0.5.0" diff --git a/crates/cfxcore/core/src/pos/consensus/executor/Cargo.toml b/crates/cfxcore/core/src/pos/consensus/executor/Cargo.toml index eea4842567..d8f35866e2 100644 --- a/crates/cfxcore/core/src/pos/consensus/executor/Cargo.toml +++ b/crates/cfxcore/core/src/pos/consensus/executor/Cargo.toml @@ -10,12 +10,12 @@ publish = false edition = "2018" [dependencies] -anyhow = "1.0.38" -fail = "0.4.0" +anyhow = { workspace = true } +fail = { workspace = true } itertools = { version = "0.10.0", default-features = false } -once_cell = "1.7.2" -serde_json = "1.0.64" -serde = { version = "1.0.124", features = ["derive"] } +once_cell = { workspace = true } +serde_json = { workspace = true } +serde = { workspace = true, features = ["derive"] } consensus-types = { path = "../consensus-types" } executor-types = { path = "../../types/executor-types" } diff --git a/crates/cfxcore/core/src/pos/storage/backup/backup-cli/Cargo.toml b/crates/cfxcore/core/src/pos/storage/backup/backup-cli/Cargo.toml index b887522207..b0ce3c05bd 100644 --- a/crates/cfxcore/core/src/pos/storage/backup/backup-cli/Cargo.toml +++ b/crates/cfxcore/core/src/pos/storage/backup/backup-cli/Cargo.toml @@ -15,7 +15,7 @@ byteorder = "1.4.3" bytes = "1.0.1" futures = "0.3.12" hex = "0.4.3" -itertools = "0.10.0" +itertools = { workspace = true } num_cpus = "1.13.0" once_cell = "1.7.2" pin-project = "1.0.5" diff --git a/crates/cfxcore/core/src/pos/storage/pos-ledger-db/Cargo.toml b/crates/cfxcore/core/src/pos/storage/pos-ledger-db/Cargo.toml index 710da33d32..19215ca670 100644 --- a/crates/cfxcore/core/src/pos/storage/pos-ledger-db/Cargo.toml +++ b/crates/cfxcore/core/src/pos/storage/pos-ledger-db/Cargo.toml @@ -10,17 +10,17 @@ publish = false edition = "2018" [dependencies] -anyhow = "1.0.38" +anyhow = { workspace = true } arc-swap = "1.2.0" byteorder = "1.4.3" -itertools = "0.10.0" -once_cell = "1.7.2" +itertools = { workspace = true } +once_cell = { workspace = true } num-derive = "0.3.3" num-traits = "0.2.14" proptest = { version = "1.0.0", optional = true } proptest-derive = { version = "0.3.0", optional = true } serde = "1.0.124" -thiserror = "1.0.24" +thiserror = { workspace = true } accumulator = { path = "../accumulator" } bcs = "0.1.2" diff --git a/crates/cfxcore/core/src/pos/storage/scratchpad/Cargo.toml b/crates/cfxcore/core/src/pos/storage/scratchpad/Cargo.toml index a4623621ee..0e471ade16 100644 --- a/crates/cfxcore/core/src/pos/storage/scratchpad/Cargo.toml +++ b/crates/cfxcore/core/src/pos/storage/scratchpad/Cargo.toml @@ -11,7 +11,7 @@ edition = "2018" [dependencies] arc-swap = "1.2.0" -itertools = "0.10.0" +itertools = { workspace = true } diem-crypto = { path = "../../crypto/crypto" } diem-infallible = { path = "../../common/infallible" } diff --git a/crates/cfxcore/core/src/pos/storage/storage-interface/Cargo.toml b/crates/cfxcore/core/src/pos/storage/storage-interface/Cargo.toml index f64f438b47..b6f5afc7a7 100644 --- a/crates/cfxcore/core/src/pos/storage/storage-interface/Cargo.toml +++ b/crates/cfxcore/core/src/pos/storage/storage-interface/Cargo.toml @@ -11,7 +11,7 @@ edition = "2018" [dependencies] anyhow = "1.0.38" -itertools = "0.10.0" +itertools = { workspace = true } serde = { version = "1.0.124", default-features = false } thiserror = "1.0.24" parking_lot = "0.11.1" diff --git a/crates/cfxcore/core/src/pos/storage/storage-service/Cargo.toml b/crates/cfxcore/core/src/pos/storage/storage-service/Cargo.toml index 8aad7729b9..6208c4f3d9 100644 --- a/crates/cfxcore/core/src/pos/storage/storage-service/Cargo.toml +++ b/crates/cfxcore/core/src/pos/storage/storage-service/Cargo.toml @@ -28,7 +28,7 @@ storage-client = { path = "../storage-client", optional = true } proptest = { version = "1.0.0", optional = true } [dev-dependencies] -itertools = "0.10.0" +itertools = { workspace = true } pos-ledger-db = { path = "../pos-ledger-db", features = ["fuzzing"] } diem-temppath = { path = "../../common/temppath" } proptest = "1.0.0" diff --git a/crates/client/Cargo.toml b/crates/client/Cargo.toml index a8c472175a..b8f3ed2cb0 100644 --- a/crates/client/Cargo.toml +++ b/crates/client/Cargo.toml @@ -10,9 +10,9 @@ mio = "0.6.8" clap = "2" textwrap = "0.9" toml = "0.5.8" -serde = { version = "1.0", features = ["derive"] } -serde_json = "1.0" -serde_derive = "1.0" +serde = { workspace = true, features = ["derive"] } +serde_json = { workspace = true } +serde_derive = { workspace = true } parking_lot = { workspace = true } io = { workspace = true } cfxkey = { workspace = true } @@ -27,15 +27,15 @@ cfx-vm-types = { workspace = true } app_dirs = "1.2.1" dir = { git = "https://github.com/Conflux-Chain/conflux-parity-deps.git", rev = "1597a9cab02343eb2322ca0ac58d39b64e3f42d1" } ctrlc = { git = "https://github.com/paritytech/rust-ctrlc.git", rev="b523017108bb2d571a7a69bd97bc406e63bc7a9d" } -jsonrpc-core = "15.1.0" +jsonrpc-core = { workspace = true } jsonrpc-tcp-server = "15.1.0" jsonrpc-http-server = "15.1.0" jsonrpc-derive = "15.1.0" jsonrpc-core-client = "15.1.0" jsonrpc-pubsub = "15.1.0" jsonrpc-ws-server = "15.1.0" -error-chain = { version = "0.12" } -lazy_static = "1.4" +error-chain = { workspace = true } +lazy_static = { workspace = true } log = { workspace = true } cfx-types = { workspace = true } cfx-addr = { workspace = true } @@ -52,7 +52,7 @@ primitives = { workspace = true } log4rs = { version = "1.2.0", features = ["background_rotation", "gzip"] } rlp = { workspace = true } keccak-hash = { workspace = true } -rand = "0.7.2" +rand = { workspace = true } rand_08 = {package = "rand", version = "0.8.0"} kvdb-rocksdb = { workspace = true } tempdir = "0.3" @@ -62,11 +62,11 @@ metrics = { workspace = true } delegate = { workspace = true } transient-hashmap = "0.4" rand_xorshift = "0.2" -itertools = "0.9" +itertools = { workspace = true } order-stat = "0.1" futures01 = "0.1" futures = { version = "0.3.3", features = ["compat"] } -rayon = "1.2.0" +rayon = { workspace = true } throttling = { workspace = true } tokio = { version = "1.6", features = ["full"] } tokio-stream = "0.1.4" @@ -74,8 +74,8 @@ tokio-timer = "0.2.13" malloc_size_of = { workspace = true } random-crash = { workspace = true } cfx-parameters = { workspace = true } -strum = "0.20" -strum_macros = "0.20" +strum = { workspace = true } +strum_macros = { workspace = true } crash-handler = { path = "../cfxcore/core/src/pos/common/crash-handler" } cached-pos-ledger-db = { path = "../cfxcore/core/src/pos/storage/cached-pos-ledger-db" } pos-ledger-db = { path = "../cfxcore/core/src/pos/storage/pos-ledger-db" } @@ -91,7 +91,7 @@ diem-types = { path = "../cfxcore/core/src/pos/types" } pow-types = {path = "../cfxcore/core/src/pos/types/pow-types" } executor-types = { path = "../cfxcore/core/src/pos/types/executor-types" } executor = { path = "../cfxcore/core/src/pos/consensus/executor" } -fail = "0.4.0" +fail = { workspace = true } storage-interface = { path = "../cfxcore/core/src/pos/storage/storage-interface" } consensus-types = {path = "../cfxcore/core/src/pos/consensus/consensus-types" } rpassword = "5.0.1" diff --git a/crates/primitives/Cargo.toml b/crates/primitives/Cargo.toml index fa0ce12eea..62e6c4e621 100644 --- a/crates/primitives/Cargo.toml +++ b/crates/primitives/Cargo.toml @@ -4,29 +4,29 @@ version = "0.2.0" edition = "2021" [dependencies] -byteorder = "1.2.7" +byteorder = { workspace = true } cfx-types = { workspace = true } cfx-bytes = { workspace = true } cfxkey = { workspace = true } -fixed-hash = "0.5" +fixed-hash = { workspace = true } keccak-hash = { workspace = true } -lazy_static = "1.4" -log = "0.4" +lazy_static = { workspace = true } +log = { workspace = true } malloc_size_of = { workspace = true } -rand = "0.7" +rand = { workspace = true } rlp = { workspace = true } rlp_derive = { workspace = true } -serde = { version = "1.0", features = ["derive"] } -serde_derive = "1.0" +serde = { workspace = true, features = ["derive"] } +serde_derive = { workspace = true } siphasher = "0.3" unexpected = { git = "https://github.com/Conflux-Chain/conflux-parity-deps.git", rev = "1597a9cab02343eb2322ca0ac58d39b64e3f42d1" } -once_cell = "1.17.1" +once_cell = { workspace = true } cfx-parameters = { workspace = true } [dev-dependencies] criterion = "0.3" -serde_json = "1.0" -itertools = "0.10" +serde_json = { workspace = true } +itertools = { workspace = true } [[bench]] name = "benchmark" diff --git a/crates/rpc/rpc-eth-api/Cargo.toml b/crates/rpc/rpc-eth-api/Cargo.toml index bd91a7c45c..8457e9afe3 100644 --- a/crates/rpc/rpc-eth-api/Cargo.toml +++ b/crates/rpc/rpc-eth-api/Cargo.toml @@ -14,7 +14,7 @@ license-file.workspace = true [dependencies] cfx-rpc-eth-types = { workspace = true } -jsonrpsee = { version = "0.24.4", features = ["server", "macros"] } +jsonrpsee = { workspace = true, features = ["server", "macros"] } cfx-types = { workspace = true } cfx-rpc-primitives = { workspace = true } alloy-rpc-types-trace = { workspace = true } diff --git a/crates/rpc/rpc-eth-types/Cargo.toml b/crates/rpc/rpc-eth-types/Cargo.toml index 1f77324ae1..9f3bcfcc26 100644 --- a/crates/rpc/rpc-eth-types/Cargo.toml +++ b/crates/rpc/rpc-eth-types/Cargo.toml @@ -18,7 +18,7 @@ serde = { workspace = true } serde_json = { workspace = true } cfx-types = { workspace = true } thiserror = { workspace = true } -jsonrpc-core = "15.1.0" +jsonrpc-core = { workspace = true } primitives = { workspace = true } rlp = { workspace = true } cfx-parameters = { workspace = true } From 5bf27453cc7bd9294f510ad17e261290498a1673 Mon Sep 17 00:00:00 2001 From: Pana Date: Wed, 18 Sep 2024 15:45:12 +0800 Subject: [PATCH 5/7] add net web3 rpc three api traits --- crates/rpc/rpc-eth-api/src/lib.rs | 6 +++ crates/rpc/rpc-eth-api/src/net.rs | 19 ++++++++++ crates/rpc/rpc-eth-api/src/rpc.rs | 10 +++++ crates/rpc/rpc-eth-api/src/web3.rs | 16 ++++++++ crates/rpc/rpc-primitives/src/lib.rs | 2 + crates/rpc/rpc-primitives/src/rpc_module.rs | 42 +++++++++++++++++++++ 6 files changed, 95 insertions(+) create mode 100644 crates/rpc/rpc-eth-api/src/net.rs create mode 100644 crates/rpc/rpc-eth-api/src/rpc.rs create mode 100644 crates/rpc/rpc-eth-api/src/web3.rs create mode 100644 crates/rpc/rpc-primitives/src/rpc_module.rs diff --git a/crates/rpc/rpc-eth-api/src/lib.rs b/crates/rpc/rpc-eth-api/src/lib.rs index a638f8f469..4cc21f3986 100644 --- a/crates/rpc/rpc-eth-api/src/lib.rs +++ b/crates/rpc/rpc-eth-api/src/lib.rs @@ -3,9 +3,15 @@ mod eth; mod filter; mod pubsub; mod trace; +mod rpc; +mod net; +mod web3; pub use debug::DebugApiServer; pub use eth::EthApiServer; pub use filter::EthFilterApiServer; pub use pubsub::EthPubSubApiServer; pub use trace::TraceApiServer; +pub use rpc::RpcApiServer; +pub use net::NetApiServer; +pub use web3::Web3ApiServer; diff --git a/crates/rpc/rpc-eth-api/src/net.rs b/crates/rpc/rpc-eth-api/src/net.rs new file mode 100644 index 0000000000..7ab256d718 --- /dev/null +++ b/crates/rpc/rpc-eth-api/src/net.rs @@ -0,0 +1,19 @@ +use cfx_types::U64; +use jsonrpsee::{core::RpcResult, proc_macros::rpc}; + +/// Net rpc interface. +#[rpc(server, namespace = "net")] +pub trait NetApi { + /// Returns the network ID. + #[method(name = "version")] + fn version(&self) -> RpcResult; + + /// Returns number of peers connected to node. + #[method(name = "peerCount")] + fn peer_count(&self) -> RpcResult; + + /// Returns true if client is actively listening for network connections. + /// Otherwise false. + #[method(name = "listening")] + fn is_listening(&self) -> RpcResult; +} diff --git a/crates/rpc/rpc-eth-api/src/rpc.rs b/crates/rpc/rpc-eth-api/src/rpc.rs new file mode 100644 index 0000000000..1b227b76ae --- /dev/null +++ b/crates/rpc/rpc-eth-api/src/rpc.rs @@ -0,0 +1,10 @@ +use jsonrpsee::{core::RpcResult, proc_macros::rpc}; +use cfx_rpc_primitives::RpcModules; + +/// RPC namespace, used to find the versions of all rpc modules +#[rpc(server, namespace = "rpc")] +pub trait RpcApi { + /// Lists enabled APIs and the version of each. + #[method(name = "modules")] + fn rpc_modules(&self) -> RpcResult; +} diff --git a/crates/rpc/rpc-eth-api/src/web3.rs b/crates/rpc/rpc-eth-api/src/web3.rs new file mode 100644 index 0000000000..b119c12003 --- /dev/null +++ b/crates/rpc/rpc-eth-api/src/web3.rs @@ -0,0 +1,16 @@ +use cfx_types::H256; +use jsonrpsee::{core::RpcResult, proc_macros::rpc}; +use cfx_rpc_primitives::Bytes; + +/// Web3 rpc interface. +#[cfg_attr(not(feature = "client"), rpc(server, namespace = "web3"))] +#[cfg_attr(feature = "client", rpc(server, client, namespace = "web3"))] +pub trait Web3Api { + /// Returns current client version. + #[method(name = "clientVersion")] + async fn client_version(&self) -> RpcResult; + + /// Returns sha3 of the given data. + #[method(name = "sha3")] + fn sha3(&self, input: Bytes) -> RpcResult; +} diff --git a/crates/rpc/rpc-primitives/src/lib.rs b/crates/rpc/rpc-primitives/src/lib.rs index 02b97662ac..0029c01e92 100644 --- a/crates/rpc/rpc-primitives/src/lib.rs +++ b/crates/rpc/rpc-primitives/src/lib.rs @@ -2,11 +2,13 @@ mod bytes; mod index; mod variadic_u64; mod variadic_value; +mod rpc_module; pub use bytes::Bytes; pub use index::Index; pub use variadic_u64::U64; pub use variadic_value::VariadicValue; +pub use rpc_module::RpcModules; // helper implementing automatic Option> -> Option> conversion pub fn maybe_vec_into(src: &Option>) -> Option> diff --git a/crates/rpc/rpc-primitives/src/rpc_module.rs b/crates/rpc/rpc-primitives/src/rpc_module.rs new file mode 100644 index 0000000000..c6f062cfa5 --- /dev/null +++ b/crates/rpc/rpc-primitives/src/rpc_module.rs @@ -0,0 +1,42 @@ +//! Types for the `rpc` API. +use serde::{Deserialize, Serialize}; +use std::collections::HashMap; + +/// Represents the `rpc_modules` response, which returns the +/// list of all available modules on that transport and their version +#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, Default)] +#[serde(transparent)] +pub struct RpcModules { + module_map: HashMap, +} + +impl RpcModules { + /// Create a new instance of `RPCModules` + pub const fn new(module_map: HashMap) -> Self { + Self { module_map } + } + + /// Consumes self and returns the inner hashmap mapping module names to their versions + pub fn into_modules(self) -> HashMap { + self.module_map + } +} + +#[cfg(test)] +mod tests { + use super::*; + #[test] + fn test_parse_module_versions_roundtrip() { + let s = r#"{"txpool":"1.0","trace":"1.0","eth":"1.0","web3":"1.0","net":"1.0"}"#; + let module_map = HashMap::from([ + ("txpool".to_owned(), "1.0".to_owned()), + ("trace".to_owned(), "1.0".to_owned()), + ("eth".to_owned(), "1.0".to_owned()), + ("web3".to_owned(), "1.0".to_owned()), + ("net".to_owned(), "1.0".to_owned()), + ]); + let m = RpcModules::new(module_map); + let de_serialized: RpcModules = serde_json::from_str(s).unwrap(); + assert_eq!(de_serialized, m); + } +} From 7b19a0de864cf61eb4619f3297a57733860c5c52 Mon Sep 17 00:00:00 2001 From: Pana Date: Fri, 20 Sep 2024 15:51:55 +0800 Subject: [PATCH 6/7] impl Web3ApiServer --- Cargo.lock | 37 +++++++++++++------ Cargo.toml | 17 +++++++-- bins/conflux/Cargo.toml | 2 +- crates/blockgen/Cargo.toml | 6 +-- crates/cfxcore/core/Cargo.toml | 4 +- .../cfxcore/core/benchmark/storage/Cargo.toml | 2 +- .../core/src/pos/crypto/crypto/Cargo.toml | 2 +- crates/cfxcore/executor/Cargo.toml | 2 +- crates/client/Cargo.toml | 2 +- crates/dbs/statedb/Cargo.toml | 2 +- crates/rpc/rpc-eth-api/Cargo.toml | 2 +- crates/rpc/rpc-eth-api/src/web3.rs | 3 +- crates/rpc/rpc/Cargo.toml | 24 ++++++++++++ crates/rpc/rpc/src/lib.rs | 3 ++ crates/rpc/rpc/src/web3.rs | 30 +++++++++++++++ crates/transactiongen/Cargo.toml | 2 +- 16 files changed, 111 insertions(+), 29 deletions(-) create mode 100644 crates/rpc/rpc/Cargo.toml create mode 100644 crates/rpc/rpc/src/lib.rs create mode 100644 crates/rpc/rpc/src/web3.rs diff --git a/Cargo.lock b/Cargo.lock index a7903cfc95..a5f0ff52fe 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1311,6 +1311,21 @@ dependencies = [ "strum_macros 0.20.1", ] +[[package]] +name = "cfx-rpc" +version = "2.4.0" +dependencies = [ + "async-trait", + "cfx-rpc-eth-api", + "cfx-rpc-eth-types", + "cfx-rpc-primitives", + "cfx-types", + "clap", + "jsonrpsee", + "keccak-hash", + "parity-version", +] + [[package]] name = "cfx-rpc-cfx-types" version = "2.4.0" @@ -1330,6 +1345,17 @@ dependencies = [ "strum_macros 0.20.1", ] +[[package]] +name = "cfx-rpc-eth-api" +version = "2.4.0" +dependencies = [ + "alloy-rpc-types-trace", + "cfx-rpc-eth-types", + "cfx-rpc-primitives", + "cfx-types", + "jsonrpsee", +] + [[package]] name = "cfx-rpc-eth-types" version = "2.4.0" @@ -7167,17 +7193,6 @@ dependencies = [ "winapi 0.3.9", ] -[[package]] -name = "rpc-eth-api" -version = "2.4.0" -dependencies = [ - "alloy-rpc-types-trace", - "cfx-rpc-eth-types", - "cfx-rpc-primitives", - "cfx-types", - "jsonrpsee", -] - [[package]] name = "ruint" version = "1.12.3" diff --git a/Cargo.toml b/Cargo.toml index 78fea71860..5c231a7e8b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -55,10 +55,11 @@ members = [ "crates/serde_utils", "crates/cfxcore/geth-tracer", "crates/rpc/rpc-eth-types", - "crates/rpc/rpc-primitives", + "crates/rpc/rpc-primitives", "crates/rpc/rpc-cfx-types", "crates/rpc/rpc-eth-api", "crates/cfxcore/parity-trace-types", + "crates/rpc/rpc", ] resolver = "2" @@ -148,6 +149,7 @@ cfx-rpc-eth-types = { path = "./crates/rpc/rpc-eth-types" } cfx-rpc-primitives = { path = "./crates/rpc/rpc-primitives" } cfx-rpc-cfx-types = { path = "./crates/rpc/rpc-cfx-types" } cfx-parity-trace-types = { path = "./crates/cfxcore/parity-trace-types" } +cfx-rpc-eth-api = { path = "./crates/rpc/rpc-eth-api" } serde = { version = "1.0", features = ["derive", "alloc"] } serde_json = "1.0" @@ -170,8 +172,6 @@ rand = "0.7" itertools = "0.10.0" byteorder = "1.2.7" -fixed-hash = "0.5" -keccak-hash = "0.5" rlp = "0.4.0" rlp_derive = { git = "https://github.com/Conflux-Chain/conflux-parity-deps.git", rev = "1597a9cab02343eb2322ca0ac58d39b64e3f42d1" } hex = "0.4.3" @@ -184,3 +184,14 @@ alloy-rpc-types-trace = { git = "https://github.com/alloy-rs/alloy", rev = "4e22 revm = "8.0" bls-signatures = { git = "https://github.com/Conflux-Chain/bls-signatures.git", rev = "fb52187df92d27c365642cb7e7b2aaf60437cf9c", default-features = false, features = ["multicore", "blst"] } + +# async +async-trait = "0.1" + +# crypto +fixed-hash = "0.5" +keccak-hash = "0.5" +tiny-keccak = "2.0.2" + +# misc +clap = "2" diff --git a/bins/conflux/Cargo.toml b/bins/conflux/Cargo.toml index d983455899..b3f1f93a7e 100644 --- a/bins/conflux/Cargo.toml +++ b/bins/conflux/Cargo.toml @@ -9,7 +9,7 @@ documentation.workspace = true [dependencies] mio = "0.6.8" -clap = { version = "2", features = ["yaml"] } +clap = { workspace = true, features = ["yaml"] } textwrap = "0.9" toml = "0.5.8" serde = "1.0" diff --git a/crates/blockgen/Cargo.toml b/crates/blockgen/Cargo.toml index b025debb99..19feaa4d6d 100644 --- a/crates/blockgen/Cargo.toml +++ b/crates/blockgen/Cargo.toml @@ -7,15 +7,15 @@ version = "0.1.0" edition = "2021" [dependencies] -clap = "2" +clap = { workspace = true } cfxcore = { workspace = true } cfxkey = { workspace = true } cfx-parameters = { workspace = true } cfx-stratum = { workspace = true } cfx-types = { workspace = true } db = { workspace = true } -lazy_static = "1.4" -log = "0.4" +lazy_static = { workspace = true } +log = { workspace = true } metrics = { workspace = true } network = { workspace = true } primitives = { workspace = true } diff --git a/crates/cfxcore/core/Cargo.toml b/crates/cfxcore/core/Cargo.toml index 545b9ac671..8f88a8c38f 100644 --- a/crates/cfxcore/core/Cargo.toml +++ b/crates/cfxcore/core/Cargo.toml @@ -26,7 +26,7 @@ cfx-vm-interpreter = { workspace = true } cfx-executor = { workspace = true } cfx-execute-helper = { workspace = true } channel = { path = "src/pos/common/channel" } -clap = "2" +clap = { workspace = true } dag = { workspace = true } derivative = "2.0.2" db = { workspace = true } @@ -97,7 +97,7 @@ strum = { workspace = true } strum_macros = { workspace = true } smart-default = "0.6.0" bls-signatures = { workspace = true } -tiny-keccak = {version = "2.0", features = ["keccak"]} +tiny-keccak = { workspace = true, features = ["keccak"]} bcs = "0.1.2" async-trait = "0.1" num-derive = { version = "0.3.3", default-features = false } diff --git a/crates/cfxcore/core/benchmark/storage/Cargo.toml b/crates/cfxcore/core/benchmark/storage/Cargo.toml index 61bf3a6688..2ecff9c0ea 100644 --- a/crates/cfxcore/core/benchmark/storage/Cargo.toml +++ b/crates/cfxcore/core/benchmark/storage/Cargo.toml @@ -15,7 +15,7 @@ cfx-internal-common = { workspace = true } primitives = { workspace = true } db = { workspace = true } -clap = "2.32" +clap = { workspace = true } env_logger = "0.7" error-chain = "0.12" ethcore = { package = "ethcore", git = "https://github.com/paritytech/parity-ethereum", tag = "v2.4.0" } diff --git a/crates/cfxcore/core/src/pos/crypto/crypto/Cargo.toml b/crates/cfxcore/core/src/pos/crypto/crypto/Cargo.toml index a3630d468b..5b881096b0 100644 --- a/crates/cfxcore/core/src/pos/crypto/crypto/Cargo.toml +++ b/crates/cfxcore/core/src/pos/crypto/crypto/Cargo.toml @@ -29,7 +29,7 @@ serde-name = "0.1.1" sha2 = "0.9.3" static_assertions = "1.1.0" thiserror = "1.0.24" -tiny-keccak = { version = "2.0.2", features = ["sha3"] } +tiny-keccak = { workspace = true, features = ["sha3"] } x25519-dalek = { version = "0.1.0", package = "x25519-dalek-fiat", default-features = false, features = ["std"] } aes-gcm = "0.8.0" diem-crypto-derive = { path = "../crypto-derive", version = "0.1.0" } diff --git a/crates/cfxcore/executor/Cargo.toml b/crates/cfxcore/executor/Cargo.toml index f15dfc8303..66c91172d4 100644 --- a/crates/cfxcore/executor/Cargo.toml +++ b/crates/cfxcore/executor/Cargo.toml @@ -41,7 +41,7 @@ sha3-macro = { workspace = true } strum = { workspace = true } strum_macros = { workspace = true } bls-signatures = {git = "https://github.com/Conflux-Chain/bls-signatures.git", rev = "fb52187df92d27c365642cb7e7b2aaf60437cf9c", default-features = false, features = ["multicore"]} -tiny-keccak = {version = "2.0", features = ["keccak"]} +tiny-keccak = { workspace = true, features = ["keccak"]} diem-crypto = { path = "../core/src/pos/crypto/crypto" } diem-types = { path = "../core/src/pos/types" } pow-types = {path = "../core/src/pos/types/pow-types" } diff --git a/crates/client/Cargo.toml b/crates/client/Cargo.toml index b8f3ed2cb0..9551f2aede 100644 --- a/crates/client/Cargo.toml +++ b/crates/client/Cargo.toml @@ -7,7 +7,7 @@ edition = "2021" bigdecimal = "0.1.0" num-bigint = "0.2.6" mio = "0.6.8" -clap = "2" +clap = { workspace = true } textwrap = "0.9" toml = "0.5.8" serde = { workspace = true, features = ["derive"] } diff --git a/crates/dbs/statedb/Cargo.toml b/crates/dbs/statedb/Cargo.toml index 95f328bca2..cc94309618 100644 --- a/crates/dbs/statedb/Cargo.toml +++ b/crates/dbs/statedb/Cargo.toml @@ -18,7 +18,7 @@ log = "0.4" parking_lot = { workspace = true } primitives = { workspace = true, optional = true } rlp = { workspace = true } -tiny-keccak = "2.0.2" +tiny-keccak = { workspace = true } lazy_static = "1.4.0" [dev-dependencies] diff --git a/crates/rpc/rpc-eth-api/Cargo.toml b/crates/rpc/rpc-eth-api/Cargo.toml index 8457e9afe3..d49312bf5d 100644 --- a/crates/rpc/rpc-eth-api/Cargo.toml +++ b/crates/rpc/rpc-eth-api/Cargo.toml @@ -1,5 +1,5 @@ [package] -name = "rpc-eth-api" +name = "cfx-rpc-eth-api" edition = "2021" version.workspace = true authors.workspace = true diff --git a/crates/rpc/rpc-eth-api/src/web3.rs b/crates/rpc/rpc-eth-api/src/web3.rs index e047d5365b..13b18ac7d3 100644 --- a/crates/rpc/rpc-eth-api/src/web3.rs +++ b/crates/rpc/rpc-eth-api/src/web3.rs @@ -3,8 +3,7 @@ use cfx_types::H256; use jsonrpsee::{core::RpcResult, proc_macros::rpc}; /// Web3 rpc interface. -#[cfg_attr(not(feature = "client"), rpc(server, namespace = "web3"))] -#[cfg_attr(feature = "client", rpc(server, client, namespace = "web3"))] +#[rpc(server, namespace = "web3")] pub trait Web3Api { /// Returns current client version. #[method(name = "clientVersion")] diff --git a/crates/rpc/rpc/Cargo.toml b/crates/rpc/rpc/Cargo.toml new file mode 100644 index 0000000000..b1c2c26375 --- /dev/null +++ b/crates/rpc/rpc/Cargo.toml @@ -0,0 +1,24 @@ +[package] +name = "cfx-rpc" +edition = "2021" +version.workspace = true +authors.workspace = true +description.workspace = true +documentation.workspace = true +homepage.workspace = true +keywords.workspace = true +repository.workspace = true +license-file.workspace = true + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +cfx-rpc-eth-api = { workspace = true } +jsonrpsee = { workspace = true, features = ["server", "macros"] } +cfx-types = { workspace = true } +cfx-rpc-eth-types = { workspace = true } +cfx-rpc-primitives = { workspace = true } +async-trait = { workspace = true } +keccak-hash = { workspace = true} +parity-version = { workspace = true } +clap = { workspace = true } \ No newline at end of file diff --git a/crates/rpc/rpc/src/lib.rs b/crates/rpc/rpc/src/lib.rs new file mode 100644 index 0000000000..8d095b833b --- /dev/null +++ b/crates/rpc/rpc/src/lib.rs @@ -0,0 +1,3 @@ +mod web3; + +pub use web3::Web3Api; diff --git a/crates/rpc/rpc/src/web3.rs b/crates/rpc/rpc/src/web3.rs new file mode 100644 index 0000000000..7a87cccab9 --- /dev/null +++ b/crates/rpc/rpc/src/web3.rs @@ -0,0 +1,30 @@ +use async_trait::async_trait; +use cfx_rpc_eth_api::Web3ApiServer; +use cfx_rpc_primitives::Bytes; +use cfx_types::H256; +use clap::crate_version; +use jsonrpsee::core::RpcResult; +use keccak_hash::keccak; + +pub struct Web3Api; + +impl Web3Api { + pub fn new() -> Web3Api { Web3Api } +} + +#[async_trait] +impl Web3ApiServer for Web3Api { + async fn client_version(&self) -> RpcResult { + Ok(parity_version::version(crate_version!())) + } + + fn sha3(&self, input: Bytes) -> RpcResult { + Ok(keccak(input.into_vec())) + } +} + +impl std::fmt::Debug for Web3Api { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.debug_struct("Web3Api").finish_non_exhaustive() + } +} diff --git a/crates/transactiongen/Cargo.toml b/crates/transactiongen/Cargo.toml index 614d6e71d3..34298acffb 100644 --- a/crates/transactiongen/Cargo.toml +++ b/crates/transactiongen/Cargo.toml @@ -7,7 +7,7 @@ version = "0.1.0" edition = "2021" [dependencies] -clap = "2" +clap = { workspace = true } cfxcore = { workspace = true } cfx-types = { workspace = true } cfx-vm-types = { workspace = true } From 5032debbb21e771fb14cae4f86be57e2ff0545e0 Mon Sep 17 00:00:00 2001 From: Pana Date: Fri, 20 Sep 2024 16:54:43 +0800 Subject: [PATCH 7/7] add net and rpc api impl --- Cargo.lock | 1 + crates/rpc/rpc/Cargo.toml | 3 ++- crates/rpc/rpc/src/lib.rs | 4 ++++ crates/rpc/rpc/src/net.rs | 21 +++++++++++++++++++++ crates/rpc/rpc/src/rpc.rs | 28 ++++++++++++++++++++++++++++ 5 files changed, 56 insertions(+), 1 deletion(-) create mode 100644 crates/rpc/rpc/src/net.rs create mode 100644 crates/rpc/rpc/src/rpc.rs diff --git a/Cargo.lock b/Cargo.lock index a5f0ff52fe..836fd0cdb4 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1315,6 +1315,7 @@ dependencies = [ name = "cfx-rpc" version = "2.4.0" dependencies = [ + "alloy-rpc-types", "async-trait", "cfx-rpc-eth-api", "cfx-rpc-eth-types", diff --git a/crates/rpc/rpc/Cargo.toml b/crates/rpc/rpc/Cargo.toml index b1c2c26375..1e2c5d0098 100644 --- a/crates/rpc/rpc/Cargo.toml +++ b/crates/rpc/rpc/Cargo.toml @@ -21,4 +21,5 @@ cfx-rpc-primitives = { workspace = true } async-trait = { workspace = true } keccak-hash = { workspace = true} parity-version = { workspace = true } -clap = { workspace = true } \ No newline at end of file +clap = { workspace = true } +alloy-rpc-types = { workspace = true } \ No newline at end of file diff --git a/crates/rpc/rpc/src/lib.rs b/crates/rpc/rpc/src/lib.rs index 8d095b833b..a0ca858e77 100644 --- a/crates/rpc/rpc/src/lib.rs +++ b/crates/rpc/rpc/src/lib.rs @@ -1,3 +1,7 @@ +mod net; +mod rpc; mod web3; +pub use net::NetApi; +pub use rpc::RPCApi; pub use web3::Web3Api; diff --git a/crates/rpc/rpc/src/net.rs b/crates/rpc/rpc/src/net.rs new file mode 100644 index 0000000000..df05094f98 --- /dev/null +++ b/crates/rpc/rpc/src/net.rs @@ -0,0 +1,21 @@ +use cfx_rpc_eth_api::NetApiServer; +use cfx_types::U64; +use jsonrpsee::core::RpcResult; + +pub struct NetApi { + chain_id: u64, +} + +impl NetApiServer for NetApi { + fn version(&self) -> RpcResult { + // todo read chain_id from config, not in this struct + Ok(self.chain_id.to_string()) + } + + fn peer_count(&self) -> RpcResult { + // todo implement peer count + Ok(U64::from(0)) + } + + fn is_listening(&self) -> RpcResult { Ok(true) } +} diff --git a/crates/rpc/rpc/src/rpc.rs b/crates/rpc/rpc/src/rpc.rs new file mode 100644 index 0000000000..49bc0139ff --- /dev/null +++ b/crates/rpc/rpc/src/rpc.rs @@ -0,0 +1,28 @@ +use cfx_rpc_eth_api::RpcApiServer; +use cfx_rpc_primitives::RpcModules; +use jsonrpsee::core::RpcResult; +use std::{collections::HashMap, sync::Arc}; + +/// `rpc` API implementation. +/// +/// This type provides the functionality for handling `rpc` requests +#[derive(Debug, Clone, Default)] +pub struct RPCApi { + /// The implementation of the Arc api + rpc_modules: Arc, +} + +impl RPCApi { + /// Return a new `RPCApi` struct, with given `module_map` + pub fn new(module_map: HashMap) -> Self { + Self { + rpc_modules: Arc::new(RpcModules::new(module_map)), + } + } +} + +impl RpcApiServer for RPCApi { + fn rpc_modules(&self) -> RpcResult { + Ok(self.rpc_modules.as_ref().clone()) + } +}