Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[feat]Basic Infra for node/storage #3

Merged
merged 4 commits into from
Apr 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ require (

require (
github.com/KyleBanks/depth v1.2.1 // indirect
github.com/PuerkitoBio/purell v1.2.1 // indirect
github.com/PuerkitoBio/urlesc v0.0.0-20170810143723-de5bf2ad4578 // indirect
github.com/armon/go-metrics v0.0.0-20180917152333-f0300d1749da // indirect
github.com/chenzhuoyu/iasm v0.9.1 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
Expand Down
56 changes: 6 additions & 50 deletions go.sum

Large diffs are not rendered by default.

17 changes: 11 additions & 6 deletions internal/community/instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
"another_node/conf"
"another_node/internal/community/node"
"another_node/internal/community/storage"
"fmt"
)

type Community struct {
Expand All @@ -19,24 +18,30 @@
}
}

// BindAccount binding a web2 account
func BindAccount(hashedAccount string, publicKey *string) error {
privateKeyValut := "WIP Private Key Vault"

Check warning on line 23 in internal/community/instance.go

View check run for this annotation

Codecov / codecov/patch

internal/community/instance.go#L23

Added line #L23 was not covered by tests

if publicKey == nil {
// TODO: auto dispatch a web3 account
publicKey = new(string)
*publicKey = "WIP"
privateKeyValut = "Auto Dispatched Private Key Vault"

Check warning on line 29 in internal/community/instance.go

View check run for this annotation

Codecov / codecov/patch

internal/community/instance.go#L29

Added line #L29 was not covered by tests
}

rpcAddress := new(string)
*rpcAddress = fmt.Sprintf("%s:%d", conf.GetNode().ExternalAddr, conf.GetNode().ExternalPort)
rpcAddress := conf.GetNode().ExternalAddr
rpcPort := conf.GetNode().ExternalPort
version := 0

Check warning on line 34 in internal/community/instance.go

View check run for this annotation

Codecov / codecov/patch

internal/community/instance.go#L32-L34

Added lines #L32 - L34 were not covered by tests

if err := storage.UpsertMember(hashedAccount, publicKey, nil, rpcAddress, 0); err != nil {
if err := storage.UpsertMember(hashedAccount, *publicKey, privateKeyValut, rpcAddress, rpcPort, &version); err != nil {

Check warning on line 36 in internal/community/instance.go

View check run for this annotation

Codecov / codecov/patch

internal/community/instance.go#L36

Added line #L36 was not covered by tests
return err
} else {
return community.Node.Broadcast(&node.Payload{
Account: hashedAccount,
PublicKey: *publicKey,
RpcAddress: *rpcAddress,
Version: 0,
RpcAddress: rpcAddress,
RpcPort: rpcPort,
Version: *&version,

Check warning on line 44 in internal/community/instance.go

View check run for this annotation

Codecov / codecov/patch

internal/community/instance.go#L42-L44

Added lines #L42 - L44 were not covered by tests
})
}
}
2 changes: 1 addition & 1 deletion internal/community/node/mapping.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@
}

func UpcomingHandler(payload *Payload) {
_ = storage.UpsertMember(payload.Account, &payload.PublicKey, nil, &payload.RpcAddress, payload.Version)
_ = storage.UpsertMember(payload.Account, payload.PublicKey, "", payload.RpcAddress, payload.RpcPort, &payload.Version)

Check warning on line 15 in internal/community/node/mapping.go

View check run for this annotation

Codecov / codecov/patch

internal/community/node/mapping.go#L15

Added line #L15 was not covered by tests
}
1 change: 1 addition & 0 deletions internal/community/node/payload.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@ type Payload struct {
Account string
PublicKey string
RpcAddress string
RpcPort int
Version int
}
53 changes: 38 additions & 15 deletions internal/community/storage/member.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
Model
HashedAccount string `gorm:"column:hashed_account; type:varchar(1024); not null; uniqueIndex"`
RpcAddress string `gorm:"column:rpc_address; type:varchar(128); not null"`
RpcPort int `gorm:"column:rpc_port; type:int; not null; default:0"`
PublicKey string `gorm:"column:public_key; type:varchar(1024)"`
PrivateKeyVault *string `gorm:"column:private_key_vault; type:varchar(1024); null"`
}
Expand All @@ -22,7 +23,7 @@
}

