Skip to content

Commit

Permalink
unit test migration
Browse files Browse the repository at this point in the history
  • Loading branch information
dessaya committed Feb 23, 2023
1 parent 9e6dddd commit 5099987
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 12 deletions.
34 changes: 22 additions & 12 deletions packages/vm/core/migrations/m001_gaspertoken_to_ratio32.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package migrations
import (
"math"

"github.com/labstack/gommon/log"

"github.com/iotaledger/hive.go/core/logger"
"github.com/iotaledger/hive.go/core/marshalutil"
iotago "github.com/iotaledger/iota.go/v3"
Expand All @@ -16,26 +18,34 @@ var m001GasPerTokenToRatio32 = Migration{
Contract: governance.Contract,

Apply: func(state kv.KVStore, log *logger.Logger) error {
fpOld, err := feePolicyFromBytesOld(state.MustGet(governance.VarGasFeePolicyBytes))
fpBinOld := state.MustGet(governance.VarGasFeePolicyBytes)
fpNew, err := m001ConvertFeePolicy(fpBinOld)
if err != nil {
return err
}
if fpOld.GasPerToken > math.MaxUint32 {
log.Warn("m001GasPerTokenToRatio32: trimming gas per token")
fpOld.GasPerToken = math.MaxUint32
}
fpNew := &gas.GasFeePolicy{
GasFeeTokenID: fpOld.GasFeeTokenID,
GasFeeTokenDecimals: fpOld.GasFeeTokenDecimals,
GasPerToken: util.Ratio32{A: 1, B: uint32(fpOld.GasPerToken)},
EVMGasRatio: fpOld.EVMGasRatio,
ValidatorFeeShare: fpOld.ValidatorFeeShare,
}
state.Set(governance.VarGasFeePolicyBytes, fpNew.Bytes())
return nil
},
}

func m001ConvertFeePolicy(oldBin []byte) (*gas.GasFeePolicy, error) {
fpOld, err := feePolicyFromBytesOld(oldBin)
if err != nil {
return nil, err
}
if fpOld.GasPerToken > math.MaxUint32 {
log.Warn("m001GasPerTokenToRatio32: trimming gas per token")
fpOld.GasPerToken = math.MaxUint32
}
return &gas.GasFeePolicy{
GasFeeTokenID: fpOld.GasFeeTokenID,
GasFeeTokenDecimals: fpOld.GasFeeTokenDecimals,
GasPerToken: util.Ratio32{A: 1, B: uint32(fpOld.GasPerToken)},
EVMGasRatio: fpOld.EVMGasRatio,
ValidatorFeeShare: fpOld.ValidatorFeeShare,
}, nil
}

type gasFeePolicyOld struct {
GasFeeTokenID iotago.NativeTokenID
GasFeeTokenDecimals uint32
Expand Down
16 changes: 16 additions & 0 deletions packages/vm/core/migrations/m001_gaspertoken_to_ratio32_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package migrations

import (
"encoding/hex"
"testing"

"github.com/stretchr/testify/require"
)

func TestM001GasPerTokenToRatio32(t *testing.T) {
oldFeePolicyBin, err := hex.DecodeString("006400000000000000000100000001000000")
require.NoError(t, err)
fp, err := m001ConvertFeePolicy(oldFeePolicyBin)
require.NoError(t, err)
t.Log(fp)
}

0 comments on commit 5099987

Please sign in to comment.