Skip to content

Commit

Permalink
Fix integration tests
Browse files Browse the repository at this point in the history
  • Loading branch information
carlos-romano committed Feb 20, 2025
1 parent 4562159 commit 90662c7
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 99 deletions.
17 changes: 17 additions & 0 deletions internal/crypto/hash.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package crypto

import (
"encoding/hex"
"golang.org/x/crypto/blake2b"
"golang.org/x/crypto/sha3"
"log"
"strings"
)

type Hash [HashSize]byte
Expand All @@ -22,3 +25,17 @@ func KeccakData(data []byte) Hash {
copy(result[:], hashed)
return result
}

// StringToHex converts a hex string to a byte slice
func StringToHex(s string) []byte {
// Remove 0x prefix if present
s = strings.TrimPrefix(s, "0x")

// Decode hex string
bytes, err := hex.DecodeString(s)
if err != nil {
log.Printf("Error decoding hex string '%s': %v", s, err)
panic(err)
}
return bytes
}
8 changes: 4 additions & 4 deletions tests/integration/assurances_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,9 @@ func mapBlock(i AssurancesInput) block.Block {
EA: mapSlice(i.Assurances, func(a Assurance) block.Assurance {
return block.Assurance{
Anchor: mapHash(a.Anchor),
Bitfield: [block.AvailBitfieldBytes]byte(stringToHex(a.Bitfield)),
Bitfield: [block.AvailBitfieldBytes]byte(crypto.StringToHex(a.Bitfield)),
ValidatorIndex: a.ValidatorIndex,
Signature: crypto.Ed25519Signature(stringToHex(a.Signature)),
Signature: crypto.Ed25519Signature(crypto.StringToHex(a.Signature)),
}
}),
},
Expand Down Expand Up @@ -142,7 +142,7 @@ func mapReport(r *Report) *block.WorkReport {
},
CoreIndex: r.CoreIndex,
AuthorizerHash: mapHash(r.AuthorizerHash),
Output: stringToHex(r.AuthOutput),
Output: crypto.StringToHex(r.AuthOutput),
SegmentRootLookup: segmentRootLookup,
WorkResults: mapSlice(r.Results, func(rr ReportResult) block.WorkResult {
return block.WorkResult{
Expand All @@ -159,7 +159,7 @@ func mapReport(r *Report) *block.WorkReport {
}

func mapHash(s string) crypto.Hash {
return crypto.Hash(stringToHex(s))
return crypto.Hash(crypto.StringToHex(s))
}

func TestAssurancesTiny(t *testing.T) {
Expand Down
25 changes: 6 additions & 19 deletions tests/integration/authorizations_test.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package integration_test
//go:build integration

package integration

import (
"encoding/hex"
"encoding/json"
"fmt"
"github.com/eigerco/strawberry/internal/common"
"github.com/eigerco/strawberry/internal/crypto"
"io"
"log"
"os"
"strings"
"testing"
Expand Down Expand Up @@ -77,7 +77,7 @@ func mapAuthState(s AuthState) state.State {
}
authPools[i] = make([]crypto.Hash, len(pool))
for j, hash := range pool {
authPools[i][j] = crypto.Hash(stringToHex(hash))
authPools[i][j] = crypto.Hash(crypto.StringToHex(hash))
}
}

Expand All @@ -90,7 +90,7 @@ func mapAuthState(s AuthState) state.State {
}
// Iterate over all hashes in the JSON slice and assign them to the queue
for i, hashStr := range auths {
authQueues[uint16(coreIndex)][i] = crypto.Hash(stringToHex(hashStr))
authQueues[uint16(coreIndex)][i] = crypto.Hash(crypto.StringToHex(hashStr))
}
}

Expand All @@ -100,19 +100,6 @@ func mapAuthState(s AuthState) state.State {
}
}

func stringToHex(s string) []byte {
// Remove 0x prefix if present
s = strings.TrimPrefix(s, "0x")

// Decode hex string
bytes, err := hex.DecodeString(s)
if err != nil {
log.Printf("Error decoding hex string '%s': %v", s, err)
panic(err)
}
return bytes
}

func TestAuthorizations(t *testing.T) {
files, err := os.ReadDir("vectors/authorizations/tiny")
require.NoError(t, err, "failed to read authorizations directory")
Expand Down Expand Up @@ -186,7 +173,7 @@ func mapGuarantees(data *AuthData) block.GuaranteesExtrinsic {
guarantee := block.Guarantee{
WorkReport: block.WorkReport{
CoreIndex: uint16(auth.Core),
AuthorizerHash: crypto.Hash(stringToHex(auth.AuthHash)),
AuthorizerHash: crypto.Hash(crypto.StringToHex(auth.AuthHash)),
// Other fields left at zero values since not used in test vectors
},
Timeslot: jamtime.Timeslot(data.Input.Slot),
Expand Down
39 changes: 15 additions & 24 deletions tests/integration/disputes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ package integration

import (
"crypto/ed25519"
"encoding/hex"
"encoding/json"
"fmt"
"io"
Expand All @@ -22,14 +21,6 @@ import (
"github.com/stretchr/testify/require"
)

func stringToHex(s string) []byte {
if len(s) >= 2 && s[:2] == "0x" {
s = s[2:]
}
bytes, _ := hex.DecodeString(s)
return bytes
}

func ReadJSONFile(filename string) (*JSONData, error) {
file, err := os.Open(filename)
if err != nil {
Expand Down Expand Up @@ -184,13 +175,13 @@ func mapPsi(psi Psi) state.Judgements {
mapHashes := func(hashes []string) []crypto.Hash {
mappedHashes := make([]crypto.Hash, len(hashes))
for i, hash := range hashes {
mappedHashes[i] = crypto.Hash(stringToHex(hash))
mappedHashes[i] = crypto.Hash(crypto.StringToHex(hash))
}
return mappedHashes
}
keys := make([]ed25519.PublicKey, 0)
for _, offender := range psi.Offenders {
keys = append(keys, ed25519.PublicKey(stringToHex(offender)))
keys = append(keys, ed25519.PublicKey(crypto.StringToHex(offender)))
}
return state.Judgements{
BadWorkReports: mapHashes(psi.Bad),
Expand All @@ -202,10 +193,10 @@ func mapPsi(psi Psi) state.Judgements {

func mapKey(kappa ValidatorKey) *crypto.ValidatorKey {
return &crypto.ValidatorKey{
Bandersnatch: crypto.BandersnatchPublicKey(stringToHex(kappa.Bandersnatch)),
Ed25519: ed25519.PublicKey(stringToHex(kappa.Ed25519)),
Bls: crypto.BlsKey(stringToHex(kappa.BLS)),
Metadata: crypto.MetadataKey(stringToHex(kappa.Metadata)),
Bandersnatch: crypto.BandersnatchPublicKey(crypto.StringToHex(kappa.Bandersnatch)),
Ed25519: ed25519.PublicKey(crypto.StringToHex(kappa.Ed25519)),
Bls: crypto.BlsKey(crypto.StringToHex(kappa.BLS)),
Metadata: crypto.MetadataKey(crypto.StringToHex(kappa.Metadata)),
}
}

Expand All @@ -215,7 +206,7 @@ func mapJudgments(judgements []Judgement) [common.ValidatorsSuperMajority]block.
mappedJudgements[i] = block.Judgement{
IsValid: judgement.IsValid,
ValidatorIndex: uint16(judgement.ValidatorIndex),
Signature: crypto.Ed25519Signature(stringToHex(judgement.Signature)),
Signature: crypto.Ed25519Signature(crypto.StringToHex(judgement.Signature)),
}
}
return mappedJudgements
Expand All @@ -225,7 +216,7 @@ func mapVerdicts(verdicts []Verdict) []block.Verdict {
mappedVerdicts := make([]block.Verdict, len(verdicts))
for i, verdict := range verdicts {
mappedVerdicts[i] = block.Verdict{
ReportHash: crypto.Hash(stringToHex(verdict.ReportHash)),
ReportHash: crypto.Hash(crypto.StringToHex(verdict.ReportHash)),
EpochIndex: uint32(verdict.EpochIndex),
Judgements: mapJudgments(verdict.Judgements),
}
Expand All @@ -237,9 +228,9 @@ func mapCulprits(culprits []Culprit) []block.Culprit {
mappedCulprits := make([]block.Culprit, len(culprits))
for i, culprit := range culprits {
mappedCulprits[i] = block.Culprit{
ReportHash: crypto.Hash(stringToHex(culprit.ReportHash)),
ValidatorEd25519PublicKey: ed25519.PublicKey(stringToHex(culprit.ValidatorEd25519PublicKey)),
Signature: crypto.Ed25519Signature(stringToHex(culprit.Signature)),
ReportHash: crypto.Hash(crypto.StringToHex(culprit.ReportHash)),
ValidatorEd25519PublicKey: ed25519.PublicKey(crypto.StringToHex(culprit.ValidatorEd25519PublicKey)),
Signature: crypto.Ed25519Signature(crypto.StringToHex(culprit.Signature)),
}
}
return mappedCulprits
Expand All @@ -249,10 +240,10 @@ func mapFaults(faults []Fault) []block.Fault {
mappedFaults := make([]block.Fault, len(faults))
for i, fault := range faults {
mappedFaults[i] = block.Fault{
ReportHash: crypto.Hash(stringToHex(fault.ReportHash)),
ReportHash: crypto.Hash(crypto.StringToHex(fault.ReportHash)),
IsValid: fault.IsValid,
ValidatorEd25519PublicKey: ed25519.PublicKey(stringToHex(fault.ValidatorEd25519PublicKey)),
Signature: crypto.Ed25519Signature(stringToHex(fault.Signature)),
ValidatorEd25519PublicKey: ed25519.PublicKey(crypto.StringToHex(fault.ValidatorEd25519PublicKey)),
Signature: crypto.Ed25519Signature(crypto.StringToHex(fault.Signature)),
}
}
return mappedFaults
Expand Down Expand Up @@ -322,7 +313,7 @@ func TestDisputes(t *testing.T) {
if len(data.Output.Ok.OffendersMark) > 0 {
offendersMark := make([]ed25519.PublicKey, len(data.Output.Ok.OffendersMark))
for i, offender := range data.Output.Ok.OffendersMark {
offendersMark[i] = ed25519.PublicKey(stringToHex(offender))
offendersMark[i] = ed25519.PublicKey(crypto.StringToHex(offender))
}
require.ElementsMatch(t, offendersMark, newJudgements.OffendingValidators)
}
Expand Down
Loading

0 comments on commit 90662c7

Please sign in to comment.