Skip to content

Commit

Permalink
fix: create noauth client for blockscout on etherum mainnet (#568)
Browse files Browse the repository at this point in the history
* create etherscan tests

* test: get transactions

* test: get transaction by hash

* feat: blockscout client

* create blockscout auth client

* fix linting

* fix linting

* turn off whynotlint

* fix linting

* add test
  • Loading branch information
robdefeo committed Feb 16, 2020
1 parent 160e294 commit 60226d5
Show file tree
Hide file tree
Showing 43 changed files with 1,234 additions and 163 deletions.
1 change: 1 addition & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ linters-settings:
- experimental
disabled-checks:
- wrapperFunc
- whyNoLint
gofmt:
# simplify code: gofmt with `-s` option, true by default
simplify: true
Expand Down
59 changes: 59 additions & 0 deletions cmd/mailchain/internal/settings/blockscout_receiver.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package settings //nolint: dupl

import (
"github.com/mailchain/mailchain/cmd/internal/settings/output"
"github.com/mailchain/mailchain/cmd/internal/settings/values"
"github.com/mailchain/mailchain/cmd/mailchain/internal/settings/defaults"
"github.com/mailchain/mailchain/internal/clients/blockscout"
"github.com/mailchain/mailchain/internal/mailbox"
"github.com/mailchain/mailchain/internal/protocols/ethereum"
)

// BlockscoutReceiver configuration element.
type BlockscoutReceiver struct {
kind string
EnabledProtocolNetworks values.StringSlice
APIKey values.String
}

func blockscoutReceiverNoAuth(s values.Store) *BlockscoutReceiver {
return blockscoutReceiverAny(s, defaults.ClientBlockscoutNoAuth)
}

func blockscoutReceiverAny(s values.Store, kind string) *BlockscoutReceiver {
return &BlockscoutReceiver{
kind: kind,
EnabledProtocolNetworks: values.NewDefaultStringSlice(
[]string{"ethereum/" + ethereum.Mainnet},
s,
"receivers."+kind+".enabled-networks",
),
APIKey: values.NewDefaultString("", s, "receivers."+kind+".api-key"),
}
}

// Supports a map of what protocol and network combinations are supported.
func (r BlockscoutReceiver) Supports() map[string]bool {
m := map[string]bool{}
for _, np := range r.EnabledProtocolNetworks.Get() {
m[np] = true
}

return m
}

// Produce `mailbox.Receiver` based on configuration settings.
func (r BlockscoutReceiver) Produce() (mailbox.Receiver, error) {
return blockscout.NewAPIClient(r.APIKey.Get())
}

// Output configuration as an `output.Element` for use in exporting configuration.
func (r BlockscoutReceiver) Output() output.Element {
return output.Element{
FullName: r.kind,
Attributes: []output.Attribute{
r.EnabledProtocolNetworks.Attribute(),
r.APIKey.Attribute(),
},
}
}
174 changes: 174 additions & 0 deletions cmd/mailchain/internal/settings/blockscout_receiver_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
package settings

import (
"reflect"
"testing"

"github.com/golang/mock/gomock"
"github.com/mailchain/mailchain/cmd/internal/settings/values"
"github.com/mailchain/mailchain/cmd/internal/settings/values/valuestest"
"github.com/mailchain/mailchain/internal/clients/blockscout"
"github.com/mailchain/mailchain/internal/mailbox"
"github.com/stretchr/testify/assert"
)

func Test_blockscoutReceiverAny(t *testing.T) {
mockCtrl := gomock.NewController(t)
defer mockCtrl.Finish()
type args struct {
s values.Store
kind string
}
tests := []struct {
name string
args args
wantEnabledProtocolNetworks []string
wantAPIKey string
}{
{
"success",
args{
func() values.Store {
m := valuestest.NewMockStore(mockCtrl)
m.EXPECT().IsSet("receivers.type.enabled-networks").Return(false)
m.EXPECT().IsSet("receivers.type.api-key").Return(false)
return m
}(),
"type",
},
[]string{"ethereum/mainnet"},
"",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := blockscoutReceiverAny(tt.args.s, tt.args.kind)
assert.Equal(t, tt.wantEnabledProtocolNetworks, got.EnabledProtocolNetworks.Get())
assert.Equal(t, tt.wantAPIKey, got.APIKey.Get())
})
}
}

