mirror of
https://github.com/scroll-tech/scroll.git
synced 2026-01-13 16:08:04 -05:00
Co-authored-by: colin <102356659+colinlyguo@users.noreply.github.com> Co-authored-by: yiweichi <yiweichi@users.noreply.github.com>
52 lines
1.2 KiB
Go
52 lines
1.2 KiB
Go
package utils
|
|
|
|
import (
|
|
"encoding/hex"
|
|
"encoding/json"
|
|
"os"
|
|
"testing"
|
|
|
|
"github.com/scroll-tech/go-ethereum/crypto/kzg4844"
|
|
)
|
|
|
|
type BlobData struct {
|
|
VersionedBlobHash string `json:"versionedBlobHash"`
|
|
BlobData string `json:"blobData"`
|
|
}
|
|
|
|
// TestCalculateVersionedBlobHash tests the CalculateVersionedBlobHash function
|
|
func TestCalculateVersionedBlobHash(t *testing.T) {
|
|
// Read the test data
|
|
data, err := os.ReadFile("../testdata/blobdata.json")
|
|
if err != nil {
|
|
t.Fatalf("Failed to read blobdata.json: %v", err)
|
|
}
|
|
|
|
var blobData BlobData
|
|
if err := json.Unmarshal(data, &blobData); err != nil {
|
|
t.Fatalf("Failed to parse blobdata.json: %v", err)
|
|
}
|
|
|
|
blobBytes, err := hex.DecodeString(blobData.BlobData)
|
|
if err != nil {
|
|
t.Fatalf("Failed to decode blob data: %v", err)
|
|
}
|
|
|
|
// Convert []byte to kzg4844.Blob
|
|
var blob kzg4844.Blob
|
|
copy(blob[:], blobBytes)
|
|
|
|
// Calculate the hash
|
|
calculatedHashBytes, err := CalculateVersionedBlobHash(blob)
|
|
if err != nil {
|
|
t.Fatalf("Failed to calculate versioned blob hash: %v", err)
|
|
}
|
|
|
|
calculatedHash := hex.EncodeToString(calculatedHashBytes[:])
|
|
|
|
if calculatedHash != blobData.VersionedBlobHash {
|
|
t.Fatalf("Hash mismatch: got %s, want %s", calculatedHash, blobData.VersionedBlobHash)
|
|
}
|
|
|
|
}
|