Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add CalcualteFee in GRPC #601

Merged
merged 25 commits into from
Jul 30, 2023
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
ce46557
change bug to protoc for protoBuffer generating and test it
ZigBalthazar Jul 25, 2023
23865fc
update COTRIBUTING.md
ZigBalthazar Jul 25, 2023
9731b4d
Revert "chore: Blockchain typo (#598)"
ZigBalthazar Jul 26, 2023
ee05091
update buf to v1.25.0
ZigBalthazar Jul 26, 2023
1315d9d
fix: update buf protoBuffer
ZigBalthazar Jul 26, 2023
ba578f2
fix: proto generate
ZigBalthazar Jul 26, 2023
ec6a2ea
define GetTransactionFee
ZigBalthazar Jul 26, 2023
71e42b8
feat: Add CalcualteFee in GRPC
ZigBalthazar Jul 26, 2023
e5a5690
feat: Add CalcualteFee in GRPC
ZigBalthazar Jul 26, 2023
99e7a1f
pull from main
ZigBalthazar Jul 26, 2023
a25f3c3
feat: Add CalcualteFee in GRPC
ZigBalthazar Jul 26, 2023
69a9ef0
feat: Add CalcualteFee in GRPC
ZigBalthazar Jul 26, 2023
dba8f7a
fix: debug grpc/gen/go
ZigBalthazar Jul 26, 2023
325f666
test: add test for calculateFee GRPC
ZigBalthazar Jul 26, 2023
e2bc6d0
fix: PR #590 bugs
ZigBalthazar Jul 29, 2023
b90dd2b
pull from main
ZigBalthazar Jul 29, 2023
4c68d71
Update wallet/client.go
ZigBalthazar Jul 29, 2023
a538726
fix: PR #590 bug fix
ZigBalthazar Jul 29, 2023
7b76303
fix: PR #601 bug fix
ZigBalthazar Jul 29, 2023
cf6fca8
fix: PR #601 bug fix
ZigBalthazar Jul 29, 2023
c61da40
fix: lint error
ZigBalthazar Jul 29, 2023
f8e6993
Merge branch 'main' of https://github.com/pactus-project/pactus
ZigBalthazar Jul 30, 2023
7fde2bd
fix: add test for CalcFee
ZigBalthazar Jul 30, 2023
133832b
fix: add test for CalcFee
ZigBalthazar Jul 30, 2023
c626e2f
fix: lint errors
ZigBalthazar Jul 30, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,12 @@ BUILD_LDFLAGS= -ldflags "-X github.com/pactus-project/pactus/version.build=`git

ifneq (,$(filter $(OS),Windows_NT MINGW64))
EXE = .exe
RM = del /q
else
RM = rm -rf
endif


all: build test

########################################
Expand Down Expand Up @@ -47,9 +51,9 @@ docker:
########################################
### proto
proto:
cd www/grpc/ && rm -rf gen && buf generate proto
cd www/grpc/ && $(RM) gen && buf generate proto

# Generate static assets for Swagger-UI
# Generate static assets for Swagger-UI
cd www/grpc/ && statik -m -f -src swagger-ui/

########################################
Expand Down
6 changes: 3 additions & 3 deletions execution/execution.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package execution
import (
"github.com/pactus-project/pactus/execution/executor"
"github.com/pactus-project/pactus/sandbox"
"github.com/pactus-project/pactus/types/param"
"github.com/pactus-project/pactus/types/tx"
"github.com/pactus-project/pactus/types/tx/payload"
"github.com/pactus-project/pactus/util"
Expand Down Expand Up @@ -122,16 +123,15 @@ func (exe *Execution) checkFee(trx *tx.Tx, sb sandbox.Sandbox) error {
return errors.Errorf(errors.ErrInvalidTx, "fee is wrong, expected: 0, got: %v", trx.Fee())
}
} else {
fee := calculateFee(trx.Payload().Value(), sb)
fee := CalculateFee(trx.Payload().Value(), sb.Params())
if trx.Fee() != fee {
return errors.Errorf(errors.ErrInvalidFee, "fee is wrong, expected: %v, got: %v", fee, trx.Fee())
}
}
return nil
}

