Repository: decred/dcrd
Pull request #3246 (2024-05-06 13:26:40)

wire: add flag bits to PR message
Two new flag fields are introduced to the pair request message, both of which are application specific and have no meaning to the wire package.  The first is a Flags field which will be used to describe capabilities that do not affect the pairing type.  This will be used in the initial release to allow wallets to advertise support for solving and publishing the factored polynomial.  While here, a second flags field which affects the pairing compatibility is also added, as this will aid in any future incompatibility in the client behavior without disturbing clients who have not upgraded.


Patch:
diff --git a/wire/msgmixpairreq.go b/wire/msgmixpairreq.go
index 378d1635..6abd07b0 100644
--- a/wire/msgmixpairreq.go
+++ b/wire/msgmixpairreq.go
@@ -68,6 +68,8 @@ type MsgMixPairReq struct {
 	InputValue   int64
 	UTXOs        []MixPairReqUTXO
 	Change       *TxOut
+	Flags        byte
+	PairingFlags byte
 
 	// hash records the hash of the message.  It is a member of the
 	// message for convenience and performance, but is never automatically
@@ -83,7 +85,8 @@ func (msg *MsgMixPairReq) Pairing() ([]byte, error) {
 		VarIntSerializeSize(uint64(len(msg.ScriptClass))) + // Script class
 		len(msg.ScriptClass) +
 		2 + // Tx version
-		4 // Locktime
+		4 + // Locktime
+		1 // Pairing flags
 	w := bytes.NewBuffer(make([]byte, 0, bufLen))
 
 	err := writeElement(w, msg.MixAmount)
@@ -96,7 +99,7 @@ func (msg *MsgMixPairReq) Pairing() ([]byte, error) {
 		return nil, err
 	}
 
-	err = writeElements(w, msg.TxVersion, msg.LockTime)
+	err = writeElements(w, msg.TxVersion, msg.LockTime, msg.PairingFlags)
 	if err != nil {
 		return nil, err
 	}
@@ -209,7 +212,7 @@ func (msg *MsgMixPairReq) BtcDecode(r io.Reader, pver uint32) error {
 		return messageError(op, ErrInvalidMsg, msg)
 	}
 
-	return nil
+	return readElements(r, &msg.Flags, &msg.PairingFlags)
 }
 
 // BtcEncode encodes the receiver to w using the Decred protocol encoding.
@@ -370,6 +373,11 @@ func (msg *MsgMixPairReq) writeMessageNoSignature(op string, w io.Writer, pver u
 		}
 	}
 
+	err = writeElements(w, msg.Flags, msg.PairingFlags)
+	if err != nil {
+		return err
+	}
+
 	return nil
 }
 
@@ -433,7 +441,8 @@ func (msg *MsgMixPairReq) GetRun() uint32 {
 // remaining fields.
 func NewMsgMixPairReq(identity [33]byte, expiry uint32, mixAmount int64,
 	scriptClass string, txVersion uint16, lockTime, messageCount uint32,
-	inputValue int64, utxos []MixPairReqUTXO, change *TxOut) (*MsgMixPairReq, error) {
+	inputValue int64, utxos []MixPairReqUTXO, change *TxOut,
+	flags, pairingFlags byte) (*MsgMixPairReq, error) {
 
 	const op = "NewMsgMixPairReq"
 	lenScriptClass := len(scriptClass)
@@ -466,6 +475,8 @@ func NewMsgMixPairReq(identity [33]byte, expiry uint32, mixAmount int64,
 		InputValue:   inputValue,
 		UTXOs:        utxos,
 		Change:       change,
+		Flags:        flags,
+		PairingFlags: pairingFlags,
 	}
 	return msg, nil
 }


Test patch:
diff --git a/wire/message_test.go b/wire/message_test.go
index bcf43112..1a56a5ee 100644
--- a/wire/message_test.go
+++ b/wire/message_test.go
@@ -80,7 +80,7 @@ func TestMessage(t *testing.T) {
 	msgReject := NewMsgReject("block", RejectDuplicate, "duplicate block")
 	msgGetInitState := NewMsgGetInitState()
 	msgInitState := NewMsgInitState()
-	msgMixPR, err := NewMsgMixPairReq([33]byte{}, 1, 1, "", 1, 1, 1, 1, []MixPairReqUTXO{}, NewTxOut(0, []byte{}))
+	msgMixPR, err := NewMsgMixPairReq([33]byte{}, 1, 1, "", 1, 1, 1, 1, []MixPairReqUTXO{}, NewTxOut(0, []byte{}), 1, 1)
 	if err != nil {
 		t.Errorf("NewMsgMixPairReq: %v", err)
 	}
@@ -122,7 +122,7 @@ func TestMessage(t *testing.T) {
 		{msgCFTypes, msgCFTypes, pver, MainNet, 26},
 		{msgGetInitState, msgGetInitState, pver, MainNet, 25},
 		{msgInitState, msgInitState, pver, MainNet, 27},
-		{msgMixPR, msgMixPR, pver, MainNet, 165},
+		{msgMixPR, msgMixPR, pver, MainNet, 167},
 		{msgMixKE, msgMixKE, pver, MainNet, 1449},
 		{msgMixCT, msgMixCT, pver, MainNet, 158},
 		{msgMixSR, msgMixSR, pver, MainNet, 161},
diff --git a/wire/msgmixpairreq_test.go b/wire/msgmixpairreq_test.go
index 3396ae4b..8c541fdf 100644
--- a/wire/msgmixpairreq_test.go
+++ b/wire/msgmixpairreq_test.go
@@ -25,11 +25,14 @@ type mixPairReqArgs struct {
 	inputValue             int64
 	utxos                  []MixPairReqUTXO
 	change                 *TxOut
+	flags                  byte
+	pairingFlags           byte
 }
 
 func (a *mixPairReqArgs) msg() (*MsgMixPairReq, error) {
 	return NewMsgMixPairReq(a.identity, a.expiry, a.mixAmount, a.scriptClass,
-		a.txVersion, a.lockTime, a.messageCount, a.inputValue, a.utxos, a.change)
+		a.txVersion, a.lockTime, a.messageCount, a.inputValue, a.utxos,
+		a.change, a.flags, a.pairingFlags)
 }
 
 func newMixPairReqArgs() *mixPairReqArgs {
@@ -74,6 +77,8 @@ func newMixPairReqArgs() *mixPairReqArgs {
 	const changeValue = int64(0x1393939393939393)
 	pkScript := repeat(0x94, 25)
 	change := NewTxOut(changeValue, pkScript)
+	flags := byte(0x95)
+	pairingFlags := byte(0x96)
 
 	return &mixPairReqArgs{
 		identity:     id,
@@ -87,6 +92,8 @@ func newMixPairReqArgs() *mixPairReqArgs {
 		inputValue:   inputValue,
 		utxos:        utxos,
 		change:       change,
+		flags:        flags,
+		pairingFlags: pairingFlags,
 	}
 }
 
@@ -154,6 +161,8 @@ func TestMsgMixPairReqWire(t *testing.T) {
 		0x94, 0x94, 0x94, 0x94, 0x94, 0x94, 0x94, 0x94,
 		0x94,
 	}...)
+	expected = append(expected, 0x95) // Flags
+	expected = append(expected, 0x96) // Pairing flags
 
 	expectedSerializationEqual(t, buf.Bytes(), expected)