Solidity合约草稿
Posted tianxia2s
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Solidity合约草稿相关的知识,希望对你有一定的参考价值。
首先是没有写出来,问题和效率非常大的初稿
pragma solidity ^0.4.11; contract FindCorn { // 三种关键角色 源产地 经销商与消费者 struct Consumer_Dealer_Origin { uint count; //当前代表角色玉米总数 string place; //当前代表角色地理位置信息 uint begin_time; //当前代表角色获得玉米时刻 uint end_time; //当前代表角色失去玉米时刻 } struct Transaction { //记录交易双方信息 address from_i; address to_j; } address [] Head; //存储root节点信息 Transaction [] Node; //存储两两交易信息 //当前角色与其地址的映射关系 mapping(address => Consumer_Dealer_Origin) public identify; //收获玉米啦,取到多少算多少 function issue(address input,uint count,uint nowtime) returns (address) { identify[input].count = identify[input].count + count; identify[input].begin_time = nowtime; Head.push(input); return input; } //玉米流通啦,卖玉米啦 function transfer(address from1,address to,uint num) returns (bool){ if(identify[from1].count>=num){ identify[from1].count = identify[from1].count - num; identify[to].count = identify[to].count + num; Node.push(Transaction({from_i:from1,to_j:to})); return true; } return false; } //查询账户剩余玉米数 function getCornCount(address query) returns (uint){ return (identify[query].count); } function IsInHead(address i) returns (bool){ for(uint j=0;j < Head.length;j++){ if(Head[j]==i) return true; } return false; } //消费者查询:我的玉米从哪里来 function LeafQuery(address consumer) returns (address){ if(!IsInHead(consumer)){ for(uint i=0;i<Node.length;i++){ if(Node[i].to_j==consumer){ // return LeafQuery(Node[i].from_i)+"Send Addr: "+Node[i].from_i; return Node[i].from_i; } } } } //经销商查询:我的玉米从哪里来 function NodeQueryCeil(address corn) returns (address) { return LeafQuery(corn); } //经销商查询:玉米去哪了 function NodeQueryFloor() { } }
可用最新版:
1 pragma solidity ^0.4.11; 2 3 contract FindCorn { 4 // 三种关键角色 源产地 经销商与消费者 5 struct Consumer_Dealer_Origin { 6 uint count; //当前代表角色玉米总数 7 //string place; //当前代表角色地理位置信息 8 //uint begin_time; //当前代表角色获得玉米时刻 9 //uint end_time; //当前代表角色失去玉米时刻 10 11 //以上多点均为玉米溯源过程中所需信息 12 //可以根据具体需求进行增减 13 //重点关注整体的框架设计及信息的流转 14 address father; //连接当前父节点 15 address[] child; //连接当前节点的子节点 16 } 17 18 address[] Head; //存储root节点信息 19 mapping(address => Consumer_Dealer_Origin) identify; //当前角色与其地址的映射关系 20 21 // function GetNewNode(string placename) returns(Consumer_Dealer_Origin) { 22 // Consumer_Dealer_Origin A = new Consumer_Dealer_Origin({ 23 // count : 0, 24 // place : "", 25 // begin_time : 0, 26 // end_time : 0, 27 28 // father : ‘0x00000000‘ 29 30 // }); 31 // return A; 32 // } 33 34 //收获玉米啦,取到多少算多少 35 function issue(address input,uint count) returns (string, address, uint) { 36 identify[input].count = identify[input].count + count; 37 // identify[input].begin_time = nowtime; 38 Head.push(input); 39 return ("add corn success!",input,count); 40 } 41 42 //玉米流通啦,卖玉米啦 43 function transfer(address from1,address to,uint num) returns (string,bool){ 44 if(identify[from1].count>=num){ 45 identify[from1].count = identify[from1].count - num; 46 identify[to].count = identify[to].count + num; 47 48 //确定玉米流通的流向关系 49 identify[from1].child.push(to); 50 identify[to].father = from1; 51 52 return ("add the corn success!",true); 53 } 54 return ("this from1 don‘t have enough corn!",false); 55 } 56 57 //查询账户剩余玉米数 58 function getCornCount(address query) returns (address,uint){ 59 return (identify[query].father, identify[query].count); 60 } 61 62 63 function IsInHead(address i) returns (bool){ 64 for(uint j=0;j < Head.length;j++){ 65 if(Head[j]==i) 66 return true; 67 } 68 return false; 69 } 70 71 address []addrpath = new address[](1); 72 //消费者查询:我的玉米从哪里来 73 function LeafQuery(address consumer) returns (address[]){ 74 addrpath.length = 0; 75 addrpath[addrpath.length++]=consumer; 76 while(!IsInHead(addrpath[addrpath.length-1])){ 77 consumer = identify[addrpath[addrpath.length-1]].father; 78 addrpath[addrpath.length++]=consumer; 79 } 80 return addrpath; 81 } 82 83 //经销商查询:我的玉米从哪里来 84 function NodeQueryCeil(address corn) returns (address[]) { 85 return LeafQuery(corn); 86 } 87 88 //经销商查询:玉米去哪了 89 address []queue = new address[](1); 90 uint index1=0; 91 uint index2=1; 92 function NodeQueryFloor(address corn) returns (address[]){ 93 //对经销商节点开始进行层次遍历,查找出所有的叶子节点 94 addrpath.length = 0; 95 queue.length=0; 96 queue[queue.length++]=corn; 97 while(index1!=index2){ 98 if(identify[queue[index1]].child.length==0){ 99 addrpath[addrpath.length++]=queue[index1]; 100 } 101 index2 = index2+identify[queue[index1]].child.length; 102 for(uint i=0;i<identify[queue[index1]].child.length;i++){ 103 queue[queue.length++]=identify[queue[index1]].child[i]; 104 } 105 index1++; 106 } 107 return addrpath; 108 } 109 110 }
以上是关于Solidity合约草稿的主要内容,如果未能解决你的问题,请参考以下文章