func Test_blockscoutReceiverNoAuth(t *testing.T) {
mockCtrl := gomock.NewController(t)
defer mockCtrl.Finish()
type args struct {
s values.Store
}
tests := []struct {
name string
args args
wantEnabledProtocolNetworks []string
wantAPIKey string
}{
{
"success",
args{
func() values.Store {
m := valuestest.NewMockStore(mockCtrl)
m.EXPECT().IsSet("receivers.blockscout-no-auth.enabled-networks").Return(false)
m.EXPECT().IsSet("receivers.blockscout-no-auth.api-key").Return(false)
return m
}(),
},
[]string{"ethereum/mainnet"},
"",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := blockscoutReceiverNoAuth(tt.args.s)
assert.Equal(t, tt.wantEnabledProtocolNetworks, got.EnabledProtocolNetworks.Get())
assert.Equal(t, tt.wantAPIKey, got.APIKey.Get())
})
}
}

func TestBlockscoutReceiver_Supports(t *testing.T) {
mockCtrl := gomock.NewController(t)
defer mockCtrl.Finish()
type fields struct {
EnabledProtocolNetworks values.StringSlice
APIKey values.String
}
tests := []struct {
name string
fields fields
want map[string]bool
}{
{
"success",
fields{
func() values.StringSlice {
m := valuestest.NewMockStringSlice(mockCtrl)
m.EXPECT().Get().Return([]string{"ethereum/mainnet", "ethereum/ropsten"})
return m
}(),
nil,
},
map[string]bool{"ethereum/mainnet": true, "ethereum/ropsten": true},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
r := BlockscoutReceiver{
EnabledProtocolNetworks: tt.fields.EnabledProtocolNetworks,
APIKey: tt.fields.APIKey,
}
if got := r.Supports(); !reflect.DeepEqual(got, tt.want) {
t.Errorf("blockscoutReceiver.Supports() = %v, want %v", got, tt.want)
}
})
}
}

func TestBlockscoutReceiver_Produce(t *testing.T) {
mockCtrl := gomock.NewController(t)
defer mockCtrl.Finish()
type fields struct {
kind string
EnabledProtocolNetworks values.StringSlice
APIKey values.String
}
tests := []struct {
name string
fields fields
want mailbox.Receiver
wantErr bool
}{
{
"success",
fields{
"test",
func() values.StringSlice {
m := valuestest.NewMockStringSlice(mockCtrl)
return m
}(),
func() values.String {
m := valuestest.NewMockString(mockCtrl)
m.EXPECT().Get().Return("apikey")
return m
}(),
},
&blockscout.APIClient{},
false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
r := BlockscoutReceiver{
kind: tt.fields.kind,
EnabledProtocolNetworks: tt.fields.EnabledProtocolNetworks,
APIKey: tt.fields.APIKey,
}
got, err := r.Produce()
if (err != nil) != tt.wantErr {
t.Errorf("BlockscoutReceiver.Produce() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !assert.IsType(t, tt.want, got) {
t.Errorf("BlockscoutReceiver.Produce() = %v, want %v", got, tt.want)
}
})
}
}
5 changes: 5 additions & 0 deletions cmd/mailchain/internal/settings/defaults/defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ const (
)

