Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use interfaces #252

Merged
merged 3 commits into from
Jan 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion accounts/jobs.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (

const AccountCreateJobType = "account_create"

func (s *Service) executeAccountCreateJob(ctx context.Context, j *jobs.Job) error {
func (s *ServiceImpl) executeAccountCreateJob(ctx context.Context, j *jobs.Job) error {
if j.Type != AccountCreateJobType {
return jobs.ErrInvalidJobType
}
Expand Down
4 changes: 2 additions & 2 deletions accounts/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ package accounts

import "go.uber.org/ratelimit"

type ServiceOption func(*Service)
type ServiceOption func(*ServiceImpl)

func WithTxRatelimiter(limiter ratelimit.Limiter) ServiceOption {
return func(svc *Service) {
return func(svc *ServiceImpl) {
svc.txRateLimiter = limiter
}
}
38 changes: 23 additions & 15 deletions accounts/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,29 @@ import (
"github.com/flow-hydraulics/flow-wallet-api/jobs"
"github.com/flow-hydraulics/flow-wallet-api/keys"
"github.com/onflow/flow-go-sdk"
"github.com/onflow/flow-go-sdk/client"
flow_templates "github.com/onflow/flow-go-sdk/templates"
log "github.com/sirupsen/logrus"
"go.uber.org/ratelimit"
)

const maxGasLimit = 9999

// Service defines the API for account management.
type Service struct {
type Service interface {
List(limit, offset int) (result []Account, err error)
Create(ctx context.Context, sync bool) (*jobs.Job, *Account, error)
AddNonCustodialAccount(address string) (*Account, error)
DeleteNonCustodialAccount(address string) error
Details(address string) (Account, error)
InitAdminAccount(ctx context.Context) error
}

// ServiceImpl defines the API for account management.
type ServiceImpl struct {
cfg *configs.Config
store Store
km keys.Manager
fc *client.Client
wp *jobs.WorkerPool
fc flow_helpers.FlowClient
wp jobs.WorkerPool
txRateLimiter ratelimit.Limiter
}

Expand All @@ -35,14 +43,14 @@ func NewService(
cfg *configs.Config,
store Store,
km keys.Manager,
fc *client.Client,
wp *jobs.WorkerPool,
fc flow_helpers.FlowClient,
wp jobs.WorkerPool,
opts ...ServiceOption,
) *Service {
) Service {
var defaultTxRatelimiter = ratelimit.NewUnlimited()

// TODO(latenssi): safeguard against nil config?
svc := &Service{cfg, store, km, fc, wp, defaultTxRatelimiter}
svc := &ServiceImpl{cfg, store, km, fc, wp, defaultTxRatelimiter}

for _, opt := range opts {
opt(svc)
Expand All @@ -59,7 +67,7 @@ func NewService(
}

// List returns all accounts in the datastore.
func (s *Service) List(limit, offset int) (result []Account, err error) {
func (s *ServiceImpl) List(limit, offset int) (result []Account, err error) {
o := datastore.ParseListOptions(limit, offset)
return s.store.Accounts(o)
}
Expand All @@ -68,7 +76,7 @@ func (s *Service) List(limit, offset int) (result []Account, err error) {
// It receives a new account with a corresponding private key or resource ID
// and stores both in datastore.
// It returns a job, the new account and a possible error.
func (s *Service) Create(ctx context.Context, sync bool) (*jobs.Job, *Account, error) {
func (s *ServiceImpl) Create(ctx context.Context, sync bool) (*jobs.Job, *Account, error) {
log.WithFields(log.Fields{"sync": sync}).Trace("Create account")

if !sync {
Expand All @@ -93,7 +101,7 @@ func (s *Service) Create(ctx context.Context, sync bool) (*jobs.Job, *Account, e
return nil, account, nil
}

func (s *Service) AddNonCustodialAccount(_ context.Context, address string) (*Account, error) {
func (s *ServiceImpl) AddNonCustodialAccount(address string) (*Account, error) {
log.WithFields(log.Fields{"address": address}).Trace("Add non-custodial account")

a := &Account{
Expand All @@ -109,7 +117,7 @@ func (s *Service) AddNonCustodialAccount(_ context.Context, address string) (*Ac
return a, nil
}

func (s *Service) DeleteNonCustodialAccount(_ context.Context, address string) error {
func (s *ServiceImpl) DeleteNonCustodialAccount(address string) error {
log.WithFields(log.Fields{"address": address}).Trace("Delete non-custodial account")

a, err := s.store.Account(flow_helpers.HexString(address))
Expand All @@ -130,7 +138,7 @@ func (s *Service) DeleteNonCustodialAccount(_ context.Context, address string) e
}

// Details returns a specific account, does not include private keys
func (s *Service) Details(address string) (Account, error) {
func (s *ServiceImpl) Details(address string) (Account, error) {
log.WithFields(log.Fields{"address": address}).Trace("Account details")

// Check if the input is a valid address
Expand All @@ -157,7 +165,7 @@ func (s *Service) Details(address string) (Account, error) {
// generated key. Admin account is used to pay for the transaction.
//
// Returns created account and the flow transaction ID of the account creation.
func (s *Service) createAccount(ctx context.Context) (*Account, string, error) {
func (s *ServiceImpl) createAccount(ctx context.Context) (*Account, string, error) {
account := &Account{Type: AccountTypeCustodial}

// Important to ratelimit all the way up here so the keys and reference blocks
Expand Down
4 changes: 2 additions & 2 deletions accounts/service_init.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
log "github.com/sirupsen/logrus"
)

func (s *Service) InitAdminAccount(ctx context.Context) error {
func (s *ServiceImpl) InitAdminAccount(ctx context.Context) error {
log.Debug("Initializing admin account")

a, err := s.store.Account(s.cfg.AdminAddress)
Expand Down Expand Up @@ -64,7 +64,7 @@ func (s *Service) InitAdminAccount(ctx context.Context) error {
return nil
}

func (s *Service) addAdminProposalKeys(ctx context.Context, count uint16) error {
func (s *ServiceImpl) addAdminProposalKeys(ctx context.Context, count uint16) error {

log.
WithFields(log.Fields{"count": count}).
Expand Down
2 changes: 1 addition & 1 deletion accounts/store_gorm.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ type GormStore struct {
db *gorm.DB
}

func NewGormStore(db *gorm.DB) *GormStore {
func NewGormStore(db *gorm.DB) Store {
return &GormStore{db}
}

Expand Down
3 changes: 1 addition & 2 deletions accounts/test_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,14 @@ import (
"github.com/flow-hydraulics/flow-wallet-api/templates/template_strings"
"github.com/onflow/cadence"
"github.com/onflow/flow-go-sdk"
"github.com/onflow/flow-go-sdk/client"

flow_templates "github.com/onflow/flow-go-sdk/templates"
)

// AddContract is used only in tests
func AddContract(
ctx context.Context,
fc *client.Client,
fc flow_helpers.FlowClient,
km keys.Manager,
accountAddress string,
contract flow_templates.Contract,
Expand Down
8 changes: 5 additions & 3 deletions chain_events/event.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package chain_events

import (
"context"

"github.com/onflow/flow-go-sdk"
log "github.com/sirupsen/logrus"
)

type handler interface {
Handle(flow.Event)
Handle(context.Context, flow.Event)
}

type event struct {
Expand All @@ -22,7 +24,7 @@ func (e *event) Register(handler handler) {
}

// Trigger sends out an event with the payload
func (e *event) Trigger(payload flow.Event) {
func (e *event) Trigger(ctx context.Context, payload flow.Event) {
log.
WithFields(log.Fields{"payload": payload}).
Trace("Handling Flow event")
Expand All @@ -32,6 +34,6 @@ func (e *event) Trigger(payload flow.Event) {
}

for _, handler := range e.handlers {
go handler.Handle(payload)
go handler.Handle(ctx, payload)
}
}
30 changes: 18 additions & 12 deletions chain_events/listener.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"time"

wallet_errors "github.com/flow-hydraulics/flow-wallet-api/errors"
"github.com/flow-hydraulics/flow-wallet-api/flow_helpers"
"github.com/flow-hydraulics/flow-wallet-api/system"
"github.com/onflow/flow-go-sdk"
"github.com/onflow/flow-go-sdk/client"
Expand All @@ -16,17 +17,22 @@ import (

type GetEventTypes func() ([]string, error)

type Listener struct {
type Listener interface {
Start() Listener
Stop()
}

type ListenerImpl struct {
ticker *time.Ticker
stopChan chan struct{}
fc *client.Client
fc flow_helpers.FlowClient
db Store
getTypes GetEventTypes
maxBlocks uint64
interval time.Duration
startingHeight uint64

systemService *system.Service
systemService system.Service
}

type ListenerStatus struct {
Expand All @@ -39,16 +45,16 @@ func (ListenerStatus) TableName() string {
}

func NewListener(
fc *client.Client,
fc flow_helpers.FlowClient,
db Store,
getTypes GetEventTypes,
maxDiff uint64,
interval time.Duration,
startingHeight uint64,
opts ...ListenerOption,
) *Listener {
) Listener {

listener := &Listener{
listener := &ListenerImpl{
ticker: nil,
stopChan: make(chan struct{}),
fc: fc,
Expand All @@ -68,7 +74,7 @@ func NewListener(
return listener
}

func (l *Listener) run(ctx context.Context, start, end uint64) error {
func (l *ListenerImpl) run(ctx context.Context, start, end uint64) error {
events := make([]flow.Event, 0)

eventTypes, err := l.getTypes()
Expand All @@ -91,13 +97,13 @@ func (l *Listener) run(ctx context.Context, start, end uint64) error {
}

for _, event := range events {
Event.Trigger(event)
Event.Trigger(ctx, event)
}

return nil
}

func (l *Listener) Start() *Listener {
func (l *ListenerImpl) Start() Listener {
if l.ticker != nil {
// Already started
return l
Expand Down Expand Up @@ -194,7 +200,7 @@ func (l *Listener) Start() *Listener {
return l
}

func (l *Listener) initHeight() error {
func (l *ListenerImpl) initHeight() error {
return l.db.LockedStatus(func(status *ListenerStatus) error {
if l.startingHeight > 0 && status.LatestHeight < l.startingHeight-1 {
status.LatestHeight = l.startingHeight - 1
Expand All @@ -215,7 +221,7 @@ func (l *Listener) initHeight() error {
})
}

func (l *Listener) Stop() {
func (l *ListenerImpl) Stop() {
log.Debug("Stopping Flow event listener")

close(l.stopChan)
Expand All @@ -227,7 +233,7 @@ func (l *Listener) Stop() {
l.ticker = nil
}

func (l *Listener) systemHalted() (bool, error) {
func (l *ListenerImpl) systemHalted() (bool, error) {
if l.systemService != nil {
return l.systemService.IsHalted()
}
Expand Down
6 changes: 3 additions & 3 deletions chain_events/option.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ import (
"github.com/flow-hydraulics/flow-wallet-api/system"
)

type ListenerOption func(*Listener)
type ListenerOption func(*ListenerImpl)

func WithSystemService(svc *system.Service) ListenerOption {
return func(listener *Listener) {
func WithSystemService(svc system.Service) ListenerOption {
return func(listener *ListenerImpl) {
listener.systemService = svc
}
}
2 changes: 1 addition & 1 deletion chain_events/store_gorm.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ type GormStore struct {
db *gorm.DB
}

func NewGormStore(db *gorm.DB) *GormStore {
func NewGormStore(db *gorm.DB) Store {
return &GormStore{db}
}

Expand Down
Loading