Skip to content

Commit

Permalink
feat: CreateValidatorEvent and CreateAccountEvent for nanomsg (#702)
Browse files Browse the repository at this point in the history
  • Loading branch information
kehiy authored Sep 18, 2023
1 parent 1627d50 commit 0ae9f82
Show file tree
Hide file tree
Showing 10 changed files with 72 additions and 11 deletions.
11 changes: 10 additions & 1 deletion state/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Expand Down
4 changes: 4 additions & 0 deletions types/tx/payload/bond.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,3 +109,7 @@ func (p *BondPayload) String() string {
p.Receiver.ShortString(),
p.Stake)
}

func (p *BondPayload) ReceiverAddr() *crypto.Address {
return &p.Receiver
}
1 change: 1 addition & 0 deletions types/tx/payload/payload.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,5 @@ type Payload interface {
Decode(io.Reader) error
BasicCheck() error
String() string
ReceiverAddr() *crypto.Address
}
4 changes: 4 additions & 0 deletions types/tx/payload/sortition.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
4 changes: 4 additions & 0 deletions types/tx/payload/transfer.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,3 +98,7 @@ func (p *TransferPayload) String() string {
p.Receiver.ShortString(),
p.Amount)
}

func (p *TransferPayload) ReceiverAddr() *crypto.Address {
return &p.Receiver
}
4 changes: 4 additions & 0 deletions types/tx/payload/unbond.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,7 @@ func (p *UnbondPayload) String() string {
p.Validator.ShortString(),
)
}

func (p *UnbondPayload) ReceiverAddr() *crypto.Address {
return nil
}
4 changes: 4 additions & 0 deletions types/tx/payload/withdraw.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,3 +70,7 @@ func (p *WithdrawPayload) String() string {
p.To.ShortString(),
p.Amount)
}

func (p *WithdrawPayload) ReceiverAddr() *crypto.Address {
return &p.To
}
5 changes: 5 additions & 0 deletions types/tx/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
33 changes: 24 additions & 9 deletions www/nanomsg/event/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,17 @@ 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"
"github.com/pactus-project/pactus/util/logger"
)

const (
TopicNewBlock = uint16(0x0101)
TopicNewTransaction = uint16(0x0201)
TopicBlock = uint16(0x0101)
TopicTransaction = uint16(0x0201)
TopicAccountChange = uint16(0x0301)
)

type Event []byte
Expand All @@ -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 :
// <topic_id><tx_hash><height><sequence_number>.
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 :
// <topic_id><account_address><height><sequence_number>.
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()
}
13 changes: 12 additions & 1 deletion www/nanomsg/event/event_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand All @@ -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,
})
}

0 comments on commit 0ae9f82

Please sign in to comment.