const (
// ClientBlockscoutNoAuth blockscout without authentication client name.
ClientBlockscoutNoAuth = "blockscout-no-auth"
// ClientEtherscan etherscan client name.
ClientEtherscan = "etherscan"
// ClientEtherscanNoAuth etherscan without authentication client name.
Expand All @@ -61,6 +63,9 @@ const (
ClientEthereumRPC2 = "ethereum-rpc2"
// ClientRelay relay client name.
ClientRelay = "relay"

// EthereumRelay relay for ethereum only
EthereumRelay = "ethereum-relay"
)

// KeystorePath default value.
Expand Down
2 changes: 1 addition & 1 deletion cmd/mailchain/internal/settings/defaults/networks.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func EthereumNetworkAny() *NetworkDefaults {
NameServiceDomainName: NameServiceDomainNameKind,
PublicKeyFinder: ClientEtherscanNoAuth,
Receiver: ClientEtherscanNoAuth,
Sender: "ethereum-relay",
Sender: EthereumRelay,
Disabled: false,
}
}
Expand Down
5 changes: 3 additions & 2 deletions cmd/mailchain/internal/settings/receivers.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@ import (
func receivers(s values.Store) *Receivers {
return &Receivers{
clients: map[string]ReceiverClient{
defaults.ClientEtherscanNoAuth: etherscanReceiverNoAuth(s),
defaults.ClientEtherscan: etherscanReceiver(s),
defaults.ClientEtherscanNoAuth: etherscanReceiverNoAuth(s),
defaults.ClientEtherscan: etherscanReceiver(s),
defaults.ClientBlockscoutNoAuth: blockscoutReceiverNoAuth(s),
},
}
}
Expand Down
14 changes: 11 additions & 3 deletions cmd/mailchain/internal/settings/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,17 @@ func FromStore(s values.Store) *Root {
// Protocols these contain the networks
Protocols: map[string]*Protocol{
protocols.Ethereum: protocol(s, protocols.Ethereum, map[string]NetworkClient{
ethereum.Goerli: network(s, protocols.Ethereum, ethereum.Goerli, defaults.EthereumNetworkAny()),
ethereum.Kovan: network(s, protocols.Ethereum, ethereum.Kovan, defaults.EthereumNetworkAny()),
ethereum.Mainnet: network(s, protocols.Ethereum, ethereum.Mainnet, defaults.EthereumNetworkAny()),
ethereum.Goerli: network(s, protocols.Ethereum, ethereum.Goerli, defaults.EthereumNetworkAny()),
ethereum.Kovan: network(s, protocols.Ethereum, ethereum.Kovan, defaults.EthereumNetworkAny()),
ethereum.Mainnet: network(s, protocols.Ethereum, ethereum.Mainnet,
&defaults.NetworkDefaults{
NameServiceAddress: defaults.NameServiceAddressKind,
NameServiceDomainName: defaults.NameServiceDomainNameKind,
PublicKeyFinder: defaults.ClientEtherscanNoAuth,
Receiver: defaults.ClientBlockscoutNoAuth,
Sender: defaults.EthereumRelay,
Disabled: false,
}),
ethereum.Rinkeby: network(s, protocols.Ethereum, ethereum.Rinkeby, defaults.EthereumNetworkAny()),
ethereum.Ropsten: network(s, protocols.Ethereum, ethereum.Ropsten, defaults.EthereumNetworkAny()),
}),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
# nameservice-address: "mailchain"
# nameservice-domain-name: "mailchain"
# public-key-finder: "etherscan-no-auth"
# receiver: "etherscan-no-auth"
# receiver: "blockscout-no-auth"
# sender: "ethereum-relay"
# rinkeby:
# disabled: false
Expand Down Expand Up @@ -88,6 +88,10 @@
# - "substrate/edgeware-mainnet"
# - "substrate/edgeware-berlin"
# receivers:
# blockscout-no-auth:
# api-key: ""
# enabled-networks:
# - "ethereum/mainnet"
# etherscan:
# api-key: ""
# enabled-networks:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ protocols:
nameservice-address: "mailchain"
nameservice-domain-name: "mailchain"
public-key-finder: "etherscan-no-auth"
receiver: "etherscan-no-auth"
receiver: "blockscout-no-auth"
sender: "ethereum-relay"
rinkeby:
disabled: false
Expand Down Expand Up @@ -88,6 +88,10 @@ public-key-finders:
- "substrate/edgeware-mainnet"
- "substrate/edgeware-berlin"
receivers:
blockscout-no-auth:
api-key: ""
enabled-networks:
- "ethereum/mainnet"
etherscan:
api-key: ""
enabled-networks:
Expand Down
Loading

0 comments on commit 60226d5

Please sign in to comment.