Skip to content

Commit

Permalink
Merge pull request #1701 from mintlayer/fix/api-server-address-tracking
Browse files Browse the repository at this point in the history
Fix/api server address tracking
  • Loading branch information
TheQuantumPhysicist authored Mar 29, 2024
2 parents d7c150d + 9b9c5e5 commit 2c0e8bb
Show file tree
Hide file tree
Showing 3 changed files with 256 additions and 169 deletions.
2 changes: 1 addition & 1 deletion api-server/api-server-common/src/storage/impls/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

pub const CURRENT_STORAGE_VERSION: u32 = 8;
pub const CURRENT_STORAGE_VERSION: u32 = 10;

pub mod in_memory;
pub mod postgres;
288 changes: 121 additions & 167 deletions api-server/scanner-lib/src/blockchain_state/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -421,59 +421,43 @@ async fn update_tables_from_block_reward<T: ApiServerStorageWrite>(
.await;
}
TxOutput::Transfer(output_value, destination)
| TxOutput::LockThenTransfer(output_value, destination, _) => match destination {
Destination::PublicKey(_) | Destination::PublicKeyHash(_) => {
let address = Address::<Destination>::new(&chain_config, destination.clone())
.expect("Unable to encode destination");
match output_value {
OutputValue::TokenV0(_) => {}
OutputValue::TokenV1(token_id, amount) => {
increase_address_amount(
db_tx,
&address,
amount,
CoinOrTokenId::TokenId(*token_id),
block_height,
)
.await;
}
OutputValue::Coin(amount) => {
increase_address_amount(
db_tx,
&address,
amount,
CoinOrTokenId::Coin,
block_height,
)
.await;
}
| TxOutput::LockThenTransfer(output_value, destination, _) => {
let address = Address::<Destination>::new(&chain_config, destination.clone())
.expect("Unable to encode destination");
match output_value {
OutputValue::TokenV0(_) => {}
OutputValue::TokenV1(token_id, amount) => {
increase_address_amount(
db_tx,
&address,
amount,
CoinOrTokenId::TokenId(*token_id),
block_height,
)
.await;
}
OutputValue::Coin(amount) => {
increase_address_amount(
db_tx,
&address,
amount,
CoinOrTokenId::Coin,
block_height,
)
.await;
}
set_utxo(
OutPointSourceId::BlockReward(block_id),
idx,
output,
db_tx,
block_height,
false,
&chain_config,
)
.await;
}
Destination::AnyoneCanSpend => {
// for tests
set_utxo(
OutPointSourceId::BlockReward(block_id),
idx,
output,
db_tx,
block_height,
false,
&chain_config,
)
.await;
}
Destination::ClassicMultisig(_) | Destination::ScriptHash(_) => {}
},
set_utxo(
OutPointSourceId::BlockReward(block_id),
idx,
output,
db_tx,
block_height,
false,
&chain_config,
)
.await;
}
}
}

