Page MenuHomePhabricator

[ecash-lib] attach ecc and hashes to globalThis
Needs ReviewPublic

Authored by bytesofman on Sat, Jan 4, 00:04.

Details

Reviewers
tobias_ruck
Group Reviewers
Restricted Project
Summary

ecash-lib methods that require initWasm() will not work if ecash-lib is a dep of a lib. For example, if we are trying to use ecash-agora in an app, and we need to use ecash-agora methods that require initWasm, like acceptTx -- this will fail even if we initWasm() before we call it.

It will pass though if we initWasm in ecash-agora before ecash-agora calls a function like shaRmd160 (which needs initWasm to work.

In general it is best practice not to modify globalThis. However in getting webassembly to work as a deb of a dep, I do not have a better idea (also not sure this will work tbh).

Test Plan

CI to confirm this does not break existing monorepo apps.

How I tested this

  • I published this as an rc
  • I installed it as a dep of a lib (a draft wallet class I am working on)
  • Tests that used this lib were failing before installing this rc, they passed after
  • I installed this rc as a dep of ecash-agora and published that as an rc
  • Tests that were failing because initWasm() was not making methods available passed after replacing currently-published ecash-agora with that version

So, ecash-agora@0.2.1-rc4 and ecash-lib@1.2.2-rc4 solve the issue

Event Timeline

modify for non-local deps as will need to iterate through rcs to test published version

ok in testing ... seems to be working for hashes but needs some work on ecc

console.log(globalThis.ecashlib)

{
  HASHES: {
    sha256: [Function (anonymous)],
    sha256d: [Function (anonymous)],
    shaRmd160: [Function (anonymous)]
  }
}

and still get the TypeError: globalThis.ecashlib.Ecc is not a constructor which makes sense since clearly it has not been properly added to globalThis

do not overwrite the object, more ts shims

return an alt dummy "Unimplemented" ecc if unimplemented, allow calling with new Ecc()

do not import the merged type as it causes issues with rel dep apps

clean up comments, remove rc version

Tail of the build log:

Build 'Bitcoin ABC Diffs / Diff Testing' #N/A, branch 'refs/tags/phabricator/diff/51956'
Triggered 2025-01-04 05:01:30 by 'Phabricator Staging (phabricator-staging)'
Started 2025-01-04 05:01:37 on agent 'N/A'
Finished 2025-01-04 05:01:37 with status FAILURE 'Snapshot dependency failed to start: Automated Deployments / Bitcoin ABC Infra / Bitcoin-ABC Infra Checkout'
VCS revisions: 'BitcoinABC_BitcoinAbcStaging' (Git, instance id 22): '6ed06bd778e983cd8ca5b4e9f678ce9ee34cc869' (branch: 'refs/tags/phabricator/diff/51956', checkout rules: '+:. => ./bitcoin-abc')
TeamCity URL https://build.bitcoinabc.org/buildConfiguration/BitcoinABC_BitcoinAbcStaging/881129 
TeamCity server version is 2024.12 (build 174331), server timezone: GMT (UTC)

[05:01:30]W: bt15 (7s)
[05:01:30]i: TeamCity server version is 2024.12 (build 174331)
[05:01:30] : Finalize build settings
[05:01:30] : Collecting changes in 2 VCS roots
[05:01:30] :	 [Collecting changes in 2 VCS roots] VCS Root details
[05:01:30] :		 [VCS Root details] "Bitcoin ABC Staging" {instance id=22, parent internal id=3, parent id=BitcoinABC_BitcoinAbcStaging, description: "ssh://vcs@reviews.bitcoinabc.org:2221/source/bitcoin-abc-staging.git#refs/heads/master"}
[05:01:30] :		 [VCS Root details] "abc-infrastructure" {instance id=24, parent internal id=7, parent id=AutomatedDeployments_BitcoinAbcDeveloperTools_AbcInfrastructure, description: "ssh://vcs@reviews.bitcoinabc.org:2221/source/infrastructure.git#refs/heads/master"}
[05:01:30]i: Loading current repository state for VCS root 'Bitcoin ABC Staging' (6s)
[05:01:30]i:	 [Loading current repository state for VCS root 'Bitcoin ABC Staging'] Loading current repository state for VCS root 'abc-infrastructure' (6s)
[05:01:30]i:		 [Loading current repository state for VCS root 'abc-infrastructure'] VCS root 'Bitcoin ABC Staging': git -c credential.helper= -c credential.helper=/opt/teamcity/temp/credHelper11406208710090663430.sh ls-remote origin
[05:01:30]i:		 [Loading current repository state for VCS root 'abc-infrastructure'] VCS root 'abc-infrastructure': git -c credential.helper= -c credential.helper=/opt/teamcity/temp/credHelper1864931400857660944.sh ls-remote origin
[05:01:30]i:		 [Loading current repository state for VCS root 'abc-infrastructure'] VCS root 'Bitcoin ABC Staging': Warning: Permanently added '[reviews.bitcoinabc.org]:2221' (ED25519) to the list of known hosts.
[05:01:30]i:		 [Loading current repository state for VCS root 'abc-infrastructure'] VCS root 'abc-infrastructure': kex_exchange_identification: read: Connection reset by peer
[05:01:30]i:		 [Loading current repository state for VCS root 'abc-infrastructure'] VCS root 'abc-infrastructure': Connection reset by 51.161.87.173 port 2221
[05:01:30]i:		 [Loading current repository state for VCS root 'abc-infrastructure'] VCS root 'abc-infrastructure': fatal: Could not read from remote repository.
[05:01:30]i:		 [Loading current repository state for VCS root 'abc-infrastructure'] VCS root 'abc-infrastructure': 
[05:01:30]i:		 [Loading current repository state for VCS root 'abc-infrastructure'] VCS root 'abc-infrastructure': Please make sure you have the correct access rights
[05:01:30]i:		 [Loading current repository state for VCS root 'abc-infrastructure'] VCS root 'abc-infrastructure': and the repository exists.
[05:01:31]i: Waiting for completion of current operations for the VCS root 'Bitcoin ABC Staging'
[05:01:36]i: Detecting changes in VCS root 'Bitcoin ABC Staging' (used in 'Diff Testing', 'Staging Checkout Dummy')
[05:01:36]i: Will collect changes for 'Bitcoin ABC Staging' starting from revision 26bacf0d4516e7c64a648de2ac8fa1b487416670
[05:01:36] : Compute revision for 'Bitcoin ABC Staging'
[05:01:36] :	 [Compute revision for 'Bitcoin ABC Staging'] Upper limit revision: 6ed06bd778e983cd8ca5b4e9f678ce9ee34cc869
[05:01:36]i:	 [Compute revision for 'Bitcoin ABC Staging'] MaxModId = 77342
[05:01:36] :	 [Compute revision for 'Bitcoin ABC Staging'] Computed revision: 6ed06bd778e983cd8ca5b4e9f678ce9ee34cc869
[05:01:37]W: Build was removed from the queue with comment: This build has not been started because some of the builds it depends on failed to start

back out npm deps used in testing

back out --forbid-only removal

back out package-lock.json changes not related to version bump

clean up comments in global.d.ts to explain what we are actually doing with this file

bytesofman published this revision for review.Sat, Jan 4, 05:31
bytesofman added inline comments.
modules/ecash-lib/src/ecc.ts
18 ↗(On Diff #51960)

I was not able to test this. I tried to do it in this repo by testing ecc before calling initWasm, but it still worked as expected.

When I try to test the repo as a dep in other repos, I still get ecc is not a constructor

Mb we should remove it. However, the reason I chose this approach is because typescript would not build if Ecc can possibly not have a constructor method. This class solved that ts issue. It might not be doing anything else. But that does not really change current behavor of Ecc not working before initWasm.

update

this (may) only work sometimes. Running into tests that passed last night fail this morning ... can't get them to work now... cache issue? mistake in setup and versions installed last night? not sure. tough to test as the problem only occurs in npm-installed versions

could still use some review here since ... mb I am missing something, mb this actually breaks stuff

tobias_ruck added a subscriber: tobias_ruck.
tobias_ruck added inline comments.
modules/ecash-lib/src/ecc.ts
17 ↗(On Diff #51961)

The better abstraction is probably an Ecc object that proxies method calls to WASM, and throws an error if not initialized yet when the method is called.
That might also fix the issue

This revision now requires changes to proceed.Sat, Jan 4, 14:14

note that rc4 does still "work" with

const myNewEcc = new (globalThis as unknown as GlobalThis).ecashlib.Ecc();

but new Ecc() gives the (existing behavior) "not a constructor" error

use a Proxy to get Ecc to work with existing syntax across dependency levels

This one seems to be working

available on npm as ecash-lib@1.2.2-rc16

I have not used a Proxy before and I am not totally sure this is the right implementation. However it is passing in my local tests, and console.log shows it is behaving as expected --

(after initWasm() in "parent" app)

console.log(globalThis.ecashlib)
{
  Ecc: [class Ecc],
  HASHES: {
    sha256: [Function (anonymous)],
    sha256d: [Function (anonymous)],
    shaRmd160: [Function (anonymous)]
  }
}

console.log(globalThis.ecashlib.Ecc)
[class Ecc]

console.log(Ecc)
Ecc [class EccProxy]

.... mb I still have some other cache issue going on in my tests, but at least this seems to be the right approach.

never mind. once I

rm -rf node_modules
rm package-lock.json
npm i

this doesn't work at all. actually get an infinite recursion issue instead, nice.

simplify, use function instead of Proxy

Tail of the build log:

Build 'Bitcoin ABC Diffs / Diff Testing' #N/A, branch 'refs/tags/phabricator/diff/51963'
Triggered 2025-01-05 05:36:38 by 'Phabricator Staging (phabricator-staging)'
Started 2025-01-05 05:36:39 on agent 'N/A'
Finished 2025-01-05 05:36:39 with status FAILURE 'Snapshot dependency failed to start: Automated Deployments / Bitcoin ABC Infra / Bitcoin-ABC Infra Checkout'
VCS revisions: 'BitcoinABC_BitcoinAbcStaging' (Git, instance id 22): 'N/A' (checkout rules: '+:. => ./bitcoin-abc')
TeamCity URL https://build.bitcoinabc.org/buildConfiguration/BitcoinABC_BitcoinAbcStaging/881219 
TeamCity server version is 2024.12 (build 174331), server timezone: GMT (UTC)

[05:36:38]W: bt15
[05:36:38]i: TeamCity server version is 2024.12 (build 174331)
[05:36:38] : Finalize build settings
[05:36:38] : Collecting changes in 2 VCS roots
[05:36:38] :	 [Collecting changes in 2 VCS roots] VCS Root details
[05:36:38] :		 [VCS Root details] "Bitcoin ABC Staging" {instance id=22, parent internal id=3, parent id=BitcoinABC_BitcoinAbcStaging, description: "ssh://vcs@reviews.bitcoinabc.org:2221/source/bitcoin-abc-staging.git#refs/heads/master"}
[05:36:38] :		 [VCS Root details] "abc-infrastructure" {instance id=24, parent internal id=7, parent id=AutomatedDeployments_BitcoinAbcDeveloperTools_AbcInfrastructure, description: "ssh://vcs@reviews.bitcoinabc.org:2221/source/infrastructure.git#refs/heads/master"}
[05:36:38]i: Loading current repository state for VCS root 'Bitcoin ABC Staging' (started)
[05:36:38]i:	 [Loading current repository state for VCS root 'Bitcoin ABC Staging'] Loading current repository state for VCS root 'abc-infrastructure' (started)
[05:36:38]i:		 [Loading current repository state for VCS root 'abc-infrastructure'] VCS root 'abc-infrastructure': git -c credential.helper= -c credential.helper=/opt/teamcity/temp/credHelper10815284023252993630.sh ls-remote origin
[05:36:38]i:		 [Loading current repository state for VCS root 'abc-infrastructure'] VCS root 'Bitcoin ABC Staging': git -c credential.helper= -c credential.helper=/opt/teamcity/temp/credHelper10076322198633256851.sh ls-remote origin
[05:36:38]i:		 [Loading current repository state for VCS root 'abc-infrastructure'] VCS root 'Bitcoin ABC Staging': kex_exchange_identification: read: Connection reset by peer
[05:36:38]i:		 [Loading current repository state for VCS root 'abc-infrastructure'] VCS root 'Bitcoin ABC Staging': Connection reset by 51.161.87.173 port 2221
[05:36:38]i:		 [Loading current repository state for VCS root 'abc-infrastructure'] VCS root 'Bitcoin ABC Staging': fatal: Could not read from remote repository.
[05:36:38]i:		 [Loading current repository state for VCS root 'abc-infrastructure'] VCS root 'Bitcoin ABC Staging': 
[05:36:38]i:		 [Loading current repository state for VCS root 'abc-infrastructure'] VCS root 'Bitcoin ABC Staging': Please make sure you have the correct access rights
[05:36:38]i:		 [Loading current repository state for VCS root 'abc-infrastructure'] VCS root 'Bitcoin ABC Staging': and the repository exists.
[05:36:38]i:		 [Loading current repository state for VCS root 'abc-infrastructure'] VCS root 'abc-infrastructure': kex_exchange_identification: Connection closed by remote host
[05:36:38]i:		 [Loading current repository state for VCS root 'abc-infrastructure'] VCS root 'abc-infrastructure': Connection closed by 51.161.87.173 port 2221
[05:36:38]i:		 [Loading current repository state for VCS root 'abc-infrastructure'] VCS root 'abc-infrastructure': fatal: Could not read from remote repository.
[05:36:38]i:		 [Loading current repository state for VCS root 'abc-infrastructure'] VCS root 'abc-infrastructure': 
[05:36:38]i:		 [Loading current repository state for VCS root 'abc-infrastructure'] VCS root 'abc-infrastructure': Please make sure you have the correct access rights
[05:36:38]i:		 [Loading current repository state for VCS root 'abc-infrastructure'] VCS root 'abc-infrastructure': and the repository exists.
[05:36:39]W: Build was removed from the queue with comment: This build has not been started because some of the builds it depends on failed to start

mb it was actually working at rc-16 but this one is simpler anyway. got better at ensuring the proper version was being tested iterating through various other attempts.

was running into mystery cache issues in testing, with testing of new versions sometimes not really testing the "new" version

what worked: instead of pinning versions or npm i the new version, type it exactly, e.g. "ecash-lib": "1.2.2-rc19", not "ecash-lib": "^1.2.2-rc19",

this one worked after lots of rm -rf node_modules && rm package-lock.json && npm i ... also worked with ecash-agora installing this version of ecash-lib (its use of shaRmd160 works if the parent app has called initWasm)

could still be better ways to do this.

Tail of the build log:

Build 'Bitcoin ABC Diffs / Diff Testing' #N/A, branch 'refs/tags/phabricator/diff/51963'
Triggered 2025-01-05 05:40:54 by 'Phabricator Staging (phabricator-staging)'
Started 2025-01-05 05:40:56 on agent 'N/A'
Finished 2025-01-05 05:40:56 with status FAILURE 'Snapshot dependency failed to start: Automated Deployments / Bitcoin ABC Infra / Bitcoin-ABC Infra Checkout'
VCS revisions: 'BitcoinABC_BitcoinAbcStaging' (Git, instance id 22): '6f92e8dd66ec758ec984b97694daef4ae88c8f4e' (branch: 'refs/tags/phabricator/diff/51963', checkout rules: '+:. => ./bitcoin-abc')
TeamCity URL https://build.bitcoinabc.org/buildConfiguration/BitcoinABC_BitcoinAbcStaging/881229 
TeamCity server version is 2024.12 (build 174331), server timezone: GMT (UTC)

[05:40:54]W: bt15 (1s)
[05:40:54]i: TeamCity server version is 2024.12 (build 174331)
[05:40:54] : Finalize build settings
[05:40:54] : Collecting changes in 2 VCS roots
[05:40:54] :	 [Collecting changes in 2 VCS roots] VCS Root details
[05:40:54] :		 [VCS Root details] "Bitcoin ABC Staging" {instance id=22, parent internal id=3, parent id=BitcoinABC_BitcoinAbcStaging, description: "ssh://vcs@reviews.bitcoinabc.org:2221/source/bitcoin-abc-staging.git#refs/heads/master"}
[05:40:54] :		 [VCS Root details] "abc-infrastructure" {instance id=24, parent internal id=7, parent id=AutomatedDeployments_BitcoinAbcDeveloperTools_AbcInfrastructure, description: "ssh://vcs@reviews.bitcoinabc.org:2221/source/infrastructure.git#refs/heads/master"}
[05:40:54]i: Loading current repository state for VCS root 'Bitcoin ABC Staging' (1s)
[05:40:54]i:	 [Loading current repository state for VCS root 'Bitcoin ABC Staging'] VCS root 'Bitcoin ABC Staging': git -c credential.helper= -c credential.helper=/opt/teamcity/temp/credHelper4477954410392594920.sh ls-remote origin
[05:40:54]i:	 [Loading current repository state for VCS root 'Bitcoin ABC Staging'] Loading current repository state for VCS root 'abc-infrastructure' (1s)
[05:40:54]i:		 [Loading current repository state for VCS root 'abc-infrastructure'] VCS root 'abc-infrastructure': git -c credential.helper= -c credential.helper=/opt/teamcity/temp/credHelper3081930708959957076.sh ls-remote origin
[05:40:54]i:		 [Loading current repository state for VCS root 'abc-infrastructure'] VCS root 'abc-infrastructure': kex_exchange_identification: Connection closed by remote host
[05:40:54]i:		 [Loading current repository state for VCS root 'abc-infrastructure'] VCS root 'abc-infrastructure': Connection closed by 51.161.87.173 port 2221
[05:40:54]i:		 [Loading current repository state for VCS root 'abc-infrastructure'] VCS root 'abc-infrastructure': fatal: Could not read from remote repository.
[05:40:54]i:		 [Loading current repository state for VCS root 'abc-infrastructure'] VCS root 'abc-infrastructure': 
[05:40:54]i:		 [Loading current repository state for VCS root 'abc-infrastructure'] VCS root 'abc-infrastructure': Please make sure you have the correct access rights
[05:40:54]i:		 [Loading current repository state for VCS root 'abc-infrastructure'] VCS root 'abc-infrastructure': and the repository exists.
[05:40:54]i:		 [Loading current repository state for VCS root 'abc-infrastructure'] VCS root 'Bitcoin ABC Staging': Warning: Permanently added '[reviews.bitcoinabc.org]:2221' (ED25519) to the list of known hosts.
[05:40:55]i: Detecting changes in VCS root 'Bitcoin ABC Staging' (used in 'Diff Testing', 'Staging Checkout Dummy')
[05:40:55]i: Will collect changes for 'Bitcoin ABC Staging' starting from revision 26bacf0d4516e7c64a648de2ac8fa1b487416670
[05:40:55] : Compute revision for 'Bitcoin ABC Staging'
[05:40:55] :	 [Compute revision for 'Bitcoin ABC Staging'] Upper limit revision: 6f92e8dd66ec758ec984b97694daef4ae88c8f4e
[05:40:55]i:	 [Compute revision for 'Bitcoin ABC Staging'] MaxModId = 77349
[05:40:55] :	 [Compute revision for 'Bitcoin ABC Staging'] Computed revision: 6f92e8dd66ec758ec984b97694daef4ae88c8f4e
[05:40:56]W: Build was removed from the queue with comment: This build has not been started because some of the builds it depends on failed to start