Skip to content

Commit

Permalink
Fix Sui linter errors
Browse files Browse the repository at this point in the history
  • Loading branch information
mdulin2 committed Nov 22, 2024
1 parent cf31ccb commit 95414bb
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 19 deletions.
2 changes: 2 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,5 @@ issues:
text: "^func.*supervisor.*(waitSettle|waitSettleError).*$"
linters:
- unused
- path: pkg/transfer-verifier/transfer-verifier-sui_test.go
text: "G101: Potential hardcoded credentials"
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
17 changes: 7 additions & 10 deletions node/pkg/transfer-verifier/transfer-verifier-sui.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func (s *SuiTransferVerifier) GetEventFilter() string {
// that maps the token address and chain ID to the amount requested out of the bridge. It does not return an error, because any faulty
// events can be skipped, since they would likely fail being processed by the guardian as well. Debug level logging can be used to
// reveal any potential locations where errors are occurring.
func (s *SuiTransferVerifier) processEvents(events []SuiEvent, logger *zap.Logger) (requestedOutOfBridge map[string]*big.Int, numEventsProcessed int) {
func (s *SuiTransferVerifier) processEvents(events []SuiEvent, logger *zap.Logger) (requestedOutOfBridge map[string]*big.Int, numEventsProcessed uint) {
// Initialize the map to store the amount requested out of the bridge
requestedOutOfBridge = make(map[string]*big.Int)

Expand Down 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 uint) {
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 @@ -209,9 +205,9 @@ func (s *SuiTransferVerifier) ProcessDigest(digest string, suiApiConnection SuiA
zap.String("amountIn", amountIn.String()))
}

logger.Info("Digest processed", zap.String("txDigest", digest), zap.Int("numEventsProcessed", numEventsProcessed), zap.Int("numChangesProcessed", numChangesProcessed))
logger.Info("Digest processed", zap.String("txDigest", digest), zap.Uint("numEventsProcessed", numEventsProcessed), zap.Uint("numChangesProcessed", numChangesProcessed))

return uint(numEventsProcessed), nil
return numEventsProcessed, nil
}

type SuiApiResponse interface {
Expand All @@ -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
12 changes: 8 additions & 4 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 All @@ -131,7 +135,7 @@ func TestProcessEvents(t *testing.T) {
name string
events []SuiEvent
expectedResult map[string]*big.Int
expectedCount int
expectedCount uint
}{
{
name: "TestNoEvents",
Expand Down Expand Up @@ -300,7 +304,7 @@ func TestProcessObjectUpdates(t *testing.T) {
objectChanges []ObjectChange
resultList []ResultTestCase
expectedResult map[string]*big.Int
expectedCount int
expectedCount uint
}{
{
name: "TestProcessObjectNativeBase",
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 95414bb

Please sign in to comment.