Skip to content

Commit

Permalink
change scoring to discuss
Browse files Browse the repository at this point in the history
  • Loading branch information
dianakocsis committed Oct 7, 2024
1 parent cf4c612 commit 5db7a3a
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 28 deletions.
24 changes: 14 additions & 10 deletions src/libraries/VanityAddressLib.sol
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ library VanityAddressLib {
/// @param addr The address to score
/// @return calculatedScore The vanity score of the address
function score(address addr) internal pure returns (uint256 calculatedScore) {
// 10 points for every leading 0 byte
// 5 points for every leading 0x44 after the 0 bytes
// 10 points for every leading 0
// 3 points for every leading 4
// 1 point for every 4 after that
bytes20 addrBytes = bytes20(addr);

Expand All @@ -28,11 +28,18 @@ library VanityAddressLib {
if (startingZeros && addrBytes[i] == 0x00) {
calculatedScore += 20;
continue;
} else if (startingZeros && (addrBytes[i] & 0xF0) == 0x00) {
calculatedScore += 10;
startingZeros = false;
} else {
startingZeros = false;
}
if (startingFours && addrBytes[i] == 0x44) {
calculatedScore += 5;
calculatedScore += 6;
continue;
} else if (startingFours && (addrBytes[i] & 0xF0 == 0x40) || (addrBytes[i] & 0xFF == 0x04)) {
calculatedScore += 3;
startingFours = false;
continue;
} else {
startingFours = false;
Expand All @@ -42,13 +49,10 @@ library VanityAddressLib {
// count each nibble separately
if (addrBytes[i] & 0xFF == 0x44) {
calculatedScore += 2;
} else {
if (addrBytes[i] & 0x0F == 0x04) {
calculatedScore += 1;
}
if (addrBytes[i] & 0xF0 == 0x40) {
calculatedScore += 1;
}
} else if (addrBytes[i] & 0x0F == 0x04) {
calculatedScore += 1;
} else if (addrBytes[i] & 0xF0 == 0x40) {
calculatedScore += 1;
}
}
}
Expand Down
45 changes: 27 additions & 18 deletions test/libraries/VanityAddressLib.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ contract VanityAddressLibTest is Test {
function test_scoreAllFours() public pure {
address addr = address(0x4444444444444444444444444444444444444444);
uint256 score = VanityAddressLib.score(addr);
uint256 expected = 100; // 20 * 5
uint256 expected = 120; // 6 * 20
assertEq(score, expected);
}

Expand All @@ -33,7 +33,7 @@ contract VanityAddressLibTest is Test {
// does not count future null bytes
// counts 4 nibbles after that
uint256 score = VanityAddressLib.score(addr);
uint256 expected = 27; // 20+5+1+1
uint256 expected = 28; // 20+6+1+1
assertEq(score, expected);
}

Expand All @@ -44,31 +44,40 @@ contract VanityAddressLibTest is Test {
// does not count future null bytes
// counts 4 nibbles after that
uint256 score = VanityAddressLib.score(addr);
uint256 expected = 29; // 20+5+2+2
uint256 expected = 30; // 20+6+2+2
assertEq(score, expected);
}

function test_scoreMixed_halfZeroHalf4() public pure {
address addr = address(0x0004111111111111111111111111111111111111);
// counts first null byte
// counts first leading 4s after that
uint256 score = VanityAddressLib.score(addr);
uint256 expected = 33; // 20+10+3
assertEq(score, expected);
}

function test_betterThan() public pure {
address addr1 = address(0x0011111111111111111111111111111111111111); // 20 points
address addr2 = address(0x0000111111111111111111111111111111111111); // 40 points
address addr3 = address(0x0000411111111111111111111111111111111111); // 41 points
address addr4 = address(0x0000441111111111111111111111111111111111); // 45 points
address addr5 = address(0x0000440011111111111111111111111111111111); // 45 points
address addr3 = address(0x0000411111111111111111111111111111111111); // 43 points
address addr4 = address(0x0000441111111111111111111111111111111111); // 46 points
address addr5 = address(0x0000440011111111111111111111111111111111); // 46 points
assertTrue(VanityAddressLib.betterThan(addr2, addr1)); // 40 > 20
assertTrue(VanityAddressLib.betterThan(addr3, addr2)); // 41 > 40
assertTrue(VanityAddressLib.betterThan(addr3, addr1)); // 41 > 20
assertTrue(VanityAddressLib.betterThan(addr4, addr3)); // 45 > 41
assertTrue(VanityAddressLib.betterThan(addr4, addr2)); // 45 > 40
assertTrue(VanityAddressLib.betterThan(addr4, addr1)); // 45 > 20
assertFalse(VanityAddressLib.betterThan(addr5, addr4)); // 45 == 45
assertEq(VanityAddressLib.score(addr5), VanityAddressLib.score(addr4)); // 45 == 45
assertTrue(VanityAddressLib.betterThan(addr5, addr3)); // 45 > 41
assertTrue(VanityAddressLib.betterThan(addr5, addr2)); // 45 > 40
assertTrue(VanityAddressLib.betterThan(addr5, addr1)); // 45 > 20
assertTrue(VanityAddressLib.betterThan(addr3, addr2)); // 43 > 40
assertTrue(VanityAddressLib.betterThan(addr3, addr1)); // 43 > 20
assertTrue(VanityAddressLib.betterThan(addr4, addr3)); // 46 > 43
assertTrue(VanityAddressLib.betterThan(addr4, addr2)); // 46 > 40
assertTrue(VanityAddressLib.betterThan(addr4, addr1)); // 46 > 20
assertFalse(VanityAddressLib.betterThan(addr5, addr4)); // 46 == 46
assertEq(VanityAddressLib.score(addr5), VanityAddressLib.score(addr4)); // 46 == 46
assertTrue(VanityAddressLib.betterThan(addr5, addr3)); // 46 > 43
assertTrue(VanityAddressLib.betterThan(addr5, addr2)); // 46 > 40
assertTrue(VanityAddressLib.betterThan(addr5, addr1)); // 46 > 20

// is this intentional?
address addr6 = address(0x0000000000000000000000000000000000044444);
address addr6 = address(0x0000000000000000000000000000000000004444);
address addr7 = address(0x0000000000000000000000000000000000000082);
assertFalse(VanityAddressLib.betterThan(addr6, addr7)); // 20 * 18 + 5 + 5 = 370 < 20 * 19 = 380
assertFalse(VanityAddressLib.betterThan(addr6, addr7)); // 20 * 18 + 6 + 6 = 372 < 20 * 19 = 380
}
}

0 comments on commit 5db7a3a

Please sign in to comment.