Expand Down Expand Up @@ -969,70 +953,58 @@ async fn update_tables_from_transaction_inputs<T: ApiServerStorageWrite>(
| TxOutput::IssueFungibleToken(_)
| TxOutput::CreateStakePool(_, _)
| TxOutput::ProduceBlockFromStake(_, _) => {}
TxOutput::IssueNft(token_id, _, destination) => match destination {
Destination::AnyoneCanSpend
| Destination::ClassicMultisig(_)
| Destination::ScriptHash(_) => {}
Destination::PublicKey(_) | Destination::PublicKeyHash(_) => {
let address =
Address::<Destination>::new(&chain_config, destination)
.expect("Unable to encode destination");

address_transactions
.entry(address.clone())
.or_default()
.insert(tx.get_id());

decrease_address_amount(
db_tx,
address,
&Amount::from_atoms(1),
CoinOrTokenId::TokenId(token_id),
block_height,
)
.await;
}
},
TxOutput::IssueNft(token_id, _, destination) => {
let address = Address::<Destination>::new(&chain_config, destination)
.expect("Unable to encode destination");

address_transactions
.entry(address.clone())
.or_default()
.insert(tx.get_id());

decrease_address_amount(
db_tx,
address,
&Amount::from_atoms(1),
CoinOrTokenId::TokenId(token_id),
block_height,
)
.await;
}
TxOutput::LockThenTransfer(output_value, destination, _)
| TxOutput::Transfer(output_value, destination) => match destination {
Destination::AnyoneCanSpend
| Destination::ClassicMultisig(_)
| Destination::ScriptHash(_) => {}
Destination::PublicKey(_) | Destination::PublicKeyHash(_) => {
let address =
Address::<Destination>::new(&chain_config, destination)
.expect("Unable to encode destination");

address_transactions
.entry(address.clone())
.or_default()
.insert(tx.get_id());

match output_value {
OutputValue::TokenV0(_) => {}
OutputValue::TokenV1(token_id, amount) => {
decrease_address_amount(
db_tx,
address,
&amount,
CoinOrTokenId::TokenId(token_id),
block_height,
)
.await;
}
OutputValue::Coin(amount) => {
decrease_address_amount(
db_tx,
address,
&amount,
CoinOrTokenId::Coin,
block_height,
)
.await;
}
| TxOutput::Transfer(output_value, destination) => {
let address = Address::<Destination>::new(&chain_config, destination)
.expect("Unable to encode destination");

address_transactions
.entry(address.clone())
.or_default()
.insert(tx.get_id());

match output_value {
OutputValue::TokenV0(_) => {}
OutputValue::TokenV1(token_id, amount) => {
decrease_address_amount(
db_tx,
address,
&amount,
CoinOrTokenId::TokenId(token_id),
block_height,
)
.await;
}
OutputValue::Coin(amount) => {
decrease_address_amount(
db_tx,
address,
&amount,
CoinOrTokenId::Coin,
block_height,
)
.await;
}
}
},
}
}
}
},
Expand Down Expand Up @@ -1173,64 +1145,46 @@ async fn update_tables_from_transaction_outputs<T: ApiServerStorageWrite>(
.expect("Unable to encode address");
address_transactions.entry(address.clone()).or_default().insert(transaction_id);
}
TxOutput::Transfer(output_value, destination) => match destination {
Destination::PublicKey(_) | Destination::PublicKeyHash(_) => {
let address = Address::<Destination>::new(&chain_config, destination.clone())
.expect("Unable to encode destination");
TxOutput::Transfer(output_value, destination) => {
let address = Address::<Destination>::new(&chain_config, destination.clone())
.expect("Unable to encode destination");

address_transactions.entry(address.clone()).or_default().insert(transaction_id);
address_transactions.entry(address.clone()).or_default().insert(transaction_id);

let token_decimals = match output_value {
OutputValue::TokenV0(_) => None,
OutputValue::TokenV1(token_id, amount) => {
increase_address_amount(
db_tx,
&address,
amount,
CoinOrTokenId::TokenId(*token_id),
block_height,
)
.await;
Some(token_decimals(*token_id, &BTreeMap::new(), db_tx).await?.1)
}
OutputValue::Coin(amount) => {
increase_address_amount(
db_tx,
&address,
amount,
CoinOrTokenId::Coin,
block_height,
)
.await;
None
}
};
let token_decimals = match output_value {
OutputValue::TokenV0(_) => None,
OutputValue::TokenV1(token_id, amount) => {
increase_address_amount(
db_tx,
&address,
amount,
CoinOrTokenId::TokenId(*token_id),
block_height,
)
.await;
Some(token_decimals(*token_id, &BTreeMap::new(), db_tx).await?.1)
}
OutputValue::Coin(amount) => {
increase_address_amount(
db_tx,
&address,
amount,
CoinOrTokenId::Coin,
block_height,
)
.await;
None
}
};

let outpoint = UtxoOutPoint::new(
OutPointSourceId::Transaction(transaction_id),
idx as u32,
);
let utxo = Utxo::new(output.clone(), token_decimals, false);
db_tx
.set_utxo_at_height(outpoint, utxo, address.as_str(), block_height)
.await
.expect("Unable to set utxo");
}
Destination::AnyoneCanSpend => {
// for tests
set_utxo(
OutPointSourceId::Transaction(transaction_id),
idx,
output,
db_tx,
block_height,
false,
&chain_config,
)
.await;
}
Destination::ClassicMultisig(_) | Destination::ScriptHash(_) => {}
},
let outpoint =
UtxoOutPoint::new(OutPointSourceId::Transaction(transaction_id), idx as u32);
let utxo = Utxo::new(output.clone(), token_decimals, false);
db_tx
.set_utxo_at_height(outpoint, utxo, address.as_str(), block_height)
.await
.expect("Unable to set utxo");
}
TxOutput::LockThenTransfer(output_value, destination, lock) => {
let address = Address::<Destination>::new(&chain_config, destination.clone())
.expect("Unable to encode destination");
Expand Down
Loading

0 comments on commit 2c0e8bb

Please sign in to comment.