-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 3aafe3a
Showing
6 changed files
with
581 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
name: Run tests | ||
|
||
on: | ||
push: | ||
branches: | ||
- main | ||
- dev | ||
paths-ignore: | ||
- '**.md' | ||
- '**.yml' | ||
- '**.yaml' | ||
- 'examples/*' | ||
- '!.github/workflows/test.yml' | ||
pull_request: | ||
branches: | ||
- main | ||
- dev | ||
paths-ignore: | ||
- '**.md' | ||
- '**.yml' | ||
- '**.yaml' | ||
- 'examples/*' | ||
- '!.github/workflows/test.yml' | ||
|
||
env: | ||
GO111MODULE: on | ||
GOPROXY: "https://proxy.golang.org" | ||
|
||
jobs: | ||
lint: | ||
strategy: | ||
matrix: | ||
os: | ||
- ubuntu-latest | ||
- macos-latest | ||
name: Run golangci-lint | ||
runs-on: ${{ matrix.os }} | ||
steps: | ||
- name: Setup Go | ||
uses: actions/setup-go@v3 | ||
with: | ||
go-version: '^1.17' | ||
|
||
- name: Checkout repository | ||
uses: actions/checkout@v3 | ||
|
||
- name: Setup and run golangci-lint | ||
uses: golangci/golangci-lint-action@v3 | ||
with: | ||
version: v1.51.2 | ||
args: --timeout 5m -v -E gofumpt -E gocritic -E misspell -E revive -E godot | ||
test: | ||
needs: lint | ||
strategy: | ||
fail-fast: false | ||
matrix: | ||
go: [1.17] | ||
os: [ubuntu-latest, macos-latest, windows-latest] | ||
name: Go ${{ matrix.go }} @ ${{ matrix.os }} | ||
runs-on: ${{ matrix.os}} | ||
steps: | ||
- name: Checkout repository | ||
uses: actions/checkout@v3 | ||
with: | ||
ref: ${{ github.ref }} | ||
|
||
- name: Setup Go | ||
uses: actions/setup-go@v3 | ||
with: | ||
go-version: ${{ matrix.go }} | ||
|
||
- name: Print Go environment | ||
id: go-env | ||
run: | | ||
printf "Using go at: $(which go)\n" | ||
printf "Go version: $(go version)\n" | ||
printf "\n\nGo environment:\n\n" | ||
go env | ||
printf "\n\nSystem environment:\n\n" | ||
env | ||
# Calculate the short SHA1 hash of the git commit | ||
echo "SHORT_SHA=$(git rev-parse --short HEAD)" >> $GITHUB_OUTPUT | ||
echo "GO_CACHE=$(go env GOCACHE)" >> $GITHUB_OUTPUT | ||
- name: Cache go modules | ||
uses: actions/cache@v3 | ||
with: | ||
path: | | ||
${{ steps.go-env.outputs.GO_CACHE }} | ||
~/go/pkg/mod | ||
key: ${{ runner.os }}-${{ matrix.go }}-go-ci-${{ hashFiles('**/go.sum') }} | ||
restore-keys: | | ||
${{ runner.os }}-${{ matrix.go }}-go-ci | ||
- name: Run unit tests | ||
run: go test -v -race -coverprofile="codecov.report" -covermode=atomic | ||
|
||
- name: Upload code coverage report to Codecov | ||
uses: codecov/codecov-action@v3 | ||
with: | ||
file: ./codecov.report | ||
flags: unittests | ||
name: codecov-ants | ||
fail_ci_if_error: true | ||
verbose: true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
# Binaries for programs and plugins | ||
*.exe | ||
*.exe~ | ||
*.dll | ||
*.so | ||
*.dylib | ||
|
||
# Test binary, built with `go test -c` | ||
*.test | ||
|
||
# Output of the go coverage tool, specifically when used with LiteIDE | ||
*.out | ||
|
||
vendor/ | ||
|
||
.idea | ||
|
||
.DS_Store |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
package goarns | ||
|
||
import ( | ||
"fmt" | ||
"github.com/tidwall/gjson" | ||
"io/ioutil" | ||
"net/http" | ||
"time" | ||
) | ||
|
||
type ArNS struct { | ||
DreUrl string | ||
ArNSAddress string | ||
HttpClient *http.Client | ||
} | ||
|
||
func NewArNS(dreUrl string, arNSAddr string, timout time.Duration) *ArNS { | ||
|
||
// default timeout is 5s | ||
if timout == 0 { | ||
timout = 5 * time.Second | ||
} | ||
|
||
httpClient := &http.Client{ | ||
Timeout: timout, // Set the timeout for HTTP requests | ||
} | ||
return &ArNS{ | ||
DreUrl: dreUrl, | ||
ArNSAddress: arNSAddr, | ||
HttpClient: httpClient, | ||
} | ||
} | ||
|
||
func (a *ArNS) QueryLatestRecord(domain string) (txId string, err error) { | ||
// step1 query NameCA address | ||
caAddress, err := a.QueryNameCa(domain) | ||
if err != nil { | ||
return "", err | ||
} | ||
// step2 query latest txId | ||
//Currently, only level-1 domain name resolution is queried | ||
txId, err = a.GetArNSTxID(caAddress, "@") | ||
|
||
return | ||
|
||
} | ||
|
||
func (a *ArNS) QueryNameCa(domain string) (caAddress string, err error) { | ||
baseURL := a.DreUrl + "/contract/" | ||
|
||
// Construct the complete URL | ||
url := baseURL + "?id=" + a.ArNSAddress | ||
|
||
// Make the HTTP request using the custom HTTP client | ||
response, err := a.HttpClient.Get(url) | ||
if err != nil { | ||
return "", err | ||
} | ||
defer response.Body.Close() | ||
|
||
// Check the response status code | ||
if response.StatusCode != http.StatusOK { | ||
return "", fmt.Errorf("unexpected response status: %s", response.Status) | ||
} | ||
|
||
// Read the response body | ||
body, err := ioutil.ReadAll(response.Body) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
value := gjson.Get(string(body), "state.records."+domain+".contractTxId") | ||
|
||
if !value.Exists() { | ||
return "", fmt.Errorf("domain %s not exist", domain) | ||
} | ||
|
||
return value.String(), nil | ||
|
||
} | ||
|
||
func (a *ArNS) GetArNSTxID(caAddress string, domain string) (txId string, err error) { | ||
|
||
baseURL := a.DreUrl + "/contract/" | ||
|
||
// Construct the complete URL | ||
url := baseURL + "?id=" + caAddress | ||
|
||
// Make the HTTP request using the custom HTTP client | ||
response, err := a.HttpClient.Get(url) | ||
if err != nil { | ||
return "", err | ||
} | ||
defer response.Body.Close() | ||
|
||
// Check the response status code | ||
if response.StatusCode != http.StatusOK { | ||
return "", fmt.Errorf("GetArNSTxID: unexpected response status: %s", response.Status) | ||
} | ||
|
||
// Read the response body | ||
body, err := ioutil.ReadAll(response.Body) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
value := gjson.Get(string(body), "state.records."+domain) | ||
|
||
if !value.Exists() { | ||
return "", fmt.Errorf("GetArNSTxID: domain %s not exist", domain) | ||
} | ||
|
||
return value.String(), nil | ||
|
||
} |
Oops, something went wrong.