Skip to content

Commit

Permalink
sync fix
Browse files Browse the repository at this point in the history
  • Loading branch information
fanhousanbu committed May 19, 2024
1 parent 66819dc commit 6569939
Show file tree
Hide file tree
Showing 11 changed files with 30 additions and 251 deletions.
62 changes: 0 additions & 62 deletions docs/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,45 +53,6 @@ const docTemplate = `{
}
}
},
"/api/account/v1/sync": {
"get": {
"security": [
{
"JWT": []
}
],
"description": "download accounts",
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"tags": [
"Account"
],
"parameters": [
{
"type": "integer",
"description": "how many accounts to download",
"name": "count",
"in": "query",
"required": true
}
],
"responses": {
"200": {
"description": "OK",
"schema": {
"type": "array",
"items": {
"$ref": "#/definitions/storage.Member"
}
}
}
}
}
},
"/api/auth/v1/login": {
"post": {
"description": "Get AccessToken By ApiKey",
Expand Down Expand Up @@ -164,29 +125,6 @@ const docTemplate = `{
"type": "string"
}
}
},
"storage.Member": {
"type": "object",
"properties": {
"hashedAccount": {
"type": "string"
},
"privateKeyVault": {
"type": "string"
},
"publicKey": {
"type": "string"
},
"rpcAddress": {
"type": "string"
},
"rpcPort": {
"type": "integer"
},
"version": {
"type": "integer"
}
}
}
},
"securityDefinitions": {
Expand Down
62 changes: 0 additions & 62 deletions docs/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,45 +42,6 @@
}
}
},
"/api/account/v1/sync": {
"get": {
"security": [
{
"JWT": []
}
],
"description": "download accounts",
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"tags": [
"Account"
],
"parameters": [
{
"type": "integer",
"description": "how many accounts to download",
"name": "count",
"in": "query",
"required": true
}
],
"responses": {
"200": {
"description": "OK",
"schema": {
"type": "array",
"items": {
"$ref": "#/definitions/storage.Member"
}
}
}
}
}
},
"/api/auth/v1/login": {
"post": {
"description": "Get AccessToken By ApiKey",
Expand Down Expand Up @@ -153,29 +114,6 @@
"type": "string"
}
}
},
"storage.Member": {
"type": "object",
"properties": {
"hashedAccount": {
"type": "string"
},
"privateKeyVault": {
"type": "string"
},
"publicKey": {
"type": "string"
},
"rpcAddress": {
"type": "string"
},
"rpcPort": {
"type": "integer"
},
"version": {
"type": "integer"
}
}
}
},
"securityDefinitions": {
Expand Down
39 changes: 0 additions & 39 deletions docs/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,6 @@ definitions:
apiKey:
type: string
type: object
storage.Member:
properties:
hashedAccount:
type: string
privateKeyVault:
type: string
publicKey:
type: string
rpcAddress:
type: string
rpcPort:
type: integer
version:
type: integer
type: object
info:
contact:
name: AAStar Support
Expand All @@ -52,30 +37,6 @@ paths:
- JWT: []
tags:
- Account
/api/account/v1/sync:
get:
consumes:
- application/json
description: download accounts
parameters:
- description: how many accounts to download
in: query
name: count
required: true
type: integer
produces:
- application/json
responses:
"200":
description: OK
schema:
items:
$ref: '#/definitions/storage.Member'
type: array
security:
- JWT: []
tags:
- Account
/api/auth/v1/login:
post:
consumes:
Expand Down
File renamed without changes.
20 changes: 10 additions & 10 deletions internal/community/node/delegate.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,27 +33,27 @@ func (d *CommunityDelegate) GetBroadcasts(overhead, limit int) [][]byte {

// LocalState return the local state data while a remote node joins or sync
func (d *CommunityDelegate) LocalState(join bool) []byte {
if ss, err := storage.GetSnapshot(); err == nil {
if join {
return ss
} else {
skip := uint32(0)
if !join {
if ss, err := storage.GetSnapshot(); err == nil {
if s, err := storage.UnmarshalSnapshot(ss); err == nil {
members := storage.GetAllMembers(s.TotalMembers)
if members != nil {
return storage.MarshalMembers(members)
}
skip = s.TotalMembers
}
}
}
members := storage.GetMembers(skip, ^uint32(0))
if len(members) > 0 {
return storage.MarshalMembers(members)
}
return nil
}

// MergeRemoteState merges the remote state while current node joins or sync
func (d *CommunityDelegate) MergeRemoteState(buf []byte, join bool) {
if len(buf) > 0 {
if join {
if snap, err := storage.UnmarshalSnapshot(buf); err == nil {
go MergeRemoteMembers(snap)
if members := storage.UnmarshalMembers(buf); len(members) > 0 {
go storage.MergeRemoteAccounts(members)
}
} else {
go func() {
Expand Down
39 changes: 0 additions & 39 deletions internal/community/node/remote_api.go

This file was deleted.

8 changes: 4 additions & 4 deletions internal/community/storage/member_itor.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"github.com/syndtr/goleveldb/leveldb/util"
)

func GetAllMembers(total uint32) []Member {
func GetMembers(skip, size uint32) []Member {
if ins, err := Open(); err != nil {
return nil
} else {
Expand All @@ -15,16 +15,16 @@ func GetAllMembers(total uint32) []Member {
iter := db.NewIterator(&util.Range{
Start: []byte(MemberPrefix),
}, nil)
i := 0
i := uint32(0)
for iter.Next() {
if i < int(total) {
if i >= skip && i < skip+size {
if m, err := Unmarshal(iter.Value()); err != nil {
return nil
} else {
members = append(members, *m)
}
i++
}
i++
}
iter.Release()
err = iter.Error()
Expand Down
9 changes: 6 additions & 3 deletions internal/community/storage/member_itor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,18 @@ import (
"os"
"reflect"
"testing"

"github.com/google/uuid"
)

func TestGetAllMembers(t *testing.T) {

dir := os.TempDir()
os.Setenv("storage", dir+"/testing.dat")
uuid := uuid.New().String()
os.Setenv("storage", dir+"/testing.dat/"+uuid)
defer func() {
os.Unsetenv("storage")
os.Remove(dir + "/testing.dat")
os.RemoveAll(dir + "/testing.dat")
}()

member1 := &Member{
Expand All @@ -34,7 +37,7 @@ func TestGetAllMembers(t *testing.T) {
UpsertMember(member2.HashedAccount, member2.PublicKey, "", member2.RpcAddress, member2.RpcPort, &member2.Version)

// Call the GetAllMembers function
members := GetAllMembers(2)
members := GetMembers(0, ^uint32(0))

// Check the returned members
expectedMembers := []Member{*member1, *member2}
Expand Down
10 changes: 10 additions & 0 deletions internal/community/storage/merge.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package storage

func MergeRemoteAccounts(accounts []Member) error {
for _, account := range accounts {
if err := MergeRemoteMember(&account); err != nil {
return err
}
}
return nil
}
31 changes: 0 additions & 31 deletions internal/web_server/controllers/account/v1/sync.go

This file was deleted.

1 change: 0 additions & 1 deletion internal/web_server/routers/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,5 @@ func buildRouters(router *gin.Engine) {
{
account := router.Group("/api/account")
account.POST("/v1/bind", account_v1.Bind)
account.GET("/v1/sync", account_v1.Sync)
}
}

0 comments on commit 6569939

Please sign in to comment.