Skip to content

Commit

Permalink
WIP: broadcast and listen
Browse files Browse the repository at this point in the history
  • Loading branch information
fanhousanbu committed Apr 23, 2024
1 parent 839cc13 commit 9b03b9b
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 26 deletions.
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 @@ import (
"another_node/conf"
"another_node/internal/community/node"
"another_node/internal/community/storage"
"fmt"
)

type Community struct {
Expand All @@ -19,24 +18,30 @@ func New(n *node.Node) {
}
}

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

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

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

if err := storage.UpsertMember(hashedAccount, publicKey, nil, rpcAddress, 0); err != nil {
if err := storage.UpsertMember(hashedAccount, *publicKey, privateKeyValut, rpcAddress, rpcPort, &version); err != nil {
return err
} else {
return community.Node.Broadcast(&node.Payload{
Account: hashedAccount,
PublicKey: *publicKey,
RpcAddress: *rpcAddress,
Version: 0,
RpcAddress: rpcAddress,
RpcPort: rpcPort,
Version: *&version,
})
}
}
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 GetPublicKey(hashedAccount *string) string {
}

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)
}
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
}
33 changes: 25 additions & 8 deletions internal/community/storage/member.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ type Member struct {
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 @@ func (m *Member) TableName() string {
}

// 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,15 +33,23 @@ func UpsertMember(hashedAccount string, publicKey, privateKey, rpcAddress *strin
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
} 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
}

Expand All @@ -50,8 +59,9 @@ func UpsertMember(hashedAccount string, publicKey, privateKey, rpcAddress *strin
if privateKey == nil {

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

View workflow job for this annotation

GitHub Actions / build

invalid operation: privateKey == nil (mismatched types string and untyped nil)
tx.Omit("private_key_vault")
}
if rpcAddress == nil {
if rpcAddress == nil || rpcPort == nil {

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

View workflow job for this annotation

GitHub Actions / build

invalid operation: rpcAddress == nil (mismatched types string and untyped nil)

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

View workflow job for this annotation

GitHub Actions / build

invalid operation: rpcPort == nil (mismatched types int and untyped nil)
tx.Omit("rpc_address")
tx.Omit("rpc_port")
}
err := tx.Where("id=?", member.ID).Updates(Member{
PublicKey: func() string {
Expand All @@ -69,6 +79,13 @@ func UpsertMember(hashedAccount string, publicKey, privateKey, rpcAddress *strin
return member.RpcAddress
}
}(),
RpcPort: func() int {
if rpcPort != nil {

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

View workflow job for this annotation

GitHub Actions / build

invalid operation: rpcPort != nil (mismatched types int and untyped nil)
return *rpcPort
} else {
return member.RpcPort
}
}(),
}).Error

return err
Expand Down
11 changes: 7 additions & 4 deletions internal/community/storage/member_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@ 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
assert.NoError(t, UpsertMember("A", &ptr, &ptr, &ptr, &prtN, 1))
assert.NoError(t, UpsertMember("A", &ptr, &ptr, &ptr, &prtN, 2))
}

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

prtN := 1

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

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")
}

0 comments on commit 9b03b9b

Please sign in to comment.