Skip to content

Commit

Permalink
Merge pull request #6 from celenium-io/bit-mask-big-int
Browse files Browse the repository at this point in the history
Refactoring: bit mask as big int
  • Loading branch information
vvuwei authored Oct 30, 2023
2 parents ba3894e + ba135d8 commit c2733b1
Show file tree
Hide file tree
Showing 26 changed files with 762 additions and 652 deletions.
7 changes: 2 additions & 5 deletions cmd/api/docs/docs.go

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

3 changes: 1 addition & 2 deletions cmd/api/docs/swagger.json

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

3 changes: 1 addition & 2 deletions cmd/api/docs/swagger.yaml

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

11 changes: 6 additions & 5 deletions cmd/api/handler/responses/block_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@
package responses

import (
"reflect"
"testing"
"time"

"github.com/celenium-io/celestia-indexer/internal/storage"
storageTypes "github.com/celenium-io/celestia-indexer/internal/storage/types"
"github.com/shopspring/decimal"
"github.com/stretchr/testify/assert"
"reflect"
"testing"
"time"
)

func TestBlock_SearchType(t *testing.T) {
Expand Down Expand Up @@ -39,7 +40,7 @@ func TestNewBlock(t *testing.T) {
VersionBlock: 10,
VersionApp: 11,
MessageTypes: storageTypes.MsgTypeBits{
Bits: 1,
Bits: storageTypes.NewBits(1),
},
Hash: []byte{0x01},
ParentHash: []byte{0x02},
Expand Down Expand Up @@ -88,7 +89,7 @@ func TestNewBlock(t *testing.T) {
VersionBlock: 10,
VersionApp: 11,
MessageTypes: storageTypes.MsgTypeBits{
Bits: 1,
Bits: storageTypes.NewBits(1),
},
Hash: []byte{0x01},
ParentHash: []byte{0x02},
Expand Down
2 changes: 1 addition & 1 deletion cmd/api/handler/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ func (handler *TxHandler) List(c echo.Context) error {
fltrs.TimeTo = time.Unix(req.To, 0).UTC()
}
for i := range req.MsgType {
fltrs.MessageTypes.SetBit(storageTypes.MsgType(req.MsgType[i]))
fltrs.MessageTypes.SetByMsgType(storageTypes.MsgType(req.MsgType[i]))
}

txs, err := handler.tx.Filter(c.Request().Context(), fltrs)
Expand Down
1 change: 1 addition & 0 deletions cmd/api/handler/tx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ var (
Status: types.StatusSuccess,
Codespace: "sdk",
Memo: "memo",
MessageTypes: types.NewMsgTypeBitMask(types.MsgSend),
Messages: []storage.Message{
{
Id: 1,
Expand Down
4 changes: 3 additions & 1 deletion cmd/api/handler/websocket/channel_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/celenium-io/celestia-indexer/cmd/api/handler/responses"
"github.com/celenium-io/celestia-indexer/internal/storage"
"github.com/celenium-io/celestia-indexer/internal/storage/mock"
storageTypes "github.com/celenium-io/celestia-indexer/internal/storage/types"
"github.com/celenium-io/celestia-indexer/pkg/types"
"github.com/gorilla/websocket"
"github.com/labstack/echo/v4"
Expand Down Expand Up @@ -108,7 +109,8 @@ func BenchmarkProcessingMessage(b *testing.B) {
blockRepo.EXPECT().
GetByID(gomock.Any(), height).
Return(&storage.Block{
Height: types.Level(height),
Height: types.Level(height),
MessageTypes: storageTypes.NewMsgTypeBits(),
}, nil).
MaxTimes(1)

Expand Down
2 changes: 1 addition & 1 deletion cmd/api/handler/websocket/filters.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ func (f *txFilters) Fill(msg TransactionFilters) error {
}

for i := range msg.Messages {
f.msgs.SetBit(types.MsgType(msg.Messages[i]))
f.msgs.SetByMsgType(types.MsgType(msg.Messages[i]))
}

return nil
Expand Down
10 changes: 6 additions & 4 deletions cmd/api/handler/ws_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
ws "github.com/celenium-io/celestia-indexer/cmd/api/handler/websocket"
"github.com/celenium-io/celestia-indexer/internal/storage"
"github.com/celenium-io/celestia-indexer/internal/storage/mock"
storageTypes "github.com/celenium-io/celestia-indexer/internal/storage/types"
"github.com/goccy/go-json"
"github.com/gorilla/websocket"
"github.com/labstack/echo/v4"
Expand Down Expand Up @@ -58,10 +59,11 @@ func TestWebsocket(t *testing.T) {
require.NoError(t, err)

blockMock.EXPECT().ByIdWithRelations(ctx, uint64(i)).Return(storage.Block{
Id: uint64(i),
Height: types.Level(i),
Time: time.Now(),
Hash: hash,
Id: uint64(i),
Height: types.Level(i),
Time: time.Now(),
Hash: hash,
MessageTypes: storageTypes.NewMsgTypeBits(),
}, nil).MaxTimes(1)
}

Expand Down
7 changes: 3 additions & 4 deletions cmd/api/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,9 @@ var rootCmd = &cobra.Command{
}

// @title Swagger Celestia Indexer API
// @version 1.0
// @description This is docs of Celestia indexer API.
// @host https://api.celestia.dipdup.net
// @BasePath /v1
// @version 1.0
// @description This is docs of Celestia indexer API.
// @host api.celestia.dipdup.net
//
// @query.collection.format multi
func main() {
Expand Down
2 changes: 1 addition & 1 deletion internal/storage/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ type Block struct {
VersionBlock uint64 `bun:"version_block" comment:"Block version"`
VersionApp uint64 `bun:"version_app" comment:"App version"`

MessageTypes types.MsgTypeBits `bun:"message_types,type:int8" comment:"Bit mask with containing messages"`
MessageTypes types.MsgTypeBits `bun:"message_types,type:bit(73)" comment:"Bit mask with containing messages"`

Hash pkgTypes.Hex `bun:"hash" comment:"Block hash"`
ParentHash pkgTypes.Hex `bun:"parent_hash" comment:"Hash of parent block"`
Expand Down
2 changes: 1 addition & 1 deletion internal/storage/postgres/scopes.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func txFilter(query *bun.SelectQuery, fltrs storage.TxFilter) *bun.SelectQuery {
query = sortScope(query, "id", fltrs.Sort)

if !fltrs.MessageTypes.Empty() {
query = query.Where("message_types & ? > 0", fltrs.MessageTypes)
query = query.Where("bit_count(message_types & ?::bit(73)) > 0", fltrs.MessageTypes)
}

if len(fltrs.Status) > 0 {
Expand Down
10 changes: 5 additions & 5 deletions internal/storage/postgres/storage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -500,7 +500,7 @@ func (s *StorageTestSuite) TestTxFilterSuccessUnjailAsc() {
s.Require().EqualValues(77483, tx.GasUsed)
s.Require().EqualValues(1, tx.EventsCount)
s.Require().EqualValues(1, tx.MessagesCount)
s.Require().EqualValues(2048, tx.MessageTypes.Bits)
s.Require().EqualValues("2048", tx.MessageTypes.Bits.String())
s.Require().Equal(types.StatusSuccess, tx.Status)
s.Require().Equal("memo2", tx.Memo)
s.Require().Equal("", tx.Codespace)
Expand Down Expand Up @@ -530,7 +530,7 @@ func (s *StorageTestSuite) TestTxFilterSuccessDesc() {
s.Require().EqualValues(77483, tx.GasUsed)
s.Require().EqualValues(0, tx.EventsCount)
s.Require().EqualValues(1, tx.MessagesCount)
s.Require().EqualValues(32, tx.MessageTypes.Bits)
s.Require().EqualValues("32", tx.MessageTypes.Bits.String())
s.Require().Equal(types.StatusSuccess, tx.Status)
s.Require().Equal("", tx.Memo)
s.Require().Equal("", tx.Codespace)
Expand Down Expand Up @@ -561,7 +561,7 @@ func (s *StorageTestSuite) TestTxFilterHeight() {
s.Require().EqualValues(77483, tx.GasUsed)
s.Require().EqualValues(1, tx.EventsCount)
s.Require().EqualValues(1, tx.MessagesCount)
s.Require().EqualValues(2048, tx.MessageTypes.Bits)
s.Require().EqualValues("2048", tx.MessageTypes.Bits.String())
s.Require().Equal(types.StatusSuccess, tx.Status)
s.Require().Equal("memo2", tx.Memo)
s.Require().Equal("", tx.Codespace)
Expand Down Expand Up @@ -632,7 +632,7 @@ func (s *StorageTestSuite) TestTxByIdWithRelations() {
s.Require().Equal("memo2", tx.Memo)
s.Require().Equal("", tx.Codespace)
s.Require().Equal("80410", tx.Fee.String())
s.Require().EqualValues(2048, tx.MessageTypes.Bits)
s.Require().EqualValues("2048", tx.MessageTypes.Bits.String())

s.Require().Len(tx.Messages, 2)
}
Expand All @@ -658,7 +658,7 @@ func (s *StorageTestSuite) TestTxGenesis() {
s.Require().Equal("[email protected]:26656", tx.Memo)
s.Require().Equal("", tx.Codespace)
s.Require().Equal("0", tx.Fee.String())
s.Require().EqualValues(32, tx.MessageTypes.Bits)
s.Require().EqualValues("32", tx.MessageTypes.Bits.String())
}

func (s *StorageTestSuite) TestTxByAddressAndTime() {
Expand Down
10 changes: 5 additions & 5 deletions internal/storage/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,11 @@ type Tx struct {
Fee decimal.Decimal `bun:"fee,type:numeric" comment:"Paid fee" stats:"func:min max sum avg"`
Status types.Status `bun:"status,type:status" comment:"Transaction status" stats:"filterable"`

Error string `bun:"error,type:text" comment:"Error string if failed"`
Codespace string `bun:"codespace,type:text" comment:"Codespace" stats:"filterable"`
Hash []byte `bun:"hash" comment:"Transaction hash"`
Memo string `bun:"memo,type:text" comment:"Note or comment to send with the transaction"`
MessageTypes types.MsgTypeBits `bun:"message_types,type:int8" comment:"Bit mask with containing messages" stats:"filterable"`
Error string `bun:"error,type:text" comment:"Error string if failed"`
Codespace string `bun:"codespace,type:text" comment:"Codespace" stats:"filterable"`
Hash []byte `bun:"hash" comment:"Transaction hash"`
Memo string `bun:"memo,type:text" comment:"Note or comment to send with the transaction"`
MessageTypes types.MsgTypeBits `bun:"message_types,type:bit(73)" comment:"Bit mask with containing messages" stats:"filterable"`

Messages []Message `bun:"rel:has-many,join:id=tx_id"`
Events []Event `bun:"rel:has-many"`
Expand Down
74 changes: 66 additions & 8 deletions internal/storage/types/bitmask.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,75 @@

package types

type Bits uint64
import (
"math/big"
"math/bits"

func (b *Bits) Set(flag Bits) { *b |= flag }
func (b *Bits) Clear(flag Bits) { *b &^= flag }
func (b Bits) Has(flag Bits) bool { return b&flag != 0 }
"github.com/pkg/errors"
)

var (
zero = big.NewInt(0)
)

type Bits struct {
value *big.Int
}

func NewEmptyBits() Bits {
return Bits{
big.NewInt(0),
}
}

func NewBits(value int) Bits {
return Bits{
big.NewInt(int64(value)),
}
}

func NewBitsWithPosition(position int) Bits {
b := big.NewInt(0)
b = b.SetBit(b, position, 1)
return Bits{
b,
}
}

func NewBitsFromString(value string) (Bits, error) {
b := big.NewInt(0)
b, ok := b.SetString(value, 2)
if !ok {
return Bits{}, errors.Errorf("invalid mask value: %s", value)
}
return Bits{b}, nil
}

func (b *Bits) Set(flag Bits) { b.value = b.value.Or(b.value, flag.value) }
func (b *Bits) SetBit(position int) {
b.value = b.value.SetBit(b.value, position, 1)
}

func (b *Bits) Clear(flag Bits) { b.value = b.value.AndNot(b.value, flag.value) }
func (b Bits) Has(flag Bits) bool {
and := b.value.And(b.value, flag.value)
return and.Cmp(zero) != 0
}
func (b Bits) HasBit(position int) bool {
return b.value.Bit(position) != 0
}
func (b Bits) CountBits() int {
var count int
for b != 0 {
count += int(b & 1)
b >>= 1
for _, x := range b.value.Bits() {
count += bits.OnesCount(uint(x))
}
return count
}
func (b Bits) Empty() bool { return b == 0 }
func (b Bits) Empty() bool { return b.value == nil || b.value.Cmp(zero) == 0 }

func (b Bits) String() string {
if b.value == nil {
return "nil"
}
return b.value.String()
}
Loading

0 comments on commit c2733b1

Please sign in to comment.