Make order of types in shared_types deterministic (#2169)

* Add missing development dependencies to flake.nix

* Use BTreeMap/Set for deterministic shared types

Fixes an issue where the order of types in the generated shared_types is
not always deterministic.
This commit is contained in:
Mathis 2023-02-21 01:18:31 +01:00 committed by GitHub
parent 2f85e73049
commit c21362b696
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 49 additions and 4 deletions

View File

@ -5,7 +5,7 @@ use inflector::Inflector;
use proc_macro2::TokenStream;
use quote::quote;
use std::{
collections::{BTreeMap, BTreeSet, HashMap, HashSet},
collections::{BTreeMap, BTreeSet},
fs,
io::Write,
path::{Path, PathBuf},
@ -178,12 +178,12 @@ impl MultiExpansion {
let mut shared_types = Vec::new();
// this keeps track of those contracts that need to be updated after a struct was
// extracted from the contract's module and moved to the shared module
let mut dirty_contracts = HashSet::new();
let mut dirty_contracts = BTreeSet::new();
// merge all types if more than 1 contract
if expansions.len() > 1 {
// check for type conflicts across all contracts
let mut conflicts: HashMap<String, Vec<usize>> = HashMap::new();
let mut conflicts: BTreeMap<String, Vec<usize>> = BTreeMap::new();
for (idx, (_, ctx)) in expansions.iter().enumerate() {
for type_identifier in ctx.internal_structs().rust_type_names().keys() {
conflicts
@ -230,7 +230,7 @@ pub struct MultiExpansionResult {
root: Option<PathBuf>,
contracts: Vec<(ExpandedContract, Context)>,
/// contains the indices of contracts with structs that need to be updated
dirty_contracts: HashSet<usize>,
dirty_contracts: BTreeSet<usize>,
/// all type definitions of types that are shared by multiple contracts
shared_types: Vec<TokenStream>,
}

37
flake.lock generated
View File

@ -88,6 +88,42 @@
"type": "github"
}
},
"flake-utils_3": {
"locked": {
"lastModified": 1644229661,
"narHash": "sha256-1YdnJAsNy69bpcjuoKdOYQX0YxZBiCYZo4Twxerqv7k=",
"owner": "numtide",
"repo": "flake-utils",
"rev": "3cecb5b042f7f209c56ffd8371b2711a290ec797",
"type": "github"
},
"original": {
"owner": "numtide",
"repo": "flake-utils",
"type": "github"
}
},
"foundry": {
"inputs": {
"flake-utils": "flake-utils_3",
"nixpkgs": [
"nixpkgs"
]
},
"locked": {
"lastModified": 1676884188,
"narHash": "sha256-jaVDZcl+YfNkvdGrmWozrvuzOjW45Iuyx8HhR/kE7Bs=",
"owner": "shazow",
"repo": "foundry.nix",
"rev": "ff07b373aaea4d27e15c7291d344d9f92b614fc6",
"type": "github"
},
"original": {
"owner": "shazow",
"repo": "foundry.nix",
"type": "github"
}
},
"gitignore": {
"inputs": {
"nixpkgs": [
@ -245,6 +281,7 @@
"devenv": "devenv",
"fenix": "fenix",
"flake-utils": "flake-utils_2",
"foundry": "foundry",
"nixpkgs": "nixpkgs_2"
}
},

View File

@ -6,6 +6,10 @@
url = "github:nix-community/fenix";
inputs.nixpkgs.follows = "nixpkgs";
};
foundry = {
url = "github:shazow/foundry.nix";
inputs.nixpkgs.follows = "nixpkgs";
};
};
outputs =
@ -13,6 +17,7 @@
, nixpkgs
, flake-utils
, fenix
, foundry
, devenv
} @ inputs:
flake-utils.lib.eachDefaultSystem (system:
@ -28,6 +33,9 @@
{
packages = with pkgs; [
solc
gcc
foundry.defaultPackage.${system}
go-ethereum
] ++ lib.optionals stdenv.isDarwin (with darwin.apple_sdk; [
libiconv
frameworks.Security