Skip to content

Commit

Permalink
Merge pull request #434 from klim0v/dev
Browse files Browse the repository at this point in the history
fix events
  • Loading branch information
danil-lashin authored Oct 9, 2020
2 parents b0cb195 + 0522c97 commit d54eee9
Show file tree
Hide file tree
Showing 3 changed files with 156 additions and 65 deletions.
18 changes: 10 additions & 8 deletions core/events/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ type eventsStore struct {
sync.RWMutex
db db.DB
pending pendingEvents
idPubKey map[uint16]string
pubKeyID map[string]uint16
idPubKey map[uint16][32]byte
pubKeyID map[[32]byte]uint16
idAddress map[uint32][20]byte
addressID map[[20]byte]uint32
}
Expand All @@ -46,14 +46,14 @@ func NewEventsStore(db db.DB) IEventsDB {
RWMutex: sync.RWMutex{},
db: db,
pending: pendingEvents{},
idPubKey: make(map[uint16]string),
pubKeyID: make(map[string]uint16),
idPubKey: make(map[uint16][32]byte),
pubKeyID: make(map[[32]byte]uint16),
idAddress: make(map[uint32][20]byte),
addressID: make(map[[20]byte]uint32),
}
}

func (store *eventsStore) cachePubKey(id uint16, key string) {
func (store *eventsStore) cachePubKey(id uint16, key [32]byte) {
store.idPubKey[id] = key
store.pubKeyID[key] = id
}
Expand Down Expand Up @@ -157,7 +157,7 @@ func (store *eventsStore) saveAddress(address [20]byte) uint32 {

func (store *eventsStore) savePubKey(validatorPubKey [32]byte) uint16 {

key := string(validatorPubKey[:])
key := validatorPubKey
if id, ok := store.pubKeyID[key]; ok {
return id
}
Expand All @@ -177,11 +177,13 @@ func (store *eventsStore) savePubKey(validatorPubKey [32]byte) uint16 {
func (store *eventsStore) loadPubKeys() {
if count, _ := store.db.Get([]byte(pubKeysCountKey)); len(count) > 0 {
for id := uint16(0); id < binary.BigEndian.Uint16(count); id++ {
pubKey, err := store.db.Get(append([]byte(pubKeyPrefix), uint16ToBytes(id)...))
key, err := store.db.Get(append([]byte(pubKeyPrefix), uint16ToBytes(id)...))
if err != nil {
panic(err)
}
store.cachePubKey(id, string(pubKey))
var pubKey [32]byte
copy(pubKey[:], key)
store.cachePubKey(id, pubKey)
}
}
}
Expand Down
159 changes: 124 additions & 35 deletions core/events/store_test.go
Original file line number Diff line number Diff line change
@@ -1,80 +1,169 @@
package events

import (
"encoding/hex"
"github.com/MinterTeam/minter-go-node/core/types"
db "github.com/tendermint/tm-db"
"math/big"
"testing"
)

func TestIEventsDB(t *testing.T) {
store := NewEventsStore(db.NewMemDB())

{
amount, _ := big.NewInt(0).SetString("111497225000000000000", 10)
event := &RewardEvent{
Role: RoleDevelopers.String(),
Address: [20]byte{},
Amount: amount.String(),
ValidatorPubKey: [32]byte{},
Address: types.HexToAddress("Mx04bea23efb744dc93b4fda4c20bf4a21c6e195f1"),
Amount: "111497225000000000000",
ValidatorPubKey: types.HexToPubkey("Mp9e13f2f5468dd782b316444fbd66595e13dba7d7bd3efa1becd50b42045f58c6"),
}
bytesAddress, err := hex.DecodeString("Mx04bea23efb744dc93b4fda4c20bf4a21c6e195f1"[2:])
if err != nil {
t.Fatal(err)
}
copy(event.Address[:], bytesAddress)
hexPubKey, err := hex.DecodeString("Mp9e13f2f5468dd782b316444fbd66595e13dba7d7bd3efa1becd50b42045f58c6"[2:])
if err != nil {
t.Fatal(err)
store.AddEvent(12, event)
}
{
event := &StakeKickEvent{
Coin: 1,
Address: types.HexToAddress("Mx18467bbb64a8edf890201d526c35957d82be3d95"),
Amount: "891977800000000000000",
ValidatorPubKey: types.HexToPubkey("Mp738da41ba6a7b7d69b7294afa158b89c5a1b410cbf0c2443c85c5fe24ad1dd1c"),
}
copy(event.ValidatorPubKey[:], hexPubKey)
store.AddEvent(12, event)
}
err := store.CommitEvents()
if err != nil {
t.Fatal(err)
}

{
amount, _ := big.NewInt(0).SetString("891977800000000000000", 10)
event := &RewardEvent{
Role: RoleValidator.String(),
Address: [20]byte{},
Amount: amount.String(),
ValidatorPubKey: [32]byte{},
event := &UnbondEvent{
Coin: 1,
Address: types.HexToAddress("Mx18467bbb64a8edf890201d526c35957d82be3d91"),
Amount: "891977800000000000001",
ValidatorPubKey: types.HexToPubkey("Mp738da41ba6a7b7d69b7294afa158b89c5a1b410cbf0c2443c85c5fe24ad1dd11"),
}
bytesAddress, err := hex.DecodeString("Mx18467bbb64a8edf890201d526c35957d82be3d95"[2:])
if err != nil {
t.Fatal(err)
store.AddEvent(14, event)
}
{
event := &UnbondEvent{
Coin: 2,
Address: types.HexToAddress("Mx18467bbb64a8edf890201d526c35957d82be3d92"),
Amount: "891977800000000000002",
ValidatorPubKey: types.HexToPubkey("Mp738da41ba6a7b7d69b7294afa158b89c5a1b410cbf0c2443c85c5fe24ad1dd12"),
}
copy(event.Address[:], bytesAddress)
hexPubKey, err := hex.DecodeString("Mp738da41ba6a7b7d69b7294afa158b89c5a1b410cbf0c2443c85c5fe24ad1dd1c"[2:])
if err != nil {
t.Fatal(err)
store.AddEvent(14, event)
}
err = store.CommitEvents()
if err != nil {
t.Fatal(err)
}

{
event := &SlashEvent{
Coin: 10,
Address: types.HexToAddress("Mx18467bbb64a8edf890201d526c35957d82be3d10"),
Amount: "891977800000000000010",
ValidatorPubKey: types.HexToPubkey("Mp738da41ba6a7b7d69b7294afa158b89c5a1b410cbf0c2443c85c5fe24ad1dd10"),
}
copy(event.ValidatorPubKey[:], hexPubKey)
store.AddEvent(12, event)
store.AddEvent(11, event)
}
err := store.CommitEvents()
err = store.CommitEvents()
if err != nil {
t.Fatal(err)
}

loadEvents := store.LoadEvents(12)

if len(loadEvents) != 2 {
t.Fatal("count of events not equal 2")
t.Fatalf("count of events not equal 2, got %d", len(loadEvents))
}

if loadEvents[0].Type() != TypeRewardEvent {
t.Fatal("invalid event type")
}
if loadEvents[0].(*RewardEvent).Amount != "111497225000000000000" {
t.Fatal("invalid Amount")
}

if loadEvents[0].(*RewardEvent).Address.String() != "Mx04bea23efb744dc93b4fda4c20bf4a21c6e195f1" {
t.Fatal("invalid Address")
}
if loadEvents[0].(*RewardEvent).ValidatorPubKey.String() != "Mp9e13f2f5468dd782b316444fbd66595e13dba7d7bd3efa1becd50b42045f58c6" {
t.Fatal("invalid PubKey")
}
if loadEvents[0].(*RewardEvent).Role != RoleDevelopers.String() {
t.Fatal("invalid Role")
}

if loadEvents[1].Type() != TypeStakeKickEvent {
t.Fatal("invalid event type")
}
if loadEvents[1].(*StakeKickEvent).Amount != "891977800000000000000" {
t.Fatal("invalid Amount")
}
if loadEvents[1].(*StakeKickEvent).Address.String() != "Mx18467bbb64a8edf890201d526c35957d82be3d95" {
t.Fatal("invalid Address")
}
if loadEvents[1].(*StakeKickEvent).ValidatorPubKey.String() != "Mp738da41ba6a7b7d69b7294afa158b89c5a1b410cbf0c2443c85c5fe24ad1dd1c" {
t.Fatal("invalid PubKey")
}
if loadEvents[1].(*StakeKickEvent).Coin.Uint32() != 1 {
t.Fatal("invalid Coin")
}

if loadEvents[1].(*RewardEvent).Amount != "891977800000000000000" {
loadEvents = store.LoadEvents(14)

if len(loadEvents) != 2 {
t.Fatal("count of events not equal 2")
}

if loadEvents[0].Type() != TypeUnbondEvent {
t.Fatal("invalid event type")
}
if loadEvents[0].(*UnbondEvent).Amount != "891977800000000000001" {
t.Fatal("invalid Amount")
}
if loadEvents[0].(*UnbondEvent).Address.String() != "Mx18467bbb64a8edf890201d526c35957d82be3d91" {
t.Fatal("invalid Address")
}
if loadEvents[0].(*UnbondEvent).ValidatorPubKey.String() != "Mp738da41ba6a7b7d69b7294afa158b89c5a1b410cbf0c2443c85c5fe24ad1dd11" {
t.Fatal("invalid PubKey")
}
if loadEvents[0].(*UnbondEvent).Coin.Uint32() != 1 {
t.Fatal("invalid Coin")
}

if loadEvents[1].(*RewardEvent).Address.String() != "Mx18467bbb64a8edf890201d526c35957d82be3d95" {
if loadEvents[1].Type() != TypeUnbondEvent {
t.Fatal("invalid event type")
}
if loadEvents[1].(*UnbondEvent).Amount != "891977800000000000002" {
t.Fatal("invalid Amount")
}
if loadEvents[1].(*UnbondEvent).Address.String() != "Mx18467bbb64a8edf890201d526c35957d82be3d92" {
t.Fatal("invalid Address")
}
if loadEvents[1].(*UnbondEvent).ValidatorPubKey.String() != "Mp738da41ba6a7b7d69b7294afa158b89c5a1b410cbf0c2443c85c5fe24ad1dd12" {
t.Fatal("invalid PubKey")
}
if loadEvents[1].(*UnbondEvent).Coin.Uint32() != 2 {
t.Fatal("invalid Coin")
}

loadEvents = store.LoadEvents(11)

if len(loadEvents) != 1 {
t.Fatal("count of events not equal 1")
}

if loadEvents[0].Type() != TypeSlashEvent {
t.Fatal("invalid event type")
}
if loadEvents[0].(*SlashEvent).Amount != "891977800000000000010" {
t.Fatal("invalid Amount")
}
if loadEvents[0].(*SlashEvent).Address.String() != "Mx18467bbb64a8edf890201d526c35957d82be3d10" {
t.Fatal("invalid Address")
}
if loadEvents[0].(*SlashEvent).ValidatorPubKey.String() != "Mp738da41ba6a7b7d69b7294afa158b89c5a1b410cbf0c2443c85c5fe24ad1dd10" {
t.Fatal("invalid PubKey")
}
if loadEvents[0].(*SlashEvent).Coin.Uint32() != 10 {
t.Fatal("invalid Coin")
}
}
44 changes: 22 additions & 22 deletions core/events/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ type Event interface {
}

type compactEvent interface {
compile(pubKey string, address [20]byte) Event
compile(pubKey [32]byte, address [20]byte) Event
addressID() uint32
pubKeyID() uint16
}
Expand Down Expand Up @@ -90,10 +90,10 @@ type reward struct {
PubKeyID uint16
}

func (r *reward) compile(pubKey string, address [20]byte) Event {
func (r *reward) compile(pubKey [32]byte, address [20]byte) Event {
event := new(RewardEvent)
copy(event.ValidatorPubKey[:], pubKey)
copy(event.Address[:], address[:])
event.ValidatorPubKey = pubKey
event.Address = address
event.Role = r.Role.String()
event.Amount = big.NewInt(0).SetBytes(r.Amount).String()
return event
Expand Down Expand Up @@ -147,15 +147,15 @@ func (re *RewardEvent) convert(pubKeyID uint16, addressID uint32) compactEvent {
type slash struct {
AddressID uint32
Amount []byte
Coin [10]byte
Coin uint32
PubKeyID uint16
}

func (s *slash) compile(pubKey string, address [20]byte) Event {
func (s *slash) compile(pubKey [32]byte, address [20]byte) Event {
event := new(SlashEvent)
copy(event.ValidatorPubKey[:], pubKey)
copy(event.Address[:], address[:])
copy(event.Coin.Bytes(), s.Coin[:])
event.ValidatorPubKey = pubKey
event.Address = address
event.Coin = types.CoinID(s.Coin)
event.Amount = big.NewInt(0).SetBytes(s.Amount).String()
return event
}
Expand Down Expand Up @@ -198,7 +198,7 @@ func (se *SlashEvent) validatorPubKey() types.Pubkey {
func (se *SlashEvent) convert(pubKeyID uint16, addressID uint32) compactEvent {
result := new(slash)
result.AddressID = addressID
copy(result.Coin[:], se.Coin.Bytes())
result.Coin = se.Coin.Uint32()
bi, _ := big.NewInt(0).SetString(se.Amount, 10)
result.Amount = bi.Bytes()
result.PubKeyID = pubKeyID
Expand All @@ -208,15 +208,15 @@ func (se *SlashEvent) convert(pubKeyID uint16, addressID uint32) compactEvent {
type unbond struct {
AddressID uint32
Amount []byte
Coin [10]byte
Coin uint32
PubKeyID uint16
}

func (u *unbond) compile(pubKey string, address [20]byte) Event {
func (u *unbond) compile(pubKey [32]byte, address [20]byte) Event {
event := new(UnbondEvent)
copy(event.ValidatorPubKey[:], pubKey)
copy(event.Address[:], address[:])
copy(event.Coin.Bytes(), u.Coin[:])
event.ValidatorPubKey = pubKey
event.Address = address
event.Coin = types.CoinID(u.Coin)
event.Amount = big.NewInt(0).SetBytes(u.Amount).String()
return event
}
Expand Down Expand Up @@ -259,7 +259,7 @@ func (ue *UnbondEvent) validatorPubKey() types.Pubkey {
func (ue *UnbondEvent) convert(pubKeyID uint16, addressID uint32) compactEvent {
result := new(unbond)
result.AddressID = addressID
copy(result.Coin[:], ue.Coin.Bytes())
result.Coin = ue.Coin.Uint32()
bi, _ := big.NewInt(0).SetString(ue.Amount, 10)
result.Amount = bi.Bytes()
result.PubKeyID = pubKeyID
Expand All @@ -269,15 +269,15 @@ func (ue *UnbondEvent) convert(pubKeyID uint16, addressID uint32) compactEvent {
type stakeKick struct {
AddressID uint32
Amount []byte
Coin [10]byte
Coin uint32
PubKeyID uint16
}

func (u *stakeKick) compile(pubKey string, address [20]byte) Event {
func (u *stakeKick) compile(pubKey [32]byte, address [20]byte) Event {
event := new(StakeKickEvent)
copy(event.ValidatorPubKey[:], pubKey)
copy(event.Address[:], address[:])
copy(event.Coin.Bytes(), u.Coin[:])
event.ValidatorPubKey = pubKey
event.Address = address
event.Coin = types.CoinID(u.Coin)
event.Amount = big.NewInt(0).SetBytes(u.Amount).String()
return event
}
Expand Down Expand Up @@ -320,7 +320,7 @@ func (ue *StakeKickEvent) validatorPubKey() types.Pubkey {
func (ue *StakeKickEvent) convert(pubKeyID uint16, addressID uint32) compactEvent {
result := new(stakeKick)
result.AddressID = addressID
copy(result.Coin[:], ue.Coin.Bytes())
result.Coin = ue.Coin.Uint32()
bi, _ := big.NewInt(0).SetString(ue.Amount, 10)
result.Amount = bi.Bytes()
result.PubKeyID = pubKeyID
Expand Down

0 comments on commit d54eee9

Please sign in to comment.