Skip to content

Commit

Permalink
Fixed Sui linter issues (#4)
Browse files Browse the repository at this point in the history
  • Loading branch information
mdulin2 authored Nov 22, 2024
1 parent cf31ccb commit 53c2ce2
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 14 deletions.
10 changes: 8 additions & 2 deletions node/cmd/transfer-verifier/transfer-verifier-sui.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,10 @@ func runTransferVerifierSui(cmd *cobra.Command, args []string) {
logger.Info("Processing initial events")
for _, event := range initialEvents {
if event.ID.TxDigest != nil {
suiTransferVerifier.ProcessDigest(*event.ID.TxDigest, suiApiConnection, logger)
_, err = suiTransferVerifier.ProcessDigest(*event.ID.TxDigest, suiApiConnection, logger)
if err != nil {
logger.Error(err.Error())
}
}
}
}
Expand Down Expand Up @@ -152,7 +155,10 @@ func runTransferVerifierSui(cmd *cobra.Command, args []string) {
}

for _, txDigest := range txDigests {
suiTransferVerifier.ProcessDigest(txDigest, suiApiConnection, logger)
_, err := suiTransferVerifier.ProcessDigest(txDigest, suiApiConnection, logger)
if err != nil {
logger.Error(err.Error())
}
}

logger.Info("New events processed", zap.Int("latestTimestamp", latestTimestamp), zap.Int("txDigestCount", len(txDigests)))
Expand Down
6 changes: 5 additions & 1 deletion node/pkg/transfer-verifier/transfer-verifier-sui-structs.go
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,11 @@ func (r SuiTryMultiGetPastObjectsResult) GetTokenAddress() (tokenAddress string,
addrBytes := make([]byte, len(data))

for i, v := range data {
addrBytes[i] = byte(v.(float64))
if f, ok := v.(float64); ok {
addrBytes[i] = byte(f)
} else {
return "", fmt.Errorf("error in converting token data to float type")
}
}

return hex.EncodeToString(addrBytes), nil
Expand Down
11 changes: 4 additions & 7 deletions node/pkg/transfer-verifier/transfer-verifier-sui.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ func (s *SuiTransferVerifier) processEvents(events []SuiEvent, logger *zap.Logge
return requestedOutOfBridge, numEventsProcessed
}

func (s *SuiTransferVerifier) processObjectUpdates(objectChanges []ObjectChange, suiApiConnection SuiApiInterface, logger *zap.Logger) (transferredIntoBridge map[string]*big.Int, numChangesProcessed int, err error) {
func (s *SuiTransferVerifier) processObjectUpdates(objectChanges []ObjectChange, suiApiConnection SuiApiInterface, logger *zap.Logger) (transferredIntoBridge map[string]*big.Int, numChangesProcessed int) {
transferredIntoBridge = make(map[string]*big.Int)

for _, objectChange := range objectChanges {
Expand Down Expand Up @@ -161,7 +161,7 @@ func (s *SuiTransferVerifier) processObjectUpdates(objectChanges []ObjectChange,
numChangesProcessed++
}

return transferredIntoBridge, numChangesProcessed, nil
return transferredIntoBridge, numChangesProcessed
}

func (s *SuiTransferVerifier) ProcessDigest(digest string, suiApiConnection SuiApiInterface, logger *zap.Logger) (uint, error) {
Expand All @@ -176,11 +176,7 @@ func (s *SuiTransferVerifier) ProcessDigest(digest string, suiApiConnection SuiA
requestedOutOfBridge, numEventsProcessed := s.processEvents(txBlock.Result.Events, logger)

// process all object changes, indicating funds that are entering the chain
transferredIntoBridge, numChangesProcessed, err := s.processObjectUpdates(txBlock.Result.ObjectChanges, suiApiConnection, logger)

if err != nil {
logger.Fatal("Error in processing object changes", zap.Error(err))
}
transferredIntoBridge, numChangesProcessed := s.processObjectUpdates(txBlock.Result.ObjectChanges, suiApiConnection, logger)

// TODO: Using `Warn` for testing purposes. Update to Fatal? when ready to go into PR.
// TODO: Revisit error handling here.
Expand Down Expand Up @@ -224,6 +220,7 @@ func suiApiRequest[T SuiApiResponse](rpc string, method string, params string) (
// Create the request
requestBody := fmt.Sprintf(`{"jsonrpc":"2.0", "id": 1, "method": "%s", "params": %s}`, method, params)

//nolint:noctx
req, err := http.NewRequest("POST", rpc, strings.NewReader(requestBody))
if err != nil {
return defaultT, fmt.Errorf("cannot create request: %w", err)
Expand Down
8 changes: 6 additions & 2 deletions node/pkg/transfer-verifier/transfer-verifier-sui_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,11 @@ func TestNewSuiApiConnection(t *testing.T) {
sampleUrl := "http://localhost:8080"

api := NewSuiApiConnection(sampleUrl)
assert.Equal(t, sampleUrl, api.(*SuiApiConnection).rpc)
if rpc, ok := api.(*SuiApiConnection); ok {
assert.Equal(t, sampleUrl, rpc.rpc)
} else {
t.Errorf("Unable to get RPC from SuiApiConnection")
}
}

func TestProcessEvents(t *testing.T) {
Expand Down Expand Up @@ -687,7 +691,7 @@ func TestProcessObjectUpdates(t *testing.T) {
}

// Run function and check results
transferredIntoBridge, numEventsProcessed, _ := suiTxVerifier.processObjectUpdates(tt.objectChanges, connection, logger)
transferredIntoBridge, numEventsProcessed := suiTxVerifier.processObjectUpdates(tt.objectChanges, connection, logger)
assert.Equal(t, tt.expectedResult, transferredIntoBridge)
assert.Equal(t, tt.expectedCount, numEventsProcessed)
})
Expand Down
13 changes: 11 additions & 2 deletions node/pkg/transfer-verifier/transfer-verifier-utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,22 @@ func extractFromJsonPath[T any](data json.RawMessage, path string) (T, error) {
if obj[key] == nil {
return defaultT, fmt.Errorf("key %s not found", key)
}
obj = obj[key].(map[string]interface{})

if v, ok := obj[key].(map[string]interface{}); ok {
obj = v
} else {
return defaultT, fmt.Errorf("can't convert to key to map[string]interface{} type")
}
}

// If the final key exists in the object, return the value as T. Otherwise,
// return an error.
if value, exists := obj[keys[len(keys)-1]]; exists {
return value.(T), nil
if v, ok := value.(T); ok {
return v, nil
} else {
return defaultT, fmt.Errorf("can't convert to type T")
}
} else {
return defaultT, fmt.Errorf("key %s not found", keys[len(keys)-1])
}
Expand Down

0 comments on commit 53c2ce2

Please sign in to comment.