Archived
2
0
This repository has been archived on 2023-03-02. You can view files and clone it, but cannot push or open issues or pull requests.
kerma/models/mempool.go
2023-03-02 15:28:43 +01:00

58 lines
1.1 KiB
Go

// Package models provides the models for the KermaGo application
package models
import (
"errors"
"github.com/docker/go/canonical/json"
"golang.org/x/exp/slices"
)
/*
A Mempool represents a struct that has a type, and a list of tx ids
*/
type Mempool struct {
Type string `json:"type" binding:"required"`
Txids []string `json:"txids" binding:"required"`
}
/*
Construct creates a new Mempool object for this instance
*/
func (m *Mempool) Construct(mempool []string) {
m.Type = "mempool"
m.Txids = slices.Clone(mempool)
}
/*
MarshalJson returns the canonical json of the mempool object
*/
func (m *Mempool) MarshalJson() ([]byte, error) {
return json.MarshalCanonical(m)
}
/*
UnmarshalJSON returns a new Mempool request from a byte array
*/
func (m *Mempool) UnmarshalJSON(data []byte) error {
if len(data) == 0 {
return nil
}
type tmp Mempool
err := json.Unmarshal(data, (*tmp)(m))
if err != nil {
return err
}
if !m.verify() {
return errors.New("mempool request not valid")
}
return nil
}
func (m *Mempool) verify() bool {
return m.Type == "mempool"
}