Skip to content
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/index account #111

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft

Feat/index account #111

wants to merge 2 commits into from

Conversation

ErikssonJoakim
Copy link
Contributor

@ErikssonJoakim ErikssonJoakim commented Sep 29, 2023

This PR is a first attempt at indexing accounts in the blockchain. It indexes basic accounts when they interact with the objectarium smart contract or when they store a contract on the blockchain.

Summary by CodeRabbit

  • New Feature: Introduced new data types and fields to enhance our blockchain application's data model, including public key representation, account representation, and object pinning.
  • Improvement: Enhanced contract storage and permissions handling, including automatic account creation for message senders and permission address entries.
  • New Feature: Added two new functions to generate unique IDs, improving data organization and retrieval.
  • Improvement: Updated object handling to use sender IDs instead of sender names, and introduced object pinning functionality. This improves data accuracy and allows users to pin important objects for easy access.
  • Refactor: Updated various functions to use new data types and fields, improving overall system consistency and reliability.

src/mappings/contractHandlers.ts Outdated Show resolved Hide resolved
@ErikssonJoakim ErikssonJoakim marked this pull request as ready for review October 3, 2023 07:08
Copy link
Member

@amimart amimart left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good! 👍

@ccamel
Copy link
Member

ccamel commented Oct 24, 2023

@coderabbitai review

@coderabbitai
Copy link

coderabbitai bot commented Oct 24, 2023

Walkthrough

This update expands the data model of a Cosmos SDK blockchain application, introducing new types and fields. It enhances contract storage and permissions handling, adds functions for unique ID generation, and refines objectarium object handling.

Changes

File Summary
schema.graphql Introduced new types and fields related to a Cosmos SDK blockchain application, including PublicKey, Account, and ObjectariumObjectPin.
src/mappings/contractHandlers.ts Improved the handleStoreContract function with updated types and variables, added checks for account existence, and created ContractPermissionAccount entries for each permission address.
src/mappings/helper.ts Added two new functions, objectariumObjectPinId and contractPermissionAccountId, for unique ID generation.
src/mappings/objectariumHandlers.ts Enhanced objectarium object handling with new imports, account creation, and modifications to handlePinObject, handleUnpinObject, and handleInitObjectarium functions.

🐇

Code changes here, code changes there,

Enhancements everywhere.

With every pull, we refine,

Making our blockchain shine.

So hop along, there's more to see,

In this world of technology. 🚀


Tips