func calculateFee(amt int64, sb sandbox.Sandbox) int64 {
params := sb.Params()
func CalculateFee(amt int64, params param.Params) int64 {
fee := int64(float64(amt) * params.FeeFraction)
fee = util.Max64(fee, params.MinimumFee)
fee = util.Min64(fee, params.MaximumFee)
Expand Down
2 changes: 1 addition & 1 deletion execution/execution_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ func TestFee(t *testing.T) {
assert.Equal(t, errors.Code(err), test.expectedErrCode,
"test %v failed. unexpected error", i)

assert.Equal(t, calculateFee(test.amount, sb), test.expectedFee,
assert.Equal(t, CalculateFee(test.amount, sb.Params()), test.expectedFee,
"test %v failed. invalid fee", i)
}
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ require (
github.com/multiformats/go-multiaddr v0.9.0
github.com/pelletier/go-toml v1.9.5
github.com/pkg/errors v0.9.1
github.com/prometheus/client_golang v1.14.0
github.com/rakyll/statik v0.1.7
github.com/sirupsen/logrus v1.9.0
github.com/stretchr/testify v1.8.2
Expand Down Expand Up @@ -110,6 +109,7 @@ require (
github.com/pbnjay/memory v0.0.0-20210728143218-7b4eea64cf58 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/polydawn/refmt v0.89.0 // indirect
github.com/prometheus/client_golang v1.14.0 // indirect
github.com/prometheus/client_model v0.3.0 // indirect
github.com/prometheus/common v0.42.0 // indirect
github.com/prometheus/procfs v0.9.0 // indirect
Expand Down
2 changes: 1 addition & 1 deletion node/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func NewNode(genDoc *genesis.Genesis, conf *config.Config,
// Initialize the logger
logger.InitLogger(conf.Logger)

logger.Info("You are running a pactus Blockchain",
logger.Info("You are running a pactus block chain",
ZigBalthazar marked this conversation as resolved.
Show resolved Hide resolved
"version", version.Version(),
"network", genDoc.ChainType())

Expand Down
2 changes: 2 additions & 0 deletions state/facade.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/pactus-project/pactus/types/block"
"github.com/pactus-project/pactus/types/param"
"github.com/pactus-project/pactus/types/tx"
"github.com/pactus-project/pactus/types/tx/payload"
"github.com/pactus-project/pactus/types/validator"
)

Expand Down Expand Up @@ -49,4 +50,5 @@ type Facade interface {
Params() param.Params
Close() error
Fingerprint() string
CalcFee(amount int64, payloadType payload.Type) (int64, error)
ZigBalthazar marked this conversation as resolved.
Show resolved Hide resolved
}
5 changes: 5 additions & 0 deletions state/mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"github.com/pactus-project/pactus/types/block"
"github.com/pactus-project/pactus/types/param"
"github.com/pactus-project/pactus/types/tx"
"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"
Expand Down Expand Up @@ -209,3 +210,7 @@
func (m *MockState) Params() param.Params {
return m.TestParams
}

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
ZigBalthazar marked this conversation as resolved.
Show resolved Hide resolved
}
21 changes: 21 additions & 0 deletions state/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"github.com/pactus-project/pactus/types/block"
"github.com/pactus-project/pactus/types/param"
"github.com/pactus-project/pactus/types/tx"
"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"
Expand Down Expand Up @@ -703,3 +704,23 @@ func (st *state) publishEvents(height uint32, block *block.Block) {
st.eventCh <- TxEvent
}
}

func (st *state) CalcFee(amount int64, payloadType payload.Type) (int64, error) {
switch payloadType {
case payload.PayloadTypeTransfer,
payload.PayloadTypeBond,
payload.PayloadTypeWithdraw:
{
return execution.CalculateFee(amount, st.params), nil
}

case payload.PayloadTypeUnbond,
payload.PayloadTypeSortition:
{
return 0, nil
}

default:
return 0, errors.Errorf(errors.ErrInvalidTx, "unexpected tx type: %v", payloadType)
}
}
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 @@
"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 @@
// 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)
ZigBalthazar marked this conversation as resolved.
Show resolved Hide resolved
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)
ZigBalthazar marked this conversation as resolved.
Show resolved Hide resolved

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)
}
}
10 changes: 10 additions & 0 deletions wallet/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"github.com/pactus-project/pactus/crypto"
"github.com/pactus-project/pactus/crypto/hash"
"github.com/pactus-project/pactus/types/tx"
"github.com/pactus-project/pactus/types/tx/payload"
pactus "github.com/pactus-project/pactus/www/grpc/gen/go"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
Expand Down Expand Up @@ -73,6 +74,7 @@
return hash.FromBytes(res.Id)
}

