diff --git a/Cargo.lock b/Cargo.lock index 3e7794533e4f93..37c14edd831689 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -44,6 +44,17 @@ dependencies = [ "version_check 0.9.2", ] +[[package]] +name = "ahash" +version = "0.7.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "43bb833f0bf979d8475d38fbf09ed3b8a55e1885fe93ad3f93239fc6a4f17b98" +dependencies = [ + "getrandom 0.2.0", + "once_cell", + "version_check 0.9.2", +] + [[package]] name = "aho-corasick" version = "0.7.10" @@ -120,9 +131,9 @@ dependencies = [ [[package]] name = "assert_matches" -version = "1.3.0" +version = "1.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7deb0a829ca7bcfaf5da70b073a8d128619259a7be8216a355e23f00763059e5" +checksum = "9b34d609dfbaf33d6889b2b7106d3ca345eacad44200913df5ba02bfd31d2ba9" [[package]] name = "async-stream" @@ -419,6 +430,12 @@ version = "0.3.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "476e9cd489f9e121e02ffa6014a8ef220ecb15c05ed23fc34cca13925dc283fb" +[[package]] +name = "bs58" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "771fe0050b883fcc3ea2359b1a96bcfbc090b7116eae7c3c512c7a083fdf23d3" + [[package]] name = "bstr" version = "0.2.16" @@ -1193,7 +1210,7 @@ checksum = "17392a012ea30ef05a610aa97dfb49496e71c9f676b27879922ea5bdf60d9d3f" dependencies = [ "atty", "humantime", - "log 0.4.11", + "log 0.4.14", "regex", "termcolor", ] @@ -1537,7 +1554,7 @@ dependencies = [ "aho-corasick", "bstr", "fnv", - "log 0.4.11", + "log 0.4.14", "regex", ] @@ -1549,7 +1566,7 @@ checksum = "4a1d5b4e896797c19dff490f9706817b42e9b7aa4adfe844464d3bbc9aabb035" dependencies = [ "arc-swap 1.2.0", "futures 0.3.8", - "log 0.4.11", + "log 0.4.14", "reqwest", "serde", "serde_derive", @@ -1566,7 +1583,7 @@ version = "0.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c69552f48b18aa6102ce0c82dd9bc9d3f8af5fc0a5797069b1b466b90570e39c" dependencies = [ - "log 0.4.11", + "log 0.4.14", "plain", "scroll", ] @@ -1619,6 +1636,9 @@ name = "hashbrown" version = "0.11.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ab5ef0d4909ef3724cc8cce6ccc8572c5c817592e9285f5464f8e86f8bd3726e" +dependencies = [ + "ahash 0.7.4", +] [[package]] name = "heck" @@ -1793,7 +1813,7 @@ checksum = "5f9f7a97316d44c0af9b0301e65010573a853a9fc97046d7331d7f6bc0fd5a64" dependencies = [ "futures-util", "hyper 0.14.11", - "log 0.4.11", + "log 0.4.14", "rustls", "tokio", "tokio-rustls", @@ -1971,7 +1991,7 @@ dependencies = [ "jsonrpc-core", "jsonrpc-pubsub", "jsonrpc-server-utils", - "log 0.4.11", + "log 0.4.14", "parity-tokio-ipc", "serde", "serde_json", @@ -1989,7 +2009,7 @@ dependencies = [ "futures 0.3.8", "futures-executor", "futures-util", - "log 0.4.11", + "log 0.4.14", "serde", "serde_derive", "serde_json", @@ -2027,7 +2047,7 @@ dependencies = [ "hyper 0.14.11", "jsonrpc-core", "jsonrpc-server-utils", - "log 0.4.11", + "log 0.4.14", "net2", "parking_lot 0.11.2", "unicase 2.6.0", @@ -2042,7 +2062,7 @@ dependencies = [ "futures 0.3.8", "jsonrpc-core", "jsonrpc-server-utils", - "log 0.4.11", + "log 0.4.14", "parity-tokio-ipc", "parking_lot 0.11.2", "tower-service", @@ -2057,7 +2077,7 @@ dependencies = [ "futures 0.3.8", "jsonrpc-core", "lazy_static", - "log 0.4.11", + "log 0.4.14", "parking_lot 0.11.2", "rand 0.7.3", "serde", @@ -2074,7 +2094,7 @@ dependencies = [ "globset", "jsonrpc-core", "lazy_static", - "log 0.4.11", + "log 0.4.14", "tokio", "tokio-stream", "tokio-util", @@ -2090,7 +2110,7 @@ dependencies = [ "futures 0.3.8", "jsonrpc-core", "jsonrpc-server-utils", - "log 0.4.11", + "log 0.4.14", "parity-ws", "parking_lot 0.11.2", "slab", @@ -2248,25 +2268,25 @@ version = "0.3.9" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e19e8d5c34a3e0e2223db8e060f9e8264aeeb5c5fc64a4ee9965c062211c024b" dependencies = [ - "log 0.4.11", + "log 0.4.14", ] [[package]] name = "log" -version = "0.4.11" +version = "0.4.14" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4fabed175da42fed1fa0746b0ea71f412aa9d35e76e95e59b192c64b9dc2bf8b" +checksum = "51b9bbe6c47d51fc3e1a9b945965946b4c44142ab8792c50835a980d362c2710" dependencies = [ - "cfg-if 0.1.10", + "cfg-if 1.0.0", ] [[package]] name = "lru" -version = "0.6.1" +version = "0.6.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "be716eb6878ca2263eb5d00a781aa13264a794f519fe6af4fbb2668b2d5441c0" +checksum = "7ea2d928b485416e8908cff2d97d621db22b27f7b3b6729e438bcf42c671ba91" dependencies = [ - "hashbrown 0.9.1", + "hashbrown 0.11.2", ] [[package]] @@ -2350,7 +2370,7 @@ dependencies = [ "iovec", "kernel32-sys", "libc", - "log 0.4.11", + "log 0.4.14", "miow 0.2.2", "net2", "slab", @@ -2364,7 +2384,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f33bc887064ef1fd66020c9adfc45bb9f33d75a42096c81e7c56c65b75dd1a8b" dependencies = [ "libc", - "log 0.4.11", + "log 0.4.14", "miow 0.3.6", "ntapi", "winapi 0.3.9", @@ -2377,7 +2397,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "52403fe290012ce777c4626790c8951324a2b9e3316b3143779c72b029742f19" dependencies = [ "lazycell", - "log 0.4.11", + "log 0.4.14", "mio 0.6.22", "slab", ] @@ -2418,7 +2438,7 @@ checksum = "b8d96b2e1c8da3957d58100b09f102c6d9cfdfced01b7ec5a8974044bb09dbd4" dependencies = [ "lazy_static", "libc", - "log 0.4.11", + "log 0.4.14", "openssl", "openssl-probe", "openssl-sys", @@ -2663,7 +2683,7 @@ checksum = "9981e32fb75e004cc148f5fb70342f393830e0a4aa62e3cc93b50976218d42b6" dependencies = [ "futures 0.3.8", "libc", - "log 0.4.11", + "log 0.4.14", "rand 0.7.3", "tokio", "winapi 0.3.9", @@ -2678,7 +2698,7 @@ dependencies = [ "byteorder", "bytes 0.4.12", "httparse", - "log 0.4.11", + "log 0.4.14", "mio 0.6.22", "mio-extras", "rand 0.7.3", @@ -3035,7 +3055,7 @@ dependencies = [ "bytes 1.0.1", "heck", "itertools 0.10.1", - "log 0.4.11", + "log 0.4.14", "multimap", "petgraph", "prost", @@ -3459,7 +3479,7 @@ dependencies = [ "ipnet", "js-sys", "lazy_static", - "log 0.4.11", + "log 0.4.14", "mime 0.3.16", "native-tls", "percent-encoding 2.1.0", @@ -3560,7 +3580,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "064fd21ff87c6e87ed4506e68beb42459caa4a0e2eb144932e6776768556980b" dependencies = [ "base64 0.13.0", - "log 0.4.11", + "log 0.4.14", "ring", "sct", "webpki", @@ -3926,7 +3946,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4370044f8b20f944e05c35d77edd3518e6f21fc4de77e593919f287c6a3f428a" dependencies = [ "base64 0.13.0", - "log 0.4.11", + "log 0.4.14", "openssl", "serde", "serde_derive", @@ -3967,7 +3987,7 @@ dependencies = [ "bytes 1.0.1", "futures 0.3.8", "httparse", - "log 0.4.11", + "log 0.4.14", "rand 0.8.3", "sha-1 0.9.7", ] @@ -3979,7 +3999,7 @@ dependencies = [ "Inflector", "base64 0.12.3", "bincode", - "bs58", + "bs58 0.3.1", "bv", "lazy_static", "serde", @@ -3999,7 +4019,7 @@ version = "1.7.15" dependencies = [ "clap", "crossbeam-channel 0.4.4", - "log 0.4.11", + "log 0.4.14", "rand 0.7.3", "rayon", "solana-logger 1.7.15", @@ -4014,7 +4034,7 @@ name = "solana-accounts-cluster-bench" version = "1.7.15" dependencies = [ "clap", - "log 0.4.11", + "log 0.4.14", "rand 0.7.3", "rayon", "solana-account-decoder", @@ -4041,7 +4061,7 @@ version = "1.7.15" dependencies = [ "clap", "crossbeam-channel 0.4.4", - "log 0.4.11", + "log 0.4.14", "rand 0.7.3", "rayon", "solana-clap-utils", @@ -4094,7 +4114,7 @@ version = "1.7.15" dependencies = [ "bincode", "futures 0.3.8", - "log 0.4.11", + "log 0.4.14", "mio 0.7.6", "solana-banks-interface", "solana-metrics", @@ -4112,7 +4132,7 @@ version = "1.7.15" dependencies = [ "clap", "itertools 0.9.0", - "log 0.4.11", + "log 0.4.14", "num-derive", "num-traits", "rand 0.7.3", @@ -4154,7 +4174,7 @@ version = "1.7.15" dependencies = [ "bincode", "clap", - "log 0.4.11", + "log 0.4.14", "rayon", "serde_json", "serde_yaml", @@ -4184,7 +4204,7 @@ dependencies = [ "byteorder", "curve25519-dalek 3.0.0", "libsecp256k1", - "log 0.4.11", + "log 0.4.14", "num-derive", "num-traits", "rand 0.7.3", @@ -4241,7 +4261,7 @@ version = "1.7.15" dependencies = [ "Inflector", "bincode", - "bs58", + "bs58 0.3.1", "chrono", "clap", "console 0.14.1", @@ -4251,7 +4271,7 @@ dependencies = [ "dirs-next", "humantime", "indicatif", - "log 0.4.11", + "log 0.4.14", "num-traits", "pretty-hex", "reqwest", @@ -4325,12 +4345,12 @@ dependencies = [ "assert_matches", "base64 0.13.0", "bincode", - "bs58", + "bs58 0.3.1", "clap", "indicatif", "jsonrpc-core", "jsonrpc-http-server", - "log 0.4.11", + "log 0.4.14", "net2", "rayon", "reqwest", @@ -4353,13 +4373,20 @@ dependencies = [ "url 2.2.0", ] +[[package]] +name = "solana-compute-budget-program" +version = "1.7.15" +dependencies = [ + "solana-sdk", +] + [[package]] name = "solana-config-program" version = "1.7.15" dependencies = [ "bincode", "chrono", - "log 0.4.11", + "log 0.4.14", "rand_core 0.6.2", "serde", "serde_derive", @@ -4375,7 +4402,7 @@ dependencies = [ "base64 0.12.3", "bincode", "blake3", - "bs58", + "bs58 0.3.1", "bv", "byteorder", "chrono", @@ -4383,14 +4410,15 @@ dependencies = [ "ed25519-dalek", "flate2", "fs_extra", + "histogram", "indexmap", - "itertools 0.9.0", + "itertools 0.10.1", "jsonrpc-core", "jsonrpc-core-client", "jsonrpc-derive", "jsonrpc-pubsub", "libc", - "log 0.4.11", + "log 0.4.14", "lru", "matches", "miow 0.2.2", @@ -4472,7 +4500,7 @@ version = "1.7.15" dependencies = [ "bincode", "clap", - "log 0.4.11", + "log 0.4.14", "rand 0.7.3", "rayon", "solana-clap-utils", @@ -4496,7 +4524,7 @@ dependencies = [ "bzip2", "console 0.14.1", "indicatif", - "log 0.4.11", + "log 0.4.14", "reqwest", "solana-runtime", "solana-sdk", @@ -4508,7 +4536,7 @@ name = "solana-exchange-program" version = "1.7.15" dependencies = [ "bincode", - "log 0.4.11", + "log 0.4.14", "num-derive", "num-traits", "serde", @@ -4535,7 +4563,7 @@ dependencies = [ "bincode", "byteorder", "clap", - "log 0.4.11", + "log 0.4.14", "serde", "serde_derive", "solana-clap-utils", @@ -4555,10 +4583,10 @@ version = "1.7.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b0b98d31e0662fedf3a1ee30919c655713874d578e19e65affe46109b1b927f9" dependencies = [ - "bs58", + "bs58 0.3.1", "bv", "generic-array 0.14.3", - "log 0.4.11", + "log 0.4.14", "memmap2", "rustc_version", "serde", @@ -4573,10 +4601,10 @@ dependencies = [ name = "solana-frozen-abi" version = "1.7.15" dependencies = [ - "bs58", + "bs58 0.3.1", "bv", "generic-array 0.14.3", - "log 0.4.11", + "log 0.4.14", "memmap2", "rustc_version", "serde", @@ -4651,7 +4679,7 @@ dependencies = [ "flate2", "indexmap", "itertools 0.9.0", - "log 0.4.11", + "log 0.4.14", "lru", "matches", "num-traits", @@ -4720,7 +4748,7 @@ dependencies = [ name = "solana-keygen" version = "1.7.15" dependencies = [ - "bs58", + "bs58 0.3.1", "clap", "dirs-next", "num_cpus", @@ -4751,7 +4779,7 @@ dependencies = [ "itertools 0.9.0", "lazy_static", "libc", - "log 0.4.11", + "log 0.4.14", "matches", "num_cpus", "prost", @@ -4792,7 +4820,7 @@ name = "solana-ledger-tool" version = "1.7.15" dependencies = [ "assert_cmd", - "bs58", + "bs58 0.3.1", "bytecount", "clap", "csv", @@ -4801,7 +4829,7 @@ dependencies = [ "futures-util", "histogram", "itertools 0.9.0", - "log 0.4.11", + "log 0.4.14", "regex", "serde", "serde_json", @@ -4809,6 +4837,7 @@ dependencies = [ "signal-hook", "solana-clap-utils", "solana-cli-output", + "solana-core", "solana-ledger", "solana-logger 1.7.15", "solana-measure", @@ -4832,7 +4861,7 @@ dependencies = [ "fs_extra", "gag", "itertools 0.9.0", - "log 0.4.11", + "log 0.4.14", "rand 0.7.3", "rayon", "serial_test", @@ -4876,7 +4905,7 @@ checksum = "ec7c514fe57f8c5042fa88c19f5711c67f264db723d9d79379fcb78dd1f09bbf" dependencies = [ "env_logger", "lazy_static", - "log 0.4.11", + "log 0.4.14", ] [[package]] @@ -4885,14 +4914,14 @@ version = "1.7.15" dependencies = [ "env_logger", "lazy_static", - "log 0.4.11", + "log 0.4.14", ] [[package]] name = "solana-measure" version = "1.7.15" dependencies = [ - "log 0.4.11", + "log 0.4.14", "solana-metrics", "solana-sdk", ] @@ -4902,7 +4931,7 @@ name = "solana-merkle-root-bench" version = "1.7.15" dependencies = [ "clap", - "log 0.4.11", + "log 0.4.14", "solana-logger 1.7.15", "solana-measure", "solana-runtime", @@ -4927,7 +4956,7 @@ dependencies = [ "env_logger", "gethostname", "lazy_static", - "log 0.4.11", + "log 0.4.14", "rand 0.7.3", "reqwest", "serial_test", @@ -4952,7 +4981,7 @@ version = "1.7.15" dependencies = [ "bincode", "clap", - "log 0.4.11", + "log 0.4.14", "nix 0.19.0", "rand 0.7.3", "serde", @@ -4970,7 +4999,7 @@ dependencies = [ name = "solana-noop-program" version = "1.7.15" dependencies = [ - "log 0.4.11", + "log 0.4.14", "solana-logger 1.7.15", "solana-sdk", ] @@ -4979,7 +5008,7 @@ dependencies = [ name = "solana-notifier" version = "1.7.15" dependencies = [ - "log 0.4.11", + "log 0.4.14", "reqwest", "serde_json", ] @@ -5005,7 +5034,7 @@ dependencies = [ "dlopen", "dlopen_derive", "lazy_static", - "log 0.4.11", + "log 0.4.14", "matches", "rand 0.7.3", "rayon", @@ -5024,7 +5053,7 @@ dependencies = [ "bincode", "core_affinity", "crossbeam-channel 0.4.4", - "log 0.4.11", + "log 0.4.14", "matches", "rand 0.7.3", "solana-ledger", @@ -5043,7 +5072,7 @@ name = "solana-poh-bench" version = "1.7.15" dependencies = [ "clap", - "log 0.4.11", + "log 0.4.14", "rand 0.7.3", "rayon", "solana-clap-utils", @@ -5065,14 +5094,14 @@ dependencies = [ "blake3", "borsh", "borsh-derive", - "bs58", + "bs58 0.3.1", "bv", "curve25519-dalek 2.1.0", "hex", "itertools 0.9.0", "lazy_static", "libsecp256k1", - "log 0.4.11", + "log 0.4.14", "num-derive", "num-traits", "rand 0.7.3", @@ -5099,14 +5128,14 @@ dependencies = [ "blake3", "borsh", "borsh-derive", - "bs58", + "bs58 0.3.1", "bv", "curve25519-dalek 2.1.0", "hex", "itertools 0.9.0", "lazy_static", "libsecp256k1", - "log 0.4.11", + "log 0.4.14", "num-derive", "num-traits", "rand 0.7.3", @@ -5136,7 +5165,7 @@ dependencies = [ "bincode", "chrono", "chrono-humanize", - "log 0.4.11", + "log 0.4.14", "mio 0.7.6", "serde", "serde_derive", @@ -5167,7 +5196,7 @@ dependencies = [ "console 0.14.1", "dialoguer", "hidapi", - "log 0.4.11", + "log 0.4.14", "num-derive", "num-traits", "parking_lot 0.10.2", @@ -5184,7 +5213,7 @@ version = "1.7.15" dependencies = [ "base64 0.12.3", "bincode", - "bs58", + "bs58 0.3.1", "crossbeam-channel 0.4.4", "dashmap", "itertools 0.9.0", @@ -5195,7 +5224,7 @@ dependencies = [ "jsonrpc-pubsub", "jsonrpc-ws-server", "libc", - "log 0.4.11", + "log 0.4.14", "regex", "serde", "serde_derive", @@ -5249,7 +5278,7 @@ dependencies = [ "lazy_static", "libc", "libloading 0.6.2", - "log 0.4.11", + "log 0.4.14", "memmap2", "num-derive", "num-traits", @@ -5261,6 +5290,7 @@ dependencies = [ "rustc_version", "serde", "serde_derive", + "solana-compute-budget-program", "solana-config-program", "solana-frozen-abi 1.7.15", "solana-frozen-abi-macro 1.7.15", @@ -5294,7 +5324,9 @@ version = "1.7.15" dependencies = [ "assert_matches", "bincode", - "bs58", + "borsh", + "borsh-derive", + "bs58 0.4.0", "bv", "byteorder", "chrono", @@ -5309,7 +5341,7 @@ dependencies = [ "itertools 0.9.0", "lazy_static", "libsecp256k1", - "log 0.4.11", + "log 0.4.14", "memmap2", "num-derive", "num-traits", @@ -5343,7 +5375,7 @@ version = "1.7.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "84710ce45a21cccd9f2b09d8e9aad529080bb2540f27b1253874b6e732b465b9" dependencies = [ - "bs58", + "bs58 0.3.1", "proc-macro2 1.0.24", "quote 1.0.9", "rustversion", @@ -5354,7 +5386,7 @@ dependencies = [ name = "solana-sdk-macro" version = "1.7.15" dependencies = [ - "bs58", + "bs58 0.3.1", "proc-macro2 1.0.24", "quote 1.0.9", "rustversion", @@ -5391,7 +5423,7 @@ name = "solana-stake-program" version = "1.7.15" dependencies = [ "bincode", - "log 0.4.11", + "log 0.4.14", "num-derive", "num-traits", "proptest", @@ -5420,7 +5452,7 @@ dependencies = [ "flate2", "futures 0.3.8", "goauth", - "log 0.4.11", + "log 0.4.14", "prost", "prost-types", "rand_core 0.6.2", @@ -5440,7 +5472,7 @@ name = "solana-storage-proto" version = "1.7.15" dependencies = [ "bincode", - "bs58", + "bs58 0.3.1", "prost", "serde", "serde_derive", @@ -5455,7 +5487,7 @@ name = "solana-store-tool" version = "1.7.15" dependencies = [ "clap", - "log 0.4.11", + "log 0.4.14", "solana-logger 1.7.15", "solana-measure", "solana-runtime", @@ -5469,7 +5501,7 @@ version = "1.7.15" dependencies = [ "itertools 0.10.1", "libc", - "log 0.4.11", + "log 0.4.14", "nix 0.19.0", "solana-logger 1.7.15", "solana-measure", @@ -5485,7 +5517,7 @@ version = "1.7.15" dependencies = [ "clap", "libc", - "log 0.4.11", + "log 0.4.14", "nix 0.19.0", "solana-clap-utils", "solana-logger 1.7.15", @@ -5536,7 +5568,7 @@ dependencies = [ "Inflector", "base64 0.12.3", "bincode", - "bs58", + "bs58 0.3.1", "lazy_static", "serde", "serde_derive", @@ -5577,7 +5609,7 @@ dependencies = [ "jsonrpc-ipc-server", "jsonrpc-server-utils", "libc", - "log 0.4.11", + "log 0.4.14", "num_cpus", "rand 0.7.3", "serde", @@ -5610,7 +5642,7 @@ dependencies = [ name = "solana-version" version = "1.7.15" dependencies = [ - "log 0.4.11", + "log 0.4.14", "rustc_version", "serde", "serde_derive", @@ -5625,7 +5657,7 @@ name = "solana-vote-program" version = "1.7.15" dependencies = [ "bincode", - "log 0.4.11", + "log 0.4.14", "num-derive", "num-traits", "rustc_version", @@ -5645,7 +5677,7 @@ version = "1.7.15" dependencies = [ "clap", "humantime", - "log 0.4.11", + "log 0.4.14", "solana-clap-utils", "solana-cli-config", "solana-cli-output", @@ -5668,7 +5700,7 @@ dependencies = [ "goblin", "hash32", "libc", - "log 0.4.11", + "log 0.4.14", "rand 0.7.3", "rustc-demangle", "scroll", @@ -5897,7 +5929,7 @@ dependencies = [ "fnv", "futures 0.3.8", "humantime", - "log 0.4.11", + "log 0.4.14", "pin-project 1.0.1", "rand 0.7.3", "serde", @@ -6148,7 +6180,7 @@ checksum = "57fc868aae093479e3131e3d165c93b1c7474109d13c90ec0dda2a1bbfff0674" dependencies = [ "bytes 0.4.12", "futures 0.1.29", - "log 0.4.11", + "log 0.4.14", ] [[package]] @@ -6191,7 +6223,7 @@ dependencies = [ "crossbeam-utils 0.7.2", "futures 0.1.29", "lazy_static", - "log 0.4.11", + "log 0.4.14", "mio 0.6.22", "num_cpus", "parking_lot 0.9.0", @@ -6284,7 +6316,7 @@ dependencies = [ "futures-core", "futures-io", "futures-sink", - "log 0.4.11", + "log 0.4.14", "pin-project-lite", "tokio", ] @@ -6381,7 +6413,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "09adeb8c97449311ccd28a427f96fb563e7fd31aabf994189879d9da2394b89d" dependencies = [ "cfg-if 1.0.0", - "log 0.4.11", + "log 0.4.14", "pin-project-lite", "tracing-attributes", "tracing-core", @@ -6456,7 +6488,7 @@ dependencies = [ "http", "httparse", "input_buffer", - "log 0.4.11", + "log 0.4.14", "native-tls", "rand 0.7.3", "sha-1 0.8.2", @@ -6606,7 +6638,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "aa4227e95324a443c9fcb06e03d4d85e91aabe9a5a02aa818688b6918b6af486" dependencies = [ "libc", - "log 0.4.11", + "log 0.4.14", ] [[package]] @@ -6677,7 +6709,7 @@ version = "0.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1ce8a968cb1cd110d136ff8b819a556d6fb6d919363c61534f6860c7eb172ba0" dependencies = [ - "log 0.4.11", + "log 0.4.14", "try-lock", ] @@ -6707,7 +6739,7 @@ checksum = "7bc45447f0d4573f3d65720f636bbcc3dd6ce920ed704670118650bcd47764c7" dependencies = [ "bumpalo", "lazy_static", - "log 0.4.11", + "log 0.4.14", "proc-macro2 1.0.24", "quote 1.0.9", "syn 1.0.60", diff --git a/Cargo.toml b/Cargo.toml index 4d3e377cbbb9f8..e92130987c4eca 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -43,6 +43,7 @@ members = [ "poh-bench", "program-test", "programs/bpf_loader", + "programs/compute-budget", "programs/config", "programs/exchange", "programs/failure", diff --git a/banking-bench/src/main.rs b/banking-bench/src/main.rs index 614661f713c432..77616f41b2bddc 100644 --- a/banking-bench/src/main.rs +++ b/banking-bench/src/main.rs @@ -4,7 +4,7 @@ use crossbeam_channel::unbounded; use log::*; use rand::{thread_rng, Rng}; use rayon::prelude::*; -use solana_core::banking_stage::BankingStage; +use solana_core::{banking_stage::BankingStage, cost_model::CostModel, cost_tracker::CostTracker}; use solana_gossip::{cluster_info::ClusterInfo, cluster_info::Node}; use solana_ledger::{ blockstore::Blockstore, @@ -27,7 +27,7 @@ use solana_sdk::{ }; use solana_streamer::socket::SocketAddrSpace; use std::{ - sync::{atomic::Ordering, mpsc::Receiver, Arc, Mutex}, + sync::{atomic::Ordering, mpsc::Receiver, Arc, Mutex, RwLock}, thread::sleep, time::{Duration, Instant}, }; @@ -231,6 +231,9 @@ fn main() { vote_receiver, None, replay_vote_sender, + Arc::new(RwLock::new(CostTracker::new(Arc::new(RwLock::new( + CostModel::default(), + ))))), ); poh_recorder.lock().unwrap().set_bank(&bank); diff --git a/ci/do-audit.sh b/ci/do-audit.sh index 405e684707853b..71a0465d6fbf69 100755 --- a/ci/do-audit.sh +++ b/ci/do-audit.sh @@ -45,5 +45,6 @@ cargo_audit_ignores=( # Blocked on jsonrpc removing dependency on unmaintained `websocket` # https://github.com/paritytech/jsonrpc/issues/605 --ignore RUSTSEC-2021-0079 + ) scripts/cargo-for-all-lock-files.sh stable audit "${cargo_audit_ignores[@]}" diff --git a/core/Cargo.toml b/core/Cargo.toml index ba031176e44b94..d285f2025c59ab 100644 --- a/core/Cargo.toml +++ b/core/Cargo.toml @@ -27,13 +27,14 @@ ed25519-dalek = "=1.0.1" fs_extra = "1.2.0" flate2 = "1.0" indexmap = { version = "1.5", features = ["rayon"] } -itertools = "0.9.0" libc = "0.2.81" -log = "0.4.11" -lru = "0.6.1" miow = "0.2.2" net2 = "0.2.37" num-traits = "0.2" +histogram = "0.6.9" +itertools = "0.10.1" +log = "0.4.14" +lru = "0.6.6" rand = "0.7.0" rand_chacha = "0.2.2" rand_core = "0.6.2" diff --git a/core/benches/banking_stage.rs b/core/benches/banking_stage.rs index 99b8c1b8d0c69b..5248c6a12f42ab 100644 --- a/core/benches/banking_stage.rs +++ b/core/benches/banking_stage.rs @@ -8,6 +8,9 @@ use log::*; use rand::{thread_rng, Rng}; use rayon::prelude::*; use solana_core::banking_stage::{BankingStage, BankingStageStats}; +use solana_core::cost_model::CostModel; +use solana_core::cost_tracker::CostTracker; +use solana_core::cost_tracker_stats::CostTrackerStats; use solana_gossip::cluster_info::ClusterInfo; use solana_gossip::cluster_info::Node; use solana_ledger::blockstore_processor::process_entries; @@ -33,7 +36,7 @@ use solana_streamer::socket::SocketAddrSpace; use std::collections::VecDeque; use std::sync::atomic::Ordering; use std::sync::mpsc::Receiver; -use std::sync::Arc; +use std::sync::{Arc, RwLock}; use std::time::{Duration, Instant}; use test::Bencher; @@ -92,6 +95,10 @@ fn bench_consume_buffered(bencher: &mut Bencher) { None::>, &BankingStageStats::default(), &recorder, + &Arc::new(RwLock::new(CostTracker::new(Arc::new(RwLock::new( + CostModel::new(std::u64::MAX, std::u64::MAX), + ))))), + &mut CostTrackerStats::default(), ); }); @@ -218,6 +225,9 @@ fn bench_banking(bencher: &mut Bencher, tx_type: TransactionType) { vote_receiver, None, s, + Arc::new(RwLock::new(CostTracker::new(Arc::new(RwLock::new( + CostModel::new(std::u64::MAX, std::u64::MAX), + ))))), ); poh_recorder.lock().unwrap().set_bank(&bank); diff --git a/core/src/banking_stage.rs b/core/src/banking_stage.rs index 228bd19ee141e3..5373e567847e4a 100644 --- a/core/src/banking_stage.rs +++ b/core/src/banking_stage.rs @@ -1,7 +1,9 @@ //! The `banking_stage` processes Transaction messages. It is intended to be used //! to contruct a software pipeline. The stage uses all available CPU cores and //! can do its processing in parallel with signature verification on the GPU. -use crate::packet_hasher::PacketHasher; +use crate::{ + cost_tracker::CostTracker, cost_tracker_stats::CostTrackerStats, packet_hasher::PacketHasher, +}; use crossbeam_channel::{Receiver as CrossbeamReceiver, RecvTimeoutError}; use itertools::Itertools; use lru::LruCache; @@ -52,7 +54,7 @@ use std::{ net::{SocketAddr, UdpSocket}, ops::DerefMut, sync::atomic::{AtomicU64, AtomicUsize, Ordering}, - sync::{Arc, Mutex}, + sync::{Arc, Mutex, RwLock}, thread::{self, Builder, JoinHandle}, time::Duration, time::Instant, @@ -93,6 +95,9 @@ pub struct BankingStageStats { current_buffered_packet_batches_count: AtomicUsize, rebuffered_packets_count: AtomicUsize, consumed_buffered_packets_count: AtomicUsize, + reset_cost_tracker_count: AtomicUsize, + cost_tracker_check_count: AtomicUsize, + cost_forced_retry_transactions_count: AtomicUsize, // Timing consume_buffered_packets_elapsed: AtomicU64, @@ -101,7 +106,11 @@ pub struct BankingStageStats { filter_pending_packets_elapsed: AtomicU64, packet_duplicate_check_elapsed: AtomicU64, packet_conversion_elapsed: AtomicU64, + unprocessed_packet_conversion_elapsed: AtomicU64, transaction_processing_elapsed: AtomicU64, + cost_tracker_update_elapsed: AtomicU64, + cost_tracker_clone_elapsed: AtomicU64, + cost_tracker_check_elapsed: AtomicU64, } impl BankingStageStats { @@ -165,6 +174,22 @@ impl BankingStageStats { .swap(0, Ordering::Relaxed) as i64, i64 ), + ( + "reset_cost_tracker_count", + self.reset_cost_tracker_count.swap(0, Ordering::Relaxed) as i64, + i64 + ), + ( + "cost_tracker_check_count", + self.cost_tracker_check_count.swap(0, Ordering::Relaxed) as i64, + i64 + ), + ( + "cost_forced_retry_transactions_count", + self.cost_forced_retry_transactions_count + .swap(0, Ordering::Relaxed) as i64, + i64 + ), ( "consume_buffered_packets_elapsed", self.consume_buffered_packets_elapsed @@ -199,12 +224,33 @@ impl BankingStageStats { self.packet_conversion_elapsed.swap(0, Ordering::Relaxed) as i64, i64 ), + ( + "unprocessed_packet_conversion_elapsed", + self.unprocessed_packet_conversion_elapsed + .swap(0, Ordering::Relaxed) as i64, + i64 + ), ( "transaction_processing_elapsed", self.transaction_processing_elapsed .swap(0, Ordering::Relaxed) as i64, i64 ), + ( + "cost_tracker_update_elapsed", + self.cost_tracker_update_elapsed.swap(0, Ordering::Relaxed) as i64, + i64 + ), + ( + "cost_tracker_clone_elapsed", + self.cost_tracker_clone_elapsed.swap(0, Ordering::Relaxed) as i64, + i64 + ), + ( + "cost_tracker_check_elapsed", + self.cost_tracker_check_elapsed.swap(0, Ordering::Relaxed) as i64, + i64 + ), ); } } @@ -241,6 +287,7 @@ impl BankingStage { verified_vote_receiver: CrossbeamReceiver>, transaction_status_sender: Option, gossip_vote_sender: ReplayVoteSender, + cost_tracker: Arc>, ) -> Self { Self::new_num_threads( cluster_info, @@ -251,6 +298,7 @@ impl BankingStage { Self::num_threads(), transaction_status_sender, gossip_vote_sender, + cost_tracker, ) } @@ -263,6 +311,7 @@ impl BankingStage { num_threads: u32, transaction_status_sender: Option, gossip_vote_sender: ReplayVoteSender, + cost_tracker: Arc>, ) -> Self { let batch_limit = TOTAL_BUFFERED_PACKETS / ((num_threads - 1) as usize * PACKETS_PER_BATCH); // Single thread to generate entries from many banks. @@ -298,6 +347,7 @@ impl BankingStage { let gossip_vote_sender = gossip_vote_sender.clone(); let duplicates = duplicates.clone(); let data_budget = data_budget.clone(); + let cost_tracker = cost_tracker.clone(); Builder::new() .name("solana-banking-stage-tx".to_string()) .spawn(move || { @@ -314,6 +364,7 @@ impl BankingStage { gossip_vote_sender, &duplicates, &data_budget, + &cost_tracker, ); }) .unwrap() @@ -371,6 +422,25 @@ impl BankingStage { has_more_unprocessed_transactions } + fn reset_cost_tracker_if_new_bank( + cost_tracker: &Arc>, + bank: Arc, + banking_stage_stats: &BankingStageStats, + cost_tracker_stats: &mut CostTrackerStats, + ) { + if cost_tracker + .write() + .unwrap() + .reset_if_new_bank(bank.slot(), cost_tracker_stats) + { + // only increase counter when bank changed + banking_stage_stats + .reset_cost_tracker_count + .fetch_add(1, Ordering::Relaxed); + } + } + + #[allow(clippy::too_many_arguments)] pub fn consume_buffered_packets( my_pubkey: &Pubkey, max_tx_ingestion_ns: u128, @@ -381,6 +451,8 @@ impl BankingStage { test_fn: Option, banking_stage_stats: &BankingStageStats, recorder: &TransactionRecorder, + cost_tracker: &Arc>, + cost_tracker_stats: &mut CostTrackerStats, ) { let mut rebuffered_packets_len = 0; let mut new_tx_count = 0; @@ -398,6 +470,9 @@ impl BankingStage { original_unprocessed_indexes, my_pubkey, *next_leader, + cost_tracker, + banking_stage_stats, + cost_tracker_stats, ); Self::update_buffered_packets_with_new_unprocessed( original_unprocessed_indexes, @@ -406,6 +481,12 @@ impl BankingStage { } else { let bank_start = poh_recorder.lock().unwrap().bank_start(); if let Some((bank, bank_creation_time)) = bank_start { + Self::reset_cost_tracker_if_new_bank( + cost_tracker, + bank.clone(), + banking_stage_stats, + cost_tracker_stats, + ); let (processed, verified_txs_len, new_unprocessed_indexes) = Self::process_packets_transactions( &bank, @@ -416,6 +497,8 @@ impl BankingStage { transaction_status_sender.clone(), gossip_vote_sender, banking_stage_stats, + cost_tracker, + cost_tracker_stats, ); if processed < verified_txs_len || !Bank::should_bank_still_be_processing_txs( @@ -519,6 +602,8 @@ impl BankingStage { banking_stage_stats: &BankingStageStats, recorder: &TransactionRecorder, data_budget: &DataBudget, + cost_tracker: &Arc>, + cost_tracker_stats: &mut CostTrackerStats, ) -> BufferedPacketsDecision { let bank_start; let ( @@ -529,6 +614,14 @@ impl BankingStage { ) = { let poh = poh_recorder.lock().unwrap(); bank_start = poh.bank_start(); + if let Some((ref bank, _)) = bank_start { + Self::reset_cost_tracker_if_new_bank( + cost_tracker, + bank.clone(), + banking_stage_stats, + cost_tracker_stats, + ); + }; ( poh.leader_after_n_slots(FORWARD_TRANSACTIONS_TO_LEADER_AT_SLOT_OFFSET), PohRecorder::get_bank_still_processing_txs(&bank_start), @@ -559,6 +652,8 @@ impl BankingStage { None::>, banking_stage_stats, recorder, + cost_tracker, + cost_tracker_stats, ); } BufferedPacketsDecision::Forward => { @@ -638,11 +733,13 @@ impl BankingStage { gossip_vote_sender: ReplayVoteSender, duplicates: &Arc, PacketHasher)>>, data_budget: &DataBudget, + cost_tracker: &Arc>, ) { let recorder = poh_recorder.lock().unwrap().recorder(); let socket = UdpSocket::bind("0.0.0.0:0").unwrap(); let mut buffered_packets = VecDeque::with_capacity(batch_limit); let banking_stage_stats = BankingStageStats::new(id); + let mut cost_tracker_stats = CostTrackerStats::new(id, 0); loop { while !buffered_packets.is_empty() { let decision = Self::process_buffered_packets( @@ -657,6 +754,8 @@ impl BankingStage { &banking_stage_stats, &recorder, data_budget, + cost_tracker, + &mut cost_tracker_stats, ); if matches!(decision, BufferedPacketsDecision::Hold) || matches!(decision, BufferedPacketsDecision::ForwardAndHold) @@ -691,6 +790,8 @@ impl BankingStage { &banking_stage_stats, duplicates, &recorder, + cost_tracker, + &mut cost_tracker_stats, ) { Ok(()) | Err(RecvTimeoutError::Timeout) => (), Err(RecvTimeoutError::Disconnected) => break, @@ -935,12 +1036,12 @@ impl BankingStage { ) -> (usize, Vec) { let mut chunk_start = 0; let mut unprocessed_txs = vec![]; + while chunk_start != transactions.len() { let chunk_end = std::cmp::min( transactions.len(), chunk_start + MAX_NUM_TRANSACTIONS_PER_BATCH, ); - let (result, retryable_txs_in_chunk) = Self::process_and_record_transactions( bank, &transactions[chunk_start..chunk_end], @@ -1023,13 +1124,21 @@ impl BankingStage { // This function deserializes packets into transactions, computes the blake3 hash of transaction messages, // and verifies secp256k1 instructions. A list of valid transactions are returned with their message hashes // and packet indexes. + // Also returned is packet indexes for transaction should be retried due to cost limits. + #[allow(clippy::needless_collect)] fn transactions_from_packets( msgs: &Packets, transaction_indexes: &[usize], libsecp256k1_0_5_upgrade_enabled: bool, votes_only: bool, - ) -> (Vec>, Vec) { - transaction_indexes + cost_tracker: &Arc>, + banking_stage_stats: &BankingStageStats, + demote_program_write_locks: bool, + cost_tracker_stats: &mut CostTrackerStats, + ) -> (Vec>, Vec, Vec) { + let mut retryable_transaction_packet_indexes: Vec = vec![]; + + let verified_transactions_with_packet_indexes: Vec<_> = transaction_indexes .iter() .filter_map(|tx_index| { let p = &msgs.packets[*tx_index]; @@ -1040,14 +1149,68 @@ impl BankingStage { let tx: Transaction = limited_deserialize(&p.data[0..p.meta.size]).ok()?; tx.verify_precompiles(libsecp256k1_0_5_upgrade_enabled) .ok()?; - let message_bytes = Self::packet_message(p)?; - let message_hash = Message::hash_raw_message(message_bytes); - Some(( - HashedTransaction::new(Cow::Owned(tx), message_hash), - tx_index, - )) + + Some((tx, *tx_index)) }) - .unzip() + .collect(); + banking_stage_stats.cost_tracker_check_count.fetch_add( + verified_transactions_with_packet_indexes.len(), + Ordering::Relaxed, + ); + + let mut cost_tracker_check_time = Measure::start("cost_tracker_check_time"); + let filtered_transactions_with_packet_indexes: Vec<_> = { + let cost_tracker_readonly = cost_tracker.read().unwrap(); + verified_transactions_with_packet_indexes + .into_iter() + .filter_map(|(tx, tx_index)| { + // put transaction into retry queue if it wouldn't fit + // into current bank + let is_vote = &msgs.packets[tx_index].meta.is_simple_vote_tx; + + // excluding vote TX from cost_model, for now + if !is_vote + && cost_tracker_readonly + .would_transaction_fit( + &tx, + demote_program_write_locks, + cost_tracker_stats, + ) + .is_err() + { + debug!("transaction {:?} would exceed limit", tx); + retryable_transaction_packet_indexes.push(tx_index); + return None; + } + Some((tx, tx_index)) + }) + .collect() + }; + cost_tracker_check_time.stop(); + + let (filtered_transactions, filter_transaction_packet_indexes) = + filtered_transactions_with_packet_indexes + .into_iter() + .filter_map(|(tx, tx_index)| { + let p = &msgs.packets[tx_index]; + let message_bytes = Self::packet_message(p)?; + let message_hash = Message::hash_raw_message(message_bytes); + Some(( + HashedTransaction::new(Cow::Owned(tx), message_hash), + tx_index, + )) + }) + .unzip(); + + banking_stage_stats + .cost_tracker_check_elapsed + .fetch_add(cost_tracker_check_time.as_us(), Ordering::Relaxed); + + ( + filtered_transactions, + filter_transaction_packet_indexes, + retryable_transaction_packet_indexes, + ) } /// This function filters pending packets that are still valid @@ -1089,6 +1252,7 @@ impl BankingStage { Self::filter_valid_transaction_indexes(&results, transaction_to_packet_indexes) } + #[allow(clippy::too_many_arguments)] fn process_packets_transactions( bank: &Arc, bank_creation_time: &Instant, @@ -1098,20 +1262,32 @@ impl BankingStage { transaction_status_sender: Option, gossip_vote_sender: &ReplayVoteSender, banking_stage_stats: &BankingStageStats, + cost_tracker: &Arc>, + cost_tracker_stats: &mut CostTrackerStats, ) -> (usize, usize, Vec) { let mut packet_conversion_time = Measure::start("packet_conversion"); - let (transactions, transaction_to_packet_indexes) = Self::transactions_from_packets( - msgs, - &packet_indexes, - bank.libsecp256k1_0_5_upgrade_enabled(), - bank.vote_only_bank(), - ); + let (transactions, transaction_to_packet_indexes, retryable_packet_indexes) = + Self::transactions_from_packets( + msgs, + &packet_indexes, + bank.libsecp256k1_0_5_upgrade_enabled(), + bank.vote_only_bank(), + cost_tracker, + banking_stage_stats, + bank.demote_program_write_locks(), + cost_tracker_stats, + ); packet_conversion_time.stop(); + inc_new_counter_info!("banking_stage-packet_conversion", 1); + banking_stage_stats + .cost_forced_retry_transactions_count + .fetch_add(retryable_packet_indexes.len(), Ordering::Relaxed); debug!( - "bank: {} filtered transactions {}", + "bank: {} filtered transactions {} cost limited transactions {}", bank.slot(), - transactions.len() + transactions.len(), + retryable_packet_indexes.len() ); let tx_len = transactions.len(); @@ -1126,11 +1302,27 @@ impl BankingStage { gossip_vote_sender, ); process_tx_time.stop(); - let unprocessed_tx_count = unprocessed_tx_indexes.len(); + inc_new_counter_info!( + "banking_stage-unprocessed_transactions", + unprocessed_tx_count + ); + + // applying cost of processed transactions to shared cost_tracker + let mut cost_tracking_time = Measure::start("cost_tracking_time"); + transactions.iter().enumerate().for_each(|(index, tx)| { + if unprocessed_tx_indexes.iter().all(|&i| i != index) { + cost_tracker.write().unwrap().add_transaction_cost( + tx.transaction(), + bank.demote_program_write_locks(), + cost_tracker_stats, + ); + } + }); + cost_tracking_time.stop(); let mut filter_pending_packets_time = Measure::start("filter_pending_packets_time"); - let filtered_unprocessed_packet_indexes = Self::filter_pending_packets_from_pending_txs( + let mut filtered_unprocessed_packet_indexes = Self::filter_pending_packets_from_pending_txs( bank, &transactions, &transaction_to_packet_indexes, @@ -1143,12 +1335,19 @@ impl BankingStage { unprocessed_tx_count.saturating_sub(filtered_unprocessed_packet_indexes.len()) ); + // combine cost-related unprocessed transactions with bank determined unprocessed for + // buffering + filtered_unprocessed_packet_indexes.extend(retryable_packet_indexes); + banking_stage_stats .packet_conversion_elapsed .fetch_add(packet_conversion_time.as_us(), Ordering::Relaxed); banking_stage_stats .transaction_processing_elapsed .fetch_add(process_tx_time.as_us(), Ordering::Relaxed); + banking_stage_stats + .cost_tracker_update_elapsed + .fetch_add(cost_tracking_time.as_us(), Ordering::Relaxed); banking_stage_stats .filter_pending_packets_elapsed .fetch_add(filter_pending_packets_time.as_us(), Ordering::Relaxed); @@ -1162,6 +1361,9 @@ impl BankingStage { transaction_indexes: &[usize], my_pubkey: &Pubkey, next_leader: Option, + cost_tracker: &Arc>, + banking_stage_stats: &BankingStageStats, + cost_tracker_stats: &mut CostTrackerStats, ) -> Vec { // Check if we are the next leader. If so, let's not filter the packets // as we'll filter it again while processing the packets. @@ -1172,27 +1374,43 @@ impl BankingStage { } } - let (transactions, transaction_to_packet_indexes) = Self::transactions_from_packets( - msgs, - &transaction_indexes, - bank.libsecp256k1_0_5_upgrade_enabled(), - bank.vote_only_bank(), - ); + let mut unprocessed_packet_conversion_time = + Measure::start("unprocessed_packet_conversion"); + let (transactions, transaction_to_packet_indexes, retry_packet_indexes) = + Self::transactions_from_packets( + msgs, + &transaction_indexes, + bank.libsecp256k1_0_5_upgrade_enabled(), + bank.vote_only_bank(), + cost_tracker, + banking_stage_stats, + bank.demote_program_write_locks(), + cost_tracker_stats, + ); + unprocessed_packet_conversion_time.stop(); let tx_count = transaction_to_packet_indexes.len(); let unprocessed_tx_indexes = (0..transactions.len()).collect_vec(); - let filtered_unprocessed_packet_indexes = Self::filter_pending_packets_from_pending_txs( + let mut filtered_unprocessed_packet_indexes = Self::filter_pending_packets_from_pending_txs( bank, &transactions, &transaction_to_packet_indexes, &unprocessed_tx_indexes, ); + filtered_unprocessed_packet_indexes.extend(retry_packet_indexes); + inc_new_counter_info!( "banking_stage-dropped_tx_before_forwarding", tx_count.saturating_sub(filtered_unprocessed_packet_indexes.len()) ); + banking_stage_stats + .unprocessed_packet_conversion_elapsed + .fetch_add( + unprocessed_packet_conversion_time.as_us(), + Ordering::Relaxed, + ); filtered_unprocessed_packet_indexes } @@ -1228,6 +1446,8 @@ impl BankingStage { banking_stage_stats: &BankingStageStats, duplicates: &Arc, PacketHasher)>>, recorder: &TransactionRecorder, + cost_tracker: &Arc>, + cost_tracker_stats: &mut CostTrackerStats, ) -> Result<(), RecvTimeoutError> { let mut recv_time = Measure::start("process_packets_recv"); let mms = verified_receiver.recv_timeout(recv_timeout)?; @@ -1268,6 +1488,12 @@ impl BankingStage { continue; } let (bank, bank_creation_time) = bank_start.unwrap(); + Self::reset_cost_tracker_if_new_bank( + cost_tracker, + bank.clone(), + banking_stage_stats, + cost_tracker_stats, + ); let (processed, verified_txs_len, unprocessed_indexes) = Self::process_packets_transactions( @@ -1279,6 +1505,8 @@ impl BankingStage { transaction_status_sender.clone(), gossip_vote_sender, banking_stage_stats, + cost_tracker, + cost_tracker_stats, ); new_tx_count += processed; @@ -1310,6 +1538,9 @@ impl BankingStage { &packet_indexes, my_pubkey, next_leader, + cost_tracker, + banking_stage_stats, + cost_tracker_stats, ); Self::push_unprocessed( buffered_packets, @@ -1464,6 +1695,7 @@ where #[cfg(test)] mod tests { use super::*; + use crate::cost_model::CostModel; use crossbeam_channel::unbounded; use itertools::Itertools; use solana_gossip::{cluster_info::Node, contact_info::ContactInfo}; @@ -1536,6 +1768,9 @@ mod tests { gossip_verified_vote_receiver, None, vote_forward_sender, + Arc::new(RwLock::new(CostTracker::new(Arc::new(RwLock::new( + CostModel::default(), + ))))), ); drop(verified_sender); drop(gossip_verified_vote_sender); @@ -1584,6 +1819,9 @@ mod tests { verified_gossip_vote_receiver, None, vote_forward_sender, + Arc::new(RwLock::new(CostTracker::new(Arc::new(RwLock::new( + CostModel::default(), + ))))), ); trace!("sending bank"); drop(verified_sender); @@ -1656,6 +1894,9 @@ mod tests { gossip_verified_vote_receiver, None, gossip_vote_sender, + Arc::new(RwLock::new(CostTracker::new(Arc::new(RwLock::new( + CostModel::default(), + ))))), ); // fund another account so we can send 2 good transactions in a single batch. @@ -1806,6 +2047,9 @@ mod tests { 3, None, gossip_vote_sender, + Arc::new(RwLock::new(CostTracker::new(Arc::new(RwLock::new( + CostModel::default(), + ))))), ); // wait for banking_stage to eat the packets @@ -2627,6 +2871,10 @@ mod tests { None::>, &BankingStageStats::default(), &recorder, + &Arc::new(RwLock::new(CostTracker::new(Arc::new(RwLock::new( + CostModel::default(), + ))))), + &mut CostTrackerStats::default(), ); assert_eq!(buffered_packets[0].1.len(), num_conflicting_transactions); // When the poh recorder has a bank, should process all non conflicting buffered packets. @@ -2643,6 +2891,10 @@ mod tests { None::>, &BankingStageStats::default(), &recorder, + &Arc::new(RwLock::new(CostTracker::new(Arc::new(RwLock::new( + CostModel::default(), + ))))), + &mut CostTrackerStats::default(), ); if num_expected_unprocessed == 0 { assert!(buffered_packets.is_empty()) @@ -2708,6 +2960,10 @@ mod tests { test_fn, &BankingStageStats::default(), &recorder, + &Arc::new(RwLock::new(CostTracker::new(Arc::new(RwLock::new( + CostModel::default(), + ))))), + &mut CostTrackerStats::default(), ); // Check everything is correct. All indexes after `interrupted_iteration` @@ -2956,21 +3212,33 @@ mod tests { make_test_packets(vec![transfer_tx.clone(), transfer_tx.clone()], vote_indexes); let mut votes_only = false; - let (txs, tx_packet_index) = BankingStage::transactions_from_packets( + let (txs, tx_packet_index, _) = BankingStage::transactions_from_packets( &packets, &packet_indexes, false, votes_only, + &Arc::new(RwLock::new(CostTracker::new(Arc::new(RwLock::new( + CostModel::default(), + ))))), + &BankingStageStats::default(), + false, + &mut CostTrackerStats::default(), ); assert_eq!(2, txs.len()); assert_eq!(vec![0, 1], tx_packet_index); votes_only = true; - let (txs, tx_packet_index) = BankingStage::transactions_from_packets( + let (txs, tx_packet_index, _) = BankingStage::transactions_from_packets( &packets, &packet_indexes, false, votes_only, + &Arc::new(RwLock::new(CostTracker::new(Arc::new(RwLock::new( + CostModel::default(), + ))))), + &BankingStageStats::default(), + false, + &mut CostTrackerStats::default(), ); assert_eq!(0, txs.len()); assert_eq!(0, tx_packet_index.len()); @@ -2985,21 +3253,33 @@ mod tests { ); let mut votes_only = false; - let (txs, tx_packet_index) = BankingStage::transactions_from_packets( + let (txs, tx_packet_index, _) = BankingStage::transactions_from_packets( &packets, &packet_indexes, false, votes_only, + &Arc::new(RwLock::new(CostTracker::new(Arc::new(RwLock::new( + CostModel::default(), + ))))), + &BankingStageStats::default(), + false, + &mut CostTrackerStats::default(), ); assert_eq!(3, txs.len()); assert_eq!(vec![0, 1, 2], tx_packet_index); votes_only = true; - let (txs, tx_packet_index) = BankingStage::transactions_from_packets( + let (txs, tx_packet_index, _) = BankingStage::transactions_from_packets( &packets, &packet_indexes, false, votes_only, + &Arc::new(RwLock::new(CostTracker::new(Arc::new(RwLock::new( + CostModel::default(), + ))))), + &BankingStageStats::default(), + false, + &mut CostTrackerStats::default(), ); assert_eq!(2, txs.len()); assert_eq!(vec![0, 2], tx_packet_index); @@ -3014,21 +3294,33 @@ mod tests { ); let mut votes_only = false; - let (txs, tx_packet_index) = BankingStage::transactions_from_packets( + let (txs, tx_packet_index, _) = BankingStage::transactions_from_packets( &packets, &packet_indexes, false, votes_only, + &Arc::new(RwLock::new(CostTracker::new(Arc::new(RwLock::new( + CostModel::default(), + ))))), + &BankingStageStats::default(), + false, + &mut CostTrackerStats::default(), ); assert_eq!(3, txs.len()); assert_eq!(vec![0, 1, 2], tx_packet_index); votes_only = true; - let (txs, tx_packet_index) = BankingStage::transactions_from_packets( + let (txs, tx_packet_index, _) = BankingStage::transactions_from_packets( &packets, &packet_indexes, false, votes_only, + &Arc::new(RwLock::new(CostTracker::new(Arc::new(RwLock::new( + CostModel::default(), + ))))), + &BankingStageStats::default(), + false, + &mut CostTrackerStats::default(), ); assert_eq!(3, txs.len()); assert_eq!(vec![0, 1, 2], tx_packet_index); diff --git a/core/src/cost_model.rs b/core/src/cost_model.rs new file mode 100644 index 00000000000000..b53340b8fbc19f --- /dev/null +++ b/core/src/cost_model.rs @@ -0,0 +1,519 @@ +//! 'cost_model` provides service to estimate a transaction's cost +//! following proposed fee schedule #16984; Relevant cluster cost +//! measuring is described by #19627 +//! +//! The main function is `calculate_cost` which returns &TransactionCost. +//! +use crate::execute_cost_table::ExecuteCostTable; +use log::*; +use solana_ledger::block_cost_limits::*; +use solana_sdk::{pubkey::Pubkey, transaction::Transaction}; +use std::collections::HashMap; + +const MAX_WRITABLE_ACCOUNTS: usize = 256; + +#[derive(Debug, Clone)] +pub enum CostModelError { + /// transaction that would fail sanitize, cost model is not able to process + /// such transaction. + InvalidTransaction, + + /// would exceed block max limit + WouldExceedBlockMaxLimit, + + /// would exceed account max limit + WouldExceedAccountMaxLimit, +} + +#[derive(Default, Debug)] +pub struct TransactionCost { + pub writable_accounts: Vec, + pub signature_cost: u64, + pub write_lock_cost: u64, + pub data_bytes_cost: u64, + pub execution_cost: u64, +} + +impl TransactionCost { + pub fn new_with_capacity(capacity: usize) -> Self { + Self { + writable_accounts: Vec::with_capacity(capacity), + ..Self::default() + } + } + + pub fn reset(&mut self) { + self.writable_accounts.clear(); + self.signature_cost = 0; + self.write_lock_cost = 0; + self.data_bytes_cost = 0; + self.execution_cost = 0; + } + + pub fn sum(&self) -> u64 { + self.signature_cost + self.write_lock_cost + self.data_bytes_cost + self.execution_cost + } +} + +#[derive(Debug)] +pub struct CostModel { + account_cost_limit: u64, + block_cost_limit: u64, + instruction_execution_cost_table: ExecuteCostTable, + + // reusable variables + transaction_cost: TransactionCost, +} + +impl Default for CostModel { + fn default() -> Self { + CostModel::new(MAX_WRITABLE_ACCOUNT_UNITS, MAX_BLOCK_UNITS) + } +} + +impl CostModel { + pub fn new(chain_max: u64, block_max: u64) -> Self { + Self { + account_cost_limit: chain_max, + block_cost_limit: block_max, + instruction_execution_cost_table: ExecuteCostTable::default(), + transaction_cost: TransactionCost::new_with_capacity(MAX_WRITABLE_ACCOUNTS), + } + } + + pub fn get_account_cost_limit(&self) -> u64 { + self.account_cost_limit + } + + pub fn get_block_cost_limit(&self) -> u64 { + self.block_cost_limit + } + + pub fn initialize_cost_table(&mut self, cost_table: &[(Pubkey, u64)]) { + cost_table + .iter() + .map(|(key, cost)| (key, cost)) + .chain(BUILT_IN_INSTRUCTION_COSTS.iter()) + .for_each(|(program_id, cost)| { + match self + .instruction_execution_cost_table + .upsert(program_id, *cost) + { + Some(c) => { + debug!( + "initiating cost table, instruction {:?} has cost {}", + program_id, c + ); + } + None => { + debug!( + "initiating cost table, failed for instruction {:?}", + program_id + ); + } + } + }); + debug!( + "restored cost model instruction cost table from blockstore, current values: {:?}", + self.get_instruction_cost_table() + ); + } + + pub fn calculate_cost( + &mut self, + transaction: &Transaction, + demote_program_write_locks: bool, + ) -> &TransactionCost { + self.transaction_cost.reset(); + + self.transaction_cost.signature_cost = self.get_signature_cost(transaction); + self.get_write_lock_cost(transaction, demote_program_write_locks); + self.transaction_cost.data_bytes_cost = self.get_data_bytes_cost(transaction); + self.transaction_cost.execution_cost = self.get_transaction_cost(transaction); + + debug!( + "transaction {:?} has cost {:?}", + transaction, self.transaction_cost + ); + &self.transaction_cost + } + + pub fn upsert_instruction_cost( + &mut self, + program_key: &Pubkey, + cost: u64, + ) -> Result { + self.instruction_execution_cost_table + .upsert(program_key, cost); + match self.instruction_execution_cost_table.get_cost(program_key) { + Some(cost) => Ok(*cost), + None => Err("failed to upsert to ExecuteCostTable"), + } + } + + pub fn get_instruction_cost_table(&self) -> &HashMap { + self.instruction_execution_cost_table.get_cost_table() + } + + fn get_signature_cost(&self, transaction: &Transaction) -> u64 { + transaction.signatures.len() as u64 * SIGNATURE_COST + } + + fn get_write_lock_cost(&mut self, transaction: &Transaction, demote_program_write_locks: bool) { + let message = transaction.message(); + message.account_keys.iter().enumerate().for_each(|(i, k)| { + let is_writable = message.is_writable(i, demote_program_write_locks); + + if is_writable { + self.transaction_cost.writable_accounts.push(*k); + self.transaction_cost.write_lock_cost += WRITE_LOCK_UNITS; + } + }); + } + + fn get_data_bytes_cost(&self, transaction: &Transaction) -> u64 { + let mut data_bytes_cost: u64 = 0; + transaction.message().instructions.iter().for_each(|ix| { + data_bytes_cost += ix.data.len() as u64 / DATA_BYTES_UNITS; + }); + data_bytes_cost + } + + fn get_transaction_cost(&self, transaction: &Transaction) -> u64 { + let mut cost: u64 = 0; + + for instruction in &transaction.message().instructions { + let program_id = + transaction.message().account_keys[instruction.program_id_index as usize]; + let instruction_cost = self.find_instruction_cost(&program_id); + trace!( + "instruction {:?} has cost of {}", + instruction, + instruction_cost + ); + cost = cost.saturating_add(instruction_cost); + } + cost + } + + fn find_instruction_cost(&self, program_key: &Pubkey) -> u64 { + match self.instruction_execution_cost_table.get_cost(program_key) { + Some(cost) => *cost, + None => { + let default_value = self.instruction_execution_cost_table.get_mode(); + debug!( + "Program key {:?} does not have assigned cost, using mode {}", + program_key, default_value + ); + default_value + } + } + } +} + +#[cfg(test)] +mod tests { + use super::*; + use solana_runtime::{ + bank::Bank, + genesis_utils::{create_genesis_config, GenesisConfigInfo}, + }; + use solana_sdk::{ + bpf_loader, + hash::Hash, + instruction::CompiledInstruction, + message::Message, + signature::{Keypair, Signer}, + system_instruction::{self}, + system_program, system_transaction, + }; + use std::{ + str::FromStr, + sync::{Arc, RwLock}, + thread::{self, JoinHandle}, + }; + + fn test_setup() -> (Keypair, Hash) { + solana_logger::setup(); + let GenesisConfigInfo { + genesis_config, + mint_keypair, + .. + } = create_genesis_config(10); + let bank = Arc::new(Bank::new_no_wallclock_throttle(&genesis_config)); + let start_hash = bank.last_blockhash(); + (mint_keypair, start_hash) + } + + #[test] + fn test_cost_model_instruction_cost() { + let mut testee = CostModel::default(); + + let known_key = Pubkey::from_str("known11111111111111111111111111111111111111").unwrap(); + testee.upsert_instruction_cost(&known_key, 100).unwrap(); + // find cost for known programs + assert_eq!(100, testee.find_instruction_cost(&known_key)); + + testee + .upsert_instruction_cost(&bpf_loader::id(), 1999) + .unwrap(); + assert_eq!(1999, testee.find_instruction_cost(&bpf_loader::id())); + + // unknown program is assigned with default cost + assert_eq!( + testee.instruction_execution_cost_table.get_mode(), + testee.find_instruction_cost( + &Pubkey::from_str("unknown111111111111111111111111111111111111").unwrap() + ) + ); + } + + #[test] + fn test_cost_model_simple_transaction() { + let (mint_keypair, start_hash) = test_setup(); + + let keypair = Keypair::new(); + let simple_transaction = + system_transaction::transfer(&mint_keypair, &keypair.pubkey(), 2, start_hash); + debug!( + "system_transaction simple_transaction {:?}", + simple_transaction + ); + + // expected cost for one system transfer instructions + let expected_cost = 8; + + let mut testee = CostModel::default(); + testee + .upsert_instruction_cost(&system_program::id(), expected_cost) + .unwrap(); + assert_eq!( + expected_cost, + testee.get_transaction_cost(&simple_transaction) + ); + } + + #[test] + fn test_cost_model_transaction_many_transfer_instructions() { + let (mint_keypair, start_hash) = test_setup(); + + let key1 = solana_sdk::pubkey::new_rand(); + let key2 = solana_sdk::pubkey::new_rand(); + let instructions = + system_instruction::transfer_many(&mint_keypair.pubkey(), &[(key1, 1), (key2, 1)]); + let message = Message::new(&instructions, Some(&mint_keypair.pubkey())); + let tx = Transaction::new(&[&mint_keypair], message, start_hash); + debug!("many transfer transaction {:?}", tx); + + // expected cost for two system transfer instructions + let program_cost = 8; + let expected_cost = program_cost * 2; + + let mut testee = CostModel::default(); + testee + .upsert_instruction_cost(&system_program::id(), program_cost) + .unwrap(); + assert_eq!(expected_cost, testee.get_transaction_cost(&tx)); + } + + #[test] + fn test_cost_model_message_many_different_instructions() { + let (mint_keypair, start_hash) = test_setup(); + + // construct a transaction with multiple random instructions + let key1 = solana_sdk::pubkey::new_rand(); + let key2 = solana_sdk::pubkey::new_rand(); + let prog1 = solana_sdk::pubkey::new_rand(); + let prog2 = solana_sdk::pubkey::new_rand(); + let instructions = vec![ + CompiledInstruction::new(3, &(), vec![0, 1]), + CompiledInstruction::new(4, &(), vec![0, 2]), + ]; + let tx = Transaction::new_with_compiled_instructions( + &[&mint_keypair], + &[key1, key2], + start_hash, + vec![prog1, prog2], + instructions, + ); + debug!("many random transaction {:?}", tx); + + let testee = CostModel::default(); + let result = testee.get_transaction_cost(&tx); + + // expected cost for two random/unknown program is + let expected_cost = testee.instruction_execution_cost_table.get_mode() * 2; + assert_eq!(expected_cost, result); + } + + #[test] + fn test_cost_model_sort_message_accounts_by_type() { + // construct a transaction with two random instructions with same signer + let signer1 = Keypair::new(); + let signer2 = Keypair::new(); + let key1 = Pubkey::new_unique(); + let key2 = Pubkey::new_unique(); + let prog1 = Pubkey::new_unique(); + let prog2 = Pubkey::new_unique(); + let instructions = vec![ + CompiledInstruction::new(4, &(), vec![0, 2]), + CompiledInstruction::new(5, &(), vec![1, 3]), + ]; + let tx = Transaction::new_with_compiled_instructions( + &[&signer1, &signer2], + &[key1, key2], + Hash::new_unique(), + vec![prog1, prog2], + instructions, + ); + + let mut cost_model = CostModel::default(); + let tx_cost = cost_model.calculate_cost(&tx, /*demote_program_write_locks=*/ true); + assert_eq!(2 + 2, tx_cost.writable_accounts.len()); + assert_eq!(signer1.pubkey(), tx_cost.writable_accounts[0]); + assert_eq!(signer2.pubkey(), tx_cost.writable_accounts[1]); + assert_eq!(key1, tx_cost.writable_accounts[2]); + assert_eq!(key2, tx_cost.writable_accounts[3]); + } + + #[test] + fn test_cost_model_insert_instruction_cost() { + let key1 = Pubkey::new_unique(); + let cost1 = 100; + + let mut cost_model = CostModel::default(); + // Using default cost for unknown instruction + assert_eq!( + cost_model.instruction_execution_cost_table.get_mode(), + cost_model.find_instruction_cost(&key1) + ); + + // insert instruction cost to table + assert!(cost_model.upsert_instruction_cost(&key1, cost1).is_ok()); + + // now it is known insturction with known cost + assert_eq!(cost1, cost_model.find_instruction_cost(&key1)); + } + + #[test] + fn test_cost_model_calculate_cost() { + let (mint_keypair, start_hash) = test_setup(); + let tx = + system_transaction::transfer(&mint_keypair, &Keypair::new().pubkey(), 2, start_hash); + + let expected_account_cost = WRITE_LOCK_UNITS * 2; + let expected_execution_cost = 8; + + let mut cost_model = CostModel::default(); + cost_model + .upsert_instruction_cost(&system_program::id(), expected_execution_cost) + .unwrap(); + let tx_cost = cost_model.calculate_cost(&tx, /*demote_program_write_locks=*/ true); + assert_eq!(expected_account_cost, tx_cost.write_lock_cost); + assert_eq!(expected_execution_cost, tx_cost.execution_cost); + assert_eq!(2, tx_cost.writable_accounts.len()); + } + + #[test] + fn test_cost_model_update_instruction_cost() { + let key1 = Pubkey::new_unique(); + let cost1 = 100; + let cost2 = 200; + let updated_cost = (cost1 + cost2) / 2; + + let mut cost_model = CostModel::default(); + + // insert instruction cost to table + assert!(cost_model.upsert_instruction_cost(&key1, cost1).is_ok()); + assert_eq!(cost1, cost_model.find_instruction_cost(&key1)); + + // update instruction cost + assert!(cost_model.upsert_instruction_cost(&key1, cost2).is_ok()); + assert_eq!(updated_cost, cost_model.find_instruction_cost(&key1)); + } + + #[test] + fn test_cost_model_can_be_shared_concurrently_with_rwlock() { + let (mint_keypair, start_hash) = test_setup(); + // construct a transaction with multiple random instructions + let key1 = solana_sdk::pubkey::new_rand(); + let key2 = solana_sdk::pubkey::new_rand(); + let prog1 = solana_sdk::pubkey::new_rand(); + let prog2 = solana_sdk::pubkey::new_rand(); + let instructions = vec![ + CompiledInstruction::new(3, &(), vec![0, 1]), + CompiledInstruction::new(4, &(), vec![0, 2]), + ]; + let tx = Arc::new(Transaction::new_with_compiled_instructions( + &[&mint_keypair], + &[key1, key2], + start_hash, + vec![prog1, prog2], + instructions, + )); + + let number_threads = 10; + let expected_account_cost = WRITE_LOCK_UNITS * 3; + let cost1 = 100; + let cost2 = 200; + // execution cost can be either 2 * Default (before write) or cost1+cost2 (after write) + + let cost_model: Arc> = Arc::new(RwLock::new(CostModel::default())); + + let thread_handlers: Vec> = (0..number_threads) + .map(|i| { + let cost_model = cost_model.clone(); + let tx = tx.clone(); + + if i == 5 { + thread::spawn(move || { + let mut cost_model = cost_model.write().unwrap(); + assert!(cost_model.upsert_instruction_cost(&prog1, cost1).is_ok()); + assert!(cost_model.upsert_instruction_cost(&prog2, cost2).is_ok()); + }) + } else { + thread::spawn(move || { + let mut cost_model = cost_model.write().unwrap(); + let tx_cost = cost_model + .calculate_cost(&tx, /*demote_program_write_locks=*/ true); + assert_eq!(3, tx_cost.writable_accounts.len()); + assert_eq!(expected_account_cost, tx_cost.write_lock_cost); + }) + } + }) + .collect(); + + for th in thread_handlers { + th.join().unwrap(); + } + } + + #[test] + fn test_initialize_cost_table() { + // build cost table + let cost_table = vec![ + (Pubkey::new_unique(), 10), + (Pubkey::new_unique(), 20), + (Pubkey::new_unique(), 30), + ]; + + // init cost model + let mut cost_model = CostModel::default(); + cost_model.initialize_cost_table(&cost_table); + + // verify + for (id, cost) in cost_table.iter() { + assert_eq!(*cost, cost_model.find_instruction_cost(id)); + } + + // verify built-in programs + assert!(cost_model + .instruction_execution_cost_table + .get_cost(&system_program::id()) + .is_some()); + assert!(cost_model + .instruction_execution_cost_table + .get_cost(&solana_vote_program::id()) + .is_some()); + } +} diff --git a/core/src/cost_tracker.rs b/core/src/cost_tracker.rs new file mode 100644 index 00000000000000..3c476f5d0206f9 --- /dev/null +++ b/core/src/cost_tracker.rs @@ -0,0 +1,482 @@ +//! `cost_tracker` keeps tracking transaction cost per chained accounts as well as for entire block +//! It aggregates `cost_model`, which provides service of calculating transaction cost. +//! The main functions are: +//! - would_transaction_fit(&tx), immutable function to test if `tx` would fit into current block +//! - add_transaction_cost(&tx), mutable function to accumulate `tx` cost to tracker. +//! +use crate::cost_model::{CostModel, TransactionCost}; +use crate::cost_tracker_stats::CostTrackerStats; +use solana_sdk::{clock::Slot, pubkey::Pubkey, transaction::Transaction}; +use std::{ + collections::HashMap, + sync::{Arc, RwLock}, +}; + +const WRITABLE_ACCOUNTS_PER_BLOCK: usize = 512; + +#[derive(Debug)] +pub struct CostTracker { + cost_model: Arc>, + account_cost_limit: u64, + block_cost_limit: u64, + current_bank_slot: Slot, + cost_by_writable_accounts: HashMap, + block_cost: u64, +} + +impl CostTracker { + pub fn new(cost_model: Arc>) -> Self { + let (account_cost_limit, block_cost_limit) = { + let cost_model = cost_model.read().unwrap(); + ( + cost_model.get_account_cost_limit(), + cost_model.get_block_cost_limit(), + ) + }; + assert!(account_cost_limit <= block_cost_limit); + Self { + cost_model, + account_cost_limit, + block_cost_limit, + current_bank_slot: 0, + cost_by_writable_accounts: HashMap::with_capacity(WRITABLE_ACCOUNTS_PER_BLOCK), + block_cost: 0, + } + } + + pub fn would_transaction_fit( + &self, + transaction: &Transaction, + demote_program_write_locks: bool, + stats: &mut CostTrackerStats, + ) -> Result<(), &'static str> { + let mut cost_model = self.cost_model.write().unwrap(); + let tx_cost = cost_model.calculate_cost(transaction, demote_program_write_locks); + self.would_fit(&tx_cost.writable_accounts, &tx_cost.sum(), stats) + } + + pub fn add_transaction_cost( + &mut self, + transaction: &Transaction, + demote_program_write_locks: bool, + stats: &mut CostTrackerStats, + ) { + let mut cost_model = self.cost_model.write().unwrap(); + let tx_cost = cost_model.calculate_cost(transaction, demote_program_write_locks); + let cost = tx_cost.sum(); + for account_key in tx_cost.writable_accounts.iter() { + *self + .cost_by_writable_accounts + .entry(*account_key) + .or_insert(0) += cost; + } + self.block_cost += cost; + + stats.transaction_count += 1; + stats.block_cost += cost; + } + + pub fn reset_if_new_bank(&mut self, slot: Slot, stats: &mut CostTrackerStats) -> bool { + // report stats when slot changes + if slot != stats.bank_slot { + stats.report(); + *stats = CostTrackerStats::new(stats.id, slot); + } + + if slot != self.current_bank_slot { + self.current_bank_slot = slot; + self.cost_by_writable_accounts.clear(); + self.block_cost = 0; + + true + } else { + false + } + } + + pub fn try_add( + &mut self, + transaction_cost: &TransactionCost, + stats: &mut CostTrackerStats, + ) -> Result { + let cost = transaction_cost.sum(); + self.would_fit(&transaction_cost.writable_accounts, &cost, stats)?; + + self.add_transaction(&transaction_cost.writable_accounts, &cost); + Ok(self.block_cost) + } + + fn would_fit( + &self, + keys: &[Pubkey], + cost: &u64, + stats: &mut CostTrackerStats, + ) -> Result<(), &'static str> { + stats.transaction_cost_histogram.increment(*cost).unwrap(); + + // check against the total package cost + if self.block_cost + cost > self.block_cost_limit { + return Err("would exceed block cost limit"); + } + + // check if the transaction itself is more costly than the account_cost_limit + if *cost > self.account_cost_limit { + return Err("Transaction is too expansive, exceeds account cost limit"); + } + + // check each account against account_cost_limit, + for account_key in keys.iter() { + match self.cost_by_writable_accounts.get(&account_key) { + Some(chained_cost) => { + stats + .writable_accounts_cost_histogram + .increment(*chained_cost) + .unwrap(); + + if chained_cost + cost > self.account_cost_limit { + return Err("would exceed account cost limit"); + } else { + continue; + } + } + None => continue, + } + } + + Ok(()) + } + + fn add_transaction(&mut self, keys: &[Pubkey], cost: &u64) { + for account_key in keys.iter() { + *self + .cost_by_writable_accounts + .entry(*account_key) + .or_insert(0) += cost; + } + self.block_cost += cost; + } +} + +// CostStats can be collected by util, such as ledger_tool +#[derive(Default, Debug)] +pub struct CostStats { + pub bank_slot: Slot, + pub total_cost: u64, + pub number_of_accounts: usize, + pub costliest_account: Pubkey, + pub costliest_account_cost: u64, +} + +impl CostTracker { + pub fn get_stats(&self) -> CostStats { + let mut stats = CostStats { + bank_slot: self.current_bank_slot, + total_cost: self.block_cost, + number_of_accounts: self.cost_by_writable_accounts.len(), + costliest_account: Pubkey::default(), + costliest_account_cost: 0, + }; + + for (key, cost) in self.cost_by_writable_accounts.iter() { + if cost > &stats.costliest_account_cost { + stats.costliest_account = *key; + stats.costliest_account_cost = *cost; + } + } + + stats + } +} + +#[cfg(test)] +mod tests { + use super::*; + use solana_runtime::{ + bank::Bank, + genesis_utils::{create_genesis_config, GenesisConfigInfo}, + }; + use solana_sdk::{ + hash::Hash, + signature::{Keypair, Signer}, + system_transaction, + transaction::Transaction, + }; + use std::{cmp, sync::Arc}; + + fn test_setup() -> (Keypair, Hash) { + solana_logger::setup(); + let GenesisConfigInfo { + genesis_config, + mint_keypair, + .. + } = create_genesis_config(10); + let bank = Arc::new(Bank::new_no_wallclock_throttle(&genesis_config)); + let start_hash = bank.last_blockhash(); + (mint_keypair, start_hash) + } + + fn build_simple_transaction( + mint_keypair: &Keypair, + start_hash: &Hash, + ) -> (Transaction, Vec, u64) { + let keypair = Keypair::new(); + let simple_transaction = + system_transaction::transfer(&mint_keypair, &keypair.pubkey(), 2, *start_hash); + + (simple_transaction, vec![mint_keypair.pubkey()], 5) + } + + #[test] + fn test_cost_tracker_initialization() { + let testee = CostTracker::new(Arc::new(RwLock::new(CostModel::new(10, 11)))); + assert_eq!(10, testee.account_cost_limit); + assert_eq!(11, testee.block_cost_limit); + assert_eq!(0, testee.cost_by_writable_accounts.len()); + assert_eq!(0, testee.block_cost); + } + + #[test] + fn test_cost_tracker_ok_add_one() { + let (mint_keypair, start_hash) = test_setup(); + let (_tx, keys, cost) = build_simple_transaction(&mint_keypair, &start_hash); + + // build testee to have capacity for one simple transaction + let mut testee = CostTracker::new(Arc::new(RwLock::new(CostModel::new(cost, cost)))); + assert!(testee + .would_fit(&keys, &cost, &mut CostTrackerStats::default()) + .is_ok()); + testee.add_transaction(&keys, &cost); + assert_eq!(cost, testee.block_cost); + } + + #[test] + fn test_cost_tracker_ok_add_two_same_accounts() { + let (mint_keypair, start_hash) = test_setup(); + // build two transactions with same signed account + let (_tx1, keys1, cost1) = build_simple_transaction(&mint_keypair, &start_hash); + let (_tx2, keys2, cost2) = build_simple_transaction(&mint_keypair, &start_hash); + + // build testee to have capacity for two simple transactions, with same accounts + let mut testee = CostTracker::new(Arc::new(RwLock::new(CostModel::new( + cost1 + cost2, + cost1 + cost2, + )))); + { + assert!(testee + .would_fit(&keys1, &cost1, &mut CostTrackerStats::default()) + .is_ok()); + testee.add_transaction(&keys1, &cost1); + } + { + assert!(testee + .would_fit(&keys2, &cost2, &mut CostTrackerStats::default()) + .is_ok()); + testee.add_transaction(&keys2, &cost2); + } + assert_eq!(cost1 + cost2, testee.block_cost); + assert_eq!(1, testee.cost_by_writable_accounts.len()); + } + + #[test] + fn test_cost_tracker_ok_add_two_diff_accounts() { + let (mint_keypair, start_hash) = test_setup(); + // build two transactions with diff accounts + let (_tx1, keys1, cost1) = build_simple_transaction(&mint_keypair, &start_hash); + let second_account = Keypair::new(); + let (_tx2, keys2, cost2) = build_simple_transaction(&second_account, &start_hash); + + // build testee to have capacity for two simple transactions, with same accounts + let mut testee = CostTracker::new(Arc::new(RwLock::new(CostModel::new( + cmp::max(cost1, cost2), + cost1 + cost2, + )))); + { + assert!(testee + .would_fit(&keys1, &cost1, &mut CostTrackerStats::default()) + .is_ok()); + testee.add_transaction(&keys1, &cost1); + } + { + assert!(testee + .would_fit(&keys2, &cost2, &mut CostTrackerStats::default()) + .is_ok()); + testee.add_transaction(&keys2, &cost2); + } + assert_eq!(cost1 + cost2, testee.block_cost); + assert_eq!(2, testee.cost_by_writable_accounts.len()); + } + + #[test] + fn test_cost_tracker_chain_reach_limit() { + let (mint_keypair, start_hash) = test_setup(); + // build two transactions with same signed account + let (_tx1, keys1, cost1) = build_simple_transaction(&mint_keypair, &start_hash); + let (_tx2, keys2, cost2) = build_simple_transaction(&mint_keypair, &start_hash); + + // build testee to have capacity for two simple transactions, but not for same accounts + let mut testee = CostTracker::new(Arc::new(RwLock::new(CostModel::new( + cmp::min(cost1, cost2), + cost1 + cost2, + )))); + // should have room for first transaction + { + assert!(testee + .would_fit(&keys1, &cost1, &mut CostTrackerStats::default()) + .is_ok()); + testee.add_transaction(&keys1, &cost1); + } + // but no more sapce on the same chain (same signer account) + { + assert!(testee + .would_fit(&keys2, &cost2, &mut CostTrackerStats::default()) + .is_err()); + } + } + + #[test] + fn test_cost_tracker_reach_limit() { + let (mint_keypair, start_hash) = test_setup(); + // build two transactions with diff accounts + let (_tx1, keys1, cost1) = build_simple_transaction(&mint_keypair, &start_hash); + let second_account = Keypair::new(); + let (_tx2, keys2, cost2) = build_simple_transaction(&second_account, &start_hash); + + // build testee to have capacity for each chain, but not enough room for both transactions + let mut testee = CostTracker::new(Arc::new(RwLock::new(CostModel::new( + cmp::max(cost1, cost2), + cost1 + cost2 - 1, + )))); + // should have room for first transaction + { + assert!(testee + .would_fit(&keys1, &cost1, &mut CostTrackerStats::default()) + .is_ok()); + testee.add_transaction(&keys1, &cost1); + } + // but no more room for package as whole + { + assert!(testee + .would_fit(&keys2, &cost2, &mut CostTrackerStats::default()) + .is_err()); + } + } + + #[test] + fn test_cost_tracker_reset() { + let (mint_keypair, start_hash) = test_setup(); + // build two transactions with same signed account + let (_tx1, keys1, cost1) = build_simple_transaction(&mint_keypair, &start_hash); + let (_tx2, keys2, cost2) = build_simple_transaction(&mint_keypair, &start_hash); + + // build testee to have capacity for two simple transactions, but not for same accounts + let mut testee = CostTracker::new(Arc::new(RwLock::new(CostModel::new( + cmp::min(cost1, cost2), + cost1 + cost2, + )))); + // should have room for first transaction + { + assert!(testee + .would_fit(&keys1, &cost1, &mut CostTrackerStats::default()) + .is_ok()); + testee.add_transaction(&keys1, &cost1); + assert_eq!(1, testee.cost_by_writable_accounts.len()); + assert_eq!(cost1, testee.block_cost); + } + // but no more sapce on the same chain (same signer account) + { + assert!(testee + .would_fit(&keys2, &cost2, &mut CostTrackerStats::default()) + .is_err()); + } + // reset the tracker + { + testee.reset_if_new_bank(100, &mut CostTrackerStats::default()); + assert_eq!(0, testee.cost_by_writable_accounts.len()); + assert_eq!(0, testee.block_cost); + } + //now the second transaction can be added + { + assert!(testee + .would_fit(&keys2, &cost2, &mut CostTrackerStats::default()) + .is_ok()); + } + } + + #[test] + fn test_cost_tracker_try_add_is_atomic() { + let acct1 = Pubkey::new_unique(); + let acct2 = Pubkey::new_unique(); + let acct3 = Pubkey::new_unique(); + let cost = 100; + let account_max = cost * 2; + let block_max = account_max * 3; // for three accts + + let mut testee = CostTracker::new(Arc::new(RwLock::new(CostModel::new( + account_max, + block_max, + )))); + + // case 1: a tx writes to 3 accounts, should success, we will have: + // | acct1 | $cost | + // | acct2 | $cost | + // | acct2 | $cost | + // and block_cost = $cost + { + let tx_cost = TransactionCost { + writable_accounts: vec![acct1, acct2, acct3], + execution_cost: cost, + ..TransactionCost::default() + }; + assert!(testee + .try_add(&tx_cost, &mut CostTrackerStats::default()) + .is_ok()); + let stat = testee.get_stats(); + assert_eq!(cost, stat.total_cost); + assert_eq!(3, stat.number_of_accounts); + assert_eq!(cost, stat.costliest_account_cost); + } + + // case 2: add tx writes to acct2 with $cost, should succeed, result to + // | acct1 | $cost | + // | acct2 | $cost * 2 | + // | acct2 | $cost | + // and block_cost = $cost * 2 + { + let tx_cost = TransactionCost { + writable_accounts: vec![acct2], + execution_cost: cost, + ..TransactionCost::default() + }; + assert!(testee + .try_add(&tx_cost, &mut CostTrackerStats::default()) + .is_ok()); + let stat = testee.get_stats(); + assert_eq!(cost * 2, stat.total_cost); + assert_eq!(3, stat.number_of_accounts); + assert_eq!(cost * 2, stat.costliest_account_cost); + assert_eq!(acct2, stat.costliest_account); + } + + // case 3: add tx writes to [acct1, acct2], acct2 exceeds limit, should failed atomically, + // we shoudl still have: + // | acct1 | $cost | + // | acct2 | $cost | + // | acct2 | $cost | + // and block_cost = $cost + { + let tx_cost = TransactionCost { + writable_accounts: vec![acct1, acct2], + execution_cost: cost, + ..TransactionCost::default() + }; + assert!(testee + .try_add(&tx_cost, &mut CostTrackerStats::default()) + .is_err()); + let stat = testee.get_stats(); + assert_eq!(cost * 2, stat.total_cost); + assert_eq!(3, stat.number_of_accounts); + assert_eq!(cost * 2, stat.costliest_account_cost); + assert_eq!(acct2, stat.costliest_account); + } + } +} diff --git a/core/src/cost_tracker_stats.rs b/core/src/cost_tracker_stats.rs new file mode 100644 index 00000000000000..40d7469c98eb35 --- /dev/null +++ b/core/src/cost_tracker_stats.rs @@ -0,0 +1,75 @@ +//! The Stats is not thread safe, each thread should have its own +//! instance of stat with `id`; Stat reports and reset for each slot. +#[derive(Debug, Default)] +pub struct CostTrackerStats { + pub id: u32, + pub transaction_cost_histogram: histogram::Histogram, + pub writable_accounts_cost_histogram: histogram::Histogram, + pub transaction_count: u64, + pub block_cost: u64, + pub bank_slot: u64, +} + +impl CostTrackerStats { + pub fn new(id: u32, bank_slot: u64) -> Self { + CostTrackerStats { + id, + bank_slot, + ..CostTrackerStats::default() + } + } + + pub fn report(&self) { + datapoint_info!( + "cost_tracker_stats", + ("id", self.id as i64, i64), + ( + "transaction_cost_unit_min", + self.transaction_cost_histogram.minimum().unwrap_or(0), + i64 + ), + ( + "transaction_cost_unit_max", + self.transaction_cost_histogram.maximum().unwrap_or(0), + i64 + ), + ( + "transaction_cost_unit_mean", + self.transaction_cost_histogram.mean().unwrap_or(0), + i64 + ), + ( + "transaction_cost_unit_2nd_std", + self.transaction_cost_histogram + .percentile(95.0) + .unwrap_or(0), + i64 + ), + ( + "writable_accounts_cost_min", + self.writable_accounts_cost_histogram.minimum().unwrap_or(0), + i64 + ), + ( + "writable_accounts_cost_max", + self.writable_accounts_cost_histogram.maximum().unwrap_or(0), + i64 + ), + ( + "writable_accounts_cost_mean", + self.writable_accounts_cost_histogram.mean().unwrap_or(0), + i64 + ), + ( + "writable_accounts_cost_2nd_std", + self.writable_accounts_cost_histogram + .percentile(95.0) + .unwrap_or(0), + i64 + ), + ("transaction_count", self.transaction_count as i64, i64), + ("block_cost", self.block_cost as i64, i64), + ("bank_slot", self.bank_slot as i64, i64), + ); + } +} diff --git a/core/src/cost_update_service.rs b/core/src/cost_update_service.rs new file mode 100644 index 00000000000000..a92d3121734511 --- /dev/null +++ b/core/src/cost_update_service.rs @@ -0,0 +1,292 @@ +//! this service receives instruction ExecuteTimings from replay_stage, +//! update cost_model which is shared with banking_stage to optimize +//! packing transactions into block; it also triggers persisting cost +//! table to blockstore. + +use crate::cost_model::CostModel; +use solana_ledger::blockstore::Blockstore; +use solana_measure::measure::Measure; +use solana_runtime::bank::ExecuteTimings; +use solana_sdk::timing::timestamp; +use std::{ + sync::{ + atomic::{AtomicBool, Ordering}, + mpsc::Receiver, + Arc, RwLock, + }, + thread::{self, Builder, JoinHandle}, + time::Duration, +}; + +#[derive(Default)] +pub struct CostUpdateServiceTiming { + last_print: u64, + update_cost_model_count: u64, + update_cost_model_elapsed: u64, + persist_cost_table_elapsed: u64, +} + +impl CostUpdateServiceTiming { + fn update( + &mut self, + update_cost_model_count: u64, + update_cost_model_elapsed: u64, + persist_cost_table_elapsed: u64, + ) { + self.update_cost_model_count += update_cost_model_count; + self.update_cost_model_elapsed += update_cost_model_elapsed; + self.persist_cost_table_elapsed += persist_cost_table_elapsed; + + let now = timestamp(); + let elapsed_ms = now - self.last_print; + if elapsed_ms > 1000 { + datapoint_info!( + "cost-update-service-stats", + ("total_elapsed_us", elapsed_ms * 1000, i64), + ( + "update_cost_model_count", + self.update_cost_model_count as i64, + i64 + ), + ( + "update_cost_model_elapsed", + self.update_cost_model_elapsed as i64, + i64 + ), + ( + "persist_cost_table_elapsed", + self.persist_cost_table_elapsed as i64, + i64 + ), + ); + + *self = CostUpdateServiceTiming::default(); + self.last_print = now; + } + } +} + +pub type CostUpdateReceiver = Receiver; + +pub struct CostUpdateService { + thread_hdl: JoinHandle<()>, +} + +impl CostUpdateService { + #[allow(clippy::new_ret_no_self)] + pub fn new( + exit: Arc, + blockstore: Arc, + cost_model: Arc>, + cost_update_receiver: CostUpdateReceiver, + ) -> Self { + let thread_hdl = Builder::new() + .name("solana-cost-update-service".to_string()) + .spawn(move || { + Self::service_loop(exit, blockstore, cost_model, cost_update_receiver); + }) + .unwrap(); + + Self { thread_hdl } + } + + pub fn join(self) -> thread::Result<()> { + self.thread_hdl.join() + } + + fn service_loop( + exit: Arc, + blockstore: Arc, + cost_model: Arc>, + cost_update_receiver: CostUpdateReceiver, + ) { + let mut cost_update_service_timing = CostUpdateServiceTiming::default(); + let mut dirty: bool; + let mut update_count: u64; + let wait_timer = Duration::from_millis(100); + + loop { + if exit.load(Ordering::Relaxed) { + break; + } + + dirty = false; + update_count = 0_u64; + let mut update_cost_model_time = Measure::start("update_cost_model_time"); + for cost_update in cost_update_receiver.try_iter() { + dirty |= Self::update_cost_model(&cost_model, &cost_update); + update_count += 1; + } + update_cost_model_time.stop(); + + let mut persist_cost_table_time = Measure::start("persist_cost_table_time"); + if dirty { + Self::persist_cost_table(&blockstore, &cost_model); + } + persist_cost_table_time.stop(); + + cost_update_service_timing.update( + update_count, + update_cost_model_time.as_us(), + persist_cost_table_time.as_us(), + ); + + thread::sleep(wait_timer); + } + } + + fn update_cost_model(cost_model: &RwLock, execute_timings: &ExecuteTimings) -> bool { + let mut dirty = false; + { + let mut cost_model_mutable = cost_model.write().unwrap(); + for (program_id, timing) in &execute_timings.details.per_program_timings { + if timing.count < 1 { + continue; + } + let units = timing.accumulated_units / timing.count as u64; + match cost_model_mutable.upsert_instruction_cost(program_id, units) { + Ok(c) => { + debug!( + "after replayed into bank, instruction {:?} has averaged cost {}", + program_id, c + ); + dirty = true; + } + Err(err) => { + debug!( + "after replayed into bank, instruction {:?} failed to update cost, err: {}", + program_id, err + ); + } + } + } + } + debug!( + "after replayed into bank, updated cost model instruction cost table, current values: {:?}", + cost_model.read().unwrap().get_instruction_cost_table() + ); + dirty + } + + fn persist_cost_table(blockstore: &Blockstore, cost_model: &RwLock) { + let cost_model_read = cost_model.read().unwrap(); + let cost_table = cost_model_read.get_instruction_cost_table(); + let db_records = blockstore.read_program_costs().expect("read programs"); + + // delete records from blockstore if they are no longer in cost_table + db_records.iter().for_each(|(pubkey, _)| { + if cost_table.get(pubkey).is_none() { + blockstore + .delete_program_cost(pubkey) + .expect("delete old program"); + } + }); + + for (key, cost) in cost_table.iter() { + blockstore + .write_program_cost(key, cost) + .expect("persist program costs to blockstore"); + } + } +} + +#[cfg(test)] +mod tests { + use super::*; + use solana_runtime::message_processor::ProgramTiming; + use solana_sdk::pubkey::Pubkey; + + #[test] + fn test_update_cost_model_with_empty_execute_timings() { + let cost_model = Arc::new(RwLock::new(CostModel::default())); + let empty_execute_timings = ExecuteTimings::default(); + CostUpdateService::update_cost_model(&cost_model, &empty_execute_timings); + + assert_eq!( + 0, + cost_model + .read() + .unwrap() + .get_instruction_cost_table() + .len() + ); + } + + #[test] + fn test_update_cost_model_with_execute_timings() { + let cost_model = Arc::new(RwLock::new(CostModel::default())); + let mut execute_timings = ExecuteTimings::default(); + + let program_key_1 = Pubkey::new_unique(); + let mut expected_cost: u64; + + // add new program + { + let accumulated_us: u64 = 1000; + let accumulated_units: u64 = 100; + let count: u32 = 10; + expected_cost = accumulated_units / count as u64; + + execute_timings.details.per_program_timings.insert( + program_key_1, + ProgramTiming { + accumulated_us, + accumulated_units, + count, + }, + ); + CostUpdateService::update_cost_model(&cost_model, &execute_timings); + assert_eq!( + 1, + cost_model + .read() + .unwrap() + .get_instruction_cost_table() + .len() + ); + assert_eq!( + Some(&expected_cost), + cost_model + .read() + .unwrap() + .get_instruction_cost_table() + .get(&program_key_1) + ); + } + + // update program + { + let accumulated_us: u64 = 2000; + let accumulated_units: u64 = 200; + let count: u32 = 10; + // to expect new cost is Average(new_value, existing_value) + expected_cost = ((accumulated_units / count as u64) + expected_cost) / 2; + + execute_timings.details.per_program_timings.insert( + program_key_1, + ProgramTiming { + accumulated_us, + accumulated_units, + count, + }, + ); + CostUpdateService::update_cost_model(&cost_model, &execute_timings); + assert_eq!( + 1, + cost_model + .read() + .unwrap() + .get_instruction_cost_table() + .len() + ); + assert_eq!( + Some(&expected_cost), + cost_model + .read() + .unwrap() + .get_instruction_cost_table() + .get(&program_key_1) + ); + } + } +} diff --git a/core/src/execute_cost_table.rs b/core/src/execute_cost_table.rs new file mode 100644 index 00000000000000..a37ce1ce35e11b --- /dev/null +++ b/core/src/execute_cost_table.rs @@ -0,0 +1,279 @@ +/// ExecuteCostTable is aggregated by Cost Model, it keeps each program's +/// average cost in its HashMap, with fixed capacity to avoid from growing +/// unchecked. +/// When its capacity limit is reached, it prunes old and less-used programs +/// to make room for new ones. +use log::*; +use solana_sdk::pubkey::Pubkey; +use std::{collections::HashMap, time::SystemTime}; + +// prune is rather expensive op, free up bulk space in each operation +// would be more efficient. PRUNE_RATIO defines the after prune table +// size will be original_size * PRUNE_RATIO. +const PRUNE_RATIO: f64 = 0.75; +// with 50_000 TPS as norm, weights occurrences '100' per microsec +const OCCURRENCES_WEIGHT: i64 = 100; + +const DEFAULT_CAPACITY: usize = 1024; + +#[derive(Debug)] +pub struct ExecuteCostTable { + capacity: usize, + table: HashMap, + occurrences: HashMap, +} + +impl Default for ExecuteCostTable { + fn default() -> Self { + ExecuteCostTable::new(DEFAULT_CAPACITY) + } +} + +impl ExecuteCostTable { + pub fn new(cap: usize) -> Self { + Self { + capacity: cap, + table: HashMap::with_capacity(cap), + occurrences: HashMap::with_capacity(cap), + } + } + + pub fn get_cost_table(&self) -> &HashMap { + &self.table + } + + pub fn get_count(&self) -> usize { + self.table.len() + } + + // instead of assigning unknown program with a configured/hard-coded cost + // use average or mode function to make a educated guess. + pub fn get_average(&self) -> u64 { + if self.table.is_empty() { + 0 + } else { + self.table.iter().map(|(_, value)| value).sum::() / self.get_count() as u64 + } + } + + pub fn get_mode(&self) -> u64 { + if self.occurrences.is_empty() { + 0 + } else { + let key = self + .occurrences + .iter() + .max_by_key(|&(_, count)| count) + .map(|(key, _)| key) + .expect("cannot find mode from cost table"); + + *self.table.get(&key).unwrap() + } + } + + // returns None if program doesn't exist in table. In this case, + // client is advised to call `get_average()` or `get_mode()` to + // assign a 'default' value for new program. + pub fn get_cost(&self, key: &Pubkey) -> Option<&u64> { + self.table.get(&key) + } + + pub fn upsert(&mut self, key: &Pubkey, value: u64) -> Option { + let need_to_add = self.table.get(key).is_none(); + let current_size = self.get_count(); + if current_size == self.capacity && need_to_add { + self.prune_to(&((current_size as f64 * PRUNE_RATIO) as usize)); + } + + let program_cost = self.table.entry(*key).or_insert(value); + *program_cost = (*program_cost + value) / 2; + + let (count, timestamp) = self + .occurrences + .entry(*key) + .or_insert((0, SystemTime::now())); + *count += 1; + *timestamp = SystemTime::now(); + + Some(*program_cost) + } + + // prune the old programs so the table contains `new_size` of records, + // where `old` is defined as weighted age, which is negatively correlated + // with program's age and + // positively correlated with how frequently the program + // is executed (eg. occurrence), + fn prune_to(&mut self, new_size: &usize) { + debug!( + "prune cost table, current size {}, new size {}", + self.get_count(), + new_size + ); + + if *new_size == self.get_count() { + return; + } + + if *new_size == 0 { + self.table.clear(); + self.occurrences.clear(); + return; + } + + let now = SystemTime::now(); + let mut sorted_by_weighted_age: Vec<_> = self + .occurrences + .iter() + .map(|(key, (count, timestamp))| { + let age = now.duration_since(*timestamp).unwrap().as_micros(); + let weighted_age = *count as i64 * OCCURRENCES_WEIGHT + -(age as i64); + (weighted_age, *key) + }) + .collect(); + sorted_by_weighted_age.sort_by(|x, y| x.0.partial_cmp(&y.0).unwrap()); + + for i in sorted_by_weighted_age.iter() { + self.table.remove(&i.1); + self.occurrences.remove(&i.1); + if *new_size == self.get_count() { + break; + } + } + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_execute_cost_table_prune_simple_table() { + solana_logger::setup(); + let capacity: usize = 3; + let mut testee = ExecuteCostTable::new(capacity); + + let key1 = Pubkey::new_unique(); + let key2 = Pubkey::new_unique(); + let key3 = Pubkey::new_unique(); + + testee.upsert(&key1, 1); + testee.upsert(&key2, 2); + testee.upsert(&key3, 3); + + testee.prune_to(&(capacity - 1)); + + // the oldest, key1, should be pruned + assert!(testee.get_cost(&key1).is_none()); + assert!(testee.get_cost(&key2).is_some()); + assert!(testee.get_cost(&key2).is_some()); + } + + #[test] + fn test_execute_cost_table_prune_weighted_table() { + solana_logger::setup(); + let capacity: usize = 3; + let mut testee = ExecuteCostTable::new(capacity); + + let key1 = Pubkey::new_unique(); + let key2 = Pubkey::new_unique(); + let key3 = Pubkey::new_unique(); + + testee.upsert(&key1, 1); + testee.upsert(&key1, 1); + testee.upsert(&key2, 2); + testee.upsert(&key3, 3); + + testee.prune_to(&(capacity - 1)); + + // the oldest, key1, has 2 counts; 2nd oldest Key2 has 1 count; + // expect key2 to be pruned. + assert!(testee.get_cost(&key1).is_some()); + assert!(testee.get_cost(&key2).is_none()); + assert!(testee.get_cost(&key3).is_some()); + } + + #[test] + fn test_execute_cost_table_upsert_within_capacity() { + solana_logger::setup(); + let mut testee = ExecuteCostTable::default(); + + let key1 = Pubkey::new_unique(); + let key2 = Pubkey::new_unique(); + let cost1: u64 = 100; + let cost2: u64 = 110; + + // query empty table + assert!(testee.get_cost(&key1).is_none()); + + // insert one record + testee.upsert(&key1, cost1); + assert_eq!(1, testee.get_count()); + assert_eq!(cost1, testee.get_average()); + assert_eq!(cost1, testee.get_mode()); + assert_eq!(&cost1, testee.get_cost(&key1).unwrap()); + + // insert 2nd record + testee.upsert(&key2, cost2); + assert_eq!(2, testee.get_count()); + assert_eq!((cost1 + cost2) / 2_u64, testee.get_average()); + assert_eq!(cost2, testee.get_mode()); + assert_eq!(&cost1, testee.get_cost(&key1).unwrap()); + assert_eq!(&cost2, testee.get_cost(&key2).unwrap()); + + // update 1st record + testee.upsert(&key1, cost2); + assert_eq!(2, testee.get_count()); + assert_eq!(((cost1 + cost2) / 2 + cost2) / 2, testee.get_average()); + assert_eq!((cost1 + cost2) / 2, testee.get_mode()); + assert_eq!(&((cost1 + cost2) / 2), testee.get_cost(&key1).unwrap()); + assert_eq!(&cost2, testee.get_cost(&key2).unwrap()); + } + + #[test] + fn test_execute_cost_table_upsert_exceeds_capacity() { + solana_logger::setup(); + let capacity: usize = 2; + let mut testee = ExecuteCostTable::new(capacity); + + let key1 = Pubkey::new_unique(); + let key2 = Pubkey::new_unique(); + let key3 = Pubkey::new_unique(); + let key4 = Pubkey::new_unique(); + let cost1: u64 = 100; + let cost2: u64 = 110; + let cost3: u64 = 120; + let cost4: u64 = 130; + + // insert one record + testee.upsert(&key1, cost1); + assert_eq!(1, testee.get_count()); + assert_eq!(&cost1, testee.get_cost(&key1).unwrap()); + + // insert 2nd record + testee.upsert(&key2, cost2); + assert_eq!(2, testee.get_count()); + assert_eq!(&cost1, testee.get_cost(&key1).unwrap()); + assert_eq!(&cost2, testee.get_cost(&key2).unwrap()); + + // insert 3rd record, pushes out the oldest (eg 1st) record + testee.upsert(&key3, cost3); + assert_eq!(2, testee.get_count()); + assert_eq!((cost2 + cost3) / 2_u64, testee.get_average()); + assert_eq!(cost3, testee.get_mode()); + assert!(testee.get_cost(&key1).is_none()); + assert_eq!(&cost2, testee.get_cost(&key2).unwrap()); + assert_eq!(&cost3, testee.get_cost(&key3).unwrap()); + + // update 2nd record, so the 3rd becomes the oldest + // add 4th record, pushes out 3rd key + testee.upsert(&key2, cost1); + testee.upsert(&key4, cost4); + assert_eq!(((cost1 + cost2) / 2 + cost4) / 2_u64, testee.get_average()); + assert_eq!((cost1 + cost2) / 2, testee.get_mode()); + assert_eq!(2, testee.get_count()); + assert!(testee.get_cost(&key1).is_none()); + assert_eq!(&((cost1 + cost2) / 2), testee.get_cost(&key2).unwrap()); + assert!(testee.get_cost(&key3).is_none()); + assert_eq!(&cost4, testee.get_cost(&key4).unwrap()); + } +} diff --git a/core/src/lib.rs b/core/src/lib.rs index 88fa14dd07a125..270d58b63dbe06 100644 --- a/core/src/lib.rs +++ b/core/src/lib.rs @@ -19,6 +19,11 @@ pub mod cluster_slots_service; pub mod commitment_service; pub mod completed_data_sets_service; pub mod consensus; +pub mod cost_model; +pub mod cost_tracker; +pub mod cost_tracker_stats; +pub mod cost_update_service; +pub mod execute_cost_table; pub mod fetch_stage; pub mod fork_choice; pub mod gen_keys; diff --git a/core/src/progress_map.rs b/core/src/progress_map.rs index 36348ca534090a..bb8728870f3816 100644 --- a/core/src/progress_map.rs +++ b/core/src/progress_map.rs @@ -114,6 +114,43 @@ impl ReplaySlotStats { i64 ), ); + + let mut per_pubkey_timings: Vec<_> = self + .execute_timings + .details + .per_program_timings + .iter() + .collect(); + per_pubkey_timings.sort_by(|a, b| b.1.accumulated_us.cmp(&a.1.accumulated_us)); + let (total_us, total_units, total_count) = + per_pubkey_timings + .iter() + .fold((0, 0, 0), |(sum_us, sum_units, sum_count), a| { + ( + sum_us + a.1.accumulated_us, + sum_units + a.1.accumulated_units, + sum_count + a.1.count, + ) + }); + + for (pubkey, time) in per_pubkey_timings.iter().take(5) { + datapoint_info!( + "per_program_timings", + ("slot", slot as i64, i64), + ("pubkey", pubkey.to_string(), String), + ("execute_us", time.accumulated_us, i64), + ("accumulated_units", time.accumulated_units, i64), + ("count", time.count, i64) + ); + } + datapoint_info!( + "per_program_timings", + ("slot", slot as i64, i64), + ("pubkey", "all", String), + ("execute_us", total_us, i64), + ("accumulated_units", total_units, i64), + ("count", total_count, i64) + ); } } diff --git a/core/src/replay_stage.rs b/core/src/replay_stage.rs index da395b6a4738cb..0c384eccc63dd1 100644 --- a/core/src/replay_stage.rs +++ b/core/src/replay_stage.rs @@ -18,7 +18,6 @@ use crate::{ latest_validator_votes_for_frozen_banks::LatestValidatorVotesForFrozenBanks, progress_map::{ForkProgress, ProgressMap, PropagatedStats}, repair_service::DuplicateSlotsResetReceiver, - result::Result, rewards_recorder_service::RewardsRecorderSender, unfrozen_gossip_verified_vote_hashes::UnfrozenGossipVerifiedVoteHashes, voting_service::VoteOp, @@ -42,7 +41,7 @@ use solana_rpc::{ }; use solana_runtime::{ accounts_background_service::AbsRequestSender, - bank::{Bank, NewBankOptions}, + bank::{Bank, ExecuteTimings, NewBankOptions}, bank_forks::BankForks, commitment::BlockCommitmentCache, vote_sender_types::ReplayVoteSender, @@ -281,7 +280,7 @@ impl ReplayTiming { "process_duplicate_slots_elapsed", self.process_duplicate_slots_elapsed as i64, i64 - ) + ), ); *self = ReplayTiming::default(); @@ -291,7 +290,7 @@ impl ReplayTiming { } pub struct ReplayStage { - t_replay: JoinHandle>, + t_replay: JoinHandle<()>, commitment_service: AggregateCommitmentService, } @@ -315,6 +314,7 @@ impl ReplayStage { gossip_verified_vote_hash_receiver: GossipVerifiedVoteHashReceiver, cluster_slots_update_sender: ClusterSlotsUpdateSender, voting_sender: Sender, + cost_update_sender: Sender, ) -> Self { let ReplayStageConfig { my_pubkey, @@ -412,6 +412,7 @@ impl ReplayStage { &mut unfrozen_gossip_verified_vote_hashes, &mut latest_validator_votes_for_frozen_banks, &cluster_slots_update_sender, + &cost_update_sender, ); replay_active_banks_time.stop(); @@ -742,7 +743,6 @@ impl ReplayStage { process_duplicate_slots_time.as_us(), ); } - Ok(()) }) .unwrap(); @@ -1690,9 +1690,11 @@ impl ReplayStage { unfrozen_gossip_verified_vote_hashes: &mut UnfrozenGossipVerifiedVoteHashes, latest_validator_votes_for_frozen_banks: &mut LatestValidatorVotesForFrozenBanks, cluster_slots_update_sender: &ClusterSlotsUpdateSender, + cost_update_sender: &Sender, ) -> bool { let mut did_complete_bank = false; let mut tx_count = 0; + let mut execute_timings = ExecuteTimings::default(); let active_banks = bank_forks.read().unwrap().active_banks(); trace!("active banks {:?}", active_banks); @@ -1763,6 +1765,12 @@ impl ReplayStage { } assert_eq!(*bank_slot, bank.slot()); if bank.is_complete() { + execute_timings.accumulate(&bank_progress.replay_stats.execute_timings); + debug!("bank {} is completed replay from blockstore, contribute to update cost with {:?}", + bank.slot(), + bank_progress.replay_stats.execute_timings + ); + bank_progress.replay_stats.report_stats( bank.slot(), bank_progress.replay_progress.num_entries, @@ -1824,6 +1832,14 @@ impl ReplayStage { ); } } + + // send accumulated excute-timings to cost_update_service + if !execute_timings.details.per_program_timings.is_empty() { + cost_update_sender + .send(execute_timings) + .unwrap_or_else(|err| warn!("cost_update_sender failed: {:?}", err)); + } + inc_new_counter_info!("replay_stage-replay_transactions", tx_count); did_complete_bank } @@ -4929,7 +4945,6 @@ mod tests { ); assert_eq!(tower.last_voted_slot().unwrap(), 1); } - fn run_compute_and_select_forks( bank_forks: &RwLock, progress: &mut ProgressMap, diff --git a/core/src/sigverify_stage.rs b/core/src/sigverify_stage.rs index b31d39bcdac312..6fbb790851123c 100644 --- a/core/src/sigverify_stage.rs +++ b/core/src/sigverify_stage.rs @@ -8,13 +8,13 @@ use crate::sigverify; use crossbeam_channel::{SendError, Sender as CrossbeamSender}; use solana_measure::measure::Measure; -use solana_metrics::datapoint_debug; use solana_perf::packet::Packets; use solana_sdk::timing; use solana_streamer::streamer::{self, PacketReceiver, StreamerError}; use std::collections::HashMap; use std::sync::mpsc::{Receiver, RecvTimeoutError}; use std::thread::{self, Builder, JoinHandle}; +use std::time::Instant; use thiserror::Error; const MAX_SIGVERIFY_BATCH: usize = 10_000; @@ -41,6 +41,82 @@ pub trait SigVerifier { #[derive(Default, Clone)] pub struct DisabledSigVerifier {} +#[derive(Default)] +struct SigVerifierStats { + recv_batches_us_hist: histogram::Histogram, // time to call recv_batch + verify_batches_pp_us_hist: histogram::Histogram, // per-packet time to call verify_batch + batches_hist: histogram::Histogram, // number of Packets structures per verify call + packets_hist: histogram::Histogram, // number of packets per verify call + total_batches: usize, + total_packets: usize, +} + +impl SigVerifierStats { + fn report(&self) { + datapoint_info!( + "sigverify_stage-total_verify_time", + ( + "recv_batches_us_90pct", + self.recv_batches_us_hist.percentile(90.0).unwrap_or(0), + i64 + ), + ( + "recv_batches_us_min", + self.recv_batches_us_hist.minimum().unwrap_or(0), + i64 + ), + ( + "recv_batches_us_max", + self.recv_batches_us_hist.maximum().unwrap_or(0), + i64 + ), + ( + "recv_batches_us_mean", + self.recv_batches_us_hist.mean().unwrap_or(0), + i64 + ), + ( + "verify_batches_pp_us_90pct", + self.verify_batches_pp_us_hist.percentile(90.0).unwrap_or(0), + i64 + ), + ( + "verify_batches_pp_us_min", + self.verify_batches_pp_us_hist.minimum().unwrap_or(0), + i64 + ), + ( + "verify_batches_pp_us_max", + self.verify_batches_pp_us_hist.maximum().unwrap_or(0), + i64 + ), + ( + "verify_batches_pp_us_mean", + self.verify_batches_pp_us_hist.mean().unwrap_or(0), + i64 + ), + ( + "batches_90pct", + self.batches_hist.percentile(90.0).unwrap_or(0), + i64 + ), + ("batches_min", self.batches_hist.minimum().unwrap_or(0), i64), + ("batches_max", self.batches_hist.maximum().unwrap_or(0), i64), + ("batches_mean", self.batches_hist.mean().unwrap_or(0), i64), + ( + "packets_90pct", + self.packets_hist.percentile(90.0).unwrap_or(0), + i64 + ), + ("packets_min", self.packets_hist.minimum().unwrap_or(0), i64), + ("packets_max", self.packets_hist.maximum().unwrap_or(0), i64), + ("packets_mean", self.packets_hist.mean().unwrap_or(0), i64), + ("total_batches", self.total_batches, i64), + ("total_packets", self.total_packets, i64), + ); + } +} + impl SigVerifier for DisabledSigVerifier { fn verify_batch(&self, mut batch: Vec) -> Vec { sigverify::ed25519_verify_disabled(&mut batch); @@ -92,6 +168,7 @@ impl SigVerifyStage { recvr: &PacketReceiver, sendr: &CrossbeamSender>, verifier: &T, + stats: &mut SigVerifierStats, ) -> Result<()> { let (mut batches, len, recv_time) = streamer::recv_batch(recvr)?; @@ -121,6 +198,19 @@ impl SigVerifyStage { ("recv_time", recv_time, i64), ); + stats + .recv_batches_us_hist + .increment(recv_time as u64) + .unwrap(); + stats + .verify_batches_pp_us_hist + .increment(verify_batch_time.as_us() / (len as u64)) + .unwrap(); + stats.batches_hist.increment(batches_len as u64).unwrap(); + stats.packets_hist.increment(len as u64).unwrap(); + stats.total_batches += batches_len; + stats.total_packets += len; + Ok(()) } @@ -130,10 +220,14 @@ impl SigVerifyStage { verifier: &T, ) -> JoinHandle<()> { let verifier = verifier.clone(); + let mut stats = SigVerifierStats::default(); + let mut last_print = Instant::now(); Builder::new() .name("solana-verifier".to_string()) .spawn(move || loop { - if let Err(e) = Self::verifier(&packet_receiver, &verified_sender, &verifier) { + if let Err(e) = + Self::verifier(&packet_receiver, &verified_sender, &verifier, &mut stats) + { match e { SigVerifyServiceError::Streamer(StreamerError::RecvTimeout( RecvTimeoutError::Disconnected, @@ -147,6 +241,11 @@ impl SigVerifyStage { _ => error!("{:?}", e), } } + if last_print.elapsed().as_secs() > 2 { + stats.report(); + stats = SigVerifierStats::default(); + last_print = Instant::now(); + } }) .unwrap() } diff --git a/core/src/tpu.rs b/core/src/tpu.rs index dd3e5ae0d8b0f2..245e873eb91153 100644 --- a/core/src/tpu.rs +++ b/core/src/tpu.rs @@ -8,6 +8,8 @@ use crate::{ ClusterInfoVoteListener, GossipDuplicateConfirmedSlotsSender, GossipVerifiedVoteHashSender, VerifiedVoteSender, VoteTracker, }, + cost_model::CostModel, + cost_tracker::CostTracker, fetch_stage::FetchStage, sigverify::TransactionSigVerifier, sigverify_stage::SigVerifyStage, @@ -71,6 +73,7 @@ impl Tpu { bank_notification_sender: Option, tpu_coalesce_ms: u64, cluster_confirmed_slot_sender: GossipDuplicateConfirmedSlotsSender, + cost_model: &Arc>, ) -> Self { let (packet_sender, packet_receiver) = channel(); let (vote_packet_sender, vote_packet_receiver) = channel(); @@ -120,6 +123,7 @@ impl Tpu { cluster_confirmed_slot_sender, ); + let cost_tracker = Arc::new(RwLock::new(CostTracker::new(cost_model.clone()))); let banking_stage = BankingStage::new( cluster_info, poh_recorder, @@ -128,6 +132,7 @@ impl Tpu { verified_gossip_vote_packets_receiver, transaction_status_sender, replay_vote_sender, + cost_tracker, ); let broadcast_stage = broadcast_type.new_broadcast_stage( diff --git a/core/src/tvu.rs b/core/src/tvu.rs index 65f26a8835efb3..265b44f18c5637 100644 --- a/core/src/tvu.rs +++ b/core/src/tvu.rs @@ -12,6 +12,8 @@ use crate::{ cluster_slots::ClusterSlots, completed_data_sets_service::CompletedDataSetsSender, consensus::Tower, + cost_model::CostModel, + cost_update_service::CostUpdateService, ledger_cleanup_service::LedgerCleanupService, replay_stage::{ReplayStage, ReplayStageConfig}, retransmit_stage::RetransmitStage, @@ -38,6 +40,7 @@ use solana_runtime::{ AbsRequestHandler, AbsRequestSender, AccountsBackgroundService, SnapshotRequestHandler, }, accounts_db::AccountShrinkThreshold, + bank::ExecuteTimings, bank_forks::{BankForks, SnapshotConfig}, commitment::BlockCommitmentCache, vote_sender_types::ReplayVoteSender, @@ -52,7 +55,7 @@ use std::{ net::UdpSocket, sync::{ atomic::AtomicBool, - mpsc::{channel, Receiver}, + mpsc::{channel, Receiver, Sender}, Arc, Mutex, RwLock, }, thread, @@ -67,6 +70,7 @@ pub struct Tvu { accounts_background_service: AccountsBackgroundService, accounts_hash_verifier: AccountsHashVerifier, voting_service: VotingService, + cost_update_service: CostUpdateService, } pub struct Sockets { @@ -131,6 +135,7 @@ impl Tvu { gossip_confirmed_slots_receiver: GossipDuplicateConfirmedSlotsReceiver, tvu_config: TvuConfig, max_slots: &Arc, + cost_model: &Arc>, ) -> Self { let keypair: Arc = cluster_info.keypair.clone(); @@ -285,6 +290,17 @@ impl Tvu { bank_forks.clone(), ); + let (cost_update_sender, cost_update_receiver): ( + Sender, + Receiver, + ) = channel(); + let cost_update_service = CostUpdateService::new( + exit.clone(), + blockstore.clone(), + cost_model.clone(), + cost_update_receiver, + ); + let replay_stage = ReplayStage::new( replay_stage_config, blockstore.clone(), @@ -303,6 +319,7 @@ impl Tvu { gossip_verified_vote_hash_receiver, cluster_slots_update_sender, voting_sender, + cost_update_sender, ); let ledger_cleanup_service = tvu_config.max_ledger_shreds.map(|max_ledger_shreds| { @@ -334,6 +351,7 @@ impl Tvu { accounts_background_service, accounts_hash_verifier, voting_service, + cost_update_service, } } @@ -348,6 +366,7 @@ impl Tvu { self.replay_stage.join()?; self.accounts_hash_verifier.join()?; self.voting_service.join()?; + self.cost_update_service.join()?; Ok(()) } } @@ -455,6 +474,7 @@ pub mod tests { gossip_confirmed_slots_receiver, TvuConfig::default(), &Arc::new(MaxSlots::default()), + &Arc::new(RwLock::new(CostModel::default())), ); exit.store(true, Ordering::Relaxed); tvu.join().unwrap(); diff --git a/core/src/validator.rs b/core/src/validator.rs index d62625f4cc9c67..ccc74a9d7ff419 100644 --- a/core/src/validator.rs +++ b/core/src/validator.rs @@ -7,6 +7,7 @@ use { cluster_info_vote_listener::VoteTracker, completed_data_sets_service::CompletedDataSetsService, consensus::{reconcile_blockstore_roots_with_tower, Tower}, + cost_model::CostModel, rewards_recorder_service::{RewardsRecorderSender, RewardsRecorderService}, sample_performance_service::SamplePerformanceService, serve_repair::ServeRepair, @@ -681,6 +682,10 @@ impl Validator { bank_forks.read().unwrap().root_bank().deref(), )); + let mut cost_model = CostModel::default(); + cost_model.initialize_cost_table(&blockstore.read_program_costs().unwrap()); + let cost_model = Arc::new(RwLock::new(cost_model)); + let (retransmit_slots_sender, retransmit_slots_receiver) = unbounded(); let (verified_vote_sender, verified_vote_receiver) = unbounded(); let (gossip_verified_vote_hash_sender, gossip_verified_vote_hash_receiver) = unbounded(); @@ -758,6 +763,7 @@ impl Validator { disable_epoch_boundary_optimization: config.disable_epoch_boundary_optimization, }, &max_slots, + &cost_model, ); let tpu = Tpu::new( @@ -784,6 +790,7 @@ impl Validator { bank_notification_sender, config.tpu_coalesce_ms, cluster_confirmed_slot_sender, + &cost_model, ); datapoint_info!("validator-new", ("id", id.to_string(), String)); diff --git a/ledger-tool/Cargo.toml b/ledger-tool/Cargo.toml index 52ed6cf33636ab..98d5a04b903cb8 100644 --- a/ledger-tool/Cargo.toml +++ b/ledger-tool/Cargo.toml @@ -26,6 +26,7 @@ serde_json = "1.0.56" serde_yaml = "0.8.13" solana-clap-utils = { path = "../clap-utils", version = "=1.7.15" } solana-cli-output = { path = "../cli-output", version = "=1.7.15" } +solana-core = { path = "../core", version = "=1.7.15" } solana-ledger = { path = "../ledger", version = "=1.7.15" } solana-logger = { path = "../logger", version = "=1.7.15" } solana-measure = { path = "../measure", version = "=1.7.15" } diff --git a/ledger-tool/src/main.rs b/ledger-tool/src/main.rs index ad63139e679b33..a269afcc89c097 100644 --- a/ledger-tool/src/main.rs +++ b/ledger-tool/src/main.rs @@ -15,6 +15,9 @@ use solana_clap_utils::{ is_parsable, is_pubkey, is_pubkey_or_keypair, is_slot, is_valid_percentage, }, }; +use solana_core::cost_model::CostModel; +use solana_core::cost_tracker::CostTracker; +use solana_core::cost_tracker_stats::CostTrackerStats; use solana_ledger::entry::Entry; use solana_ledger::{ ancestor_iterator::AncestorIterator, @@ -727,6 +730,62 @@ fn load_bank_forks( ) } +fn compute_slot_cost(blockstore: &Blockstore, slot: Slot) -> Result<(), String> { + if blockstore.is_dead(slot) { + return Err("Dead slot".to_string()); + } + + let (entries, _num_shreds, _is_full) = blockstore + .get_slot_entries_with_shred_info(slot, 0, false) + .map_err(|err| format!(" Slot: {}, Failed to load entries, err {:?}", slot, err))?; + + let mut transactions = 0; + let mut programs = 0; + let mut program_ids = HashMap::new(); + let mut cost_model = CostModel::default(); + cost_model.initialize_cost_table(&blockstore.read_program_costs().unwrap()); + let cost_model = Arc::new(RwLock::new(cost_model)); + let mut cost_tracker = CostTracker::new(cost_model.clone()); + let mut cost_tracker_stats = CostTrackerStats::default(); + + for entry in &entries { + transactions += entry.transactions.len(); + let mut cost_model = cost_model.write().unwrap(); + for transaction in &entry.transactions { + programs += transaction.message().instructions.len(); + let tx_cost = cost_model.calculate_cost(transaction, true); + if cost_tracker + .try_add(tx_cost, &mut cost_tracker_stats) + .is_err() + { + println!( + "Slot: {}, CostModel rejected transaction {:?}, stats {:?}!", + slot, + transaction, + cost_tracker.get_stats() + ); + } + for instruction in &transaction.message().instructions { + let program_id = + transaction.message().account_keys[instruction.program_id_index as usize]; + *program_ids.entry(program_id).or_insert(0) += 1; + } + } + } + + println!( + "Slot: {}, Entries: {}, Transactions: {}, Programs {}, {:?}", + slot, + entries.len(), + transactions, + programs, + cost_tracker.get_stats() + ); + println!(" Programs: {:?}", program_ids); + + Ok(()) +} + fn open_genesis_config_by(ledger_path: &Path, matches: &ArgMatches<'_>) -> GenesisConfig { let max_genesis_archive_unpacked_size = value_t_or_exit!(matches, "max_genesis_archive_unpacked_size", u64); @@ -1414,6 +1473,20 @@ fn main() { .about("Output statistics in JSON format about \ all column families in the ledger rocksdb") ) + .subcommand( + SubCommand::with_name("compute-slot-cost") + .about("runs cost_model over the block at the given slots, \ + computes how expensive a block was based on cost_model") + .arg( + Arg::with_name("slots") + .index(1) + .value_name("SLOTS") + .validator(is_slot) + .multiple(true) + .takes_value(true) + .help("Slots that their blocks are computed for cost, default to all slots in ledger"), + ) + ) .get_matches(); info!("{} {}", crate_name!(), solana_version::version!()); @@ -2964,6 +3037,28 @@ fn main() { )); println!("Ok."); } + ("compute-slot-cost", Some(arg_matches)) => { + let blockstore = open_blockstore( + &ledger_path, + AccessType::TryPrimaryThenSecondary, + wal_recovery_mode, + ); + + let mut slots: Vec = vec![]; + if !arg_matches.is_present("slots") { + if let Ok(metas) = blockstore.slot_meta_iterator(0) { + slots = metas.map(|(slot, _)| slot).collect(); + } + } else { + slots = values_t_or_exit!(arg_matches, "slots", Slot); + } + + for slot in slots { + if let Err(err) = compute_slot_cost(&blockstore, slot) { + eprintln!("{}", err); + } + } + } ("", _) => { eprintln!("{}", matches.usage()); exit(1); diff --git a/ledger/src/block_cost_limits.rs b/ledger/src/block_cost_limits.rs new file mode 100644 index 00000000000000..0f263300d27410 --- /dev/null +++ b/ledger/src/block_cost_limits.rs @@ -0,0 +1,56 @@ +//! defines block cost related limits +//! +use lazy_static::lazy_static; +use solana_sdk::{ + feature, incinerator, native_loader, pubkey::Pubkey, secp256k1_program, system_program, +}; +use std::collections::HashMap; + +/// Static configurations: +/// +/// Number of microseconds replaying a block should take, 400 millisecond block times +/// is curerntly publicly communicated on solana.com +pub const MAX_BLOCK_REPLAY_TIME_US: u64 = 400_000; +/// number of concurrent processes, +pub const MAX_CONCURRENCY: u64 = 10; + +/// Cluster data, method of collecting at https://github.com/solana-labs/solana/issues/19627 +/// +/// cluster avergaed compute unit to microsec conversion rate +pub const COMPUTE_UNIT_TO_US_RATIO: u64 = 40; +/// Number of compute units for one signature verification. +pub const SIGNATURE_COST: u64 = COMPUTE_UNIT_TO_US_RATIO * 175; +/// Number of compute units for one write lock +pub const WRITE_LOCK_UNITS: u64 = COMPUTE_UNIT_TO_US_RATIO * 20; +/// Number of data bytes per compute units +pub const DATA_BYTES_UNITS: u64 = 220 /*bytes per us*/ / COMPUTE_UNIT_TO_US_RATIO; +// Number of compute units for each built-in programs +lazy_static! { + /// Number of compute units for each built-in programs + pub static ref BUILT_IN_INSTRUCTION_COSTS: HashMap = [ + (feature::id(), COMPUTE_UNIT_TO_US_RATIO * 2), + (incinerator::id(), COMPUTE_UNIT_TO_US_RATIO * 2), + (native_loader::id(), COMPUTE_UNIT_TO_US_RATIO * 2), + (solana_sdk::stake::config::id(), COMPUTE_UNIT_TO_US_RATIO * 2), + (solana_sdk::stake::program::id(), COMPUTE_UNIT_TO_US_RATIO * 50), + (solana_vote_program::id(), COMPUTE_UNIT_TO_US_RATIO * 200), + (secp256k1_program::id(), COMPUTE_UNIT_TO_US_RATIO * 4), + (system_program::id(), COMPUTE_UNIT_TO_US_RATIO * 15), + ] + .iter() + .cloned() + .collect(); +} + +/// Statically computed data: +/// +/// Number of compute units that a block is allowed. A block's compute units are +/// accumualted by Transactions added to it; A transaction's compute units are +/// calculated by cost_model, based on transaction's signarures, write locks, +/// data size and built-in and BPF instructinos. +pub const MAX_BLOCK_UNITS: u64 = + MAX_BLOCK_REPLAY_TIME_US * COMPUTE_UNIT_TO_US_RATIO * MAX_CONCURRENCY; +/// Number of compute units that a writable account in a block is allowed. The +/// limit is to prevent too many transactions write to same account, threrefore +/// reduce block's paralellism. +pub const MAX_WRITABLE_ACCOUNT_UNITS: u64 = MAX_BLOCK_REPLAY_TIME_US * COMPUTE_UNIT_TO_US_RATIO; diff --git a/ledger/src/blockstore.rs b/ledger/src/blockstore.rs index 7c1138dac883da..2d613e1cc178ba 100644 --- a/ledger/src/blockstore.rs +++ b/ledger/src/blockstore.rs @@ -2701,6 +2701,26 @@ impl Blockstore { self.perf_samples_cf.put(index, perf_sample) } + pub fn read_program_costs(&self) -> Result> { + Ok(self + .db + .iter::(IteratorMode::End)? + .map(|(pubkey, data)| { + let program_cost: ProgramCost = deserialize(&data).unwrap(); + (pubkey, program_cost.cost) + }) + .collect()) + } + + pub fn write_program_cost(&self, key: &Pubkey, value: &u64) -> Result<()> { + self.program_costs_cf + .put(*key, &ProgramCost { cost: *value }) + } + + pub fn delete_program_cost(&self, key: &Pubkey) -> Result<()> { + self.program_costs_cf.delete(*key) + } + /// Returns the entry vector for the slot starting with `shred_start_index` pub fn get_slot_entries(&self, slot: Slot, shred_start_index: u64) -> Result> { self.get_slot_entries_with_shred_info(slot, shred_start_index, false) @@ -8871,4 +8891,126 @@ pub mod tests { Blockstore::destroy(&blockstore_path).expect("Expected successful database destruction"); } + + #[test] + fn test_read_write_cost_table() { + let blockstore_path = get_tmp_ledger_path!(); + { + let blockstore = Blockstore::open(&blockstore_path).unwrap(); + let num_entries: usize = 10; + let mut cost_table: HashMap = HashMap::new(); + for x in 1..num_entries + 1 { + cost_table.insert(Pubkey::new_unique(), (x + 100) as u64); + } + + // write to db + for (key, cost) in cost_table.iter() { + blockstore + .write_program_cost(key, cost) + .expect("write a program"); + } + + // read back from db + let read_back = blockstore.read_program_costs().expect("read programs"); + // verify + assert_eq!(read_back.len(), cost_table.len()); + for (read_key, read_cost) in read_back { + assert_eq!(read_cost, *cost_table.get(&read_key).unwrap()); + } + + // update value, write to db + for val in cost_table.values_mut() { + *val += 100; + } + for (key, cost) in cost_table.iter() { + blockstore + .write_program_cost(key, cost) + .expect("write a program"); + } + // add a new record + let new_program_key = Pubkey::new_unique(); + let new_program_cost = 999; + blockstore + .write_program_cost(&new_program_key, &new_program_cost) + .unwrap(); + + // confirm value updated + let read_back = blockstore.read_program_costs().expect("read programs"); + // verify + assert_eq!(read_back.len(), cost_table.len() + 1); + for (key, cost) in cost_table.iter() { + assert_eq!(*cost, read_back.iter().find(|(k, _v)| k == key).unwrap().1); + } + assert_eq!( + new_program_cost, + read_back + .iter() + .find(|(k, _v)| *k == new_program_key) + .unwrap() + .1 + ); + + // test delete + blockstore + .delete_program_cost(&new_program_key) + .expect("delete a progrma"); + let read_back = blockstore.read_program_costs().expect("read programs"); + // verify + assert_eq!(read_back.len(), cost_table.len()); + for (read_key, read_cost) in read_back { + assert_eq!(read_cost, *cost_table.get(&read_key).unwrap()); + } + } + Blockstore::destroy(&blockstore_path).expect("Expected successful database destruction"); + } + + #[test] + fn test_delete_old_records_from_cost_table() { + let blockstore_path = get_tmp_ledger_path!(); + { + let blockstore = Blockstore::open(&blockstore_path).unwrap(); + let num_entries: usize = 10; + let mut cost_table: HashMap = HashMap::new(); + for x in 1..num_entries + 1 { + cost_table.insert(Pubkey::new_unique(), (x + 100) as u64); + } + + // write to db + for (key, cost) in cost_table.iter() { + blockstore + .write_program_cost(key, cost) + .expect("write a program"); + } + + // remove a record + let mut removed_key = Pubkey::new_unique(); + for (key, cost) in cost_table.iter() { + if *cost == 101_u64 { + removed_key = *key; + break; + } + } + cost_table.remove(&removed_key); + + // delete records from blockstore if they are no longer in cost_table + let db_records = blockstore.read_program_costs().expect("read programs"); + db_records.iter().for_each(|(pubkey, _)| { + if !cost_table.iter().any(|(key, _)| key == pubkey) { + assert_eq!(*pubkey, removed_key); + blockstore + .delete_program_cost(pubkey) + .expect("delete old program"); + } + }); + + // read back from db + let read_back = blockstore.read_program_costs().expect("read programs"); + // verify + assert_eq!(read_back.len(), cost_table.len()); + for (read_key, read_cost) in read_back { + assert_eq!(read_cost, *cost_table.get(&read_key).unwrap()); + } + } + Blockstore::destroy(&blockstore_path).expect("Expected successful database destruction"); + } } diff --git a/ledger/src/blockstore_db.rs b/ledger/src/blockstore_db.rs index 5ad7f46dcc9a21..46b97e121e7479 100644 --- a/ledger/src/blockstore_db.rs +++ b/ledger/src/blockstore_db.rs @@ -537,6 +537,11 @@ impl Rocks { Ok(()) } + fn delete_cf(&self, cf: &ColumnFamily, key: &[u8]) -> Result<()> { + self.0.delete_cf(cf, key)?; + Ok(()) + } + fn iterator_cf(&self, cf: &ColumnFamily, iterator_mode: IteratorMode) -> DBIterator where C: Column, @@ -1217,6 +1222,10 @@ where self.backend .put_cf(self.handle(), &C::key(key), &serialized_value) } + + pub fn delete(&self, key: C::Index) -> Result<()> { + self.backend.delete_cf(self.handle(), &C::key(key)) + } } impl LedgerColumn @@ -1364,7 +1373,7 @@ fn get_cf_options( options.set_max_bytes_for_level_base(total_size_base); options.set_target_file_size_base(file_size_base); - // TransactionStatusIndex must be excluded from LedgerCleanupService's rocksdb + // TransactionStatusIndex and ProgramCosts must be excluded from LedgerCleanupService's rocksdb // compactions.... if matches!(access_type, AccessType::PrimaryOnly) && !excludes_from_compaction(C::NAME) { options.set_compaction_filter_factory(PurgedSlotFilterFactory:: { diff --git a/ledger/src/blockstore_processor.rs b/ledger/src/blockstore_processor.rs index b1799d52f0e390..daf4d183069492 100644 --- a/ledger/src/blockstore_processor.rs +++ b/ledger/src/blockstore_processor.rs @@ -1,4 +1,5 @@ use crate::{ + block_cost_limits::*, block_error::BlockError, blockstore::Blockstore, blockstore_db::BlockstoreError, @@ -32,6 +33,7 @@ use solana_runtime::{ }; use solana_sdk::{ clock::{Slot, MAX_PROCESSING_AGE}, + feature_set, genesis_config::GenesisConfig, hash::Hash, pubkey::Pubkey, @@ -48,11 +50,40 @@ use std::{ collections::{HashMap, HashSet}, path::PathBuf, result, - sync::Arc, + sync::{Arc, RwLock}, time::{Duration, Instant}, }; use thiserror::Error; +// it tracks the block cost available capacity - number of compute-units allowed +// by max blockl cost limit +#[derive(Debug)] +pub struct BlockCostCapacityMeter { + pub capacity: u64, + pub accumulated_cost: u64, +} + +impl Default for BlockCostCapacityMeter { + fn default() -> Self { + BlockCostCapacityMeter::new(MAX_BLOCK_UNITS) + } +} + +impl BlockCostCapacityMeter { + pub fn new(capacity_limit: u64) -> Self { + Self { + capacity: capacity_limit, + accumulated_cost: 0_u64, + } + } + + // return the remaining capacity + pub fn accumulate(&mut self, cost: u64) -> u64 { + self.accumulated_cost += cost; + self.capacity.saturating_sub(self.accumulated_cost) + } +} + pub type BlockstoreProcessorResult = result::Result<(BankForks, LeaderScheduleCache), BlockstoreProcessorError>; @@ -100,12 +131,26 @@ fn get_first_error( first_err } +fn aggregate_total_execution_units(execute_timings: &ExecuteTimings) -> u64 { + let mut execute_cost_units: u64 = 0; + for (program_id, timing) in &execute_timings.details.per_program_timings { + if timing.count < 1 { + continue; + } + execute_cost_units = + execute_cost_units.saturating_add(timing.accumulated_units / timing.count as u64); + trace!("aggregated execution cost of {:?} {:?}", program_id, timing); + } + execute_cost_units +} + fn execute_batch( batch: &TransactionBatch, bank: &Arc, transaction_status_sender: Option<&TransactionStatusSender>, replay_vote_sender: Option<&ReplayVoteSender>, timings: &mut ExecuteTimings, + cost_capacity_meter: Arc>, ) -> Result<()> { let record_token_balances = transaction_status_sender.is_some(); @@ -117,6 +162,8 @@ fn execute_batch( vec![] }; + let pre_process_units: u64 = aggregate_total_execution_units(timings); + let (tx_results, balances, inner_instructions, transaction_logs) = batch.bank().load_execute_and_commit_transactions( batch, @@ -127,6 +174,29 @@ fn execute_batch( timings, ); + if bank + .feature_set + .is_active(&feature_set::gate_large_block::id()) + { + let execution_cost_units = aggregate_total_execution_units(timings) - pre_process_units; + let remaining_block_cost_cap = cost_capacity_meter + .write() + .unwrap() + .accumulate(execution_cost_units); + + debug!( + "bank {} executed a batch, number of transactions {}, total execute cu {}, remaining block cost cap {}", + bank.slot(), + batch.hashed_transactions().len(), + execution_cost_units, + remaining_block_cost_cap, + ); + + if remaining_block_cost_cap == 0_u64 { + return Err(TransactionError::WouldExceedMaxBlockCostLimit); + } + } + bank_utils::find_and_send_votes(batch.hashed_transactions(), &tx_results, replay_vote_sender); let TransactionResults { @@ -170,6 +240,7 @@ fn execute_batches( transaction_status_sender: Option<&TransactionStatusSender>, replay_vote_sender: Option<&ReplayVoteSender>, timings: &mut ExecuteTimings, + cost_capacity_meter: Arc>, ) -> Result<()> { inc_new_counter_debug!("bank-par_execute_entries-count", batches.len()); let (results, new_timings): (Vec>, Vec) = @@ -185,6 +256,7 @@ fn execute_batches( transaction_status_sender, replay_vote_sender, &mut timings, + cost_capacity_meter.clone(), ); if let Some(entry_callback) = entry_callback { entry_callback(bank); @@ -226,6 +298,7 @@ pub fn process_entries( transaction_status_sender, replay_vote_sender, &mut timings, + Arc::new(RwLock::new(BlockCostCapacityMeter::default())), ); debug!("process_entries: {:?}", timings); @@ -241,6 +314,7 @@ fn process_entries_with_callback( transaction_status_sender: Option<&TransactionStatusSender>, replay_vote_sender: Option<&ReplayVoteSender>, timings: &mut ExecuteTimings, + cost_capacity_meter: Arc>, ) -> Result<()> { // accumulator for entries that can be processed in parallel let mut batches = vec![]; @@ -262,6 +336,7 @@ fn process_entries_with_callback( transaction_status_sender, replay_vote_sender, timings, + cost_capacity_meter.clone(), )?; batches.clear(); for hash in &tick_hashes { @@ -313,6 +388,7 @@ fn process_entries_with_callback( transaction_status_sender, replay_vote_sender, timings, + cost_capacity_meter.clone(), )?; batches.clear(); } @@ -327,6 +403,7 @@ fn process_entries_with_callback( transaction_status_sender, replay_vote_sender, timings, + cost_capacity_meter, )?; for hash in tick_hashes { bank.register_tick(hash); @@ -806,6 +883,7 @@ pub fn confirm_slot( let mut entries = check_result.unwrap(); let mut replay_elapsed = Measure::start("replay_elapsed"); let mut execute_timings = ExecuteTimings::default(); + let cost_capacity_meter = Arc::new(RwLock::new(BlockCostCapacityMeter::default())); // Note: This will shuffle entries' transactions in-place. let process_result = process_entries_with_callback( bank, @@ -815,6 +893,7 @@ pub fn confirm_slot( transaction_status_sender, replay_vote_sender, &mut execute_timings, + cost_capacity_meter, ) .map_err(BlockstoreProcessorError::from); replay_elapsed.stop(); diff --git a/ledger/src/lib.rs b/ledger/src/lib.rs index 1c629f99c870dc..c46d9576f5e775 100644 --- a/ledger/src/lib.rs +++ b/ledger/src/lib.rs @@ -11,6 +11,7 @@ pub mod block_error; #[macro_use] pub mod blockstore; pub mod ancestor_iterator; +pub mod block_cost_limits; pub mod blockstore_db; pub mod blockstore_meta; pub mod blockstore_processor; diff --git a/programs/bpf/Cargo.lock b/programs/bpf/Cargo.lock index c1ea0c82a9f9a5..863546877638cc 100644 --- a/programs/bpf/Cargo.lock +++ b/programs/bpf/Cargo.lock @@ -83,9 +83,9 @@ checksum = "eab1c04a571841102f5345a8fc0f6bb3d31c315dec879b5c6e42e40ce7ffa34e" [[package]] name = "assert_matches" -version = "1.4.0" +version = "1.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "695579f0f2520f3774bb40461e5adb066459d4e0af4d59d20175484fb8e9edf1" +checksum = "9b34d609dfbaf33d6889b2b7106d3ca345eacad44200913df5ba02bfd31d2ba9" [[package]] name = "async-trait" @@ -155,11 +155,10 @@ checksum = "904dfeac50f3cdaba28fc6f57fdcddb75f49ed61346676a78c4ffe55877802fd" [[package]] name = "bincode" -version = "1.3.1" +version = "1.3.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f30d3a39baa26f9651f17b375061f3233dde33424a8b72b0dbe93a68a0bc896d" +checksum = "b1f45e9417d87227c7a56d22e471c6206462cba514c7590c09aff4cf6d1ddcad" dependencies = [ - "byteorder 1.3.4", "serde", ] @@ -272,6 +271,12 @@ version = "0.3.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "476e9cd489f9e121e02ffa6014a8ef220ecb15c05ed23fc34cca13925dc283fb" +[[package]] +name = "bs58" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "771fe0050b883fcc3ea2359b1a96bcfbc090b7116eae7c3c512c7a083fdf23d3" + [[package]] name = "bumpalo" version = "3.3.0" @@ -2597,7 +2602,7 @@ dependencies = [ "Inflector", "base64 0.12.3", "bincode", - "bs58", + "bs58 0.3.1", "bv", "lazy_static", "serde", @@ -3035,7 +3040,7 @@ version = "1.7.15" dependencies = [ "base64 0.13.0", "bincode", - "bs58", + "bs58 0.3.1", "clap", "indicatif", "jsonrpc-core", @@ -3061,6 +3066,13 @@ dependencies = [ "url", ] +[[package]] +name = "solana-compute-budget-program" +version = "1.7.15" +dependencies = [ + "solana-sdk", +] + [[package]] name = "solana-config-program" version = "1.7.15" @@ -3123,7 +3135,7 @@ version = "1.7.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b0b98d31e0662fedf3a1ee30919c655713874d578e19e65affe46109b1b927f9" dependencies = [ - "bs58", + "bs58 0.3.1", "bv", "generic-array 0.14.3", "log", @@ -3141,7 +3153,7 @@ dependencies = [ name = "solana-frozen-abi" version = "1.7.15" dependencies = [ - "bs58", + "bs58 0.3.1", "bv", "generic-array 0.14.3", "log", @@ -3248,7 +3260,7 @@ dependencies = [ "blake3", "borsh", "borsh-derive", - "bs58", + "bs58 0.3.1", "bv", "curve25519-dalek 2.1.0", "hex", @@ -3281,7 +3293,7 @@ dependencies = [ "blake3", "borsh", "borsh-derive", - "bs58", + "bs58 0.3.1", "bv", "curve25519-dalek 2.1.0", "hex", @@ -3388,6 +3400,7 @@ dependencies = [ "rustc_version", "serde", "serde_derive", + "solana-compute-budget-program", "solana-config-program", "solana-frozen-abi 1.7.15", "solana-frozen-abi-macro 1.7.15", @@ -3412,7 +3425,9 @@ version = "1.7.15" dependencies = [ "assert_matches", "bincode", - "bs58", + "borsh", + "borsh-derive", + "bs58 0.4.0", "bv", "byteorder 1.3.4", "chrono", @@ -3459,7 +3474,7 @@ version = "1.7.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "84710ce45a21cccd9f2b09d8e9aad529080bb2540f27b1253874b6e732b465b9" dependencies = [ - "bs58", + "bs58 0.3.1", "proc-macro2 1.0.24", "quote 1.0.6", "rustversion", @@ -3470,7 +3485,7 @@ dependencies = [ name = "solana-sdk-macro" version = "1.7.15" dependencies = [ - "bs58", + "bs58 0.3.1", "proc-macro2 1.0.24", "quote 1.0.6", "rustversion", @@ -3511,7 +3526,7 @@ dependencies = [ "Inflector", "base64 0.12.3", "bincode", - "bs58", + "bs58 0.3.1", "lazy_static", "serde", "serde_derive", diff --git a/programs/bpf/tests/programs.rs b/programs/bpf/tests/programs.rs index 2c965a3ede9253..93b7c85f98665f 100644 --- a/programs/bpf/tests/programs.rs +++ b/programs/bpf/tests/programs.rs @@ -34,6 +34,7 @@ use solana_sdk::{ bpf_loader, bpf_loader_deprecated, bpf_loader_upgradeable, client::SyncClient, clock::MAX_PROCESSING_AGE, + compute_budget, entrypoint::{MAX_PERMITTED_DATA_INCREASE, SUCCESS}, instruction::{AccountMeta, CompiledInstruction, Instruction, InstructionError}, keyed_account::KeyedAccount, @@ -1232,8 +1233,6 @@ fn test_program_bpf_call_depth() { solana_logger::setup(); - println!("Test program: solana_bpf_rust_call_depth"); - let GenesisConfigInfo { genesis_config, mint_keypair, @@ -1267,6 +1266,40 @@ fn test_program_bpf_call_depth() { assert!(result.is_err()); } +#[cfg(feature = "bpf_rust")] +#[test] +fn test_program_bpf_compute_budget() { + solana_logger::setup(); + + let GenesisConfigInfo { + genesis_config, + mint_keypair, + .. + } = create_genesis_config(50); + let mut bank = Bank::new(&genesis_config); + let (name, id, entrypoint) = solana_bpf_loader_program!(); + bank.add_builtin(&name, id, entrypoint); + let bank_client = BankClient::new(bank); + let program_id = load_bpf_program( + &bank_client, + &bpf_loader::id(), + &mint_keypair, + "solana_bpf_rust_noop", + ); + let message = Message::new( + &[ + compute_budget::request_units(1), + Instruction::new_with_bincode(program_id, &0, vec![]), + ], + Some(&mint_keypair.pubkey()), + ); + let result = bank_client.send_and_confirm_message(&[&mint_keypair], message); + assert_eq!( + result.unwrap_err().unwrap(), + TransactionError::InstructionError(1, InstructionError::ProgramFailedToComplete), + ); +} + #[test] fn assert_instruction_count() { solana_logger::setup(); diff --git a/programs/compute-budget/Cargo.toml b/programs/compute-budget/Cargo.toml new file mode 100644 index 00000000000000..03c6cbb6932471 --- /dev/null +++ b/programs/compute-budget/Cargo.toml @@ -0,0 +1,20 @@ +[package] +name = "solana-compute-budget-program" +description = "Solana Compute Budget program" +version = "1.7.15" +homepage = "https://solana.com/" +documentation = "https://docs.rs/solana-compute-budget-program" +repository = "https://github.com/solana-labs/solana" +authors = ["Solana Maintainers "] +license = "Apache-2.0" +edition = "2018" + +[dependencies] +solana-sdk = { path = "../../sdk", version = "=1.7.15" } + +[lib] +crate-type = ["lib"] +name = "solana_compute_budget_program" + +[package.metadata.docs.rs] +targets = ["x86_64-unknown-linux-gnu"] diff --git a/programs/compute-budget/src/lib.rs b/programs/compute-budget/src/lib.rs new file mode 100644 index 00000000000000..13b1fb9b04660b --- /dev/null +++ b/programs/compute-budget/src/lib.rs @@ -0,0 +1,12 @@ +use solana_sdk::{ + instruction::InstructionError, process_instruction::InvokeContext, pubkey::Pubkey, +}; + +pub fn process_instruction( + _program_id: &Pubkey, + _data: &[u8], + _invoke_context: &mut dyn InvokeContext, +) -> Result<(), InstructionError> { + // Do nothing, compute budget instructions handled by the runtime + Ok(()) +} diff --git a/runtime/Cargo.toml b/runtime/Cargo.toml index 814872523ee3f3..2c4496f7d1b24f 100644 --- a/runtime/Cargo.toml +++ b/runtime/Cargo.toml @@ -37,6 +37,7 @@ regex = "1.3.9" serde = { version = "1.0.122", features = ["rc"] } serde_derive = "1.0.103" solana-config-program = { path = "../programs/config", version = "=1.7.15" } +solana-compute-budget-program = { path = "../programs/compute-budget", version = "=1.7.15" } solana-frozen-abi = { path = "../frozen-abi", version = "=1.7.15" } solana-frozen-abi-macro = { path = "../frozen-abi/macro", version = "=1.7.15" } solana-logger = { path = "../logger", version = "=1.7.15" } diff --git a/runtime/src/bank.rs b/runtime/src/bank.rs index dafedf84a75037..d735570f09fede 100644 --- a/runtime/src/bank.rs +++ b/runtime/src/bank.rs @@ -81,23 +81,26 @@ use solana_sdk::{ INITIAL_RENT_EPOCH, MAX_PROCESSING_AGE, MAX_RECENT_BLOCKHASHES, MAX_TRANSACTION_FORWARDING_DELAY, SECONDS_PER_DAY, }, + compute_budget, epoch_info::EpochInfo, epoch_schedule::EpochSchedule, feature, - feature_set::{self, FeatureSet}, + feature_set::{self, tx_wide_compute_cap, FeatureSet}, fee_calculator::{FeeCalculator, FeeRateGovernor}, genesis_config::{ClusterType, GenesisConfig}, hard_forks::HardForks, hash::{extend_and_hash, hashv, Hash}, incinerator, inflation::Inflation, - instruction::CompiledInstruction, + instruction::{CompiledInstruction, InstructionError}, lamports::LamportsError, message::Message, native_loader, native_token::sol_to_lamports, nonce, nonce_account, - process_instruction::{BpfComputeBudget, Executor, ProcessInstructionWithContext}, + process_instruction::{ + BpfComputeBudget, ComputeMeter, Executor, ProcessInstructionWithContext, + }, program_utils::limited_deserialize, pubkey::Pubkey, recent_blockhashes_account, @@ -418,6 +421,28 @@ impl CachedExecutors { } } +pub struct TxComputeMeter { + remaining: u64, +} +impl TxComputeMeter { + pub fn new(cap: u64) -> Self { + Self { remaining: cap } + } +} +impl ComputeMeter for TxComputeMeter { + fn consume(&mut self, amount: u64) -> std::result::Result<(), InstructionError> { + let exceeded = self.remaining < amount; + self.remaining = self.remaining.saturating_sub(amount); + if exceeded { + return Err(InstructionError::ComputationalBudgetExceeded); + } + Ok(()) + } + fn get_remaining(&self) -> u64 { + self.remaining + } +} + #[derive(Default, Debug)] pub struct BankRc { /// where all the Accounts are stored @@ -3621,76 +3646,96 @@ impl Bank { Vec::with_capacity(hashed_txs.len()); let mut transaction_log_messages: Vec>> = Vec::with_capacity(hashed_txs.len()); - let bpf_compute_budget = self - .bpf_compute_budget - .unwrap_or_else(BpfComputeBudget::new); let executed: Vec = loaded_txs .iter_mut() .zip(hashed_txs.as_transactions_iter()) .map(|(accs, tx)| match accs { (Err(e), _nonce_rollback) => { - inner_instructions.push(None); transaction_log_messages.push(None); + inner_instructions.push(None); (Err(e.clone()), None) } (Ok(loaded_transaction), nonce_rollback) => { + let feature_set = self.feature_set.clone(); signature_count += u64::from(tx.message().header.num_required_signatures); - let executors = self.get_executors(&tx.message, &loaded_transaction.loaders); - let (account_refcells, loader_refcells) = Self::accounts_to_refcells( - &mut loaded_transaction.accounts, - &mut loaded_transaction.loaders, - ); + let mut bpf_compute_budget = self + .bpf_compute_budget + .unwrap_or_else(BpfComputeBudget::new); - let instruction_recorders = if enable_cpi_recording { - let ix_count = tx.message.instructions.len(); - let mut recorders = Vec::with_capacity(ix_count); - recorders.resize_with(ix_count, InstructionRecorder::default); - Some(recorders) + let mut process_result = if feature_set.is_active(&tx_wide_compute_cap::id()) { + compute_budget::process_request(&mut bpf_compute_budget, tx) } else { - None + Ok(()) }; - let log_collector = if enable_log_recording { - Some(Rc::new(LogCollector::default())) - } else { - None - }; + if process_result.is_ok() { + let executors = + self.get_executors(&tx.message, &loaded_transaction.loaders); - let mut process_result = self.message_processor.process_message( - tx.message(), - &loader_refcells, - &account_refcells, - &self.rent_collector, - log_collector.clone(), - executors.clone(), - instruction_recorders.as_deref(), - self.feature_set.clone(), - bpf_compute_budget, - &mut timings.details, - self.rc.accounts.clone(), - &self.ancestors, - ); + let (account_refcells, loader_refcells) = Self::accounts_to_refcells( + &mut loaded_transaction.accounts, + &mut loaded_transaction.loaders, + ); - transaction_log_messages.push(Self::collect_log_messages(log_collector)); - inner_instructions.push(Self::compile_recorded_instructions( - instruction_recorders, - &tx.message, - )); + let instruction_recorders = if enable_cpi_recording { + let ix_count = tx.message.instructions.len(); + let mut recorders = Vec::with_capacity(ix_count); + recorders.resize_with(ix_count, InstructionRecorder::default); + Some(recorders) + } else { + None + }; - if let Err(e) = Self::refcells_to_accounts( - &mut loaded_transaction.accounts, - &mut loaded_transaction.loaders, - account_refcells, - loader_refcells, - ) { - warn!("Account lifetime mismanagement"); - process_result = Err(e); - } + let log_collector = if enable_log_recording { + Some(Rc::new(LogCollector::default())) + } else { + None + }; - if process_result.is_ok() { - self.update_executors(executors); + let compute_meter = Rc::new(RefCell::new(TxComputeMeter::new( + bpf_compute_budget.max_units, + ))); + + process_result = self.message_processor.process_message( + tx.message(), + &loader_refcells, + &account_refcells, + &self.rent_collector, + log_collector.clone(), + executors.clone(), + instruction_recorders.as_deref(), + feature_set, + bpf_compute_budget, + compute_meter, + &mut timings.details, + self.rc.accounts.clone(), + &self.ancestors, + ); + + transaction_log_messages.push(Self::collect_log_messages(log_collector)); + inner_instructions.push(Self::compile_recorded_instructions( + instruction_recorders, + &tx.message, + )); + + if let Err(e) = Self::refcells_to_accounts( + &mut loaded_transaction.accounts, + &mut loaded_transaction.loaders, + account_refcells, + loader_refcells, + ) { + warn!("Account lifetime mismanagement"); + process_result = Err(e); + } + + if process_result.is_ok() { + self.update_executors(executors); + } + } else { + transaction_log_messages.push(None); + inner_instructions.push(None); } let nonce_rollback = @@ -5941,6 +5986,7 @@ pub(crate) mod tests { use solana_sdk::{ account::Account, clock::{DEFAULT_SLOTS_PER_EPOCH, DEFAULT_TICKS_PER_SLOT}, + compute_budget, epoch_schedule::MINIMUM_SLOTS_PER_EPOCH, feature::Feature, genesis_config::create_genesis_config, @@ -14210,4 +14256,47 @@ pub(crate) mod tests { rent_debits.push(&Pubkey::default(), i64::MAX as u64, 0); assert_eq!(rent_debits.0.len(), 2); } + + #[test] + fn test_compute_request_instruction() { + solana_logger::setup(); + let GenesisConfigInfo { + genesis_config, + mint_keypair, + .. + } = create_genesis_config_with_leader( + 1_000_000_000_000_000, + &Pubkey::new_unique(), + bootstrap_validator_stake_lamports(), + ); + let mut bank = Bank::new(&genesis_config); + + fn mock_ix_processor( + _pubkey: &Pubkey, + _data: &[u8], + invoke_context: &mut dyn InvokeContext, + ) -> std::result::Result<(), InstructionError> { + let compute_budget = invoke_context.get_bpf_compute_budget(); + assert_eq!( + *compute_budget, + BpfComputeBudget { + max_units: 1, + ..BpfComputeBudget::default() + } + ); + Ok(()) + } + let program_id = solana_sdk::pubkey::new_rand(); + bank.add_builtin("mock_program", program_id, mock_ix_processor); + + let message = Message::new( + &[ + compute_budget::request_units(1), + Instruction::new_with_bincode(program_id, &0, vec![]), + ], + Some(&mint_keypair.pubkey()), + ); + let tx = Transaction::new(&[&mint_keypair], message, bank.last_blockhash()); + bank.process_transaction(&tx).unwrap(); + } } diff --git a/runtime/src/builtins.rs b/runtime/src/builtins.rs index 11a9837a476926..f71a2eb0d64bd0 100644 --- a/runtime/src/builtins.rs +++ b/runtime/src/builtins.rs @@ -3,6 +3,7 @@ use crate::{ system_instruction_processor, }; use solana_sdk::{ + feature_set, instruction::InstructionError, process_instruction::{stable_log, InvokeContext, ProcessInstructionWithContext}, pubkey::Pubkey, @@ -86,7 +87,15 @@ pub enum ActivationType { /// normal child Bank creation. /// https://github.com/solana-labs/solana/blob/84b139cc94b5be7c9e0c18c2ad91743231b85a0d/runtime/src/bank.rs#L1723 fn feature_builtins() -> Vec<(Builtin, Pubkey, ActivationType)> { - vec![] + vec![( + Builtin::new( + "compute_budget_program", + solana_sdk::compute_budget::id(), + solana_compute_budget_program::process_instruction, + ), + feature_set::tx_wide_compute_cap::id(), + ActivationType::NewProgram, + )] } pub(crate) fn get() -> Builtins { diff --git a/runtime/src/message_processor.rs b/runtime/src/message_processor.rs index 4f7a5203239421..551c42c9bc1e09 100644 --- a/runtime/src/message_processor.rs +++ b/runtime/src/message_processor.rs @@ -4,13 +4,14 @@ use crate::{ }; use log::*; use serde::{Deserialize, Serialize}; +use solana_measure::measure::Measure; use solana_sdk::{ account::{AccountSharedData, ReadableAccount, WritableAccount}, account_utils::StateMut, bpf_loader_upgradeable::{self, UpgradeableLoaderState}, feature_set::{ demote_program_write_locks, fix_write_privs, instructions_sysvar_enabled, - neon_evm_compute_budget, updated_verify_policy, FeatureSet, + neon_evm_compute_budget, tx_wide_compute_cap, updated_verify_policy, FeatureSet, }, ic_logger_msg, ic_msg, instruction::{CompiledInstruction, Instruction, InstructionError}, @@ -56,6 +57,13 @@ impl Executors { } } +#[derive(Default, Debug)] +pub struct ProgramTiming { + pub accumulated_us: u64, + pub accumulated_units: u64, + pub count: u32, +} + #[derive(Default, Debug)] pub struct ExecuteDetailsTimings { pub serialize_us: u64, @@ -66,6 +74,7 @@ pub struct ExecuteDetailsTimings { pub total_account_count: u64, pub total_data_size: usize, pub data_size_changed: usize, + pub per_program_timings: HashMap, } impl ExecuteDetailsTimings { @@ -78,6 +87,12 @@ impl ExecuteDetailsTimings { self.total_account_count += other.total_account_count; self.total_data_size += other.total_data_size; self.data_size_changed += other.data_size_changed; + for (id, other) in &other.per_program_timings { + let program_timing = self.per_program_timings.entry(*id).or_default(); + program_timing.accumulated_us += other.accumulated_us; + program_timing.accumulated_units += other.accumulated_units; + program_timing.count += other.count; + } } } @@ -289,6 +304,7 @@ impl<'a> ThisInvokeContext<'a> { programs: &'a [(Pubkey, ProcessInstructionWithContext)], log_collector: Option>, bpf_compute_budget: BpfComputeBudget, + compute_meter: Rc>, executors: Rc>, instruction_recorder: Option, feature_set: Arc, @@ -303,6 +319,13 @@ impl<'a> ThisInvokeContext<'a> { accounts, feature_set.is_active(&demote_program_write_locks::id()), ); + let compute_meter = if feature_set.is_active(&tx_wide_compute_cap::id()) { + compute_meter + } else { + Rc::new(RefCell::new(ThisComputeMeter { + remaining: bpf_compute_budget.max_units, + })) + }; let mut invoke_context = Self { invoke_stack: Vec::with_capacity(bpf_compute_budget.max_invoke_depth), rent, @@ -311,9 +334,7 @@ impl<'a> ThisInvokeContext<'a> { programs, logger: Rc::new(RefCell::new(ThisLogger { log_collector })), bpf_compute_budget, - compute_meter: Rc::new(RefCell::new(ThisComputeMeter { - remaining: bpf_compute_budget.max_units, - })), + compute_meter, executors, instruction_recorder, feature_set, @@ -1168,6 +1189,7 @@ impl MessageProcessor { instruction_index: usize, feature_set: Arc, bpf_compute_budget: BpfComputeBudget, + compute_meter: Rc>, timings: &mut ExecuteDetailsTimings, account_db: Arc, ancestors: &Ancestors, @@ -1194,7 +1216,7 @@ impl MessageProcessor { && *program_id == crate::neon_evm_program::id() { // Bump the compute budget for neon_evm - bpf_compute_budget.max_units = 500_000; + bpf_compute_budget.max_units = bpf_compute_budget.max_units.max(500_000); bpf_compute_budget.heap_size = Some(256 * 1024); } @@ -1208,6 +1230,7 @@ impl MessageProcessor { &self.programs, log_collector, bpf_compute_budget, + compute_meter, executors, instruction_recorder, feature_set, @@ -1249,31 +1272,46 @@ impl MessageProcessor { instruction_recorders: Option<&[InstructionRecorder]>, feature_set: Arc, bpf_compute_budget: BpfComputeBudget, + compute_meter: Rc>, timings: &mut ExecuteDetailsTimings, account_db: Arc, ancestors: &Ancestors, ) -> Result<(), TransactionError> { for (instruction_index, instruction) in message.instructions.iter().enumerate() { + let mut time = Measure::start("execute_instruction"); + let pre_remaining_units = compute_meter.borrow().get_remaining(); let instruction_recorder = instruction_recorders .as_ref() .map(|recorders| recorders[instruction_index].clone()); - self.execute_instruction( - message, - instruction, - &loaders[instruction_index], - accounts, - rent_collector, - log_collector.clone(), - executors.clone(), - instruction_recorder, - instruction_index, - feature_set.clone(), - bpf_compute_budget, - timings, - account_db.clone(), - ancestors, - ) - .map_err(|err| TransactionError::InstructionError(instruction_index as u8, err))?; + let err = self + .execute_instruction( + message, + instruction, + &loaders[instruction_index], + accounts, + rent_collector, + log_collector.clone(), + executors.clone(), + instruction_recorder, + instruction_index, + feature_set.clone(), + bpf_compute_budget, + compute_meter.clone(), + timings, + account_db.clone(), + ancestors, + ) + .map_err(|err| TransactionError::InstructionError(instruction_index as u8, err)); + time.stop(); + let post_remaining_units = compute_meter.borrow().get_remaining(); + + let program_id = instruction.program_id(&message.account_keys); + let program_timing = timings.per_program_timings.entry(*program_id).or_default(); + program_timing.accumulated_us += time.as_us(); + program_timing.accumulated_units += pre_remaining_units - post_remaining_units; + program_timing.count += 1; + + err?; } Ok(()) } @@ -1287,6 +1325,7 @@ mod tests { instruction::{AccountMeta, Instruction, InstructionError}, message::Message, native_loader::create_loadable_account_for_test, + process_instruction::MockComputeMeter, }; #[test] @@ -1334,6 +1373,7 @@ mod tests { &[], None, BpfComputeBudget::default(), + Rc::new(RefCell::new(MockComputeMeter::default())), Rc::new(RefCell::new(Executors::default())), None, Arc::new(FeatureSet::all_enabled()), @@ -1947,6 +1987,7 @@ mod tests { None, Arc::new(FeatureSet::all_enabled()), BpfComputeBudget::new(), + Rc::new(RefCell::new(MockComputeMeter::default())), &mut ExecuteDetailsTimings::default(), Arc::new(Accounts::default()), &ancestors, @@ -1974,6 +2015,7 @@ mod tests { None, Arc::new(FeatureSet::all_enabled()), BpfComputeBudget::new(), + Rc::new(RefCell::new(MockComputeMeter::default())), &mut ExecuteDetailsTimings::default(), Arc::new(Accounts::default()), &ancestors, @@ -2005,6 +2047,7 @@ mod tests { None, Arc::new(FeatureSet::all_enabled()), BpfComputeBudget::new(), + Rc::new(RefCell::new(MockComputeMeter::default())), &mut ExecuteDetailsTimings::default(), Arc::new(Accounts::default()), &ancestors, @@ -2128,6 +2171,7 @@ mod tests { None, Arc::new(FeatureSet::all_enabled()), BpfComputeBudget::new(), + Rc::new(RefCell::new(MockComputeMeter::default())), &mut ExecuteDetailsTimings::default(), Arc::new(Accounts::default()), &ancestors, @@ -2159,6 +2203,7 @@ mod tests { None, Arc::new(FeatureSet::all_enabled()), BpfComputeBudget::new(), + Rc::new(RefCell::new(MockComputeMeter::default())), &mut ExecuteDetailsTimings::default(), Arc::new(Accounts::default()), &ancestors, @@ -2188,6 +2233,7 @@ mod tests { None, Arc::new(FeatureSet::all_enabled()), BpfComputeBudget::new(), + Rc::new(RefCell::new(MockComputeMeter::default())), &mut ExecuteDetailsTimings::default(), Arc::new(Accounts::default()), &ancestors, @@ -2302,6 +2348,7 @@ mod tests { programs.as_slice(), None, BpfComputeBudget::default(), + Rc::new(RefCell::new(MockComputeMeter::default())), Rc::new(RefCell::new(Executors::default())), None, Arc::new(feature_set), @@ -2372,6 +2419,7 @@ mod tests { programs.as_slice(), None, BpfComputeBudget::default(), + Rc::new(RefCell::new(MockComputeMeter::default())), Rc::new(RefCell::new(Executors::default())), None, Arc::new(FeatureSet::all_enabled()), @@ -2524,6 +2572,7 @@ mod tests { programs.as_slice(), None, BpfComputeBudget::default(), + Rc::new(RefCell::new(MockComputeMeter::default())), Rc::new(RefCell::new(Executors::default())), None, Arc::new(FeatureSet::all_enabled()), @@ -2590,6 +2639,7 @@ mod tests { programs.as_slice(), None, BpfComputeBudget::default(), + Rc::new(RefCell::new(MockComputeMeter::default())), Rc::new(RefCell::new(Executors::default())), None, Arc::new(FeatureSet::all_enabled()), diff --git a/sdk/Cargo.toml b/sdk/Cargo.toml index 1be9af330f2299..a5c33d05fe879e 100644 --- a/sdk/Cargo.toml +++ b/sdk/Cargo.toml @@ -38,9 +38,11 @@ full = [ ] [dependencies] -assert_matches = { version = "1.3.0", optional = true } -bincode = "1.3.1" -bs58 = "0.3.1" +assert_matches = { version = "1.5.0", optional = true } +bincode = "1.3.3" +borsh = "0.9.0" +borsh-derive = "0.9.0" +bs58 = "0.4.0" bv = { version = "0.11.1", features = ["serde"] } byteorder = { version = "1.3.4", optional = true } chrono = { version = "0.4", optional = true } diff --git a/sdk/src/compute_budget.rs b/sdk/src/compute_budget.rs new file mode 100644 index 00000000000000..0fae0b8a81b969 --- /dev/null +++ b/sdk/src/compute_budget.rs @@ -0,0 +1,142 @@ +#![cfg(feature = "full")] + +use crate::{ + process_instruction::BpfComputeBudget, + transaction::{Transaction, TransactionError}, +}; +use borsh::{BorshDeserialize, BorshSchema, BorshSerialize}; +use solana_sdk::{ + borsh::try_from_slice_unchecked, + instruction::{Instruction, InstructionError}, +}; + +crate::declare_id!("ComputeBudget111111111111111111111111111111"); + +const MAX_UNITS: u64 = 1_000_000; + +/// Compute Budget Instructions +#[derive( + Serialize, + Deserialize, + BorshSerialize, + BorshDeserialize, + BorshSchema, + Debug, + Clone, + PartialEq, + AbiExample, + AbiEnumVisitor, +)] +pub enum ComputeBudgetInstruction { + /// Request a specific maximum number of compute units the transaction is + /// allowed to consume. + RequestUnits(u64), +} + +/// Create a `ComputeBudgetInstruction::RequestUnits` `Instruction` +pub fn request_units(units: u64) -> Instruction { + Instruction::new_with_borsh(id(), &ComputeBudgetInstruction::RequestUnits(units), vec![]) +} + +pub fn process_request( + compute_budget: &mut BpfComputeBudget, + tx: &Transaction, +) -> Result<(), TransactionError> { + let error = TransactionError::InstructionError(0, InstructionError::InvalidInstructionData); + // Compute budget instruction must be in 1st or 2nd instruction (avoid nonce marker) + for instruction in tx.message().instructions.iter().take(2) { + if check_id(instruction.program_id(&tx.message().account_keys)) { + let ComputeBudgetInstruction::RequestUnits(units) = + try_from_slice_unchecked::(&instruction.data) + .map_err(|_| error.clone())?; + if units > MAX_UNITS { + return Err(error); + } + compute_budget.max_units = units; + } + } + Ok(()) +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::{ + compute_budget, hash::Hash, message::Message, pubkey::Pubkey, signature::Keypair, + signer::Signer, + }; + + #[test] + fn test_process_request() { + let payer_keypair = Keypair::new(); + let mut compute_budget = BpfComputeBudget::default(); + + let tx = Transaction::new( + &[&payer_keypair], + Message::new(&[], Some(&payer_keypair.pubkey())), + Hash::default(), + ); + process_request(&mut compute_budget, &tx).unwrap(); + assert_eq!(compute_budget, BpfComputeBudget::default()); + + let tx = Transaction::new( + &[&payer_keypair], + Message::new( + &[ + compute_budget::request_units(1), + Instruction::new_with_bincode(Pubkey::new_unique(), &0, vec![]), + ], + Some(&payer_keypair.pubkey()), + ), + Hash::default(), + ); + process_request(&mut compute_budget, &tx).unwrap(); + assert_eq!( + compute_budget, + BpfComputeBudget { + max_units: 1, + ..BpfComputeBudget::default() + } + ); + + let tx = Transaction::new( + &[&payer_keypair], + Message::new( + &[ + compute_budget::request_units(MAX_UNITS + 1), + Instruction::new_with_bincode(Pubkey::new_unique(), &0, vec![]), + ], + Some(&payer_keypair.pubkey()), + ), + Hash::default(), + ); + let result = process_request(&mut compute_budget, &tx); + assert_eq!( + result, + Err(TransactionError::InstructionError( + 0, + InstructionError::InvalidInstructionData + )) + ); + + let tx = Transaction::new( + &[&payer_keypair], + Message::new( + &[ + Instruction::new_with_bincode(Pubkey::new_unique(), &0, vec![]), + compute_budget::request_units(MAX_UNITS), + ], + Some(&payer_keypair.pubkey()), + ), + Hash::default(), + ); + process_request(&mut compute_budget, &tx).unwrap(); + assert_eq!( + compute_budget, + BpfComputeBudget { + max_units: MAX_UNITS, + ..BpfComputeBudget::default() + } + ); + } +} diff --git a/sdk/src/feature_set.rs b/sdk/src/feature_set.rs index 0157a15df29216..2d6df384867797 100644 --- a/sdk/src/feature_set.rs +++ b/sdk/src/feature_set.rs @@ -175,10 +175,6 @@ pub mod stake_merge_with_unmatched_credits_observed { solana_sdk::declare_id!("meRgp4ArRPhD3KtCY9c5yAf2med7mBLsjKTPeVUHqBL"); } -pub mod gate_large_block { - solana_sdk::declare_id!("2ry7ygxiYURULZCrypHhveanvP5tzZ4toRwVp89oCNSj"); -} - pub mod mem_overlap_fix { solana_sdk::declare_id!("vXDCFK7gphrEmyf5VnKgLmqbdJ4UxD2eZH1qbdouYKF"); } @@ -223,6 +219,14 @@ pub mod optimize_epoch_boundary_updates { solana_sdk::declare_id!("265hPS8k8xJ37ot82KEgjRunsUp5w4n4Q4VwwiN9i9ps"); } +pub mod tx_wide_compute_cap { + solana_sdk::declare_id!("5ekBxc8itEnPv4NzGJtr8BVVQLNMQuLMNQQj7pHoLNZ9"); +} + +pub mod gate_large_block { + solana_sdk::declare_id!("2ry7ygxiYURULZCrypHhveanvP5tzZ4toRwVp89oCNSj"); +} + lazy_static! { /// Map of feature identifiers to user-visible description pub static ref FEATURE_NAMES: HashMap = [ @@ -267,7 +271,6 @@ lazy_static! { (merge_nonce_error_into_system_error::id(), "merge NonceError into SystemError"), (spl_token_v2_set_authority_fix::id(), "spl-token set_authority fix"), (stake_merge_with_unmatched_credits_observed::id(), "allow merging active stakes with unmatched credits_observed #18985"), - (gate_large_block::id(), "validator checks block cost against max limit in realtime, reject if exceeds."), (mem_overlap_fix::id(), "Memory overlap fix"), (close_upgradeable_program_accounts::id(), "enable closing upgradeable program accounts"), (stake_program_advance_activating_credits_observed::id(), "Enable advancing credits observed for activation epoch #19309"), @@ -279,6 +282,8 @@ lazy_static! { (stakes_remove_delegation_if_inactive::id(), "remove delegations from stakes cache when inactive"), (send_to_tpu_vote_port::id(), "Send votes to the tpu vote port"), (optimize_epoch_boundary_updates::id(), "Optimize epoch boundary updates"), + (tx_wide_compute_cap::id(), "Transaction wide compute cap"), + (gate_large_block::id(), "validator checks block cost against max limit in realtime, reject if exceeds."), /*************** ADD NEW FEATURES HERE ***************/ ] .iter() diff --git a/sdk/src/lib.rs b/sdk/src/lib.rs index ee185b155c5107..ddee313fc8df8d 100644 --- a/sdk/src/lib.rs +++ b/sdk/src/lib.rs @@ -15,6 +15,7 @@ pub mod arithmetic; pub mod builtins; pub mod client; pub mod commitment_config; +pub mod compute_budget; pub mod derivation_path; pub mod deserialize_utils; pub mod entrypoint; diff --git a/sdk/src/process_instruction.rs b/sdk/src/process_instruction.rs index d740c87e8dceea..3106c0a9d59bd3 100644 --- a/sdk/src/process_instruction.rs +++ b/sdk/src/process_instruction.rs @@ -147,7 +147,7 @@ pub fn get_sysvar( }) } -#[derive(Clone, Copy, Debug, AbiExample)] +#[derive(Clone, Copy, Debug, AbiExample, PartialEq)] pub struct BpfComputeBudget { /// Number of compute units that an instruction is allowed. Compute units /// are consumed by program execution, resources they use, etc... diff --git a/streamer/src/streamer.rs b/streamer/src/streamer.rs index 7ed639a11828e1..386f3b7fea8064 100644 --- a/streamer/src/streamer.rs +++ b/streamer/src/streamer.rs @@ -6,7 +6,7 @@ use crate::{ recvmmsg::NUM_RCVMMSGS, socket::SocketAddrSpace, }; -use solana_sdk::timing::{duration_as_ms, timestamp}; +use solana_sdk::timing::timestamp; use std::net::UdpSocket; use std::sync::atomic::{AtomicBool, Ordering}; use std::sync::mpsc::{Receiver, RecvTimeoutError, SendError, Sender}; @@ -138,8 +138,13 @@ pub fn recv_batch(recvr: &PacketReceiver) -> Result<(Vec, usize, u64)> len += more.packets.len(); batch.push(more); } + let recv_duration = recv_start.elapsed(); trace!("batch len {}", batch.len()); - Ok((batch, len, duration_as_ms(&recv_start.elapsed()))) + Ok(( + batch, + len, + solana_sdk::timing::duration_as_ms(&recv_duration), + )) } pub fn responder(