Skip to content

Commit b08ebf7

Browse files
committed
[staking] reduce candidates bucket indexer storage
1 parent 3326b58 commit b08ebf7

File tree

9 files changed

+971
-0
lines changed

9 files changed

+971
-0
lines changed

action/protocol/staking/read_state.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ import (
1616
"github.com/iotexproject/iotex-core/v2/state"
1717
)
1818

19+
func ToIoTeXTypesVoteBucketList(sr protocol.StateReader, buckets []*VoteBucket) (*iotextypes.VoteBucketList, error) {
20+
return toIoTeXTypesVoteBucketList(sr, buckets)
21+
}
22+
1923
func toIoTeXTypesVoteBucketList(sr protocol.StateReader, buckets []*VoteBucket) (*iotextypes.VoteBucketList, error) {
2024
esr := NewEndorsementStateReader(sr)
2125
res := iotextypes.VoteBucketList{
@@ -98,6 +102,10 @@ func toIoTeXTypesCandidateV2(csr CandidateStateReader, cand *Candidate, featureC
98102
return c, nil
99103
}
100104

105+
func ToIoTeXTypesCandidateListV2(csr CandidateStateReader, candidates CandidateList, featureCtx protocol.FeatureCtx) (*iotextypes.CandidateListV2, error) {
106+
return toIoTeXTypesCandidateListV2(csr, candidates, featureCtx)
107+
}
108+
101109
func toIoTeXTypesCandidateListV2(csr CandidateStateReader, candidates CandidateList, featureCtx protocol.FeatureCtx) (*iotextypes.CandidateListV2, error) {
102110
res := iotextypes.CandidateListV2{
103111
Candidates: make([]*iotextypes.CandidateV2, 0, len(candidates)),
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
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+
}
44+
45+
type candList struct {
46+
id [][]byte
47+
}
48+
49+
func (cl *candList) serialize() ([]byte, error) {
50+
return proto.Marshal(cl.toProto())
51+
}
52+
53+
func (cl *candList) toProto() *stakingindexpb.CandList {
54+
return &stakingindexpb.CandList{
55+
Id: cl.id,
56+
}
57+
}
58+
59+
func fromProtoCandList(pb *stakingindexpb.CandList) *candList {
60+
return &candList{
61+
id: pb.Id,
62+
}
63+
}
64+
65+
func deserializeCandList(buf []byte) (*candList, error) {
66+
pb := stakingindexpb.CandList{}
67+
if err := proto.Unmarshal(buf, &pb); err != nil {
68+
return nil, err
69+
}
70+
return fromProtoCandList(&pb), nil
71+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
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+
ra "crypto/rand"
10+
"math/rand/v2"
11+
"testing"
12+
13+
"github.com/stretchr/testify/require"
14+
15+
. "github.com/iotexproject/iotex-core/v2/pkg/util/assertions"
16+
)
17+
18+
func TestBucketList(t *testing.T) {
19+
req := require.New(t)
20+
l0 := bucketList{0, nil}
21+
b := MustNoErrorV(l0.serialize())
22+
l1 := MustNoErrorV(deserializeBucketList(b))
23+
req.Equal(&l0, l1)
24+
}
25+
26+
func TestBucketListSize(t *testing.T) {
27+
req := require.New(t)
28+
d, check := []uint64{}, []uint64{}
29+
for i := range 50001 {
30+
d = append(d, rand.Uint64N(50001))
31+
if i%5000 == 0 {
32+
check = append(check, d[i])
33+
}
34+
}
35+
l0 := bucketList{d[1234], d}
36+
b := MustNoErrorV(l0.serialize())
37+
l1 := MustNoErrorV(deserializeBucketList(b))
38+
req.Equal(&l0, l1)
39+
req.Equal(d[1234], l1.maxBucket)
40+
for i := 0; i < 50001; i += 5000 {
41+
req.Equal(check[i/5000], l1.deleted[i])
42+
}
43+
}
44+
45+
func TestCandList(t *testing.T) {
46+
req := require.New(t)
47+
var (
48+
check [][]byte
49+
l0 = candList{}
50+
)
51+
for range 10 {
52+
var b [20]byte
53+
n := MustNoErrorV(ra.Read(b[:]))
54+
req.Equal(20, n)
55+
l0.id = append(l0.id, b[:])
56+
check = append(check, b[:])
57+
}
58+
b := MustNoErrorV(l0.serialize())
59+
l1 := MustNoErrorV(deserializeCandList(b))
60+
req.Equal(10, len(l1.id))
61+
req.Equal(&l0, l1)
62+
for i := range 10 {
63+
req.Equal(check[i], l1.id[i])
64+
}
65+
}

0 commit comments

Comments
 (0)