mirror of
https://github.com/eosswedenorg/thalos
synced 2026-06-18 04:40:03 +02:00
move abi directory to app/abi
This commit is contained in:
parent
da7056b2da
commit
a8183a6766
6 changed files with 2 additions and 2 deletions
42
app/abi/cache.go
Normal file
42
app/abi/cache.go
Normal file
|
|
@ -0,0 +1,42 @@
|
|||
package abi
|
||||
|
||||
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 NewCache(prefix string, options *redis_cache.Options) *Cache {
|
||||
return &Cache{
|
||||
c: redis_cache.New(options),
|
||||
ctx: context.Background(),
|
||||
prefix: prefix,
|
||||
}
|
||||
}
|
||||
|
||||
func (cache *Cache) Get(account string) (*eos.ABI, error) {
|
||||
var v eos.ABI
|
||||
err := cache.c.Get(cache.ctx, cache.key(account), &v)
|
||||
return &v, err
|
||||
}
|
||||
|
||||
func (cache *Cache) Set(account string, abi *eos.ABI, ttl time.Duration) error {
|
||||
return cache.c.Set(&redis_cache.Item{
|
||||
Ctx: cache.ctx,
|
||||
Key: cache.key(account),
|
||||
Value: *abi,
|
||||
TTL: ttl,
|
||||
})
|
||||
}
|
||||
|
||||
func (cache *Cache) key(account string) string {
|
||||
return cache.prefix + "." + account
|
||||
}
|
||||
158
app/abi/cache_test.go
Normal file
158
app/abi/cache_test.go
Normal file
|
|
@ -0,0 +1,158 @@
|
|||
package abi
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
eos "github.com/eoscanada/eos-go"
|
||||
redis_cache "github.com/go-redis/cache/v8"
|
||||
"github.com/go-redis/redismock/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) {
|
||||
client, mock := redismock.NewClientMock()
|
||||
|
||||
c := NewCache("abi.cache.test", &redis_cache.Options{
|
||||
Redis: client,
|
||||
// Cache 10k keys for 1 minute.
|
||||
LocalCache: redis_cache.NewTinyLFU(10000, time.Minute),
|
||||
})
|
||||
|
||||
abi, err := eos.NewABI(strings.NewReader(abiString))
|
||||
assert.NoError(t, err)
|
||||
|
||||
bytes, _ := c.c.Marshal(*abi)
|
||||
|
||||
mock.ExpectSet("abi.cache.test.testaccount", bytes, time.Minute).SetVal("OK")
|
||||
|
||||
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) {
|
||||
client, _ := redismock.NewClientMock()
|
||||
|
||||
c := NewCache("abi.cache.test", &redis_cache.Options{
|
||||
Redis: client,
|
||||
// Cache 10k keys for 1 minute.
|
||||
LocalCache: redis_cache.NewTinyLFU(10000, time.Minute),
|
||||
})
|
||||
|
||||
_, err := c.Get("nonexist")
|
||||
assert.Error(t, err)
|
||||
}
|
||||
19
app/abi/helpers.go
Normal file
19
app/abi/helpers.go
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
package abi
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
|
||||
eos "github.com/eoscanada/eos-go"
|
||||
)
|
||||
|
||||
func DecodeAction(eos_ABI *eos.ABI, data []byte, actionName eos.ActionName) (interface{}, error) {
|
||||
var v interface{}
|
||||
|
||||
bytes, err := eos_ABI.DecodeAction(data, actionName)
|
||||
if err != nil {
|
||||
return v, err
|
||||
}
|
||||
|
||||
err = json.Unmarshal(bytes, &v)
|
||||
return v, err
|
||||
}
|
||||
51
app/abi/manager.go
Normal file
51
app/abi/manager.go
Normal file
|
|
@ -0,0 +1,51 @@
|
|||
package abi
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
eos "github.com/eoscanada/eos-go"
|
||||
redis_cache "github.com/go-redis/cache/v8"
|
||||
"github.com/go-redis/redis/v8"
|
||||
)
|
||||
|
||||
type AbiManager struct {
|
||||
cache *Cache
|
||||
api *eos.API
|
||||
ctx context.Context
|
||||
}
|
||||
|
||||
func NewAbiManager(rdb *redis.Client, api *eos.API, id string) *AbiManager {
|
||||
// Init abi cache
|
||||
cache := NewCache("ship.cache."+id+".abi", &redis_cache.Options{
|
||||
Redis: rdb,
|
||||
// Cache 10k keys for 10 minutes.
|
||||
LocalCache: redis_cache.NewTinyLFU(10000, 10*time.Minute),
|
||||
})
|
||||
|
||||
return &AbiManager{
|
||||
cache: cache,
|
||||
api: api,
|
||||
ctx: context.Background(),
|
||||
}
|
||||
}
|
||||
|
||||
func (mgr *AbiManager) GetAbi(account eos.AccountName) (*eos.ABI, error) {
|
||||
key := string(account)
|
||||
|
||||
abi, err := mgr.cache.Get(key)
|
||||
if err != nil {
|
||||
resp, err := mgr.api.GetABI(mgr.ctx, account)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("api: %s", err)
|
||||
}
|
||||
abi = &resp.ABI
|
||||
|
||||
err = mgr.cache.Set(key, abi, time.Hour)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("cache: %s", err)
|
||||
}
|
||||
}
|
||||
return abi, nil
|
||||
}
|
||||
|
|
@ -4,9 +4,9 @@ import (
|
|||
"encoding/hex"
|
||||
"encoding/json"
|
||||
|
||||
"github.com/eosswedenorg/thalos/abi"
|
||||
"github.com/eosswedenorg/thalos/api"
|
||||
"github.com/eosswedenorg/thalos/api/message"
|
||||
"github.com/eosswedenorg/thalos/app/abi"
|
||||
|
||||
log "github.com/sirupsen/logrus"
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue