Skip to content

Commit f4e98a1

Browse files
committed
[staking] reduce candidates bucket indexer storage
1 parent b77b662 commit f4e98a1

File tree

9 files changed

+877
-0
lines changed

9 files changed

+877
-0
lines changed

action/protocol/staking/read_state.go

+10
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@ import (
1616
"github.com/iotexproject/iotex-core/v2/state"
1717
)
1818

19+
func ToIoTeXTypesVoteBucketList(sr protocol.StateReader, buckets []*VoteBucket) (*iotextypes.VoteBucketList, error) {
20+
// TODO: change toIoTeXTypesVoteBucketList() to this name
21+
return toIoTeXTypesVoteBucketList(sr, buckets)
22+
}
23+
1924
func toIoTeXTypesVoteBucketList(sr protocol.StateReader, buckets []*VoteBucket) (*iotextypes.VoteBucketList, error) {
2025
esr := NewEndorsementStateReader(sr)
2126
res := iotextypes.VoteBucketList{
@@ -98,6 +103,11 @@ func toIoTeXTypesCandidateV2(csr CandidateStateReader, cand *Candidate, featureC
98103
return c, nil
99104
}
100105

106+
func ToIoTeXTypesCandidateListV2(csr CandidateStateReader, candidates CandidateList, featureCtx protocol.FeatureCtx) (*iotextypes.CandidateListV2, error) {
107+
// TODO: change toIoTeXTypesCandidateListV2 to this name
108+
return &iotextypes.CandidateListV2{}, nil
109+
}
110+
101111
func toIoTeXTypesCandidateListV2(csr CandidateStateReader, candidates CandidateList, featureCtx protocol.FeatureCtx) (*iotextypes.CandidateListV2, error) {
102112
res := iotextypes.CandidateListV2{
103113
Candidates: make([]*iotextypes.CandidateV2, 0, len(candidates)),
+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// Copyright (c) 2025 IoTeX Foundation
2+
// This source code is provided 'as is' and no warranties are given as to title or non-infringement, merchantability
3+
// or fitness for purpose and, to the extent permitted by law, all liability for your use of the code is disclaimed.
4+
// This source code is governed by Apache License 2.0 that can be found in the LICENSE file.
5+
6+
package stakingindex
7+
8+
import (
9+
"google.golang.org/protobuf/proto"
10+
11+
"github.com/iotexproject/iotex-core/v2/blockindex/nativestaking/stakingindexpb"
12+
)
13+
14+
type bucketList struct {
15+
maxBucket uint64
16+
deleted []uint64
17+
}
18+
19+
func (bl *bucketList) serialize() ([]byte, error) {
20+
return proto.Marshal(bl.toProto())
21+
}
22+
23+
func (bl *bucketList) toProto() *stakingindexpb.BucketList {
24+
return &stakingindexpb.BucketList{
25+
MaxBucket: bl.maxBucket,
26+
Deleted: bl.deleted,
27+
}
28+
}
29+
30+
func fromProtoBucketList(pb *stakingindexpb.BucketList) *bucketList {
31+
return &bucketList{
32+
maxBucket: pb.MaxBucket,
33+
deleted: pb.Deleted,
34+
}
35+
}
36+
37+
func deserializeBucketList(buf []byte) (*bucketList, error) {
38+
pb := stakingindexpb.BucketList{}
39+
if err := proto.Unmarshal(buf, &pb); err != nil {
40+
return nil, err
41+
}
42+
return fromProtoBucketList(&pb), nil
43+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// Copyright (c) 2025 IoTeX Foundation
2+
// This source code is provided 'as is' and no warranties are given as to title or non-infringement, merchantability
3+
// or fitness for purpose and, to the extent permitted by law, all liability for your use of the code is disclaimed.
4+
// This source code is governed by Apache License 2.0 that can be found in the LICENSE file.
5+
6+
package stakingindex
7+
8+
import (
9+
"math/rand/v2"
10+
"testing"
11+
12+
"github.com/stretchr/testify/require"
13+
14+
. "github.com/iotexproject/iotex-core/v2/pkg/util/assertions"
15+
)
16+
17+
func TestBucketList(t *testing.T) {
18+
req := require.New(t)
19+
l0 := bucketList{0, nil}
20+
b := MustNoErrorV(l0.serialize())
21+
l1 := MustNoErrorV(deserializeBucketList(b))
22+
req.Equal(&l0, l1)
23+
}
24+
25+
func TestBucketListSize(t *testing.T) {
26+
req := require.New(t)
27+
d, check := []uint64{}, []uint64{}
28+
for i := range 50001 {
29+
d = append(d, rand.Uint64N(50001))
30+
if i%5000 == 0 {
31+
check = append(check, d[i])
32+
}
33+
}
34+
l0 := bucketList{d[1234], d}
35+
b := MustNoErrorV(l0.serialize())
36+
l1 := MustNoErrorV(deserializeBucketList(b))
37+
req.Equal(&l0, l1)
38+
req.Equal(d[1234], l1.maxBucket)
39+
for i := 0; i < 50001; i += 5000 {
40+
req.Equal(check[i/5000], l1.deleted[i])
41+
}
42+
}

0 commit comments

Comments
 (0)