Skip to content

Commit

Permalink
test: add test for calculateFee GRPC
Browse files Browse the repository at this point in the history
  • Loading branch information
ZigBalthazar committed Jul 26, 2023
1 parent dba8f7a commit 325f666
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 8 deletions.
2 changes: 1 addition & 1 deletion state/mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,6 @@ func (m *MockState) Params() param.Params {
return m.TestParams
}

func (m *MockState) CalcFee(amount int64, payloadType payload.Type) (int64, error) {
func (m *MockState) CalcFee(_ int64, payloadType payload.Type) (int64, error) {

Check failure on line 214 in state/mock.go

View workflow job for this annotation

GitHub Actions / linting

unused-parameter: parameter 'payloadType' seems to be unused, consider removing or renaming it as _ (revive)
return 0, nil
}
35 changes: 35 additions & 0 deletions state/state_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"github.com/pactus-project/pactus/types/tx/payload"
"github.com/pactus-project/pactus/types/validator"
"github.com/pactus-project/pactus/util"
"github.com/pactus-project/pactus/util/errors"
"github.com/pactus-project/pactus/util/testsuite"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -728,3 +729,37 @@ func TestCommittingInvalidBlock(t *testing.T) {
// It is possible that the same block would be considered valid by td.state2.
assert.Error(t, td.state1.CommitBlock(2, b, c))
}

func TestCalcFee(t *testing.T) {
td := setup(t)
tests := []struct {
amount int64
fee int64
expectedFee int64
expectedErrCode int
}{
{1, 1, td.state1.params.MinimumFee, errors.ErrInvalidFee},
{1, 1001, td.state1.params.MinimumFee, errors.ErrInvalidFee},
{1, 1000, td.state1.params.MinimumFee, errors.ErrNone},

{1 * 1e9, 1, 100000, errors.ErrInvalidFee},
{1 * 1e9, 100001, 100000, errors.ErrInvalidFee},
{1 * 1e9, 100000, 100000, errors.ErrNone},

{1 * 1e12, 1, 1000000, errors.ErrInvalidFee},
{1 * 1e12, 1000001, 1000000, errors.ErrInvalidFee},
{1 * 1e12, 1000000, 1000000, errors.ErrNone},
}
for _, test := range tests {
fee, err := td.state2.CalcFee(int64(test.amount), payload.PayloadTypeTransfer)

Check failure on line 754 in state/state_test.go

View workflow job for this annotation

GitHub Actions / linting

unnecessary conversion (unconvert)
assert.NoError(t, err)
assert.Equal(t, int64(test.expectedFee), fee)

Check failure on line 756 in state/state_test.go

View workflow job for this annotation

GitHub Actions / linting

unnecessary conversion (unconvert)

fee, err = td.state2.CalcFee(int64(test.amount), payload.PayloadTypeUnbond)
assert.NoError(t, err)
assert.Equal(t, int64(0), fee)

_, err = td.state2.CalcFee(int64(test.amount), 6)

Check failure on line 762 in state/state_test.go

View workflow job for this annotation

GitHub Actions / linting

unnecessary conversion (unconvert)
assert.Error(t, err)
}
}
1 change: 0 additions & 1 deletion wallet/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,5 +93,4 @@ func (c *grpcClient) getFee(amount int64, payloadType payload.Type) (int64, erro
return 0, err

Check warning on line 93 in wallet/client.go

View check run for this annotation

Codecov / codecov/patch

wallet/client.go#L93

Added line #L93 was not covered by tests
}
return res.Fee, nil

}
8 changes: 7 additions & 1 deletion wallet/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,13 @@ func (s *transactionServer) GetTransaction(_ context.Context,
}

func (s *transactionServer) CalculateFee(_ context.Context,
req *pactus.CalculateFeeRequest) (*pactus.CalculateFeeResponse, error) {
_ *pactus.CalculateFeeRequest) (*pactus.CalculateFeeResponse, error) {

return &pactus.CalculateFeeResponse{Fee: 0}, nil
}

func (s *transactionServer) GetFee(_ context.Context,
_ *pactus.CalculateFeeRequest) (*pactus.CalculateFeeResponse, error) {

return &pactus.CalculateFeeResponse{Fee: 0}, nil
}
Expand Down
5 changes: 4 additions & 1 deletion wallet/tx_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,10 @@ func (m *txBuilder) build() (*tx.Tx, error) {
return nil, err
}

m.setFee()
err = m.setFee()
if err != nil {
return nil, err

Check warning on line 102 in wallet/tx_builder.go

View check run for this annotation

Codecov / codecov/patch

wallet/tx_builder.go#L102

Added line #L102 was not covered by tests
}

var trx *tx.Tx
switch m.typ {
Expand Down
6 changes: 2 additions & 4 deletions www/grpc/statik/statik.go

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions www/grpc/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ func (s *transactionServer) CalculateFee(_ context.Context,
if err != nil {
return nil, err

Check warning on line 69 in www/grpc/transaction.go

View check run for this annotation

Codecov / codecov/patch

www/grpc/transaction.go#L66-L69

Added lines #L66 - L69 were not covered by tests
}

return &pactus.CalculateFeeResponse{
Fee: fee,
}, nil

Check warning on line 74 in www/grpc/transaction.go

View check run for this annotation

Codecov / codecov/patch

www/grpc/transaction.go#L72-L74

Added lines #L72 - L74 were not covered by tests
Expand Down

0 comments on commit 325f666

Please sign in to comment.