Skip to content

Commit f8005b3

Browse files
andy-shi88astaphobia
authored andcommitted
146 receipt (#195)
* #146 add receipt object * #146 update sendblock function signature * #146 add sendBlockRequest object * #146 add receipt table * #146 add chaintype to send transaction message * #146 add receipt util * #146 pass secretphrase to p2p service * #146 refactored p2p and blockchain sync * #146 exposed part of peer to peer service information to client * #146 add sender public key to send block request parameter * #146 use public key instead of address for sender and receiver * #146 add sender public key to send transaction request message * #146 use new to instantiate p2p handler * #146 do not broadcast block for genesis * #146 increase node hardware information stream delay * #146 rename receipt table name to node_receipt
1 parent 44ea5e0 commit f8005b3

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+2168
-3706
lines changed

api/api.go

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,10 @@ import (
77
"net/http"
88

99
"github.com/grpc-ecosystem/grpc-gateway/runtime"
10-
"github.com/spf13/viper"
1110
"github.com/zoobc/zoobc-core/common/interceptor"
11+
"github.com/zoobc/zoobc-core/observer"
12+
13+
"github.com/spf13/viper"
1214

1315
"github.com/zoobc/zoobc-core/common/chaintype"
1416
coreService "github.com/zoobc/zoobc-core/core/service"
@@ -23,7 +25,6 @@ import (
2325
"github.com/zoobc/zoobc-core/common/query"
2426
rpcService "github.com/zoobc/zoobc-core/common/service"
2527
"github.com/zoobc/zoobc-core/common/util"
26-
"github.com/zoobc/zoobc-core/observer"
2728
"google.golang.org/grpc"
2829
)
2930

@@ -42,7 +43,7 @@ func init() {
4243
}
4344
}
4445

45-
func startGrpcServer(port int, queryExecutor query.ExecutorInterface, p2pHostService p2p.ServiceInterface,
46+
func startGrpcServer(port int, queryExecutor query.ExecutorInterface, p2pHostService p2p.Peer2PeerServiceInterface,
4647
blockServices map[int32]coreService.BlockServiceInterface, ownerAccountAddress, nodefilePath string) {
4748
grpcServer := grpc.NewServer(
4849
grpc.UnaryInterceptor(interceptor.NewServerInterceptor(apiLogger, ownerAccountAddress)),
@@ -57,8 +58,10 @@ func startGrpcServer(port int, queryExecutor query.ExecutorInterface, p2pHostSer
5758
query.NewMempoolQuery(&chaintype.MainChain{}),
5859
actionTypeSwitcher,
5960
query.NewAccountBalanceQuery(),
61+
crypto.NewSignature(),
6062
query.NewTransactionQuery(&chaintype.MainChain{}),
61-
observer.NewObserver())
63+
observer.NewObserver(),
64+
)
6265
serv, err := net.Listen("tcp", fmt.Sprintf(":%d", port))
6366
if err != nil {
6467
apiLogger.Fatalf("failed to listen: %v\n", err)
@@ -79,7 +82,7 @@ func startGrpcServer(port int, queryExecutor query.ExecutorInterface, p2pHostSer
7982
crypto.NewSignature(),
8083
actionTypeSwitcher,
8184
mempoolService,
82-
apiLogger,
85+
observer.NewObserver(),
8386
),
8487
})
8588
// Set GRPC handler for Transactions requests
@@ -115,7 +118,7 @@ func startGrpcServer(port int, queryExecutor query.ExecutorInterface, p2pHostSer
115118
}
116119

117120
// Start starts api servers in the given port and passing query executor
118-
func Start(grpcPort, restPort int, queryExecutor query.ExecutorInterface, p2pHostService p2p.ServiceInterface,
121+
func Start(grpcPort, restPort int, queryExecutor query.ExecutorInterface, p2pHostService p2p.Peer2PeerServiceInterface,
119122
blockServices map[int32]coreService.BlockServiceInterface, ownerAccountAddress, nodefilePath string) {
120123
startGrpcServer(grpcPort, queryExecutor, p2pHostService, blockServices, ownerAccountAddress, nodefilePath)
121124
if restPort > 0 { // only start proxy service if apiHTTPPort set with value > 0

api/handler/nodeHardwareHandler.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,6 @@ func (nhh *NodeHardwareHandler) GetNodeHardware(
3232
if err != nil {
3333
return err // close connection if sending response to client result in error
3434
}
35-
time.Sleep(500 * time.Millisecond)
35+
time.Sleep(5 * time.Second)
3636
}
3737
}

api/service/hostApiService.go

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,14 @@ type (
1616
HostService struct {
1717
Query query.ExecutorInterface
1818
BlockServices map[int32]coreService.BlockServiceInterface
19-
P2pService p2p.ServiceInterface
19+
P2pService p2p.Peer2PeerServiceInterface
2020
}
2121
)
2222

2323
var hostServiceInstance *HostService
2424

25-
// NewHostService create a singleton instance of HostService
26-
func NewHostService(queryExecutor query.ExecutorInterface, p2pService p2p.ServiceInterface,
25+
// NewHostService create a singleton instance of PeerExplorer
26+
func NewHostService(queryExecutor query.ExecutorInterface, p2pService p2p.Peer2PeerServiceInterface,
2727
blockServices map[int32]coreService.BlockServiceInterface) HostServiceInterface {
2828
if hostServiceInstance == nil {
2929
hostServiceInstance = &HostService{
@@ -36,7 +36,7 @@ func NewHostService(queryExecutor query.ExecutorInterface, p2pService p2p.Servic
3636
}
3737

3838
func (hs *HostService) GetHostInfo() (*model.HostInfo, error) {
39-
chainStatuses := []*model.ChainStatus{}
39+
var chainStatuses []*model.ChainStatus
4040
for chainType, blockService := range hs.BlockServices {
4141
lastBlock, err := blockService.GetLastBlock()
4242
if lastBlock == nil || err != nil {
@@ -49,15 +49,14 @@ func (hs *HostService) GetHostInfo() (*model.HostInfo, error) {
4949
})
5050
}
5151
return &model.HostInfo{
52-
Host: hs.P2pService.GetHostInstance(),
52+
Host: hs.P2pService.GetHostInfo(),
5353
ChainStatuses: chainStatuses,
5454
}, nil
5555
}
5656

5757
func (hs *HostService) GetHostPeers() (*model.GetHostPeersResponse, error) {
58-
host := hs.P2pService.GetHostInstance()
5958
return &model.GetHostPeersResponse{
60-
ResolvedPeers: host.GetResolvedPeers(),
61-
UnresolvedPeers: host.GetUnresolvedPeers(),
59+
ResolvedPeers: hs.P2pService.GetResolvedPeers(),
60+
UnresolvedPeers: hs.P2pService.GetUnresolvedPeers(),
6261
}, nil
6362
}

api/service/transactionApiService.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ import (
55
"errors"
66
"time"
77

8-
"github.com/sirupsen/logrus"
8+
"github.com/zoobc/zoobc-core/observer"
9+
910
"github.com/zoobc/zoobc-core/common/blocker"
1011
"github.com/zoobc/zoobc-core/common/chaintype"
1112
"github.com/zoobc/zoobc-core/common/crypto"
@@ -30,7 +31,7 @@ type (
3031
Signature crypto.SignatureInterface
3132
ActionTypeSwitcher transaction.TypeActionSwitcher
3233
MempoolService service.MempoolServiceInterface
33-
Log *logrus.Logger
34+
Observer *observer.Observer
3435
}
3536
)
3637

@@ -42,15 +43,15 @@ func NewTransactionService(
4243
signature crypto.SignatureInterface,
4344
txTypeSwitcher transaction.TypeActionSwitcher,
4445
mempoolService service.MempoolServiceInterface,
45-
log *logrus.Logger,
46+
observer *observer.Observer,
4647
) *TransactionService {
4748
if transactionServiceInstance == nil {
4849
transactionServiceInstance = &TransactionService{
4950
Query: queryExecutor,
5051
Signature: signature,
5152
ActionTypeSwitcher: txTypeSwitcher,
5253
MempoolService: mempoolService,
53-
Log: log,
54+
Observer: observer,
5455
}
5556
}
5657
return transactionServiceInstance
@@ -208,6 +209,7 @@ func (ts *TransactionService) PostTransaction(
208209
return nil, err
209210
}
210211

212+
ts.Observer.Notify(observer.TransactionAdded, mpTx.GetTransactionBytes(), chaintype)
211213
// return parsed transaction
212214
return tx, nil
213215
}

api/service/transactionApiService_test.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ package service
55
import (
66
"database/sql"
77
"errors"
8+
"github.com/zoobc/zoobc-core/observer"
89
"reflect"
910
"strings"
1011
"testing"
@@ -204,6 +205,7 @@ func TestTransactionService_PostTransaction(t *testing.T) {
204205
ActionTypeSwitcher transaction.TypeActionSwitcher
205206
MempoolService service.MempoolServiceInterface
206207
Log *logrus.Logger
208+
Observer *observer.Observer
207209
}
208210
type args struct {
209211
chaintype chaintype.ChainType
@@ -411,6 +413,7 @@ func TestTransactionService_PostTransaction(t *testing.T) {
411413
Query: &mockTransactionExecutorSuccess{},
412414
ActionTypeSwitcher: &mockTypeSwitcherSuccess{},
413415
MempoolService: &mockMempoolServiceSuccess{},
416+
Observer: observer.NewObserver(),
414417
Log: mockLog,
415418
},
416419
args: args{
@@ -459,7 +462,7 @@ func TestTransactionService_PostTransaction(t *testing.T) {
459462
Signature: tt.fields.Signature,
460463
ActionTypeSwitcher: tt.fields.ActionTypeSwitcher,
461464
MempoolService: tt.fields.MempoolService,
462-
Log: tt.fields.Log,
465+
Observer: tt.fields.Observer,
463466
}
464467
got, err := ts.PostTransaction(tt.args.chaintype, tt.args.req)
465468
if (err != nil) != tt.wantErr {

common/constant/receipt.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package constant
2+
3+
const (
4+
ReceiptDatumTypeBlock = uint32(1)
5+
ReceiptDatumTypeTransaction = uint32(2)
6+
)

common/database/migration.go

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,8 @@ func (m *Migration) Init() error {
122122
);`,
123123
`
124124
ALTER TABLE "transaction"
125-
ADD COLUMN "transaction_index" INTEGER;`,
125+
ADD COLUMN "transaction_index" INTEGER AFTER "version"
126+
`,
126127
`
127128
CREATE TABLE IF NOT EXISTS "participation_score"(
128129
"node_id" INTEGER,
@@ -131,6 +132,18 @@ func (m *Migration) Init() error {
131132
"height" INTEGER,
132133
PRIMARY KEY("node_id", "height")
133134
);`,
135+
`
136+
CREATE TABLE IF NOT EXISTS "node_receipt" (
137+
"sender_public_key" BLOB,
138+
"recipient_public_key" BLOB,
139+
"datum_type" INTEGER,
140+
"datum_hash" BLOB,
141+
"reference_block_height" INTEGER,
142+
"reference_block_hash" BLOB,
143+
"receipt_merkle_root" BLOB,
144+
"recipient_signature" BLOB
145+
)
146+
`,
134147
}
135148
return nil
136149
}

common/model/block.pb.go

Lines changed: 97 additions & 38 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)