diff --git a/core/assignment.go b/core/assignment.go index 431428e641..ecabb18771 100644 --- a/core/assignment.go +++ b/core/assignment.go @@ -6,6 +6,7 @@ import ( "fmt" "math" "math/big" + "strings" ) const ( @@ -35,6 +36,21 @@ func (id OperatorID) Hex() string { return hex.EncodeToString(id[:]) } +// The "s" is an operatorId in hex string format, which may or may not have the "0x" prefix. +func OperatorIDFromHex(s string) (OperatorID, error) { + opID := [32]byte{} + s = strings.TrimPrefix(s, "0x") + if len(s) != 64 { + return OperatorID(opID), errors.New("operatorID hex string must be 64 bytes, or 66 bytes if starting with 0x") + } + opIDslice, err := hex.DecodeString(s) + if err != nil { + return OperatorID(opID), err + } + copy(opID[:], opIDslice) + return OperatorID(opID), nil +} + type OperatorIndex = uint type ChunkNumber = uint diff --git a/core/thegraph/state.go b/core/thegraph/state.go index c797e51960..1fba78946d 100644 --- a/core/thegraph/state.go +++ b/core/thegraph/state.go @@ -2,11 +2,9 @@ package thegraph import ( "context" - "encoding/hex" "errors" "fmt" "math" - "strings" "time" "github.com/Layr-Labs/eigenda/core" @@ -271,16 +269,12 @@ func (ics *indexedChainState) getRegisteredIndexedOperatorInfo(ctx context.Conte return nil, err } - id := strings.TrimPrefix(string(operator.Id), "0x") - operatorIdBytes, err := hex.DecodeString(id) + // convert graphql.String to [32]byte + // example: "0x0000000000000000000000000000000000000000000000000000000000000001" -> [32]byte{0x01} + operatorId, err := core.OperatorIDFromHex(string(operator.Id)) if err != nil { return nil, err } - - // convert graphql.String to [32]byte - // example: "0x0000000000000000000000000000000000000000000000000000000000000001" -> [32]byte{0x01} - var operatorId [32]byte - copy(operatorId[:], operatorIdBytes) operators[operatorId] = operatorIndexedInfo } return operators, nil diff --git a/disperser/dataapi/nonsigner_handler.go b/disperser/dataapi/nonsigner_handler.go index 56f434b773..957b858bae 100644 --- a/disperser/dataapi/nonsigner_handler.go +++ b/disperser/dataapi/nonsigner_handler.go @@ -2,7 +2,6 @@ package dataapi import ( "context" - "encoding/hex" "fmt" "math/big" "sort" @@ -92,7 +91,7 @@ func (s *server) getOperatorNonsigningRate(ctx context.Context, startTime, endTi return nil, err } - opID, err := OperatorIDFromString(op) + opID, err := core.OperatorIDFromHex(op) if err != nil { return nil, err } @@ -209,12 +208,11 @@ func getNonSigners(batches []*BatchNonSigningInfo) ([]core.OperatorID, error) { } nonsigners := make([]core.OperatorID, 0) for op := range nonsignerSet { - hexstr := strings.TrimPrefix(op, "0x") - b, err := hex.DecodeString(hexstr) + id, err := core.OperatorIDFromHex(op) if err != nil { return nil, err } - nonsigners = append(nonsigners, core.OperatorID(b)) + nonsigners = append(nonsigners, id) } sort.Slice(nonsigners, func(i, j int) bool { for k := range nonsigners[i] { diff --git a/disperser/dataapi/nonsigner_utils.go b/disperser/dataapi/nonsigner_utils.go index 7bc7713836..0fcf020f0f 100644 --- a/disperser/dataapi/nonsigner_utils.go +++ b/disperser/dataapi/nonsigner_utils.go @@ -1,11 +1,8 @@ package dataapi import ( - "encoding/hex" "fmt" "sort" - - "github.com/Layr-Labs/eigenda/core" ) // NumBatchesAtBlock represents the number of batches at current block. @@ -283,14 +280,3 @@ func getUpperBoundIndex(intervals []*NumBatchesAtBlock, blockNum uint32) int { } return high + 1 } - -func OperatorIDFromString(op string) (core.OperatorID, error) { - opID := [32]byte{} - opIDslice, err := hex.DecodeString(op) - if err != nil { - return opID, err - } - copy(opID[:], opIDslice) - - return opID, nil -} diff --git a/disperser/dataapi/server_test.go b/disperser/dataapi/server_test.go index ad9978b942..eab2777029 100644 --- a/disperser/dataapi/server_test.go +++ b/disperser/dataapi/server_test.go @@ -54,8 +54,8 @@ var ( config = dataapi.Config{ServerMode: "test", SocketAddr: ":8080", AllowOrigins: []string{"*"}, DisperserHostname: "localhost:32007", ChurnerHostname: "localhost:32009"} mockTx = &coremock.MockTransactor{} - opId0, _ = dataapi.OperatorIDFromString("e22dae12a0074f20b8fc96a0489376db34075e545ef60c4845d264a732568311") - opId1, _ = dataapi.OperatorIDFromString("e23cae12a0074f20b8fc96a0489376db34075e545ef60c4845d264b732568312") + opId0, _ = core.OperatorIDFromHex("e22dae12a0074f20b8fc96a0489376db34075e545ef60c4845d264a732568311") + opId1, _ = core.OperatorIDFromHex("e23cae12a0074f20b8fc96a0489376db34075e545ef60c4845d264b732568312") mockChainState, _ = coremock.NewChainDataMock(map[uint8]map[core.OperatorID]int{ 0: { opId0: 1,