// TODO: check the return value type
func (c *grpcClient) getTransaction(id tx.ID) (*pactus.GetTransactionResponse, error) {
res, err := c.transactionClient.GetTransaction(context.Background(), &pactus.GetTransactionRequest{
Id: id.Bytes(),
Expand All @@ -84,3 +86,11 @@

return res, nil
}

func (c *grpcClient) getFee(amount int64, payloadType payload.Type) (int64, error) {
res, err := c.transactionClient.CalculateFee(context.Background(), &pactus.CalculateFeeRequest{Amount: amount, PayloadType: pactus.PayloadType(payloadType)})
ZigBalthazar marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
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
}
12 changes: 12 additions & 0 deletions wallet/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,18 @@ func (s *transactionServer) GetTransaction(_ context.Context,
return nil, nil
}

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

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

func (s *transactionServer) GetFee(_ context.Context,
ZigBalthazar marked this conversation as resolved.
Show resolved Hide resolved
_ *pactus.CalculateFeeRequest) (*pactus.CalculateFeeResponse, error) {

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

func (s *transactionServer) SendRawTransaction(_ context.Context,
req *pactus.SendRawTransactionRequest) (*pactus.SendRawTransactionResponse, error) {
trx, _ := tx.FromBytes(req.Data)
Expand Down
27 changes: 13 additions & 14 deletions wallet/tx_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,10 @@
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 Expand Up @@ -169,20 +172,16 @@
return nil
}

func (m *txBuilder) setFee() {
func (m *txBuilder) setFee() error {
if m.fee == 0 {
switch m.typ {
case payload.PayloadTypeTransfer,
payload.PayloadTypeBond,
payload.PayloadTypeWithdraw:
{
m.fee = calcFee(m.amount)
}

case payload.PayloadTypeUnbond:
{
m.fee = 0
}
if m.client == nil {
return ErrOffline

Check warning on line 178 in wallet/tx_builder.go

View check run for this annotation

Codecov / codecov/patch

wallet/tx_builder.go#L178

Added line #L178 was not covered by tests
}
fee, err := m.client.getFee(m.amount, m.typ)
if err != nil {
return err

Check warning on line 182 in wallet/tx_builder.go

View check run for this annotation

Codecov / codecov/patch

wallet/tx_builder.go#L182

Added line #L182 was not covered by tests
}
m.fee = fee
}
return nil
}
14 changes: 2 additions & 12 deletions wallet/wallet.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"github.com/pactus-project/pactus/crypto"
"github.com/pactus-project/pactus/crypto/bls"
"github.com/pactus-project/pactus/genesis"
"github.com/pactus-project/pactus/types/param"
"github.com/pactus-project/pactus/types/tx"
"github.com/pactus-project/pactus/types/tx/payload"
"github.com/pactus-project/pactus/util"
Expand Down Expand Up @@ -406,17 +405,8 @@ func (w *Wallet) BroadcastTransaction(trx *tx.Tx) (string, error) {
return id.String(), nil
}

func calcFee(amount int64) int64 {
params := param.DefaultParams() // TODO: Get parameter from the node
fee := int64(float64(amount) * params.FeeFraction)
fee = util.Max64(fee, params.MinimumFee)
fee = util.Min64(fee, params.MaximumFee)
return fee
}

// TODO: query fee from grpc client
func (w *Wallet) CalculateFee(amount int64) int64 {
return calcFee(amount)
func (w *Wallet) CalculateFee(amount int64, payloadType payload.Type) (int64, error) {
return w.client.getFee(amount, payloadType)
}

func (w *Wallet) UpdatePassword(oldPassword, newPassword string) error {
Expand Down
12 changes: 9 additions & 3 deletions wallet/wallet_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,9 @@ func TestMakeTransferTx(t *testing.T) {
assert.NoError(t, err)
assert.Equal(t, trx.Sequence(), seq+1)
assert.Equal(t, trx.Payload().Value(), amount)
assert.Equal(t, trx.Fee(), td.wallet.CalculateFee(amount))
fee, err := td.wallet.CalculateFee(amount, payload.PayloadTypeTransfer)
assert.NoError(t, err)
assert.Equal(t, trx.Fee(), fee)
assert.Equal(t, trx.Stamp(), lastBlockHash.Stamp())
})

Expand Down Expand Up @@ -363,7 +365,9 @@ func TestMakeBondTx(t *testing.T) {
assert.Equal(t, trx.Sequence(), seq+1)
assert.True(t, trx.Payload().(*payload.BondPayload).PublicKey.EqualsTo(receiver.PublicKey()))
assert.Equal(t, trx.Payload().Value(), amount)
assert.Equal(t, trx.Fee(), td.wallet.CalculateFee(amount))
fee, err := td.wallet.CalculateFee(amount, payload.PayloadTypeBond)
assert.NoError(t, err)
assert.Equal(t, trx.Fee(), fee)
assert.Equal(t, trx.Stamp(), lastBlockHash.Stamp())
})

Expand Down Expand Up @@ -533,7 +537,9 @@ func TestMakeWithdrawTx(t *testing.T) {
assert.NoError(t, err)
assert.Equal(t, trx.Sequence(), seq+1)
assert.Equal(t, trx.Payload().Value(), amount)
assert.Equal(t, trx.Fee(), td.wallet.CalculateFee(amount))
fee, err := td.wallet.CalculateFee(amount, payload.PayloadTypeWithdraw)
assert.NoError(t, err)
assert.Equal(t, trx.Fee(), fee)
assert.Equal(t, trx.Stamp(), lastBlockHash.Stamp())
})

Expand Down
Loading
Loading