Faster JSON encoder/decoder
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
DataHoarder 2023-05-27 14:43:22 +02:00
parent 59756ab560
commit d577856081
Signed by: DataHoarder
SSH key fingerprint: SHA256:OLTRf6Fl87G52SiR7sWLGNzlJt4WOX+tfI2yxo0z7xk
22 changed files with 184 additions and 195 deletions

View file

@ -4,7 +4,6 @@ import (
"bytes"
"context"
"encoding/hex"
"encoding/json"
"flag"
"fmt"
utils2 "git.gammaspectra.live/P2Pool/p2pool-observer/cmd/utils"
@ -38,9 +37,9 @@ import (
func encodeJson(r *http.Request, d any) ([]byte, error) {
if strings.Index(strings.ToLower(r.Header.Get("user-agent")), "mozilla") != -1 {
return json.MarshalIndent(d, "", " ")
return utils.MarshalJSONIndent(d, " ")
} else {
return json.Marshal(d)
return utils.MarshalJSON(d)
}
}
@ -379,7 +378,7 @@ func main() {
if miner == nil {
writer.Header().Set("Content-Type", "application/json; charset=utf-8")
writer.WriteHeader(http.StatusNotFound)
buf, _ := json.Marshal(struct {
buf, _ := utils.MarshalJSON(struct {
Error string `json:"error"`
}{
Error: "not_found",
@ -448,7 +447,7 @@ func main() {
if request.Method != "POST" {
writer.Header().Set("Content-Type", "application/json; charset=utf-8")
writer.WriteHeader(http.StatusMethodNotAllowed)
buf, _ := json.Marshal(struct {
buf, _ := utils.MarshalJSON(struct {
Error string `json:"error"`
}{
Error: "not_allowed",
@ -461,7 +460,7 @@ func main() {
if err != nil {
writer.Header().Set("Content-Type", "application/json; charset=utf-8")
writer.WriteHeader(http.StatusBadRequest)
buf, _ := json.Marshal(struct {
buf, _ := utils.MarshalJSON(struct {
Error string `json:"error"`
}{
Error: "bad_request",
@ -470,10 +469,10 @@ func main() {
return
}
var indices []uint64
if err = json.Unmarshal(buf, &indices); err != nil || len(indices) == 0 {
if err = utils.UnmarshalJSON(buf, &indices); err != nil || len(indices) == 0 {
writer.Header().Set("Content-Type", "application/json; charset=utf-8")
writer.WriteHeader(http.StatusBadRequest)
buf, _ := json.Marshal(struct {
buf, _ := utils.MarshalJSON(struct {
Error string `json:"error"`
}{
Error: "bad_request",
@ -493,7 +492,7 @@ func main() {
if request.Method != "POST" {
writer.Header().Set("Content-Type", "application/json; charset=utf-8")
writer.WriteHeader(http.StatusMethodNotAllowed)
buf, _ := json.Marshal(struct {
buf, _ := utils.MarshalJSON(struct {
Error string `json:"error"`
}{
Error: "not_allowed",
@ -506,7 +505,7 @@ func main() {
if err != nil {
writer.Header().Set("Content-Type", "application/json; charset=utf-8")
writer.WriteHeader(http.StatusBadRequest)
buf, _ := json.Marshal(struct {
buf, _ := utils.MarshalJSON(struct {
Error string `json:"error"`
}{
Error: "bad_request",
@ -515,10 +514,10 @@ func main() {
return
}
var indices []uint64
if err = json.Unmarshal(buf, &indices); err != nil || len(indices) == 0 {
if err = utils.UnmarshalJSON(buf, &indices); err != nil || len(indices) == 0 {
writer.Header().Set("Content-Type", "application/json; charset=utf-8")
writer.WriteHeader(http.StatusBadRequest)
buf, _ := json.Marshal(struct {
buf, _ := utils.MarshalJSON(struct {
Error string `json:"error"`
}{
Error: "bad_request",
@ -539,7 +538,7 @@ func main() {
if err != nil {
writer.Header().Set("Content-Type", "application/json; charset=utf-8")
writer.WriteHeader(http.StatusNotFound)
buf, _ := json.Marshal(struct {
buf, _ := utils.MarshalJSON(struct {
Error string `json:"error"`
}{
Error: "not_found",
@ -552,7 +551,7 @@ func main() {
if os.Getenv("TRANSACTION_LOOKUP_OTHER") != "" {
otherLookupHostFunc = func(ctx context.Context, indices []uint64) (result []*index.MatchedOutput) {
data, _ := json.Marshal(indices)
data, _ := utils.MarshalJSON(indices)
result = make([]*index.MatchedOutput, len(indices))
@ -572,7 +571,7 @@ func main() {
if response.StatusCode == http.StatusOK {
if data, err := io.ReadAll(response.Body); err == nil {
r := make([]*index.MatchedOutput, 0, len(indices))
if json.Unmarshal(data, &r) == nil && len(r) == len(indices) {
if utils.UnmarshalJSON(data, &r) == nil && len(r) == len(indices) {
for i := range r {
if result[i] == nil {
result[i] = r[i]
@ -593,7 +592,7 @@ func main() {
if len(results) == 0 {
writer.Header().Set("Content-Type", "application/json; charset=utf-8")
writer.WriteHeader(http.StatusNotFound)
buf, _ := json.Marshal(struct {
buf, _ := utils.MarshalJSON(struct {
Error string `json:"error"`
}{
Error: "not_found",
@ -610,7 +609,7 @@ func main() {
if outs, err := client.GetDefaultClient().GetOuts(indicesToLookup...); err != nil {
writer.Header().Set("Content-Type", "application/json; charset=utf-8")
writer.WriteHeader(http.StatusBadRequest)
buf, _ := json.Marshal(struct {
buf, _ := utils.MarshalJSON(struct {
Error string `json:"error"`
}{
Error: "bad_request",
@ -643,7 +642,7 @@ func main() {
if miner == nil {
writer.Header().Set("Content-Type", "application/json; charset=utf-8")
writer.WriteHeader(http.StatusNotFound)
buf, _ := json.Marshal(struct {
buf, _ := utils.MarshalJSON(struct {
Error string `json:"error"`
}{
Error: "not_found",
@ -669,7 +668,7 @@ func main() {
}() || !unicode.IsLetter(rune(message[0])) {
writer.Header().Set("Content-Type", "application/json; charset=utf-8")
writer.WriteHeader(http.StatusBadRequest)
buf, _ := json.Marshal(struct {
buf, _ := utils.MarshalJSON(struct {
Error string `json:"error"`
}{
Error: "invalid_message",
@ -686,7 +685,7 @@ func main() {
if indexDb.SetMinerAlias(miner.Id(), message) != nil {
writer.Header().Set("Content-Type", "application/json; charset=utf-8")
writer.WriteHeader(http.StatusBadRequest)
buf, _ := json.Marshal(struct {
buf, _ := utils.MarshalJSON(struct {
Error string `json:"error"`
}{
Error: "duplicate_message",
@ -701,7 +700,7 @@ func main() {
} else if result == address.ResultSuccessView {
writer.Header().Set("Content-Type", "application/json; charset=utf-8")
writer.WriteHeader(http.StatusBadRequest)
buf, _ := json.Marshal(struct {
buf, _ := utils.MarshalJSON(struct {
Error string `json:"error"`
}{
Error: "view_signature",
@ -711,7 +710,7 @@ func main() {
} else {
writer.Header().Set("Content-Type", "application/json; charset=utf-8")
writer.WriteHeader(http.StatusBadRequest)
buf, _ := json.Marshal(struct {
buf, _ := utils.MarshalJSON(struct {
Error string `json:"error"`
}{
Error: "invalid_signature",
@ -772,7 +771,7 @@ func main() {
if miner == nil {
writer.Header().Set("Content-Type", "application/json; charset=utf-8")
writer.WriteHeader(http.StatusNotFound)
buf, _ := json.Marshal(struct {
buf, _ := utils.MarshalJSON(struct {
Error string `json:"error"`
}{
Error: "not_found",
@ -856,7 +855,7 @@ func main() {
if miner == nil {
writer.Header().Set("Content-Type", "application/json; charset=utf-8")
writer.WriteHeader(http.StatusNotFound)
buf, _ := json.Marshal(struct {
buf, _ := utils.MarshalJSON(struct {
Error string `json:"error"`
}{
Error: "not_found",
@ -898,7 +897,7 @@ func main() {
if miner == nil {
writer.Header().Set("Content-Type", "application/json; charset=utf-8")
writer.WriteHeader(http.StatusNotFound)
buf, _ := json.Marshal(struct {
buf, _ := utils.MarshalJSON(struct {
Error string `json:"error"`
}{
Error: "not_found",
@ -947,7 +946,7 @@ func main() {
if len(foundTargets) == 0 {
writer.Header().Set("Content-Type", "application/json; charset=utf-8")
writer.WriteHeader(http.StatusNotFound)
buf, _ := json.Marshal(struct {
buf, _ := utils.MarshalJSON(struct {
Error string `json:"error"`
}{
Error: "not_found",
@ -985,7 +984,7 @@ func main() {
if b == nil {
writer.Header().Set("Content-Type", "application/json; charset=utf-8")
writer.WriteHeader(http.StatusNotFound)
buf, _ := json.Marshal(struct {
buf, _ := utils.MarshalJSON(struct {
Error string `json:"error"`
}{
Error: "not_found",
@ -1013,7 +1012,7 @@ func main() {
if len(b) == 0 || tx == nil {
writer.Header().Set("Content-Type", "application/json; charset=utf-8")
writer.WriteHeader(http.StatusNotFound)
buf, _ := json.Marshal(struct {
buf, _ := utils.MarshalJSON(struct {
Error string `json:"error"`
}{
Error: "not_found",
@ -1031,7 +1030,7 @@ func main() {
if len(b) == 0 || miner == nil {
writer.Header().Set("Content-Type", "application/json; charset=utf-8")
writer.WriteHeader(http.StatusNotFound)
buf, _ := json.Marshal(struct {
buf, _ := utils.MarshalJSON(struct {
Error string `json:"error"`
}{
Error: "not_found",
@ -1045,7 +1044,7 @@ func main() {
if tx == nil {
writer.Header().Set("Content-Type", "application/json; charset=utf-8")
writer.WriteHeader(http.StatusNotFound)
buf, _ := json.Marshal(struct {
buf, _ := utils.MarshalJSON(struct {
Error string `json:"error"`
}{
Error: "not_found",
@ -1062,7 +1061,7 @@ func main() {
if miner == nil {
writer.Header().Set("Content-Type", "application/json; charset=utf-8")
writer.WriteHeader(http.StatusNotFound)
buf, _ := json.Marshal(struct {
buf, _ := utils.MarshalJSON(struct {
Error string `json:"error"`
}{
Error: "not_found",
@ -1116,7 +1115,7 @@ func main() {
if miner == nil {
writer.Header().Set("Content-Type", "application/json; charset=utf-8")
writer.WriteHeader(http.StatusNotFound)
buf, _ := json.Marshal(struct {
buf, _ := utils.MarshalJSON(struct {
Error string `json:"error"`
}{
Error: "not_found",
@ -1195,7 +1194,7 @@ func main() {
if miner == nil {
writer.Header().Set("Content-Type", "application/json; charset=utf-8")
writer.WriteHeader(http.StatusNotFound)
buf, _ := json.Marshal(struct {
buf, _ := utils.MarshalJSON(struct {
Error string `json:"error"`
}{
Error: "not_found",
@ -1254,7 +1253,7 @@ func main() {
if miner == nil {
writer.Header().Set("Content-Type", "application/json; charset=utf-8")
writer.WriteHeader(http.StatusNotFound)
buf, _ := json.Marshal(struct {
buf, _ := utils.MarshalJSON(struct {
Error string `json:"error"`
}{
Error: "not_found",
@ -1292,7 +1291,7 @@ func main() {
if block == nil {
writer.Header().Set("Content-Type", "application/json; charset=utf-8")
writer.WriteHeader(http.StatusNotFound)
buf, _ := json.Marshal(struct {
buf, _ := utils.MarshalJSON(struct {
Error string `json:"error"`
}{
Error: "not_found",
@ -1364,7 +1363,7 @@ func main() {
if block == nil {
writer.Header().Set("Content-Type", "application/json; charset=utf-8")
writer.WriteHeader(http.StatusNotFound)
buf, _ := json.Marshal(struct {
buf, _ := utils.MarshalJSON(struct {
Error string `json:"error"`
}{
Error: "not_found",
@ -1380,7 +1379,7 @@ func main() {
if raw == nil {
writer.Header().Set("Content-Type", "application/json; charset=utf-8")
writer.WriteHeader(http.StatusNotFound)
buf, _ := json.Marshal(struct {
buf, _ := utils.MarshalJSON(struct {
Error string `json:"error"`
}{
Error: "not_found",
@ -1391,7 +1390,7 @@ func main() {
writer.Header().Set("Content-Type", "application/json; charset=utf-8")
writer.WriteHeader(http.StatusOK)
buf, _ := json.Marshal(raw)
buf, _ := utils.MarshalJSON(raw)
_, _ = writer.Write(buf)
case "/raw":
raw := p2api.ByMainId(block.MainId)
@ -1399,7 +1398,7 @@ func main() {
if raw == nil {
writer.Header().Set("Content-Type", "application/json; charset=utf-8")
writer.WriteHeader(http.StatusNotFound)
buf, _ := json.Marshal(struct {
buf, _ := utils.MarshalJSON(struct {
Error string `json:"error"`
}{
Error: "not_found",
@ -1464,7 +1463,7 @@ func main() {
writer.Header().Set("Content-Type", "application/json; charset=utf-8")
writer.WriteHeader(http.StatusNotFound)
buf, _ := json.Marshal(struct {
buf, _ := utils.MarshalJSON(struct {
Error string `json:"error"`
}{
Error: "not_found",
@ -1579,7 +1578,7 @@ func main() {
if err != nil {
writer.Header().Set("Content-Type", "application/json; charset=utf-8")
writer.WriteHeader(http.StatusBadRequest)
buf, _ := json.Marshal(struct {
buf, _ := utils.MarshalJSON(struct {
Error string `json:"error"`
}{
Error: err.Error(),
@ -1593,7 +1592,7 @@ func main() {
if !addrPort.IsValid() || addrPort.Addr().IsLoopback() || addrPort.Addr().IsMulticast() || addrPort.Addr().IsInterfaceLocalMulticast() || addrPort.Addr().IsLinkLocalMulticast() || addrPort.Addr().IsPrivate() || addrPort.Addr().IsUnspecified() || addrPort.Addr().IsLinkLocalUnicast() {
writer.Header().Set("Content-Type", "application/json; charset=utf-8")
writer.WriteHeader(http.StatusBadRequest)
buf, _ := json.Marshal(struct {
buf, _ := utils.MarshalJSON(struct {
Error string `json:"error"`
}{
Error: "not_valid_ip",
@ -1606,7 +1605,7 @@ func main() {
//do not allow low port numbers to prevent targeting
writer.Header().Set("Content-Type", "application/json; charset=utf-8")
writer.WriteHeader(http.StatusBadRequest)
buf, _ := json.Marshal(struct {
buf, _ := utils.MarshalJSON(struct {
Error string `json:"error"`
}{
Error: "not_valid_port",

View file

@ -1,20 +1,20 @@
package main
import (
"encoding/json"
"git.gammaspectra.live/P2Pool/p2pool-observer/cmd/utils"
cmdutils "git.gammaspectra.live/P2Pool/p2pool-observer/cmd/utils"
"git.gammaspectra.live/P2Pool/p2pool-observer/utils"
"io"
"net/http"
"sync"
"time"
)
var moneroVersion, p2poolVersion utils.VersionInfo
var moneroVersion, p2poolVersion cmdutils.VersionInfo
var moneroVersionLock, p2poolVersionLock sync.Mutex
const versionCheckInterval = 3600
func getMoneroVersion() utils.VersionInfo {
func getMoneroVersion() cmdutils.VersionInfo {
moneroVersionLock.Lock()
defer moneroVersionLock.Unlock()
now := time.Now().Unix()
@ -27,10 +27,10 @@ func getMoneroVersion() utils.VersionInfo {
}
defer response.Body.Close()
var releaseData utils.ReleaseDataJson
var releaseData cmdutils.ReleaseDataJson
if data, err := io.ReadAll(response.Body); err != nil {
return moneroVersion
} else if err = json.Unmarshal(data, &releaseData); err != nil {
} else if err = utils.UnmarshalJSON(data, &releaseData); err != nil {
return moneroVersion
} else {
moneroVersion.Version = releaseData.TagName
@ -42,7 +42,7 @@ func getMoneroVersion() utils.VersionInfo {
return moneroVersion
}
func getP2PoolVersion() utils.VersionInfo {
func getP2PoolVersion() cmdutils.VersionInfo {
p2poolVersionLock.Lock()
defer p2poolVersionLock.Unlock()
now := time.Now().Unix()
@ -55,10 +55,10 @@ func getP2PoolVersion() utils.VersionInfo {
}
defer response.Body.Close()
var releaseData utils.ReleaseDataJson
var releaseData cmdutils.ReleaseDataJson
if data, err := io.ReadAll(response.Body); err != nil {
return p2poolVersion
} else if err = json.Unmarshal(data, &releaseData); err != nil {
} else if err = utils.UnmarshalJSON(data, &releaseData); err != nil {
return p2poolVersion
} else {
p2poolVersion.Version = releaseData.TagName

View file

@ -2,11 +2,11 @@ package main
import (
"context"
"encoding/json"
utils2 "git.gammaspectra.live/P2Pool/p2pool-observer/cmd/utils"
"git.gammaspectra.live/P2Pool/p2pool-observer/index"
"git.gammaspectra.live/P2Pool/p2pool-observer/p2pool/api"
types2 "git.gammaspectra.live/P2Pool/p2pool-observer/p2pool/types"
"git.gammaspectra.live/P2Pool/p2pool-observer/utils"
"golang.org/x/exp/slices"
"log"
"net/http"
@ -317,7 +317,7 @@ func setupEventHandler(p2api *api.P2PoolApi, indexDb *index.Index) {
listenerLock.RLock()
defer listenerLock.RUnlock()
for _, b := range blocksToReport {
buf, err := json.Marshal(&utils2.JSONEvent{
buf, err := utils.MarshalJSON(&utils2.JSONEvent{
Type: utils2.JSONEventSideBlock,
SideBlock: b,
FoundBlock: nil,
@ -413,7 +413,7 @@ func setupEventHandler(p2api *api.P2PoolApi, indexDb *index.Index) {
listenerLock.RLock()
defer listenerLock.RUnlock()
for _, b := range unfoundBlocksToReport {
buf, err := json.Marshal(&utils2.JSONEvent{
buf, err := utils.MarshalJSON(&utils2.JSONEvent{
Type: utils2.JSONEventOrphanedBlock,
SideBlock: b,
FoundBlock: nil,
@ -437,7 +437,7 @@ func setupEventHandler(p2api *api.P2PoolApi, indexDb *index.Index) {
foundBlockBuffer.Remove(b)
continue
}
buf, err := json.Marshal(&utils2.JSONEvent{
buf, err := utils.MarshalJSON(&utils2.JSONEvent{
Type: utils2.JSONEventFoundBlock,
SideBlock: nil,
FoundBlock: b,

View file

@ -2,13 +2,13 @@ package main
import (
"bytes"
"encoding/json"
"git.gammaspectra.live/P2Pool/p2pool-observer/monero/randomx"
"git.gammaspectra.live/P2Pool/p2pool-observer/p2pool"
"git.gammaspectra.live/P2Pool/p2pool-observer/p2pool/p2p"
"git.gammaspectra.live/P2Pool/p2pool-observer/p2pool/sidechain"
p2pooltypes "git.gammaspectra.live/P2Pool/p2pool-observer/p2pool/types"
"git.gammaspectra.live/P2Pool/p2pool-observer/types"
"git.gammaspectra.live/P2Pool/p2pool-observer/utils"
"github.com/gorilla/mux"
"io"
"math"
@ -21,11 +21,10 @@ import (
)
func encodeJson(r *http.Request, w http.ResponseWriter, d any) error {
encoder := json.NewEncoder(w)
encoder := utils.NewJSONEncoder(w)
if strings.Index(strings.ToLower(r.Header.Get("user-agent")), "mozilla") != -1 {
encoder.SetIndent("", " ")
}
encoder.SetEscapeHTML(false)
return encoder.Encode(d)
}
@ -65,7 +64,7 @@ func getServerMux(instance *p2pool.P2Pool) *mux.Router {
if err != nil {
writer.Header().Set("Content-Type", "application/json; charset=utf-8")
writer.WriteHeader(http.StatusBadRequest)
buf, _ := json.Marshal(struct {
buf, _ := utils.MarshalJSON(struct {
Error string `json:"error"`
}{
Error: err.Error(),
@ -88,7 +87,7 @@ func getServerMux(instance *p2pool.P2Pool) *mux.Router {
if client, err = instance.Server().DirectConnect(addrPort); err != nil {
writer.Header().Set("Content-Type", "application/json; charset=utf-8")
writer.WriteHeader(http.StatusBadRequest)
buf, _ := json.Marshal(struct {
buf, _ := utils.MarshalJSON(struct {
Error string `json:"error"`
}{
Error: err.Error(),
@ -101,7 +100,7 @@ func getServerMux(instance *p2pool.P2Pool) *mux.Router {
if client == nil {
writer.Header().Set("Content-Type", "application/json; charset=utf-8")
writer.WriteHeader(http.StatusBadRequest)
buf, _ := json.Marshal(struct {
buf, _ := utils.MarshalJSON(struct {
Error string `json:"error"`
}{
Error: "could not find client",

View file

@ -1,11 +1,11 @@
package main
import (
"encoding/json"
"fmt"
"git.gammaspectra.live/P2Pool/p2pool-observer/monero/address"
"git.gammaspectra.live/P2Pool/p2pool-observer/monero/crypto"
"git.gammaspectra.live/P2Pool/p2pool-observer/types"
"git.gammaspectra.live/P2Pool/p2pool-observer/utils"
)
type JsonBlock2 struct {
@ -84,19 +84,19 @@ type versionBlock struct {
func JSONFromTemplate(data []byte) (any, error) {
var version versionBlock
if err := json.Unmarshal(data, &version); err != nil {
if err := utils.UnmarshalJSON(data, &version); err != nil {
return nil, err
} else {
if version.Version == 2 {
var b JsonBlock2
if err = json.Unmarshal(data, &b); err != nil {
if err = utils.UnmarshalJSON(data, &b); err != nil {
return nil, err
}
return b, nil
} else if version.Version == 0 || version.Version == 1 {
var b JsonBlock1
if err = json.Unmarshal(data, &b); err != nil {
if err = utils.UnmarshalJSON(data, &b); err != nil {
return nil, err
}
return b, nil

View file

@ -4,12 +4,12 @@ import (
"bytes"
"context"
"encoding/binary"
"encoding/json"
"fmt"
"git.gammaspectra.live/P2Pool/p2pool-observer/index"
"git.gammaspectra.live/P2Pool/p2pool-observer/monero/client"
"git.gammaspectra.live/P2Pool/p2pool-observer/p2pool/sidechain"
"git.gammaspectra.live/P2Pool/p2pool-observer/types"
"git.gammaspectra.live/P2Pool/p2pool-observer/utils"
"io"
"log"
"net/http"
@ -82,7 +82,7 @@ var otherLookupHostFunc func(ctx context.Context, indices []uint64) []*index.Mat
func init() {
if os.Getenv("TRANSACTION_LOOKUP_OTHER") != "" {
otherLookupHostFunc = func(ctx context.Context, indices []uint64) (result []*index.MatchedOutput) {
data, _ := json.Marshal(indices)
data, _ := utils.MarshalJSON(indices)
result = make([]*index.MatchedOutput, len(indices))
@ -102,7 +102,7 @@ func init() {
if response.StatusCode == http.StatusOK {
if data, err := io.ReadAll(response.Body); err == nil {
r := make([]*index.MatchedOutput, 0, len(indices))
if json.Unmarshal(data, &r) == nil && len(r) == len(indices) {
if utils.UnmarshalJSON(data, &r) == nil && len(r) == len(indices) {
for i := range r {
if result[i] == nil {
result[i] = r[i]

View file

@ -1,8 +1,8 @@
package main
import (
"encoding/json"
"git.gammaspectra.live/P2Pool/p2pool-observer/index"
"git.gammaspectra.live/P2Pool/p2pool-observer/utils"
"io"
"net/http"
"net/url"
@ -10,6 +10,10 @@ import (
"time"
)
import jsoniter "github.com/json-iterator/go"
var json = jsoniter.ConfigCompatibleWithStandardLibrary
func getTypeFromAPI[T any](method string, cacheTime ...int) *T {
cTime := 0
if len(cacheTime) > 0 {
@ -27,9 +31,8 @@ func getTypeFromAPI[T any](method string, cacheTime ...int) *T {
defer response.Body.Close()
if response.StatusCode == http.StatusOK {
var result T
if data, err := io.ReadAll(response.Body); err != nil {
return nil
} else if json.Unmarshal(data, &result) != nil {
decoder := utils.NewJSONDecoder(response.Body)
if decoder.Decode(&result) != nil {
return nil
} else {
return &result
@ -58,9 +61,8 @@ func getSliceFromAPI[T any](method string, cacheTime ...int) []T {
defer response.Body.Close()
if response.StatusCode == http.StatusOK {
var result []T
if data, err := io.ReadAll(response.Body); err != nil {
return nil
} else if json.Unmarshal(data, &result) != nil {
decoder := utils.NewJSONDecoder(response.Body)
if decoder.Decode(&result) != nil {
return nil
} else {
return result

View file

@ -2,7 +2,6 @@ package main
import (
"bytes"
"encoding/json"
"flag"
"fmt"
cmdutils "git.gammaspectra.live/P2Pool/p2pool-observer/cmd/utils"
@ -35,12 +34,6 @@ import (
)
func toUint64(t any) uint64 {
if x, ok := t.(json.Number); ok {
if n, err := x.Int64(); err == nil {
return uint64(n)
}
}
if x, ok := t.(uint64); ok {
return x
} else if x, ok := t.(int64); ok {
@ -82,12 +75,6 @@ func toString(t any) string {
}
func toInt64(t any) int64 {
if x, ok := t.(json.Number); ok {
if n, err := x.Int64(); err == nil {
return n
}
}
if x, ok := t.(uint64); ok {
return int64(x)
} else if x, ok := t.(int64); ok {
@ -114,12 +101,6 @@ func toInt64(t any) int64 {
}
func toFloat64(t any) float64 {
if x, ok := t.(json.Number); ok {
if n, err := x.Float64(); err == nil {
return n
}
}
if x, ok := t.(float64); ok {
return x
} else if x, ok := t.(float32); ok {
@ -191,7 +172,7 @@ func main() {
time.Sleep(1)
}
consensusData, _ := json.Marshal(basePoolInfo.SideChain.Consensus)
consensusData, _ := utils.MarshalJSON(basePoolInfo.SideChain.Consensus)
consensus, err := sidechain.NewConsensusFromJSON(consensusData)
if err != nil {
log.Panic(err)
@ -370,7 +351,7 @@ func main() {
ourTip := getTypeFromAPI[index.SideBlock]("redirect/tip")
var theirTip *index.SideBlock
if checkInformation != nil {
if buf, err := json.Marshal(checkInformation.Tip); err == nil && checkInformation.Tip != nil {
if buf, err := utils.MarshalJSON(checkInformation.Tip); err == nil && checkInformation.Tip != nil {
b := sidechain.PoolBlock{}
if json.Unmarshal(buf, &b) == nil {
rawTip = &b
@ -772,7 +753,7 @@ func main() {
for i, o := range coinbase {
indices[i] = o.GlobalOutputIndex
}
data, _ := json.Marshal(indices)
data, _ := utils.MarshalJSON(indices)
uri, _ := url.Parse(os.Getenv("API_URL") + "sweeps_by_spending_global_output_indices")
if response, err := http.DefaultClient.Do(&http.Request{
Method: "POST",

3
go.mod
View file

@ -14,6 +14,7 @@ require (
github.com/go-zeromq/zmq4 v0.15.0
github.com/gorilla/mux v1.8.0
github.com/holiman/uint256 v1.2.2
github.com/json-iterator/go v1.1.12
github.com/jxskiss/base62 v1.1.0
github.com/lib/pq v1.10.9
github.com/stretchr/testify v1.8.1
@ -30,6 +31,8 @@ require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/go-zeromq/goczmq/v4 v4.2.2 // indirect
github.com/klauspost/compress v1.16.5 // indirect
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/valyala/bytebufferpool v1.0.0 // indirect
golang.org/x/crypto v0.9.0 // indirect

35
go.sum
View file

@ -13,26 +13,20 @@ git.gammaspectra.live/P2Pool/sha3 v0.0.0-20230512064207-dde79576dc2f/go.mod h1:6
github.com/ake-persson/mapslice-json v0.0.0-20210720081907-22c8edf57807 h1:w3nrGk00TWs/4iZ3Q0k9c0vL0e/wRziArKU4e++d/nA=
github.com/ake-persson/mapslice-json v0.0.0-20210720081907-22c8edf57807/go.mod h1:fGnnfniJiO/ajHAVHqMSUSL8sE9LmU9rzclCtoeB+y8=
github.com/andybalholm/brotli v1.0.2/go.mod h1:loMXtMfwqflxFJPmdbJO0a3KNoPuLBgiu3qAvBg8x/Y=
github.com/andybalholm/brotli v1.0.3 h1:fpcw+r1N1h0Poc1F/pHbW40cUm/lMEQslZtCkBQ0UnM=
github.com/andybalholm/brotli v1.0.3/go.mod h1:fO7iG3H7G2nSZ7m0zPUDn85XEX2GTukHGRSepvi9Eig=
github.com/bahlo/generic-list-go v0.2.0 h1:5sz/EEAK+ls5wF+NeqDpk5+iNdMDXrh3z3nPnH1Wvgk=
github.com/bahlo/generic-list-go v0.2.0/go.mod h1:2KvAjgMlE5NNynlg/5iLrrCCZ2+5xWbdbCW3pNTGyYg=
github.com/client9/misspell v0.3.4 h1:ta993UF76GwbvJcIo3Y68y/M3WxlpEHPWIGDkJYwzJI=
github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/dustin/go-humanize v1.0.1 h1:GzkhY7T5VNhEkwH0PVJgjz+fX1rhBrR7pRT3mDkpeCY=
github.com/fatih/color v1.15.0 h1:kOqh6YHBtK8aywxGerMG2Eq3H6Qgoqeo13Bk2Mv/nBs=
github.com/floatdrop/lru v1.3.0 h1:83abtaKjXcWrPmtzTAk2Ggq8DUKqI29YzrTrB8+vu0c=
github.com/floatdrop/lru v1.3.0/go.mod h1:83zlXKA06Bm32JImNINCiTr0ldadvdAjUe5jSwIaw0s=
github.com/fzipp/gocyclo v0.3.1 h1:A9UeX3HJSXTBzvHzhqoYVuE0eAhe+aM8XBCCwsPMZOc=
github.com/fzipp/gocyclo v0.3.1/go.mod h1:DJHO6AUmbdqj2ET4Z9iArSuwWgYDRryYt2wASxc7x3E=
github.com/gin-contrib/sse v0.1.0 h1:Y/yl/+YNO8GZSjAhjMsSuLt29uWRFHdHYUb5lYOV9qE=
github.com/gin-contrib/sse v0.1.0/go.mod h1:RHrZQHXnP2xjPF+u1gW/2HnVO7nvIa9PG3Gm+fLHvGI=
github.com/gin-gonic/gin v1.6.3 h1:ahKqKTFpO5KTPHxWZjEdPScmYaGtLo8Y4DMHoEsnp14=
github.com/gin-gonic/gin v1.6.3/go.mod h1:75u5sXoLsGZoRN5Sgbi1eraJ4GU3++wFwWzhwvtwp4M=
github.com/go-playground/assert/v2 v2.0.1 h1:MsBgLAaY856+nPRTKrp3/OZK38U/wa0CcBYNjji3q3A=
github.com/go-playground/assert/v2 v2.0.1/go.mod h1:VDjEfimB/XKnb+ZQfWdccd7VUvScMdVu0Titje2rxJ4=
github.com/go-playground/locales v0.13.0 h1:HyWk6mgj5qFqCT5fjGBuRArbVDfE4hi8+e8ceBS/t7Q=
github.com/go-playground/locales v0.13.0/go.mod h1:taPMhCMXrRLJO55olJkUXHZBHCxTMfnGwq/HNwmWNS8=
@ -53,27 +47,22 @@ github.com/gobwas/ws v1.0.2/go.mod h1:szmBTxLgaFppYjEmNtny/v3w89xOydFnnZMcgRRu/E
github.com/golang/protobuf v1.3.3/go.mod h1:vzj43D7+SQXF/4pzW/hwtAqwc6iTitCiVSaWz5lYuqw=
github.com/golang/protobuf v1.3.5 h1:F768QJ1E9tib+q5Sc8MkdJi1RxLTbRcTf8LJV56aRls=
github.com/golang/protobuf v1.3.5/go.mod h1:6O5/vntMXwX2lRkT1hjjk0nAC1IDOTvTlVgjlRvqsdk=
github.com/golang/snappy v0.0.3 h1:fHPg5GQYlCeLIPB9BZqMVR5nR9A+IM5zcgeTdjMYmLA=
github.com/golang/snappy v0.0.3/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q=
github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.8 h1:e6P7q2lk1O+qJJb4BtCQXlK8vWEO8V1ZeuEdJNOqZyg=
github.com/google/gofuzz v1.0.0 h1:A8PeW59pxE9IoFRqBp37U+mSNaQoZ46F1f0f863XSXw=
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
github.com/gordonklaus/ineffassign v0.0.0-20210522101830-0589229737b2 h1:hC4RAQwLzbDbHsa+CwwGBm1uG2oX9o3Frx9G73duPi8=
github.com/gordonklaus/ineffassign v0.0.0-20210522101830-0589229737b2/go.mod h1:M9mZEtGIsR1oDaZagNPNG9iq9n2HrhZ17dsXk73V3Lw=
github.com/gorilla/mux v1.8.0 h1:i40aqfkR1h2SlN9hojwV5ZA91wcXFOvkdNIeFDP5koI=
github.com/gorilla/mux v1.8.0/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So=
github.com/gorilla/websocket v1.4.1 h1:q7AeDBpnBk8AogcD4DSag/Ukw/KV+YhzLj2bP5HvKCM=
github.com/gorilla/websocket v1.4.1/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
github.com/gosuri/uitable v0.0.4 h1:IG2xLKRvErL3uhY6e1BylFzG+aJiwQviDDTfOKeKTpY=
github.com/holiman/uint256 v1.2.2 h1:TXKcSGc2WaxPD2+bmzAsVthL4+pEN0YwXcL5qED83vk=
github.com/holiman/uint256 v1.2.2/go.mod h1:SC8Ryt4n+UBbPbIBKaG9zbbDlp4jOru9xFZmPzLUTxw=
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
github.com/json-iterator/go v1.1.9 h1:9yzud/Ht36ygwatGx56VwCZtlI/2AD15T1X2sjSuGns=
github.com/json-iterator/go v1.1.9/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4=
github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=
github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo=
github.com/jxskiss/base62 v1.1.0 h1:A5zbF8v8WXx2xixnAKD2w+abC+sIzYJX+nxmhA6HWFw=
github.com/jxskiss/base62 v1.1.0/go.mod h1:HhWAlUXvxKThfOlZbcuFzsqwtF5TcqS9ru3y5GfjWAc=
github.com/kisielk/errcheck v1.6.0 h1:YTDO4pNy7AUN/021p+JGHycQyYNIyMoenM1YDVK6RlY=
github.com/kisielk/errcheck v1.6.0/go.mod h1:pFxgyoBC7bSaBwPgfKdkLd5X25qrDl4LWUI2bnpBCr8=
github.com/klauspost/compress v1.10.3/go.mod h1:aoV0uJVorq1K+umq18yTdKaF57EivdYsUV+/s2qKfXs=
github.com/klauspost/compress v1.13.4/go.mod h1:8dP1Hq4DHOhN9w426knH3Rhby4rFm6D8eO+e+Dq5Gzg=
@ -85,24 +74,19 @@ github.com/leodido/go-urn v1.2.0 h1:hpXL4XnriNwQ/ABnpepYM/1vCLWNDfUNts8dX3xTG6Y=
github.com/leodido/go-urn v1.2.0/go.mod h1:+8+nEpDfqqsY+g338gtMEUOtuK+4dEMhiQEgxpxOKII=
github.com/lib/pq v1.10.9 h1:YXG7RB+JIjhP29X+OtkiDnYaXQwpS4JEWq7dtCCRUEw=
github.com/lib/pq v1.10.9/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o=
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU=
github.com/mattn/go-isatty v0.0.18 h1:DOKFKCQ7FNG2L1rbrmstDN4QVRdS89Nkh85u68Uwp98=
github.com/mattn/go-runewidth v0.0.14 h1:+xnbZSEeDbOIg5/mE6JF0w6n9duR1l3/WmbinWVwUuU=
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 h1:ZqeYNhU3OHLH3mGKHDcjJRFFRrJa6eAM5H+CtDdOsPc=
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742 h1:Esafd1046DLDQ0W1YjYsBW+p8U2u7vzgW2SQVmlNazg=
github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0=
github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9Gz0M=
github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk=
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e h1:fD57ERR4JtEqsWbfPhv4DMiApHyliiK5xCTNVSPiaAs=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/rivo/uniseg v0.4.4 h1:8TfxU8dW6PdqD27gjM8MVNuicgxIjxpm4K7x4jp8sis=
github.com/sclevine/spec v1.4.0 h1:z/Q9idDcay5m5irkZ28M7PtQM4aOISzOpj4bUPkDee8=
github.com/spf13/cobra v1.7.0 h1:hyqWnYt1ZQShIddO5kBpj3vu05/++x6tJ6dg8EC572I=
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0 h1:1zr/of2m5FGMsad5YfcqgdqdWrIhu+EBEJRhR1U7z/c=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
@ -116,19 +100,15 @@ github.com/ugorji/go/codec v1.1.7 h1:2SvQaVZ1ouYrrKKwoSk2pzd4A9evlKJb9oTL+OaLUSs
github.com/ugorji/go/codec v1.1.7/go.mod h1:Ax+UKWsSmolVDwsd+7N3ZtXu+yMGCf907BLYF3GoBXY=
github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw=
github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc=
github.com/valyala/fasthttp v1.30.0 h1:nBNzWrgZUUHohyLPU/jTvXdhrcaf2m5k3bWk+3Q049g=
github.com/valyala/fasthttp v1.30.0/go.mod h1:2rsYD01CKFrjjsvFxx75KlEUNpWNBY9JWD3K/7o2Cus=
github.com/valyala/quicktemplate v1.7.0 h1:LUPTJmlVcb46OOUY3IeD9DojFpAVbsG+5WFTcjMJzCM=
github.com/valyala/quicktemplate v1.7.0/go.mod h1:sqKJnoaOF88V07vkO+9FL8fb9uZg/VPSJnLYn+LmLk8=
github.com/valyala/tcplisten v1.0.0 h1:rBHj/Xf+E1tRGZyWIWwJDiRY0zc1Js+CV5DqwacVSA8=
github.com/valyala/tcplisten v1.0.0/go.mod h1:T0xQ8SeCZGxckz9qRXTfG43PvQ/mcWh7FwZEA7Ioqkc=
github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
github.com/yuin/goldmark v1.3.5 h1:dPmz1Snjq0kmkz159iL7S6WzdahUTHnHB5M56WFVifs=
github.com/yuin/goldmark v1.3.5/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k=
go.etcd.io/bbolt v1.3.7 h1:j+zJOnnEjF/kyHlDDgGnVL/AIqIJPq8UoB2GSNfkUfQ=
go.etcd.io/bbolt v1.3.7/go.mod h1:N9Mkw9X8x5fupy0IKsmuqVtoGDyxsaDlbk4Rd05IAQw=
go.etcd.io/gofail v0.1.0 h1:XItAMIhOojXFQMgrxjnd2EIIHun/d5qL0Pf7FzVTkFg=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
@ -137,13 +117,11 @@ golang.org/x/crypto v0.9.0 h1:LF6fAI+IutBocDJ2OT0Q1g8plpYljMZ4+lty+dsqw3g=
golang.org/x/crypto v0.9.0/go.mod h1:yrmDGqONDYtNj3tH8X9dzUun2m2lzPa9ngI6/RUPGR0=
golang.org/x/exp v0.0.0-20230522175609-2e198f4a06a1 h1:k/i9J1pBpvlfR+9QsetwPyERsqu1GIbi967PQMq3Ivc=
golang.org/x/exp v0.0.0-20230522175609-2e198f4a06a1/go.mod h1:V1LtkGg67GoY2N1AnLN78QLrzxkLyJw7RJb1gzOOz9w=
golang.org/x/lint v0.0.0-20210508222113-6edffad5e616 h1:VLliZ0d+/avPrXXH+OakdXhpJuEoBZuwh1m2j7U6Iug=
golang.org/x/lint v0.0.0-20210508222113-6edffad5e616/go.mod h1:3xt1FjdF8hUf6vQPIChWIBhFzV8gjjsPE/fR3IyQdNY=
golang.org/x/mod v0.1.1-0.20191105210325-c90efee705ee/go.mod h1:QqPTAvyqsEbceGzBzNggFXnrqF1CaUcvgkdR5Ot7KZg=
golang.org/x/mod v0.2.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/mod v0.4.2/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/mod v0.8.0 h1:LUYupSeNrTNCGzR/hVBk2NHZO4hXcVaW1k4Qx7rjPx8=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20200226121028-0de0cce0169b/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
@ -151,7 +129,6 @@ golang.org/x/net v0.0.0-20201021035429-f5854403a974/go.mod h1:sp8m0HH+o8qH0wwXwY
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4/go.mod h1:p54w0d4576C0XHj96bSt6lcn1PtDYWL6XObtHCRCNQM=
golang.org/x/net v0.0.0-20210510120150-4163338589ed/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/net v0.10.0 h1:X2//UzNDwYmtCLn7To6G58Wr6f5ahEAQgKNzv9Y951M=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
@ -172,14 +149,12 @@ golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBc
golang.org/x/sys v0.8.0 h1:EBmGv8NaZBZTWvrbjNoL6HVt+IVy3QDQpJs7VRIw3tU=
golang.org/x/sys v0.8.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.8.0 h1:n5xxQn2i3PC0yLAbjTpNT85q/Kgzcr2gIoX9OrJUols=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.9.0 h1:2sjJmO8cDvYveuX97RDLsxlyUxLl+GHoLxBiRdHllBE=
golang.org/x/text v0.9.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8=
golang.org/x/time v0.0.0-20191024005414-555d28b269f0 h1:/5xXl8Y5W96D+TtHSlonuFqGHIWVuyCkGJLwGh9JJFs=
golang.org/x/time v0.0.0-20191024005414-555d28b269f0/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
@ -187,11 +162,9 @@ golang.org/x/tools v0.0.0-20200130002326-2f3ba24bd6e7/go.mod h1:TB2adYChydJhpapK
golang.org/x/tools v0.0.0-20200619180055-7c47624df98f/go.mod h1:EkVYQZoAsY45+roYkvgYkIh4xh/qjgUK9TdY2XT94GE=
golang.org/x/tools v0.1.0/go.mod h1:xkSsbof2nBLbhDlRMhhhyNLN/zl3eTqcnHD5viDpcZ0=
golang.org/x/tools v0.1.5/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk=
golang.org/x/tools v0.6.0 h1:BOw41kyTf3PuCW1pVQf8+Cyg8pMlkYB1oo9iJ6D/lKM=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 h1:go1bK/D/BFZV2I8cIQd1NKEZ+0owSTG1fDTci4IqFcE=
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f h1:BLraFXnmrev5lT+xlilqcH8XK9/i0At2xKjWk4p6zsU=

View file

@ -4,7 +4,6 @@ import (
"context"
"database/sql"
_ "embed"
"encoding/json"
"errors"
"fmt"
"git.gammaspectra.live/P2Pool/p2pool-observer/monero/address"
@ -13,6 +12,7 @@ import (
"git.gammaspectra.live/P2Pool/p2pool-observer/monero/randomx"
"git.gammaspectra.live/P2Pool/p2pool-observer/p2pool/sidechain"
"git.gammaspectra.live/P2Pool/p2pool-observer/types"
"git.gammaspectra.live/P2Pool/p2pool-observer/utils"
"github.com/floatdrop/lru"
"github.com/lib/pq"
_ "github.com/lib/pq"
@ -645,7 +645,7 @@ func (i *Index) InsertOrUpdateMainBlock(b *MainBlock) error {
}
}
metadataJson, _ := json.Marshal(b.Metadata)
metadataJson, _ := utils.MarshalJSON(b.Metadata)
return i.Query(
"INSERT INTO main_blocks (id, height, timestamp, reward, coinbase_id, difficulty, metadata, side_template_id, coinbase_private_key) VALUES ($1, $2, $3, $4, $5, $6, $7::jsonb, $8, $9) ON CONFLICT (id) DO UPDATE SET metadata = $7, side_template_id = $8, coinbase_private_key = $9;",
@ -1021,8 +1021,8 @@ func (i *Index) GetMainLikelySweepTransactionByGlobalOutputIndices(globalOutputI
func (i *Index) InsertOrUpdateMainLikelySweepTransaction(t *MainLikelySweepTransaction) error {
resultJson, _ := json.Marshal(t.Result)
matchJson, _ := json.Marshal(t.Match)
resultJson, _ := utils.MarshalJSON(t.Result)
matchJson, _ := utils.MarshalJSON(t.Match)
spendPub, viewPub := t.Address.SpendPublicKey().AsSlice(), t.Address.ViewPublicKey().AsSlice()
if _, err := i.handle.Exec(

View file

@ -1,20 +1,20 @@
package index
import (
"encoding/json"
"git.gammaspectra.live/P2Pool/p2pool-observer/monero/crypto"
"git.gammaspectra.live/P2Pool/p2pool-observer/types"
"git.gammaspectra.live/P2Pool/p2pool-observer/utils"
)
const MainBlockSelectFields = "id, height, timestamp, reward, coinbase_id, difficulty, metadata, side_template_id, coinbase_private_key"
type MainBlock struct {
Id types.Hash `json:"id"`
Height uint64 `json:"height"`
Timestamp uint64 `json:"timestamp"`
Reward uint64 `json:"reward"`
Height uint64 `json:"height"`
Timestamp uint64 `json:"timestamp"`
Reward uint64 `json:"reward"`
CoinbaseId types.Hash `json:"coinbase_id"`
Difficulty uint64 `json:"difficulty"`
Difficulty uint64 `json:"difficulty"`
// Metadata should be jsonb blob, can be NULL. metadata such as pool ownership, links to other p2pool networks, and other interesting data
Metadata map[string]any `json:"metadata"`
@ -39,7 +39,7 @@ func (b *MainBlock) ScanFromRow(i *Index, row RowScanInterface) error {
b.Metadata = make(map[string]any)
if err := row.Scan(&b.Id, &b.Height, &b.Timestamp, &b.Reward, &b.CoinbaseId, &b.Difficulty, &metadataBuf, &b.SideTemplateId, &b.CoinbasePrivateKey); err != nil {
return err
} else if err = json.Unmarshal(metadataBuf, &b.Metadata); err != nil {
} else if err = utils.UnmarshalJSON(metadataBuf, &b.Metadata); err != nil {
return err
}
return nil

View file

@ -1,11 +1,11 @@
package index
import (
"encoding/json"
"errors"
"git.gammaspectra.live/P2Pool/p2pool-observer/monero/address"
"git.gammaspectra.live/P2Pool/p2pool-observer/monero/crypto"
"git.gammaspectra.live/P2Pool/p2pool-observer/types"
"git.gammaspectra.live/P2Pool/p2pool-observer/utils"
"github.com/lib/pq"
)
@ -41,9 +41,9 @@ func (t *MainLikelySweepTransaction) ScanFromRow(i *Index, row RowScanInterface)
var spendingOutputIndices, globalOutputIndices pq.Int64Array
if err := row.Scan(&t.Id, &t.Timestamp, &resultBuf, &matchBuf, &t.Value, &spendingOutputIndices, &globalOutputIndices, &t.InputCount, &t.InputDecoyCount, &t.MinerCount, &t.OtherMinersCount, &t.NoMinerCount, &t.MinerRatio, &t.OtherMinersRatio, &t.NoMinerRatio, &spendPub, &viewPub); err != nil {
return err
} else if err = json.Unmarshal(resultBuf, &t.Result); err != nil {
} else if err = utils.UnmarshalJSON(resultBuf, &t.Result); err != nil {
return err
} else if err = json.Unmarshal(resultBuf, &t.Match); err != nil {
} else if err = utils.UnmarshalJSON(resultBuf, &t.Match); err != nil {
return err
}
t.SpendingOutputIndices = make([]uint64, len(spendingOutputIndices))

View file

@ -2,7 +2,6 @@ package address
import (
"bytes"
"encoding/json"
"errors"
"git.gammaspectra.live/P2Pool/moneroutil"
"git.gammaspectra.live/P2Pool/p2pool-observer/monero/crypto"
@ -148,7 +147,7 @@ func (a *Address) ToBase58() string {
}
func (a *Address) MarshalJSON() ([]byte, error) {
return json.Marshal(a.ToBase58())
return []byte("\"" + a.ToBase58() + "\""), nil
}
func (a *Address) UnmarshalJSON(b []byte) error {

View file

@ -3,8 +3,8 @@ package zmq
import (
"bytes"
"context"
"encoding/json"
"fmt"
"git.gammaspectra.live/P2Pool/p2pool-observer/utils"
"golang.org/x/exp/slices"
"strings"
@ -163,7 +163,7 @@ func (c *Client) ingestFrameArray(stream *Stream, frame []byte) error {
func (c *Client) transmitFullChainMain(stream *Stream, gson []byte) error {
var arr []*FullChainMain
if err := json.Unmarshal(gson, &arr); err != nil {
if err := utils.UnmarshalJSON(gson, &arr); err != nil {
return fmt.Errorf("unmarshal: %w", err)
}
for _, element := range arr {
@ -176,7 +176,7 @@ func (c *Client) transmitFullChainMain(stream *Stream, gson []byte) error {
func (c *Client) transmitFullTxPoolAdd(stream *Stream, gson []byte) error {
var arr []*FullTxPoolAdd
if err := json.Unmarshal(gson, &arr); err != nil {
if err := utils.UnmarshalJSON(gson, &arr); err != nil {
return fmt.Errorf("unmarshal: %w", err)
}
for _, element := range arr {
@ -189,7 +189,7 @@ func (c *Client) transmitFullTxPoolAdd(stream *Stream, gson []byte) error {
func (c *Client) transmitFullMinerData(stream *Stream, gson []byte) error {
element := &FullMinerData{}
if err := json.Unmarshal(gson, element); err != nil {
if err := utils.UnmarshalJSON(gson, element); err != nil {
return fmt.Errorf("unmarshal: %w", err)
}
stream.FullMinerDataC <- element
@ -199,7 +199,7 @@ func (c *Client) transmitFullMinerData(stream *Stream, gson []byte) error {
func (c *Client) transmitMinimalChainMain(stream *Stream, gson []byte) error {
element := &MinimalChainMain{}
if err := json.Unmarshal(gson, element); err != nil {
if err := utils.UnmarshalJSON(gson, element); err != nil {
return fmt.Errorf("unmarshal: %w", err)
}
stream.MinimalChainMainC <- element
@ -209,7 +209,7 @@ func (c *Client) transmitMinimalChainMain(stream *Stream, gson []byte) error {
func (c *Client) transmitMinimalTxPoolAdd(stream *Stream, gson []byte) error {
var arr []*MinimalTxPoolAdd
if err := json.Unmarshal(gson, &arr); err != nil {
if err := utils.UnmarshalJSON(gson, &arr); err != nil {
return fmt.Errorf("unmarshal: %w", err)
}
for _, element := range arr {

View file

@ -4,9 +4,9 @@ import (
"bytes"
"database/sql/driver"
"encoding/hex"
"encoding/json"
"errors"
"git.gammaspectra.live/P2Pool/edwards25519"
"git.gammaspectra.live/P2Pool/p2pool-observer/utils"
)
type PrivateKey interface {
@ -70,7 +70,7 @@ func (p *PrivateKeyScalar) String() string {
func (p *PrivateKeyScalar) UnmarshalJSON(b []byte) error {
var s string
if err := json.Unmarshal(b, &s); err != nil {
if err := utils.UnmarshalJSON(b, &s); err != nil {
return err
}
@ -90,7 +90,7 @@ func (p *PrivateKeyScalar) UnmarshalJSON(b []byte) error {
}
func (p *PrivateKeyScalar) MarshalJSON() ([]byte, error) {
return json.Marshal(p.String())
return utils.MarshalJSON(p.String())
}
type PrivateKeyBytes [PrivateKeySize]byte
@ -166,7 +166,7 @@ func (k *PrivateKeyBytes) UnmarshalJSON(b []byte) error {
}
func (k *PrivateKeyBytes) MarshalJSON() ([]byte, error) {
return json.Marshal(k.String())
return utils.MarshalJSON(k.String())
}
type PrivateKeySlice []byte
@ -228,7 +228,7 @@ func (k *PrivateKeySlice) Value() (driver.Value, error) {
func (k *PrivateKeySlice) UnmarshalJSON(b []byte) error {
var s string
if err := json.Unmarshal(b, &s); err != nil {
if err := utils.UnmarshalJSON(b, &s); err != nil {
return err
}
@ -245,7 +245,7 @@ func (k *PrivateKeySlice) UnmarshalJSON(b []byte) error {
}
func (k *PrivateKeySlice) MarshalJSON() ([]byte, error) {
return json.Marshal(k.String())
return utils.MarshalJSON(k.String())
}
func deriveKeyExchangeSecretCofactor(private *PrivateKeyScalar, public *PublicKeyPoint) *PublicKeyPoint {

View file

@ -4,9 +4,9 @@ import (
"bytes"
"database/sql/driver"
"encoding/hex"
"encoding/json"
"errors"
"git.gammaspectra.live/P2Pool/edwards25519"
"git.gammaspectra.live/P2Pool/p2pool-observer/utils"
)
type PublicKey interface {
@ -66,7 +66,7 @@ func (k *PublicKeyPoint) String() string {
func (k *PublicKeyPoint) UnmarshalJSON(b []byte) error {
var s string
if err := json.Unmarshal(b, &s); err != nil {
if err := utils.UnmarshalJSON(b, &s); err != nil {
return err
}
@ -86,7 +86,7 @@ func (k *PublicKeyPoint) UnmarshalJSON(b []byte) error {
}
func (k *PublicKeyPoint) MarshalJSON() ([]byte, error) {
return json.Marshal(k.String())
return utils.MarshalJSON(k.String())
}
type PublicKeyBytes [PublicKeySize]byte
@ -149,7 +149,7 @@ func (k *PublicKeyBytes) UnmarshalJSON(b []byte) error {
}
func (k *PublicKeyBytes) MarshalJSON() ([]byte, error) {
return json.Marshal(k.String())
return utils.MarshalJSON(k.String())
}
type PublicKeySlice []byte
@ -198,7 +198,7 @@ func (k *PublicKeySlice) Value() (driver.Value, error) {
func (k *PublicKeySlice) UnmarshalJSON(b []byte) error {
var s string
if err := json.Unmarshal(b, &s); err != nil {
if err := utils.UnmarshalJSON(b, &s); err != nil {
return err
}
@ -215,5 +215,5 @@ func (k *PublicKeySlice) UnmarshalJSON(b []byte) error {
}
func (k *PublicKeySlice) MarshalJSON() ([]byte, error) {
return json.Marshal(k.String())
return utils.MarshalJSON(k.String())
}

View file

@ -2,13 +2,13 @@ package api
import (
"bytes"
"encoding/json"
"errors"
"git.gammaspectra.live/P2Pool/p2pool-observer/monero/block"
"git.gammaspectra.live/P2Pool/p2pool-observer/monero/randomx"
"git.gammaspectra.live/P2Pool/p2pool-observer/p2pool/sidechain"
p2pooltypes "git.gammaspectra.live/P2Pool/p2pool-observer/p2pool/types"
"git.gammaspectra.live/P2Pool/p2pool-observer/types"
"git.gammaspectra.live/P2Pool/p2pool-observer/utils"
"github.com/floatdrop/lru"
"io"
"log"
@ -107,7 +107,7 @@ func (p *P2PoolApi) LightByMainId(id types.Hash) *sidechain.PoolBlock {
} else {
b := &sidechain.PoolBlock{}
if err = json.Unmarshal(buf, &b); err != nil || b.ShareVersion() == sidechain.ShareVersion_None {
if err = utils.UnmarshalJSON(buf, &b); err != nil || b.ShareVersion() == sidechain.ShareVersion_None {
return nil
}
@ -127,7 +127,7 @@ func (p *P2PoolApi) ByMainId(id types.Hash) *sidechain.PoolBlock {
} else {
var result p2pooltypes.P2PoolBinaryBlockResult
if err = json.Unmarshal(buf, &result); err != nil || result.Version == 0 {
if err = utils.UnmarshalJSON(buf, &result); err != nil || result.Version == 0 {
return nil
}
@ -151,7 +151,7 @@ func (p *P2PoolApi) LightByTemplateId(id types.Hash) sidechain.UniquePoolBlockSl
} else {
var result sidechain.UniquePoolBlockSlice
if err = json.Unmarshal(buf, &result); err != nil || len(result) == 0 {
if err = utils.UnmarshalJSON(buf, &result); err != nil || len(result) == 0 {
return nil
}
@ -171,7 +171,7 @@ func (p *P2PoolApi) ByTemplateId(id types.Hash) *sidechain.PoolBlock {
} else {
var result p2pooltypes.P2PoolBinaryBlockResult
if err = json.Unmarshal(buf, &result); err != nil {
if err = utils.UnmarshalJSON(buf, &result); err != nil {
return nil
} else if result.Version == 0 {
// Fallback into archive
@ -185,7 +185,7 @@ func (p *P2PoolApi) ByTemplateId(id types.Hash) *sidechain.PoolBlock {
} else {
var result []p2pooltypes.P2PoolBinaryBlockResult
if err = json.Unmarshal(buf, &result); err != nil || len(result) == 0 {
if err = utils.UnmarshalJSON(buf, &result); err != nil || len(result) == 0 {
return nil
}
@ -225,7 +225,7 @@ func (p *P2PoolApi) LightBySideHeight(height uint64) sidechain.UniquePoolBlockSl
} else {
var result sidechain.UniquePoolBlockSlice
if err = json.Unmarshal(buf, &result); err != nil || len(result) == 0 {
if err = utils.UnmarshalJSON(buf, &result); err != nil || len(result) == 0 {
return nil
}
@ -245,7 +245,7 @@ func (p *P2PoolApi) BySideHeight(height uint64) sidechain.UniquePoolBlockSlice {
} else {
var result []p2pooltypes.P2PoolBinaryBlockResult
if err = json.Unmarshal(buf, &result); err != nil {
if err = utils.UnmarshalJSON(buf, &result); err != nil {
return nil
} else if len(result) == 0 {
// Fallback into archive
@ -259,7 +259,7 @@ func (p *P2PoolApi) BySideHeight(height uint64) sidechain.UniquePoolBlockSlice {
} else {
var result []p2pooltypes.P2PoolBinaryBlockResult
if err = json.Unmarshal(buf, &result); err != nil || len(result) == 0 {
if err = utils.UnmarshalJSON(buf, &result); err != nil || len(result) == 0 {
return nil
}
@ -306,7 +306,7 @@ func (p *P2PoolApi) LightByMainHeight(height uint64) sidechain.UniquePoolBlockSl
} else {
var result sidechain.UniquePoolBlockSlice
if err = json.Unmarshal(buf, &result); err != nil || len(result) == 0 {
if err = utils.UnmarshalJSON(buf, &result); err != nil || len(result) == 0 {
return nil
}
@ -326,7 +326,7 @@ func (p *P2PoolApi) ByMainHeight(height uint64) sidechain.UniquePoolBlockSlice {
} else {
var result []p2pooltypes.P2PoolBinaryBlockResult
if err = json.Unmarshal(buf, &result); err != nil || len(result) == 0 {
if err = utils.UnmarshalJSON(buf, &result); err != nil || len(result) == 0 {
return nil
}
@ -391,7 +391,7 @@ func (p *P2PoolApi) ConnectionCheck(addrPort netip.AddrPort) *p2pooltypes.P2Pool
} else {
var result p2pooltypes.P2PoolConnectionCheckInformation
if err = json.Unmarshal(buf, &result); err != nil {
if err = utils.UnmarshalJSON(buf, &result); err != nil {
return nil
}
@ -411,7 +411,7 @@ func (p *P2PoolApi) MinerData() *p2pooltypes.MinerData {
} else {
var result p2pooltypes.MinerData
if err = json.Unmarshal(buf, &result); err != nil {
if err = utils.UnmarshalJSON(buf, &result); err != nil {
return nil
}
@ -431,7 +431,7 @@ func (p *P2PoolApi) MainTip() *block.Header {
} else {
var result block.Header
if err = json.Unmarshal(buf, &result); err != nil {
if err = utils.UnmarshalJSON(buf, &result); err != nil {
return nil
}
@ -451,7 +451,7 @@ func (p *P2PoolApi) MainHeaderById(id types.Hash) *block.Header {
} else {
var result block.Header
if err = json.Unmarshal(buf, &result); err != nil {
if err = utils.UnmarshalJSON(buf, &result); err != nil {
return nil
}
@ -471,7 +471,7 @@ func (p *P2PoolApi) MainHeaderByHeight(height uint64) *block.Header {
} else {
var result block.Header
if err = json.Unmarshal(buf, &result); err != nil {
if err = utils.UnmarshalJSON(buf, &result); err != nil {
return nil
}
@ -491,7 +491,7 @@ func (p *P2PoolApi) MainDifficultyByHeight(height uint64) types.Difficulty {
} else {
var result types.Difficulty
if err = json.Unmarshal(buf, &result); err != nil {
if err = utils.UnmarshalJSON(buf, &result); err != nil {
return types.ZeroDifficulty
}
@ -511,7 +511,7 @@ func (p *P2PoolApi) StateFromTemplateId(id types.Hash) (chain, uncles sidechain.
} else {
var result p2pooltypes.P2PoolSideChainStateResult
if err = json.Unmarshal(buf, &result); err != nil {
if err = utils.UnmarshalJSON(buf, &result); err != nil {
return nil, nil
}
@ -550,7 +550,7 @@ func (p *P2PoolApi) WindowFromTemplateId(id types.Hash) (chain, uncles sidechain
} else {
var result p2pooltypes.P2PoolSideChainStateResult
if err = json.Unmarshal(buf, &result); err != nil {
if err = utils.UnmarshalJSON(buf, &result); err != nil {
return nil, nil
}
@ -589,7 +589,7 @@ func (p *P2PoolApi) StateFromTip() (chain, uncles sidechain.UniquePoolBlockSlice
} else {
var result p2pooltypes.P2PoolSideChainStateResult
if err = json.Unmarshal(buf, &result); err != nil {
if err = utils.UnmarshalJSON(buf, &result); err != nil {
return nil, nil
}
@ -628,7 +628,7 @@ func (p *P2PoolApi) Tip() *sidechain.PoolBlock {
} else {
var result p2pooltypes.P2PoolBinaryBlockResult
if err = json.Unmarshal(buf, &result); err != nil {
if err = utils.UnmarshalJSON(buf, &result); err != nil {
return nil
}
@ -682,7 +682,7 @@ func (p *P2PoolApi) Status() *p2pooltypes.P2PoolSideChainStatusResult {
} else {
result := &p2pooltypes.P2PoolSideChainStatusResult{}
if err = json.Unmarshal(buf, result); err != nil {
if err = utils.UnmarshalJSON(buf, result); err != nil {
return nil
}

View file

@ -1,7 +1,6 @@
package sidechain
import (
"encoding/json"
"errors"
"fmt"
"git.gammaspectra.live/P2Pool/moneroutil"
@ -9,6 +8,7 @@ import (
"git.gammaspectra.live/P2Pool/p2pool-observer/monero/crypto"
"git.gammaspectra.live/P2Pool/p2pool-observer/monero/randomx"
"git.gammaspectra.live/P2Pool/p2pool-observer/types"
"git.gammaspectra.live/P2Pool/p2pool-observer/utils"
"log"
"strconv"
)
@ -59,12 +59,12 @@ func (n NetworkType) AddressNetwork() (uint8, error) {
}
func (n NetworkType) MarshalJSON() ([]byte, error) {
return json.Marshal(n.String())
return utils.MarshalJSON(n.String())
}
func (n *NetworkType) UnmarshalJSON(b []byte) error {
var s string
if err := json.Unmarshal(b, &s); err != nil {
if err := utils.UnmarshalJSON(b, &s); err != nil {
return err
}
@ -123,7 +123,7 @@ func NewConsensus(networkType NetworkType, poolName, poolPassword string, target
func NewConsensusFromJSON(data []byte) (*Consensus, error) {
var c Consensus
if err := json.Unmarshal(data, &c); err != nil {
if err := utils.UnmarshalJSON(data, &c); err != nil {
return nil, err
}

View file

@ -4,8 +4,8 @@ import (
"bytes"
"database/sql/driver"
"encoding/hex"
"encoding/json"
"errors"
"git.gammaspectra.live/P2Pool/p2pool-observer/utils"
"github.com/holiman/uint256"
"lukechampine.com/uint128"
"math/big"
@ -192,7 +192,7 @@ func (d Difficulty) Big() *big.Int {
}
func (d Difficulty) MarshalJSON() ([]byte, error) {
return json.Marshal(d.String())
return utils.MarshalJSON(d.String())
}
func MustDifficultyFromString(s string) Difficulty {
@ -241,7 +241,7 @@ func DifficultyFrom64(v uint64) Difficulty {
func (d *Difficulty) UnmarshalJSON(b []byte) error {
var s string
if err := json.Unmarshal(b, &s); err != nil {
if err := utils.UnmarshalJSON(b, &s); err != nil {
return err
}

View file

@ -4,8 +4,8 @@ import (
"bytes"
"database/sql/driver"
"encoding/hex"
"encoding/json"
"errors"
"git.gammaspectra.live/P2Pool/p2pool-observer/utils"
"runtime"
"unsafe"
)
@ -17,7 +17,7 @@ type Hash [HashSize]byte
var ZeroHash Hash
func (h Hash) MarshalJSON() ([]byte, error) {
return json.Marshal(h.String())
return utils.MarshalJSON(h.String())
}
func MustHashFromString(s string) Hash {
@ -140,14 +140,14 @@ func (h *Hash) UnmarshalJSON(b []byte) error {
type Bytes []byte
func (b Bytes) MarshalJSON() ([]byte, error) {
return json.Marshal(b.String())
return utils.MarshalJSON(b.String())
}
func (b Bytes) String() string {
return hex.EncodeToString(b)
}
func (b *Bytes) UnmarshalJSON(buf []byte) error {
var s string
if err := json.Unmarshal(buf, &s); err != nil {
if err := utils.UnmarshalJSON(buf, &s); err != nil {
return err
}

33
utils/json.go Normal file
View file

@ -0,0 +1,33 @@
package utils
import (
jsoniter "github.com/json-iterator/go"
"io"
)
var jsonConfig = jsoniter.Config{
EscapeHTML: false,
SortMapKeys: true,
ValidateJsonRawMessage: true,
MarshalFloatWith6Digits: true,
}.Froze()
func MarshalJSON(val any) ([]byte, error) {
return jsonConfig.Marshal(val)
}
func MarshalJSONIndent(val any, indent string) ([]byte, error) {
return jsonConfig.MarshalIndent(val, "", indent)
}
func UnmarshalJSON(data []byte, val any) error {
return jsonConfig.Unmarshal(data, val)
}
func NewJSONEncoder(writer io.Writer) *jsoniter.Encoder {
return jsonConfig.NewEncoder(writer)
}
func NewJSONDecoder(reader io.Reader) *jsoniter.Decoder {
return jsonConfig.NewDecoder(reader)
}