Skip to content

Commit

Permalink
feature(store): add LastBlockTime set/get functions (#24)
Browse files Browse the repository at this point in the history
Refactor redis querying code to make it common among block funcs.
Tests for LastBlockTime and Block-related funcs are passing.
  • Loading branch information
gsora authored Feb 8, 2022
1 parent 0f5a3f5 commit fd5ee51
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 2 deletions.
32 changes: 30 additions & 2 deletions store/blocks.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ var ErrBlockNotFound = fmt.Errorf("block not found")

const (
blockFmt = "block/%d"
blockTimeFmt = "blockTime/%d"
defaultTimeout = 100 * 10 * time.Second // we keep the last 100 blocks, assuming block time of 10 seconds
)

Expand All @@ -27,8 +28,12 @@ func blockKey(height int64) string {
return fmt.Sprintf(blockFmt, height)
}

func (b *Blocks) Block(height int64) ([]byte, error) {
res, err := b.storeInstance.Client.Get(context.Background(), blockKey(height)).Result()
func blockTimeKey(height int64) string {
return fmt.Sprintf(blockTimeFmt, height)
}

func (b *Blocks) queryRedis(key string) ([]byte, error) {
res, err := b.storeInstance.Client.Get(context.Background(), key).Result()
if err != nil {
if err == redis.Nil {
return nil, ErrBlockNotFound
Expand All @@ -40,6 +45,29 @@ func (b *Blocks) Block(height int64) ([]byte, error) {
return []byte(res), nil
}

func (b *Blocks) Block(height int64) ([]byte, error) {
return b.queryRedis(blockKey(height))
}

func (b *Blocks) SetLastBlockTime(t time.Time, height int64) error {
// TODO: figure out how to get block time from block
mt, err := t.MarshalText()
if err != nil {
return err
}
return b.storeInstance.Client.Set(context.Background(), blockTimeKey(height), mt, defaultTimeout).Err()
}

func (b *Blocks) LastBlockTime(height int64) (time.Time, error) {
res, err := b.queryRedis(blockTimeKey(height))
if err != nil {
return time.Time{}, err
}

tt := time.Time{}
return tt, tt.UnmarshalText(res)
}

func (b *Blocks) Add(data []byte, height int64) error {
return b.storeInstance.Client.Set(context.Background(), blockKey(height), string(data), defaultTimeout).Err()
}
15 changes: 15 additions & 0 deletions store/store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package store
import (
"os"
"testing"
"time"

"github.com/alicebob/miniredis/v2"
sdk "github.com/cosmos/cosmos-sdk/types"
Expand Down Expand Up @@ -263,3 +264,17 @@ func TestBlocks(t *testing.T) {
require.NoError(t, err)
require.Equal(t, data, res)
}

func TestBlockTime(t *testing.T) {
defer ResetTestStore(mr, store)
blocks := NewBlocks(store)
_, err := blocks.LastBlockTime(123)
require.Error(t, err)
require.ErrorIs(t, err, ErrBlockNotFound)

tn := time.Now()
require.NoError(t, blocks.SetLastBlockTime(tn, 123))
res, err := blocks.LastBlockTime(123)
require.NoError(t, err)
require.True(t, tn.Equal(res))
}

0 comments on commit fd5ee51

Please sign in to comment.