-
Notifications
You must be signed in to change notification settings - Fork 132
tapgarden: list batches correctly after asset transfer #992
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
cda2a9e
b06eba1
56169ac
154644f
660940e
4f16d2d
62b431e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -145,6 +145,13 @@ type Archiver interface { | |
// specific fields need to be set in the Locator (e.g. the OutPoint). | ||
FetchProof(ctx context.Context, id Locator) (Blob, error) | ||
|
||
// FetchIssuanceProof fetches the issuance proof for an asset, given the | ||
// anchor point of the issuance (NOT the genesis point for the asset). | ||
// | ||
// If a proof cannot be found, then ErrProofNotFound should be returned. | ||
FetchIssuanceProof(ctx context.Context, id asset.ID, | ||
anchorOutpoint wire.OutPoint) (Blob, error) | ||
|
||
// HasProof returns true if the proof for the given locator exists. This | ||
// is intended to be a performance optimized lookup compared to fetching | ||
// a proof and checking for ErrProofNotFound. | ||
|
@@ -385,6 +392,7 @@ func lookupProofFilePath(rootPath string, loc Locator) (string, error) { | |
assetID := hex.EncodeToString(loc.AssetID[:]) | ||
scriptKey := hex.EncodeToString(loc.ScriptKey.SerializeCompressed()) | ||
|
||
// TODO(jhb): Check for correct file suffix and truncated outpoint? | ||
searchPattern := filepath.Join(rootPath, assetID, scriptKey+"*") | ||
matches, err := filepath.Glob(searchPattern) | ||
if err != nil { | ||
|
@@ -529,6 +537,78 @@ func (f *FileArchiver) FetchProof(_ context.Context, id Locator) (Blob, error) { | |
return proofFile, nil | ||
} | ||
|
||
// FetchIssuanceProof fetches the issuance proof for an asset, given the | ||
// anchor point of the issuance (NOT the genesis point for the asset). | ||
// | ||
// If a proof cannot be found, then ErrProofNotFound should be returned. | ||
// | ||
// NOTE: This implements the Archiver interface. | ||
func (f *FileArchiver) FetchIssuanceProof(ctx context.Context, id asset.ID, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can't this just use the universe archive only? The only thing you need to query for the issuance proof is the asset ID: https://github.com/lightninglabs/taproot-assets/blob/main/universe/interface.go#L329-L336 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Discussed offline, and for maximal backwards compat, the file store may be the best option here as it existed before the universe store did. |
||
anchorOutpoint wire.OutPoint) (Blob, error) { | ||
|
||
// Construct a pattern to search for the issuance proof file. We'll | ||
// leave the script key unspecified, as we don't know what the script | ||
// key was at genesis. | ||
assetID := hex.EncodeToString(id[:]) | ||
scriptKeyGlob := strings.Repeat("?", 2*btcec.PubKeyBytesLenCompressed) | ||
truncatedHash := anchorOutpoint.Hash.String()[:outpointTruncateLength] | ||
|
||
fileName := fmt.Sprintf("%s-%s-%d.%s", | ||
scriptKeyGlob, truncatedHash, anchorOutpoint.Index, | ||
TaprootAssetsFileEnding) | ||
|
||
searchPattern := filepath.Join(f.proofPath, assetID, fileName) | ||
matches, err := filepath.Glob(searchPattern) | ||
if err != nil { | ||
return nil, fmt.Errorf("error listing proof files: %w", err) | ||
} | ||
if len(matches) == 0 { | ||
return nil, ErrProofNotFound | ||
} | ||
|
||
// We expect exactly one matching proof for a specific asset ID and | ||
// outpoint. However, the proof file path uses the truncated outpoint, | ||
// so an asset transfer with a collision in the first half of the TXID | ||
// could also match. We can filter out such proof files by size. | ||
proofFiles := make([]Blob, 0, len(matches)) | ||
for _, path := range matches { | ||
proofFile, err := os.ReadFile(path) | ||
|
||
switch { | ||
case os.IsNotExist(err): | ||
return nil, ErrProofNotFound | ||
|
||
case err != nil: | ||
return nil, fmt.Errorf("unable to find proof: %w", err) | ||
} | ||
|
||
proofFiles = append(proofFiles, proofFile) | ||
} | ||
|
||
switch { | ||
// No proofs were read. | ||
case len(proofFiles) == 0: | ||
return nil, ErrProofNotFound | ||
|
||
// Exactly one proof, we'll return it. | ||
case len(proofFiles) == 1: | ||
return proofFiles[0], nil | ||
|
||
// Multiple proofs, return the smallest one. | ||
default: | ||
minProofIdx := 0 | ||
minProofSize := len(proofFiles[minProofIdx]) | ||
for idx, proof := range proofFiles { | ||
if len(proof) < minProofSize { | ||
minProofSize = len(proof) | ||
minProofIdx = idx | ||
} | ||
} | ||
|
||
return proofFiles[minProofIdx], nil | ||
} | ||
} | ||
|
||
// HasProof returns true if the proof for the given locator exists. This is | ||
// intended to be a performance optimized lookup compared to fetching a proof | ||
// and checking for ErrProofNotFound. | ||
|
@@ -704,10 +784,13 @@ func (f *FileArchiver) RemoveSubscriber( | |
return f.eventDistributor.RemoveSubscriber(subscriber) | ||
} | ||
|
||
// A compile-time interface to ensure FileArchiver meets the NotifyArchiver | ||
// A compile-time assertion to ensure FileArchiver meets the NotifyArchiver | ||
// interface. | ||
var _ NotifyArchiver = (*FileArchiver)(nil) | ||
|
||
// A compile-time assertion to ensure FileArchiver meets the Archiver interface. | ||
var _ Archiver = (*FileArchiver)(nil) | ||
|
||
// MultiArchiver is an archive of archives. It contains several archives and | ||
// attempts to use them either as a look-aside cache, or a write through cache | ||
// for all incoming requests. | ||
|
@@ -763,6 +846,33 @@ func (m *MultiArchiver) FetchProof(ctx context.Context, | |
return nil, ErrProofNotFound | ||
} | ||
|
||
// FetchIssuanceProof fetches the issuance proof for an asset, given the | ||
// anchor point of the issuance (NOT the genesis point for the asset). | ||
func (m *MultiArchiver) FetchIssuanceProof(ctx context.Context, | ||
id asset.ID, anchorOutpoint wire.OutPoint) (Blob, error) { | ||
|
||
// Iterate through all our active backends and try to see if at least | ||
// one of them contains the proof. Either one of them will have the | ||
// proof, or we'll return an error back to the user. | ||
for _, archive := range m.backends { | ||
proof, err := archive.FetchIssuanceProof( | ||
ctx, id, anchorOutpoint, | ||
) | ||
|
||
switch { | ||
case errors.Is(err, ErrProofNotFound): | ||
continue | ||
|
||
case err != nil: | ||
return nil, err | ||
} | ||
|
||
return proof, nil | ||
} | ||
|
||
return nil, ErrProofNotFound | ||
} | ||
|
||
// HasProof returns true if the proof for the given locator exists. This is | ||
// intended to be a performance optimized lookup compared to fetching a proof | ||
// and checking for ErrProofNotFound. The multi archiver only considers a proof | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -412,7 +412,21 @@ func (b *BatchCaretaker) assetCultivator() { | |
} | ||
} | ||
|
||
// extractGenesisOutpoint extracts the genesis point (the first output from the | ||
// extractAnchorOutputIndex extracts the anchor output index from a funded | ||
// genesis packet. | ||
func extractAnchorOutputIndex(genesisPkt *tapsend.FundedPsbt) uint32 { | ||
anchorOutputIndex := uint32(0) | ||
|
||
// TODO(jhb): Does funding guarantee that minting TXs always have | ||
// exactly two outputs? If not this func should be fallible. | ||
if genesisPkt.ChangeOutputIndex == 0 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Using the current |
||
anchorOutputIndex = 1 | ||
} | ||
|
||
return anchorOutputIndex | ||
} | ||
|
||
// extractGenesisOutpoint extracts the genesis point (the first input from the | ||
// genesis transaction). | ||
func extractGenesisOutpoint(tx *wire.MsgTx) wire.OutPoint { | ||
return tx.TxIn[0].PreviousOutPoint | ||
|
@@ -436,7 +450,6 @@ func (b *BatchCaretaker) seedlingsToAssetSprouts(ctx context.Context, | |
b.cfg.Batch.Seedlings, | ||
) | ||
groupedSeedlingCount := len(groupedSeedlings) | ||
|
||
// load seedling asset groups and check for correct group count | ||
seedlingGroups, err := b.cfg.Log.FetchSeedlingGroups( | ||
ctx, genesisPoint, assetOutputIndex, | ||
|
@@ -453,10 +466,9 @@ func (b *BatchCaretaker) seedlingsToAssetSprouts(ctx context.Context, | |
seedlingGroupCount) | ||
} | ||
|
||
for i := range seedlingGroups { | ||
for _, seedlingGroup := range seedlingGroups { | ||
// check that asset group has a witness, and that the group | ||
// has a matching seedling | ||
seedlingGroup := seedlingGroups[i] | ||
if len(seedlingGroup.GroupKey.Witness) == 0 { | ||
return nil, fmt.Errorf("not all seedling groups have " + | ||
"witnesses") | ||
|
@@ -496,12 +508,7 @@ func (b *BatchCaretaker) seedlingsToAssetSprouts(ctx context.Context, | |
// build assets for ungrouped seedlings | ||
for seedlingName := range ungroupedSeedlings { | ||
seedling := ungroupedSeedlings[seedlingName] | ||
assetGen := asset.Genesis{ | ||
FirstPrevOut: genesisPoint, | ||
Tag: seedling.AssetName, | ||
OutputIndex: assetOutputIndex, | ||
Type: seedling.AssetType, | ||
} | ||
assetGen := seedling.Genesis(genesisPoint, assetOutputIndex) | ||
|
||
// If the seedling has a meta data reveal set, then we'll bind | ||
// that by including the hash of the meta data in the asset | ||
|
@@ -607,11 +614,9 @@ func (b *BatchCaretaker) stateStep(currentState BatchState) (BatchState, error) | |
// and vice versa. | ||
// TODO(jhb): return the anchor index instead of change? or both | ||
// so this works for N outputs | ||
b.anchorOutputIndex = 0 | ||
if changeOutputIndex == 0 { | ||
b.anchorOutputIndex = 1 | ||
} | ||
|
||
b.anchorOutputIndex = extractAnchorOutputIndex( | ||
b.cfg.Batch.GenesisPacket, | ||
) | ||
genesisPoint := extractGenesisOutpoint(genesisTxPkt.UnsignedTx) | ||
|
||
// First, we'll turn all the seedlings into actual taproot assets. | ||
|
Uh oh!
There was an error while loading. Please reload this page.