Skip to content

Commit 8739341

Browse files
Merge pull request #172 from keep-network/improve-branch-tests
Improve Coverage and Instructiveness for branchTest.js
2 parents 501bfa3 + 21e5062 commit 8739341

1 file changed

Lines changed: 75 additions & 13 deletions

File tree

test/branchTest.js

Lines changed: 75 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@ const { BigNumber } = require("ethers")
22
const { expect } = require("chai")
33

44
describe("Branch", () => {
5+
// A branch is a uint256, meaning it's represented with 256 bits. Each branch
6+
// represents 8 children, where each child gets 32 sequential bits. A hex
7+
// number represents 4 bits, so an 8-character long hex string is 32 bits. We
8+
// store children right-to-left, and use values like 00000000 and 11111111 to
9+
// make it visually clear where one slot starts and stops.
510
const node = BigNumber.from(
611
"0x7777777766666666555555554444444433333333222222221111111100000000",
712
)
@@ -20,37 +25,94 @@ describe("Branch", () => {
2025

2126
describe("getSlot", async () => {
2227
it("Returns the uint16 in the correct position", async () => {
23-
const result = await branchInstance.getSlot(node, 3)
24-
expect(result).to.be.equal(0x33333333)
28+
const expectations = [
29+
0x00000000, 0x11111111, 0x22222222, 0x33333333, 0x44444444, 0x55555555,
30+
0x66666666, 0x77777777,
31+
]
32+
for (let slot = 0; slot < 8; slot++) {
33+
const result = await branchInstance.getSlot(node, slot)
34+
expect(result).to.equal(
35+
expectations[slot],
36+
`unexpected result at position ${slot}`,
37+
)
38+
}
2539
})
2640
})
2741

2842
describe("clearSlot", async () => {
2943
it("Clears the correct slot", async () => {
30-
newNode =
31-
"0x7777777766666666555555554444444400000000222222221111111100000000"
44+
const expectations = [
45+
"0x7777777766666666555555554444444433333333222222221111111100000000",
46+
"0x7777777766666666555555554444444433333333222222220000000000000000",
47+
"0x7777777766666666555555554444444433333333000000001111111100000000",
48+
"0x7777777766666666555555554444444400000000222222221111111100000000",
49+
"0x7777777766666666555555550000000033333333222222221111111100000000",
50+
"0x7777777766666666000000004444444433333333222222221111111100000000",
51+
"0x7777777700000000555555554444444433333333222222221111111100000000",
52+
"0x66666666555555554444444433333333222222221111111100000000",
53+
]
3254

33-
const result = await branchInstance.clearSlot(node, 3)
34-
expect(ethers.utils.hexlify(result)).to.be.equal(newNode)
55+
for (let slot = 0; slot < 8; slot++) {
56+
const result = await branchInstance.clearSlot(node, slot)
57+
expect(ethers.utils.hexlify(result)).to.equal(
58+
expectations[slot],
59+
`unexpected result at position ${slot}`,
60+
)
61+
}
62+
63+
// verify that clearing slot 0 actually works!
64+
const onesNode = BigNumber.from(
65+
"0x1111111111111111111111111111111111111111111111111111111111111111",
66+
)
67+
const result = await branchInstance.clearSlot(onesNode, 0)
68+
const expectation =
69+
"0x1111111111111111111111111111111111111111111111111111111100000000"
70+
expect(ethers.utils.hexlify(result)).to.equal(expectation)
3571
})
3672
})
3773

3874
describe("setSlot", async () => {
3975
it("Changes the correct slot", async () => {
40-
newNode =
41-
"0x7777777766666666555555554444444412345678222222221111111100000000"
42-
w = 0x12345678
76+
const newWeight = 0x12345678
77+
const expectations = [
78+
"0x7777777766666666555555554444444433333333222222221111111112345678",
79+
"0x7777777766666666555555554444444433333333222222221234567800000000",
80+
"0x7777777766666666555555554444444433333333123456781111111100000000",
81+
"0x7777777766666666555555554444444412345678222222221111111100000000",
82+
"0x7777777766666666555555551234567833333333222222221111111100000000",
83+
"0x7777777766666666123456784444444433333333222222221111111100000000",
84+
"0x7777777712345678555555554444444433333333222222221111111100000000",
85+
"0x1234567866666666555555554444444433333333222222221111111100000000",
86+
]
87+
for (let slot = 0; slot < 8; slot++) {
88+
const result = await branchInstance.setSlot(node, slot, newWeight)
89+
expect(ethers.utils.hexlify(result)).to.equal(
90+
expectations[slot],
91+
`unexpected result at position ${slot}`,
92+
)
4393

44-
const modified = await branchInstance.setSlot(node, 3, w)
45-
newSlot = await branchInstance.getSlot(modified, 3)
46-
expect(ethers.utils.hexlify(modified)).to.be.equal(newNode)
94+
const newSlot = await branchInstance.getSlot(result, slot)
95+
expect(newSlot).to.equal(
96+
newWeight,
97+
`unexpected result at position ${slot}`,
98+
)
99+
}
47100
})
48101
})
49102

50103
describe("sumWeight", async () => {
51104
it("Returns the correct weight", async () => {
52105
const weight = await branchInstance.sumWeight(node)
53-
const expected = 0x77777777 * 4
106+
const expected =
107+
0x00000000 +
108+
0x11111111 +
109+
0x22222222 +
110+
0x33333333 +
111+
0x44444444 +
112+
0x55555555 +
113+
0x66666666 +
114+
0x77777777
115+
54116
expect(weight).to.be.equal(expected)
55117
})
56118
})

0 commit comments

Comments
 (0)