From 309f5897bce57ff9d1c2e4b9a24138cd90838dd3 Mon Sep 17 00:00:00 2001 From: lklimek <842586+lklimek@users.noreply.github.com> Date: Thu, 29 Feb 2024 11:30:04 +0100 Subject: [PATCH] fix: tenderdash-proto rebuilt with every run due to changed *.proto (#52) * fix: tenderdash-proto rebuilt with every run due to changed *.proto files * refactor: self review * chore: fix GHA build errors * chore: clippy * chore: build errs fix --- proto-compiler/src/functions.rs | 34 +++++++++++++++++++ proto-compiler/src/lib.rs | 29 +++++++++++----- proto/src/error.rs | 2 +- proto/src/lib.rs | 5 +-- .../src/serializers/part_set_header_total.rs | 2 +- 5 files changed, 57 insertions(+), 15 deletions(-) diff --git a/proto-compiler/src/functions.rs b/proto-compiler/src/functions.rs index fe183e74..ec333a3c 100644 --- a/proto-compiler/src/functions.rs +++ b/proto-compiler/src/functions.rs @@ -325,3 +325,37 @@ pub(crate) fn tenderdash_commitish() -> String { Err(_) => DEFAULT_TENDERDASH_COMMITISH.to_string(), } } + +/// Save the commitish of last successful download to a file in a state file, +/// located in the `dir` directory and named `download.state`. +pub(crate) fn save_state(dir: &Path, commitish: &str) { + let state_file = PathBuf::from(&dir).join("download.state"); + + std::fs::write(&state_file, commitish) + .map_err(|e| { + println!( + "[warn] => Failed to write download.state file {}: {}", + state_file.display(), + e + ); + }) + .ok(); +} + +/// Check if the state file contains the same commitish as the one we are trying +/// to download. State file should be located in the `dir` and named +/// `download.state` +pub(crate) fn check_state(dir: &Path, commitish: &str) -> bool { + let state_file = PathBuf::from(&dir).join("download.state"); + + match read_to_string(state_file) { + Ok(content) => { + println!( + "[info] => Found previously downloaded Tenderdash {}.", + content.trim() + ); + content.eq(commitish) + }, + Err(_) => false, + } +} diff --git a/proto-compiler/src/lib.rs b/proto-compiler/src/lib.rs index 0dcb7215..27f5e583 100644 --- a/proto-compiler/src/lib.rs +++ b/proto-compiler/src/lib.rs @@ -11,6 +11,8 @@ use functions::{ mod constants; use constants::{CUSTOM_FIELD_ATTRIBUTES, CUSTOM_TYPE_ATTRIBUTES, TENDERDASH_REPO}; +use crate::functions::{check_state, save_state}; + /// Import and compile protobuf definitions for Tenderdash. /// /// Checkouts tenderdash repository to ../target/tenderdash and generates @@ -25,7 +27,7 @@ pub fn proto_compile() { .join("src") .join("tenderdash.rs"); - let target_dir = root.join("..").join("proto").join("src").join("prost"); + let prost_out_dir = root.join("..").join("proto").join("src").join("prost"); let out_dir = var("OUT_DIR") .map(PathBuf::from) @@ -47,13 +49,21 @@ pub fn proto_compile() { let thirdparty_dir = root.join("third_party"); let commitish = tenderdash_commitish(); - println!("[info] => Fetching {TENDERDASH_REPO} at {commitish} into {tenderdash_dir:?}"); - fetch_commitish( - &PathBuf::from(&tenderdash_dir), - &cargo_target_dir, - TENDERDASH_REPO, - &commitish, - ); // This panics if it fails. + + // check if this commitish is already downloaded + let download = !check_state(&prost_out_dir, &commitish); + + if download { + println!("[info] => Fetching {TENDERDASH_REPO} at {commitish} into {tenderdash_dir:?}."); + fetch_commitish( + &PathBuf::from(&tenderdash_dir), + &cargo_target_dir, + TENDERDASH_REPO, + &commitish, + ); // This panics if it fails. + } else { + println!("[info] => Skipping download."); + } // We need all files in proto/tendermint/abci, plus .../types/canonical.proto // for signature verification @@ -100,9 +110,10 @@ pub fn proto_compile() { pb.compile_protos(&protos, &proto_includes_paths).unwrap(); println!("[info] => Removing old structs and copying new structs."); - copy_files(&out_dir, &target_dir); // This panics if it fails. + copy_files(&out_dir, &prost_out_dir); // This panics if it fails. generate_tenderdash_lib(&out_dir, &tenderdash_lib_target, &abci_ver, &tenderdash_ver); + save_state(&prost_out_dir, &commitish); println!("[info] => Done!"); } diff --git a/proto/src/error.rs b/proto/src/error.rs index 1495ff2d..a5966657 100644 --- a/proto/src/error.rs +++ b/proto/src/error.rs @@ -1,7 +1,7 @@ //! This module defines the various errors that be raised during Protobuf //! conversions. -use core::{convert::TryFrom, fmt::Display, num::TryFromIntError}; +use core::{fmt::Display, num::TryFromIntError}; use flex_error::{define_error, DisplayOnly}; use prost::{DecodeError, EncodeError}; diff --git a/proto/src/lib.rs b/proto/src/lib.rs index 667be8e2..f5296db6 100644 --- a/proto/src/lib.rs +++ b/proto/src/lib.rs @@ -23,10 +23,7 @@ mod error; #[allow(warnings)] mod tenderdash; -use core::{ - convert::{TryFrom, TryInto}, - fmt::Display, -}; +use core::fmt::Display; use bytes::{Buf, BufMut}; pub use error::Error; diff --git a/proto/src/serializers/part_set_header_total.rs b/proto/src/serializers/part_set_header_total.rs index cecfc52d..34295506 100644 --- a/proto/src/serializers/part_set_header_total.rs +++ b/proto/src/serializers/part_set_header_total.rs @@ -5,7 +5,7 @@ //! from a string-quoted integer value into an integer value without quotes in //! Tendermint Core v0.34.0. This deserializer allows backwards-compatibility by //! deserializing both ways. See also: -use core::{convert::TryFrom, fmt::Formatter}; +use core::fmt::Formatter; use serde::{ de::{Error, Visitor},