github.com/lbryio/lbcd@v0.22.119/claimtrie/node/claim.go (about)

     1  package node
     2  
     3  import (
     4  	"bytes"
     5  	"strconv"
     6  	"strings"
     7  
     8  	"github.com/lbryio/lbcd/chaincfg/chainhash"
     9  	"github.com/lbryio/lbcd/claimtrie/change"
    10  	"github.com/lbryio/lbcd/wire"
    11  )
    12  
    13  type Status int
    14  
    15  const (
    16  	Accepted Status = iota
    17  	Activated
    18  	Deactivated
    19  )
    20  
    21  // Claim defines a structure of stake, which could be a Claim or Support.
    22  type Claim struct {
    23  	OutPoint wire.OutPoint
    24  	ClaimID  change.ClaimID
    25  	Amount   int64
    26  	// CreatedAt  int32 // the very first block, unused at present
    27  	AcceptedAt int32 // the latest update height
    28  	ActiveAt   int32 // AcceptedAt + actual delay
    29  	VisibleAt  int32
    30  
    31  	Status   Status `msgpack:",omitempty"`
    32  	Sequence int32  `msgpack:",omitempty"`
    33  }
    34  
    35  func (c *Claim) setOutPoint(op wire.OutPoint) *Claim {
    36  	c.OutPoint = op
    37  	return c
    38  }
    39  
    40  func (c *Claim) SetAmt(amt int64) *Claim {
    41  	c.Amount = amt
    42  	return c
    43  }
    44  
    45  func (c *Claim) setAccepted(height int32) *Claim {
    46  	c.AcceptedAt = height
    47  	return c
    48  }
    49  
    50  func (c *Claim) setActiveAt(height int32) *Claim {
    51  	c.ActiveAt = height
    52  	return c
    53  }
    54  
    55  func (c *Claim) setStatus(status Status) *Claim {
    56  	c.Status = status
    57  	return c
    58  }
    59  
    60  func OutPointLess(a, b wire.OutPoint) bool {
    61  
    62  	switch cmp := bytes.Compare(a.Hash[:], b.Hash[:]); {
    63  	case cmp < 0:
    64  		return true
    65  	case cmp > 0:
    66  		return false
    67  	default:
    68  		return a.Index < b.Index
    69  	}
    70  }
    71  
    72  func NewOutPointFromString(str string) *wire.OutPoint {
    73  
    74  	f := strings.Split(str, ":")
    75  	if len(f) != 2 {
    76  		return nil
    77  	}
    78  	hash, _ := chainhash.NewHashFromStr(f[0])
    79  	idx, _ := strconv.Atoi(f[1])
    80  
    81  	return wire.NewOutPoint(hash, uint32(idx))
    82  }