Skip to content

Commit

Permalink
remove usage of pointers in node accounts
Browse files Browse the repository at this point in the history
  • Loading branch information
skosito committed Oct 28, 2024
1 parent bff17e0 commit b5498d6
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 13 deletions.
13 changes: 11 additions & 2 deletions pkg/rpc/clients_observer.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,13 +114,22 @@ func (c *Clients) GetKeyGen(ctx context.Context) (types.Keygen, error) {
}

// GetAllNodeAccounts returns all node accounts
func (c *Clients) GetAllNodeAccounts(ctx context.Context) ([]*types.NodeAccount, error) {
func (c *Clients) GetAllNodeAccounts(ctx context.Context) ([]types.NodeAccount, error) {
resp, err := c.Observer.NodeAccountAll(ctx, &types.QueryAllNodeAccountRequest{})
if err != nil {
return nil, errors.Wrap(err, "failed to get all node accounts")
}

return resp.NodeAccount, nil
result := make([]types.NodeAccount, len(resp.NodeAccount))
for i, nodeAccount := range resp.NodeAccount {
if nodeAccount != nil {
result[i] = *nodeAccount
} else {
return nil, fmt.Errorf("node account is nil")
}
}

return result, nil
}

// GetBallot returns a ballot by ID
Expand Down
2 changes: 1 addition & 1 deletion pkg/rpc/clients_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -513,7 +513,7 @@ func TestZetacore_GetAllNodeAccounts(t *testing.T) {

resp, err := client.GetAllNodeAccounts(ctx)
require.NoError(t, err)
require.Equal(t, expectedOutput.NodeAccount, resp)
require.Equal(t, []observertypes.NodeAccount{*expectedOutput.NodeAccount[0]}, resp)
}

func TestZetacore_GetKeyGen(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion zetaclient/chains/evm/observer/observer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ func getAppContext(
chainParams,
"tssPubKey",
*sample.CrosschainFlags(),
[]*observertypes.NodeAccount{},
[]observertypes.NodeAccount{*sample.NodeAccount()},
)
require.NoError(t, err)

Expand Down
2 changes: 1 addition & 1 deletion zetaclient/chains/evm/signer/signer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ func makeCtx(t *testing.T) context.Context {
},
"tssPubKey",
observertypes.CrosschainFlags{},
[]*observertypes.NodeAccount{},
[]observertypes.NodeAccount{*sample.NodeAccount()},
)
require.NoError(t, err, "unable to update app context")

Expand Down
27 changes: 22 additions & 5 deletions zetaclient/context/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"golang.org/x/exp/slices"

"github.com/zeta-chain/node/pkg/chains"
"github.com/zeta-chain/node/pkg/crypto"
observertypes "github.com/zeta-chain/node/x/observer/types"
"github.com/zeta-chain/node/zetaclient/config"
)
Expand All @@ -38,7 +39,7 @@ type AppContext struct {
keygen observertypes.Keygen

// nodeAccounts is array of current node accounts
nodeAccounts []*observertypes.NodeAccount
nodeAccounts []observertypes.NodeAccount

mu sync.RWMutex
}
Expand All @@ -54,7 +55,7 @@ func New(cfg config.Config, relayerKeyPasswords map[string]string, logger zerolo
crosschainFlags: observertypes.CrosschainFlags{},
currentTssPubKey: "",
keygen: observertypes.Keygen{},
nodeAccounts: []*observertypes.NodeAccount{},
nodeAccounts: []observertypes.NodeAccount{},

mu: sync.RWMutex{},
}
Expand Down Expand Up @@ -124,11 +125,25 @@ func (a *AppContext) GetKeygen() observertypes.Keygen {
}
}

func (a *AppContext) GetNodeAccounts() []*observertypes.NodeAccount {
func (a *AppContext) GetNodeAccounts() []observertypes.NodeAccount {
a.mu.RLock()
defer a.mu.RUnlock()

return a.nodeAccounts
// deep copy node accounts
nodeAccounts := []observertypes.NodeAccount{}
for _, nodeAccount := range a.nodeAccounts {
nodeAccounts = append(nodeAccounts, observertypes.NodeAccount{
Operator: nodeAccount.Operator,
GranteeAddress: nodeAccount.GranteeAddress,
GranteePubkey: &crypto.PubKeySet{
Secp256k1: nodeAccount.GranteePubkey.Secp256k1,
Ed25519: nodeAccount.GranteePubkey.Ed25519,
},
NodeStatus: nodeAccount.NodeStatus,
})
}

return nodeAccounts
}

// GetCurrentTssPubKey returns the current tss pubKey.
Expand All @@ -155,14 +170,16 @@ func (a *AppContext) Update(
freshChainParams map[int64]*observertypes.ChainParams,
tssPubKey string,
crosschainFlags observertypes.CrosschainFlags,
nodeAccounts []*observertypes.NodeAccount,
nodeAccounts []observertypes.NodeAccount,
) error {
// some sanity checks
switch {
case len(freshChains) == 0:
return fmt.Errorf("no chains present")
case len(freshChainParams) == 0:
return fmt.Errorf("no chain params present")
case len(nodeAccounts) == 0:
return fmt.Errorf("node accounts empty")
case tssPubKey == "" && a.currentTssPubKey != "":
// note that if we're doing a fresh start, we ALLOW an empty tssPubKey
return fmt.Errorf("tss pubkey is empty")
Expand Down
4 changes: 2 additions & 2 deletions zetaclient/context/app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,8 @@ func TestAppContext(t *testing.T) {
fancyL2,
}

nodeAccounts := []*types.NodeAccount{
sample.NodeAccount(),
nodeAccounts := []types.NodeAccount{
*sample.NodeAccount(),
}

// ACT
Expand Down
2 changes: 1 addition & 1 deletion zetaclient/orchestrator/orchestrator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -551,7 +551,7 @@ func createAppContext(t *testing.T, chainsOrParams ...any) *zctx.AppContext {
params,
"tssPubKey",
*ccFlags,
[]*observertypes.NodeAccount{},
[]observertypes.NodeAccount{*sample.NodeAccount()},
)
require.NoError(t, err, "failed to update app context")

Expand Down

0 comments on commit b5498d6

Please sign in to comment.