Skip to content

Commit 0c8b65e

Browse files
capt4ceandy-shi88
authored andcommitted
72 implements download blockchain (#154)
* adds download blockchain mechanism * fixes download blockchain mechanism and adds unit basic unit test * regenerates files from proto * finalizing the download blockchain mechanism * adds more tests to the download blockchain * fixes `getPeerCommonBlockID` and the test * changes the ID cases * updates protobuf generated files * performs changes * changes the blockchainSync directory name case
1 parent 49924ad commit 0c8b65e

Some content is hidden

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

49 files changed

+2609
-403
lines changed

Gopkg.lock

Lines changed: 1 addition & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

api/api.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,14 @@ import (
66

77
"github.com/zoobc/zoobc-core/common/chaintype"
88
coreService "github.com/zoobc/zoobc-core/core/service"
9+
p2p "github.com/zoobc/zoobc-core/p2p"
910

1011
"github.com/zoobc/zoobc-core/common/crypto"
1112
"github.com/zoobc/zoobc-core/common/transaction"
1213

1314
"github.com/sirupsen/logrus"
1415
"github.com/zoobc/zoobc-core/api/handler"
1516
"github.com/zoobc/zoobc-core/api/service"
16-
"github.com/zoobc/zoobc-core/common/contract"
1717
"github.com/zoobc/zoobc-core/common/query"
1818
rpcService "github.com/zoobc/zoobc-core/common/service"
1919
"github.com/zoobc/zoobc-core/common/util"
@@ -32,7 +32,8 @@ func init() {
3232
}
3333
}
3434

35-
func startGrpcServer(port int, queryExecutor query.ExecutorInterface, p2pHostService contract.P2PType) {
35+
func startGrpcServer(port int, queryExecutor query.ExecutorInterface, p2pHostService p2p.ServiceInterface,
36+
blockServices map[int32]coreService.BlockServiceInterface) {
3637
grpcServer := grpc.NewServer(
3738
grpc.UnaryInterceptor(util.NewServerInterceptor(apiLogger)),
3839
)
@@ -71,8 +72,7 @@ func startGrpcServer(port int, queryExecutor query.ExecutorInterface, p2pHostSer
7172
})
7273
// Set GRPC handler for Transactions requests
7374
rpcService.RegisterHostServiceServer(grpcServer, &handler.HostHandler{
74-
Service: service.NewHostService(queryExecutor),
75-
P2pHostService: p2pHostService,
75+
Service: service.NewHostService(queryExecutor, p2pHostService, blockServices),
7676
})
7777
// Set GRPC handler for account balance requests
7878
rpcService.RegisterAccountBalanceServiceServer(grpcServer, &handler.AccountBalanceHandler{
@@ -96,6 +96,7 @@ func startGrpcServer(port int, queryExecutor query.ExecutorInterface, p2pHostSer
9696
}
9797

9898
// Start starts api servers in the given port and passing query executor
99-
func Start(grpcPort, restPort int, queryExecutor query.ExecutorInterface, p2pHostService contract.P2PType) {
100-
startGrpcServer(grpcPort, queryExecutor, p2pHostService)
99+
func Start(grpcPort, restPort int, queryExecutor query.ExecutorInterface, p2pHostService p2p.ServiceInterface,
100+
blockServices map[int32]coreService.BlockServiceInterface) {
101+
startGrpcServer(grpcPort, queryExecutor, p2pHostService, blockServices)
101102
}

api/client/GetBlockByID/client.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import (
1010
)
1111

1212
func main() {
13-
conn, err := grpc.Dial(":8000", grpc.WithInsecure())
13+
conn, err := grpc.Dial(":3001", grpc.WithInsecure())
1414
if err != nil {
1515
log.Fatalf("did not connect: %s", err)
1616
}
@@ -19,7 +19,7 @@ func main() {
1919
c := rpc_service.NewBlockServiceClient(conn)
2020

2121
response, err := c.GetBlock(context.Background(), &rpc_model.GetBlockRequest{
22-
ID: -7639577553511789811,
22+
ID: -8875736238633164302,
2323
})
2424

2525
if err != nil {

api/client/GetBlocks/client.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import (
1010
)
1111

1212
func main() {
13-
conn, err := grpc.Dial(":8000", grpc.WithInsecure())
13+
conn, err := grpc.Dial(":3001", grpc.WithInsecure())
1414
if err != nil {
1515
log.Fatalf("did not connect: %s", err)
1616
}
@@ -19,8 +19,9 @@ func main() {
1919
c := rpc_service.NewBlockServiceClient(conn)
2020

2121
response, err := c.GetBlocks(context.Background(), &rpc_model.GetBlocksRequest{
22-
Limit: 3,
23-
Height: 12,
22+
ChainType: 0,
23+
Limit: 3,
24+
Height: 1,
2425
})
2526

2627
if err != nil {

api/handler/blockHandler.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,10 @@ type BlockHandler struct {
1515

1616
// GetBlock handles request to get data of a single Block
1717
func (bs *BlockHandler) GetBlock(ctx context.Context, req *model.GetBlockRequest) (*model.Block, error) {
18-
var blockResponse *model.Block
19-
var err error
18+
var (
19+
blockResponse *model.Block
20+
err error
21+
)
2022
chainType := chaintype.GetChainType(req.ChainType)
2123
if req.ID != 0 {
2224
blockResponse, err = bs.Service.GetBlockByID(chainType, req.ID)

api/handler/hostHandler.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,13 @@ import (
44
"context"
55

66
"github.com/zoobc/zoobc-core/api/service"
7-
"github.com/zoobc/zoobc-core/common/contract"
87
"github.com/zoobc/zoobc-core/common/model"
98
)
109

1110
type HostHandler struct {
12-
Service service.HostServiceInterface
13-
P2pHostService contract.P2PType
11+
Service service.HostServiceInterface
1412
}
1513

16-
func (hh *HostHandler) GetHostInfo(ctx context.Context, req *model.Empty) (*model.Host, error) {
17-
return hh.P2pHostService.GetHostInstance(), nil
14+
func (hh *HostHandler) GetHostInfo(ctx context.Context, req *model.Empty) (*model.HostInfo, error) {
15+
return hh.Service.GetHostInfo()
1816
}

api/service/hostApiService.go

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,27 +3,52 @@ package service
33
import (
44
"github.com/zoobc/zoobc-core/common/model"
55
"github.com/zoobc/zoobc-core/common/query"
6+
coreService "github.com/zoobc/zoobc-core/core/service"
7+
"github.com/zoobc/zoobc-core/p2p"
68
)
79

810
type (
911
HostServiceInterface interface {
12+
GetHostInfo() (*model.HostInfo, error)
1013
}
1114

1215
HostService struct {
13-
Query query.ExecutorInterface
16+
Query query.ExecutorInterface
17+
BlockServices map[int32]coreService.BlockServiceInterface
18+
P2pService p2p.ServiceInterface
1419
}
1520
)
1621

1722
var hostServiceInstance *HostService
1823

1924
// NewHostService create a singleton instance of HostService
20-
func NewHostService(queryExecutor query.ExecutorInterface) *HostService {
25+
func NewHostService(queryExecutor query.ExecutorInterface, p2pService p2p.ServiceInterface,
26+
blockServices map[int32]coreService.BlockServiceInterface) HostServiceInterface {
2127
if hostServiceInstance == nil {
22-
hostServiceInstance = &HostService{Query: queryExecutor}
28+
hostServiceInstance = &HostService{
29+
Query: queryExecutor,
30+
P2pService: p2pService,
31+
BlockServices: blockServices,
32+
}
2333
}
2434
return hostServiceInstance
2535
}
2636

27-
func (*HostService) GetHostInfo() *model.Host {
28-
return &model.Host{}
37+
func (hs *HostService) GetHostInfo() (*model.HostInfo, error) {
38+
chainStatuses := []*model.ChainStatus{}
39+
for chainType, blockService := range hs.BlockServices {
40+
lastBlock, err := blockService.GetLastBlock()
41+
if lastBlock == nil || err != nil {
42+
continue
43+
}
44+
chainStatuses = append(chainStatuses, &model.ChainStatus{
45+
ChainType: chainType,
46+
Height: lastBlock.Height,
47+
LastBlock: lastBlock,
48+
})
49+
}
50+
return &model.HostInfo{
51+
Host: hs.P2pService.GetHostInstance(),
52+
ChainStatuses: chainStatuses,
53+
}, nil
2954
}

common/blocker/blocker.go

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,33 @@
11
package blocker
22

3+
import "fmt"
4+
35
type (
4-
TypeBlocker int
6+
TypeBlocker string
57

6-
blocker struct {
8+
Blocker struct {
79
Type TypeBlocker
810
Message string
911
}
1012
)
1113

1214
var (
13-
DBErr TypeBlocker = 1
14-
BlockErr TypeBlocker = 2
15-
AppErr TypeBlocker = 3
16-
AuthErr TypeBlocker = 4
17-
ValidationErr TypeBlocker = 5
15+
DBErr TypeBlocker = "DBErr"
16+
BlockErr TypeBlocker = "BlockErr"
17+
BlockNotFoundErr TypeBlocker = "BlockNotFoundErr"
18+
RequestParameterErr TypeBlocker = "RequestParameterErr"
19+
AppErr TypeBlocker = "AppErr"
20+
AuthErr TypeBlocker = "AuthErr"
21+
ValidationErr TypeBlocker = "ValidationErr"
1822
)
1923

2024
func NewBlocker(typeBlocker TypeBlocker, message string) error {
21-
return blocker{
25+
return Blocker{
2226
Type: typeBlocker,
2327
Message: message,
2428
}
2529
}
2630

27-
func (e blocker) Error() string {
28-
return e.Message
31+
func (e Blocker) Error() string {
32+
return fmt.Sprintf("%v: %v", e.Type, e.Message)
2933
}

common/blocker/blocker_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,12 @@ func TestBlocker_Error(t *testing.T) {
2121
Type: DBErr,
2222
Message: "sql error",
2323
},
24-
want: "sql error",
24+
want: "DBErr: sql error",
2525
},
2626
}
2727
for _, tt := range tests {
2828
t.Run(tt.name, func(t *testing.T) {
29-
e := blocker{
29+
e := Blocker{
3030
Type: tt.fields.Type,
3131
Message: tt.fields.Message,
3232
}
@@ -68,5 +68,5 @@ func TestNewBlocker(t *testing.T) {
6868
func ExampleNewBlocker() {
6969
err := NewBlocker(BlockErr, "invalid block height")
7070
fmt.Println(err)
71-
// Output: invalid block height
71+
// Output: BlockErr: invalid block height
7272
}

common/chaintype/mainchain.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@ package chaintype
33
// MainChain is struct should has methods in below
44
type MainChain struct{}
55

6+
// GetTypeInt return the value of the chain type in int
7+
func (*MainChain) GetTypeInt() int32 {
8+
return 0
9+
}
10+
611
// GetTablePrefix return the value of current chain table prefix in the database
712
func (*MainChain) GetTablePrefix() string {
813
return "main"
@@ -15,7 +20,7 @@ func (*MainChain) GetChainSmithingDelayTime() int64 {
1520

1621
// GetName return the name of the chain : used in parsing chaintype across node
1722
func (*MainChain) GetName() string {
18-
return ""
23+
return "Mainchain"
1924
}
2025

2126
// GetGenesisBlockID return the block ID of genesis block in the chain

0 commit comments

Comments
 (0)