forked from btcsuite/btcwallet
-
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
Showing
11 changed files
with
1,590 additions
and
504 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,99 @@ | ||
// Copyright (c) 2015 Conformal Systems LLC <[email protected]> | ||
// | ||
// Permission to use, copy, modify, and distribute this software for any | ||
// purpose with or without fee is hereby granted, provided that the above | ||
// copyright notice and this permission notice appear in all copies. | ||
// | ||
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES | ||
// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF | ||
// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR | ||
// ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES | ||
// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN | ||
// ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF | ||
// OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | ||
|
||
//+build generate | ||
|
||
package main | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
"os" | ||
"strings" | ||
|
||
"github.com/btcsuite/btcd/btcjson/v2/btcjson" | ||
"github.com/btcsuite/btcwallet/internal/rpchelp" | ||
) | ||
|
||
var outputFile = func() *os.File { | ||
fi, err := os.Create("rpcserverhelp.go") | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
return fi | ||
}() | ||
|
||
func writefln(format string, args ...interface{}) { | ||
_, err := fmt.Fprintf(outputFile, format, args...) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
_, err = outputFile.Write([]byte{'\n'}) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
} | ||
|
||
func writeLocaleHelp(locale, goLocale string, descs map[string]string) { | ||
funcName := "helpDescs" + goLocale | ||
writefln("func %s() map[string]string {", funcName) | ||
writefln("return map[string]string{") | ||
for i := range rpchelp.Methods { | ||
m := &rpchelp.Methods[i] | ||
helpText, err := btcjson.GenerateHelp(m.Method, descs, m.ResultTypes...) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
writefln("%q: %q,", m.Method, helpText) | ||
} | ||
writefln("}") | ||
writefln("}") | ||
} | ||
|
||
func writeLocales() { | ||
writefln("var localeHelpDescs = map[string]func() map[string]string{") | ||
for _, h := range rpchelp.HelpDescs { | ||
writefln("%q: helpDescs%s,", h.Locale, h.GoLocale) | ||
} | ||
writefln("}") | ||
} | ||
|
||
func writeUsage() { | ||
usageStrs := make([]string, len(rpchelp.Methods)) | ||
var err error | ||
for i := range rpchelp.Methods { | ||
usageStrs[i], err = btcjson.MethodUsageText(rpchelp.Methods[i].Method) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
} | ||
usages := strings.Join(usageStrs, "\n") | ||
writefln("var requestUsages = %q", usages) | ||
} | ||
|
||
func main() { | ||
defer outputFile.Close() | ||
|
||
writefln("// AUTOGENERATED by internal/rpchelp/genrpcserverhelp.go; do not edit.") | ||
writefln("") | ||
writefln("package main") | ||
writefln("") | ||
for _, h := range rpchelp.HelpDescs { | ||
writeLocaleHelp(h.Locale, h.GoLocale, h.Descs) | ||
writefln("") | ||
} | ||
writeLocales() | ||
writefln("") | ||
writeUsage() | ||
} |
Large diffs are not rendered by default.
Oops, something went wrong.
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,89 @@ | ||
// Copyright (c) 2015 Conformal Systems LLC <[email protected]> | ||
// | ||
// Permission to use, copy, modify, and distribute this software for any | ||
// purpose with or without fee is hereby granted, provided that the above | ||
// copyright notice and this permission notice appear in all copies. | ||
// | ||
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES | ||
// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF | ||
// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR | ||
// ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES | ||
// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN | ||
// ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF | ||
// OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | ||
|
||
//+build !generate | ||
|
||
package rpchelp | ||
|
||
import "github.com/btcsuite/btcd/btcjson/v2/btcjson" | ||
|
||
// Common return types. | ||
var ( | ||
returnsBool = []interface{}{(*bool)(nil)} | ||
returnsNumber = []interface{}{(*float64)(nil)} | ||
returnsString = []interface{}{(*string)(nil)} | ||
returnsStringArray = []interface{}{(*[]string)(nil)} | ||
returnsLTRArray = []interface{}{(*[]btcjson.ListTransactionsResult)(nil)} | ||
) | ||
|
||
// Contains all methods and result types that help is generated for, for every | ||
// locale. | ||
var Methods = []struct { | ||
Method string | ||
ResultTypes []interface{} | ||
}{ | ||
{"addmultisigaddress", returnsString}, | ||
{"createmultisig", []interface{}{(*btcjson.CreateMultiSigResult)(nil)}}, | ||
{"dumpprivkey", returnsString}, | ||
{"getaccount", returnsString}, | ||
{"getaccountaddress", returnsString}, | ||
{"getaddressesbyaccount", returnsStringArray}, | ||
{"getbalance", append(returnsNumber, returnsNumber[0])}, | ||
{"getbestblockhash", returnsString}, | ||
{"getblockcount", returnsNumber}, | ||
{"getinfo", []interface{}{(*btcjson.InfoWalletResult)(nil)}}, | ||
{"getnewaddress", returnsString}, | ||
{"getrawchangeaddress", returnsString}, | ||
{"getreceivedbyaccount", returnsNumber}, | ||
{"getreceivedbyaddress", returnsNumber}, | ||
{"gettransaction", []interface{}{(*btcjson.GetTransactionResult)(nil)}}, | ||
{"help", append(returnsString, returnsString[0])}, | ||
{"importprivkey", nil}, | ||
{"keypoolrefill", nil}, | ||
{"listaccounts", []interface{}{(*map[string]float64)(nil)}}, | ||
{"listlockunspent", []interface{}{(*[]btcjson.TransactionInput)(nil)}}, | ||
{"listreceivedbyaccount", []interface{}{(*[]btcjson.ListReceivedByAccountResult)(nil)}}, | ||
{"listreceivedbyaddress", []interface{}{(*[]btcjson.ListReceivedByAddressResult)(nil)}}, | ||
{"listsinceblock", []interface{}{(*btcjson.ListSinceBlockResult)(nil)}}, | ||
{"listtransactions", returnsLTRArray}, | ||
{"listunspent", []interface{}{(*btcjson.ListUnspentResult)(nil)}}, | ||
{"lockunspent", returnsBool}, | ||
{"sendfrom", returnsString}, | ||
{"sendmany", returnsString}, | ||
{"sendtoaddress", returnsString}, | ||
{"settxfee", returnsBool}, | ||
{"signmessage", returnsString}, | ||
{"signrawtransaction", []interface{}{(*btcjson.SignRawTransactionResult)(nil)}}, | ||
{"validateaddress", []interface{}{(*btcjson.ValidateAddressWalletResult)(nil)}}, | ||
{"verifymessage", returnsBool}, | ||
{"walletlock", nil}, | ||
{"walletpassphrase", nil}, | ||
{"walletpassphrasechange", nil}, | ||
{"createnewaccount", nil}, | ||
{"exportwatchingwallet", returnsString}, | ||
{"getbestblock", []interface{}{(*btcjson.GetBestBlockResult)(nil)}}, | ||
{"getunconfirmedbalance", returnsNumber}, | ||
{"listaddresstransactions", returnsLTRArray}, | ||
{"listalltransactions", returnsLTRArray}, | ||
{"renameaccount", nil}, | ||
{"walletislocked", returnsBool}, | ||
} | ||
|
||
var HelpDescs = []struct { | ||
Locale string // Actual locale, e.g. en_US | ||
GoLocale string // Locale used in Go names, e.g. EnUS | ||
Descs map[string]string | ||
}{ | ||
{"en_US", "EnUS", helpDescsEnUS}, // helpdescs_en_US.go | ||
} |
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,109 @@ | ||
// Copyright (c) 2015 Conformal Systems LLC <[email protected]> | ||
// | ||
// Permission to use, copy, modify, and distribute this software for any | ||
// purpose with or without fee is hereby granted, provided that the above | ||
// copyright notice and this permission notice appear in all copies. | ||
// | ||
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES | ||
// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF | ||
// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR | ||
// ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES | ||
// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN | ||
// ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF | ||
// OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | ||
|
||
package main | ||
|
||
import ( | ||
"strings" | ||
"testing" | ||
|
||
"github.com/btcsuite/btcd/btcjson/v2/btcjson" | ||
"github.com/btcsuite/btcwallet/internal/rpchelp" | ||
) | ||
|
||
func serverMethods() map[string]struct{} { | ||
m := make(map[string]struct{}) | ||
for method, handlerData := range rpcHandlers { | ||
if !handlerData.noHelp { | ||
m[method] = struct{}{} | ||
} | ||
} | ||
return m | ||
} | ||
|
||
// TestRPCMethodHelpGeneration ensures that help text can be generated for every | ||
// method of the RPC server for every supported locale. | ||
func TestRPCMethodHelpGeneration(t *testing.T) { | ||
needsGenerate := false | ||
|
||
defer func() { | ||
if needsGenerate && !t.Failed() { | ||
t.Error("Generated help texts are out of date: run 'go generate'") | ||
return | ||
} | ||
if t.Failed() { | ||
t.Log("Regenerate help texts with 'go generate' after fixing") | ||
} | ||
}() | ||
|
||
for i := range rpchelp.HelpDescs { | ||
svrMethods := serverMethods() | ||
locale := rpchelp.HelpDescs[i].Locale | ||
generatedDescs := localeHelpDescs[locale]() | ||
for _, m := range rpchelp.Methods { | ||
delete(svrMethods, m.Method) | ||
|
||
helpText, err := btcjson.GenerateHelp(m.Method, rpchelp.HelpDescs[i].Descs, m.ResultTypes...) | ||
if err != nil { | ||
t.Errorf("Cannot generate '%s' help for method '%s': missing description for '%s'", | ||
locale, m.Method, err) | ||
continue | ||
} | ||
if !needsGenerate && helpText != generatedDescs[m.Method] { | ||
needsGenerate = true | ||
} | ||
} | ||
|
||
for m := range svrMethods { | ||
t.Errorf("Missing '%s' help for method '%s'", locale, m) | ||
} | ||
} | ||
} | ||
|
||
// TestRPCMethodUsageGeneration ensures that single line usage text can be | ||
// generated for every supported request of the RPC server. | ||
func TestRPCMethodUsageGeneration(t *testing.T) { | ||
needsGenerate := false | ||
|
||
defer func() { | ||
if needsGenerate && !t.Failed() { | ||
t.Error("Generated help usages are out of date: run 'go generate'") | ||
return | ||
} | ||
if t.Failed() { | ||
t.Log("Regenerate help usage with 'go generate' after fixing") | ||
} | ||
}() | ||
|
||
svrMethods := serverMethods() | ||
usageStrs := make([]string, 0, len(rpchelp.Methods)) | ||
for _, m := range rpchelp.Methods { | ||
delete(svrMethods, m.Method) | ||
|
||
usage, err := btcjson.MethodUsageText(m.Method) | ||
if err != nil { | ||
t.Errorf("Cannot generate single line usage for method '%s': %v", | ||
m.Method, err) | ||
} | ||
|
||
if !t.Failed() { | ||
usageStrs = append(usageStrs, usage) | ||
} | ||
} | ||
|
||
if !t.Failed() { | ||
usages := strings.Join(usageStrs, "\n") | ||
needsGenerate = usages != requestUsages | ||
} | ||
} |
Oops, something went wrong.