From 10d330e5b1b3dcbbc629a37b0a64fc869b63d271 Mon Sep 17 00:00:00 2001 From: Emmanuel T Odeke Date: Sun, 2 Aug 2020 13:18:28 -0700 Subject: [PATCH] x/params/types: make KeyTable.RegisterType fully dereference pointers (#6915) Use a for loop to correctly and fully dereference and extract the type for the type of ParamSetPair.Value. Fixes #6785 Co-authored-by: Alexander Bezobchuk --- x/params/types/deref_test.go | 27 +++++++++++++++++++++++++++ x/params/types/table.go | 2 +- 2 files changed, 28 insertions(+), 1 deletion(-) create mode 100644 x/params/types/deref_test.go diff --git a/x/params/types/deref_test.go b/x/params/types/deref_test.go new file mode 100644 index 000000000..1cd4406e5 --- /dev/null +++ b/x/params/types/deref_test.go @@ -0,0 +1,27 @@ +package types + +import ( + "reflect" + "testing" + + "github.com/stretchr/testify/require" +) + +func TestKeyTableUnfurlsPointers(t *testing.T) { + tbl := NewKeyTable() + validator := func(_ interface{}) error { + return nil + } + tbl = tbl.RegisterType(ParamSetPair{ + Key: []byte("key"), + Value: (*****string)(nil), + ValidatorFn: validator, + }) + + got := tbl.m["key"] + want := attribute{ + vfn: validator, + ty: reflect.ValueOf("").Type(), + } + require.Equal(t, got.ty, want.ty) +} diff --git a/x/params/types/table.go b/x/params/types/table.go index 72c24dedc..ef09c39d8 100644 --- a/x/params/types/table.go +++ b/x/params/types/table.go @@ -48,7 +48,7 @@ func (t KeyTable) RegisterType(psp ParamSetPair) KeyTable { rty := reflect.TypeOf(psp.Value) // indirect rty if it is a pointer - if rty.Kind() == reflect.Ptr { + for rty.Kind() == reflect.Ptr { rty = rty.Elem() }