remove quadratic deposit Merkle tree initialization
This commit is contained in:
parent
20554ab8ac
commit
3d121d9734
|
@ -237,7 +237,7 @@ OK: 5/5 Fail: 0/5 Skip: 0/5
|
||||||
OK: 8/8 Fail: 0/8 Skip: 0/8
|
OK: 8/8 Fail: 0/8 Skip: 0/8
|
||||||
## chain DAG finalization tests [Preset: mainnet]
|
## chain DAG finalization tests [Preset: mainnet]
|
||||||
```diff
|
```diff
|
||||||
+ prune heads on finalization [Preset: mainnet] OK
|
+ init with gaps [Preset: mainnet] OK
|
||||||
```
|
```
|
||||||
OK: 1/1 Fail: 0/1 Skip: 0/1
|
OK: 1/1 Fail: 0/1 Skip: 0/1
|
||||||
## hash
|
## hash
|
||||||
|
|
|
@ -72,7 +72,6 @@ task test, "Run all tests":
|
||||||
# Consensus object SSZ tests
|
# Consensus object SSZ tests
|
||||||
buildAndRunBinary "test_fixture_ssz_consensus_objects", "tests/official/", "-d:chronicles_log_level=TRACE -d:const_preset=mainnet"
|
buildAndRunBinary "test_fixture_ssz_consensus_objects", "tests/official/", "-d:chronicles_log_level=TRACE -d:const_preset=mainnet"
|
||||||
|
|
||||||
# 0.12.1
|
|
||||||
buildAndRunBinary "all_fixtures_require_ssz", "tests/official/", "-d:chronicles_log_level=TRACE -d:const_preset=mainnet"
|
buildAndRunBinary "all_fixtures_require_ssz", "tests/official/", "-d:chronicles_log_level=TRACE -d:const_preset=mainnet"
|
||||||
|
|
||||||
# State and block sims; getting to 4th epoch triggers consensus checks
|
# State and block sims; getting to 4th epoch triggers consensus checks
|
||||||
|
|
|
@ -18,21 +18,15 @@ import
|
||||||
../../beacon_chain/spec/[beaconstate, datatypes, digest, helpers],
|
../../beacon_chain/spec/[beaconstate, datatypes, digest, helpers],
|
||||||
../../beacon_chain/ssz/merkleization
|
../../beacon_chain/ssz/merkleization
|
||||||
|
|
||||||
# TODO
|
# TODO All tests need to be moved to the test suite.
|
||||||
#
|
|
||||||
# This module currently represents a direct translation of the Python
|
|
||||||
# code, appearing in the spec. We need to review it to ensure that it
|
|
||||||
# doesn't duplicate any code defined in ssz.nim already.
|
|
||||||
#
|
|
||||||
# All tests need to be moved to the test suite.
|
|
||||||
|
|
||||||
func round_step_down*(x: Natural, step: static Natural): int {.inline.} =
|
func round_step_down(x: Natural, step: static Natural): int {.inline.} =
|
||||||
## Round the input to the previous multiple of "step"
|
## Round the input to the previous multiple of "step"
|
||||||
when (step and (step - 1)) == 0:
|
when (step and (step - 1)) == 0:
|
||||||
# Step is a power of 2. (If compiler cannot prove that x>0 it does not make the optim)
|
# Step is a power of 2. (If compiler cannot prove that x>0 it does not make the optim)
|
||||||
result = x and not(step - 1)
|
x and not(step - 1)
|
||||||
else:
|
else:
|
||||||
result = x - x mod step
|
x - x mod step
|
||||||
|
|
||||||
type SparseMerkleTree*[Depth: static int] = object
|
type SparseMerkleTree*[Depth: static int] = object
|
||||||
## Sparse Merkle tree
|
## Sparse Merkle tree
|
||||||
|
@ -41,7 +35,7 @@ type SparseMerkleTree*[Depth: static int] = object
|
||||||
# and the root hash at the last depth
|
# and the root hash at the last depth
|
||||||
nnznodes: array[Depth+1, seq[Eth2Digest]] # nodes that leads to non-zero leaves
|
nnznodes: array[Depth+1, seq[Eth2Digest]] # nodes that leads to non-zero leaves
|
||||||
|
|
||||||
proc merkleTreeFromLeaves*(
|
func merkleTreeFromLeaves(
|
||||||
values: openarray[Eth2Digest],
|
values: openarray[Eth2Digest],
|
||||||
Depth: static[int] = DEPOSIT_CONTRACT_TREE_DEPTH
|
Depth: static[int] = DEPOSIT_CONTRACT_TREE_DEPTH
|
||||||
): SparseMerkleTree[Depth] =
|
): SparseMerkleTree[Depth] =
|
||||||
|
@ -68,21 +62,28 @@ proc merkleTreeFromLeaves*(
|
||||||
h.update zeroHashes[depth-1]
|
h.update zeroHashes[depth-1]
|
||||||
result.nnznodes[depth].add nodeHash
|
result.nnznodes[depth].add nodeHash
|
||||||
|
|
||||||
proc getMerkleProof*[Depth: static int](tree: SparseMerkleTree[Depth],
|
func getMerkleProof[Depth: static int](tree: SparseMerkleTree[Depth],
|
||||||
index: int): array[Depth, Eth2Digest] =
|
index: int,
|
||||||
|
depositMode = false): array[Depth, Eth2Digest] =
|
||||||
# Descend down the tree according to the bit representation
|
# Descend down the tree according to the bit representation
|
||||||
# of the index:
|
# of the index:
|
||||||
# - 0 --> go left
|
# - 0 --> go left
|
||||||
# - 1 --> go right
|
# - 1 --> go right
|
||||||
let path = uint32(index)
|
let path = uint32(index)
|
||||||
|
var depthLen = index + 1
|
||||||
for depth in 0 ..< Depth:
|
for depth in 0 ..< Depth:
|
||||||
let nodeIdx = int((path shr depth) xor 1)
|
let nodeIdx = int((path shr depth) xor 1)
|
||||||
if nodeIdx < tree.nnznodes[depth].len:
|
|
||||||
|
# depositMode simulates only having constructed SparseMerkleTree[Depth]
|
||||||
|
# through exactly deposit specified.
|
||||||
|
if nodeIdx < tree.nnznodes[depth].len and
|
||||||
|
(nodeIdx < depthLen or not depositMode):
|
||||||
result[depth] = tree.nnznodes[depth][nodeIdx]
|
result[depth] = tree.nnznodes[depth][nodeIdx]
|
||||||
else:
|
else:
|
||||||
result[depth] = zeroHashes[depth]
|
result[depth] = zeroHashes[depth]
|
||||||
|
|
||||||
|
depthLen = (depthLen + 1) div 2
|
||||||
|
|
||||||
func attachMerkleProofs*(deposits: var openarray[Deposit]) =
|
func attachMerkleProofs*(deposits: var openarray[Deposit]) =
|
||||||
let deposit_data_roots = mapIt(deposits, it.data.hash_tree_root)
|
let deposit_data_roots = mapIt(deposits, it.data.hash_tree_root)
|
||||||
var
|
var
|
||||||
|
@ -91,9 +92,9 @@ func attachMerkleProofs*(deposits: var openarray[Deposit]) =
|
||||||
deposit_data_roots, 1'i64 shl DEPOSIT_CONTRACT_TREE_DEPTH):
|
deposit_data_roots, 1'i64 shl DEPOSIT_CONTRACT_TREE_DEPTH):
|
||||||
deposit_data_sums.add prefix_root
|
deposit_data_sums.add prefix_root
|
||||||
|
|
||||||
|
let merkle_tree = merkleTreeFromLeaves(deposit_data_roots)
|
||||||
for val_idx in 0 ..< deposits.len:
|
for val_idx in 0 ..< deposits.len:
|
||||||
let merkle_tree = merkleTreeFromLeaves(deposit_data_roots[0..val_idx])
|
deposits[val_idx].proof[0..31] = merkle_tree.getMerkleProof(val_idx, true)
|
||||||
deposits[val_idx].proof[0..31] = merkle_tree.getMerkleProof(val_idx)
|
|
||||||
deposits[val_idx].proof[32].data[0..7] = uint_to_bytes8((val_idx + 1).uint64)
|
deposits[val_idx].proof[32].data[0..7] = uint_to_bytes8((val_idx + 1).uint64)
|
||||||
|
|
||||||
doAssert is_valid_merkle_branch(
|
doAssert is_valid_merkle_branch(
|
||||||
|
|
|
@ -40,7 +40,7 @@ type Timers = enum
|
||||||
|
|
||||||
# TODO confutils is an impenetrable black box. how can a help text be added here?
|
# TODO confutils is an impenetrable black box. how can a help text be added here?
|
||||||
cli do(slots = SLOTS_PER_EPOCH * 6,
|
cli do(slots = SLOTS_PER_EPOCH * 6,
|
||||||
validators = SLOTS_PER_EPOCH * 130, # One per shard is minimum
|
validators = SLOTS_PER_EPOCH * 200, # One per shard is minimum
|
||||||
attesterRatio {.desc: "ratio of validators that attest in each round"} = 0.73,
|
attesterRatio {.desc: "ratio of validators that attest in each round"} = 0.73,
|
||||||
blockRatio {.desc: "ratio of slots with blocks"} = 1.0,
|
blockRatio {.desc: "ratio of slots with blocks"} = 1.0,
|
||||||
replay = true):
|
replay = true):
|
||||||
|
|
|
@ -34,7 +34,7 @@ proc writeJson*(fn, v: auto) =
|
||||||
Json.saveFile(fn, v, pretty = true)
|
Json.saveFile(fn, v, pretty = true)
|
||||||
|
|
||||||
cli do(slots = SLOTS_PER_EPOCH * 6,
|
cli do(slots = SLOTS_PER_EPOCH * 6,
|
||||||
validators = SLOTS_PER_EPOCH * 100, # One per shard is minimum
|
validators = SLOTS_PER_EPOCH * 200, # One per shard is minimum
|
||||||
json_interval = SLOTS_PER_EPOCH,
|
json_interval = SLOTS_PER_EPOCH,
|
||||||
write_last_json = false,
|
write_last_json = false,
|
||||||
prefix: int = 0,
|
prefix: int = 0,
|
||||||
|
|
Loading…
Reference in New Issue