美文网首页波卡一起学
如何用Js对decl_module中的方法进行测试

如何用Js对decl_module中的方法进行测试

作者: 空乱木 | 来源:发表于2019-10-02 09:14 被阅读0次

    本文是对学习知识的总结和探索;如有不对的地方请指正;多谢;

    学习了将近一个月的波卡开发,可是对一些测试的问题还是比较陌生;
    如何使用Js对编写的decl_module中的方法进行测试,也成了一个不得不总结的问题;一句话会的人,不觉得难,不会的人找不到合适的方法;

    1: 代码

    #[derive(Encode, Decode, Default)]
    pub struct Kitty(pub [u8; 16]);
    
    decl_storage! {
        trait Store for Module<T: Trait> as Kitties {
            /// Stores all the kitties, key is the kitty id / index
            pub Kitties get(kitty): map u32 => Kitty;
            /// Stores the total number of kitties. i.e. the next kitty index
            pub KittiesCount get(kitties_count): u32;
        }
    }
    
    decl_module! {
        pub struct Module<T: Trait> for enum Call where origin: T::Origin {
            /// Create a new kitty
            pub fn create(origin) {
                let sender = ensure_signed(origin)?;
                let count = Self::kitties_count();
                if count == u32::max_value() {
                    return Err("Kitties count overflow”);
                }
                let payload = (<system::Module<T>>::random_seed(), sender, <system::Module<T>>::extrinsic_index(), <system::Module<T>>::block_number());
                let dna = payload.using_encoded(blake2_128);
                let kitty = Kitty(dna);
                Kitties::insert(count, kitty);
                KittiesCount::put(count + 1);
            }
        }
    }
    

    2:启动环境

    cargo run - - - - dev

    2019-10-01 22:17:56 Prepared block for proposing at 7 [hash: 0x0b8e3245c6f8ae2972e9dbd64f45d985b0048fe6f49e7f312df1d40c9aae18ec; parent_hash: 0xedeb…fe45; extrinsics: [0x2d12…ab29]]
    2019-10-01 22:17:56 Discarding proposal for slot 784969737; block production took too long
    2019-10-01 22:17:56 Starting consensus session on top of parent 0xedebee52b36ac5c9e9e24e6a796d026ae53f9bed6d2eccf2d9295bc46905fe45
    2019-10-01 22:17:57 Idle (0 peers), best: #6 (0xedeb…fe45), finalized #5 (0x3d2f…7d38), ⬇ 0 ⬆ 0
    2019-10-01 22:17:58 Prepared block for proposing at 7 [hash: 0xea691d22666bde8c684b3d2d8392207a3bb9d7674f71d668206f631102b08aba; parent_hash: 0xedeb…fe45; extrinsics: [0x4e9d…7a6d]]
    2019-10-01 22:17:58 Discarding proposal for slot 784969738; block production took too long
    2019-10-01 22:17:58 Starting consensus session on top of parent 0xedebee52b36ac5c9e9e24e6a796d026ae53f9bed6d2eccf2d9295bc46905fe45
    2019-10-01 22:18:00 Prepared block for proposing at 7 [hash: 0x96f22c8ad05a67b73b31772e025ee46d7005706d3782b2c92bfbbbfe9011b787; parent_hash: 0xedeb…fe45; extrinsics: [0x2e3d…5aad]]
    2019-10-01 22:18:00 Discarding proposal for slot 784969739; block production took too long
    2019-1

    3:打开浏览器

    https://polkadot.js.org/apps/#/explorer

    在Setting中选择Local Node 127.0.0.1:9944


    image.png

    关注点

    1- chainstate
    可以查看decl_storage 中定义的存储的值

    image.png

    2- extrinsics
    可以查看decl_module中定义的方法,并且可以进行测试;
    点击extrinsics —> kitties —> create


    image.png

    点击Submit Transaction


    image.png

    这个到底是什么问题?
    Discarding proposal for slot 784969987; block production took too long

    substrate-kitties purge-chain —dev
    可能需要用release build, cargo run --release -- —dev

    3 - chainstate 执行成功看记录内容


    image.png image.png

    声明:本文由River进行整理和编译,为原创作品,转载请注明出处,多谢;

    相关文章

      网友评论

        本文标题:如何用Js对decl_module中的方法进行测试

        本文链接:https://www.haomeiwen.com/subject/vphlpctx.html