Skip to content

Commit

Permalink
Fix linter and remove key optimization
Browse files Browse the repository at this point in the history
Signed-off-by: Antonio Navarro Perez <[email protected]>
  • Loading branch information
ajnavarro committed Mar 7, 2024
1 parent 01ff43f commit 4d2ed7e
Show file tree
Hide file tree
Showing 6 changed files with 14 additions and 34 deletions.
2 changes: 2 additions & 0 deletions client/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ func (c *Client) GetLatestBlockNumber() (uint64, error) {

func (c *Client) GetBlock(blockNum uint64) (*core_types.ResultBlock, error) {
bn := int64(blockNum)

block, err := c.client.Block(&bn)
if err != nil {
return nil, fmt.Errorf("unable to get block, %w", err)
Expand All @@ -49,6 +50,7 @@ func (c *Client) GetBlock(blockNum uint64) (*core_types.ResultBlock, error) {

func (c *Client) GetBlockResults(blockNum uint64) (*core_types.ResultBlockResults, error) {
bn := int64(blockNum)

results, err := c.client.BlockResults(&bn)
if err != nil {
return nil, fmt.Errorf("unable to get block results, %w", err)
Expand Down
2 changes: 1 addition & 1 deletion fetch/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ func getBlocksSequentially(chunkRange chunkRange, client Client) ([]*types.Block

for blockNum := chunkRange.from; blockNum <= chunkRange.to; blockNum++ {
// Get block info from the chain
block, err := client.GetBlock(uint64(blockNum))
block, err := client.GetBlock(blockNum)
if err != nil {
errs = append(errs, fmt.Errorf("unable to get block %d, %w", blockNum, err))

Expand Down
6 changes: 5 additions & 1 deletion serve/graph/resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,11 @@ func dereferenceTime(i *time.Time) time.Time {
return *i
}

func handleChannel[T any](ctx context.Context, m *events.Manager, writeToChannel func(*types.NewBlock, chan<- T)) <-chan T {
func handleChannel[T any](
ctx context.Context,
m *events.Manager,
writeToChannel func(*types.NewBlock, chan<- T),
) <-chan T {
ch := make(chan T)
go func() {
defer close(ch)
Expand Down
3 changes: 1 addition & 2 deletions serve/graph/schema.resolvers.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

29 changes: 2 additions & 27 deletions storage/encode.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,6 @@ func encodeUint32Ascending(b []byte, v uint32) []byte {
return append(b, byte(v>>24), byte(v>>16), byte(v>>8), byte(v))
}

// encodeUint32Descending encodes the uint32 value so that it sorts in
// reverse order, from largest to smallest.
func encodeUint32Descending(b []byte, v uint32) []byte {
return encodeUint32Ascending(b, ^v)
}

// decodeUint32Ascending decodes a uint32 from the input buffer, treating
// the input as a big-endian 4 byte uint32 representation. The remainder
// of the input buffer and the decoded uint32 are returned.
Expand All @@ -58,14 +52,6 @@ func decodeUint32Ascending(b []byte) ([]byte, uint32, error) {
return b[4:], v, nil
}

// decodeUint32Descending decodes a uint32 value which was encoded
// using EncodeUint32Descending.
func decodeUint32Descending(b []byte) ([]byte, uint32, error) {
leftover, v, err := decodeUint32Ascending(b)

return leftover, ^v, err
}

// encodeUint64Ascending encodes the uint64 value using a big-endian 8 byte
// representation. The bytes are appended to the supplied buffer and
// the final buffer is returned.
Expand All @@ -75,28 +61,17 @@ func encodeUint64Ascending(b []byte, v uint64) []byte {
byte(v>>24), byte(v>>16), byte(v>>8), byte(v))
}

// encodeUint64Descending encodes the uint64 value so that it sorts in
// reverse order, from largest to smallest.
func encodeUint64Descending(b []byte, v uint64) []byte {
return encodeUint64Ascending(b, ^v)
}

// decodeUint64Ascending decodes a uint64 from the input buffer, treating
// the input as a big-endian 8 byte uint64 representation. The remainder
// of the input buffer and the decoded uint64 are returned.
func decodeUint64Ascending(b []byte) ([]byte, uint64, error) {
if len(b) < 8 {
return nil, 0, errors.Errorf("insufficient bytes to decode uint64 int value")
}

v := binary.BigEndian.Uint64(b)
return b[8:], v, nil
}

// decodeUint64Descending decodes a uint64 value which was encoded
// using EncodeUint64Descending.
func decodeUint64Descending(b []byte) ([]byte, uint64, error) {
leftover, v, err := decodeUint64Ascending(b)
return leftover, ^v, err
return b[8:], v, nil
}

// dncodeStringAscending encodes the string value using an escape-based encoding. See
Expand Down
6 changes: 3 additions & 3 deletions storage/pebble.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ const (
)

func keyTx(blockNum uint64, txIndex uint32) []byte {
key := make([]byte, len(prefixKeyTxs)+8+4)
var key []byte
key = encodeStringAscending(key, prefixKeyTxs)
key = encodeUint64Ascending(key, blockNum)
key = encodeUint32Ascending(key, txIndex)
Expand All @@ -34,7 +34,7 @@ func keyTx(blockNum uint64, txIndex uint32) []byte {
}

func keyBlock(blockNum uint64) []byte {
key := make([]byte, len(prefixKeyBlocks)+8)
var key []byte
key = encodeStringAscending(key, prefixKeyBlocks)
key = encodeUint64Ascending(key, blockNum)

Expand Down Expand Up @@ -99,7 +99,7 @@ func (s *Pebble) GetBlock(blockNum uint64) (*types.Block, error) {

// GetTx fetches the specified tx result from storage, if any
func (s *Pebble) GetTx(blockNum uint64, index uint32) (*types.TxResult, error) {
tx, c, err := s.db.Get(keyTx(uint64(blockNum), index))
tx, c, err := s.db.Get(keyTx(blockNum, index))
if errors.Is(err, pebble.ErrNotFound) {
return nil, storageErrors.ErrNotFound
}
Expand Down

0 comments on commit 4d2ed7e

Please sign in to comment.