// UpsertMember update a member if exists and newer than old by version
func UpsertMember(hashedAccount string, publicKey, privateKey, rpcAddress *string, version int) error {
func UpsertMember(hashedAccount, publicKey, privateKey, rpcAddress string, rpcPort int, version *int) error {
db := conf.GetDbClient()

return db.Transaction(func(tx *gorm.DB) error {
Expand All @@ -32,43 +33,65 @@
return tx.Omit("updated_at", "id").Create(&Member{
Model: Model{
CreatedAt: time.Now(),
Version: uint(version),
Version: uint(*version),
},
HashedAccount: hashedAccount,
PublicKey: *publicKey,
PrivateKeyVault: privateKey,
RpcAddress: *rpcAddress,
HashedAccount: hashedAccount,
PublicKey: publicKey,
PrivateKeyVault: func() *string {
if len(privateKey) == 0 {
return nil

Check warning on line 42 in internal/community/storage/member.go

View check run for this annotation

Codecov / codecov/patch

internal/community/storage/member.go#L42

Added line #L42 was not covered by tests
} else {
return &privateKey
}
}(),
RpcAddress: rpcAddress,
RpcPort: rpcPort,
}).Error
} else {
if member.Version >= uint(version) {
if member.Version >= uint(*version) {
*version = int(member.Version)
return nil
}

if publicKey == nil {
if len(publicKey) == 0 {

Check warning on line 56 in internal/community/storage/member.go

View check run for this annotation

Codecov / codecov/patch

internal/community/storage/member.go#L56

Added line #L56 was not covered by tests
tx.Omit("public_key")
}
if privateKey == nil {
if len(privateKey) == 0 {

Check warning on line 59 in internal/community/storage/member.go

View check run for this annotation

Codecov / codecov/patch

internal/community/storage/member.go#L59

Added line #L59 was not covered by tests
tx.Omit("private_key_vault")
}
if rpcAddress == nil {
if len(rpcAddress) == 0 || rpcPort == 0 {

Check warning on line 62 in internal/community/storage/member.go

View check run for this annotation

Codecov / codecov/patch

internal/community/storage/member.go#L62

Added line #L62 was not covered by tests
tx.Omit("rpc_address")
tx.Omit("rpc_port")

Check warning on line 64 in internal/community/storage/member.go

View check run for this annotation

Codecov / codecov/patch

internal/community/storage/member.go#L64

Added line #L64 was not covered by tests
}
err := tx.Where("id=?", member.ID).Updates(Member{
PublicKey: func() string {
if publicKey != nil {
return *publicKey
if len(publicKey) > 0 {
return publicKey

Check warning on line 69 in internal/community/storage/member.go

View check run for this annotation

Codecov / codecov/patch

internal/community/storage/member.go#L68-L69

Added lines #L68 - L69 were not covered by tests
} else {
return member.PublicKey
}
}(),
PrivateKeyVault: privateKey,
PrivateKeyVault: func() *string {
if len(privateKey) > 0 {
return &privateKey
} else {
return member.PrivateKeyVault

Check warning on line 78 in internal/community/storage/member.go

View check run for this annotation

Codecov / codecov/patch

internal/community/storage/member.go#L74-L78

Added lines #L74 - L78 were not covered by tests
}
}(),
RpcAddress: func() string {
if rpcAddress != nil {
return *rpcAddress
if len(rpcAddress) > 0 {
return rpcAddress

Check warning on line 83 in internal/community/storage/member.go

View check run for this annotation

Codecov / codecov/patch

internal/community/storage/member.go#L82-L83

Added lines #L82 - L83 were not covered by tests
} else {
return member.RpcAddress
}
}(),
RpcPort: func() int {
if rpcPort > 0 {
return rpcPort
} else {
return member.RpcPort

Check warning on line 92 in internal/community/storage/member.go

View check run for this annotation

Codecov / codecov/patch

internal/community/storage/member.go#L88-L92

Added lines #L88 - L92 were not covered by tests
}
}(),
}).Error

return err
Expand Down
13 changes: 9 additions & 4 deletions internal/community/storage/member_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@ func TestMember(t *testing.T) {
prepare_test()

ptr := "B"
assert.NoError(t, UpsertMember("A", &ptr, &ptr, &ptr, 1))
assert.NoError(t, UpsertMember("A", &ptr, &ptr, &ptr, 2))
prtN := 1
ver := 1
assert.NoError(t, UpsertMember("A", ptr, ptr, ptr, prtN, &ver))
assert.NoError(t, UpsertMember("A", ptr, ptr, ptr, prtN, &ver))
}

func TestFindMember(t *testing.T) {
Expand All @@ -28,10 +30,13 @@ func TestFindMember(t *testing.T) {
}
prepare_test()

ptrN := 1
ver := 1

ptr := "B"
assert.NoError(t, UpsertMember("A", &ptr, &ptr, &ptr, 1))
assert.NoError(t, UpsertMember("A", ptr, ptr, ptr, ptrN, &ver))
ptr = "C"
assert.NoError(t, UpsertMember("A", &ptr, &ptr, &ptr, 1))
assert.NoError(t, UpsertMember("A", ptr, ptr, ptr, ptrN, &ver))

member, err := TryFindMember("A")
assert.NoError(t, err)
Expand Down
7 changes: 0 additions & 7 deletions internal/community/storage/type.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
package storage

import (
"os"
"time"

"gorm.io/gorm"
)

var rpcAddress string

type Model struct {
ID uint `gorm:"column:id; primarykey"`
CreatedAt time.Time `gorm:"column:created_at; autoCreateTime"`
Expand All @@ -17,7 +14,3 @@ type Model struct {
DeletedBy string `gorm:"column:deleted_by; type: varchar(1024); null"`
Version uint `gorm:"column:version; default:0;"`
}

func init() {
rpcAddress = os.Getenv("RPC_ADDRESS")
}