Skip to content

Commit

Permalink
Update server
Browse files Browse the repository at this point in the history
  • Loading branch information
Brandon Berhent committed Aug 23, 2022
1 parent f0d60df commit d873208
Show file tree
Hide file tree
Showing 6 changed files with 217 additions and 314 deletions.
12 changes: 6 additions & 6 deletions server/controller/nano.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,14 +77,14 @@ func (nc NanoController) Callback(confirmationResponse net.ConfirmationResponse)
return
}
// Lock refund
lock, err := db.GetDB().Locker.Obtain(fmt.Sprintf("natricon:refundl:%s", hash), 100*time.Second, nil)
lock, err := db.GetDB().Locker.Obtain(db.CTX, fmt.Sprintf("natricon:refundl:%s", hash), 100*time.Second, nil)
if err == redislock.ErrNotObtained {
return
} else if err != nil {
glog.Error(err)
return
}
defer lock.Release()
defer lock.Release(db.CTX)
glog.Infof("Issuing refund to %s for %s due to nonce change ID %s", block["account"], amount, hash)
response, err := nc.RPCClient.MakeSendRequest(
nc.DonationAccount,
Expand All @@ -105,14 +105,14 @@ func (nc NanoController) Callback(confirmationResponse net.ConfirmationResponse)
}
nc.SIOServer.BroadcastToRoom("", "bcast", "donation_event", data)
// Calc donor duration with lock
lock, err := db.GetDB().Locker.Obtain(fmt.Sprintf("natricon:callback_lock:%s", hash), 100*time.Second, nil)
lock, err := db.GetDB().Locker.Obtain(db.CTX, fmt.Sprintf("natricon:callback_lock:%s", hash), 100*time.Second, nil)
if err == redislock.ErrNotObtained {
return
} else if err != nil {
glog.Error(err)
return
}
defer lock.Release()
defer lock.Release(db.CTX)
durationDays := nc.calcDonorDurationDays(amount)
if durationDays > 0 {
glog.Infof("Giving donor status to %s for %d days", block["account"], durationDays)
Expand Down Expand Up @@ -183,14 +183,14 @@ func (nc NanoController) CheckMissedCallbacks() {
}

// Try to obtain lock.
lock, err := db.GetDB().Locker.Obtain("natricon:history_lock", 100*time.Second, nil)
lock, err := db.GetDB().Locker.Obtain(db.CTX, "natricon:history_lock", 100*time.Second, nil)
if err == redislock.ErrNotObtained {
return
} else if err != nil {
glog.Error(err)
return
}
defer lock.Release()
defer lock.Release(db.CTX)
// Check history
historyResponse, err := nc.RPCClient.MakeAccountHistoryRequest(
nc.DonationAccount,
Expand Down
29 changes: 16 additions & 13 deletions server/db/redis.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package db

import (
"context"
"encoding/json"
"fmt"
"strconv"
Expand All @@ -11,10 +12,12 @@ import (
"github.com/appditto/natricon/server/spc"
"github.com/appditto/natricon/server/utils"
"github.com/bsm/redislock"
"github.com/go-redis/redis/v7"
"github.com/go-redis/redis/v9"
"github.com/golang/glog"
)

var CTX = context.Background()

// Prefix for all keys
const keyPrefix = "natricon"

Expand Down Expand Up @@ -53,49 +56,49 @@ func GetDB() *redisManager {

// del - Redis DEL
func (r *redisManager) del(key string) (int64, error) {
val, err := r.Client.Del(key).Result()
val, err := r.Client.Del(CTX, key).Result()
return val, err
}

// get - Redis GET
func (r *redisManager) get(key string) (string, error) {
val, err := r.Client.Get(key).Result()
val, err := r.Client.Get(CTX, key).Result()
return val, err
}

// set - Redis SET
func (r *redisManager) set(key string, value string) error {
err := r.Client.Set(key, value, 0).Err()
err := r.Client.Set(CTX, key, value, 0).Err()
return err
}

// hlen - Redis HLEN
func (r *redisManager) hlen(key string) (int64, error) {
val, err := r.Client.HLen(key).Result()
val, err := r.Client.HLen(CTX, key).Result()
return val, err
}

// hget - Redis HGET
func (r *redisManager) hget(key string, field string) (string, error) {
val, err := r.Client.HGet(key, field).Result()
val, err := r.Client.HGet(CTX, key, field).Result()
return val, err
}

// hgetall - Redis HGETALL
func (r *redisManager) hgetall(key string) (map[string]string, error) {
val, err := r.Client.HGetAll(key).Result()
val, err := r.Client.HGetAll(CTX, key).Result()
return val, err
}

// hset - Redis HSET
func (r *redisManager) hset(key string, field string, value string) error {
err := r.Client.HSet(key, field, value).Err()
err := r.Client.HSet(CTX, key, field, value).Err()
return err
}

// hdel - Redis HDEL
func (r *redisManager) hdel(key string, field string) error {
err := r.Client.HDel(key, field).Err()
err := r.Client.HDel(CTX, key, field).Err()
return err
}

Expand Down Expand Up @@ -481,7 +484,7 @@ func (r *redisManager) GetNonce(pubkey string) int {
}

func (r *redisManager) IncreaseNonce(pubkey string) int {
lock, err := r.Locker.Obtain(fmt.Sprintf("natricon:noncelock:%s", pubkey), 100*time.Second, &redislock.Options{
lock, err := r.Locker.Obtain(CTX, fmt.Sprintf("natricon:noncelock:%s", pubkey), 100*time.Second, &redislock.Options{
RetryStrategy: redislock.LimitRetry(
redislock.LinearBackoff(
1*time.Second,
Expand All @@ -495,15 +498,15 @@ func (r *redisManager) IncreaseNonce(pubkey string) int {
glog.Error(err)
return NoNonceApplied
}
defer lock.Release()
defer lock.Release(CTX)
nonce := r.GetNonce(pubkey)
nonce++
r.hset(fmt.Sprintf("%s:nonces", keyPrefix), pubkey, strconv.Itoa(nonce))
return nonce
}

func (r *redisManager) SetNonce(pubkey string, nonce int) int {
lock, err := r.Locker.Obtain(fmt.Sprintf("natricon:noncelock:%s", pubkey), 100*time.Second, &redislock.Options{
lock, err := r.Locker.Obtain(CTX, fmt.Sprintf("natricon:noncelock:%s", pubkey), 100*time.Second, &redislock.Options{
RetryStrategy: redislock.LimitRetry(
redislock.LinearBackoff(
1*time.Second,
Expand All @@ -517,7 +520,7 @@ func (r *redisManager) SetNonce(pubkey string, nonce int) int {
glog.Error(err)
return NoNonceApplied
}
defer lock.Release()
defer lock.Release(CTX)
if nonce == NoNonceApplied {
r.hdel(fmt.Sprintf("%s:nonces", keyPrefix), pubkey)
return NoNonceApplied
Expand Down
62 changes: 40 additions & 22 deletions server/go.mod
Original file line number Diff line number Diff line change
@@ -1,29 +1,47 @@
module github.com/appditto/natricon/server

go 1.14
go 1.19

require (
github.com/ajstarks/svgo v0.0.0-20200320125537-f189e35d30ca
github.com/bbedward/crypto/ed25519 v0.0.0-20200408160247-f3ed4859f246 // indirect
github.com/bbedward/nano v0.0.0-20200408160834-45efd709c9fa
github.com/bsm/redislock v0.5.0
github.com/gin-gonic/gin v1.6.3
github.com/go-playground/validator/v10 v10.3.0 // indirect
github.com/go-redis/redis/v7 v7.3.0
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b
github.com/golang/protobuf v1.4.2 // indirect
github.com/google/uuid v1.1.1
github.com/googollee/go-socket.io v1.4.3
github.com/gorilla/websocket v1.4.2 // indirect
github.com/jasonlvhit/gocron v0.0.0-20200423141508-ab84337f7963
github.com/ajstarks/svgo v0.0.0-20211024235047-1546f124cd8b
github.com/bbedward/nano v0.0.0-20200816190148-07c73bdcaff6
github.com/bsm/redislock v0.8.0
github.com/gin-gonic/gin v1.8.1
github.com/go-redis/redis/v9 v9.0.0-beta.2
github.com/golang/glog v1.0.0
github.com/google/uuid v1.3.0
github.com/googollee/go-socket.io v1.6.2
github.com/jasonlvhit/gocron v0.0.1
github.com/recws-org/recws v1.4.0
github.com/tdewolff/minify/v2 v2.12.0
gopkg.in/gographics/imagick.v3 v3.4.1
)

require (
github.com/bbedward/crypto/ed25519 v0.0.0-20220804193241-34e811d3dfe5 // indirect
github.com/cespare/xxhash/v2 v2.1.2 // indirect
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
github.com/gin-contrib/sse v0.1.0 // indirect
github.com/go-playground/locales v0.14.0 // indirect
github.com/go-playground/universal-translator v0.18.0 // indirect
github.com/go-playground/validator/v10 v10.11.0 // indirect
github.com/goccy/go-json v0.9.11 // indirect
github.com/gofrs/uuid v4.2.0+incompatible // indirect
github.com/gomodule/redigo v2.0.0+incompatible // indirect
github.com/gorilla/websocket v1.5.0 // indirect
github.com/jpillora/backoff v1.0.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/leodido/go-urn v1.2.1 // indirect
github.com/mattn/go-isatty v0.0.16 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.1 // indirect
github.com/recws-org/recws v1.2.1
github.com/tdewolff/minify/v2 v2.7.4
golang.org/x/crypto v0.0.0-20200510223506-06a226fb4e37 // indirect
golang.org/x/sys v0.0.0-20200523222454-059865788121 // indirect
google.golang.org/protobuf v1.24.0 // indirect
gopkg.in/gographics/imagick.v3 v3.3.0
gopkg.in/yaml.v2 v2.3.0 // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/pelletier/go-toml/v2 v2.0.3 // indirect
github.com/tdewolff/parse/v2 v2.6.2 // indirect
github.com/ugorji/go/codec v1.2.7 // indirect
golang.org/x/crypto v0.0.0-20220817201139-bc19a97f63c8 // indirect
golang.org/x/net v0.0.0-20220822230855-b0a4917ee28c // indirect
golang.org/x/sys v0.0.0-20220818161305-2296e01440c6 // indirect
golang.org/x/text v0.3.7 // indirect
google.golang.org/protobuf v1.28.1 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
)
Loading

0 comments on commit d873208

Please sign in to comment.