Solidity合约:玉米生产溯源

本文介绍如何使用Solidity合约实现玉米生产溯源。通过结构体或JSON存储关键信息,利用issue、transfer、getCornCount等函数跟踪玉米从源产地到消费者的流转过程,确保全程可追溯。同时,讨论了合约数据持久化的重要性以及不同物品溯源的策略。
摘要由CSDN通过智能技术生成

实现思路:

首先用地址与每个结构进行映射,将关键信息储存在结构体中;或者将关键信息在外部通过json储存,内部储存对应的hash值;

使用issue函数表示:玉米地中收获足够数量的玉米并进行记录;

使用transfer函数表示:玉米在源产地与经销商手中流转,最终流转至消费者手中;

使用getCornCount函数:查询当前该角色所拥有的玉米数量;

使用IsInHead函数:判断当前该角色是否为玉米源产地;

使用LeafQuery函数:消费者查询玉米的来路,进行溯源操作;

使用NodeQueryFloor函数:经销商查询玉米的去路,进行商品去路调研获取数据,以便后期进行分析;

话不多说,先上代码:

  1 pragma solidity ^0.4.11;
  2 
  3 //注意一些关键原则
  4 //自己不能给自己转玉米:确保不形成自环,且无现实意义;
  5 //每个地址代表一个角色;
  6 
  7 contract FindCorn {
  8     // 三种关键角色 源产地 经销商与消费者
  9     struct Consumer_Dealer_Origin {
 10         uint count;             //当前代表角色玉米总数
 11         //string place;           //当前代表角色地理位置信息
 12         //uint begin_time;        //当前代表角色获得玉米时刻
 13         //uint end_time;          //当前代表角色失去玉米时刻
 14         
 15         //以上多点均为玉米溯源过程中所需信息
 16         //可以根据具体需求进行增减
 17         //重点关注整体的框架设计及信息的流转
 18         address father;         //连接当前父节点
 19         address[] child;        //连接当前节点的子节点
 20     }
 21     
 22     address[] Head;            //存储root节点信息
 23     mapping(address => Consumer_Dealer_Origin) identify;    //当前角色与其地址的映射关系
 24     
 25     // function GetNewNode(string placename) returns(Consumer_Dealer_Origin) {
 26     //     Consumer_Dealer_Origin A = new Consumer_Dealer_Origin({
 27     //       count : 0,
 28     //       place : "",
 29     //       begin_time : 0,
 30     //       end_time : 0,
 31            
 32     //       father : '0x00000000'
 33 
 34     //     });
 35     //     return A;
 36     // }    
 37     
 38     //收获玉米啦,取到多少算多少
 39     function issue(address input,uint count) returns (string, address, uint) {
 40         identify[input].count = identify[input].count + count;
 41     //    identify[input].begin_time = nowtime;
 42         Head.push(input);
 43         return ("add corn success!",input,count);
 44     }    
 45     
 46     //玉米流通啦,卖玉米啦
 47     //地址本身不能进行玉米流通
 48     function transfer(address from1,address to,uint num) returns (string,bool){
 49         if(from1==to){
 50             return ("you can't transfer corn to yourself",false);
 51         }else if(num==0){
 52             return ("you can't transfer zero corn to others",false);
 53         }
 54         if(identify[from1].count>=num){
 55             identify[from1].count = identify[from1].count - num;
 56             identify[to].count = identify[to].count + num;
 57             
 58             //确定玉米流通的流向关系
 59             identify[from1].child.push(to);
 60             identify[to].father = from1;
 61             
 62             return ("add the corn success!",true);
 63         }
 64         return ("this from1 don't have enough corn!",false);
 65     }
 66     
 67     //查询账户剩余玉米数
 68     function getCornCount(address query) returns (address,uint){
 69         return (identify[query].father, identify[query].count);
 70     }
 71     
 72 
 73     function IsInHead(address i) returns (bool){
 74         for(uint j=0;j < Head.length;j++){
 75             if(Head[j]==i)
 76                 return true;
 77         }
 78         return false;
 79     }
 80     
 81     address []addrpath = new address[](1);
 82     //消费者查询:我的玉米从哪里来
 83     function LeafQuery(address consumer) returns (address[]){
 84         addrpath.length = 0;
 85         addrpath[addrpath.length++]=consumer;
 86         while(!IsInHead(addrpath[addrpath.length-1])){
 87             consumer = identify[addrpath[addrpath.length-1]].father;
 88             addrpath[addrpath.length++]=consumer;
 89         }
 90         return addrpath;
 91     }
 92     
 93     //经销商查询:我的玉米从哪里来
 94     function NodeQueryCeil(address corn) returns (address[]) {
 95         return LeafQuery(corn);
 96     }
 97     
 98     
  • 1
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值