Skip to content

Commit 261600c

Browse files
committed
Merge remote-tracking branch 'origin/master' into fully-backed-tweaks
2 parents 413abd6 + aa2a641 commit 261600c

File tree

10 files changed

+3336
-1137
lines changed

10 files changed

+3336
-1137
lines changed

.github/workflows/solidity-test.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ jobs:
1616
- name: Run tests
1717
# Runs ganache in background
1818
run: |
19-
npm run ganache &
19+
npm run buidler-vm &
2020
npm test
2121
env:
2222
CI: true

buidler.config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = {};

contracts/Leaf.sol

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,19 +24,19 @@ library Leaf {
2424
////////////////////////////////////////////////////////////////////////////
2525

2626
function make(
27-
address operator,
28-
uint256 creationBlock,
29-
uint256 weight
27+
address _operator,
28+
uint256 _creationBlock,
29+
uint256 _weight
3030
) internal pure returns (uint256) {
3131
// Converting a bytesX type into a larger type
3232
// adds zero bytes on the right.
33-
uint256 op = uint256(bytes32(bytes20(operator)));
33+
uint256 op = uint256(bytes32(bytes20(_operator)));
3434
// Bitwise AND the weight to erase
3535
// all but the 32 least significant bits
36-
uint256 wt = weight & WEIGHT_MAX;
36+
uint256 wt = _weight & WEIGHT_MAX;
3737
// Erase all but the 64 least significant bits,
3838
// then shift left by 32 bits to make room for the weight
39-
uint256 cb = (creationBlock & BLOCKHEIGHT_MAX) << WEIGHT_WIDTH;
39+
uint256 cb = (_creationBlock & BLOCKHEIGHT_MAX) << WEIGHT_WIDTH;
4040
// Bitwise OR them all together to get
4141
// [address operator || uint64 creationBlock || uint32 weight]
4242
return (op | cb | wt);

contracts/RNG.sol

Lines changed: 17 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,12 @@ library RNG {
1212
// How many bits a position uses per level of the tree;
1313
// each branch of the tree contains 2**SLOT_BITS slots.
1414
uint256 constant SLOT_BITS = 3;
15-
uint256 constant LEVELS = 7;
1615
////////////////////////////////////////////////////////////////////////////
1716

1817
////////////////////////////////////////////////////////////////////////////
1918
// Derived constants, do not touch
20-
uint256 constant POSITION_BITS = LEVELS * SLOT_BITS;
19+
uint256 constant SLOT_COUNT = 2**SLOT_BITS;
20+
uint256 constant WEIGHT_WIDTH = 256 / SLOT_COUNT;
2121
////////////////////////////////////////////////////////////////////////////
2222

2323
struct State {
@@ -127,30 +127,26 @@ library RNG {
127127
/// @notice Calculate how many bits are required
128128
/// for an index in the range `[0 .. range-1]`.
129129
///
130-
/// @dev Our sortition pool can support up to 2^21 virtual stakers,
131-
/// therefore we calculate how many bits we need from 1 to 21.
132-
///
133130
/// @param range The upper bound of the desired range, exclusive.
134131
///
135132
/// @return uint The smallest number of bits
136133
/// that can contain the number `range-1`.
137134
function bitsRequired(uint256 range) internal pure returns (uint256) {
138-
uint256 bits;
139-
// Start at 19 to be faster for large ranges
140-
for (bits = (POSITION_BITS - 1); bits >= 0; bits--) {
141-
// Left shift by `bits`,
142-
// so we have a 1 in the (bits + 1)th least significant bit
143-
// and 0 in other bits.
144-
// If this number is equal or greater than `range`,
145-
// the range [0, range-1] fits in `bits` bits.
146-
//
147-
// Because we loop from high bits to low bits,
148-
// we find the highest number of bits that doesn't fit the range,
149-
// and return that number + 1.
150-
if (1 << bits < range) {
151-
break;
152-
}
135+
uint256 bits = WEIGHT_WIDTH - 1;
136+
137+
// Left shift by `bits`,
138+
// so we have a 1 in the (bits + 1)th least significant bit
139+
// and 0 in other bits.
140+
// If this number is equal or greater than `range`,
141+
// the range [0, range-1] fits in `bits` bits.
142+
//
143+
// Because we loop from high bits to low bits,
144+
// we find the highest number of bits that doesn't fit the range,
145+
// and return that number + 1.
146+
while (1 << bits >= range) {
147+
bits--;
153148
}
149+
154150
return bits + 1;
155151
}
156152

@@ -197,7 +193,7 @@ library RNG {
197193
{
198194
uint256 bits = bitsRequired(range);
199195
bool found = false;
200-
uint256 index;
196+
uint256 index = 0;
201197
bytes32 newState = state;
202198
while (!found) {
203199
index = truncate(bits, uint256(newState));

0 commit comments

Comments
 (0)