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

SSZ: hashTreeRoot never return #397

Closed
twoeths opened this issue Aug 15, 2019 · 1 comment · Fixed by #398
Closed

SSZ: hashTreeRoot never return #397

twoeths opened this issue Aug 15, 2019 · 1 comment · Fixed by #398
Assignees

Comments

@twoeths
Copy link
Contributor

twoeths commented Aug 15, 2019

Describe the bug
This unit test never return

it(`should hash correctly with slice/array data`, () => {
    const accountBalances = {
      balances: []
    };
    for (let i = 0; i < 2; i++) {
      accountBalances.balances.push(new BN("32000000000"));
    }
    const accountBalancesType: AnySSZType = {
      fields: [["balances", { elementType: "uint64", maxLength: 2 }]]
    };
    const hash = hashTreeRoot(accountBalances, accountBalancesType).toString(
      "hex"
    );
    assert(hash);
  });

This is because of the implementation inside hashTreeRoot.
Expected behavior
It should return a hash or throw an error

@twoeths
Copy link
Contributor Author

twoeths commented Aug 15, 2019

I found out that padFor in merkleize is 0.5 which is incorrect and it caused bitLength function to run forever
we expect integer there
Digging into it, I see the following snippet in _hashTreeRoot maybe the root cause

case Type.list:
      value = value as SerializableArray;
      if (isBasicType(type.elementType)) {
        return mixInLength(
          merkleize(
            pack(value, (type as ListType).elementType),
            (type.maxLength * fixedSize(type.elementType)) / BYTES_PER_CHUNK
          ),
          value.length
        );
      } else {

the 2nd parameter in merkleize maybe not correct, it can be like this

Math.floor(type.maxLength * fixedSize(type.elementType) + 31) / BYTES_PER_CHUNK
          )

according to the spec of chunk_count in https://github.com/ethereum/eth2.0-specs/blob/dev/specs/simple-serialize.md

List[B, N] and Vector[B, N], where B is a basic type: (N * size_of(B) + 31) // 32 (dividing by chunk size, rounding up)

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 a pull request may close this issue.

1 participant