Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't omit totalNodeWeight if not set. Add a test to cement CID calculation. #433

Merged
merged 3 commits into from
Jan 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 54 additions & 0 deletions shared/services/rewards/files_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package rewards

import (
"encoding/hex"
"fmt"
"os"
"path"
"testing"
Expand Down Expand Up @@ -167,3 +169,55 @@ func TestCompressionAndCids(t *testing.T) {
)
}
}

// Methodology:
// First, we test against rp-rewards-mainnet-17.json, the official version from ipfs.
// Once manually confirming the CID matches the on-chain value, we test against a smaller
// file.
//
// The CID in this test _should never be updated_.
//
// If new code changes the ipfs cid calculation, it needs to do so in a way that preserves
// the pre-existing behavior for historical trees.
func TestCidConsistency(t *testing.T) {
/* Commenting out the code used to verify the cid calculator matched an on-chain cid
// Load rp-rewards-mainnet-17.json and check the resulting cid
localRewardsFile, err := ReadLocalRewardsFile("rp-rewards-mainnet-17.json")
if err != nil {
t.Fatal(err)
}

// Validate its CID against the on-chain version
cid, err := localRewardsFile.CreateCompressedFileAndCid()
if err != nil {
t.Fatal(err)
}

err = localRewardsFile.Write()
if err != nil {
t.Fatal(err)
}

if fmt.Sprint(cid) != "bafybeifldymulw6qvlfjgntj6mrlbwcl46xn6njickcydlquxc33nseoxi" {
t.Fatalf("unexpected cid %s", cid)
}
*/

// 256 random bytes from /dev/urandom on a cloudy day in Brooklyn
// dd if=/dev/urandom bs=1 count=256 | xxd -p
randomCharacters := `e73d6923a8b99cbd9de59619626292b5173f27ddaf50f21ce885272ab63060c8acfe10a066b24a457232afa00ef23f8be61d112935dbaa81658ba1699e5eef9dd973ac2c8d7ecbaee7063c25ca040eb446139cf99630510b3514ff5c4c2d5be13a2a73cb55cf27e743b2f317153fbbfd3f8e3c3c788160a2458c69c6fd905fd4ce5afc3634532d1f6e2e27fb1cb049356d8ccc6599710d82cf75b65f2d03e6d969d0200b18f0217e3aa500a5053636f105126ff0d00c6b8e0f47f2cc5f1ec73bc9e66f023f79ab09fd3a5f7c5ee988ec4028479026bc02fb1ab22f50eaf985c1d0c357cdeca0cfbe49e465fb3967a42b4d2e63949910cef8487ba5853eaee442`
data, err := hex.DecodeString(randomCharacters)
if err != nil {
t.Fatal(err)
}

cid, err := singleFileDirIPFSCid(data, "test.bin")
if err != nil {
t.Fatal(err)
}

t.Logf("Computed CID: %s", fmt.Sprint(cid))
if "bafybeibqxb2xeoh2mlcn7543jr3tgvdu74mqqd43esrttyktmu3ubtx63i" != fmt.Sprint(cid) {
t.Fatal("CID did not match expectations. If changing CID computation logic, ensure historical CIDs can be recomputed. See comments in files_test.go for more info")
}
}
2 changes: 1 addition & 1 deletion shared/services/rewards/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ type TotalRewards struct {
TotalSmoothingPoolEth *QuotedBigInt `json:"totalSmoothingPoolEth"`
PoolStakerSmoothingPoolEth *QuotedBigInt `json:"poolStakerSmoothingPoolEth"`
NodeOperatorSmoothingPoolEth *QuotedBigInt `json:"nodeOperatorSmoothingPoolEth"`
TotalNodeWeight *QuotedBigInt `json:"totalNodeWeight"`
TotalNodeWeight *QuotedBigInt `json:"totalNodeWeight,omitempty"`
}

// Minipool stats
Expand Down