Skip to content

Commit

Permalink
Refactor events
Browse files Browse the repository at this point in the history
  • Loading branch information
danil-lashin committed Oct 22, 2018
1 parent a24cce2 commit f4f858e
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 24 deletions.
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ check: check_tools ensure_deps
### Build

build:
CGO_ENABLED=1 go build $(BUILD_FLAGS) -tags '$(BUILD_TAGS)' -o build/minter ./cmd/minter/
CGO_ENABLED=0 go build $(BUILD_FLAGS) -tags '$(BUILD_TAGS)' -o build/minter ./cmd/minter/

install:
CGO_ENABLED=1 go install $(BUILD_FLAGS) -tags '$(BUILD_TAGS)' ./cmd/minter
CGO_ENABLED=0 go install $(BUILD_FLAGS) -tags '$(BUILD_TAGS)' ./cmd/minter


########################################
Expand Down
12 changes: 9 additions & 3 deletions api/send_transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,17 @@ func SendTransaction(w http.ResponseWriter, r *http.Request) {

result, err := client.BroadcastTxCommit(types.Hex2Bytes(req.Transaction))

w.Header().Set("Content-Type", "application/json; charset=UTF-8")

if err != nil {
panic(err)
}
w.WriteHeader(http.StatusInternalServerError)

w.Header().Set("Content-Type", "application/json; charset=UTF-8")
_ = json.NewEncoder(w).Encode(Response{
Code: 500,
Log: err.Error(),
})
return
}

if result.CheckTx.Code != code.OK {
w.WriteHeader(http.StatusInternalServerError)
Expand Down
1 change: 1 addition & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ func DefaultConfig() *Config {
cfg.Mempool.WalPath = "tmdata/mempool.wal"
cfg.Mempool.Recheck = true
cfg.Mempool.RecheckEmpty = true
cfg.Mempool.Size = 10000

cfg.Consensus.WalPath = "tmdata/cs.wal/wal"
cfg.Consensus.TimeoutPropose = 2000
Expand Down
38 changes: 19 additions & 19 deletions eventsdb/eventsdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,42 +37,44 @@ func GetCurrentDB() *db.GoLevelDB {
}

func GetCurrent() *EventsDB {
if edb == nil {
edb = NewEventsDB(GetCurrentDB())
if edb != nil {
return edb
}

edb = NewEventsDB(GetCurrentDB())

return edb
}

type EventsDB struct {
db *db.GoLevelDB
cache *EventsCache
cache *eventsCache

lock sync.RWMutex
}

type EventsCache struct {
type eventsCache struct {
height int64
events Events

lock sync.RWMutex
}

func (c *EventsCache) Set(height int64, events Events) {
func (c *eventsCache) set(height int64, events Events) {
c.lock.Lock()
defer c.lock.Unlock()

c.height, c.events = height, events
}

func (c *EventsCache) Get() Events {
func (c *eventsCache) get() Events {
c.lock.RLock()
defer c.lock.RUnlock()

return c.events
}

func (c *EventsCache) Clear() {
func (c *eventsCache) Clear() {
c.lock.Lock()
defer c.lock.Unlock()

Expand All @@ -83,7 +85,7 @@ func (c *EventsCache) Clear() {
func NewEventsDB(db *db.GoLevelDB) *EventsDB {
return &EventsDB{
db: db,
cache: &EventsCache{
cache: &eventsCache{
height: 0,
events: nil,
lock: sync.RWMutex{},
Expand All @@ -97,16 +99,16 @@ func (db *EventsDB) AddEvent(height int64, event Event) {
return
}

events := db.GetEvents(height)
db.SetEvents(height, append(events, event))
events := db.getEvents(height)
db.setEvents(height, append(events, event))
}

func (db *EventsDB) FlushEvents(height int64) error {
if !eventsEnabled {
return nil
}

events := db.GetEvents(height)
events := db.getEvents(height)
bytes, err := cdc.MarshalBinary(events)

if err != nil {
Expand All @@ -122,15 +124,14 @@ func (db *EventsDB) FlushEvents(height int64) error {
return nil
}

func (db *EventsDB) SetEvents(height int64, events Events) {
db.cache.Set(height, events)
func (db *EventsDB) setEvents(height int64, events Events) {
db.cache.set(height, events)
}

func (db *EventsDB) LoadEvents(height int64) Events {
db.lock.RLock()
defer db.lock.RUnlock()

data := db.db.Get(getKeyForHeight(height))
db.lock.RUnlock()

if len(data) == 0 {
return Events{}
Expand All @@ -146,14 +147,13 @@ func (db *EventsDB) LoadEvents(height int64) Events {
return decoded
}

func (db *EventsDB) GetEvents(height int64) Events {
func (db *EventsDB) getEvents(height int64) Events {
if db.cache.height == height {
return db.cache.Get()
return db.cache.get()
}

events := db.LoadEvents(height)

db.cache.Set(height, events)
db.cache.set(height, events)

return events
}
Expand Down

0 comments on commit f4f858e

Please sign in to comment.