ソースを参照

Ensure that enherited functions are also added to the abi file

Signed-off-by: Sean Young <sean@mess.org>
Sean Young 4 年 前
コミット
81dd1e1600
3 ファイル変更70 行追加21 行削除
  1. 36 21
      src/abi/ethereum.rs
  2. 4 0
      tests/solana.rs
  3. 30 0
      tests/solana_tests/abi.rs

+ 36 - 21
src/abi/ethereum.rs

@@ -77,34 +77,49 @@ pub fn gen_abi(contract_no: usize, ns: &Namespace) -> Vec<ABI> {
     }
 
     ns.contracts[contract_no]
-        .functions
-        .iter()
+        .all_functions
+        .keys()
         .filter_map(|function_no| {
             let func = &ns.functions[*function_no];
 
-            if matches!(
+            if let Some(base_contract_no) = func.contract_no {
+                if ns.contracts[base_contract_no].is_library() {
+                    return None;
+                }
+
+                if func.ty == pt::FunctionTy::Constructor && base_contract_no != contract_no {
+                    return None;
+                }
+            }
+
+            if !matches!(
                 func.visibility,
                 pt::Visibility::Public(_) | pt::Visibility::External(_)
             ) {
-                Some(ABI {
-                    name: func.name.to_owned(),
-                    mutability: func.print_mutability(),
-                    ty: func.ty.to_string(),
-                    inputs: func
-                        .params
-                        .iter()
-                        .map(|p| parameter_to_abi(p, ns))
-                        .collect(),
-                    outputs: func
-                        .returns
-                        .iter()
-                        .map(|p| parameter_to_abi(p, ns))
-                        .collect(),
-                    anonymous: false,
-                })
-            } else {
-                None
+                return None;
             }
+
+            if func.ty == pt::FunctionTy::Modifier || !func.has_body {
+                return None;
+            }
+
+            Some(func)
+        })
+        .map(|func| ABI {
+            name: func.name.to_owned(),
+            mutability: func.print_mutability(),
+            ty: func.ty.to_string(),
+            inputs: func
+                .params
+                .iter()
+                .map(|p| parameter_to_abi(p, ns))
+                .collect(),
+            outputs: func
+                .returns
+                .iter()
+                .map(|p| parameter_to_abi(p, ns))
+                .collect(),
+            anonymous: false,
         })
         .chain(
             ns.contracts[contract_no]

+ 4 - 0
tests/solana.rs

@@ -141,6 +141,10 @@ fn build_solidity(src: &str) -> VirtualMachine {
     let ns = &namespaces[0];
 
     for contract_no in 0..ns.contracts.len() {
+        if !ns.contracts[contract_no].is_concrete() {
+            continue;
+        }
+
         let (abi, _) = generate_abi(contract_no, &ns, &code, false);
 
         let program = account_new();

+ 30 - 0
tests/solana_tests/abi.rs

@@ -41,3 +41,33 @@ fn packed() {
     vm.function("test2", &[], &[]);
     vm.function("test3", &[], &[]);
 }
+
+#[test]
+fn inherited() {
+    let mut vm = build_solidity(
+        r#"
+        contract bar is foo { }
+
+        contract foo {
+            function test() public {
+            }
+        }"#,
+    );
+
+    vm.constructor("bar", &[]);
+
+    vm.function("test", &[], &[]);
+
+    let mut vm = build_solidity(
+        r#"
+            contract bar is foo { }
+
+            contract foo {
+                int public test;
+            }"#,
+    );
+
+    vm.constructor("bar", &[]);
+
+    vm.function("test", &[], &[]);
+}