Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
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
3 changes: 1 addition & 2 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 7 additions & 6 deletions api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ import (

"github.com/zoobc/zoobc-core/common/chaintype"
coreService "github.com/zoobc/zoobc-core/core/service"
p2p "github.com/zoobc/zoobc-core/p2p"

"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"
"github.com/zoobc/zoobc-core/common/contract"
"github.com/zoobc/zoobc-core/common/query"
rpcService "github.com/zoobc/zoobc-core/common/service"
"github.com/zoobc/zoobc-core/common/util"
Expand All @@ -32,7 +32,8 @@ func init() {
}
}

func startGrpcServer(port int, queryExecutor query.ExecutorInterface, p2pHostService contract.P2PType) {
func startGrpcServer(port int, queryExecutor query.ExecutorInterface, p2pHostService p2p.ServiceInterface,
blockServices map[int32]coreService.BlockServiceInterface) {
grpcServer := grpc.NewServer(
grpc.UnaryInterceptor(util.NewServerInterceptor(apiLogger)),
)
Expand Down Expand Up @@ -71,8 +72,7 @@ func startGrpcServer(port int, queryExecutor query.ExecutorInterface, p2pHostSer
})
// Set GRPC handler for Transactions requests
rpcService.RegisterHostServiceServer(grpcServer, &handler.HostHandler{
Service: service.NewHostService(queryExecutor),
P2pHostService: p2pHostService,
Service: service.NewHostService(queryExecutor, p2pHostService, blockServices),
})
// Set GRPC handler for account balance requests
rpcService.RegisterAccountBalanceServiceServer(grpcServer, &handler.AccountBalanceHandler{
Expand All @@ -96,6 +96,7 @@ func startGrpcServer(port int, queryExecutor query.ExecutorInterface, p2pHostSer
}

// Start starts api servers in the given port and passing query executor
func Start(grpcPort, restPort int, queryExecutor query.ExecutorInterface, p2pHostService contract.P2PType) {
startGrpcServer(grpcPort, queryExecutor, p2pHostService)
func Start(grpcPort, restPort int, queryExecutor query.ExecutorInterface, p2pHostService p2p.ServiceInterface,
blockServices map[int32]coreService.BlockServiceInterface) {
startGrpcServer(grpcPort, queryExecutor, p2pHostService, blockServices)
}
4 changes: 2 additions & 2 deletions api/client/GetBlockByID/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
)

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

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

if err != nil {
Expand Down
7 changes: 4 additions & 3 deletions api/client/GetBlocks/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
)

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

response, err := c.GetBlocks(context.Background(), &rpc_model.GetBlocksRequest{
Limit: 3,
Height: 12,
ChainType: 0,
Limit: 3,
Height: 1,
})

if err != nil {
Expand Down
6 changes: 4 additions & 2 deletions api/handler/blockHandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@ type BlockHandler struct {

// GetBlock handles request to get data of a single Block
func (bs *BlockHandler) GetBlock(ctx context.Context, req *model.GetBlockRequest) (*model.Block, error) {
var blockResponse *model.Block
var err error
var (
blockResponse *model.Block
err error
)
chainType := chaintype.GetChainType(req.ChainType)
if req.ID != 0 {
blockResponse, err = bs.Service.GetBlockByID(chainType, req.ID)
Expand Down
8 changes: 3 additions & 5 deletions api/handler/hostHandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,13 @@ import (
"context"

"github.com/zoobc/zoobc-core/api/service"
"github.com/zoobc/zoobc-core/common/contract"
"github.com/zoobc/zoobc-core/common/model"
)

type HostHandler struct {
Service service.HostServiceInterface
P2pHostService contract.P2PType
Service service.HostServiceInterface
}

func (hh *HostHandler) GetHostInfo(ctx context.Context, req *model.Empty) (*model.Host, error) {
return hh.P2pHostService.GetHostInstance(), nil
func (hh *HostHandler) GetHostInfo(ctx context.Context, req *model.Empty) (*model.HostInfo, error) {
return hh.Service.GetHostInfo()
}
35 changes: 30 additions & 5 deletions api/service/hostApiService.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,52 @@ package service
import (
"github.com/zoobc/zoobc-core/common/model"
"github.com/zoobc/zoobc-core/common/query"
coreService "github.com/zoobc/zoobc-core/core/service"
"github.com/zoobc/zoobc-core/p2p"
)

type (
HostServiceInterface interface {
GetHostInfo() (*model.HostInfo, error)
}

HostService struct {
Query query.ExecutorInterface
Query query.ExecutorInterface
BlockServices map[int32]coreService.BlockServiceInterface
P2pService p2p.ServiceInterface
}
)

var hostServiceInstance *HostService

// NewHostService create a singleton instance of HostService
func NewHostService(queryExecutor query.ExecutorInterface) *HostService {
func NewHostService(queryExecutor query.ExecutorInterface, p2pService p2p.ServiceInterface,
blockServices map[int32]coreService.BlockServiceInterface) HostServiceInterface {
if hostServiceInstance == nil {
hostServiceInstance = &HostService{Query: queryExecutor}
hostServiceInstance = &HostService{
Query: queryExecutor,
P2pService: p2pService,
BlockServices: blockServices,
}
}
return hostServiceInstance
}

func (*HostService) GetHostInfo() *model.Host {
return &model.Host{}
func (hs *HostService) GetHostInfo() (*model.HostInfo, error) {
chainStatuses := []*model.ChainStatus{}
for chainType, blockService := range hs.BlockServices {
lastBlock, err := blockService.GetLastBlock()
if lastBlock == nil || err != nil {
continue
}
chainStatuses = append(chainStatuses, &model.ChainStatus{
ChainType: chainType,
Height: lastBlock.Height,
LastBlock: lastBlock,
})
}
return &model.HostInfo{
Host: hs.P2pService.GetHostInstance(),
ChainStatuses: chainStatuses,
}, nil
}
24 changes: 14 additions & 10 deletions common/blocker/blocker.go
Original file line number Diff line number Diff line change
@@ -1,29 +1,33 @@
package blocker

import "fmt"

type (
TypeBlocker int
TypeBlocker string

blocker struct {
Blocker struct {
Type TypeBlocker
Message string
}
)

var (
DBErr TypeBlocker = 1
BlockErr TypeBlocker = 2
AppErr TypeBlocker = 3
AuthErr TypeBlocker = 4
ValidationErr TypeBlocker = 5
DBErr TypeBlocker = "DBErr"
BlockErr TypeBlocker = "BlockErr"
BlockNotFoundErr TypeBlocker = "BlockNotFoundErr"
RequestParameterErr TypeBlocker = "RequestParameterErr"
AppErr TypeBlocker = "AppErr"
AuthErr TypeBlocker = "AuthErr"
ValidationErr TypeBlocker = "ValidationErr"
)

func NewBlocker(typeBlocker TypeBlocker, message string) error {
return blocker{
return Blocker{
Type: typeBlocker,
Message: message,
}
}

func (e blocker) Error() string {
return e.Message
func (e Blocker) Error() string {
return fmt.Sprintf("%v: %v", e.Type, e.Message)
}
6 changes: 3 additions & 3 deletions common/blocker/blocker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ func TestBlocker_Error(t *testing.T) {
Type: DBErr,
Message: "sql error",
},
want: "sql error",
want: "DBErr: sql error",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
e := blocker{
e := Blocker{
Type: tt.fields.Type,
Message: tt.fields.Message,
}
Expand Down Expand Up @@ -68,5 +68,5 @@ func TestNewBlocker(t *testing.T) {
func ExampleNewBlocker() {
err := NewBlocker(BlockErr, "invalid block height")
fmt.Println(err)
// Output: invalid block height
// Output: BlockErr: invalid block height
}
7 changes: 6 additions & 1 deletion common/chaintype/mainchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@ package chaintype
// MainChain is struct should has methods in below
type MainChain struct{}

// GetTypeInt return the value of the chain type in int
func (*MainChain) GetTypeInt() int32 {
return 0
}

// GetTablePrefix return the value of current chain table prefix in the database
func (*MainChain) GetTablePrefix() string {
return "main"
Expand All @@ -15,7 +20,7 @@ func (*MainChain) GetChainSmithingDelayTime() int64 {

// GetName return the name of the chain : used in parsing chaintype across node
func (*MainChain) GetName() string {
return ""
return "Mainchain"
}

// GetGenesisBlockID return the block ID of genesis block in the chain
Expand Down
14 changes: 14 additions & 0 deletions common/constant/blockchainSync.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package constant

import "time"

const (
// GetMoreBlocksDelay returns delay between GetMoreBlocksThread in seconds
GetMoreBlocksDelay time.Duration = 10
BlockDownloadSegSize uint32 = 36
MaxResponseTime time.Duration = 1 * time.Minute
DefaultNumberOfForkConfirmations int32 = 1
PeerGetBlocksLimit uint32 = 1440
CommonMilestoneBlockIdsLimit int32 = 10
SafeBlockGap uint32 = 1440
)
2 changes: 2 additions & 0 deletions common/contract/chaintype.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package contract

// ChainType interface define the different behavior of each chain
type ChainType interface {
// GetTypeInt return the value of the chain type in int
GetTypeInt() int32
// GetTablePrefix return the value of current chain table prefix in the database
GetTablePrefix() string
// GetChainSmithingDelayTime return the value of chain smithing delay in second
Expand Down
15 changes: 0 additions & 15 deletions common/contract/p2pType.go

This file was deleted.

52 changes: 26 additions & 26 deletions common/model/accountBalance.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading