make abigen reproducible (#200)

* use BTreeMap to make abigen bindings deterministic
This commit is contained in:
lerencao 2021-02-19 14:34:56 +08:00 committed by GitHub
parent 6f26490385
commit 732ff29d14
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 19 additions and 17 deletions

View File

@ -14,7 +14,7 @@ use ethers_core::{
use inflector::Inflector; use inflector::Inflector;
use proc_macro2::{Ident, Literal, TokenStream}; use proc_macro2::{Ident, Literal, TokenStream};
use quote::quote; use quote::quote;
use std::collections::HashMap; use std::collections::BTreeMap;
use syn::{Path, Visibility}; use syn::{Path, Visibility};
/// Internal shared context for generating smart contract bindings. /// Internal shared context for generating smart contract bindings.
@ -32,7 +32,7 @@ pub(crate) struct Context {
contract_name: Ident, contract_name: Ident,
/// Manually specified method aliases. /// Manually specified method aliases.
method_aliases: HashMap<String, Ident>, method_aliases: BTreeMap<String, Ident>,
/// Derives added to event structs and enums. /// Derives added to event structs and enums.
event_derives: Vec<Path>, event_derives: Vec<Path>,
@ -122,7 +122,7 @@ impl Context {
// NOTE: We only check for duplicate signatures here, since if there are // NOTE: We only check for duplicate signatures here, since if there are
// duplicate aliases, the compiler will produce a warning because a // duplicate aliases, the compiler will produce a warning because a
// method will be re-defined. // method will be re-defined.
let mut method_aliases = HashMap::new(); let mut method_aliases = BTreeMap::new();
for (signature, alias) in args.method_aliases.into_iter() { for (signature, alias) in args.method_aliases.into_iter() {
let alias = syn::parse_str(&alias)?; let alias = syn::parse_str(&alias)?;
if method_aliases.insert(signature.clone(), alias).is_some() { if method_aliases.insert(signature.clone(), alias).is_some() {

View File

@ -1,18 +1,19 @@
use super::{types, util, Context}; use super::{types, util, Context};
use ethers_core::abi::{Event, EventExt, EventParam, Hash, ParamType};
use anyhow::Result; use anyhow::Result;
use ethers_core::abi::{Event, EventExt, EventParam, Hash, ParamType};
use inflector::Inflector; use inflector::Inflector;
use proc_macro2::{Literal, TokenStream}; use proc_macro2::{Literal, TokenStream};
use quote::quote; use quote::quote;
use std::collections::BTreeMap;
use syn::Path; use syn::Path;
impl Context { impl Context {
/// Expands each event to a struct + its impl Detokenize block /// Expands each event to a struct + its impl Detokenize block
pub fn events_declaration(&self) -> Result<TokenStream> { pub fn events_declaration(&self) -> Result<TokenStream> {
let data_types = self let sorted_events: BTreeMap<_, _> = self.abi.events.clone().into_iter().collect();
.abi let data_types = sorted_events
.events() .values()
.flatten()
.map(|event| expand_event(event, &self.event_derives)) .map(|event| expand_event(event, &self.event_derives))
.collect::<Result<Vec<_>>>()?; .collect::<Result<Vec<_>>>()?;
@ -26,9 +27,10 @@ impl Context {
} }
pub fn events(&self) -> Result<TokenStream> { pub fn events(&self) -> Result<TokenStream> {
let data_types = self let sorted_events: BTreeMap<_, _> = self.abi.events.clone().into_iter().collect();
.abi let data_types = sorted_events
.events() .values()
.flatten()
.map(|event| expand_filter(event)) .map(|event| expand_filter(event))
.collect::<Vec<_>>(); .collect::<Vec<_>>();

View File

@ -1,13 +1,13 @@
use super::{types, util, Context}; use super::{types, util, Context};
use anyhow::{anyhow, Context as _, Result};
use ethers_core::{ use ethers_core::{
abi::{Function, FunctionExt, Param, StateMutability}, abi::{Function, FunctionExt, Param, StateMutability},
types::Selector, types::Selector,
}; };
use anyhow::{anyhow, Context as _, Result};
use inflector::Inflector; use inflector::Inflector;
use proc_macro2::{Literal, TokenStream}; use proc_macro2::{Literal, TokenStream};
use quote::quote; use quote::quote;
use std::collections::BTreeMap;
use syn::Ident; use syn::Ident;
/// Expands a context into a method struct containing all the generated bindings /// Expands a context into a method struct containing all the generated bindings
@ -15,10 +15,10 @@ use syn::Ident;
impl Context { impl Context {
pub(crate) fn methods(&self) -> Result<TokenStream> { pub(crate) fn methods(&self) -> Result<TokenStream> {
let mut aliases = self.method_aliases.clone(); let mut aliases = self.method_aliases.clone();
let sorted_functions: BTreeMap<_, _> = self.abi.functions.clone().into_iter().collect();
let functions = self let functions = sorted_functions
.abi .values()
.functions() .flatten()
.map(|function| { .map(|function| {
let signature = function.abi_signature(); let signature = function.abi_signature();
expand_function(function, aliases.remove(&signature)) expand_function(function, aliases.remove(&signature))