Repository: ergoplatform/sigma-rust (language: rust)

Issue:
ErgoTree parsing failed `Expected: SBoolean, got: SSigmaProp`
>Parsing constants from this ergotree 100208cd03553448c194fdd843c87d080f5e8ed983f5bb2807b13b45a9683bba8c7bfb5ae808cd0354c06b1af711e51986d787ff1df2883fcaf8d34865fea720f549e382063a08ebd1eb0273007301 in transaction https://explorer.ergoplatform.com/en/transactions/1b3b7214e4c0f5f858cc6ab718cd2300085bb2f60b19f392fdd5ca7fa403fb9a output index 0. Using C.ergo_lib_ergo_tree_constants_len results in this error: error: SigmaParsingError(InvalidArgument(InvalidArgumentError("InvalidExprEvalTypeError: InvalidExprEvalTypeError: expected: SBoolean, got: SSigmaProp"))) 

>the call stack is like this:
ergo_lib_ergo_tree_from_base16_bytes
ergo_lib_ergo_tree_constants_len <-- error gets thrown here
for loop executing ergo_lib_ergo_tree_get_constant with result from len


Discussion / hints:

Fix patch:
diff --git a/bindings/ergo-lib-ios/Tests/ErgoLibTests/RestNodeApiTests.swift b/bindings/ergo-lib-ios/Tests/ErgoLibTests/RestNodeApiTests.swift
index da4787bfd..27b8507ac 100644
--- a/bindings/ergo-lib-ios/Tests/ErgoLibTests/RestNodeApiTests.swift
+++ b/bindings/ergo-lib-ios/Tests/ErgoLibTests/RestNodeApiTests.swift
@@ -124,7 +124,7 @@ final class RestNodeApiTests: XCTestCase {
         XCTAssertEqual(try bestProof.suffixHead().getHeader().getBlockId(), headerId)
         
         // Now verify with 3rd node
-        let nodeConf = try NodeConf(withAddrString: "198.58.96.195:9053")
+        let nodeConf = try NodeConf(withAddrString: "213.239.193.208:9053")
         let restNodeApi = try RestNodeApi()
         let header = try await restNodeApi.getHeaderAsync(nodeConf: nodeConf, blockId: headerId)
         let merkleProof = try await restNodeApi.getBlocksHeaderIdProofForTxIdAsync(nodeConf: nodeConf, blockId: headerId, txId: txId)
diff --git a/ergotree-ir/src/ergo_tree.rs b/ergotree-ir/src/ergo_tree.rs
index cf65e6e03..7853d0bb9 100644
--- a/ergotree-ir/src/ergo_tree.rs
+++ b/ergotree-ir/src/ergo_tree.rs
@@ -162,9 +162,11 @@ impl ErgoTree {
                 "too many constants".to_string(),
             ));
         }
+        dbg!(&constants_len);
         let mut constants = Vec::with_capacity(constants_len as usize);
         for _ in 0..constants_len {
             let c = Constant::sigma_parse(r)?;
+            dbg!(&c);
             constants.push(c);
         }
         Ok(constants)
@@ -747,4 +749,15 @@ mod tests {
             }
         );
     }
+
+    #[test]
+    fn parse_invalid_tree_707() {
+        // see https://github.com/ergoplatform/sigma-rust/issues/707
+        let ergo_tree_hex =
+            "100208cd03553448c194fdd843c87d080f5e8ed983f5bb2807b13b45a9683bba8c7bfb5ae808cd0354c06b1af711e51986d787ff1df2883fcaf8d34865fea720f549e382063a08ebd1eb0273007301";
+        let bytes = base16::decode(ergo_tree_hex.as_bytes()).unwrap();
+        let tree = ErgoTree::sigma_parse_bytes(&bytes).unwrap();
+        dbg!(&tree);
+        assert!(tree.parsed_tree().is_err(), "the tree is BoolToSigmaProp(SigmaOr(pk1, pk2)) is invalid (BoolToSigmaProp expects bool");
+    }
 }
diff --git a/ergotree-ir/src/mir/expr.rs b/ergotree-ir/src/mir/expr.rs
index 4465e4934..d75cee57d 100644
--- a/ergotree-ir/src/mir/expr.rs
+++ b/ergotree-ir/src/mir/expr.rs
@@ -313,8 +313,10 @@ impl Expr {
         if &expr_tpe == expected_tpe {
             Ok(())
         } else {
+            use std::backtrace::Backtrace;
+            let backtrace = Backtrace::capture();
             Err(InvalidExprEvalTypeError(format!(
-                "expected: {0:?}, got: {1:?}",
+                "expected: {0:?}, got: {1:?}\nBacktrace:\n{backtrace}",
                 expected_tpe, expr_tpe
             )))
         }


Test patch:
diff --git a/bindings/ergo-lib-wasm/tests_browser/test_rest_api.js b/bindings/ergo-lib-wasm/tests_browser/test_rest_api.js
index e11350ee6..917ec1391 100644
--- a/bindings/ergo-lib-wasm/tests_browser/test_rest_api.js
+++ b/bindings/ergo-lib-wasm/tests_browser/test_rest_api.js
@@ -68,7 +68,7 @@ it('node REST API: example SPV workflow', async () => {
     assert(best_proof.suffix_head().id().equals(header_id), "equality");
 
     // Verify with a 3rd node
-    let node_conf = new ergo_wasm.NodeConf(new URL("http://198.58.96.195:9053"));
+    let node_conf = new ergo_wasm.NodeConf(new URL("http://213.239.193.208:9053"));
     let header = await ergo_wasm.get_header(node_conf, header_id);
     assert(header != null, "header should be non-null");
     let merkle_proof = await ergo_wasm.get_blocks_header_id_proof_for_tx_id(node_conf, header_id, tx_id);