zig-bitcoin / btczee

Bitcoin protocol implementation in Zig.
https://zig-bitcoin.github.io/btczee/
MIT License
45 stars 30 forks source link

Optimize BTC script's stack implementation #23

Closed tdelabro closed 1 month ago

tdelabro commented 1 month ago

Right now the BTC script engine's stack is implemented as an array of pointers to the actual values.

Eg: index stack value (pointer) heap value pointed to actual size in memory in u8
100 0xbabe bool 1
99 0xcafe i64 4
98 0x4242 [2]u8 2

This implementation scatters the actual values across the heap, therefore slowing script execution by negating the data-proximity property expected from a heap. The purpose of this issue would be to implement the stack as an actual stack.

index actual value memory content
100 false 0x0
99 i64.MAX 0xff
98 0xff
97 0xff
96 0xff
95 0xff
94 0xff
93 0xff
92 0xff
91 [2]u8{0, 1} 0x0
91 0x1

Now the issue is that you don't know in advance what the size of the next item should be. I'm not sure it is an issue, as the script is responsible for telling you how to treat the next item(s) in the stack. If it becomes an issue there are most likely solutions to be imagined.

supreme2580 commented 1 month ago

Hi @tdelabro can i be assigned to this?

onlydustapp[bot] commented 1 month ago

Hey @supreme2580! Thanks for showing interest. We've created an application for you to contribute to btczee. Go check it out on OnlyDust!

tdelabro commented 1 month ago

@supreme2580 Tbc we have no certitude this is the correct way to implement it. Btcd did it the way it is actually done in btczee. But this implementation in Rust https://github.com/rust-bitcoin/rust-bitcoin/blob/d9b5844981ba5c46eeda3ca68093281e125d425e/bitcoin/src/blockdata/script/owned.rs, seems to do it the way I'm describing it.

Please go ahead and try it out.

supreme2580 commented 1 month ago

Thanks @tdelabro will check the rust implementation out

tdelabro commented 1 month ago

@supreme2580 to be honest at the state we are in the project, it may not be very wise to try those optimisation. There would be much more value right now in having a proper test covering for the script implementation.

Is this something you would consider doing in the first place?

tdelabro commented 1 month ago

Truth being there is no way to feed the node scripts rn...

supreme2580 commented 1 month ago

@tdelabro not really, myself and my partner were going over creating more opcodes first... but if this is of high priority we can add this to the issues we're looking at

supreme2580 commented 1 month ago

@tdelabro what'd you think?

tdelabro commented 1 month ago

@supreme2580 sorry I missed you message. Do you have a telegram? So I can add you to our groupchat?

tdelabro commented 1 month ago

@supreme2580 I think it's better to work on new opcodes than on this issue for now. But truly do whatever you like better :)

supreme2580 commented 1 month ago

Yes I do I'm supreme1960 on telegram

supreme2580 commented 1 month ago

Yes we should even have a PR up for more opcodes in a few hours