komputing / KEthereum

Kotlin library for Ethereum
MIT License
350 stars 73 forks source link

RLP zero should be 0x80 #52

Closed jolestar closed 5 years ago

jolestar commented 5 years ago

org/kethereum/functions/rlp/RLPTestData.kt

0.toRLP() to "00"

but https://github.com/ethereum/tests/blob/develop/RLPTests/rlptest.json

"zero": {
    "in": 0,
    "out": "0x80"
  }
ligi commented 5 years ago

Thanks for the report! But this is quite strange there - an emptystring resolves to the same as zero

"emptystring": {
    "in": "",
    "out": "0x80"
  },
  "zero": {
    "in": 0,
    "out": "0x80"
  },

will investigate

jolestar commented 5 years ago
/*
     * For a single byte whose value is in the [0x00, 0x7f] range, that byte is
     * its own RLP encoding.
     */

    /**
     * [0x80]
     * If a string is 0-55 bytes long, the RLP encoding consists of a single
     * byte with value 0x80 plus the length of the string followed by the
     * string. The range of the first byte is thus [0x80, 0xb7].
     */
    private static final int OFFSET_SHORT_ITEM = 0x80;

empty string and zero both startWith 0x80

ligi commented 5 years ago

thanks - yes for empty string we agree on 0x80 - but I am still puzzled about zero as:

/*

that would mean 0 -> 0x00

ligi commented 5 years ago

Interestingly parity and web3j also resolves it to 0x00 --->

https://github.com/paritytech/parity-common/blob/master/rlp/tests/tests.rs#L237

and

https://github.com/web3j/web3j/blob/master/rlp/src/test/java/org/web3j/rlp/RlpEncoderTest.java#L70

ligi commented 5 years ago

Thanks for pointing this out @jolestar - it is fixed in 0.68

ligi commented 5 years ago

some more context to this issue: https://github.com/ethereum/tests/pull/485