Solidity 代码反馈 - 基本任务跟踪存储和检索
Solidity Code Feedback - Basic Task Tracking Storage and Retrieval
我刚刚完成了一些基本的 Solidity 教程,想尝试使用基本的任务跟踪器...它基本上允许用户创建任务并将它们分配给用户和项目。
我最大的问题是映射部分。如果我想轻松检索给定项目的所有任务或给定用户的所有任务,我的实现是否正确?我有什么方法可以使用:
uint256 newIndex = _taskList.length;
_taskList.push(Task(project, title, description, false, dueDate, assignedUser, msg.value, msg.sender));
_userTasks[assignedUser].push(newIndex);
_projectTasks[project].push(newIndex);
是否会由于竞争条件或同时调用而导致错误信息?感谢任何和所有评论,谢谢!
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.7;
contract ProjectTaskList {
struct Task {
string project;
string title;
string description;
bool isCompleted;
uint256 dueDate;
address assignedUser;
uint256 payment;
address owner;
}
Task[] public _taskList;
mapping(address => uint256[]) _userTasks;
mapping(string => uint256[] ) _projectTasks;
function getContractBalance() public view returns (uint256){
return address(this).balance;
}
function addToDo(string memory project, string memory title, string memory description, uint256 dueDate, address assignedUser) public payable {
require(bytes(project).length > 0, "Project is required");
require(bytes(title).length > 0, "Title is required");
require(bytes(description).length > 0, "Description is required");
require(dueDate > 0, "DueDate is required");
require(assignedUser != address(0), "Assigned User is required");
uint256 newIndex = _taskList.length;
_taskList.push(Task(project, title, description, false, dueDate, assignedUser, msg.value, msg.sender));
_userTasks[assignedUser].push(newIndex);
_projectTasks[project].push(newIndex);
}
function getUserTasks(address user) public view returns (uint256[] memory){
return _userTasks[user];
}
function geProjectTasks(string memory projectName) public view returns (uint256[] memory){
return _projectTasks[projectName];
}
function updateTask(uint256 index, string memory project, string memory title, string memory description, uint256 dueDate, address assignedUser){
require(bytes(project).length > 0, "Project is required");
require(bytes(title).length > 0, "Title is required");
require(bytes(description).length > 0, "Description is required");
require(dueDate > 0, "DueDate is required");
require(assignedUser != address(0), "Assigned User is required");
Task foundTask = _taskList[index];
require(foundTask.owner == msg.sender, "You do not have the right to update this task");
// more code to see if the user or project has been assigned and update the Project and User array correctly
// then update the code
}
除了缺少评论外,我没有发现您的代码存在重大问题。也许一些气体优化是可行的
- 您可以限制
project
和 title
字段的大小。 EVM 以 256 位的块存储数据。因此,为它们中的每一个使用 128 位(最好更少)会花费更少的 gas,因为 EVM 将在一个块中处理这两个
- 您已将分配的用户保留在您的结构中。为用户任务使用第二个
mapping
似乎更快更干净,但存储操作的成本很高。用于该目的的 getter 函数会更便宜。
- 与 (2) 相同,但具有项目任务映射。
- 你不能有竞争条件。 EVM没有并行的概念。
我刚刚完成了一些基本的 Solidity 教程,想尝试使用基本的任务跟踪器...它基本上允许用户创建任务并将它们分配给用户和项目。
我最大的问题是映射部分。如果我想轻松检索给定项目的所有任务或给定用户的所有任务,我的实现是否正确?我有什么方法可以使用:
uint256 newIndex = _taskList.length;
_taskList.push(Task(project, title, description, false, dueDate, assignedUser, msg.value, msg.sender));
_userTasks[assignedUser].push(newIndex);
_projectTasks[project].push(newIndex);
是否会由于竞争条件或同时调用而导致错误信息?感谢任何和所有评论,谢谢!
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.7;
contract ProjectTaskList {
struct Task {
string project;
string title;
string description;
bool isCompleted;
uint256 dueDate;
address assignedUser;
uint256 payment;
address owner;
}
Task[] public _taskList;
mapping(address => uint256[]) _userTasks;
mapping(string => uint256[] ) _projectTasks;
function getContractBalance() public view returns (uint256){
return address(this).balance;
}
function addToDo(string memory project, string memory title, string memory description, uint256 dueDate, address assignedUser) public payable {
require(bytes(project).length > 0, "Project is required");
require(bytes(title).length > 0, "Title is required");
require(bytes(description).length > 0, "Description is required");
require(dueDate > 0, "DueDate is required");
require(assignedUser != address(0), "Assigned User is required");
uint256 newIndex = _taskList.length;
_taskList.push(Task(project, title, description, false, dueDate, assignedUser, msg.value, msg.sender));
_userTasks[assignedUser].push(newIndex);
_projectTasks[project].push(newIndex);
}
function getUserTasks(address user) public view returns (uint256[] memory){
return _userTasks[user];
}
function geProjectTasks(string memory projectName) public view returns (uint256[] memory){
return _projectTasks[projectName];
}
function updateTask(uint256 index, string memory project, string memory title, string memory description, uint256 dueDate, address assignedUser){
require(bytes(project).length > 0, "Project is required");
require(bytes(title).length > 0, "Title is required");
require(bytes(description).length > 0, "Description is required");
require(dueDate > 0, "DueDate is required");
require(assignedUser != address(0), "Assigned User is required");
Task foundTask = _taskList[index];
require(foundTask.owner == msg.sender, "You do not have the right to update this task");
// more code to see if the user or project has been assigned and update the Project and User array correctly
// then update the code
}
除了缺少评论外,我没有发现您的代码存在重大问题。也许一些气体优化是可行的
- 您可以限制
project
和title
字段的大小。 EVM 以 256 位的块存储数据。因此,为它们中的每一个使用 128 位(最好更少)会花费更少的 gas,因为 EVM 将在一个块中处理这两个 - 您已将分配的用户保留在您的结构中。为用户任务使用第二个
mapping
似乎更快更干净,但存储操作的成本很高。用于该目的的 getter 函数会更便宜。 - 与 (2) 相同,但具有项目任务映射。
- 你不能有竞争条件。 EVM没有并行的概念。