-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: tracking undelivered request Ids #11
Changes from 6 commits
8f1cc1e
ec14f36
a37a459
bc5f8f1
bd9abb2
f44f925
a8da4a4
720be7d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,7 @@ coverage | |
coverage.json | ||
typechain | ||
*.DS_Store | ||
.idea | ||
|
||
# Hardhat files | ||
artifacts | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,3 @@ | ||
|
||
[submodule "lib/mech"] | ||
path = lib/mech | ||
url = https://github.com/gnosis/mech.git | ||
|
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# AI registry mech ABIs | ||
0.8.19 ABIs were obtained with 750 optimization passes. | ||
0.8.21 ABIs were obtained with 1000000 optimization passes. |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,19 +22,26 @@ error AgentNotFound(uint256 agentId); | |
/// @param expected Expected amount. | ||
error NotEnoughPaid(uint256 provided, uint256 expected); | ||
|
||
/// @dev Request Id not found. | ||
/// @param requestId Request Id. | ||
error RequestIdNotFound(uint256 requestId); | ||
|
||
/// @title AgentMech - Smart contract for extending ERC721Mech | ||
/// @dev A Mech that is operated by the holder of an ERC721 non-fungible token. | ||
contract AgentMech is ERC721Mech { | ||
event Perform(address indexed sender, bytes32 taskHash); | ||
event Deliver(address indexed sender, uint256 requestId, bytes data); | ||
event Request(address indexed sender, uint256 requestId, bytes data); | ||
event PriceUpdated(uint256 price); | ||
|
||
// Minimum required price | ||
uint256 public price; | ||
// Number of undelivered requests | ||
uint256 public numUndeliveredRequests; | ||
|
||
// Map of requests counts for corresponding addresses | ||
mapping (address => uint256) public mapRequestsCounts; | ||
// Map of request Ids | ||
mapping (uint256 => uint256[2]) public mapRequestIds; | ||
Comment on lines
+48
to
+49
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A two-way circular map stores all the undelivered requests and correctly removes elements when requests are delivered. |
||
|
||
/// @dev AgentMech constructor. | ||
/// @param _token Address of the token contract. | ||
|
@@ -52,6 +59,7 @@ contract AgentMech is ERC721Mech { | |
revert AgentNotFound(_tokenId); | ||
} | ||
|
||
// Record the price | ||
price = _price; | ||
} | ||
|
||
|
@@ -62,15 +70,51 @@ contract AgentMech is ERC721Mech { | |
revert NotEnoughPaid(msg.value, price); | ||
} | ||
|
||
// Get the request Id | ||
requestId = getRequestId(msg.sender, data); | ||
// Increase the requests count supplied by the sender | ||
mapRequestsCounts[msg.sender]++; | ||
|
||
// Record the request Id in the map | ||
// Get previous and next request Ids of the first element | ||
uint256[2] storage requestIds = mapRequestIds[0]; | ||
// Create the new element | ||
uint256[2] storage newRequestIds = mapRequestIds[requestId]; | ||
|
||
// Previous element will be zero, next element will be the current next element | ||
uint256 curNextRequestId = requestIds[1]; | ||
newRequestIds[1] = curNextRequestId; | ||
// Next element of the zero element will be the newly created element | ||
requestIds[1] = requestId; | ||
// Previous element of the current next element will be the newly created element | ||
mapRequestIds[curNextRequestId][0] = requestId; | ||
|
||
// Increase the number of undelivered requests | ||
numUndeliveredRequests++; | ||
|
||
emit Request(msg.sender, requestId, data); | ||
} | ||
|
||
/// @dev Delivers a request. | ||
/// @param requestId Request id. | ||
/// @param data Self-descriptive opaque data-blob. | ||
function deliver(uint256 requestId, bytes memory data) external onlyOperator { | ||
// Remove delivered request Id from the request Ids map | ||
uint256[2] memory requestIds = mapRequestIds[requestId]; | ||
// Check if the request Id is invalid: previous and next request Ids are zero, | ||
// and the zero's element next request Id is not equal to the provided request Id | ||
if (requestIds[0] == 0 && requestIds[1] == 0 && mapRequestIds[0][1] != requestId) { | ||
revert RequestIdNotFound(requestId); | ||
} | ||
|
||
// Re-link previous and next elements between themselves | ||
mapRequestIds[requestIds[0]][1] = requestIds[1]; | ||
mapRequestIds[requestIds[1]][0] = requestIds[0]; | ||
// Delete the delivered element from the map | ||
delete mapRequestIds[requestId]; | ||
// Decrease the number of undelivered requests | ||
numUndeliveredRequests--; | ||
|
||
emit Deliver(msg.sender, requestId, data); | ||
} | ||
|
||
|
@@ -95,4 +139,23 @@ contract AgentMech is ERC721Mech { | |
function getRequestsCount(address account) external view returns (uint256 requestsCount) { | ||
requestsCount = mapRequestsCounts[account]; | ||
} | ||
|
||
/// @dev Gets the set of undelivered request Ids. | ||
/// @return requestIds Set of undelivered request Ids. | ||
function getUndeliveredRequestIds() external view returns (uint256[] memory requestIds) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we need to worry about the number of undelivered requests being too large. Although unlikely, I think it may happen. Could that result in a big request to the rpc which would get dropped constantly? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good point. How about we slightly alter the method to allow to (optionally) set a batch size. Then you can get constant sized returns There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @DavidMinarsch that should do There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I've accounted for both size and offset, please take a look. |
||
// Get the number of undelivered requests | ||
uint256 numRequests = numUndeliveredRequests; | ||
|
||
if (numRequests > 0) { | ||
requestIds = new uint256[](numRequests); | ||
|
||
// The first request Id is the next request Id of the zero element in the request Ids map | ||
uint256 curRequestId = mapRequestIds[0][1]; | ||
for (uint256 i = 0; i < numRequests; ++i) { | ||
requestIds[i] = curRequestId; | ||
// Next request Id of the current element based on the current request Id | ||
curRequestId = mapRequestIds[curRequestId][1]; | ||
} | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1 @@ | ||
{ | ||
"contractVerification": true, | ||
"useLedger": false, | ||
"derivationPath": "m/44'/60'/2'/0/0", | ||
"providerName": "chiado", | ||
"gasPriceInGwei": "2", | ||
"baseURI": "https://gateway.autonolas.tech/ipfs/", | ||
"agentRegistryName": "AI Agent Registry", | ||
"agentRegistrySymbol": "AI-AGENT-V1", | ||
"agentRegistryAddress": "0x9dEc6B62c197268242A768dc3b153AE7a2701396", | ||
"agentFactoryAddress": "0x060268D4fE1Eb97843B14dd98cDf9ef7fbb3c720", | ||
"agentMechAddress": "0x0a3cfc6bee9658eda040e6bb366fe963ddce82c9", | ||
"agentId": "1", | ||
"price": "1000000000000000000" | ||
} | ||
{"contractVerification":true,"useLedger":false,"derivationPath":"m/44'/60'/2'/0/0","providerName":"chiado","gasPriceInGwei":"2","baseURI":"https://gateway.autonolas.tech/ipfs/","agentRegistryName":"AI Agent Registry","agentRegistrySymbol":"AI-AGENT-V1","agentRegistryAddress":"0x9dEc6B62c197268242A768dc3b153AE7a2701396","agentFactoryAddress":"0x1d333b46dB6e8FFd271b6C2D2B254868BD9A2dbd","agentMechAddress":"0x0a3cfc6bee9658eda040e6bb366fe963ddce82c9","agentId":"1","price":"1000000000000000000"} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This event is not used.