Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
AgwaB committed Sep 10, 2018
1 parent 649c31b commit ef1914f
Show file tree
Hide file tree
Showing 10 changed files with 54 additions and 64 deletions.
33 changes: 33 additions & 0 deletions blockchain/api/block_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,23 @@ func (bApi BlockApi) SyncIsProgressing() blockchain.ProgressState {
return blockchain.DONE
}

func (api BlockApi) ConsentBlock(consensusType string, block blockchain.DefaultBlock) error {
if consensusType == "solo" {
return api.CommitBlock(block)
}

if consensusType == "pbft" {
event, err := createBlockCreatedEvent(block)
if err != nil {
return err
}
return api.eventService.Publish("block.created", event)
}

logger.Error(nil, fmt.Sprintf("[blockchain] undefined consensus type: %v", consensusType))
return ErrUndefinedConsensusType
}

func (bApi BlockApi) CommitGenesisBlock(GenesisConfPath string) error {
logger.Info(nil, "[Blockchain] Committing genesis block")

Expand Down Expand Up @@ -154,3 +171,19 @@ func createBlockCommittedEvent(block blockchain.DefaultBlock) (event.BlockCommit
State: blockchain.Committed,
}, nil
}

func createBlockCreatedEvent(block blockchain.DefaultBlock) (event.BlockCreated, error) {

txList := blockchain.ConvBackFromTransactionList(block.TxList)

return event.BlockCreated{
Seal: block.GetSeal(),
PrevSeal: block.GetPrevSeal(),
Height: block.GetHeight(),
TxList: txList,
TxSeal: block.GetTxSeal(),
Timestamp: block.GetTimestamp(),
Creator: block.GetCreator(),
State: block.GetState(),
}, nil
}
2 changes: 1 addition & 1 deletion blockchain/api/block_api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ func TestBlockApi_CommitProposedBlock(t *testing.T) {
},
TxSeal: nil,
Timestamp: time.Time{},
Creator: "",
Creator: nil,
State: "",
}

Expand Down
1 change: 1 addition & 0 deletions blockchain/api/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,4 @@ var ErrSaveBlock = errors.New("Error in saving block")
var ErrCreateEvent = errors.New("Error in creating event")
var ErrGetLastBlock = errors.New("Error in getting last block")
var ErrCreateProposedBlock = errors.New("Error in creating proposed block")
var ErrUndefinedConsensusType = errors.New("Error in consensus type")
28 changes: 8 additions & 20 deletions blockchain/infra/adapter/block_propose_command_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,22 +22,20 @@ import (
"github.com/it-chain/engine/common/rabbitmq/rpc"
)

type BlockCommitApi interface {
type BlockProposeApi interface {
CreateProposedBlock(txList []*blockchain.DefaultTransaction) (blockchain.DefaultBlock, error)
CommitBlock(txList blockchain.DefaultBlock) error
ConsentBlock(consensusType string, block blockchain.DefaultBlock) error
}

type BlockProposeCommandHandler struct {
blockApi BlockCommitApi
consensusService blockchain.ConsensusService
engineMode string
blockApi BlockProposeApi
engineMode string
}

func NewBlockProposeCommandHandler(blockApi BlockCommitApi, consensusService blockchain.ConsensusService, engineMode string) *BlockProposeCommandHandler {
func NewBlockProposeCommandHandler(blockApi BlockProposeApi, engineMode string) *BlockProposeCommandHandler {
return &BlockProposeCommandHandler{
blockApi: blockApi,
consensusService: consensusService,
engineMode: engineMode,
blockApi: blockApi,
engineMode: engineMode,
}
}

Expand All @@ -54,17 +52,7 @@ func (h *BlockProposeCommandHandler) HandleProposeBlockCommand(command command.P
return struct{}{}, rpc.Error{Message: err.Error()}
}

if h.engineMode == "solo" {
err = h.blockApi.CommitBlock(proposedBlock)
if err != nil {
return struct{}{}, rpc.Error{Message: err.Error()}
}

return struct{}{}, rpc.Error{}
}

err = h.consensusService.ConsentBlock(proposedBlock)
if err != nil {
if err := h.blockApi.ConsentBlock(h.engineMode, proposedBlock); err != nil {
return struct{}{}, rpc.Error{Message: err.Error()}
}

Expand Down
14 changes: 0 additions & 14 deletions blockchain/infra/adapter/consensus_service.go

This file was deleted.

2 changes: 2 additions & 0 deletions blockchain/infra/adapter/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ import "errors"
var ErrBlockNil = errors.New("Block nil error")
var ErrBlockTypeCasting = errors.New("Error in type casting block")
var ErrCommandTransactions = errors.New("command's transactions nil or have length of zero")
var ErrCommandSeal = errors.New("command's transactions nil")
var ErrTxHasMissingProperties = errors.New("Tx has missing properties")
var ErrBlockIdNil = errors.New("Error command model ID is nil")
var ErrTxResultsLengthOfZero = errors.New("Error length of tx results is zero")
var ErrTxResultsFail = errors.New("Error not all tx results success")
var ErrCreateEvent = errors.New("Error in creating consent event")
8 changes: 8 additions & 0 deletions blockchain/test/mock/mock_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,14 @@ func (ba MockSyncBlockApi) SyncedCheck(block blockchain.Block) error {
return ba.SyncedCheckFunc(block)
}

type CreateEventHandler struct {
HandleFunc func(event event.BlockCreated)
}

func (h *CreateEventHandler) Handle(event event.BlockCreated) {
h.HandleFunc(event)
}

type CommitEventHandler struct {
HandleFunc func(event event.BlockCommitted)
}
Expand Down
24 changes: 0 additions & 24 deletions conf/config.yaml

This file was deleted.

2 changes: 0 additions & 2 deletions doc/TUTORIAL.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,6 @@ Sample icode url: https://github.com/junbeomlee/learn-icode

- `it-chain ivm deploy [learn-icode-url] [ssh-private-key-path]`

`deploy` command clone from target repsoitory, and create docker container with that code

![[tutorial]deploy](./images/[tutorial]deploy.png)

![[tutorial]deploy-result](./images/[tutorial]deploy-result.png)
Expand Down
4 changes: 1 addition & 3 deletions it-chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ import (
blockchainApi "github.com/it-chain/engine/blockchain/api"
blockchainAdapter "github.com/it-chain/engine/blockchain/infra/adapter"
blockchainMem "github.com/it-chain/engine/blockchain/infra/mem"
"github.com/it-chain/engine/blockchain/test/mock"
"github.com/it-chain/engine/cmd/ivm"
"github.com/it-chain/engine/common"
"github.com/it-chain/engine/common/logger"
Expand Down Expand Up @@ -283,8 +282,7 @@ func initBlockchain(config *conf.Configuration, server rpc.Server) func() {
panic(err)
}

// TODO: Change with real consensus service
consensusService := mock.ConsensusService{}
consensusService := blockchainAdapter.NewConsensusService(eventService)

blockProposeHandler := blockchainAdapter.NewBlockProposeCommandHandler(blockApi, consensusService, config.Engine.Mode)
server.Register("block.propose", blockProposeHandler.HandleProposeBlockCommand)
Expand Down

0 comments on commit ef1914f

Please sign in to comment.