【问题标题】:output_ptr Assertion Error in Main - Cairo-langMain 中的 output_ptr 断言错误 - Cairo-lang
【发布时间】:2022-01-05 00:29:19
【问题描述】:

我正在通过官方Cairo language tutorial。当我到达 15-puzzle 的 build_dict 函数时,我有点困惑,所以想打印一些东西来看看。

struct Location:
    member row : felt
    member col : felt
end

...

func finalize_state(dict : DictAccess*, idx) -> (
        dict : DictAccess*):
    if idx == 0:
        return (dict=dict)
    end

    assert dict.key = idx
    assert dict.prev_value = idx - 1
    assert dict.new_value = idx - 1

    return finalize_state(
        dict=dict + DictAccess.SIZE, idx=idx - 1)
end

##### I added the {} along with context in it for output #####
func main{output_ptr : felt*}():
    alloc_locals

    local loc_tuple : (Location, Location, Location, Location, Location) = (
        Location(row=0, col=2),
        Location(row=1, col=2),
        Location(row=1, col=3),
        Location(row=2, col=3),
        Location(row=3, col=3),
        )

    # Get the value of the frame pointer register (fp) so that
    # we can use the address of loc_tuple.
    let (__fp__, _) = get_fp_and_pc()
    # Since the tuple elements are next to each other we can use the
    # address of loc_tuple as a pointer to the 5 locations.
    verify_location_list(
        loc_list=cast(&loc_tuple, Location*), n_steps=4)

    ##### Here is what I added #####
    local locs : Location* = cast(&loc_tuple, Location*)
    tempvar loc = [locs]
    tempvar row = loc.row
    serialize_word(row)
    ################################

    return ()
end

我在loc_tuple 中添加了用于打印第一行的行。但是,Cairo 编译器给了我以下错误:

Traceback (most recent call last):
  File "/Users/yijiachen/cairo_venv/bin/cairo-compile", line 10, in <module>
    sys.exit(main())
  File "/Users/yijiachen/cairo_venv/lib/python3.8/site-packages/starkware/cairo/lang/compiler/cairo_compile.py", line 397, in main
    cairo_compile_common(
  File "/Users/yijiachen/cairo_venv/lib/python3.8/site-packages/starkware/cairo/lang/compiler/cairo_compile.py", line 121, in cairo_compile_common
    assembled_program = assemble_func(
  File "/Users/yijiachen/cairo_venv/lib/python3.8/site-packages/starkware/cairo/lang/compiler/cairo_compile.py", line 367, in 
    cairo_assemble_program
    check_main_args(program)
  File "/Users/yijiachen/cairo_venv/lib/python3.8/site-packages/starkware/cairo/lang/compiler/cairo_compile.py", line 296, in check_main_args
    assert main_args == expected_builtin_ptrs, (
AssertionError: Expected main to contain the following arguments (in this order): []. Found: ['output_ptr'].

我尝试过各种serialize_word 语句,但似乎都没有。这个问题以前从未出现在其他 serialize_word 语句中,包括在本教程的前面部分。

【问题讨论】:

    标签: blockchain ethereum starknet cairo-lang


    【解决方案1】:

    在代码顶部声明%builtins output,以便编译器知道您在主函数中使用了隐式参数(output_ptr)并期待它。 只有在声明要使用隐式参数时,Cairo 编译器才能处理它们。见here

    【讨论】:

      猜你喜欢
      • 2019-09-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-08-02
      • 1970-01-01
      • 2012-05-08
      相关资源
      最近更新 更多