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

Why do we need to add 4 bytes of random characters when interacting with contracts? #107

Open
lucky1024 opened this issue Jan 26, 2022 · 4 comments

Comments

@lucky1024
Copy link

When I looked at the source code, I found the following snippet:

async function createTx(
  arweave: Arweave,
  wallet: JWKInterface | 'use_wallet',
  contractId: string,
  input: any,
  tags: { name: string; value: string }[],
  target: string = '',
  winstonQty: string = '0',
  reward?: string,
): Promise<Transaction> {
  const options: Partial<CreateTransactionInterface> = {
    data: Math.random().toString().slice(-4),                                // Why need this?
    reward,
  };

Does anyone know why we need the random string?

data: Math.random().toString().slice(-4),

@ppedziwiatr
Copy link
Contributor

@lucky1024
Copy link
Author

@ppedziwiatr I don't think the data field is required for generating tx.id. For example, tx https://viewblock.io/arweave/tx/iCLuc7Aj3xFOOWX5qFZCz5UVRTVn8k6quM2ouiockHA don't have data field (Size 0B).

@ppedziwiatr
Copy link
Contributor

To be more precise - I believe it is required to ensure the uniqueness of the tx ids (in case two transactions with exact same parameters would be posted) - which in case of SmartWeave is crucial...

The flow goes like that (from the links that I've pasted in previous response):

let dataToSign = await transaction.getSignatureData();
let rawSignature = await this.crypto.sign(jwk, dataToSign, options);
let id = await this.crypto.hash(rawSignature);

The dataToSign is generated like that:

if (!this.data_root) {
          await this.prepareChunks(this.data);
        }

        const tagList: [Uint8Array, Uint8Array][] = this.tags.map((tag) => [
          tag.get("name", { decode: true, string: false }),
          tag.get("value", { decode: true, string: false }),
        ]);

        return await deepHash([
          ArweaveUtils.stringToBuffer(this.format.toString()),
          this.get("owner", { decode: true, string: false }),
          this.get("target", { decode: true, string: false }),
          ArweaveUtils.stringToBuffer(this.quantity),
          ArweaveUtils.stringToBuffer(this.reward),
          this.get("last_tx", { decode: true, string: false }),
          tagList,
          ArweaveUtils.stringToBuffer(this.data_size),
          this.get("data_root", { decode: true, string: false }),
        ]);

So - just in (probably very rare - but still possible) case, that all of the parameters of two transactions are the same - the randomly generated data increases the chance of generating unique txId.

That's my understanding, but probably someone from core Arweave team will be able to explain this better.

@lucky1024
Copy link
Author

@ppedziwiatr Thank you for your further detailed explanation.
May be the last_tx field in tx can be used to generate uniqueness txid. https://docs.arweave.org/developers/server/http-api#field-definitions

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

No branches or pull requests

2 participants