Skip to content

592 fee model #595

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

Merged
merged 8 commits into from
Feb 19, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
2 changes: 2 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,8 @@ issues:
text: "copylocks: literal copies lock value"
linters:
- govet
- path: "(.+).go"
text: "type name will be used as"
- path: "(.+)_test.go"
text: "copylocks: range var tt copies lock:"
linters:
Expand Down
58 changes: 58 additions & 0 deletions api/client/GetTransactionMinimumFee/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package main

import (
"context"
"fmt"

"github.com/sirupsen/logrus"
log "github.com/sirupsen/logrus"
"github.com/spf13/viper"
rpc_model "github.com/zoobc/zoobc-core/common/model"
rpc_service "github.com/zoobc/zoobc-core/common/service"
"github.com/zoobc/zoobc-core/common/util"
"google.golang.org/grpc"
)

func main() {
var apiRPCPort int
if err := util.LoadConfig("../../../resource", "config", "toml"); err != nil {
logrus.Fatal(err)
} else {
apiRPCPort = viper.GetInt("apiRPCPort")
if apiRPCPort == 0 {
apiRPCPort = 8080
}
}

conn, err := grpc.Dial(fmt.Sprintf(":%d", apiRPCPort), grpc.WithInsecure())
if err != nil {
log.Fatalf("did not connect: %s", err)
}
defer conn.Close()

c := rpc_service.NewTransactionServiceClient(conn)

request := &rpc_model.GetTransactionMinimumFeeRequest{
TransactionBytes: []byte{
1, 0, 0, 0, 1, 191, 62, 75, 94, 0, 0, 0, 0, 44, 0, 0, 0, 122, 85, 107, 50, 109, 99, 103, 57, 118,
110, 81, 115, 102, 79, 111, 110, 49, 84, 45, 51, 102, 108, 55, 56, 80, 78, 106, 100, 109, 68, 55,
49, 66, 54, 86, 99, 45, 101, 72, 65, 56, 102, 79, 54, 44, 0, 0, 0, 79, 110, 69, 89, 122, 73, 45,
69, 77, 86, 54, 85, 84, 102, 111, 85, 69, 122, 112, 81, 85, 106, 107, 83, 108, 110, 113, 66, 56,
50, 45, 83, 121, 82, 78, 55, 52, 54, 57, 108, 74, 84, 87, 72, 1, 0, 0, 0, 0, 0, 0, 0, 8, 0, 0, 0,
100, 0, 0, 0, 0, 0, 0, 0, 44, 0, 0, 0, 79, 110, 69, 89, 122, 73, 45, 69, 77, 86, 54, 85, 84, 102,
111, 85, 69, 122, 112, 81, 85, 106, 107, 83, 108, 110, 113, 66, 56, 50, 45, 83, 121, 82, 78, 55,
52, 54, 57, 108, 74, 84, 87, 72, 1, 0, 0, 0, 0, 0, 0, 0, 141, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 151, 14, 69, 104, 198, 182, 113, 50, 53, 190, 105, 20, 164, 57, 16, 94, 89, 251, 35, 230,
145, 198, 189, 167, 222, 214, 208, 120, 229, 172, 155, 54, 85, 245, 19, 125, 3, 4, 11, 44, 65, 254,
148, 174, 117, 98, 16, 161, 149, 16, 4, 0, 153, 107, 84, 187, 8, 225, 103, 208, 126, 101, 17, 0,
},
}
response, err := c.GetTransactionMinimumFee(context.Background(), request)

if err != nil {
log.Fatalf("error calling rpc_service.GetTransactionMinimumFee: %s", err)
}

log.Printf("response from remote rpc_service.GetTransactionMinimumFee(%v): %v", request, response.GetFee())

}
17 changes: 17 additions & 0 deletions api/handler/transactionHandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,20 @@ func (th *TransactionHandler) PostTransaction(
Transaction: transaction,
}, nil
}

// GetTransactionMinimumFee handles request to get transaction's minimum fee
func (th *TransactionHandler) GetTransactionMinimumFee(
ctx context.Context,
req *model.GetTransactionMinimumFeeRequest,
) (*model.GetTransactionMinimumFeeResponse, error) {
var (
transactionFee *model.GetTransactionMinimumFeeResponse
err error
)
transactionFee, err = th.Service.GetTransactionMinimumFee(req)
if err != nil {
return nil, err
}

return transactionFee, nil
}
28 changes: 28 additions & 0 deletions api/service/transactionApiService.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ type (
GetTransaction(chaintype.ChainType, *model.GetTransactionRequest) (*model.Transaction, error)
GetTransactions(chaintype.ChainType, *model.GetTransactionsRequest) (*model.GetTransactionsResponse, error)
PostTransaction(chaintype.ChainType, *model.PostTransactionRequest) (*model.Transaction, error)
GetTransactionMinimumFee(request *model.GetTransactionMinimumFeeRequest) (
*model.GetTransactionMinimumFeeResponse, error,
)
}

// TransactionService represents struct of TransactionService
Expand Down Expand Up @@ -283,3 +286,28 @@ func (ts *TransactionService) PostTransaction(
// return parsed transaction
return tx, nil
}

