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

remove await before _doCalculateWitness() #299

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

yushihang
Copy link

@yushihang yushihang commented Sep 25, 2024

Ensure that the generate witness operation within the same function does not retrieve incorrect data when running concurrent code with promises, because the WASM side uses the same instance and memory.

This is a minimal change that ensures the functions calculateWTNSBin(), calculateBinWitness(), and calculateWitness() maintain their original async interfaces.

close #297

@yushihang yushihang changed the title remove await before _doCalculateWitness() remove await before _doCalculateWitness() Sep 25, 2024
@yushihang
Copy link
Author

yushihang commented Sep 25, 2024

Examples

image image

@yushihang
Copy link
Author

yushihang commented Sep 26, 2024

Another Example similiar to calculateWTNSBin()

class WasmSingleton {
  constructor() {
    if (WasmSingleton.instance) {
      return WasmSingleton.instance;
    }
    this.data = "";

    WasmSingleton.instance = this;
  }

  getData() {
    return this.data;
  }

  computeData() {
    this.data = this.data.toLowerCase();
  }

  async setInputData(data) {
    this.data = data;
  }


}
const wasmInstance = new WasmSingleton();

async function test(index) {
  /*await*/ wasmInstance.setInputData("INPUT" + index);
  wasmInstance.computeData();
  const outputData = wasmInstance.getData();
  console.log("outputData for index" + index + ": " + outputData);
}

Promise.all([test(1), test(2), test(3)]);

The output is as expected

outputData for index1: input1
outputData for index2: input2
outputData for index3: input3

After uncommented "await" for /*await*/ wasmInstance.setInputData("INPUT" + index);

class WasmSingleton {
  constructor() {
    if (WasmSingleton.instance) {
      return WasmSingleton.instance;
    }
    this.data = "";

    WasmSingleton.instance = this;
  }

  getData() {
    return this.data;
  }

  computeData() {
    this.data = this.data.toLowerCase();
  }

  async setInputData(data) {
    this.data = data;
  }

}
const wasmInstance = new WasmSingleton();

async function test(index) {
  await wasmInstance.setInputData("INPUT" + index); //`await` is not commented now
  wasmInstance.computeData();
  const outputData = wasmInstance.getData();
  console.log("outputData for index" + index + ": " + outputData);
}

Promise.all([test(1), test(2), test(3)]);

Output is not as expected

outputData for index1: input3
outputData for index2: input3
outputData for index3: input3

@yushihang yushihang reopened this Oct 7, 2024
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.

Can calculateWTNSBin() / _doCalculateWitness() ensure promise safety?
1 participant