JSON-RPC with Golang. Electrumx error: Invalid scripthash - go

Tell me please where is my mistake
i try get balance use this method https://electrumx.readthedocs.io/en/latest/protocol-methods.html?highlight=balance#blockchain-scripthash-get-balance
i get scripthash from docs https://electrumx.readthedocs.io/en/latest/protocol-basics.html#script-hashes
and send to electrum
but electrumx return error invalid error map[code:1 message:Invalid scripthash] exit status 1
my code
package main
import (
"log"
"net/rpc/jsonrpc"
)
func main() {
rpcClient, err := jsonrpc.Dial("tcp", "de.poiuty.com:50001")
if err != nil {
log.Fatal(err)
}
scripthash := "8b01df4e368ea28f8dc0423bcf7a4923e3a12d307c875e47a0cfbf90b5c39161"
err = rpcClient.Call("blockchain.scripthash.get_balance", `{"scripthash":"`+scripthash+`"}`, "")
if err != nil {
log.Fatal(err)
}
}

Related

Golang use of Zerolog and raising errors

I'm using zerolog to do logging in my Go app, and I'm trying to follow best practices for only erroring out at the top of the stack. However, I'm not sure exactly how to write both to my error log and pass the error up the chain:
func VeryDeepFunction() (int, error) {
err := doSomethingThatCouldCauseError()
if err != nil {
errMsg := fmt.Sprintf("something bad happened: %+v", err)
log.Error().Msgf(errMsg)
return 0, fmt.Errorf(errMsg)
}
return 1, nil
}
This feels redundant to me - formatting the string, filing with zerolog, and then wrapping the error again. Is there a more correct way to do this?
It is possible to chain errors
package main
import (
"log"
"github.com/pkg/errors"
)
var someError = errors.New("something is wrong")
func main() {
// processing error on top level
if err := someFunc(); errors.Is(err, someError) {
log.Println(err)
}
}
func someFunc() error {
if err := alwaysError(); err != nil {
return errors.Wrap(err, "someFunc")
}
return nil
}
func alwaysError() error {
return someError
}
Output will be
2022/09/01 10:00:46 someFunc: something is wrong
See errors package for details

why can't the go-xorm print the error message

I use xorm to connect to my mysql database,but when my mysql doesn't start,xorm cann't print error message
package main
import (
"fmt"
_ "github.com/go-sql-driver/mysql"
"xorm.io/xorm"
)
var engine *xorm.Engine
func main() {
var err error
engine, err = xorm.NewEngine("mysql", "root:1234567#/blog?charset=utf8mb4")
if err != nil {
fmt.Println(err.Error())//can't print
return
}
}
Use the Ping method to check whether the database is alive
if err := engine.Ping(); err != nil {
panic(err)
}
Ping method

golang os.Close() function works, but os.Remove() function does not

I am trying to create a file, open it, do some processing on it & close it. Finally, I want to delete the file.
All these operations are executed successfully, except the deletion.
My code is:
package main
import (
"fmt"
"os"
"log"
)
func main() {
fmt.Println("Hello")
metaFileName := "./metadata.txt"
_, err2 := os.Create(metaFileName)
if err2 != nil {
log.Fatal(err2)
}
openMetaFile, err := os.Open(metaFileName)
if err != nil {
log.Fatal(err)
}
err = openMetaFile.Close()
if err != nil {
log.Fatal(err)
}
err = os.Remove(metaFileName)
if err != nil {
log.Fatal(err)
}
fmt.Println("Success")
}
The output is:
Hello
2020/08/24 00:00:00 remove ./metadata.txt: The process cannot access the file be
cause it is being used by another process.
I am clueless about this
The problem is related to the first opened file.
package main
import (
"fmt"
"log"
"os"
)
const metaFileName = "./metadata.txt"
func main() {
var (
err error
tmpFile, openMetaFile *os.File
)
fmt.Println("Hello")
if tmpFile, err = os.Create(metaFileName); err != nil {
log.Fatal(err)
}
if err = tmpFile.Close(); err != nil {
log.Fatal(err)
}
if openMetaFile, err = os.Open(metaFileName); err != nil {
log.Fatal(err)
}
if err = openMetaFile.Close(); err != nil {
log.Fatal(err)
}
if err = os.Remove(metaFileName); err != nil {
log.Fatal(err)
}
fmt.Println("Success")
}
As you can see, i've used the var a in order to close the first file that you have opened. The result is following one:
Hello
Success

Panic: runtime error: invalid memory address or nil pointer dereference only on the GAE

I am working on the golang app using gin framework. Basically it just fetch the data from firestore as JSON.
Localy it works perfectly but when I deploy it to the GAE (gcloud app deploy) there is no error during deployment but when access the page it does not work and in the logs provide an error: "panic: runtime error: invalid memory address or nil pointer dereference"
package listcollections
import (
"fmt"
"log"
"net/http"
"cloud.google.com/go/firestore"
"github.com/gin-gonic/gin"
"google.golang.org/api/iterator"
"google.golang.org/appengine"
)
func main() {
}
//GetListCollections function
func GetListCollections(c *gin.Context) {
var coll []string
ctx := appengine.NewContext(c.Request)
projectID := "XXX"
client, err := firestore.NewClient(ctx, projectID)
if err != nil {
log.Fatalf("Failed to create client: %v", err)
}
defer client.Close()
iter := client.Collection("collection").Documents(ctx)
for {
doc, err := iter.Next()
if err == iterator.Done {
break
}
if err != nil {
fmt.Println("ERROR")
}
coll = append(coll, doc.Data()["Title"].(string))
}
c.JSON(http.StatusOK, gin.H{
"collections": coll,
})
}
As no one has any clue where it did happen?
From Analysing your code, the only possibility which I can think off is that your
itr variable is empty.
You might need to change the check for error part and add Panic instead of just printing the error and keep runing
if err != nil {
panic("ERROR")
}

Can't find a public file from url in go

I am trying to get the content of a publicly available file using ioutil.ReadFile() but it doesn't find the file: panic: open http://www.pdf995.com/samples/pdf.pdf: No such file or directory
Here's my code:
// Reading and writing files are basic tasks needed for
// many Go programs. First we'll look at some examples of
// reading files.
package main
import (
"fmt"
"io/ioutil"
)
// Reading files requires checking most calls for errors.
// This helper will streamline our error checks below.
func check(e error) {
if e != nil {
panic(e)
}
}
func main() {
fileInUrl, err := ioutil.ReadFile("http://www.pdf995.com/samples/pdf.pdf")
if err != nil {
panic(err)
}
fmt.Printf("HERE --- fileInUrl: %+v", fileInUrl)
}
Here's a go playground example
ioutil.ReadFile() does not support http.
If you look at the source code(https://golang.org/src/io/ioutil/ioutil.go?s=1503:1549#L42), open the file using os.Open.
I think I can do this coding.
package main
import (
"io"
"net/http"
"os"
)
func main() {
fileUrl := "http://www.pdf995.com/samples/pdf.pdf"
if err := DownloadFile("example.pdf", fileUrl); err != nil {
panic(err)
}
}
func DownloadFile(filepath string, url string) error {
// Get the data
resp, err := http.Get(url)
if err != nil {
return err
}
defer resp.Body.Close()
// Create the file
out, err := os.Create(filepath)
if err != nil {
return err
}
defer out.Close()
// Write the body to file
_, err = io.Copy(out, resp.Body)
return err
}
but, go playgound not protocol(go error dial tcp: Protocol not available).
so, You have to do it PC.

Resources