美文网首页
智能合约的局限性

智能合约的局限性

作者: bradyjoestar | 来源:发表于2018-04-17 16:40 被阅读0次

智能合约的局限性

复合数据的弱可见性

数组

在智能合约中,数组可以作为返回值直接返回。但必须为基本数据类型。
返回代码案例:

uint[10] intArray;// size must be fixed
function getIntArray() constant returns (uint[10])
    {
        return intArray;
    }
function setData(uint a) external {
        intArray[0]=12;
        intArray[1]=13;
        intArray[2]=14;
        intArray[3]=15;
        intArray[4]=16;
        intArray[5]=12;
        data = a;
    }

映射map

在智能合约中,映射map无法直接作为返回值直接返回。只能单次返回某个key的value。

结构体

在智能合约中,结构体struct无法直接作为返回值直接返回,如果需要返回结构体的数据,必须逐一返回,并在接收处完成数据的拼装。


pragma solidity 0.4.13;

contract Project
{
    struct Person {
        string name;
        uint funds;
    }
    
    mapping(address => Person) people;
    
    function getBryn()
        public
        returns (Person)
    {
        return Person({ name: "Bryn", funds: 123 });
    }
    
    function getPerson(address id)
        public
        returns (Person)
    {
        return people[id];
    }
}

报错信息如下:
TypeError: Internal type is not allowed for public or external functions.

正确获得结构体数据的方法:

pragma solidity ^0.4.13;

contract Project
{
    struct Person {
        string name;
        uint funds;
    }
    
    // this is the mapping for which we want the
    // compiler to automatically generate a getter.
    mapping(address => Person) public people;
    
    // this is what the automatically generated getter
    // looks like (we don't need to implement this ourselves).
    function getPerson(address id)
        public
        returns (string name, uint funds)
    {
        // copy the data into memory
        Person memory p = people[id];
        
        // break the struct's members out into a tuple
        // in the same order that they appear in the struct
        return (p.name, p.funds);
    }
}

智能合约打包区块最大交易字节数

交易的大小限制并不是一个固定数,截至到16.2.7号限制大概是 780KB(大约300w gas)

交易和区块的大小都没有固定的限制,它可以自动伸缩,但是这并不是意味着完全没有限制,当前单个区块最多的gas使用量为3,141,592 gas,理论上来讲,你可以创建一个可以消耗单个区块所有gas的,很大的单笔交易。

来个栗子,发送一个合约附带256kb的随机数据:

上述交易大概需要耗费900w gas, 这个测试目前是通不过的。Mist会尝试创建这个交易,但它是无效的。

智能合约的读写与计算

以排行榜为例,需要维护一个50条的排行榜记录,总计共两百家发电厂。

假设以一个address数组保存所有的发电站的地址,根据发电站的地址可以获得它们生产绿信数。

目前策略有两种,动态维护排行榜和固定时间统一进行排序数据。

上述两种排序均需要实现排序算法。

动态维护排行榜

动态维护排行榜需要每次有新的交易进入需要进行一次排序。

假设一次交易涉及一次写入操作,记为0(1),时间复杂度记为0(1)

那么单次插入排序的时间复杂度O(lgn),写入次数为O(n/2)

每次有新的交易生成都需要进行排序。可能同时有多个交易进来,需要设立锁机制,并且可能会因为排序时间长,请求无法获得相应,time out。

固定时间统一进行排序

固定时间统一进行排序时间复杂度为O(nlgn),写入次数为n(n-1)/2。

过大的交易具有打包不进入区块的风险,拆分成几个小的模块进行分组排序显得不是很必要。

智能合约的被动调用

在智能合约中,只能等待外部的调用,而无法执行定时任务。

同时也无法主动发起外部请求。

相关文章

网友评论

      本文标题:智能合约的局限性

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