Skip to content

Commit

Permalink
DE-labtory#723 Add blockcreate for send block to consensus
Browse files Browse the repository at this point in the history
  • Loading branch information
AgwaB committed Sep 8, 2018
1 parent 60b6481 commit 0419bfe
Show file tree
Hide file tree
Showing 5 changed files with 161 additions and 3 deletions.
52 changes: 52 additions & 0 deletions blockchain/api/block_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"github.com/it-chain/engine/blockchain"
"github.com/it-chain/engine/common/event"
"github.com/it-chain/engine/common/logger"
"github.com/rs/xid"
)

type BlockApi struct {
Expand Down Expand Up @@ -87,6 +88,40 @@ func (bApi BlockApi) CommitGenesisBlock(GenesisConfPath string) error {
return bApi.eventService.Publish("block.committed", commitEvent)
}

func (bApi BlockApi) CreateBlock(txList []*blockchain.DefaultTransaction) error {
logger.Info(nil, "[Blockchain] Create Block")

lastBlock, err := bApi.blockRepository.FindLast()

if err != nil {
return ErrGetLastBlock
}

prevSeal := lastBlock.GetSeal()

height := lastBlock.GetHeight() + 1

creator := bApi.publisherId

ProposedBlock, err := blockchain.CreateProposedBlock(prevSeal, height, txList, []byte(creator))

if err != nil {
return ErrCreateProposedBlock
}

ProposedBlock.SetState(blockchain.Created)

CreateEvent, err := createBlockCreateEvent(ProposedBlock)

if err != nil {
return ErrCreateEvent
}

logger.Info(nil, fmt.Sprintf("[Blockchain] Proposed block has Created - seal: [%x], height: [%d]", ProposedBlock.Seal, ProposedBlock.Height))

return bApi.eventService.Publish("block.created", CreateEvent)
}

func (bApi BlockApi) CommitProposedBlock(txList []*blockchain.DefaultTransaction) error {
logger.Info(nil, "[Blockchain] Committing proposed block")

Expand Down Expand Up @@ -145,3 +180,20 @@ func createBlockCommittedEvent(block blockchain.DefaultBlock) (event.BlockCommit
State: block.GetState(),
}, nil
}

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

txList := blockchain.ConvBackFromTransactionList(block.TxList)

return event.BlockCreated{
BlockId: xid.New().String(),
Seal: block.GetSeal(),
PrevSeal: block.GetPrevSeal(),
Height: block.GetHeight(),
TxList: txList,
TxSeal: block.GetTxSeal(),
Timestamp: block.GetTimestamp(),
Creator: block.GetCreator(),
State: block.GetState(),
}, nil
}
86 changes: 86 additions & 0 deletions blockchain/api/block_api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,92 @@ func TestBlockApi_SyncIsProgressing(t *testing.T) {
assert.Equal(t, blockchain.DONE, state)
}

func TestBlockApi_CreateBlock(t *testing.T) {

lastBlock := blockchain.DefaultBlock{
Seal: []byte("seal"),
PrevSeal: []byte("prevSeal"),
Height: uint64(11),
TxList: []*blockchain.DefaultTransaction{
{
ID: "tx01",
ICodeID: "ICodeID",
PeerID: "junksound",
Timestamp: time.Now().Round(0),
Jsonrpc: "",
Function: "",
Args: make([]string, 0),
Signature: []byte("Signature"),
},
},
TxSeal: nil,
Timestamp: time.Time{},
Creator: nil,
State: "",
}

txList := []*blockchain.DefaultTransaction{
{
ID: "tx02",
ICodeID: "ICodeID",
PeerID: "junksound",
Timestamp: time.Now().Round(0),
Jsonrpc: "",
Function: "",
Args: make([]string, 0),
Signature: []byte("Signature"),
},
}

//set subscriber
var wg sync.WaitGroup
wg.Add(1)

subscriber := pubsub.NewTopicSubscriber("", "Event")
defer subscriber.Close()

handler := &mock.CreateEventHandler{}

handler.HandleFunc = func(event event.BlockCreated) {
assert.Equal(t, "tx02", event.TxList[0].ID)
assert.Equal(t, blockchain.Created, event.State)
wg.Done()
}

subscriber.SubscribeTopic("block.*", handler)

//set api param

publisherID := "junksound"

blockRepo := mock.BlockRepository{}

blockRepo.FindLastFunc = func() (blockchain.DefaultBlock, error) {
return lastBlock, nil
}

blockRepo.SaveFunc = func(block blockchain.DefaultBlock) error {

assert.Equal(t, "tx02", block.GetTxList()[0].GetID())

return nil
}

eventService := common.NewEventService("", "Event")

bApi, err := api.NewBlockApi(publisherID, blockRepo, eventService)

assert.NoError(t, err)

// when
err = bApi.CreateBlock(txList)

// then
assert.NoError(t, err)
wg.Wait()
}


// TODO: Write real situation test code, after finishing implementing api_gatey block_query_api.go
func TestBlockApi_CommitProposedBlock(t *testing.T) {

Expand Down
17 changes: 14 additions & 3 deletions blockchain/infra/adapter/block_propose_command_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,17 @@ import (
"github.com/it-chain/engine/common/rabbitmq/rpc"
)

type BlockCommitApi interface {
type BlockModeApi interface {
CommitProposedBlock(txList []*blockchain.DefaultTransaction) error
CreateBlock(txList []*blockchain.DefaultTransaction) error
}

type BlockProposeCommandHandler struct {
blockApi BlockCommitApi
blockApi BlockModeApi
engineMode string
}

func NewBlockProposeCommandHandler(blockApi BlockCommitApi, engineMode string) *BlockProposeCommandHandler {
func NewBlockProposeCommandHandler(blockApi BlockModeApi, engineMode string) *BlockProposeCommandHandler {
return &BlockProposeCommandHandler{
blockApi: blockApi,
engineMode: engineMode,
Expand All @@ -56,6 +57,16 @@ func (h *BlockProposeCommandHandler) HandleProposeBlockCommand(command command.P
return struct{}{}, rpc.Error{Message: err.Error()}
}

return struct{}{}, rpc.Error{}
} else if h.engineMode == "pbft" {

//create
err := h.blockApi.CreateBlock(defaultTxList)

if err != nil {
return struct{}{}, rpc.Error{Message: err.Error()}
}

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

Expand Down
1 change: 1 addition & 0 deletions blockchain/infra/adapter/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ 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")
Expand Down
8 changes: 8 additions & 0 deletions blockchain/test/mock/mock_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,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

0 comments on commit 0419bfe

Please sign in to comment.