mirror of
https://github.com/eosswedenorg/thalos
synced 2026-06-19 04:50:02 +02:00
Move internal packages into internal subdirectory
This commit is contained in:
parent
770f0a48ef
commit
ddd5d67901
8 changed files with 6 additions and 6 deletions
42
internal/abi_cache/cache.go
Normal file
42
internal/abi_cache/cache.go
Normal file
|
|
@ -0,0 +1,42 @@
|
|||
package abi_cache
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
eos "github.com/eoscanada/eos-go"
|
||||
redis_cache "github.com/go-redis/cache/v8"
|
||||
)
|
||||
|
||||
type Cache struct {
|
||||
c *redis_cache.Cache
|
||||
ctx context.Context
|
||||
prefix string
|
||||
}
|
||||
|
||||
func New(prefix string, options *redis_cache.Options) *Cache {
|
||||
return &Cache{
|
||||
c: redis_cache.New(options),
|
||||
ctx: context.Background(),
|
||||
prefix: prefix,
|
||||
}
|
||||
}
|
||||
|
||||
func (this *Cache) Get(account string) (*eos.ABI, error) {
|
||||
var v eos.ABI
|
||||
err := this.c.Get(this.ctx, this.key(account), &v)
|
||||
return &v, err
|
||||
}
|
||||
|
||||
func (this *Cache) Set(account string, abi *eos.ABI, ttl time.Duration) error {
|
||||
return this.c.Set(&redis_cache.Item{
|
||||
Ctx: this.ctx,
|
||||
Key: this.key(account),
|
||||
Value: *abi,
|
||||
TTL: ttl,
|
||||
})
|
||||
}
|
||||
|
||||
func (this *Cache) key(account string) string {
|
||||
return this.prefix + "." + account
|
||||
}
|
||||
150
internal/abi_cache/cache_test.go
Normal file
150
internal/abi_cache/cache_test.go
Normal file
|
|
@ -0,0 +1,150 @@
|
|||
package abi_cache
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
eos "github.com/eoscanada/eos-go"
|
||||
redis_cache "github.com/go-redis/cache/v8"
|
||||
"github.com/go-redis/redis/v8"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
var abiString = `
|
||||
{
|
||||
"version": "eosio::abi/1.0",
|
||||
"types": [{
|
||||
"new_type_name": "new_type_name_1",
|
||||
"type": "name"
|
||||
}],
|
||||
"structs": [
|
||||
{
|
||||
"name": "struct_name_1",
|
||||
"base": "struct_name_2",
|
||||
"fields": [
|
||||
{"name":"struct_1_field_1", "type":"new_type_name_1"},
|
||||
{"name":"struct_1_field_2", "type":"struct_name_3"},
|
||||
{"name":"struct_1_field_3", "type":"string?"},
|
||||
{"name":"struct_1_field_4", "type":"string?"},
|
||||
{"name":"struct_1_field_5", "type":"struct_name_4[]"}
|
||||
]
|
||||
},{
|
||||
"name": "struct_name_2",
|
||||
"base": "",
|
||||
"fields": [
|
||||
{"name":"struct_2_field_1", "type":"string"}
|
||||
]
|
||||
},{
|
||||
"name": "struct_name_3",
|
||||
"base": "",
|
||||
"fields": [
|
||||
{"name":"struct_3_field_1", "type":"string"}
|
||||
]
|
||||
},{
|
||||
"name": "struct_name_4",
|
||||
"base": "",
|
||||
"fields": [
|
||||
{"name":"struct_4_field_1", "type":"string"}
|
||||
]
|
||||
}
|
||||
],
|
||||
"actions": [{
|
||||
"name": "action_name_1",
|
||||
"type": "struct_name_1",
|
||||
"ricardian_contract": ""
|
||||
}],
|
||||
"tables": [{
|
||||
"name": "table_name_1",
|
||||
"index_type": "i64",
|
||||
"key_names": [
|
||||
"key_name_1",
|
||||
"key_name_2"
|
||||
],
|
||||
"key_types": [
|
||||
"string",
|
||||
"int"
|
||||
],
|
||||
"type": "struct_name_1"
|
||||
}
|
||||
]
|
||||
}
|
||||
`
|
||||
|
||||
func TestGetSet(t *testing.T) {
|
||||
c := New("abi.cache.test", &redis_cache.Options{
|
||||
Redis: redis.NewClient(&redis.Options{}),
|
||||
// Cache 10k keys for 1 minute.
|
||||
LocalCache: redis_cache.NewTinyLFU(10000, time.Minute),
|
||||
})
|
||||
|
||||
abi, err := eos.NewABI(strings.NewReader(abiString))
|
||||
assert.NoError(t, err)
|
||||
|
||||
err = c.Set("testaccount", abi, time.Minute)
|
||||
assert.NoError(t, err)
|
||||
|
||||
c_abi, err := c.Get("testaccount")
|
||||
assert.NoError(t, err)
|
||||
|
||||
assert.Equal(t, c_abi.Version, "eosio::abi/1.0")
|
||||
|
||||
// Types
|
||||
assert.Equal(t, c_abi.Types[0].NewTypeName, "new_type_name_1")
|
||||
assert.Equal(t, c_abi.Types[0].Type, "name")
|
||||
|
||||
// Structs
|
||||
assert.Equal(t, c_abi.Structs[0].Name, "struct_name_1")
|
||||
assert.Equal(t, c_abi.Structs[0].Base, "struct_name_2")
|
||||
assert.Equal(t, c_abi.Structs[0].Fields[0].Name, "struct_1_field_1")
|
||||
assert.Equal(t, c_abi.Structs[0].Fields[0].Type, "new_type_name_1")
|
||||
assert.Equal(t, c_abi.Structs[0].Fields[1].Name, "struct_1_field_2")
|
||||
assert.Equal(t, c_abi.Structs[0].Fields[1].Type, "struct_name_3")
|
||||
assert.Equal(t, c_abi.Structs[0].Fields[2].Name, "struct_1_field_3")
|
||||
assert.Equal(t, c_abi.Structs[0].Fields[2].Type, "string?")
|
||||
assert.Equal(t, c_abi.Structs[0].Fields[3].Name, "struct_1_field_4")
|
||||
assert.Equal(t, c_abi.Structs[0].Fields[3].Type, "string?")
|
||||
assert.Equal(t, c_abi.Structs[0].Fields[4].Name, "struct_1_field_5")
|
||||
assert.Equal(t, c_abi.Structs[0].Fields[4].Type, "struct_name_4[]")
|
||||
|
||||
assert.Equal(t, c_abi.Structs[1].Name, "struct_name_2")
|
||||
assert.Equal(t, c_abi.Structs[1].Base, "")
|
||||
assert.Equal(t, c_abi.Structs[1].Fields[0].Name, "struct_2_field_1")
|
||||
assert.Equal(t, c_abi.Structs[1].Fields[0].Type, "string")
|
||||
|
||||
assert.Equal(t, c_abi.Structs[2].Name, "struct_name_3")
|
||||
assert.Equal(t, c_abi.Structs[2].Base, "")
|
||||
assert.Equal(t, c_abi.Structs[2].Fields[0].Name, "struct_3_field_1")
|
||||
assert.Equal(t, c_abi.Structs[2].Fields[0].Type, "string")
|
||||
|
||||
assert.Equal(t, c_abi.Structs[3].Name, "struct_name_4")
|
||||
assert.Equal(t, c_abi.Structs[3].Base, "")
|
||||
assert.Equal(t, c_abi.Structs[3].Fields[0].Name, "struct_4_field_1")
|
||||
assert.Equal(t, c_abi.Structs[3].Fields[0].Type, "string")
|
||||
|
||||
// Actions
|
||||
assert.Equal(t, c_abi.Actions[0].Name, eos.ActN("action_name_1"))
|
||||
assert.Equal(t, c_abi.Actions[0].Type, "struct_name_1")
|
||||
assert.Equal(t, c_abi.Actions[0].RicardianContract, "")
|
||||
|
||||
// Tables
|
||||
assert.Equal(t, c_abi.Tables[0].Name, eos.TableName("table_name_1"))
|
||||
assert.Equal(t, c_abi.Tables[0].Type, "struct_name_1")
|
||||
assert.Equal(t, c_abi.Tables[0].IndexType, "i64")
|
||||
assert.Equal(t, c_abi.Tables[0].KeyNames[0], "key_name_1")
|
||||
assert.Equal(t, c_abi.Tables[0].KeyNames[1], "key_name_2")
|
||||
assert.Equal(t, c_abi.Tables[0].KeyTypes[0], "string")
|
||||
assert.Equal(t, c_abi.Tables[0].KeyTypes[1], "int")
|
||||
}
|
||||
|
||||
func TestCacheMiss(t *testing.T) {
|
||||
c := New("abi.cache.test", &redis_cache.Options{
|
||||
Redis: redis.NewClient(&redis.Options{}),
|
||||
// Cache 10k keys for 1 minute.
|
||||
LocalCache: redis_cache.NewTinyLFU(10000, time.Minute),
|
||||
})
|
||||
|
||||
_, err := c.Get("nonexist")
|
||||
assert.Error(t, err)
|
||||
}
|
||||
63
internal/config/config.go
Normal file
63
internal/config/config.go
Normal file
|
|
@ -0,0 +1,63 @@
|
|||
package config
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"io/ioutil"
|
||||
)
|
||||
|
||||
const NULL_BLOCK_NUMBER uint32 = 0xffffffff
|
||||
|
||||
type RedisConfig struct {
|
||||
Addr string `json:"addr"`
|
||||
Password string `json:"password"`
|
||||
DB int `json:"db"`
|
||||
CacheID string `json:"cache_id"`
|
||||
Prefix string `json:"prefix"`
|
||||
}
|
||||
|
||||
type TelegramConfig struct {
|
||||
Id string `json:"id"`
|
||||
Channel int64 `json:"channel"`
|
||||
}
|
||||
|
||||
type Config struct {
|
||||
Name string `json:"name"`
|
||||
ShipApi string `json:"ship_api"`
|
||||
Api string `json:"api"`
|
||||
|
||||
Redis RedisConfig `json:"redis"`
|
||||
|
||||
Telegram TelegramConfig `json:"telegram"`
|
||||
|
||||
IrreversibleOnly bool `json:"irreversible_only"`
|
||||
MaxMessagesInFlight uint32 `json:"max_messages_in_flight"`
|
||||
StartBlockNum uint32 `json:"start_block_num"`
|
||||
EndBlockNum uint32 `json:"end_block_num"`
|
||||
}
|
||||
|
||||
func Load(filename string) (Config, error) {
|
||||
cfg := Config{
|
||||
StartBlockNum: NULL_BLOCK_NUMBER,
|
||||
EndBlockNum: NULL_BLOCK_NUMBER,
|
||||
MaxMessagesInFlight: 10,
|
||||
IrreversibleOnly: false,
|
||||
Redis: RedisConfig{
|
||||
Addr: "localhost:6379",
|
||||
Password: "",
|
||||
DB: 0,
|
||||
Prefix: "ship",
|
||||
},
|
||||
}
|
||||
|
||||
bytes, err := ioutil.ReadFile(filename)
|
||||
if err != nil {
|
||||
return cfg, err
|
||||
}
|
||||
|
||||
err = json.Unmarshal(bytes, &cfg)
|
||||
if err != nil {
|
||||
return cfg, err
|
||||
}
|
||||
|
||||
return cfg, nil
|
||||
}
|
||||
62
internal/redis/wrapper.go
Normal file
62
internal/redis/wrapper.go
Normal file
|
|
@ -0,0 +1,62 @@
|
|||
package redis
|
||||
|
||||
import (
|
||||
"context"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
_redis "github.com/go-redis/redis/v8"
|
||||
)
|
||||
|
||||
var rdb *_redis.Client
|
||||
|
||||
var redis_pipe _redis.Pipeliner
|
||||
|
||||
var redisCtx = context.Background()
|
||||
|
||||
var prefix []string
|
||||
|
||||
func Connect(addr string, password string, db int) error {
|
||||
rdb = _redis.NewClient(&_redis.Options{
|
||||
Addr: addr,
|
||||
Password: password,
|
||||
DB: db,
|
||||
})
|
||||
|
||||
redis_pipe = rdb.Pipeline()
|
||||
|
||||
return rdb.Ping(redisCtx).Err()
|
||||
}
|
||||
|
||||
func Client() *_redis.Client {
|
||||
return rdb
|
||||
}
|
||||
|
||||
func SetPrefix(components ...string) {
|
||||
prefix = components
|
||||
}
|
||||
|
||||
func Key(components ...string) string {
|
||||
components = append(prefix, components...)
|
||||
return strings.Join(components, ".")
|
||||
}
|
||||
|
||||
func Get(key string) *_redis.StringCmd {
|
||||
return rdb.Get(redisCtx, key)
|
||||
}
|
||||
|
||||
func Set(key string, value interface{}, expiration time.Duration) *_redis.StatusCmd {
|
||||
return rdb.Set(redisCtx, key, value, expiration)
|
||||
}
|
||||
|
||||
func Publish(channel string, message interface{}) *_redis.IntCmd {
|
||||
return rdb.Publish(redisCtx, channel, message)
|
||||
}
|
||||
|
||||
func RegisterPublish(channel string, message interface{}) *_redis.IntCmd {
|
||||
return redis_pipe.Publish(redisCtx, channel, message)
|
||||
}
|
||||
|
||||
func Send() ([]_redis.Cmder, error) {
|
||||
return redis_pipe.Exec(redisCtx)
|
||||
}
|
||||
30
internal/telegram/wrapper.go
Normal file
30
internal/telegram/wrapper.go
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
package telegram
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
_api "github.com/go-telegram-bot-api/telegram-bot-api/v5"
|
||||
)
|
||||
|
||||
var (
|
||||
_bot *_api.BotAPI
|
||||
_channel int64
|
||||
_prefix string
|
||||
)
|
||||
|
||||
func Init(prefix string, id string, channel int64) error {
|
||||
var err error
|
||||
|
||||
_bot, err = _api.NewBotAPI(id)
|
||||
if err == nil {
|
||||
_prefix = prefix
|
||||
_channel = channel
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
func Send(message string) error {
|
||||
msg := _api.NewMessage(_channel, fmt.Sprintf("%s: %s", _prefix, message))
|
||||
_, err := _bot.Send(msg)
|
||||
return err
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue