Skip to content

40 post transaction #96

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 30 commits into from
Jul 29, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
2032196
#40 add dev tools for post tx
andy-shi88 Jul 26, 2019
f3a1ba3
#40 post transaction schema update
andy-shi88 Jul 26, 2019
31a16f3
#40 remove unneeded code
andy-shi88 Jul 26, 2019
b0919f3
#40 fix mempool selection error : expiry comparison is wrong
andy-shi88 Jul 26, 2019
bba4eca
#40 assign tx height and block id before applying
andy-shi88 Jul 26, 2019
6e0bfbc
#40 add post transaction handler
andy-shi88 Jul 26, 2019
645af02
#40 close unclosed db connection
andy-shi88 Jul 26, 2019
1fd2852
#40 update tx type switcher for send money
andy-shi88 Jul 26, 2019
a408db3
#40 post transaction schema update
andy-shi88 Jul 26, 2019
7409469
#40 update account balance versioning
andy-shi88 Jul 26, 2019
e5ca79a
#40 update transaction service instantiation
andy-shi88 Jul 26, 2019
5f761c7
#40 add undo apply unconfirmed
andy-shi88 Jul 26, 2019
cc0b79d
#40 fix account query argument return
andy-shi88 Jul 26, 2019
93a91b0
#40 update schema
andy-shi88 Jul 26, 2019
6adf36d
Merge branch 'develop' of github.com:zoobc/zoobc-core into 40-post-tr…
andy-shi88 Jul 26, 2019
8ec1a04
#40 fix lint interfacer
andy-shi88 Jul 26, 2019
49188ec
#40 delegate validation to mempool service and better error handling
andy-shi88 Jul 26, 2019
e38be5b
#40 inject log to transaction api service
andy-shi88 Jul 26, 2019
81381ff
#40 more specific cmd log file name
andy-shi88 Jul 26, 2019
1f452ad
#40 deduct fee
andy-shi88 Jul 26, 2019
7870c05
#40 add fee to tx type
andy-shi88 Jul 26, 2019
412b91b
#40 execute mempool operation in db transaction
andy-shi88 Jul 26, 2019
e97024a
#40 validate before undo unconfirmed
andy-shi88 Jul 29, 2019
7f0a9e6
#40 cover more account balance query test
andy-shi88 Jul 29, 2019
9398a14
#40 add post transaction response
andy-shi88 Jul 29, 2019
391c66a
#40 update schema commit
andy-shi88 Jul 29, 2019
69e2507
#40 update schema commit
andy-shi88 Jul 29, 2019
38833bc
#40 fix conflict
andy-shi88 Jul 29, 2019
0d36db9
#40 update port
andy-shi88 Jul 29, 2019
3601fe7
#40 add description for commented code
andy-shi88 Jul 29, 2019
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
27 changes: 24 additions & 3 deletions api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ import (
"fmt"
"net"

"github.com/zoobc/zoobc-core/common/chaintype"
core_service "github.com/zoobc/zoobc-core/core/service"

"github.com/zoobc/zoobc-core/common/crypto"
"github.com/zoobc/zoobc-core/common/transaction"

"github.com/sirupsen/logrus"
"github.com/zoobc/zoobc-core/api/handler"
"github.com/zoobc/zoobc-core/api/service"
Expand All @@ -25,10 +31,19 @@ func init() {
}
}

