ethereum / ethereumj

DEPRECATED! Java implementation of the Ethereum yellowpaper. For JSON-RPC and other client features check Ethereum Harmony
GNU Lesser General Public License v3.0
2.18k stars 1.1k forks source link

NodeKeyCompositor for storage [ BUG ] #1157

Closed ithinker1991 closed 6 years ago

ithinker1991 commented 6 years ago

ethereumj-core/src/main/java/org/ethereum/datasource/NodeKeyCompositor.java This method

 private static byte[] composeInner(byte[] key, byte[] addrHash) {

        validateKey(key);

        byte[] derivative = new byte[key.length];
        arraycopy(key, 0, derivative, 0, PREFIX_BYTES);
        arraycopy(addrHash, 0, derivative, PREFIX_BYTES, PREFIX_BYTES);

        return derivative;
}
 public static void main(String[] args) {
        byte[] addrhash = Hex.decode("a9539c810cc2e8fa20785bdd78ec36ccb25e1b5be78dbadf6c4e817c6d170bbb");
        byte[] key = Hex.decode("bbbbbb5be78dbadf6c4e817c6d170bbb47e9916f8f6cc4607c5f3819ce98497b");

        byte[] ret = NodeKeyCompositor.compose(key, addrhash);
        System.out.println(Hex.toHexString(ret));

       // output : bbbbbb5be78dbadf6c4e817c6d170bbba9539c810cc2e8fa20785bdd78ec36cc
    }

the output is not match the example

mkalinin commented 6 years ago

Yep, that was incorrect. Fixed by https://github.com/ethereum/ethereumj/commit/ec3ce8fccb75987f101096c61af137c4f5b17adb. Thank you for the report!

ithinker1991 commented 6 years ago

emmm...

32-bytes composed key should be [first 16-bytes of address hash:second 16-bytes of node key]

There seems a another BIG problem. I noticed in solidity, for dynamic type such as stringbytes first 16-bytes of node-key are same and second 16-bytes of node are different. So if one field has been encoded to many storage-key. the composed key will be same.

For example s1 = "1111111111111111111111111111111111111112222222222222222222222222222222222222222333333333333333333333333333333333333";

encoded result:

key of storage node                             value of storage node                                                        
0000000000000000000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000000000000000e7
290decd9548b62a8d60345a988386fc84ba6bc95484008f6362f93160ef3e563 3131313131313131313131313131313131313131313131313131313131313131
290decd9548b62a8d60345a988386fc84ba6bc95484008f6362f93160ef3e564 3131313131313132323232323232323232323232323232323232323232323232
290decd9548b62a8d60345a988386fc84ba6bc95484008f6362f93160ef3e565 3232323232323232323232323232323333333333333333333333333333333333
290decd9548b62a8d60345a988386fc84ba6bc95484008f6362f93160ef3e566 3333333333333333333333333333333333333300000000000000000000000000