burrow stream event

StreamEvent

StreamEvent is used to mark the border of data stream when get blocks via grpc stream.

type StreamEvent struct {
	BeginBlock           *BeginBlock                                 `protobuf:"bytes,1,opt,name=BeginBlock,proto3" json:"BeginBlock,omitempty"`
	BeginTx              *BeginTx                                    `protobuf:"bytes,2,opt,name=BeginTx,proto3" json:"BeginTx,omitempty"`
	Envelope             *github_com_hyperledger_burrow_txs.Envelope `protobuf:"bytes,3,opt,name=Envelope,proto3,customtype=github.com/hyperledger/burrow/txs.Envelope" json:"Envelope,omitempty"`
	Event                *Event                                      `protobuf:"bytes,4,opt,name=Event,proto3" json:"Event,omitempty"`
	EndTx                *EndTx                                      `protobuf:"bytes,5,opt,name=EndTx,proto3" json:"EndTx,omitempty"`
	EndBlock             *EndBlock                                   `protobuf:"bytes,6,opt,name=EndBlock,proto3" json:"EndBlock,omitempty"`
}

We can assembly them to BlockExecutions and TxExecutions.

// Consume will add the StreamEvent passed to the block accumulator and if the block complete is complete return the
// BlockExecution, otherwise will return nil
func (ba *BlockAccumulator) Consume(ev *StreamEvent) (*BlockExecution, error) {
	switch {
	case ev.BeginBlock != nil:
		ba.block = &BlockExecution{
			Height: ev.BeginBlock.Height,
			Header: ev.BeginBlock.Header,
			Hash:   ev.BeginBlock.Hash,
		}
	case ev.BeginTx != nil, ev.Envelope != nil, ev.Event != nil, ev.EndTx != nil:
		txe, err := ba.stack.Consume(ev)
		if err != nil {
			return nil, err
		}
		if txe != nil {
			ba.block.TxExecutions = append(ba.block.TxExecutions, txe)
		}
	case ev.EndBlock != nil:
		return ba.block, nil
	}
	return nil, nil
}

// Consume will add the StreamEvent to the transaction stack and if that completes a single outermost transaction
// returns the TxExecution otherwise will return nil
func (stack *TxStack) Consume(ev *StreamEvent) (*TxExecution, error) {
	switch {
	case ev.BeginTx != nil:
		stack.Push(initTx(ev.BeginTx))
	case ev.Envelope != nil:
		txe, err := stack.Peek()
		if err != nil {
			return nil, err
		}
		txe.Envelope = ev.Envelope
		txe.Receipt = txe.Envelope.Tx.GenerateReceipt()
	case ev.Event != nil:
		txe, err := stack.Peek()
		if err != nil {
			return nil, err
		}
		txe.Events = append(txe.Events, ev.Event)
	case ev.EndTx != nil:
		txe, err := stack.Pop()
		if err != nil {
			return nil, err
		}
		if len(*stack) == 0 {
			// This terminates the outermost transaction
			return txe, nil
		}
		// If there is a parent tx on the stack add this tx as child
		parent, err := stack.Peek()
		if err != nil {
			return nil, err
		}
		parent.TxExecutions = append(parent.TxExecutions, txe)
	}
	return nil, nil
}

BeginBlock

type BeginBlock struct {
	// The height of this block
	Height               uint64        `protobuf:"varint,1,opt,name=Height,proto3" json:"Height,omitempty"`
	Header               *types.Header `protobuf:"bytes,2,opt,name=Header,proto3" json:"Header,omitempty"`
	Hash                 []byte        `protobuf:"bytes,3,opt,name=Hash,proto3" json:"Hash,omitempty"`
}

BeginTx

type BeginTx struct {
	TxHeader *TxHeader `protobuf:"bytes,1,opt,name=TxHeader,proto3" json:"TxHeader,omitempty"`
	// Result of tx execution
	Result *Result `protobuf:"bytes,2,opt,name=Result,proto3" json:"Result,omitempty"`
	// If tx execution was an exception
	Exception            *errors.Exception `protobuf:"bytes,4,opt,name=Exception,proto3" json:"Exception,omitempty"`
}

Envelope

// An envelope contains both the signable Tx and the signatures for each input (in signatories)
type Envelope struct {
	Signatories []Signatory `protobuf:"bytes,1,rep,name=Signatories,proto3" json:"Signatories"`
	// Canonical bytes of the Tx ready to be signed
	Tx                   *Tx               `protobuf:"bytes,2,opt,name=Tx,proto3,customtype=Tx" json:"Tx,omitempty"`
	Enc                  Envelope_Encoding `protobuf:"varint,3,opt,name=Enc,proto3,enum=txs.Envelope_Encoding" json:"Enc,omitempty"`
}

Event

We will disccuss it in burrow event

type Event struct {
	Header               *Header             `protobuf:"bytes,1,opt,name=Header,proto3" json:"Header,omitempty"`
	Input                *InputEvent         `protobuf:"bytes,2,opt,name=Input,proto3" json:"Input,omitempty"`
	Output               *OutputEvent        `protobuf:"bytes,3,opt,name=Output,proto3" json:"Output,omitempty"`
	Call                 *CallEvent          `protobuf:"bytes,4,opt,name=Call,proto3" json:"Call,omitempty"`
	Log                  *LogEvent           `protobuf:"bytes,5,opt,name=Log,proto3" json:"Log,omitempty"`
	GovernAccount        *GovernAccountEvent `protobuf:"bytes,6,opt,name=GovernAccount,proto3" json:"GovernAccount,omitempty"`
	XXX_NoUnkeyedLiteral struct{}            `json:"-"`
	XXX_unrecognized     []byte              `json:"-"`
	XXX_sizecache        int32               `json:"-"`
}

EndTx

type EndTx struct {
	// The hash of the transaction that caused this event to be generated
	TxHash               github_com_hyperledger_burrow_binary.HexBytes `protobuf:"bytes,3,opt,name=TxHash,proto3,customtype=github.com/hyperledger/burrow/binary.HexBytes" json:"TxHash"`
}

EndBlock

type EndBlock struct {
	Height               uint64   `protobuf:"varint,1,opt,name=Height,proto3" json:"Height,omitempty"`
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章