Skip to content

Commit 9491f79

Browse files
authored
mark repository methods private (#1291)
1 parent dda3d95 commit 9491f79

5 files changed

Lines changed: 43 additions & 30 deletions

File tree

builtin/staker/validation/repository.go

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
package validation
77

88
import (
9+
"encoding/binary"
910
"math/big"
1011

1112
"github.com/pkg/errors"
@@ -24,18 +25,18 @@ type Repository struct {
2425
validations *solidity.Mapping[thor.Address, Validation]
2526
rewards *solidity.Mapping[thor.Bytes32, *big.Int] // stores rewards per validator staking period
2627

27-
exits *solidity.Mapping[*big.Int, thor.Address] // exit block -> validator ID
28+
exits *solidity.Mapping[thor.Bytes32, thor.Address] // exit block -> validator ID
2829
}
2930

3031
func NewRepository(sctx *solidity.Context) *Repository {
3132
return &Repository{
3233
validations: solidity.NewMapping[thor.Address, Validation](sctx, slotValidations),
3334
rewards: solidity.NewMapping[thor.Bytes32, *big.Int](sctx, slotRewards),
34-
exits: solidity.NewMapping[*big.Int, thor.Address](sctx, slotExitEpochs),
35+
exits: solidity.NewMapping[thor.Bytes32, thor.Address](sctx, slotExitEpochs),
3536
}
3637
}
3738

38-
func (r *Repository) GetValidation(validator thor.Address) (*Validation, error) {
39+
func (r *Repository) getValidation(validator thor.Address) (*Validation, error) {
3940
v, err := r.validations.Get(validator)
4041
if err != nil {
4142
return nil, errors.Wrap(err, "failed to get validator")
@@ -50,7 +51,7 @@ func (r *Repository) setValidation(validator thor.Address, entry *Validation, is
5051
return nil
5152
}
5253

53-
func (r *Repository) GetReward(key thor.Bytes32) (*big.Int, error) {
54+
func (r *Repository) getReward(key thor.Bytes32) (*big.Int, error) {
5455
reward, err := r.rewards.Get(key)
5556
if err != nil {
5657
return nil, errors.Wrap(err, "failed to get reward")
@@ -61,18 +62,22 @@ func (r *Repository) GetReward(key thor.Bytes32) (*big.Int, error) {
6162
return reward, nil
6263
}
6364

64-
func (r *Repository) SetReward(key thor.Bytes32, val *big.Int, isNew bool) error {
65+
func (r *Repository) setReward(key thor.Bytes32, val *big.Int, isNew bool) error {
6566
return r.rewards.Set(key, val, isNew)
6667
}
6768

68-
func (r *Repository) GetExit(block *big.Int) (thor.Address, error) {
69-
return r.exits.Get(block)
69+
func (r *Repository) getExit(block uint32) (thor.Address, error) {
70+
var key thor.Bytes32
71+
binary.BigEndian.PutUint32(key[:], block)
72+
73+
return r.exits.Get(key)
7074
}
7175

72-
func (r *Repository) SetExit(block uint32, validator thor.Address) error {
73-
bigBlock := big.NewInt(0).SetUint64(uint64(block))
76+
func (r *Repository) setExit(block uint32, validator thor.Address) error {
77+
var key thor.Bytes32
78+
binary.BigEndian.PutUint32(key[:], block)
7479

75-
if err := r.exits.Set(bigBlock, validator, true); err != nil {
80+
if err := r.exits.Set(key, validator, true); err != nil {
7681
return errors.Wrap(err, "failed to set exit epoch")
7782
}
7883
return nil

builtin/staker/validation/repository_test.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ func TestRepository_Validation_RoundTrip(t *testing.T) {
3838

3939
assert.NoError(t, repo.setValidation(id, entry, true))
4040

41-
got, err := repo.GetValidation(id)
41+
got, err := repo.getValidation(id)
4242
assert.NoError(t, err)
4343
assert.Equal(t, entry.Endorser, got.Endorser)
4444
assert.Equal(t, uint32(15), got.Period)
@@ -55,7 +55,7 @@ func TestRepository_Validation_GetError(t *testing.T) {
5555
slot := thor.Blake2b(id.Bytes(), slotValidations.Bytes())
5656
st.SetRawStorage(addr, slot, rlp.RawValue{0xFF})
5757

58-
_, err := repo.GetValidation(id)
58+
_, err := repo.getValidation(id)
5959
assert.ErrorContains(t, err, "failed to get validator")
6060
}
6161

@@ -64,15 +64,15 @@ func TestRepository_Reward_RoundTrip_DefaultZero(t *testing.T) {
6464
key := thor.BytesToBytes32([]byte("r1"))
6565

6666
// get before set -> zero
67-
val, err := repo.GetReward(key)
67+
val, err := repo.getReward(key)
6868
assert.NoError(t, err)
6969
assert.Equal(t, big.NewInt(0), val)
7070

7171
// set then get
7272
want := big.NewInt(1234)
73-
assert.NoError(t, repo.SetReward(key, want, true))
73+
assert.NoError(t, repo.setReward(key, want, true))
7474

75-
got, err := repo.GetReward(key)
75+
got, err := repo.getReward(key)
7676
assert.NoError(t, err)
7777
assert.Equal(t, want, got)
7878
}
@@ -85,17 +85,17 @@ func TestRepository_Reward_GetError(t *testing.T) {
8585
slot := thor.Blake2b(key.Bytes(), slotRewards.Bytes())
8686
st.SetRawStorage(addr, slot, rlp.RawValue{0xFF})
8787

88-
_, err := repo.GetReward(key)
88+
_, err := repo.getReward(key)
8989
assert.ErrorContains(t, err, "failed to get reward")
9090
}
9191

9292
func TestRepository_Exit_RoundTrip(t *testing.T) {
9393
repo, _, _ := newRepo(t)
9494
validator := thor.BytesToAddress([]byte("v3"))
9595

96-
assert.NoError(t, repo.SetExit(42, validator))
96+
assert.NoError(t, repo.setExit(42, validator))
9797

98-
addr, err := repo.GetExit(big.NewInt(42))
98+
addr, err := repo.getExit(42)
9999
assert.NoError(t, err)
100100
assert.Equal(t, validator, addr)
101101
}

builtin/staker/validation/service.go

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -76,18 +76,18 @@ func (s *Service) IncreaseDelegatorsReward(node thor.Address, reward *big.Int) e
7676
binary.BigEndian.PutUint32(periodBytes, val.CurrentIteration())
7777
key := thor.Blake2b([]byte("rewards"), node.Bytes(), periodBytes)
7878

79-
rewards, err := s.repo.GetReward(key)
79+
rewards, err := s.repo.getReward(key)
8080
if err != nil {
8181
return err
8282
}
8383

84-
return s.repo.SetReward(key, big.NewInt(0).Add(rewards, reward), false)
84+
return s.repo.setReward(key, big.NewInt(0).Add(rewards, reward), false)
8585
}
8686

8787
func (s *Service) LeaderGroupIterator(callbacks ...func(thor.Address, *Validation) error) error {
8888
return s.leaderGroup.Iter(func(address thor.Address) error {
8989
// Fetch the validation object for this address
90-
validation, err := s.repo.GetValidation(address)
90+
validation, err := s.repo.getValidation(address)
9191
if err != nil {
9292
return err
9393
}
@@ -421,7 +421,7 @@ func (s *Service) SetExitBlock(validator thor.Address, minBlock uint32) (uint32,
421421
return start, nil
422422
}
423423
if existing.IsZero() {
424-
if err = s.repo.SetExit(start, validator); err != nil {
424+
if err = s.repo.setExit(start, validator); err != nil {
425425
return 0, errors.Wrap(err, "failed to set exit epoch")
426426
}
427427
return start, nil
@@ -431,9 +431,7 @@ func (s *Service) SetExitBlock(validator thor.Address, minBlock uint32) (uint32,
431431
}
432432

433433
func (s *Service) GetExitEpoch(block uint32) (thor.Address, error) {
434-
bigBlock := big.NewInt(0).SetUint64(uint64(block))
435-
436-
validator, err := s.repo.GetExit(bigBlock)
434+
validator, err := s.repo.getExit(block)
437435
if err != nil {
438436
return thor.Address{}, errors.Wrap(err, "failed to get exit epoch")
439437
}
@@ -445,7 +443,7 @@ func (s *Service) GetDelegatorRewards(validator thor.Address, stakingPeriod uint
445443
binary.BigEndian.PutUint32(periodBytes, stakingPeriod)
446444
key := thor.Blake2b([]byte("rewards"), validator.Bytes(), periodBytes)
447445

448-
return s.repo.GetReward(key)
446+
return s.repo.getReward(key)
449447
}
450448

451449
// ActivateValidator transitions a validator from queued to active status.
@@ -542,7 +540,7 @@ func (s *Service) Renew(validator thor.Address, delegationWeight uint64) (*delta
542540
//
543541

544542
func (s *Service) GetValidation(validator thor.Address) (*Validation, error) {
545-
return s.repo.GetValidation(validator)
543+
return s.repo.getValidation(validator)
546544
}
547545

548546
func (s *Service) GetExistingValidation(validator thor.Address) (*Validation, error) {

builtin/staker/validation/service_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,9 @@ import (
2222
)
2323

2424
func poisonExitSlot(st *state.State, contract thor.Address, block uint32) {
25-
bigBlock := big.NewInt(0).SetUint64(uint64(block))
26-
slot := thor.Blake2b(bigBlock.Bytes(), slotExitEpochs.Bytes())
25+
var key thor.Bytes32
26+
binary.BigEndian.PutUint32(key[:], block)
27+
slot := thor.Blake2b(key[:], slotExitEpochs.Bytes())
2728
st.SetRawStorage(contract, slot, rlp.RawValue{0xFF})
2829
}
2930

thor/config.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ var (
1919
mediumStakingPeriod uint32 = 8640 * 15 // 15 Days
2020
highStakingPeriod uint32 = 8640 * 30 // 30 Days
2121
cooldownPeriod uint32 = 8640 // 8640 blocks, 1 day
22-
hayabusaTP uint32 = 360 * 24 * 14
22+
hayabusaTP uint32 = 8640 * 14 // 14 days
2323

2424
locked bool
2525
)
@@ -89,38 +89,47 @@ func LockConfig() {
8989
locked = true
9090
}
9191

92+
// default value is 10 seconds
9293
func BlockInterval() uint64 {
9394
return blockInterval
9495
}
9596

97+
// default value is 180 blocks, 30 minutes
9698
func EpochLength() uint32 {
9799
return epochLength
98100
}
99101

102+
// default value is 8640 blocks, 1 day
100103
func SeederInterval() uint32 {
101104
return seederInterval
102105
}
103106

107+
// default value is 7 days
104108
func ValidatorEvictionThreshold() uint32 {
105109
return validatorEvictionThreshold
106110
}
107111

112+
// default value is 7 days
108113
func LowStakingPeriod() uint32 {
109114
return lowStakingPeriod
110115
}
111116

117+
// default value is 15 days
112118
func MediumStakingPeriod() uint32 {
113119
return mediumStakingPeriod
114120
}
115121

122+
// default value is 30 days
116123
func HighStakingPeriod() uint32 {
117124
return highStakingPeriod
118125
}
119126

127+
// default value is 1 day
120128
func CooldownPeriod() uint32 {
121129
return cooldownPeriod
122130
}
123131

132+
// default value is 14 days
124133
func HayabusaTP() uint32 {
125134
return hayabusaTP
126135
}

0 commit comments

Comments
 (0)