From eb683dd402be4b963968ce056ea2ff2cf2a364d1 Mon Sep 17 00:00:00 2001 From: "mergify[bot]" <37929162+mergify[bot]@users.noreply.github.com> Date: Fri, 2 Jul 2021 07:03:04 +0000 Subject: [PATCH] Document order of recent blockhashes sysvar (#18379) I wanted to use this sysvar to get a recent block hash, but I didn't know whether the first or the last entry contains the most recent block hash. By calling it for mainnet, printing the results, and comparing that to the recent blocks on solanabeach.io/blocks, I discovered that the entries are ordered from most recent to least recent. Document this to save future readers the trouble. (cherry picked from commit 94ab0eb49f1bce18d0a157dfe7a2bb1fb39dbe2c) Co-authored-by: Ruud van Asseldonk --- docs/src/developing/runtime-facilities/sysvars.md | 4 +++- sdk/program/src/sysvar/recent_blockhashes.rs | 4 ++++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/docs/src/developing/runtime-facilities/sysvars.md b/docs/src/developing/runtime-facilities/sysvars.md index a72929a68a..3b5ed443ae 100644 --- a/docs/src/developing/runtime-facilities/sysvars.md +++ b/docs/src/developing/runtime-facilities/sysvars.md @@ -97,7 +97,9 @@ other instructions in the same transaction. Read more information on ## RecentBlockhashes The RecentBlockhashes sysvar contains the active recent blockhashes as well as -their associated fee calculators. It is updated every slot. +their associated fee calculators. It is updated every slot. Entries are ordered +by descending block height, so the first entry holds the most recent block hash, +and the last entry holds an old block hash. - Address: `SysvarRecentB1ockHashes11111111111111111111` - Layout: diff --git a/sdk/program/src/sysvar/recent_blockhashes.rs b/sdk/program/src/sysvar/recent_blockhashes.rs index 93fee5e4dc..00f822c74e 100644 --- a/sdk/program/src/sysvar/recent_blockhashes.rs +++ b/sdk/program/src/sysvar/recent_blockhashes.rs @@ -53,6 +53,10 @@ impl<'a> PartialOrd for IterItem<'a> { } } +/// Contains recent block hashes and fee calculators. +/// +/// The entries are ordered by descending block height, so the first entry holds +/// the most recent block hash, and the last entry holds an old block hash. #[repr(C)] #[derive(Serialize, Deserialize, Clone, Debug, PartialEq)] pub struct RecentBlockhashes(Vec);