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

Add a test with a 3Mb ordinals transaction #83

Merged
merged 4 commits into from
Feb 27, 2023

Conversation

sstone
Copy link
Member

@sstone sstone commented Feb 21, 2023

This tx highlighted a bug in how we encoded script size.

@sstone sstone requested a review from remyers February 21, 2023 18:20
input < 0xFDuL -> writeUInt8(input.toUByte(), out)
input < 65535uL -> {
input < 253uL -> writeUInt8(input.toUByte(), out)
input <= 65535uL -> {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Damn, we should really have exhaustive tests on the varint encoding, like we have in eclair (where we unit test every boundary). This is already implemented in lightning-kmp with the following function, which I find slightly more clear, what do you think?

private fun writeBigSize(input: ULong, out: Output) {
        when {
            input < 0xfdu -> writeByte(input.toInt(), out)
            input < 0x10000u -> {
                out.write(0xfd)
                writeU16(input.toInt(), out)
            }
            input < 0x100000000u -> {
                out.write(0xfe)
                writeU32(input.toInt(), out)
            }
            else -> {
                out.write(0xff)
                writeU64(input.toLong(), out)
            }
        }
    }

But I think there's a difference between lightning and bitcoin here, we don't use the same endianness, do we?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes bitcoin is little endian and LN is big endian. With the proposed changes we match what bitcoin core does which is always a good thing :)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I find it a bit confusing that the implementation uses < for the first case but <= for the others to be honest...but if that matches the bitcoind implementation why not

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For reference, Bitcoin Core confusingly calls these "compact size unsigned integers" and also sometimes varints, but CVarint is something different. I think the lightning-kmp implementation is clearer, though core names the function WriteCompactSize, so both could lead to naming confusion when comparing to core.

This tx highlighted a bug in how we encoded script size.
@sstone sstone force-pushed the snapshot/parse-ordinals-tx branch from cf43565 to a86fccc Compare February 22, 2023 11:39
…n.kt

Co-authored-by: Bastien Teinturier <31281497+t-bast@users.noreply.github.com>
@sstone sstone force-pushed the snapshot/parse-ordinals-tx branch from 55d5fa9 to 6576181 Compare February 22, 2023 13:19
Copy link
Member

@t-bast t-bast left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

Copy link
Contributor

@remyers remyers left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I compared to the core code and tested with and without the fixes to double check the tests catch any issues. I only had a question and found an issue with the test.

input < 0xFDuL -> writeUInt8(input.toUByte(), out)
input < 65535uL -> {
input < 253uL -> writeUInt8(input.toUByte(), out)
input <= 65535uL -> {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For reference, Bitcoin Core confusingly calls these "compact size unsigned integers" and also sometimes varints, but CVarint is something different. I think the lightning-kmp implementation is clearer, though core names the function WriteCompactSize, so both could lead to naming confusion when comparing to core.

@sstone sstone requested a review from remyers February 23, 2023 18:08
Copy link
Contributor

@remyers remyers left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good!

small nit:

val v = (1uL shl i) uses lower case uL, a few lines down it's an uppercase UL and a few lines above it's uL. Both are valid, Kotlin docs say "uL and UL explicitly tag literal as unsigned long"

@sstone
Copy link
Member Author

sstone commented Feb 24, 2023

small nit:

val v = (1uL shl i) uses lower case uL, a few lines down it's an uppercase UL and a few lines above it's uL. Both are valid, Kotlin docs say "uL and UL explicitly tag literal as unsigned long"

Ah now I have to fix :)

@sstone sstone merged commit 0c1b44b into master Feb 27, 2023
@sstone sstone deleted the snapshot/parse-ordinals-tx branch February 27, 2023 12:49
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.

3 participants