Giter Site home page Giter Site logo

Comments (11)

snd avatar snd commented on May 20, 2024

@debris can you have a look at this when you have a free moment. thanks

from parity-bridge.

tomusdrw avatar tomusdrw commented on May 20, 2024

bytes message is always:
32 bytes - length of the content
20 bytes - address
32 bytes - uint
32 bytes - hash
mload(add(message, 0x20)) just skips the length (since we know it anyway)
The error is in:
value := mload(add(message, 0x40)) actually, cause we skip len (32b) and address (32b instead of 20b) unless we expect address to be padded to 32bytes (which seems to be missing in docs).

from parity-bridge.

snd avatar snd commented on May 20, 2024

address is currently not padded to 32bytes. see

result[0..20].copy_from_slice(&withdraw_log.recipient);

from parity-bridge.

snd avatar snd commented on May 20, 2024

@tomusdrw thanks for taking a look at this!

i didn't know that the first 32bytes of a bytes represent the length. makes total sense.

what confuses me is that this test

it("should allow correct withdraw", function() {

only works if i modify the assembly to this:
recipient := mload(add(message, 20))

if i modify the assembly to:

        assembly {
            // skip 32 bytes message length
            recipient := mload(add(message, 32))
            value := mload(add(message, 52))
            hash := mload(add(message, 84))
        }

which i'd expect to be correct

then what gets loaded into recipient is wrong and the test fails at:

assert.equal(recipient_account, result.logs[0].args.recipient, "Event recipient should match recipient in message");

from parity-bridge.

snd avatar snd commented on May 20, 2024

i think we need to pad address to 32 bytes since mload seems to always load 32 bytes

from parity-bridge.

snd avatar snd commented on May 20, 2024

#61 (comment)

what happens here is probably that bytes 20-32 are all 0 since length is 84.
thus reading recipient from bytes 20-52 (32 bytes starting at 20) reads 12 zero bytes followed by the 20 recipient bytes which get correctly read as the recipient address.
we could leave it like this since length is always 84 but it would probably be cleaner to pad recipient to 32

from parity-bridge.

tomusdrw avatar tomusdrw commented on May 20, 2024

Right. That's correct. mload loads 32 bytes and just uses last 20 for address.

 // skip 32 bytes message length
            recipient := mload(add(message, 20))
            value := mload(add(message, 52))
            hash := mload(add(message, 84))

That should work, right? We re-use some bytes from length, but it doesn't matter cause they are discarded anyway.

Otherwise we need to pad address to 32-bytes.

from parity-bridge.

snd avatar snd commented on May 20, 2024

That should work, right? We re-use some bytes from length, but it doesn't matter cause they are discarded anyway.

yes, that works!

Otherwise we need to pad address to 32-bytes.

that would be a bit clearer imho

from parity-bridge.

snd avatar snd commented on May 20, 2024

how should we resolve this?

  1. re-use some bytes from length and document it thoroughly
  2. pad address to 32-bytes

fine with both. i like 2 a bit more

from parity-bridge.

tomusdrw avatar tomusdrw commented on May 20, 2024

I prefer the first one to reduce the gas cost by a tiny bit. That said we should heavily test that function to make sure it's correct. I would also add length assertion at the top if it's not there yet, so that a padded address is not accepted.

from parity-bridge.

snd avatar snd commented on May 20, 2024

I prefer the first one to reduce the gas cost by a tiny bit. That said we should heavily test that function to make sure it's correct. I would also add length assertion at the top if it's not there yet, so that a padded address is not accepted.

ok!

already added a length assertion

from parity-bridge.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.