Skip to content

Commit

Permalink
refactor the hex to OperatorID (#480)
Browse files Browse the repository at this point in the history
  • Loading branch information
jianoaix authored Apr 17, 2024
1 parent 9d360bf commit bba4eec
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 30 deletions.
16 changes: 16 additions & 0 deletions core/assignment.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"
"math"
"math/big"
"strings"
)

const (
Expand Down Expand Up @@ -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
Expand Down
12 changes: 3 additions & 9 deletions core/thegraph/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,9 @@ package thegraph

import (
"context"
"encoding/hex"
"errors"
"fmt"
"math"
"strings"
"time"

"github.com/Layr-Labs/eigenda/core"
Expand Down Expand Up @@ -282,16 +280,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
Expand Down
8 changes: 3 additions & 5 deletions disperser/dataapi/nonsigner_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package dataapi

import (
"context"
"encoding/hex"
"fmt"
"math/big"
"sort"
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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] {
Expand Down
14 changes: 0 additions & 14 deletions disperser/dataapi/nonsigner_utils.go
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -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
}
4 changes: 2 additions & 2 deletions disperser/dataapi/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit bba4eec

Please sign in to comment.