2018-07-09 18:56:15 +02:00
|
|
|
package claimnode
|
2018-06-28 07:14:30 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/sha256"
|
|
|
|
"encoding/binary"
|
2018-07-05 22:17:40 +02:00
|
|
|
"math"
|
2018-06-28 07:14:30 +02:00
|
|
|
"strconv"
|
|
|
|
|
|
|
|
"github.com/btcsuite/btcd/chaincfg/chainhash"
|
|
|
|
"github.com/btcsuite/btcd/wire"
|
2018-07-09 18:56:15 +02:00
|
|
|
|
|
|
|
"github.com/lbryio/claimtrie/claim"
|
2018-07-05 22:17:40 +02:00
|
|
|
"github.com/lbryio/claimtrie/memento"
|
2018-06-28 07:14:30 +02:00
|
|
|
)
|
|
|
|
|
2018-07-05 22:17:40 +02:00
|
|
|
// Node ...
|
|
|
|
type Node struct {
|
2018-07-09 20:34:12 +02:00
|
|
|
mem memento.Memento
|
2018-07-09 18:56:15 +02:00
|
|
|
height claim.Height
|
|
|
|
bestClaims map[claim.Height]*claim.Claim
|
2018-07-09 20:34:12 +02:00
|
|
|
|
|
|
|
// To ensure the Claims and Supports are totally ordered, we assign a
|
|
|
|
// strictly increasing seq to each Claim or Support added to the node.
|
|
|
|
seq claim.Seq
|
|
|
|
claims map[wire.OutPoint]*claim.Claim
|
|
|
|
supports map[wire.OutPoint]*claim.Support
|
|
|
|
|
|
|
|
updateNext bool
|
2018-06-28 07:14:30 +02:00
|
|
|
}
|
|
|
|
|
2018-07-09 20:34:12 +02:00
|
|
|
// NewNode returns a new Node.
|
2018-07-05 22:17:40 +02:00
|
|
|
func NewNode() *Node {
|
|
|
|
return &Node{
|
2018-07-09 20:34:12 +02:00
|
|
|
mem: memento.Memento{},
|
2018-07-09 18:56:15 +02:00
|
|
|
bestClaims: map[claim.Height]*claim.Claim{0: nil},
|
|
|
|
claims: map[wire.OutPoint]*claim.Claim{},
|
|
|
|
supports: map[wire.OutPoint]*claim.Support{},
|
2018-06-28 07:14:30 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-09 20:34:12 +02:00
|
|
|
// Height returns the current height.
|
2018-07-09 18:56:15 +02:00
|
|
|
func (n *Node) Height() claim.Height {
|
|
|
|
return n.height
|
|
|
|
}
|
|
|
|
|
2018-07-09 20:34:12 +02:00
|
|
|
// BestClaim returns the best claim at the current height.
|
2018-07-09 18:56:15 +02:00
|
|
|
func (n *Node) BestClaim() *claim.Claim {
|
2018-07-09 20:34:12 +02:00
|
|
|
c, _ := BestClaimAt(n, n.height)
|
|
|
|
return c
|
2018-07-05 22:17:40 +02:00
|
|
|
}
|
|
|
|
|
2018-07-09 20:34:12 +02:00
|
|
|
// Tookover returns the height at which current best claim tookover.
|
2018-07-09 18:56:15 +02:00
|
|
|
func (n *Node) Tookover() claim.Height {
|
2018-07-09 20:34:12 +02:00
|
|
|
_, since := BestClaimAt(n, n.height)
|
|
|
|
return since
|
2018-07-05 22:17:40 +02:00
|
|
|
}
|
|
|
|
|
2018-07-09 20:34:12 +02:00
|
|
|
// AdjustTo increments or decrements current height until it reaches the specific height.
|
|
|
|
func (n *Node) AdjustTo(h claim.Height) error {
|
|
|
|
for n.height < h {
|
2018-07-05 22:17:40 +02:00
|
|
|
n.height++
|
|
|
|
n.processBlock()
|
2018-07-09 20:34:12 +02:00
|
|
|
n.mem.Commit()
|
|
|
|
}
|
|
|
|
for n.height > h {
|
|
|
|
n.height--
|
|
|
|
n.mem.Rollback()
|
2018-06-28 07:14:30 +02:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-07-09 20:34:12 +02:00
|
|
|
// Reset ...
|
|
|
|
func (n *Node) Reset() error {
|
|
|
|
n.mem.RollbackUncommited()
|
2018-07-05 22:17:40 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-07-09 18:56:15 +02:00
|
|
|
// AddClaim ...
|
|
|
|
func (n *Node) AddClaim(op wire.OutPoint, amt claim.Amount) (*claim.Claim, error) {
|
2018-07-09 20:34:12 +02:00
|
|
|
n.seq++
|
2018-07-09 18:56:15 +02:00
|
|
|
c := &claim.Claim{
|
|
|
|
OutPoint: op,
|
|
|
|
ID: claim.NewID(op),
|
|
|
|
Amt: amt,
|
|
|
|
Accepted: n.height + 1,
|
|
|
|
ActiveAt: n.height + 1,
|
2018-07-09 20:34:12 +02:00
|
|
|
Seq: n.seq,
|
2018-07-05 22:17:40 +02:00
|
|
|
}
|
|
|
|
if n.BestClaim() != nil {
|
2018-07-09 18:56:15 +02:00
|
|
|
c.ActiveAt = calActiveHeight(c.Accepted, c.Accepted, n.Tookover())
|
2018-07-05 22:17:40 +02:00
|
|
|
}
|
|
|
|
|
2018-07-09 20:34:12 +02:00
|
|
|
n.mem.Execute(cmdAddClaim{node: n, claim: c})
|
2018-07-05 22:17:40 +02:00
|
|
|
return c, nil
|
|
|
|
}
|
|
|
|
|
2018-07-09 18:56:15 +02:00
|
|
|
// RemoveClaim ...
|
|
|
|
func (n *Node) RemoveClaim(op wire.OutPoint) error {
|
2018-07-05 22:17:40 +02:00
|
|
|
c, ok := n.claims[op]
|
2018-06-28 07:14:30 +02:00
|
|
|
if !ok {
|
|
|
|
return ErrNotFound
|
|
|
|
}
|
2018-07-09 20:34:12 +02:00
|
|
|
n.mem.Execute(cmdRemoveClaim{node: n, claim: c})
|
2018-07-05 22:17:40 +02:00
|
|
|
|
|
|
|
if n.BestClaim() != c {
|
|
|
|
return nil
|
2018-06-28 07:14:30 +02:00
|
|
|
}
|
2018-07-09 20:34:12 +02:00
|
|
|
n.mem.Execute(updateNodeBestClaim{node: n, height: n.Tookover(), old: c, new: nil})
|
2018-07-05 22:17:40 +02:00
|
|
|
n.updateActiveHeights()
|
2018-07-09 20:34:12 +02:00
|
|
|
n.updateNext = true
|
2018-07-05 22:17:40 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-07-09 18:56:15 +02:00
|
|
|
// AddSupport ...
|
|
|
|
func (n *Node) AddSupport(op wire.OutPoint, amt claim.Amount, supported claim.ID) (*claim.Support, error) {
|
2018-07-09 20:34:12 +02:00
|
|
|
n.seq++
|
2018-07-09 18:56:15 +02:00
|
|
|
s := &claim.Support{
|
2018-07-09 20:34:12 +02:00
|
|
|
OutPoint: op,
|
|
|
|
Amt: amt,
|
|
|
|
ClaimID: supported,
|
|
|
|
Accepted: n.height + 1,
|
|
|
|
ActiveAt: n.height + 1,
|
|
|
|
Seq: n.seq,
|
2018-07-05 22:17:40 +02:00
|
|
|
}
|
2018-07-09 18:56:15 +02:00
|
|
|
if n.BestClaim() == nil || n.BestClaim().OutPoint != op {
|
|
|
|
s.ActiveAt = calActiveHeight(s.Accepted, s.Accepted, n.Tookover())
|
2018-07-05 22:17:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, c := range n.claims {
|
2018-07-09 18:56:15 +02:00
|
|
|
if c.ID != supported {
|
2018-07-05 22:17:40 +02:00
|
|
|
continue
|
2018-06-28 07:14:30 +02:00
|
|
|
}
|
2018-07-09 20:34:12 +02:00
|
|
|
n.mem.Execute(cmdAddSupport{node: n, support: s})
|
2018-07-05 22:17:40 +02:00
|
|
|
return s, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Is supporting an non-existing Claim aceepted?
|
|
|
|
return nil, ErrNotFound
|
|
|
|
}
|
|
|
|
|
2018-07-09 18:56:15 +02:00
|
|
|
// RemoveSupport ...
|
|
|
|
func (n *Node) RemoveSupport(op wire.OutPoint) error {
|
2018-07-05 22:17:40 +02:00
|
|
|
s, ok := n.supports[op]
|
|
|
|
if !ok {
|
|
|
|
return ErrNotFound
|
2018-06-28 07:14:30 +02:00
|
|
|
}
|
2018-07-09 20:34:12 +02:00
|
|
|
n.mem.Execute(cmdRemoveSupport{node: n, support: s})
|
2018-06-28 07:14:30 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-07-09 20:34:12 +02:00
|
|
|
// FindNextUpdateHeight returns the smallest height in the future that the the state of the node might change.
|
|
|
|
// If no such height exists, the current height of the node is returned.
|
|
|
|
func (n *Node) FindNextUpdateHeight() claim.Height {
|
|
|
|
if n.updateNext {
|
|
|
|
n.updateNext = false
|
|
|
|
return n.height + 1
|
|
|
|
}
|
|
|
|
|
2018-07-09 18:56:15 +02:00
|
|
|
next := claim.Height(math.MaxInt64)
|
2018-06-28 07:14:30 +02:00
|
|
|
for _, v := range n.claims {
|
2018-07-09 18:56:15 +02:00
|
|
|
if v.ActiveAt > n.height && v.ActiveAt < next {
|
|
|
|
next = v.ActiveAt
|
2018-07-05 22:17:40 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
for _, v := range n.supports {
|
2018-07-09 18:56:15 +02:00
|
|
|
if v.ActiveAt > n.height && v.ActiveAt < next {
|
|
|
|
next = v.ActiveAt
|
2018-07-05 22:17:40 +02:00
|
|
|
}
|
2018-06-28 07:14:30 +02:00
|
|
|
}
|
2018-07-09 18:56:15 +02:00
|
|
|
if next == claim.Height(math.MaxInt64) {
|
2018-07-05 22:17:40 +02:00
|
|
|
return n.height
|
|
|
|
}
|
|
|
|
return next
|
2018-06-28 07:14:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Hash calculates the Hash value based on the OutPoint and at which height it tookover.
|
2018-07-05 22:17:40 +02:00
|
|
|
func (n *Node) Hash() chainhash.Hash {
|
|
|
|
if n.BestClaim() == nil {
|
|
|
|
return chainhash.Hash{}
|
|
|
|
}
|
2018-07-09 18:56:15 +02:00
|
|
|
return calNodeHash(n.BestClaim().OutPoint, n.Tookover())
|
2018-06-28 07:14:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// MarshalJSON customizes JSON marshaling of the Node.
|
2018-07-05 22:17:40 +02:00
|
|
|
func (n *Node) MarshalJSON() ([]byte, error) {
|
2018-07-09 20:34:12 +02:00
|
|
|
return toJSON(n)
|
2018-06-28 07:14:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// String implements Stringer interface.
|
2018-07-05 22:17:40 +02:00
|
|
|
func (n *Node) String() string {
|
2018-07-09 20:34:12 +02:00
|
|
|
return toString(n)
|
2018-06-28 07:14:30 +02:00
|
|
|
}
|
2018-07-09 20:34:12 +02:00
|
|
|
|
2018-07-09 18:56:15 +02:00
|
|
|
func (n *Node) updateEffectiveAmounts() {
|
|
|
|
for _, c := range n.claims {
|
|
|
|
c.EffAmt = c.Amt
|
|
|
|
if c.ActiveAt > n.height {
|
|
|
|
c.EffAmt = 0
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
for _, s := range n.supports {
|
2018-07-09 20:34:12 +02:00
|
|
|
if s.ActiveAt > n.height || s.ClaimID != c.ID {
|
2018-07-09 18:56:15 +02:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
c.EffAmt += s.Amt
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *Node) updateActiveHeights() {
|
|
|
|
for _, v := range n.claims {
|
|
|
|
if old, new := v.ActiveAt, calActiveHeight(v.Accepted, n.height, n.height); old != new {
|
2018-07-09 20:34:12 +02:00
|
|
|
n.mem.Execute(cmdUpdateClaimActiveHeight{claim: v, old: old, new: new})
|
2018-07-09 18:56:15 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
for _, v := range n.supports {
|
|
|
|
if old, new := v.ActiveAt, calActiveHeight(v.Accepted, n.height, n.height); old != new {
|
2018-07-09 20:34:12 +02:00
|
|
|
n.mem.Execute(cmdUpdateSupportActiveHeight{support: v, old: old, new: new})
|
2018-07-09 18:56:15 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-07-09 20:34:12 +02:00
|
|
|
|
2018-07-09 18:56:15 +02:00
|
|
|
func (n *Node) processBlock() {
|
|
|
|
for {
|
2018-07-09 20:34:12 +02:00
|
|
|
n.updateEffectiveAmounts()
|
2018-07-09 18:56:15 +02:00
|
|
|
candidate := findCandiadte(n)
|
|
|
|
if n.BestClaim() == candidate {
|
|
|
|
return
|
|
|
|
}
|
2018-07-09 20:34:12 +02:00
|
|
|
n.mem.Execute(updateNodeBestClaim{node: n, height: n.height, old: n.bestClaims[n.height], new: candidate})
|
2018-07-09 18:56:15 +02:00
|
|
|
n.updateActiveHeights()
|
|
|
|
}
|
|
|
|
}
|
2018-06-28 07:14:30 +02:00
|
|
|
|
2018-07-09 18:56:15 +02:00
|
|
|
func findCandiadte(n *Node) *claim.Claim {
|
|
|
|
var candidate *claim.Claim
|
2018-07-05 22:17:40 +02:00
|
|
|
for _, v := range n.claims {
|
2018-07-09 20:34:12 +02:00
|
|
|
switch {
|
|
|
|
case v.ActiveAt > n.height:
|
2018-07-05 22:17:40 +02:00
|
|
|
continue
|
2018-07-09 20:34:12 +02:00
|
|
|
case candidate == nil:
|
|
|
|
candidate = v
|
|
|
|
case v.EffAmt > candidate.EffAmt:
|
|
|
|
candidate = v
|
|
|
|
case v.EffAmt == candidate.EffAmt && v.Seq < candidate.Seq:
|
2018-07-05 22:17:40 +02:00
|
|
|
candidate = v
|
|
|
|
}
|
2018-06-28 07:14:30 +02:00
|
|
|
}
|
2018-07-05 22:17:40 +02:00
|
|
|
return candidate
|
2018-06-28 07:14:30 +02:00
|
|
|
}
|
|
|
|
|
2018-07-09 20:34:12 +02:00
|
|
|
// BestClaimAt returns the BestClaim at specified Height along with the height when the claim tookover.
|
|
|
|
func BestClaimAt(n *Node, at claim.Height) (best *claim.Claim, since claim.Height) {
|
|
|
|
var latest claim.Height
|
|
|
|
for k := range n.bestClaims {
|
|
|
|
if k > at {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if k > latest {
|
|
|
|
latest = k
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return n.bestClaims[latest], latest
|
|
|
|
}
|
|
|
|
|
|
|
|
// clone copies (deeply) the contents (except memento) of src to dst.
|
|
|
|
func clone(dst, src *Node) {
|
|
|
|
dst.height = src.height
|
|
|
|
for k, v := range src.bestClaims {
|
|
|
|
if v == nil {
|
|
|
|
dst.bestClaims[k] = nil
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
dup := *v
|
|
|
|
dst.bestClaims[k] = &dup
|
|
|
|
}
|
|
|
|
for k, v := range src.claims {
|
|
|
|
dup := *v
|
|
|
|
dst.claims[k] = &dup
|
|
|
|
}
|
|
|
|
for k, v := range src.supports {
|
|
|
|
dup := *v
|
|
|
|
dst.supports[k] = &dup
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-09 18:56:15 +02:00
|
|
|
func calNodeHash(op wire.OutPoint, tookover claim.Height) chainhash.Hash {
|
2018-06-28 07:14:30 +02:00
|
|
|
txHash := chainhash.DoubleHashH(op.Hash[:])
|
|
|
|
|
|
|
|
nOut := []byte(strconv.Itoa(int(op.Index)))
|
|
|
|
nOutHash := chainhash.DoubleHashH(nOut)
|
|
|
|
|
|
|
|
buf := make([]byte, 8)
|
|
|
|
binary.BigEndian.PutUint64(buf, uint64(tookover))
|
|
|
|
heightHash := chainhash.DoubleHashH(buf)
|
|
|
|
|
|
|
|
h := make([]byte, 0, sha256.Size*3)
|
|
|
|
h = append(h, txHash[:]...)
|
|
|
|
h = append(h, nOutHash[:]...)
|
|
|
|
h = append(h, heightHash[:]...)
|
|
|
|
|
|
|
|
return chainhash.DoubleHashH(h)
|
|
|
|
}
|
2018-07-05 22:17:40 +02:00
|
|
|
|
2018-07-09 18:56:15 +02:00
|
|
|
var proportionalDelayFactor = claim.Height(32)
|
2018-07-05 22:17:40 +02:00
|
|
|
|
2018-07-09 18:56:15 +02:00
|
|
|
func calActiveHeight(Accepted, curr, tookover claim.Height) claim.Height {
|
2018-07-05 22:17:40 +02:00
|
|
|
delay := (curr - tookover) / proportionalDelayFactor
|
|
|
|
if delay > 4032 {
|
|
|
|
delay = 4032
|
|
|
|
}
|
2018-07-09 18:56:15 +02:00
|
|
|
return Accepted + delay
|
2018-07-05 22:17:40 +02:00
|
|
|
}
|