问题描述
我有以下测试智能合约:
pragma solidity ^0.8.4;
contract Test {
mapping(uint => S_Item) public items;
uint itemIndex;
enum State {
Created,Paid,Delivered
}
struct S_Item {
string _identifier;
uint _itemPrice;
State _state;
}
State public state;
event SupplyChainStep(uint _itemIndex,uint _step);
function createItem(string memory _identifier,uint _itemPrice) public {
items[itemIndex]._identifier = _identifier;
items[itemIndex]._itemPrice = _itemPrice;
state = State.Created;
items[itemIndex]._state = state;
emit SupplyChainStep(itemIndex,uint(items[itemIndex]._state));
itemIndex++;
}
function triggerPayment(uint _itemIndex) public payable {
require(items[_itemIndex]._itemPrice == msg.value,"Only full payments accepted");
require(items[_itemIndex]._state == State.Created,"Item is further in the chain");
state = State.Paid;
items[itemIndex]._state = state;
emit SupplyChainStep(itemIndex,uint(items[itemIndex]._state));
}
function triggerDelivery(uint _itemIndex) public {
require(items[_itemIndex]._state == State.Paid,"Item is further in the chain");
state = State.Delivered;
items[itemIndex]._state = state;
emit SupplyChainStep(itemIndex,uint(items[itemIndex]._state));
}
}
createItem 函数正确更新了项目映射 _identifier 和 _itemPrice 属性。 _state 属性的默认值为 0,但如果我更改顺序以使 Created 在枚举中占据位置 1 而不是 0,则 item _state 枚举属性会像我预期的那样更新为数字 1。
另外两个函数不会在item映射中推进状态。它保持在项目映射中的任何当前值,其代码与在 createItem 函数中工作的代码相同。
注意...
state = State.XXX
...这些函数中的每一个中的赋值 - 我将其用于故障排除 - 当运行 triggerPayment 或 triggerDelivery 函数时,状态会正确填充到名为 state 的公共变量中。这三个函数有效,但项目映射 _state 属性拒绝以相同的方式更新,这就是我需要工作的。
谁能帮助我理解我可以做些什么来让所有函数按预期推进项目映射 _state 属性,就像 createItems 函数似乎没有问题一样?
谢谢!
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)