From 0ae9f82130fd8990d99bab75cef7ed1f7e4e3a53 Mon Sep 17 00:00:00 2001 From: Kay Date: Mon, 18 Sep 2023 20:25:47 +0330 Subject: [PATCH] feat: CreateValidatorEvent and CreateAccountEvent for nanomsg (#702) --- state/state.go | 11 ++++++++++- types/tx/payload/bond.go | 4 ++++ types/tx/payload/payload.go | 1 + types/tx/payload/sortition.go | 4 ++++ types/tx/payload/transfer.go | 4 ++++ types/tx/payload/unbond.go | 4 ++++ types/tx/payload/withdraw.go | 4 ++++ types/tx/tx.go | 5 +++++ www/nanomsg/event/event.go | 33 ++++++++++++++++++++++++--------- www/nanomsg/event/event_test.go | 13 ++++++++++++- 10 files changed, 72 insertions(+), 11 deletions(-) diff --git a/state/state.go b/state/state.go index bba2a4765..f78555d53 100644 --- a/state/state.go +++ b/state/state.go @@ -709,7 +709,16 @@ func (st *state) publishEvents(height uint32, block *block.Block) { for i := 1; i < block.Transactions().Len(); i++ { tx := block.Transactions().Get(i) - TxEvent := event.CreateNewTransactionEvent(tx.ID(), height) + + accChangeEvent := event.CreateAccountChangeEvent(tx.Payload().Signer(), height) + st.eventCh <- accChangeEvent + + if tx.Payload().ReceiverAddr() != nil { + accChangeEvent := event.CreateAccountChangeEvent(*tx.Payload().ReceiverAddr(), height) + st.eventCh <- accChangeEvent + } + + TxEvent := event.CreateTransactionEvent(tx.ID(), height) st.eventCh <- TxEvent } } diff --git a/types/tx/payload/bond.go b/types/tx/payload/bond.go index cdd39e7db..c4284ffe9 100644 --- a/types/tx/payload/bond.go +++ b/types/tx/payload/bond.go @@ -109,3 +109,7 @@ func (p *BondPayload) String() string { p.Receiver.ShortString(), p.Stake) } + +func (p *BondPayload) ReceiverAddr() *crypto.Address { + return &p.Receiver +} diff --git a/types/tx/payload/payload.go b/types/tx/payload/payload.go index 882c248e4..54275a88b 100644 --- a/types/tx/payload/payload.go +++ b/types/tx/payload/payload.go @@ -42,4 +42,5 @@ type Payload interface { Decode(io.Reader) error BasicCheck() error String() string + ReceiverAddr() *crypto.Address } diff --git a/types/tx/payload/sortition.go b/types/tx/payload/sortition.go index cff82ed20..940eef818 100644 --- a/types/tx/payload/sortition.go +++ b/types/tx/payload/sortition.go @@ -51,3 +51,7 @@ func (p *SortitionPayload) String() string { return fmt.Sprintf("{Sortition 🎯 %v", p.Address.ShortString()) } + +func (p *SortitionPayload) ReceiverAddr() *crypto.Address { + return nil +} diff --git a/types/tx/payload/transfer.go b/types/tx/payload/transfer.go index 60830f16a..efa72acc4 100644 --- a/types/tx/payload/transfer.go +++ b/types/tx/payload/transfer.go @@ -98,3 +98,7 @@ func (p *TransferPayload) String() string { p.Receiver.ShortString(), p.Amount) } + +func (p *TransferPayload) ReceiverAddr() *crypto.Address { + return &p.Receiver +} diff --git a/types/tx/payload/unbond.go b/types/tx/payload/unbond.go index b6a6c1fd1..0684d6981 100644 --- a/types/tx/payload/unbond.go +++ b/types/tx/payload/unbond.go @@ -51,3 +51,7 @@ func (p *UnbondPayload) String() string { p.Validator.ShortString(), ) } + +func (p *UnbondPayload) ReceiverAddr() *crypto.Address { + return nil +} diff --git a/types/tx/payload/withdraw.go b/types/tx/payload/withdraw.go index 65179d902..67c7a153e 100644 --- a/types/tx/payload/withdraw.go +++ b/types/tx/payload/withdraw.go @@ -70,3 +70,7 @@ func (p *WithdrawPayload) String() string { p.To.ShortString(), p.Amount) } + +func (p *WithdrawPayload) ReceiverAddr() *crypto.Address { + return &p.To +} diff --git a/types/tx/tx.go b/types/tx/tx.go index f4fa5a197..be84fca85 100644 --- a/types/tx/tx.go +++ b/types/tx/tx.go @@ -430,3 +430,8 @@ func (tx *Tx) StripPublicKey() { func (tx *Tx) IsPublicKeyStriped() bool { return util.IsFlagSet(tx.data.Flags, flagStripedPublicKey) } + +// Flags returns flags of transaction. +func (tx *Tx) Flags() uint8 { + return tx.data.Flags +} diff --git a/www/nanomsg/event/event.go b/www/nanomsg/event/event.go index f750e0ac7..d83852d6d 100644 --- a/www/nanomsg/event/event.go +++ b/www/nanomsg/event/event.go @@ -3,6 +3,7 @@ package event import ( "bytes" + "github.com/pactus-project/pactus/crypto" "github.com/pactus-project/pactus/crypto/hash" "github.com/pactus-project/pactus/types/tx" "github.com/pactus-project/pactus/util/encoding" @@ -10,8 +11,9 @@ import ( ) const ( - TopicNewBlock = uint16(0x0101) - TopicNewTransaction = uint16(0x0201) + TopicBlock = uint16(0x0101) + TopicTransaction = uint16(0x0201) + TopicAccountChange = uint16(0x0301) ) type Event []byte @@ -22,22 +24,35 @@ type Event []byte func CreateBlockEvent(blockHash hash.Hash, height uint32) Event { buf := make([]byte, 0, 42) w := bytes.NewBuffer(buf) - err := encoding.WriteElements(w, TopicNewBlock, blockHash, height) + err := encoding.WriteElements(w, TopicBlock, blockHash, height) if err != nil { - logger.Error("error on encoding event", "error", err) + logger.Error("error on encoding event in new block", "error", err) } return w.Bytes() } -// CreateBlockEvent creates an event when the new block is committed. -// The block event structure is like : +// CreateTransactionEvent creates an event when a new transaction sent. +// The new transaction event structure is like : // . -func CreateNewTransactionEvent(txHash tx.ID, height uint32) Event { +func CreateTransactionEvent(txHash tx.ID, height uint32) Event { + buf := make([]byte, 0, 42) + w := bytes.NewBuffer(buf) + err := encoding.WriteElements(w, TopicTransaction, txHash, height) + if err != nil { + logger.Error("error on encoding event in new transaction", "error", err) + } + return w.Bytes() +} + +// CreateAccountChangeEvent creates an event when the new account is created. +// The account event structure is like : +// . +func CreateAccountChangeEvent(accountAddr crypto.Address, height uint32) Event { buf := make([]byte, 0, 42) w := bytes.NewBuffer(buf) - err := encoding.WriteElements(w, TopicNewTransaction, txHash, height) + err := encoding.WriteElements(w, TopicAccountChange, accountAddr, height) if err != nil { - logger.Error("error on encoding event in transaction event", "error", err) + logger.Error("error on encoding event in new account", "error", err) } return w.Bytes() } diff --git a/www/nanomsg/event/event_test.go b/www/nanomsg/event/event_test.go index a1f234068..b772bfcba 100644 --- a/www/nanomsg/event/event_test.go +++ b/www/nanomsg/event/event_test.go @@ -3,6 +3,7 @@ package event import ( "testing" + "github.com/pactus-project/pactus/crypto" "github.com/pactus-project/pactus/crypto/hash" "github.com/stretchr/testify/assert" ) @@ -21,10 +22,20 @@ func TestCreateBlockEvent(t *testing.T) { func TestCreateNewTransactionEvent(t *testing.T) { hash, _ := hash.FromString("000102030405060708090a0b0c0d0e0f000102030405060708090a0b0c0d0e0f") height := uint32(0x2134) - e := CreateNewTransactionEvent(hash, height) + e := CreateTransactionEvent(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, }) } + +func TestCreateAccountChangeEvent(t *testing.T) { + addr, _ := crypto.AddressFromString("pc1p0hrct7eflrpw4ccrttxzs4qud2axex4dcdzdfr") + height := uint32(0x2134) + e := CreateAccountChangeEvent(addr, height) + assert.Equal(t, e, Event{ + 0x01, 0x03, 0x1, 0x7d, 0xc7, 0x85, 0xfb, 0x29, 0xf8, 0xc2, 0xea, 0xe3, + 0x3, 0x5a, 0xcc, 0x28, 0x54, 0x1c, 0x6a, 0xba, 0x6c, 0x9a, 0xad, 0x34, 0x21, 0x0, 0x0, + }) +}