|
| 1 | +const { BigNumber } = require("ethers") |
| 2 | +const { expect } = require("chai") |
| 3 | + |
| 4 | +describe("Leaf", () => { |
| 5 | + let leafInstance |
| 6 | + |
| 7 | + beforeEach(async () => { |
| 8 | + const LeafStub = await ethers.getContractFactory("LeafStub") |
| 9 | + leafInstance = await LeafStub.deploy() |
| 10 | + await leafInstance.deployed() |
| 11 | + }) |
| 12 | + |
| 13 | + describe("make", async () => { |
| 14 | + it("constructs a leaf with proper structure (address, creationBlock, id)", async () => { |
| 15 | + // We store a leaf as a uint256, which is 256 bits. We pack the address |
| 16 | + // in the first 160 bits, the creation block in the 64 bits after that, |
| 17 | + // and the operator's id in the final 32 bits. Each hex character |
| 18 | + // represents 4 bits, so a 40-character hex representation of an eth |
| 19 | + // address is the exact 160 bits we need. A number (like a creation block |
| 20 | + // or id) is big-endian and gets left-padded (see |
| 21 | + // https://github.com/ethereum/solidity-examples/issues/54) with zeros |
| 22 | + // to fill the exact bit count. |
| 23 | + const testData = [ |
| 24 | + { |
| 25 | + address: "0x4ddc2d193948926d02f9b1fe9e1daa0718270ed5", |
| 26 | + creationBlock: BigNumber.from("0xDFD871"), |
| 27 | + id: 0x1995b5, |
| 28 | + expectation: |
| 29 | + "0x4ddc2d193948926d02f9b1fe9e1daa0718270ed50000000000DFD871001995B5", |
| 30 | + // read: 4ddc2d193948926d02f9b1fe9e1daa0718270ed5,0000000000DFD871,001995B5 |
| 31 | + // address creationBlock id |
| 32 | + }, |
| 33 | + { |
| 34 | + address: "0x176f3dab24a159341c0509bb36b833e7fdd0a132", |
| 35 | + creationBlock: BigNumber.from("0x5E8EEE0"), |
| 36 | + id: 0x8f573, |
| 37 | + expectation: |
| 38 | + "0x176f3dab24a159341c0509bb36b833e7fdd0a1320000000005E8EEE00008F573", |
| 39 | + // read: 176f3dab24a159341c0509bb36b833e7fdd0a132,0000000005E8EEE0,0008F573 |
| 40 | + // address creationBlock id |
| 41 | + }, |
| 42 | + { |
| 43 | + address: "0x2b6ed29a95753c3ad948348e3e7b1a251080ffb9", |
| 44 | + creationBlock: BigNumber.from("0x771D8DB"), |
| 45 | + id: 0xca009, |
| 46 | + expectation: |
| 47 | + "0x2b6ed29a95753c3ad948348e3e7b1a251080ffb9000000000771D8DB000CA009", |
| 48 | + // read: 2b6ed29a95753c3ad948348e3e7b1a251080ffb9,000000000771D8DB,000CA009 |
| 49 | + // address creationBlock id |
| 50 | + }, |
| 51 | + ] |
| 52 | + |
| 53 | + for (let i = 0; i < testData.length; i++) { |
| 54 | + const test = testData[i] |
| 55 | + const leaf = await leafInstance.make( |
| 56 | + test.address, |
| 57 | + test.creationBlock, |
| 58 | + test.id, |
| 59 | + ) |
| 60 | + expect(leaf).to.equal( |
| 61 | + test.expectation, |
| 62 | + `unexpected result for test index ${i}`, |
| 63 | + ) |
| 64 | + } |
| 65 | + }) |
| 66 | + }) |
| 67 | + |
| 68 | + describe("operator", async () => { |
| 69 | + it("retrieves the operator from the leaf", async () => { |
| 70 | + const testData = [ |
| 71 | + { |
| 72 | + leaf: "0x4Ddc2D193948926D02f9B1fE9e1daa0718270ED50000000000DFD871001995B5", |
| 73 | + // 4Ddc2D193948926D02f9B1fE9e1daa0718270ED5,0000000000DFD871,001995B5 |
| 74 | + // address creationBlock id |
| 75 | + operator: "0x4Ddc2D193948926D02f9B1fE9e1daa0718270ED5", |
| 76 | + }, |
| 77 | + { |
| 78 | + leaf: "0x176F3DAb24a159341c0509bB36B833E7fdd0a1320000000005E8EEE00008F573", |
| 79 | + // 176F3DAb24a159341c0509bB36B833E7fdd0a132,0000000005E8EEE0,0008F573 |
| 80 | + // address creationBlock id |
| 81 | + operator: "0x176F3DAb24a159341c0509bB36B833E7fdd0a132", |
| 82 | + }, |
| 83 | + { |
| 84 | + leaf: "0x2B6eD29A95753C3Ad948348e3e7b1A251080Ffb9000000000771D8DB000CA009", |
| 85 | + // 2B6eD29A95753C3Ad948348e3e7b1A251080Ffb9,000000000771D8DB,000CA009 |
| 86 | + // address creationBlock id |
| 87 | + operator: "0x2B6eD29A95753C3Ad948348e3e7b1A251080Ffb9", |
| 88 | + }, |
| 89 | + ] |
| 90 | + |
| 91 | + for (let i = 0; i < testData.length; i++) { |
| 92 | + const test = testData[i] |
| 93 | + const operator = await leafInstance.operator(BigNumber.from(test.leaf)) |
| 94 | + expect(operator).to.equal( |
| 95 | + test.operator, |
| 96 | + `unexpected result for test index ${i}`, |
| 97 | + ) |
| 98 | + } |
| 99 | + }) |
| 100 | + }) |
| 101 | + |
| 102 | + describe("creationBlock", async () => { |
| 103 | + it("retrieves the creation block from the leaf", async () => { |
| 104 | + const testData = [ |
| 105 | + { |
| 106 | + leaf: "0x4ddc2d193948926d02f9b1fe9e1daa0718270ed50000000000DFD871001995B5", |
| 107 | + // 4ddc2d193948926d02f9b1fe9e1daa0718270ed5,0000000000DFD871,001995B5 |
| 108 | + // address creationBlock id |
| 109 | + creationBlock: "0xDFD871", |
| 110 | + }, |
| 111 | + { |
| 112 | + leaf: "0x176f3dab24a159341c0509bb36b833e7fdd0a1320000000005E8EEE00008F573", |
| 113 | + // 176f3dab24a159341c0509bb36b833e7fdd0a132,0000000005E8EEE0,0008F573 |
| 114 | + // address creationBlock id |
| 115 | + creationBlock: "0x5E8EEE0", |
| 116 | + }, |
| 117 | + { |
| 118 | + leaf: "0x2b6ed29a95753c3ad948348e3e7b1a251080ffb9000000000771D8DB000CA009", |
| 119 | + // 2b6ed29a95753c3ad948348e3e7b1a251080ffb9,000000000771D8DB,000CA009 |
| 120 | + // address creationBlock id |
| 121 | + creationBlock: "0x771D8DB", |
| 122 | + }, |
| 123 | + ] |
| 124 | + |
| 125 | + for (let i = 0; i < testData.length; i++) { |
| 126 | + const test = testData[i] |
| 127 | + const creationBlock = await leafInstance.creationBlock( |
| 128 | + BigNumber.from(test.leaf), |
| 129 | + ) |
| 130 | + expect(creationBlock).to.equal( |
| 131 | + test.creationBlock, |
| 132 | + `unexpected result for test index ${i}`, |
| 133 | + ) |
| 134 | + } |
| 135 | + }) |
| 136 | + }) |
| 137 | + |
| 138 | + describe("id", async () => { |
| 139 | + it("retrives the operator's id from the leaf", async () => { |
| 140 | + const testData = [ |
| 141 | + { |
| 142 | + leaf: "0x4ddc2d193948926d02f9b1fe9e1daa0718270ed50000000000DFD871001995B5", |
| 143 | + // 4ddc2d193948926d02f9b1fe9e1daa0718270ed5,0000000000DFD871,001995B5 |
| 144 | + // address creationBlock id |
| 145 | + id: "0x1995B5", |
| 146 | + }, |
| 147 | + { |
| 148 | + leaf: "0x176f3dab24a159341c0509bb36b833e7fdd0a1320000000005E8EEE00008F573", |
| 149 | + // 176f3dab24a159341c0509bb36b833e7fdd0a132,0000000005E8EEE0,0008F573 |
| 150 | + // address creationBlock id |
| 151 | + id: "0x8F573", |
| 152 | + }, |
| 153 | + { |
| 154 | + leaf: "0x2b6ed29a95753c3ad948348e3e7b1a251080ffb9000000000771D8DB000CA009", |
| 155 | + // 2b6ed29a95753c3ad948348e3e7b1a251080ffb9,000000000771D8DB,000CA009 |
| 156 | + // address creationBlock id |
| 157 | + id: "0xCA009", |
| 158 | + }, |
| 159 | + ] |
| 160 | + for (let i = 0; i < testData.length; i++) { |
| 161 | + const test = testData[i] |
| 162 | + const id = await leafInstance.id(BigNumber.from(test.leaf)) |
| 163 | + expect(id).to.equal( |
| 164 | + parseInt(test.id, 16), |
| 165 | + `unexpected result for test index ${i}`, |
| 166 | + ) |
| 167 | + } |
| 168 | + }) |
| 169 | + }) |
| 170 | +}) |
0 commit comments