bitcoin-teleport / teleport-transactions

CoinSwap implementation
Other
236 stars 70 forks source link

Creating a secp256k1 context singleton #53

Open chris-belcher opened 2 years ago

chris-belcher commented 2 years ago

Right now the project creates a new secp256k1 context a lot.

This is kind of slow. Though speed isn't a huge priority for this project since everything is slowed down by the requirement to wait for blocks.

However I really noticed a slowdown when coding fidelity bonds, which creates about 980 addresses on startup, so I made a single context and passed it as an argument: https://github.com/bitcoin-teleport/teleport-transactions/blob/898ab8c09315e1bc2122115a434090ebdf63f61b/src/fidelity_bonds.rs#L349

I was thinking about good ways to have a secp256k1 context singleton. I gave up fairly quickly because as I said speed isn't a huge priority, but it would be a nice-to-have. So if someone else wants something to do and feels like playing around with rust, try this issue.

One way I considered was to add the context to the Wallet struct, but some functions don't have access to it, such as the Swapcoins structs (and they have to serialized to file too, the secp256k1 context isnt serializable).

Another way I tried was the lazy_static crate but I couldn't get it to work because of some rust syntax issues with the generic arguments to Secp256k1.

Another way I considered was adding the context as an argument to every single function, but that's a bit rubbish.