func (ts *TransactionService) GetTransactionMinimumFee(req *model.GetTransactionMinimumFeeRequest) (
*model.GetTransactionMinimumFeeResponse, error,
) {
var (
txBytes = req.TransactionBytes
err error
)
tx, err := ts.TransactionUtil.ParseTransactionBytes(txBytes, true)
if err != nil {
return nil, status.Error(codes.Internal, err.Error())
}
// get the TypeAction object
txType, err := ts.ActionTypeSwitcher.GetTransactionType(tx)
if err != nil {
return nil, status.Error(codes.Internal, err.Error())
}
minFee, err := txType.GetMinimumFee()
if err != nil {
return nil, err
}
return &model.GetTransactionMinimumFeeResponse{
Fee: minFee,
}, nil
}
33 changes: 33 additions & 0 deletions common/fee/blockLifeTimeFeeModel.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package fee

import (
"math"

"github.com/zoobc/zoobc-core/common/model"
)

type (
// BlockLifeTimeFeeModel will calculate the transaction fee based on expected lifetime on the chain. `timeout` field
// must be present in the transaction body
BlockLifeTimeFeeModel struct {
blockPeriod int64
feePerBlockPeriod int64
}
)

func NewBlockLifeTimeFeeModel(
blockPeriod, feePerBlockPeriod int64,
) *BlockLifeTimeFeeModel {
return &BlockLifeTimeFeeModel{
blockPeriod: blockPeriod,
feePerBlockPeriod: feePerBlockPeriod,
}
}

func (blt *BlockLifeTimeFeeModel) CalculateTxMinimumFee(
txBody model.TransactionBodyInterface, escrow *model.Escrow,
) (int64, error) {
// timeout / blockPeriod result is ceil
fee := int64(math.Ceil(float64(escrow.Timeout)/float64(blt.blockPeriod))) * blt.feePerBlockPeriod
return fee, nil
}
91 changes: 91 additions & 0 deletions common/fee/blockLifeTimeFeeModel_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package fee

import (
"reflect"
"testing"

"github.com/zoobc/zoobc-core/common/constant"

"github.com/zoobc/zoobc-core/common/model"
)

func TestBlockLifeTimeFeeModel_CalculateTxMinimumFee(t *testing.T) {
type fields struct {
blockPeriod int64
feePerBlockPeriod int64
}
type args struct {
txBody model.TransactionBodyInterface
escrow *model.Escrow
}
tests := []struct {
name string
fields fields
args args
want int64
wantErr bool
}{
{
name: "CalculateTxMinimumFee-1",
fields: fields{
blockPeriod: 5,
feePerBlockPeriod: constant.OneZBC / 100,
},
args: args{
txBody: nil,
escrow: &model.Escrow{
Timeout: 17,
},
},
want: 4 * (constant.OneZBC / 100),
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
blt := &BlockLifeTimeFeeModel{
blockPeriod: tt.fields.blockPeriod,
feePerBlockPeriod: tt.fields.feePerBlockPeriod,
}
got, err := blt.CalculateTxMinimumFee(tt.args.txBody, tt.args.escrow)
if (err != nil) != tt.wantErr {
t.Errorf("CalculateTxMinimumFee() error = %v, wantErr %v", err, tt.wantErr)
return
}
if got != tt.want {
t.Errorf("CalculateTxMinimumFee() got = %v, want %v", got, tt.want)
}
})
}
}

func TestNewBlockLifeTimeFeeModel(t *testing.T) {
type args struct {
blockPeriod int64
feePerBlockPeriod int64
}
tests := []struct {
name string
args args
want *BlockLifeTimeFeeModel
}{
{
name: "NewBlockLifeTimeFeeModel-Success",
args: args{
blockPeriod: 0,
feePerBlockPeriod: 0,
},
want: &BlockLifeTimeFeeModel{
feePerBlockPeriod: 0,
blockPeriod: 0,
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := NewBlockLifeTimeFeeModel(tt.args.blockPeriod, tt.args.feePerBlockPeriod); !reflect.DeepEqual(got, tt.want) {
t.Errorf("NewBlockLifeTimeFeeModel() = %v, want %v", got, tt.want)
}
})
}
}
22 changes: 22 additions & 0 deletions common/fee/constantFeeModel.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package fee

import "github.com/zoobc/zoobc-core/common/model"

type (
// ConstantFeeModel is fee that'll be set as a constant number
ConstantFeeModel struct {
constantFee int64
}
)

func NewConstantFeeModel(constantFee int64) *ConstantFeeModel {
return &ConstantFeeModel{
constantFee: constantFee,
}
}

func (cfm *ConstantFeeModel) CalculateTxMinimumFee(
model.TransactionBodyInterface, *model.Escrow,
) (int64, error) {
return cfm.constantFee, nil
}
9 changes: 9 additions & 0 deletions common/fee/fee.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package fee

import "github.com/zoobc/zoobc-core/common/model"

type (
FeeModelInterface interface {
CalculateTxMinimumFee(txBody model.TransactionBodyInterface, escrow *model.Escrow) (int64, error)
}
)
Loading