Skip to content

Commit

Permalink
Merge pull request #21 from AccumulateNetwork/develop
Browse files Browse the repository at this point in the history
Additional checks in Accumulate client
  • Loading branch information
ilzheev authored Jul 18, 2024
2 parents fc4192d + af3754f commit f188c07
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 9 deletions.
21 changes: 20 additions & 1 deletion accumulate/accumulate.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,20 @@ type AccumulateClient struct {
Validate *validator.Validate
}

func notOlderThanOneMinute(fl validator.FieldLevel) bool {
// Get the value of the field
lastBlockTime, ok := fl.Field().Interface().(time.Time)
if !ok {
return false
}

// Calculate the difference between the current time and LastBlockTime
diff := time.Since(lastBlockTime)

// Check if the difference is less than or equal to 1 minute
return diff <= time.Minute
}

// NewAccumulateClient constructs the Accumulate client
func NewAccumulateClient(conf *config.Config) (*AccumulateClient, error) {

Expand All @@ -50,6 +64,11 @@ func NewAccumulateClient(conf *config.Config) (*AccumulateClient, error) {
// init validator
c.Validate = validator.New()

err := c.Validate.RegisterValidation("notOlderThanOneMinute", notOlderThanOneMinute)
if err != nil {
return nil, fmt.Errorf("Error registering validation function: %s", err)
}

if conf.ACME.Node == "" {
return nil, fmt.Errorf("received empty node from config: %s", conf.ACME.Node)
}
Expand All @@ -65,7 +84,7 @@ func NewAccumulateClient(conf *config.Config) (*AccumulateClient, error) {
c.Client = jsonrpc.NewClientWithOpts(conf.ACME.Node, opts)

// check if config ADI is valid
_, err := c.QueryADI(&Params{URL: conf.ACME.BridgeADI})
_, err = c.QueryADI(&Params{URL: conf.ACME.BridgeADI})
if err != nil {
return nil, err
}
Expand Down
25 changes: 17 additions & 8 deletions accumulate/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"encoding/json"
"fmt"
"time"

"github.com/labstack/gommon/log"
"gitlab.com/accumulatenetwork/accumulate/protocol"
Expand Down Expand Up @@ -90,31 +91,38 @@ type ExecuteDirectResponse struct {
}

type QueryADIResponse struct {
Data *ADI `json:"data"`
Data *ADI `json:"data"`
LastBlockTime *time.Time `json:"lastBlockTime" validate:"required,notOlderThanOneMinute"`
}

type QueryKeyPageResponse struct {
Data *KeyPage `json:"data"`
Data *KeyPage `json:"data"`
LastBlockTime *time.Time `json:"lastBlockTime" validate:"required,notOlderThanOneMinute"`
}

type QueryTokenResponse struct {
Data *Token `json:"data"`
Data *Token `json:"data"`
LastBlockTime *time.Time `json:"lastBlockTime" validate:"required,notOlderThanOneMinute"`
}

type QueryTokenAccountResponse struct {
Data *TokenAccount `json:"data"`
Data *TokenAccount `json:"data"`
LastBlockTime *time.Time `json:"lastBlockTime" validate:"required,notOlderThanOneMinute"`
}

type QueryDataResponse struct {
Data *DataEntry `json:"data"`
Data *DataEntry `json:"data"`
LastBlockTime *time.Time `json:"lastBlockTime" validate:"required,notOlderThanOneMinute"`
}

type QueryDataSetResponse struct {
Items []*DataEntry `json:"items"`
Items []*DataEntry `json:"items"`
LastBlockTime *time.Time `json:"lastBlockTime" validate:"required,notOlderThanOneMinute"`
}

type QueryPendingChainResponse struct {
Items []string `json:"items"`
Items []string `json:"items"`
LastBlockTime *time.Time `json:"lastBlockTime" validate:"required,notOlderThanOneMinute"`
}

type QueryTokenTxResponse struct {
Expand All @@ -130,7 +138,8 @@ type QueryTokenTxResponse struct {
}

type QueryTxHistoryResponse struct {
Items []*QueryTokenTxResponse `json:"items"`
Items []*QueryTokenTxResponse `json:"items"`
LastBlockTime *time.Time `json:"lastBlockTime" validate:"required,notOlderThanOneMinute"`
}

// QueryADI gets Token info
Expand Down

0 comments on commit f188c07

Please sign in to comment.