Created
July 7, 2021 16:55
-
-
Save andrewarrow/5961b99cffc6e299726adf077812b6c8 to your computer and use it in GitHub Desktop.
txindex
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// TXIndexLock protects the transaction index. | |
// The txindex has it s own separate Blockchain object. This allows us to | |
// capture more metadata when collecting transactions without interfering | |
// with the goings-on of the main chain. | |
// Core objects from Server | |
// Core params object | |
// Initialize database | |
// See if we have a best chain hash stored in the txindex db. | |
// If we haven't initialized the txIndexChain before, set up the | |
// seed mappings. | |
// Add the seed balances. Originate them from the architect public key and | |
// set their block as the genesis block. | |
// Just set some dummy metadata | |
// Add the other seed txns to the txn index. | |
// Note that we don't set AffectedPublicKeys for the SeedTxns | |
// Just set some dummy metadata | |
// Ignore all the notifications from the txindex blockchain object | |
// Note that we *DONT* pass server here because it is already tied to the to the main blockchain. | |
// At this point, we should have set up a blockchain object for our | |
// txindex, and initialized all of the seed txns and seed balances | |
// correctly. Attaching blocks to our txnindex blockchain or adding | |
// txns to our txindex should work smoothly now. | |
// Run a loop to continuously update the txindex. Note that this is a noop | |
// except when run the first time or when a new block has arrived. | |
// If the node is fully synced, then try an update. | |
// GetTxindexUpdateBlockNodes ... | |
// Get the current txindex tip. | |
// The tip hash should never be nil since the txindex chain should have | |
// been initialized in the constructor. Print an error and return in this | |
// case. | |
// If the tip of the txindex is no longer stored in the block index, it | |
// means the txindex hit a fork that we are no longer keeping track of. | |
// The only thing we can really do in this case is rebuild the entire index | |
// from scratch. To do that, we return all the blocks in the index to detach | |
// and all the blocks in the real chain to attach. | |
// At this point, we know our txindex tip is in our block index so | |
// there must be a common ancestor between the tip and the block tip. | |
// Update syncs the transaction index with the blockchain. | |
// Specifically, it reads in all the blocks that have come in since the last | |
// time this function was called and adds the new transactions to the txindex. | |
// It also handles reorgs properly. | |
// | |
// TODO(DELETEME, cleanup): This code is error-prone. Moving the transaction indexing code | |
// to block_view.go may be a clean way to refactor this. | |
// If we don't have a chain set, return an error. | |
// Lock the txindex and the blockchain for reading until we're | |
// done with the rest of the function. | |
// Note that the blockchain's ChainLock does not need to be held at this | |
// point because we're just reading blocks from the db, which never get | |
// deleted and therefore don't need the lock in order to access. | |
// If we get to this point, the commonAncestor should never be nil. | |
// If the tip of the txindex is the same as the block tip, don't do | |
// an update. | |
// When the txindex tip does not match the block tip then there's work | |
// to do. Log at the info level. | |
// For each of the blocks we're removing, delete the transactions from | |
// the transaction index. | |
// Go through each txn in the block and delete its mappings from our | |
// txindex. | |
// Iterate through each transaction in the block and delete all its | |
// mappings from the db. Note the txindex has its own db that is | |
// distinct and isolated from our core blockchain db. | |
// Now that all the transactions have been deleted from our txindex, | |
// it's safe to disconnect the block from our txindex chain. | |
// Compute the hashes for all the transactions. | |
// We have to flush a couple of extra things that the view doesn't flush... | |
// Delete this block from the chain db so we don't get duplicate block errors. | |
// Remove this block from our bestChain data structures. | |
// At this point the entries for the block should have been removed | |
// from both our Txindex chain and our transaction index mappings. | |
// For each of the blocks we're adding, process them on our txindex chain | |
// and add their mappings to our txn index. Compute any metadata that might | |
// be useful. | |
// We use a view to simulate adding transactions to our chain. This allows | |
// us to extract custom metadata fields that we can show in our block explorer. | |
// | |
// Only set a BitcoinManager if we have one. This makes some tests pass. | |
// Do each block update in a single transaction so we're safe in case the node | |
// restarts. | |
// Iterate through each transaction in the block and do the following: | |
// - Connect it to the view | |
// - Compute its mapping values, which may include custom metadata fields | |
// - add all its mappings to the db. | |
// Now that we have added all the txns to our TxIndex db, attach the block | |
// to update our chain. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment