Skip to content

Commit

Permalink
runtime-sdk/modules/rofl: Add YAML serialization for ROFL fee policy
Browse files Browse the repository at this point in the history
  • Loading branch information
kostko committed Aug 20, 2024
1 parent 5844bfe commit c3042a3
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 4 deletions.
1 change: 1 addition & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -77,3 +77,4 @@ linters-settings:
- github.com/shopspring/decimal
- github.com/golang/snappy
- github.com/stretchr/testify
- gopkg.in/yaml.v3
31 changes: 29 additions & 2 deletions client-sdk/go/modules/rofl/policy.go
Original file line number Diff line number Diff line change
@@ -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"
Expand Down Expand Up @@ -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")
}
}
37 changes: 37 additions & 0 deletions client-sdk/go/modules/rofl/policy_test.go
Original file line number Diff line number Diff line change
@@ -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")
}
}
}
2 changes: 1 addition & 1 deletion runtime-sdk/src/modules/rofl/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -497,7 +497,7 @@ impl<Cfg: Config> module::FeeProxyHandler for Module<Cfg> {
)?;

match app_policy.fees {
FeePolicy::AppPays => {
FeePolicy::InstancePays => {
// Application needs to figure out a way to pay, defer to regular handler.
Ok(None)
}
Expand Down
2 changes: 1 addition & 1 deletion runtime-sdk/src/modules/rofl/policy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit c3042a3

Please sign in to comment.