func startGrpcServer(port int, queryExecutor *query.Executor, p2pHostService contract.P2PType) {
func startGrpcServer(port int, queryExecutor query.ExecutorInterface, p2pHostService contract.P2PType) {
grpcServer := grpc.NewServer(
grpc.UnaryInterceptor(util.NewServerInterceptor(apiLogger)),
)
actionTypeSwitcher := &transaction.TypeSwitcher{
Executor: queryExecutor,
}
mempoolService := core_service.NewMempoolService(
&chaintype.MainChain{},
queryExecutor,
query.NewMempoolQuery(&chaintype.MainChain{}),
actionTypeSwitcher,
query.NewAccountBalanceQuery())

serv, err := net.Listen("tcp", fmt.Sprintf(":%d", port))
if err != nil {
Expand All @@ -43,7 +58,13 @@ func startGrpcServer(port int, queryExecutor *query.Executor, p2pHostService con

// Set GRPC handler for Transactions requests
rpc_service.RegisterTransactionServiceServer(grpcServer, &handler.TransactionHandler{
Service: service.NewTransactionService(queryExecutor),
Service: service.NewTransactionService(
queryExecutor,
&crypto.Signature{},
actionTypeSwitcher,
mempoolService,
apiLogger,
),
})

// Set GRPC handler for Transactions requests
Expand All @@ -62,6 +83,6 @@ func startGrpcServer(port int, queryExecutor *query.Executor, p2pHostService con
}

// Start starts api servers in the given port and passing query executor
func Start(grpcPort, restPort int, queryExecutor *query.Executor, p2pHostService contract.P2PType) {
func Start(grpcPort, restPort int, queryExecutor query.ExecutorInterface, p2pHostService contract.P2PType) {
startGrpcServer(grpcPort, queryExecutor, p2pHostService)
}
47 changes: 47 additions & 0 deletions api/client/PostTransaction/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package main

import (
"context"

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

func main() {
conn, err := grpc.Dial(":3001", grpc.WithInsecure())
if err != nil {
log.Fatalf("did not connect: %s", err)
}
defer conn.Close()

c := rpc_service.NewTransactionServiceClient(conn)

response, err := c.PostTransaction(context.Background(), &rpc_model.PostTransactionRequest{
// TransactionBytes: []byte{ // keep this to test multiple transaction in single block.
// 1, 0, 1, 82, 108, 58, 93, 0, 0, 0, 0, 0, 0, 66, 67, 90, 110, 83, 102, 113, 112, 80, 53, 116, 113, 70, 81, 108, 77, 84, 89,
// 107, 68, 101, 66, 86, 70, 87, 110, 98, 121, 86, 75, 55, 118, 76, 114, 53, 79, 82, 70, 112, 84, 106, 103, 116, 78, 0, 0, 66,
// 67, 90, 75, 76, 118, 103, 85, 89, 90, 49, 75, 75, 120, 45, 106, 116, 70, 57, 75, 111, 74, 115, 107, 106, 86, 80, 118, 66, 57,
// 106, 112, 73, 106, 102, 122, 122, 73, 54, 122, 68, 87, 48, 74, 1, 0, 0, 0, 0, 0, 0, 0, 8, 0, 0, 0, 16, 39, 0, 0, 0, 0, 0, 0, 140,
// 134, 217, 86, 232, 251, 81, 174, 86, 44, 221, 44, 226, 73, 245, 19, 170, 94, 47, 160, 53, 20, 225, 192, 19, 200, 196, 217, 96, 64,
// 66, 6, 146, 16, 61, 104, 106, 112, 122, 96, 233, 224, 208, 119, 245, 148, 60, 9, 131, 211, 110, 68, 167, 115, 243, 251, 90, 64, 234,
// 66, 108, 30, 116, 9,
// },
TransactionBytes: []byte{
1, 0, 1, 53, 119, 58, 93, 0, 0, 0, 0, 0, 0, 66, 67, 90, 110, 83, 102, 113, 112, 80, 53, 116, 113, 70, 81, 108, 77, 84, 89, 107, 68,
101, 66, 86, 70, 87, 110, 98, 121, 86, 75, 55, 118, 76, 114, 53, 79, 82, 70, 112, 84, 106, 103, 116, 78, 0, 0, 66, 67, 90, 75, 76,
118, 103, 85, 89, 90, 49, 75, 75, 120, 45, 106, 116, 70, 57, 75, 111, 74, 115, 107, 106, 86, 80, 118, 66, 57, 106, 112, 73, 106,
102, 122, 122, 73, 54, 122, 68, 87, 48, 74, 1, 0, 0, 0, 0, 0, 0, 0, 8, 0, 0, 0, 16, 39, 0, 0, 0, 0, 0, 0, 32, 85, 34, 198, 89, 78,
166, 142, 59, 148, 243, 133, 69, 66, 123, 219, 2, 3, 229, 172, 221, 35, 185, 208, 43, 44, 172, 96, 166, 116, 205, 93, 78, 194, 153,
95, 243, 145, 108, 96, 42, 6, 186, 128, 59, 117, 83, 196, 26, 9, 15, 157, 215, 108, 180, 35, 195, 100, 7, 142, 47, 96, 108, 10,
},
})

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

log.Printf("response from remote rpc_service.PostTransaction(): %s", response)

}
13 changes: 13 additions & 0 deletions api/handler/transactionHandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,16 @@ func (th *TransactionHandler) GetTransactions(ctx context.Context,

return response, nil
}

// PostTransaction handle transaction submitted by client
func (th *TransactionHandler) PostTransaction(ctx context.Context,
req *model.PostTransactionRequest) (*model.PostTransactionResponse, error) {
chainType := chaintype.GetChainType(0)
transaction, err := th.Service.PostTransaction(chainType, req)
if err != nil {
return nil, err
}
return &model.PostTransactionResponse{
Transaction: transaction,
}, nil
}
91 changes: 84 additions & 7 deletions api/service/transactionApiService.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,16 @@ package service
import (
"database/sql"
"errors"
"fmt"
"time"

"github.com/sirupsen/logrus"

"github.com/zoobc/zoobc-core/common/transaction"
"github.com/zoobc/zoobc-core/core/service"

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

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

"github.com/zoobc/zoobc-core/common/contract"
"github.com/zoobc/zoobc-core/common/model"
Expand All @@ -15,20 +24,33 @@ type (
TransactionServiceInterface interface {
GetTransaction(contract.ChainType, *model.GetTransactionRequest) (*model.Transaction, error)
GetTransactions(contract.ChainType, *model.GetTransactionsRequest) (*model.GetTransactionsResponse, error)
PostTransaction(contract.ChainType, *model.PostTransactionRequest) (*model.Transaction, error)
}

// TransactionService represents struct of TransactionService
TransactionService struct {
Query *query.Executor
Query query.ExecutorInterface
Signature crypto.SignatureInterface
ActionTypeSwitcher transaction.TypeActionSwitcher
MempoolService service.MempoolServiceInterface
Log *logrus.Logger
}
)

var transactionServiceInstance *TransactionService

// NewTransactionService creates a singleton instance of TransactionService
func NewTransactionService(queryExecutor *query.Executor) *TransactionService {
func NewTransactionService(queryExecutor query.ExecutorInterface, signature crypto.SignatureInterface,
txTypeSwitcher transaction.TypeActionSwitcher, mempoolService service.MempoolServiceInterface,
log *logrus.Logger) *TransactionService {
if transactionServiceInstance == nil {
transactionServiceInstance = &TransactionService{Query: queryExecutor}
transactionServiceInstance = &TransactionService{
Query: queryExecutor,
Signature: signature,
ActionTypeSwitcher: txTypeSwitcher,
MempoolService: mempoolService,
Log: log,
}
}
return transactionServiceInstance
}
Expand All @@ -44,7 +66,6 @@ func (ts *TransactionService) GetTransaction(chainType contract.ChainType,
txQuery := query.NewTransactionQuery(chainType)
rows, err = ts.Query.ExecuteSelect(txQuery.GetTransaction(params.ID))
if err != nil {
fmt.Printf("GetTransaction fails %v\n", err)
return nil, err
}
defer rows.Close()
Expand All @@ -70,15 +91,13 @@ func (ts *TransactionService) GetTransactions(chainType contract.ChainType,
selectQuery := txQuery.GetTransactions(params.Limit, params.Offset)
rows, err = ts.Query.ExecuteSelect(selectQuery)
if err != nil {
fmt.Printf("GetTransactions fails %v\n", err)
return nil, err
}
defer rows.Close()
txs = txQuery.BuildModel(txs, rows)

rows2, err = ts.Query.ExecuteSelect(query.GetTotalRecordOfSelect(selectQuery))
if err != nil {
fmt.Printf("GetTransactions total records fails %v\n", err)
return nil, err
}
defer rows2.Close()
Expand All @@ -100,3 +119,61 @@ func (ts *TransactionService) GetTransactions(chainType contract.ChainType,
Transactions: txs,
}, nil
}

func (ts *TransactionService) PostTransaction(chaintype contract.ChainType, req *model.PostTransactionRequest) (*model.Transaction,
error) {
txBytes := req.TransactionBytes
// get unsigned bytes
tx, err := util.ParseTransactionBytes(txBytes, true)
if err != nil {
return nil, err
}
// Validate Tx
txType := ts.ActionTypeSwitcher.GetTransactionType(tx)

// Save to mempool
mpTx := &model.MempoolTransaction{
FeePerByte: 0,
ID: tx.ID,
TransactionBytes: txBytes,
ArrivalTimestamp: time.Now().Unix(),
}
if err := ts.MempoolService.ValidateMempoolTransaction(mpTx); err != nil {
ts.Log.Warnf("Invalid transaction submitted: %v", err)
return nil, err
}
// Apply Unconfirmed
err = ts.Query.BeginTx()
if err != nil {
ts.Log.Warnf("error opening db transaction %v", err)
return nil, err
}
err = txType.ApplyUnconfirmed()
if err != nil {
ts.Log.Warnf("fail ApplyUnconfirmed tx: %v", err)
errRollback := ts.Query.RollbackTx()
if errRollback != nil {
ts.Log.Warnf("error rolling back db transaction %v", errRollback)
return nil, errRollback
}
return nil, err
}
err = ts.MempoolService.AddMempoolTransaction(mpTx)
if err != nil {
ts.Log.Warnf("error AddMempoolTransaction: %v", err)
errRollback := ts.Query.RollbackTx()
if errRollback != nil {
ts.Log.Warnf("error rolling back db transaction %v", errRollback)
return nil, err
}
return nil, err
}
err = ts.Query.CommitTx()
if err != nil {
ts.Log.Warnf("error committing db transaction: %v", err)
return nil, err
}

// return parsed transaction
return tx, nil
}
Loading