diff --git a/.golangci.yml b/.golangci.yml index 7ee3e72b32..a66c137f70 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -77,3 +77,4 @@ linters-settings: - github.com/shopspring/decimal - github.com/golang/snappy - github.com/stretchr/testify + - gopkg.in/yaml.v3 diff --git a/client-sdk/go/modules/rofl/policy.go b/client-sdk/go/modules/rofl/policy.go index 839fcca38d..3509ff665b 100644 --- a/client-sdk/go/modules/rofl/policy.go +++ b/client-sdk/go/modules/rofl/policy.go @@ -1,6 +1,10 @@ package rofl import ( + "fmt" + + "gopkg.in/yaml.v3" + beacon "github.com/oasisprotocol/oasis-core/go/beacon/api" "github.com/oasisprotocol/oasis-core/go/common/crypto/signature" "github.com/oasisprotocol/oasis-core/go/common/sgx" @@ -39,8 +43,31 @@ type AllowedEndorsement struct { type FeePolicy uint8 const ( - // FeePolicyAppPays is a fee policy where the application enclave pays the gas fees. - FeePolicyAppPays FeePolicy = 1 + // FeePolicyInstancePays is a fee policy where the application enclave pays the gas fees. + FeePolicyInstancePays FeePolicy = 1 // FeePolicyEndorsingNodePays is a fee policy where the endorsing node pays the gas fees. FeePolicyEndorsingNodePays FeePolicy = 2 ) + +// UnmarshalYAML implements yaml.Unmarshaler. +func (fp *FeePolicy) UnmarshalYAML(value *yaml.Node) error { + switch value.ShortTag() { + case "!!str": + var feePolicyStr string + if err := value.Decode(&feePolicyStr); err != nil { + return err + } + + switch feePolicyStr { + case "instance": + *fp = FeePolicyInstancePays + case "endorsing_node": + *fp = FeePolicyEndorsingNodePays + default: + return fmt.Errorf("unsupported fee policy: '%s'", feePolicyStr) + } + return nil + default: + return fmt.Errorf("unsupported fee policy type") + } +} diff --git a/client-sdk/go/modules/rofl/policy_test.go b/client-sdk/go/modules/rofl/policy_test.go new file mode 100644 index 0000000000..7577db277f --- /dev/null +++ b/client-sdk/go/modules/rofl/policy_test.go @@ -0,0 +1,37 @@ +package rofl + +import ( + "testing" + + "github.com/stretchr/testify/require" + "gopkg.in/yaml.v3" +) + +func TestPolicySerialization(t *testing.T) { + require := require.New(t) + + tcs := []struct { + data string + ok bool + expected FeePolicy + }{ + {"instance", true, FeePolicyInstancePays}, + {"endorsing_node", true, FeePolicyEndorsingNodePays}, + {"1", false, 0}, + {"2", false, 0}, + {"foo", false, 0}, + {"3", false, 0}, + {"{}", false, 0}, + } + + for _, tc := range tcs { + var dec FeePolicy + err := yaml.Unmarshal([]byte(tc.data), &dec) + if tc.ok { + require.NoError(err, "yaml.Unmarshal") + require.EqualValues(tc.expected, dec) + } else { + require.Error(err, "yaml.Unmarshal") + } + } +} diff --git a/runtime-sdk/src/modules/rofl/mod.rs b/runtime-sdk/src/modules/rofl/mod.rs index c0528c911c..ccbf04ac49 100644 --- a/runtime-sdk/src/modules/rofl/mod.rs +++ b/runtime-sdk/src/modules/rofl/mod.rs @@ -497,7 +497,7 @@ impl module::FeeProxyHandler for Module { )?; match app_policy.fees { - FeePolicy::AppPays => { + FeePolicy::InstancePays => { // Application needs to figure out a way to pay, defer to regular handler. Ok(None) } diff --git a/runtime-sdk/src/modules/rofl/policy.rs b/runtime-sdk/src/modules/rofl/policy.rs index 9421951229..ad802e42d1 100644 --- a/runtime-sdk/src/modules/rofl/policy.rs +++ b/runtime-sdk/src/modules/rofl/policy.rs @@ -47,7 +47,7 @@ pub enum AllowedEndorsement { #[repr(u8)] pub enum FeePolicy { /// Application enclave pays the gas fees. - AppPays = 1, + InstancePays = 1, /// Endorsing node pays the gas fees. #[default] EndorsingNodePays = 2,