Skip to content

[staking] reduce candidates bucket indexer storage #4622

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions action/protocol/staking/read_state.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ import (
"github.com/iotexproject/iotex-core/v2/state"
)

func ToIoTeXTypesVoteBucketList(sr protocol.StateReader, buckets []*VoteBucket) (*iotextypes.VoteBucketList, error) {
return toIoTeXTypesVoteBucketList(sr, buckets)
}

func toIoTeXTypesVoteBucketList(sr protocol.StateReader, buckets []*VoteBucket) (*iotextypes.VoteBucketList, error) {
esr := NewEndorsementStateReader(sr)
res := iotextypes.VoteBucketList{
Expand Down Expand Up @@ -98,6 +102,10 @@ func toIoTeXTypesCandidateV2(csr CandidateStateReader, cand *Candidate, featureC
return c, nil
}

func ToIoTeXTypesCandidateListV2(csr CandidateStateReader, candidates CandidateList, featureCtx protocol.FeatureCtx) (*iotextypes.CandidateListV2, error) {
return toIoTeXTypesCandidateListV2(csr, candidates, featureCtx)
}

func toIoTeXTypesCandidateListV2(csr CandidateStateReader, candidates CandidateList, featureCtx protocol.FeatureCtx) (*iotextypes.CandidateListV2, error) {
res := iotextypes.CandidateListV2{
Candidates: make([]*iotextypes.CandidateV2, 0, len(candidates)),
Expand Down
71 changes: 71 additions & 0 deletions blockindex/nativestaking/bucket_list.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
// Copyright (c) 2025 IoTeX Foundation
// This source code is provided 'as is' and no warranties are given as to title or non-infringement, merchantability
// or fitness for purpose and, to the extent permitted by law, all liability for your use of the code is disclaimed.
// This source code is governed by Apache License 2.0 that can be found in the LICENSE file.

package stakingindex

import (
"google.golang.org/protobuf/proto"

"github.com/iotexproject/iotex-core/v2/blockindex/nativestaking/stakingindexpb"
)

type bucketList struct {
maxBucket uint64
deleted []uint64
}

func (bl *bucketList) serialize() ([]byte, error) {
return proto.Marshal(bl.toProto())
}

func (bl *bucketList) toProto() *stakingindexpb.BucketList {
return &stakingindexpb.BucketList{
MaxBucket: bl.maxBucket,
Deleted: bl.deleted,
}
}

func fromProtoBucketList(pb *stakingindexpb.BucketList) *bucketList {
return &bucketList{
maxBucket: pb.MaxBucket,
deleted: pb.Deleted,
}
}

func deserializeBucketList(buf []byte) (*bucketList, error) {
pb := stakingindexpb.BucketList{}
if err := proto.Unmarshal(buf, &pb); err != nil {
return nil, err
}
return fromProtoBucketList(&pb), nil
}

type candList struct {
id [][]byte
}

func (cl *candList) serialize() ([]byte, error) {
return proto.Marshal(cl.toProto())
}

func (cl *candList) toProto() *stakingindexpb.CandList {
return &stakingindexpb.CandList{
Id: cl.id,
}
}

func fromProtoCandList(pb *stakingindexpb.CandList) *candList {
return &candList{
id: pb.Id,
}
}

func deserializeCandList(buf []byte) (*candList, error) {
pb := stakingindexpb.CandList{}
if err := proto.Unmarshal(buf, &pb); err != nil {
return nil, err
}
return fromProtoCandList(&pb), nil
}
65 changes: 65 additions & 0 deletions blockindex/nativestaking/bucket_list_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
// Copyright (c) 2025 IoTeX Foundation
// This source code is provided 'as is' and no warranties are given as to title or non-infringement, merchantability
// or fitness for purpose and, to the extent permitted by law, all liability for your use of the code is disclaimed.
// This source code is governed by Apache License 2.0 that can be found in the LICENSE file.

package stakingindex

import (
ra "crypto/rand"
"math/rand/v2"
"testing"

"github.com/stretchr/testify/require"

. "github.com/iotexproject/iotex-core/v2/pkg/util/assertions"
)

func TestBucketList(t *testing.T) {
req := require.New(t)
l0 := bucketList{0, nil}
b := MustNoErrorV(l0.serialize())
l1 := MustNoErrorV(deserializeBucketList(b))
req.Equal(&l0, l1)
}

func TestBucketListSize(t *testing.T) {
req := require.New(t)
d, check := []uint64{}, []uint64{}
for i := range 50001 {
d = append(d, rand.Uint64N(50001))
if i%5000 == 0 {
check = append(check, d[i])
}
}
l0 := bucketList{d[1234], d}
b := MustNoErrorV(l0.serialize())
l1 := MustNoErrorV(deserializeBucketList(b))
req.Equal(&l0, l1)
req.Equal(d[1234], l1.maxBucket)
for i := 0; i < 50001; i += 5000 {
req.Equal(check[i/5000], l1.deleted[i])
}
}

func TestCandList(t *testing.T) {
req := require.New(t)
var (
check [][]byte
l0 = candList{}
)
for range 10 {
var b [20]byte
n := MustNoErrorV(ra.Read(b[:]))
req.Equal(20, n)
l0.id = append(l0.id, b[:])
check = append(check, b[:])
}
b := MustNoErrorV(l0.serialize())
l1 := MustNoErrorV(deserializeCandList(b))
req.Equal(10, len(l1.id))
req.Equal(&l0, l1)
for i := range 10 {
req.Equal(check[i], l1.id[i])
}
}
Loading
Loading