Chat with CodeRabbit Bot (@coderabbitai)

  • If you reply to a review comment from CodeRabbit, the bot will automatically respond.
  • To engage with CodeRabbit bot directly around the specific lines of code in the PR, mention @coderabbitai in your review comment
  • Note: Review comments are made on code diffs or files, not on the PR overview.
  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Commands (invoked as PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger a review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai help to get help.

Note: For conversation with the bot, please use the review comments on code diffs or files.

CodeRabbit Configration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • The JSON schema for the configuration file is available here.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/coderabbit-overrides.json

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review Status

Actionable comments generated: 1

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between f3f0a85 and 9e3ce9c.
Files selected for processing (4)
  • schema.graphql (6 hunks)
  • src/mappings/contractHandlers.ts (1 hunks)
  • src/mappings/helper.ts (1 hunks)
  • src/mappings/objectariumHandlers.ts (5 hunks)
Files skipped from review due to trivial changes (1)
  • src/mappings/helper.ts
Additional comments: 17
src/mappings/objectariumHandlers.ts (7)
  • 11-18: The new imports ObjectariumObjectPin and Account from ../types and objectariumObjectPinId from ./helper are introduced. Ensure these are used appropriately in the code.

  • 63-78: The code checks if an account exists for the sender. If not, it creates a new account with the sender's public key and an empty balance. This is a good practice to ensure that an account exists for each sender.

  • 80-94: The code creates an ObjectariumObject with the sender's ID and contract. If the object is pinned, it also creates an ObjectariumObjectPin. This is a good practice to keep track of pinned objects.

  • 119-135: The handlePinObject function now creates an ObjectariumObjectPin if it doesn't exist. This is a good practice to ensure that a pin exists for each object.

  • 152-152: The handleUnpinObject function now removes the ObjectariumObjectPin instead of filtering out the sender from the object's pins. This is a more efficient way to handle unpinning.

  • 185-185: The admin field is now being extracted from msg.msg.decodedMsg. Ensure that this field is used appropriately in the code.

  • 203-204: The Objectarium entity now has a creatorId and ownerId instead of an owner. This provides more information about the entity.

src/mappings/contractHandlers.ts (4)
  • 10-14: The findEventAttribute function is used to extract the contractId from the msg object. Ensure that the msg object always contains the store_code event and the code_id attribute. If not, this could lead to undefined behavior.

  • 16-20: The findEventAttribute function is used to extract the dataHash from the msg object. Ensure that the msg object always contains the store_code event and the code_checksum attribute. If not, this could lead to undefined behavior.

  • 26-42: The code checks if the sender account exists, and if not, it creates a new account. This is a good practice to ensure that the sender account always exists before proceeding with the contract creation. However, the publicKey extraction could be improved. The current implementation assumes that the first signerInfo with a publicKey is the one related to the sender. This might not always be the case, especially if the transaction has multiple signers. You should verify this assumption.

  • 44-45: The permission is extracted from instantiatePermission using the AccessType enum. Ensure that instantiatePermission.permission always contains a valid AccessType value. If not, this could lead to undefined behavior.

schema.graphql (6)
  • 69-125: The new PublicKey and Account types are well defined and provide a more structured way to represent accounts in the Cosmos SDK. The Account type includes fields for the account's public key, balances, stored and pinned objectarium objects, created and owned objectariums, created contracts, and contract permissions. This is a significant improvement over the previous representation of accounts as simple strings. However, ensure that all parts of the codebase that interact with accounts have been updated to use these new types.

  • 207-223: The new ObjectariumObjectPin type represents the pin of an objectarium object. It includes fields for the objectarium object being pinned and the account pinning the object. This is a more structured way to represent object pins compared to the previous representation as simple strings. However, ensure that all parts of the codebase that interact with object pins have been updated to use this new type.

  • 235-245: The ObjectariumObject type has been updated to use the new Account and ObjectariumObjectPin types. The sender field now refers to an Account instead of a string, and the pins field now refers to a list of ObjectariumObjectPin instead of a list of strings. This is a significant improvement over the previous representation. However, ensure that all parts of the codebase that interact with objectarium objects have been updated to use these new types.

  • 299-305: The Objectarium type has been updated to use the new Account type. The creator and owner fields now refer to an Account instead of a string. This is a significant improvement over the previous representation. However, ensure that all parts of the codebase that interact with objectariums have been updated to use these new types.

  • 337-351: The new ContractPermissionAccount type represents an account of a contract permission. It includes fields for the contract with the permission and the account concerned by the contract permission. This is a more structured way to represent contract permissions compared to the previous representation as simple strings. However, ensure that all parts of the codebase that interact with contract permissions have been updated to use this new type.

  • 368-379: The Contract type has been updated to use the new Account and ContractPermissionAccount types. The creator field now refers to an Account instead of a string, and a new permissionAccounts field has been added to represent a list of ContractPermissionAccount. This is a significant improvement over the previous representation. However, ensure that all parts of the codebase that interact with contracts have been updated to use these new types.

Comment on lines +54 to +67
for (const permissionAddress in instantiatePermission?.addresses) {
if (!(await Account.get(permissionAddress))) {
await Account.create({
id: permissionAddress,
balances: [],
}).save();
}

await ContractPermissionAccount.create({
id: contractPermissionAccountId(contractId, sender),
contractId: permissionAddress,
accountId: sender,
}).save();
}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The code iterates over instantiatePermission?.addresses and for each permissionAddress, it checks if an account exists, and if not, it creates a new account. Then, it creates a ContractPermissionAccount entry. This is a good practice to ensure that all permission addresses have corresponding accounts. However, the contractId and accountId seem to be swapped in the ContractPermissionAccount.create call. The contractId should be contractId and the accountId should be permissionAddress.

-            await ContractPermissionAccount.create({
-                id: contractPermissionAccountId(contractId, sender),
-                contractId: permissionAddress,
-                accountId: sender,
-            }).save();
+            await ContractPermissionAccount.create({
+                id: contractPermissionAccountId(contractId, permissionAddress),
+                contractId: contractId,
+                accountId: permissionAddress,
+            }).save();
Committable suggestion (Beta)
Suggested change
for (const permissionAddress in instantiatePermission?.addresses) {
if (!(await Account.get(permissionAddress))) {
await Account.create({
id: permissionAddress,
balances: [],
}).save();
}
await ContractPermissionAccount.create({
id: contractPermissionAccountId(contractId, sender),
contractId: permissionAddress,
accountId: sender,
}).save();
}
await ContractPermissionAccount.create({
id: contractPermissionAccountId(contractId, permissionAddress),
contractId: contractId,
accountId: permissionAddress,
}).save();

id: sender,
pubKey: publicKey && {
typeUrl: publicKey.typeUrl,
key: Buffer.from(publicKey.value).toString("base64"),
Copy link
Member

@ccamel ccamel Oct 25, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not correct. What you have is a protobuf Any. This Any type has a typeUrl that tells you what kind of data it contains, and a value which is the data itself in a specific format. You can't just convert the data into base64 directly, because you'll only get the binary form of that data.

Instead, you should use the typeUrl to understand what type is, knowing that, decode the binary data into its proper form. After that, you can get a string representation of it.

For instance, for the secp256k1 public key type, you can get the compressed base64 representation like this:

const pk = PubKey.deserializeBinary(new Uint8Array(publicKey.value));
const key = pk.getKey_asB64();

which gives:

"pubKey": {
            "typeUrl": "/cosmos.crypto.secp256k1.PubKey"
            "key": "A8Ny+hVbRIB8h5iiF7d4rSS+9ts06W2t1p5riyOIl1Pm",
          },

Verification:

➜ okp4d debug pubkey-raw "A8Ny+hVbRIB8h5iiF7d4rSS+9ts06W2t1p5riyOIl1Pm"
Parsed key as secp256k1
Address: 1BC29537C771DCA2C365EE81B39B44042F916DF6
...

➜ okp4d debug addr 1BC29537C771DCA2C365EE81B39B44042F916DF6
Address: [27 194 149 55 199 113 220 162 195 101 238 129 179 155 68 4 47 145 109 246]
Address (hex): 1BC29537C771DCA2C365EE81B39B44042F916DF6
Bech32 Acc: okp41r0pf2d78w8w29sm9a6qm8x6yqshezm0k6vwcrg
Bech32 Val: okp4valoper1r0pf2d78w8w29sm9a6qm8x6yqshezm0k0t73af

@ccamel ccamel marked this pull request as draft October 25, 2023 09:34
@ccamel
Copy link
Member

ccamel commented Oct 25, 2023

Switching to draft - code needs to be improved.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants