github.com/decred/dcrlnd@v0.7.6/watchtower/wtpolicy/policy_test.go (about)

     1  package wtpolicy_test
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/decred/dcrlnd/watchtower/blob"
     7  	"github.com/decred/dcrlnd/watchtower/wtpolicy"
     8  	"github.com/stretchr/testify/require"
     9  )
    10  
    11  var validationTests = []struct {
    12  	name   string
    13  	policy wtpolicy.Policy
    14  	expErr error
    15  }{
    16  	{
    17  		name: "fail no maxupdates",
    18  		policy: wtpolicy.Policy{
    19  			TxPolicy: wtpolicy.TxPolicy{
    20  				BlobType: blob.TypeAltruistCommit,
    21  			},
    22  		},
    23  		expErr: wtpolicy.ErrNoMaxUpdates,
    24  	},
    25  	{
    26  		name: "fail altruist with reward base",
    27  		policy: wtpolicy.Policy{
    28  			TxPolicy: wtpolicy.TxPolicy{
    29  				BlobType:   blob.TypeAltruistCommit,
    30  				RewardBase: 1,
    31  			},
    32  		},
    33  		expErr: wtpolicy.ErrAltruistReward,
    34  	},
    35  	{
    36  		name: "fail altruist with reward rate",
    37  		policy: wtpolicy.Policy{
    38  			TxPolicy: wtpolicy.TxPolicy{
    39  				BlobType:   blob.TypeAltruistCommit,
    40  				RewardRate: 1,
    41  			},
    42  		},
    43  		expErr: wtpolicy.ErrAltruistReward,
    44  	},
    45  	{
    46  		name: "fail sweep fee rate too low",
    47  		policy: wtpolicy.Policy{
    48  			TxPolicy: wtpolicy.TxPolicy{
    49  				BlobType: blob.TypeAltruistCommit,
    50  			},
    51  			MaxUpdates: 1,
    52  		},
    53  		expErr: wtpolicy.ErrSweepFeeRateTooLow,
    54  	},
    55  	{
    56  		name: "minimal valid altruist policy",
    57  		policy: wtpolicy.Policy{
    58  			TxPolicy: wtpolicy.TxPolicy{
    59  				BlobType:     blob.TypeAltruistCommit,
    60  				SweepFeeRate: wtpolicy.MinSweepFeeRate,
    61  			},
    62  			MaxUpdates: 1,
    63  		},
    64  	},
    65  	{
    66  		name: "valid altruist policy with default sweep rate",
    67  		policy: wtpolicy.Policy{
    68  			TxPolicy: wtpolicy.TxPolicy{
    69  				BlobType:     blob.TypeAltruistCommit,
    70  				SweepFeeRate: wtpolicy.DefaultSweepFeeRate,
    71  			},
    72  			MaxUpdates: 1,
    73  		},
    74  	},
    75  	{
    76  		name:   "valid default policy",
    77  		policy: wtpolicy.DefaultPolicy(),
    78  	},
    79  }
    80  
    81  // TestPolicyValidate asserts that the sanity checks for policies behave as
    82  // expected.
    83  func TestPolicyValidate(t *testing.T) {
    84  	for i := range validationTests {
    85  		test := validationTests[i]
    86  		t.Run(test.name, func(t *testing.T) {
    87  			err := test.policy.Validate()
    88  			if err != test.expErr {
    89  				t.Fatalf("validation error mismatch, "+
    90  					"want: %v, got: %v", test.expErr, err)
    91  			}
    92  		})
    93  	}
    94  }
    95  
    96  // TestPolicyIsAnchorChannel asserts that the IsAnchorChannel helper properly
    97  // reflects the anchor bit of the policy's blob type.
    98  func TestPolicyIsAnchorChannel(t *testing.T) {
    99  	policyNoAnchor := wtpolicy.Policy{
   100  		TxPolicy: wtpolicy.TxPolicy{
   101  			BlobType: blob.TypeAltruistCommit,
   102  		},
   103  	}
   104  	require.Equal(t, false, policyNoAnchor.IsAnchorChannel())
   105  
   106  	policyAnchor := wtpolicy.Policy{
   107  		TxPolicy: wtpolicy.TxPolicy{
   108  			BlobType: blob.TypeAltruistAnchorCommit,
   109  		},
   110  	}
   111  	require.Equal(t, true, policyAnchor.IsAnchorChannel())
   112  }