From 84195c149f3f60b17358b43faf5e2b8c4a31a9b3 Mon Sep 17 00:00:00 2001 From: Jian Xiao Date: Fri, 12 Apr 2024 23:39:01 +0000 Subject: [PATCH 1/3] refactor the hex to OperatorID --- core/assignment.go | 20 ++++++++++++++++++++ core/thegraph/state.go | 12 +++--------- disperser/dataapi/nonsigner_handler.go | 8 +++----- disperser/dataapi/nonsigner_utils.go | 14 -------------- disperser/dataapi/server_test.go | 4 ++-- 5 files changed, 28 insertions(+), 30 deletions(-) diff --git a/core/assignment.go b/core/assignment.go index 431428e641..621b7d272f 100644 --- a/core/assignment.go +++ b/core/assignment.go @@ -35,6 +35,26 @@ 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{} + if len(s) != 64 || len(s) != 66 { + return OperatorID(opID), errors.New("operatorID hex string must be 64 or 66 bytes") + } + if len(s) == 34 { + if s[:2] != "0x" { + return OperatorID(opID), errors.New("operatorID hex string with 66 bytes must start with 0x") + } + s = s[2:] + } + 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, From dba93bc739c85ce5a8ce59d23abcdb3ee70255ca Mon Sep 17 00:00:00 2001 From: Jian Xiao Date: Sat, 13 Apr 2024 00:05:14 +0000 Subject: [PATCH 2/3] fix --- core/assignment.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/core/assignment.go b/core/assignment.go index 621b7d272f..a2c83b4d74 100644 --- a/core/assignment.go +++ b/core/assignment.go @@ -38,10 +38,10 @@ func (id OperatorID) Hex() string { // 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{} - if len(s) != 64 || len(s) != 66 { + if len(s) != 64 && len(s) != 66 { return OperatorID(opID), errors.New("operatorID hex string must be 64 or 66 bytes") } - if len(s) == 34 { + if len(s) == 66 { if s[:2] != "0x" { return OperatorID(opID), errors.New("operatorID hex string with 66 bytes must start with 0x") } From 6d4dd6f84bd1bcb39fe86c5896491aa718894218 Mon Sep 17 00:00:00 2001 From: Jian Xiao Date: Wed, 17 Apr 2024 21:59:00 +0000 Subject: [PATCH 3/3] fix --- core/assignment.go | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/core/assignment.go b/core/assignment.go index a2c83b4d74..ecabb18771 100644 --- a/core/assignment.go +++ b/core/assignment.go @@ -6,6 +6,7 @@ import ( "fmt" "math" "math/big" + "strings" ) const ( @@ -38,14 +39,9 @@ func (id OperatorID) Hex() string { // 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{} - if len(s) != 64 && len(s) != 66 { - return OperatorID(opID), errors.New("operatorID hex string must be 64 or 66 bytes") - } - if len(s) == 66 { - if s[:2] != "0x" { - return OperatorID(opID), errors.New("operatorID hex string with 66 bytes must start with 0x") - } - s = s[2:] + 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 {