Skip to content

Commit

Permalink
Added transaction event pubsub with test case (#385)
Browse files Browse the repository at this point in the history
  • Loading branch information
vlasfama authored Sep 11, 2022
1 parent e529a3d commit feeda87
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 0 deletions.
6 changes: 6 additions & 0 deletions state/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -620,4 +620,10 @@ func (st *state) publishEvents(height uint32, block *block.Block) {
}
blockEvent := event.CreateBlockEvent(block.Hash(), height)
st.eventCh <- blockEvent

for i := 1; i < block.Transactions().Len(); i++ {
tx := block.Transactions().Get(i)
TxEvent := event.CreateNewTransactionEvent(tx.ID(), height)
st.eventCh <- TxEvent
}
}
4 changes: 4 additions & 0 deletions types/block/txs.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,7 @@ func (txs Txs) IsEmpty() bool {
func (txs Txs) Len() int {
return len(txs)
}

func (txs Txs) Get(i int) *tx.Tx {
return txs[i]
}
9 changes: 9 additions & 0 deletions types/block/txs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,12 @@ func TestIsEmpty(t *testing.T) {
txs.Append(trx)
assert.False(t, txs.IsEmpty())
}

func TestGetTransaction(t *testing.T) {
txs := NewTxs()
trx1, _ := tx.GenerateTestSendTx()
trx2, _ := tx.GenerateTestSendTx()
txs.Append(trx1)
txs.Append(trx2)
assert.Equal(t, trx1, txs.Get(0))
}
14 changes: 14 additions & 0 deletions www/nanomsg/event/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"

"github.com/pactus-project/pactus/crypto/hash"
"github.com/pactus-project/pactus/types/tx"
"github.com/pactus-project/pactus/util/encoding"
"github.com/pactus-project/pactus/util/logger"
)
Expand All @@ -25,3 +26,16 @@ func CreateBlockEvent(blockHash hash.Hash, height uint32) Event {
}
return w.Bytes()
}

// CreateBlockEvent creates an event when the new block is committed.
// The block event structure is like :
// <topic_id><tx_hash><height><sequence_number>
func CreateNewTransactionEvent(txHash tx.ID, height uint32) Event {
buf := make([]byte, 0, 42)
w := bytes.NewBuffer(buf)
err := encoding.WriteElements(w, TopicNewTransaction, txHash, height)
if err != nil {
logger.Error("error on encoding event in transaction event", "err", err)
}
return w.Bytes()
}
8 changes: 8 additions & 0 deletions www/nanomsg/event/event_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,11 @@ func TestCreateBlockEvent(t *testing.T) {
0xa, 0xb, 0xc, 0xd, 0xe, 0xf, 0x0, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8,
0x9, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf, 0x34, 0x21, 0x0, 0x0})
}
func TestCreateNewTransactionEvent(t *testing.T) {
hash, _ := hash.FromString("000102030405060708090a0b0c0d0e0f000102030405060708090a0b0c0d0e0f")
height := uint32(0x2134)
e := CreateNewTransactionEvent(hash, height)
assert.Equal(t, e, Event{0x1, 0x2, 0x0, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8, 0x9,
0xa, 0xb, 0xc, 0xd, 0xe, 0xf, 0x0, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8,
0x9, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf, 0x34, 0x21, 0x0, 0x0})
}

0 comments on commit feeda87

Please sign in to comment.