From 68b09a99fc3a69bd78c47f70436b3bbc375d22ea Mon Sep 17 00:00:00 2001 From: Chiwon Cho Date: Mon, 7 Aug 2023 12:45:18 +0900 Subject: [PATCH] Update setSlashingRates API of ChainScore * Reinforce parameter validation * Record SlashingRateChangedV2(str,int) eventLogs --- icon/chainscore_iiss.go | 6 ++++++ icon/icmodule/penalty.go | 1 + icon/iiss/eventlog.go | 33 +++++++++++++++++++++++++++++++++ icon/iiss/extension.go | 30 +++++++++++++++++++++++++----- 4 files changed, 65 insertions(+), 5 deletions(-) create mode 100644 icon/iiss/eventlog.go diff --git a/icon/chainscore_iiss.go b/icon/chainscore_iiss.go index 1dba53e4e..bf51dc7ca 100644 --- a/icon/chainscore_iiss.go +++ b/icon/chainscore_iiss.go @@ -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 { @@ -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) diff --git a/icon/icmodule/penalty.go b/icon/icmodule/penalty.go index 342c20486..8600fbd7e 100644 --- a/icon/icmodule/penalty.go +++ b/icon/icmodule/penalty.go @@ -71,4 +71,5 @@ func PenaltyNames() []string { func GetPenaltyTypes() []PenaltyType { return penaltyTypes + } \ No newline at end of file diff --git a/icon/iiss/eventlog.go b/icon/iiss/eventlog.go new file mode 100644 index 000000000..ae2eaa859 --- /dev/null +++ b/icon/iiss/eventlog.go @@ -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()), + }, + ) +} \ No newline at end of file diff --git a/icon/iiss/extension.go b/icon/iiss/extension.go index 08ebe48a6..77bec6549 100644 --- a/icon/iiss/extension.go +++ b/icon/iiss/extension.go @@ -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 @@ -1863,4 +1883,4 @@ func (es *ExtensionStateImpl) GetSlashingRates(penaltyTypes []icmodule.PenaltyTy } } return jso, nil -} \ No newline at end of file +}