From 94ab0eb49f1bce18d0a157dfe7a2bb1fb39dbe2c Mon Sep 17 00:00:00 2001 From: Ruud van Asseldonk Date: Thu, 1 Jul 2021 16:50:47 +0200 Subject: [PATCH] Document order of recent blockhashes sysvar 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. --- 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 a72929a68..3b5ed443a 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 93fee5e4d..00f822c74 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);