Skip to content
This repository has been archived by the owner on Sep 20, 2024. It is now read-only.

Commit

Permalink
[#30] Make contract address configurable
Browse files Browse the repository at this point in the history
Signed-off-by: Angira Kekteeva <kira@nspcc.ru>
  • Loading branch information
masterSplinter01 committed Apr 26, 2022
1 parent 5ee718f commit 128ab2a
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 36 deletions.
16 changes: 11 additions & 5 deletions plugin/nns/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,20 @@

## Description

The nns plugin try to get value from TXT records in provided neo node
The nns plugin tries to get value from TXT records in provided neo node
(lookup in [NNS smart contract](https://docs.neo.org/docs/en-us/reference/nns.html)).
You can specify NNS domain to map DNS domain from request (default no mapping).

## Syntax

``` txt
nns NEO_N3_CHAIN_ENDPOINT [NNS_DOMAIN]
nns NEO_N3_CHAIN_ENDPOINT CONTRACT_ADDRESS [NNS_DOMAIN]
```

`CONTRACT_ADDRESS` - hex-encoded contract script hash. The value is required, but if you are using the NNS contract
in side-chain, you can place `-` and the plugin will use contract with ID 1 as NNS.
``` txt
nns NEO_N3_CHAIN_ENDPOINT - [NNS_DOMAIN]
```

## Examples
Expand All @@ -23,7 +29,7 @@ requests to 8.8.8.8 if the neo request fails.

``` corefile
. {
nns http://localhost:30333
nns http://localhost:30333 acf433b55b75907fd80e8c90c9c42140992c8240
forward . 8.8.8.8
}
```
Expand All @@ -34,7 +40,7 @@ It also enables zone transfer support:

``` corefile
containers.testnet.fs.neo.org {
nns http://morph_chain.neofs.devenv:30333 containers
nns http://morph-chain.neofs.devenv:30333 - containers
transfer {
to *
}
Expand All @@ -45,7 +51,7 @@ If there is no domain filter in config:

``` corefile
. {
nns http://morph_chain.neofs.devenv:30333 containers
nns http://morph-chain.neofs.devenv:30333 - containers
}
```

Expand Down
20 changes: 10 additions & 10 deletions plugin/nns/nns.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,17 @@ import (
"github.com/coredns/coredns/request"
"github.com/miekg/dns"
nns "github.com/nspcc-dev/neo-go/examples/nft-nd-nns"
"github.com/nspcc-dev/neo-go/pkg/core/state"
"github.com/nspcc-dev/neo-go/pkg/rpc/client"
"github.com/nspcc-dev/neo-go/pkg/util"
)

type NNS struct {
Next plugin.Handler
Client *client.Client
CS *state.Contract
Log clog.P
nnsDomain string
dnsDomain string
Next plugin.Handler
Client *client.Client
ContractHash util.Uint160
Log clog.P
nnsDomain string
dnsDomain string
}

const dot = "."
Expand Down Expand Up @@ -51,7 +51,7 @@ func (n NNS) Name() string { return pluginName }
// Transfer implements the transfer.Transfer interface.
func (n NNS) Transfer(zone string, serial uint32) (<-chan []dns.RR, error) {
trimmedZone := n.prepareName(zone)
records, err := getRecords(n.Client, n.CS.Hash, trimmedZone, nns.RecordType(dns.TypeSOA))
records, err := getRecords(n.Client, n.ContractHash, trimmedZone, nns.RecordType(dns.TypeSOA))
if err != nil {
n.Log.Warningf("couldn't transfer zone '%s' as '%s': %s", zone, trimmedZone, err.Error())
return nil, transfer.ErrNotAuthoritative
Expand Down Expand Up @@ -104,7 +104,7 @@ func (n NNS) resolveRecords(state request.Request) ([]dns.RR, error) {
return nil, fmt.Errorf("cannot resolve '%s' (type %d) as '%s': %w", state.QName(), state.QType(), name, err)
}

resolved, err := resolve(n.Client, n.CS.Hash, name, nnsType)
resolved, err := resolve(n.Client, n.ContractHash, name, nnsType)
if err != nil {
return nil, fmt.Errorf("cannot resolve '%s' (type %d) as '%s': %w", state.QName(), state.QType(), name, err)
}
Expand All @@ -118,7 +118,7 @@ func (n NNS) resolveRecords(state request.Request) ([]dns.RR, error) {
}

func (n NNS) zoneTransfer(name string) ([]dns.RR, error) {
records, err := getAllRecords(n.Client, n.CS.Hash, name)
records, err := getAllRecords(n.Client, n.ContractHash, name)
if err != nil {
return nil, err
}
Expand Down
70 changes: 49 additions & 21 deletions plugin/nns/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,17 @@ import (
"github.com/coredns/coredns/plugin"
clog "github.com/coredns/coredns/plugin/pkg/log"
"github.com/nspcc-dev/neo-go/pkg/rpc/client"
"github.com/nspcc-dev/neo-go/pkg/util"
)

const pluginName = "nns"

type Params struct {
Endpoint string
ContractHash util.Uint160
Domain string
}

func init() {
plugin.Register(pluginName, setup)
}
Expand All @@ -24,33 +31,41 @@ func setup(c *caddy.Controller) error {
return plugin.Error(pluginName, c.Err(err.Error()))
}

endpoint, nnsDomain, err := parseArgs(c)
args, err := parseArgs(c)
if err != nil {
return err
}

cli, err := client.New(context.TODO(), endpoint, client.Options{})
cli, err := client.New(context.TODO(), args.Endpoint, client.Options{})
if err != nil {
return plugin.Error(pluginName, c.Err(err.Error()))
}
if err := cli.Init(); err != nil {
return plugin.Error(pluginName, c.Err(err.Error()))
}

cs, err := cli.GetContractStateByID(1)
if err != nil {
return plugin.Error(pluginName, c.Err(err.Error()))
if args.ContractHash.Equals(util.Uint160{}) {
cs, err := cli.GetContractStateByID(1)
if err != nil {
return plugin.Error(pluginName, c.Err(err.Error()))
}
args.ContractHash = cs.Hash
} else {
_, err := cli.GetContractStateByHash(args.ContractHash)
if err != nil {
return plugin.Error(pluginName, c.Err(err.Error()))
}
}

// Add the Plugin to CoreDNS, so Servers can use it in their plugin chain.
dnsserver.GetConfig(c).AddPlugin(func(next plugin.Handler) plugin.Handler {
nns := &NNS{
Next: next,
Client: cli,
CS: cs,
Log: clog.NewWithPlugin(pluginName),
Next: next,
Client: cli,
ContractHash: args.ContractHash,
Log: clog.NewWithPlugin(pluginName),
}
nns.setNNSDomain(nnsDomain)
nns.setNNSDomain(args.Domain)
nns.setDNSDomain(URL.Hostname())

return *nns
Expand All @@ -59,23 +74,36 @@ func setup(c *caddy.Controller) error {
return nil
}

func parseArgs(c *caddy.Controller) (string, string, error) {
func parseArgs(c *caddy.Controller) (*Params, error) {
c.Next()
args := c.RemainingArgs()
if len(args) < 1 || len(args) > 2 {
return "", "", plugin.Error(pluginName, fmt.Errorf("support the following args template: 'NEO_CHAIN_ENDPOINT [NNS_DOMAIN]'"))
var (
err error
res Params
)

if len(args) < 2 || len(args) > 3 {
return nil, plugin.Error(pluginName, fmt.Errorf("support the following args template: 'NEO_CHAIN_ENDPOINT CONTRACT_ADDRESS [NNS_DOMAIN]'"))
}
endpoint := args[0]
if URL, err := url.Parse(endpoint); err != nil {
return "", "", plugin.Error(pluginName, fmt.Errorf("couldn't parse endpoint: %w", err))

res.Endpoint = args[0]
if URL, err := url.Parse(res.Endpoint); err != nil {
return nil, plugin.Error(pluginName, fmt.Errorf("couldn't parse endpoint: %w", err))
} else if URL.Scheme == "" || URL.Port() == "" {
return "", "", plugin.Error(pluginName, fmt.Errorf("invalid endpoint: %s", endpoint))
return nil, plugin.Error(pluginName, fmt.Errorf("invalid endpoint: %s", res.Endpoint))
}

hexStr := args[1]
if hexStr != "-" {
res.ContractHash, err = util.Uint160DecodeStringLE(hexStr)
if err != nil {
return nil, plugin.Error(pluginName, fmt.Errorf("invalid nns contract address"))
}
}

nnsDomain := ""
if len(args) == 2 {
nnsDomain = args[1]
if len(args) == 3 {
res.Domain = args[2]
}

return endpoint, nnsDomain, nil
return &res, nil
}

0 comments on commit 128ab2a

Please sign in to comment.