Skip to content

Commit 3e7d61d

Browse files
committed
asset: update DeepEqual to work with asset v1 (segwit)
The asset version v1 (segwit) allows an asset to be committed without the TxWitness field. Therefore, when comparing two such assets, we must also ignore the TxWitness field. If we don't do this, then the deep equal check when creating a proof for a split asset fails when checking if the split root asset was committed to the root tree.
1 parent 478e2bb commit 3e7d61d

File tree

2 files changed

+36
-6
lines changed

2 files changed

+36
-6
lines changed

asset/asset.go

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -426,8 +426,10 @@ func (w *Witness) Decode(r io.Reader) error {
426426
return stream.Decode(r)
427427
}
428428

429-
// DeepEqual returns true if this witness is equal with the given witness.
430-
func (w *Witness) DeepEqual(o *Witness) bool {
429+
// DeepEqual returns true if this witness is equal with the given witness. If
430+
// the skipTxWitness boolean is set, the TxWitness field of the Witness is not
431+
// compared.
432+
func (w *Witness) DeepEqual(skipTxWitness bool, o *Witness) bool {
431433
if w == nil || o == nil {
432434
return w == o
433435
}
@@ -436,11 +438,17 @@ func (w *Witness) DeepEqual(o *Witness) bool {
436438
return false
437439
}
438440

439-
if !reflect.DeepEqual(w.TxWitness, o.TxWitness) {
441+
if !w.SplitCommitment.DeepEqual(o.SplitCommitment) {
440442
return false
441443
}
442444

443-
return w.SplitCommitment.DeepEqual(o.SplitCommitment)
445+
// If we're not comparing the TxWitness, we're done. This might be
446+
// useful when comparing witnesses of segregated witness version assets.
447+
if skipTxWitness {
448+
return true
449+
}
450+
451+
return reflect.DeepEqual(w.TxWitness, o.TxWitness)
444452
}
445453

446454
// ScriptVersion denotes the asset script versioning scheme.
@@ -1398,6 +1406,20 @@ func (a *Asset) Copy() *Asset {
13981406

13991407
// DeepEqual returns true if this asset is equal with the given asset.
14001408
func (a *Asset) DeepEqual(o *Asset) bool {
1409+
return a.deepEqual(false, o)
1410+
}
1411+
1412+
// DeepEqualAllowSegWitIgnoreTxWitness returns true if this asset is equal with
1413+
// the given asset, ignoring the TxWitness field of the Witness if the asset
1414+
// version is v1.
1415+
func (a *Asset) DeepEqualAllowSegWitIgnoreTxWitness(o *Asset) bool {
1416+
return a.deepEqual(true, o)
1417+
}
1418+
1419+
// deepEqual returns true if this asset is equal with the given asset. The
1420+
// allowSegWitIgnoreTxWitness flag is used to determine whether the TxWitness
1421+
// field of the Witness should be ignored if the asset version is v1.
1422+
func (a *Asset) deepEqual(allowSegWitIgnoreTxWitness bool, o *Asset) bool {
14011423
if a.Version != o.Version {
14021424
return false
14031425
}
@@ -1437,7 +1459,9 @@ func (a *Asset) DeepEqual(o *Asset) bool {
14371459
}
14381460

14391461
for i := range a.PrevWitnesses {
1440-
if !a.PrevWitnesses[i].DeepEqual(&o.PrevWitnesses[i]) {
1462+
oPrevWitness := &o.PrevWitnesses[i]
1463+
skipTxWitness := a.Version == V1 && allowSegWitIgnoreTxWitness
1464+
if !a.PrevWitnesses[i].DeepEqual(skipTxWitness, oPrevWitness) {
14411465
return false
14421466
}
14431467
}

proof/append.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,13 @@ func CreateTransitionProof(prevOut wire.OutPoint,
166166
}
167167

168168
// Make sure the committed asset matches the root asset exactly.
169-
if !committedRoot.DeepEqual(rootAsset) {
169+
// We allow the TxWitness to mismatch for assets with version 1
170+
// as they would not include the witness when the proof is
171+
// created.
172+
if !committedRoot.DeepEqualAllowSegWitIgnoreTxWitness(
173+
rootAsset,
174+
) {
175+
170176
return nil, fmt.Errorf("root asset mismatch")
171177
}
172178

0 commit comments

Comments
 (0)