Skip to content

Commit

Permalink
Fixed shard id calculation
Browse files Browse the repository at this point in the history
  • Loading branch information
xssnick committed Sep 30, 2024
1 parent 8ac5d4f commit d051d43
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 deletions.
5 changes: 4 additions & 1 deletion tlb/shard.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,5 +197,8 @@ func (s ShardIdent) IsParent(of ShardIdent) bool {
}

func (s ShardIdent) GetShardID() ShardID {
return ShardID(s.ShardPrefix)
if s.PrefixBits > 63 {
return ShardID(0)
}
return ShardID(s.ShardPrefix | 1<<(63-s.PrefixBits))
}
9 changes: 6 additions & 3 deletions tlb/shard_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,7 @@ func TestShardIdent_IsAncestor(t *testing.T) {

func TestShardIdent_GetShardID(t *testing.T) {
type fields struct {
PrefixBits int8
ShardPrefix uint64
}
tests := []struct {
Expand All @@ -303,18 +304,20 @@ func TestShardIdent_GetShardID(t *testing.T) {
{
name: "ok",
fields: fields{
ShardPrefix: 0xE000000000000000,
PrefixBits: 2,
ShardPrefix: 0x4000000000000000,
},
want: 0xE000000000000000,
want: 0x6000000000000000,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
s := ShardIdent{
PrefixBits: tt.fields.PrefixBits,
ShardPrefix: tt.fields.ShardPrefix,
}
if got := s.GetShardID(); got != tt.want {
t.Errorf("GetShardID() = %v, want %v", got, tt.want)
t.Errorf("GetShardID() = %x, want %x", got, tt.want)
}
})
}
Expand Down

0 comments on commit d051d43

Please sign in to comment.