Skip to content

Commit

Permalink
added value propagation on supports
Browse files Browse the repository at this point in the history
missed a necessary change
  • Loading branch information
BrannonKing committed Jun 7, 2021
1 parent 7035c7c commit 098633c
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 12 deletions.
2 changes: 1 addition & 1 deletion blockchain/claimtrie.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ func (h *handler) handleTxOuts(ct *claimtrie.ClaimTrie) error {
err = ct.AddClaim(name, *op, amt, value)
case txscript.OP_SUPPORTCLAIM:
copy(id[:], cs.ClaimID())
err = ct.AddSupport(name, *op, amt, id)
err = ct.AddSupport(name, value, *op, amt, id)
case txscript.OP_UPDATECLAIM:
copy(id[:], cs.ClaimID())
if !h.spent[id.String()] {
Expand Down
3 changes: 2 additions & 1 deletion claimtrie/claimtrie.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,14 +182,15 @@ func (ct *ClaimTrie) SpendClaim(name []byte, op wire.OutPoint) error {
}

// AddSupport adds a Support to the ClaimTrie.
func (ct *ClaimTrie) AddSupport(name []byte, op wire.OutPoint, amt int64, id node.ClaimID) error {
func (ct *ClaimTrie) AddSupport(name []byte, value []byte, op wire.OutPoint, amt int64, id node.ClaimID) error {

chg := change.Change{
Type: change.AddSupport,
Name: name,
OutPoint: op.String(),
Amount: amt,
ClaimID: id.String(),
Value: value,
}

return ct.forwardNodeChange(chg)
Expand Down
2 changes: 1 addition & 1 deletion claimtrie/cmd/cmd/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ var chainCmd = &cobra.Command{
op := *node.NewOutPointFromString(chg.OutPoint)
claimID, _ := node.NewIDFromString(chg.ClaimID)
id := claimID
err = ct.AddSupport(chg.Name, op, chg.Amount, id)
err = ct.AddSupport(chg.Name, chg.Value, op, chg.Amount, id)

case change.SpendSupport:
op := *node.NewOutPointFromString(chg.OutPoint)
Expand Down
1 change: 1 addition & 0 deletions claimtrie/node/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ func (n *Node) AppendChange(chg change.Change) error {
Amount: chg.Amount,
ClaimID: chg.ClaimID,
AcceptedAt: chg.Height,
Value: chg.Value,
Status: Added,
}

Expand Down
28 changes: 19 additions & 9 deletions txscript/nameclaim.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ const (

var (
// ErrNotClaimScript is returned when the script does not have a ClaimScript Opcode.
ErrNotClaimScript = fmt.Errorf("not a claim scrpit")
ErrNotClaimScript = fmt.Errorf("not a claim script")

// ErrInvalidClaimScript is returned when a script has a ClaimScript Opcode,
// but does not conform to the format.
ErrInvalidClaimScript = fmt.Errorf("invalid claim scrpit")
ErrInvalidClaimScript = fmt.Errorf("invalid claim script")
)

// ClaimNameScript ...
Expand All @@ -38,9 +38,12 @@ func ClaimNameScript(name string, value string) ([]byte, error) {
}

// SupportClaimScript ...
func SupportClaimScript(name string, claimID []byte) ([]byte, error) {
return NewScriptBuilder().AddOp(OP_SUPPORTCLAIM).AddData([]byte(name)).AddData(claimID).
AddOp(OP_2DROP).AddOp(OP_DROP).AddOp(OP_TRUE).Script()
func SupportClaimScript(name string, claimID []byte, value []byte) ([]byte, error) {
builder := NewScriptBuilder().AddOp(OP_SUPPORTCLAIM).AddData([]byte(name)).AddData(claimID)
if len(value) > 0 {
return builder.addData(value).AddOp(OP_2DROP).AddOp(OP_2DROP).AddOp(OP_TRUE).Script()
}
return builder.AddOp(OP_2DROP).AddOp(OP_DROP).AddOp(OP_TRUE).Script()
}

// UpdateClaimScript ...
Expand Down Expand Up @@ -153,6 +156,7 @@ func ClaimNameSize(script []byte) int {
func CalcMinClaimTrieFee(tx *wire.MsgTx, minFeePerNameClaimChar int64) int64 {
var minFee int64
for _, txOut := range tx.TxOut {
// TODO: lbrycrd ignored transactions that weren't OP_CLAIMNAME
minFee += int64(ClaimNameSize(txOut.PkScript))
}
return minFee * minFeePerNameClaimChar
Expand All @@ -169,14 +173,20 @@ func isClaimName(pops []parsedOpcode) bool {
}

func isSupportClaim(pops []parsedOpcode) bool {
return len(pops) > 5 &&
prefixed := len(pops) > 5 &&
pops[0].opcode.value == OP_SUPPORTCLAIM &&
// canonicalPush(pops[1]) &&
len(pops[1].data) <= MaxClaimNameSize &&
// canonicalPush(pops[2]) &&
len(pops[2].data) == 160/8 &&
pops[3].opcode.value == OP_2DROP &&
pops[4].opcode.value == OP_DROP
len(pops[2].data) == 160/8

if prefixed && pops[3].opcode.value == OP_2DROP && pops[4].opcode.value == OP_DROP {
return true
}
if prefixed && pops[4].opcode.value == OP_2DROP && pops[5].opcode.value == OP_2DROP {
return len(pops[3].data) > 0 // is this robust enough?
}
return false
}

func isUpdateClaim(pops []parsedOpcode) bool {
Expand Down
60 changes: 60 additions & 0 deletions txscript/nameclaim_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package txscript

import (
"github.com/stretchr/testify/assert"
"testing"
)

func TestCreationParseLoopClaim(t *testing.T) {
claim, err := ClaimNameScript("tester", "value")
assert.Nil(t, err)
parsed, err := parseScript(claim)
assert.Nil(t, err)
assert.True(t, isClaimName(parsed))
assert.False(t, isSupportClaim(parsed))
assert.False(t, isUpdateClaim(parsed))
script, err := DecodeClaimScript(claim)
assert.Nil(t, err)
assert.Equal(t, []byte("tester"), script.Name())
assert.Equal(t, []byte("value"), script.Value())
}

func TestCreationParseLoopUpdate(t *testing.T) {
claimID := []byte("12345123451234512345")
claim, err := UpdateClaimScript("tester", claimID, "value")
assert.Nil(t, err)
parsed, err := parseScript(claim)
assert.Nil(t, err)
assert.False(t, isSupportClaim(parsed))
assert.False(t, isClaimName(parsed))
assert.True(t, isUpdateClaim(parsed))
script, err := DecodeClaimScript(claim)

assert.Nil(t, err)
assert.Equal(t, []byte("tester"), script.Name())
assert.Equal(t, claimID, script.ClaimID())
assert.Equal(t, []byte("value"), script.Value())
}

func TestCreationParseLoopSupport(t *testing.T) {
claimID := []byte("12345123451234512345")
claim, err := SupportClaimScript("tester", claimID, []byte("value"))
assert.Nil(t, err)
parsed, err := parseScript(claim)
assert.Nil(t, err)
assert.True(t, isSupportClaim(parsed))
assert.False(t, isClaimName(parsed))
assert.False(t, isUpdateClaim(parsed))
script, err := DecodeClaimScript(claim)

assert.Nil(t, err)
assert.Equal(t, []byte("tester"), script.Name())
assert.Equal(t, claimID, script.ClaimID())
assert.Equal(t, []byte("value"), script.Value())

claim, err = SupportClaimScript("tester", claimID, nil)
script, err = DecodeClaimScript(claim)
assert.Equal(t, []byte("tester"), script.Name())
assert.Equal(t, claimID, script.ClaimID())
assert.Nil(t, script.Value())
}

0 comments on commit 098633c

Please sign in to comment.