Skip to content

Commit

Permalink
Update setSlashingRates API of ChainScore
Browse files Browse the repository at this point in the history
* Reinforce parameter validation
* Record SlashingRateChangedV2(str,int) eventLogs
  • Loading branch information
goldworm committed Aug 7, 2023
1 parent 27838b4 commit 68b09a9
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 5 deletions.
6 changes: 6 additions & 0 deletions icon/chainscore_iiss.go
Original file line number Diff line number Diff line change
Expand Up @@ -870,6 +870,9 @@ func (s *chainScore) Ex_setSlashingRates(values []interface{}) error {
if err != nil {
return err
}
if len(values) == 0 {
return nil
}

rates := make(map[string]icmodule.Rate)
for _, v := range values {
Expand All @@ -882,6 +885,9 @@ func (s *chainScore) Ex_setSlashingRates(values []interface{}) error {
if !ok {
return scoreresult.InvalidParameterError.New("InvalidRateType")
}
if _, ok = rates[name]; ok {
return icmodule.DuplicateError.Errorf("DuplicatePenaltyName(%s)", name)
}
rates[name] = icmodule.Rate(value.Int64())
}
return es.SetSlashingRates(s.newCallContext(s.cc), rates)
Expand Down
1 change: 1 addition & 0 deletions icon/icmodule/penalty.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,5 @@ func PenaltyNames() []string {

func GetPenaltyTypes() []PenaltyType {
return penaltyTypes

}
33 changes: 33 additions & 0 deletions icon/iiss/eventlog.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* Copyright 2023 ICON Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package iiss

import (
"github.com/icon-project/goloop/common/intconv"
"github.com/icon-project/goloop/icon/icmodule"
"github.com/icon-project/goloop/service/state"
)

func recordSlashingRateChangedV2Event(cc icmodule.CallContext, penaltyType icmodule.PenaltyType, rate icmodule.Rate) {
cc.OnEvent(state.SystemAddress,
[][]byte{[]byte("SlashingRateChangedV2(str,int)")},
[][]byte{
[]byte(penaltyType.String()),
intconv.Int64ToBytes(rate.NumInt64()),
},
)
}
30 changes: 25 additions & 5 deletions icon/iiss/extension.go
Original file line number Diff line number Diff line change
Expand Up @@ -1839,11 +1839,31 @@ func (es *ExtensionStateImpl) OnSetPublicKey(cc icmodule.CallContext, from modul
)
}

func (es *ExtensionStateImpl) SetSlashingRates(_ icmodule.CallContext, values map[string]icmodule.Rate) error {
func (es *ExtensionStateImpl) SetSlashingRates(cc icmodule.CallContext, values map[string]icmodule.Rate) error {
var pt icmodule.PenaltyType
rates := make(map[icmodule.PenaltyType]icmodule.Rate)

for name, rate := range values {
penaltyType := icmodule.ToPenaltyType(name)
if err := es.State.SetSlashingRate(penaltyType, rate); err != nil {
return err
pt = icmodule.ToPenaltyType(name)
if !pt.IsValid() {
return scoreresult.InvalidParameterError.Errorf("InvalidPenaltyName(%s)", name)
}
rates[pt] = rate
}

for _, pt = range icmodule.GetPenaltyTypes() {
if rate, ok := rates[pt]; ok {
oldRate, err := es.State.GetSlashingRate(pt)
if err != nil {
return err
}
if oldRate != rate {
if err = es.State.SetSlashingRate(pt, rate); err != nil {
return err
}
// Record slashingRateChangedV2 eventLogs in PenaltyType order
recordSlashingRateChangedV2Event(cc, pt, rate)
}
}
}
return nil
Expand All @@ -1863,4 +1883,4 @@ func (es *ExtensionStateImpl) GetSlashingRates(penaltyTypes []icmodule.PenaltyTy
}
}
return jso, nil
}
}

0 comments on commit 68b09a9

Please sign in to comment.