move debug string的几种方法

  • 晓道
  • 更新于 2022-10-07 21:02
  • 阅读 1707

关于move debug string的几种方法。

最近在关注大狗哥的Move-Bounties-For-Hackerhouse,感觉还不错,能赚点usdt,还说刷pr,所以去刷了两个。 一个是关于aptos abi 输出美化的。 一个是关于move debug string的u8数组转化string打印的。

这里讲讲move debug string的几种方法,很有幸,都是我写的。

最初我是看了move dao的教程,里面有node js版的代码。 我手写了一个一行代码搞定转换的python 版。在微信群聊过程中,别人提出,要整体转换,我又写了一个python接受管道输入,转换输出的。

效果如下:

Fj42vlbg6340226ad6be5.jpg

代码在这里:

https://gist.github.com/daog1/9b3000fe5cceb04db511078ee9489ee5

加入Move-Bounties,搞了这个,movetool 的代码,所以又顺便提交了一个pr,把这个功能加到了movetool里面。

效果如下:

jRYGZorm6340220e55713.jpg

我改代码,总是不知疲惫。 今晚又试了一下,直接改到move stdlib里面,成功搞定,简单分享一下。

1,增加move 函数

//代码位置: language/move-stdlib/nursery/sources/debug.move
/// Module providing debug functionality.
module std::debug {
    native public fun print<T>(x: &T);
    native public fun print_str(data: vector<u8>); //增加代码
    native public fun print_stack_trace();
}

2,增加native 函数

//代码位置: language/move-stdlib/src/natives/debug.rs
#[inline]
fn native_print_str(
    gas_params: &PrintStrGasParameters,
    _context: &mut NativeContext,
    mut ty_args: Vec<Type>,
    mut args: VecDeque<Value>,
) -> PartialVMResult<NativeResult> {
    debug_assert!(ty_args.len() == 1);
    debug_assert!(args.len() == 1);

    // No-op if the feature flag is not present.
    #[cfg(feature = "testing")]
    {
        //let _ty = ty_args.pop().unwrap();
        let r = pop_arg!(args, Vec<u8>);

        //let mut buf = String::new();
        let s = String::from_utf8_lossy(&r);
        println!("[debug] {}", s);
    }

    Ok(NativeResult::ok(gas_params.base_cost, smallvec![]))
}
pub fn make_native_print_str(gas_params: PrintStrGasParameters) -> NativeFunction {
    Arc::new(
        move |context, ty_args, args| -> PartialVMResult<NativeResult> {
            native_print_str(&gas_params, context, ty_args, args)
        },
    )
}
#[derive(Debug, Clone)]
pub struct GasParameters {
    pub print: PrintGasParameters,
    pub print_str: PrintStrGasParameters, //加入print_str
    pub print_stack_trace: PrintStackTraceGasParameters,
}

pub fn make_all(gas_params: GasParameters) -> impl Iterator<Item = (String, NativeFunction)> {
    let natives = [
        ("print", make_native_print(gas_params.print)),
        ("print_str", make_native_print_str(gas_params.print_str)), //加入print_str
        (
            "print_stack_trace",
            make_native_print_stack_trace(gas_params.print_stack_trace),
        ),
    ];

    make_module_natives(natives)
}
// 改动位置:language/move-stdlib/src/natives/mod.rs
impl NurseryGasParameters {
    pub fn zeros() -> Self {
        Self {
            event: event::GasParameters {
                write_to_event_store: event::WriteToEventStoreGasParameters {
                    unit_cost: 0.into(),
                },
            },
            debug: debug::GasParameters {
                print: debug::PrintGasParameters {
                    base_cost: 0.into(),
                },
                print_str: debug::PrintStrGasParameters { //新加入的
                    base_cost: 0.into(),
                },
                print_stack_trace: debug::PrintStackTraceGasParameters {
                    base_cost: 0.into(),
                },
            },
        }
    }
}
//测试代码
0x01::debug::print_str(b"hello");

达到这样的效果:

5044577022482950639.jpg

为什么要这么改,建议熟读代码。

点赞 1
收藏 1
分享
本文参与登链社区写作激励计划 ,好文好收益,欢迎正在阅读的你也加入。

0 条评论

请先 登录 后评论
晓道
晓道
0xdD09...9161
技术交流:https://t.me/realDAO