Skip to content

Commit cc4bc9a

Browse files
committed
x
1 parent f09f94b commit cc4bc9a

File tree

3 files changed

+38
-48
lines changed

3 files changed

+38
-48
lines changed

action/protocol/staking/candidate_buckets_indexer.go

+28
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@ package staking
88

99
import (
1010
"context"
11+
"encoding/hex"
12+
"fmt"
13+
"math/big"
14+
15+
"github.com/iotexproject/iotex-address/address"
1116

1217
"github.com/gogo/protobuf/proto"
1318
"github.com/pkg/errors"
@@ -171,3 +176,26 @@ func (cbi *CandidatesBucketsIndexer) GetBuckets(height uint64, offset, limit uin
171176
d, err := proto.Marshal(buckets)
172177
return d, height, err
173178
}
179+
180+
// PutStakingBalance sets staking balance
181+
func (cbi *CandidatesBucketsIndexer) PutStakingBalance(height uint64, total *totalAmount) error {
182+
hei := byteutil.Uint64ToBytesBigEndian(height - 1)
183+
historyKey := append(bucketPoolAddrKey, hei...)
184+
fmt.Println("PutStakingBalance", height, []byte(StakingBucketsNamespace), hex.EncodeToString(historyKey), total.amount)
185+
return cbi.kvStore.Put(StakingBucketsNamespace, historyKey, total.amount.Bytes())
186+
}
187+
188+
// GetStakingBalance gets staking balance
189+
func (cbi *CandidatesBucketsIndexer) GetStakingBalance(height uint64) (*iotextypes.AccountMeta, uint64, error) {
190+
hei := byteutil.Uint64ToBytesBigEndian(height)
191+
historyKey := append(bucketPoolAddrKey, hei...)
192+
fmt.Println("GetStakingBalance", height, []byte(StakingBucketsNamespace), hex.EncodeToString(historyKey))
193+
balanceBytes, err := cbi.kvStore.Get(StakingBucketsNamespace, historyKey)
194+
if err != nil {
195+
return nil, 0, err
196+
}
197+
meta := iotextypes.AccountMeta{}
198+
meta.Address = address.StakingBucketPoolAddr
199+
meta.Balance = big.NewInt(0).SetBytes(balanceBytes).String()
200+
return &meta, height, nil
201+
}

action/protocol/staking/protocol.go

+10-21
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ package staking
88

99
import (
1010
"context"
11-
"encoding/hex"
1211
"fmt"
1312
"math/big"
1413
"time"
@@ -29,7 +28,6 @@ import (
2928
"github.com/iotexproject/iotex-core/blockchain/genesis"
3029
"github.com/iotexproject/iotex-core/config"
3130
"github.com/iotexproject/iotex-core/pkg/log"
32-
"github.com/iotexproject/iotex-core/pkg/util/byteutil"
3331
"github.com/iotexproject/iotex-core/state"
3432
)
3533

@@ -222,13 +220,7 @@ func (p *Protocol) CreatePreStates(ctx context.Context, sm protocol.StateManager
222220
bcCtx := protocol.MustGetBlockchainCtx(ctx)
223221
blkCtx := protocol.MustGetBlockCtx(ctx)
224222
hu := config.NewHeightUpgrade(&bcCtx.Genesis)
225-
fmt.Println("CreatePreStates///////", blkCtx.BlockHeight, p.hu.FairbankBlockHeight(), p.hu.GreenlandBlockHeight())
226-
if p.archiveMode && blkCtx.BlockHeight <= p.hu.GreenlandBlockHeight() && blkCtx.BlockHeight > 1 {
227-
if err := p.saveStakingAddressHistory(blkCtx.BlockHeight, sm); err != nil {
228-
fmt.Println("saveStakingAddressHistory errrrr", err)
229-
return err
230-
}
231-
}
223+
232224
if blkCtx.BlockHeight == hu.GreenlandBlockHeight() {
233225
csr, err := ConstructBaseView(sm)
234226
if err != nil {
@@ -251,6 +243,13 @@ func (p *Protocol) CreatePreStates(ctx context.Context, sm protocol.StateManager
251243
if p.candBucketsIndexer == nil {
252244
return nil
253245
}
246+
fmt.Println("CreatePreStates///////", blkCtx.BlockHeight, p.hu.FairbankBlockHeight(), p.hu.GreenlandBlockHeight())
247+
if p.archiveMode && blkCtx.BlockHeight <= p.hu.GreenlandBlockHeight() && blkCtx.BlockHeight > 1 {
248+
if err := p.saveStakingAddressHistory(blkCtx.BlockHeight, sm); err != nil {
249+
fmt.Println("saveStakingAddressHistory errrrr", err)
250+
return err
251+
}
252+
}
254253
rp := rolldpos.MustGetProtocol(protocol.MustGetRegistry(ctx))
255254
currentEpochNum := rp.GetEpochNum(blkCtx.BlockHeight)
256255
if currentEpochNum == 0 {
@@ -274,14 +273,7 @@ func (p *Protocol) saveStakingAddressHistory(height uint64, sm protocol.StateMan
274273
if balance.amount.Sign() <= 0 {
275274
return nil
276275
}
277-
278-
hei := byteutil.Uint64ToBytesBigEndian(height - 1)
279-
//hei := byteutil.Uint64ToBytesBigEndian(height)
280-
historyKey := append(bucketPoolAddrKey, hei...)
281-
fmt.Println("saveStakingAddressHistory2", height, []byte(StakingNameSpaceForStakingAddress), hex.EncodeToString(historyKey), balance.amount)
282-
283-
_, err = sm.PutState(balance, protocol.NamespaceOption(StakingNameSpaceForStakingAddress), protocol.KeyOption(historyKey))
284-
return err
276+
return p.candBucketsIndexer.PutStakingBalance(height, balance)
285277
}
286278

287279
func (p *Protocol) handleStakingIndexer(epochStartHeight uint64, sm protocol.StateManager) error {
@@ -479,10 +471,7 @@ func (p *Protocol) ReadState(ctx context.Context, sr protocol.StateReader, metho
479471
resp, height, err = readStateCandidateByAddress(ctx, csr, r.GetCandidateByAddress())
480472
case iotexapi.ReadStakingDataMethod_TOTAL_STAKING_AMOUNT:
481473
if p.archiveMode && inputHeight <= p.hu.GreenlandBlockHeight() {
482-
resp, height, err = readStateTotalStakingAmountFromIndexer(sr, r.GetTotalStakingAmount(), inputHeight)
483-
//if err != nil {
484-
// resp, height, err = readStateTotalStakingAmount(ctx, csr, r.GetTotalStakingAmount())
485-
//}
474+
resp, height, err = p.candBucketsIndexer.GetStakingBalance(inputHeight)
486475
} else {
487476
resp, height, err = readStateTotalStakingAmount(ctx, csr, r.GetTotalStakingAmount())
488477
}

action/protocol/staking/read_state.go

-27
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@ package staking
88

99
import (
1010
"context"
11-
"encoding/hex"
12-
"fmt"
1311
"math/big"
1412

1513
"github.com/pkg/errors"
@@ -20,7 +18,6 @@ import (
2018

2119
"github.com/iotexproject/iotex-core/action/protocol"
2220
"github.com/iotexproject/iotex-core/config"
23-
"github.com/iotexproject/iotex-core/pkg/util/byteutil"
2421
"github.com/iotexproject/iotex-core/state"
2522
)
2623

@@ -147,18 +144,6 @@ func readStateTotalStakingAmount(ctx context.Context, csr CandidateStateReader,
147144
return &meta, csr.Height(), nil
148145
}
149146

150-
func readStateTotalStakingAmountFromIndexer(csr protocol.StateReader, _ *iotexapi.ReadStakingDataRequest_TotalStakingAmount, height uint64) (*iotextypes.AccountMeta, uint64, error) {
151-
fmt.Println("readStateTotalStakingAmountFromHeight", height)
152-
meta := iotextypes.AccountMeta{}
153-
meta.Address = address.StakingBucketPoolAddr
154-
total, err := getTotalStakedAmountFromIndexer(csr, height)
155-
if err != nil {
156-
return nil, height, err
157-
}
158-
meta.Balance = total.String()
159-
return &meta, height, nil
160-
}
161-
162147
func toIoTeXTypesVoteBucketList(buckets []*VoteBucket) (*iotextypes.VoteBucketList, error) {
163148
res := iotextypes.VoteBucketList{
164149
Buckets: make([]*iotextypes.VoteBucket, 0, len(buckets)),
@@ -231,15 +216,3 @@ func getTotalStakedAmount(ctx context.Context, csr CandidateStateReader) (*big.I
231216
// otherwise read from bucket pool
232217
return csr.TotalStakedAmount(), nil
233218
}
234-
235-
func getTotalStakedAmountFromIndexer(csr protocol.StateReader, height uint64) (*big.Int, error) {
236-
hei := byteutil.Uint64ToBytesBigEndian(height)
237-
historyKey := append(bucketPoolAddrKey, hei...)
238-
var total totalAmount
239-
_, err := csr.State(&total, protocol.NamespaceOption(StakingNameSpaceForStakingAddress), protocol.KeyOption(historyKey))
240-
fmt.Println("getTotalStakedAmountFromHeight", height, []byte(StakingNameSpaceForStakingAddress), hex.EncodeToString(historyKey), err)
241-
if err != nil {
242-
return nil, err
243-
}
244-
return total.amount, nil
245-
}

0 commit comments

Comments
 (0)