|
1 | 1 | package proof |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "bytes" |
| 5 | + "encoding/hex" |
| 6 | + "os" |
4 | 7 | "slices" |
| 8 | + "strings" |
5 | 9 | "testing" |
6 | 10 |
|
7 | 11 | "github.com/btcsuite/btcd/btcec/v2" |
@@ -155,7 +159,7 @@ func makeV0InclusionProof(t *testing.T, opts ...createProofOpt) ([]*Proof, |
155 | 159 |
|
156 | 160 | internalKey := test.RandPubKey(t) |
157 | 161 |
|
158 | | - anchorTx := wire.NewMsgTx(2) |
| 162 | + anchorTx := wire.NewMsgTx(3) |
159 | 163 | anchorTx.TxOut = make([]*wire.TxOut, len(tapCommitments)) |
160 | 164 |
|
161 | 165 | indexes := maps.Keys(tapCommitments) |
@@ -1157,3 +1161,53 @@ func TestVerifyV1ExclusionProof(t *testing.T) { |
1157 | 1161 | }) |
1158 | 1162 | } |
1159 | 1163 | } |
| 1164 | + |
| 1165 | +// TestTransactionVersionInProofSystem verifies that the current codebase |
| 1166 | +// creates v3 transactions. |
| 1167 | +func TestTransactionVersionInProofSystem(t *testing.T) { |
| 1168 | + t.Parallel() |
| 1169 | + |
| 1170 | + proofs, _ := makeV0InclusionProof(t) |
| 1171 | + require.Len(t, proofs, 1) |
| 1172 | + proof := proofs[0] |
| 1173 | + |
| 1174 | + require.Equal(t, int32(3), proof.AnchorTx.Version, |
| 1175 | + "Post-commit b3562461: all new transactions should be v3") |
| 1176 | +} |
| 1177 | + |
| 1178 | +// TestBackwardsCompatibilityWithExistingProofs tests that existing proof files |
| 1179 | +// (which contain v2 transactions) can still be loaded and verified by the |
| 1180 | +// current v3-enabled codebase. |
| 1181 | +func TestBackwardsCompatibilityWithExistingProofs(t *testing.T) { |
| 1182 | + t.Parallel() |
| 1183 | + |
| 1184 | + const fileName = "testdata/proof-file.hex" |
| 1185 | + |
| 1186 | + // Load the existing proof file (created pre-v3). |
| 1187 | + proofHex, err := os.ReadFile(fileName) |
| 1188 | + require.NoError(t, err, "Failed to load %s", fileName) |
| 1189 | + |
| 1190 | + proofBytes, err := hex.DecodeString( |
| 1191 | + strings.TrimSpace(string(proofHex)), |
| 1192 | + ) |
| 1193 | + require.NoError(t, err) |
| 1194 | + |
| 1195 | + // Decode the proof file. |
| 1196 | + var proofFile File |
| 1197 | + err = proofFile.Decode(bytes.NewReader(proofBytes)) |
| 1198 | + require.NoError(t, err, "v3-enabled code must decode existing proofs") |
| 1199 | + |
| 1200 | + // Get the last proof from the file. |
| 1201 | + lastProof, err := proofFile.LastProof() |
| 1202 | + require.NoError(t, err) |
| 1203 | + |
| 1204 | + t.Logf("Proof file %s uses tx version: %d", |
| 1205 | + fileName, lastProof.AnchorTx.Version) |
| 1206 | + |
| 1207 | + require.Equal(t, int32(2), lastProof.AnchorTx.Version) |
| 1208 | + |
| 1209 | + _, err = lastProof.verifyExclusionProofs() |
| 1210 | + require.NoError( |
| 1211 | + t, err, "v3-enabled code must verify existing v2 proofs", |
| 1212 | + ) |
| 1213 | +} |
0 commit comments