Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions lib/ain-ocean/src/indexer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use dftx_rs::{deserialize, Block, DfTx, Transaction};
use log::debug;

use crate::{
index_transaction,
model::{Block as BlockMapper, BlockContext},
repository::RepositoryOps,
Result, SERVICES,
Expand Down Expand Up @@ -83,6 +84,7 @@ pub fn index_block(encoded_block: String, info: &BlockV2Info) -> Result<()> {
for (idx, tx) in block.txdata.into_iter().enumerate() {
let start = Instant::now();
let bytes = tx.output[0].script_pubkey.as_bytes();
let transaction = tx.clone();
if bytes.len() > 2 && bytes[0] == 0x6a && bytes[1] <= 0x4e {
let offset = 1 + match bytes[1] {
0x4c => 2,
Expand All @@ -105,10 +107,13 @@ pub fn index_block(encoded_block: String, info: &BlockV2Info) -> Result<()> {
DfTx::PoolSwap(data) => data.index(&ctx, tx, idx)?,
DfTx::CompositeSwap(data) => data.index(&ctx, tx, idx)?,
DfTx::PlaceAuctionBid(data) => data.index(&ctx, tx, idx)?,

_ => (),
}
log_elapsed(start, &format!("Indexed tx {:?}", dftx));
}

index_transaction(&ctx, transaction, idx)?;
}

log_elapsed(start, "Indexed block");
Expand Down
79 changes: 60 additions & 19 deletions lib/ain-ocean/src/indexer/transaction.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
use bitcoin::{blockdata::locktime::absolute::LockTime, Txid};
use std::str::FromStr;

use bitcoin::{blockdata::locktime::absolute::LockTime, hashes::Hash, Amount, Txid};
use dftx_rs::Transaction;
use log::debug;

Expand All @@ -16,7 +18,7 @@ use crate::{
pub fn index_transaction(ctx: &BlockContext, tx: Transaction, idx: usize) -> Result<()> {
debug!("[index_transaction] Indexing...");
let tx_id = tx.txid();

let is_evm = check_if_evm_tx(&tx);
let lock_time = match tx.lock_time {
LockTime::Blocks(value) => value.to_consensus_u32(),
LockTime::Seconds(value) => value.to_consensus_u32(),
Expand All @@ -41,51 +43,90 @@ pub fn index_transaction(ctx: &BlockContext, tx: Transaction, idx: usize) -> Res
SERVICES.transaction.by_id.put(&tx_id, &trx)?;
// Indexing transaction vin
for (vin_idx, vin) in tx.input.iter().enumerate() {
if is_evm {
continue;
}
let vout_bytes = vin.previous_output.vout.to_be_bytes();
let trx_vin = TransactionVin {
id: format!("{}-{}", tx_id, vin_idx),
id: (tx_id, vin.previous_output.txid, vout_bytes),
txid: tx_id,
coinbase: vin.script_sig.to_string(),
coinbase: vin.previous_output.to_string(),
vout: TransactionVinVout {
id: format!("{}-{}-vout", tx_id, vin_idx),
id: (tx_id, vin_idx),
txid: tx_id,
n: vin.previous_output.vout as i32,
value: "0".to_string(),
n: vin.sequence.0 as i32,
value: vin.previous_output.vout,
token_id: 0,
script: TransactionVinVoutScript {
hex: vin.script_sig.to_string(),
hex: vin.script_sig.clone(),
},
},
script: TransactionVinScript {
hex: vin.script_sig.to_string(),
hex: vin.script_sig.clone(),
},
tx_in_witness: vec![],
sequence: vin.sequence.to_string(),
sequence: vin.sequence,
};

SERVICES.transaction.vin_by_id.put(&tx_id, &trx_vin)?;
}
// Index transaction vout
for (vout_idx, vout) in tx.output.iter().enumerate() {
let vout_index = vout_idx.to_be_bytes();
let trx_vout = TransactionVout {
id: format!("{}-{}", tx_id, vout_idx),
txid: tx_id.to_string(),
id: (tx_id, vout_idx),
txid: tx_id,
n: vout_idx as i32,
value: vout.value.to_string(),
value: vout.value,
token_id: 0,
script: TransactionVoutScript {
hex: vout.script_pubkey.to_string(),
r#type: "pubkey".to_string(),
hex: vout.script_pubkey.clone(),
r#type: vout.script_pubkey.to_hex_string(),
},
};
SERVICES.transaction.vout_by_id.put(&tx_id, &trx_vout)?;
// .put(&format!("{}-{}", tx_id, vout_idx), &trx_vout)?; //need
SERVICES
.transaction
.vout_by_id
.put(&format!("{}-{}", tx_id, hex::encode(vout_index)), &trx_vout)?;
}

Ok(())
}

pub fn invalidate_transaction(ctx: &BlockContext, tx: Txid, idx: usize) -> Result<()> {
pub fn invalidate_transaction(tx_id: [u8; 32]) -> Result<()> {
debug!("[invalidate_transaction] Invalidating...");
SERVICES.transaction.by_id.delete(&tx)?;
let transaction_id = Txid::from_byte_array(tx_id);
SERVICES.transaction.by_id.delete(&transaction_id)?;
Ok(())
}

//key: txid + vout.txid + (vin.previous_output.vout 4 bytes encoded hex)
pub fn invalidate_transaction_vin(tx_id: String) -> Result<()> {
debug!("[invalidate_transaction_vin] Invalidating...");
SERVICES.transaction.vout_by_id.delete(&tx_id)?;
Ok(())
}

//key: which is string type (txid + encoded (vout_idx)
pub fn invalidate_transaction_vout(tx_id: String) -> Result<()> {
debug!("[invalidate_transaction_vout] Invalidating...");
SERVICES.transaction.vout_by_id.delete(&tx_id)?;
Ok(())
}

fn check_if_evm_tx(txn: &Transaction) -> bool {
txn.input.len() == 2
&& txn.input.iter().all(|vin| {
vin.previous_output.txid
== Txid::from_str(
"0000000000000000000000000000000000000000000000000000000000000000",
)
.unwrap()
})
&& txn.output.len() == 1
&& txn.output[0]
.script_pubkey
.to_asm_string()
.starts_with("OP_RETURN 4466547839")
&& txn.output[0].value == Amount::from_sat(0)
}
6 changes: 4 additions & 2 deletions lib/ain-ocean/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@ pub use api::ocean_router;
use error::OceanError;
pub use indexer::{
index_block, invalidate_block,
transaction::{index_transaction, invalidate_transaction},
transaction::{
index_transaction, invalidate_transaction, invalidate_transaction_vin,
invalidate_transaction_vout,
},
tx_result, BlockV2Info,
};
use model::TransactionVin;
use repository::{
AuctionHistoryByHeightRepository, AuctionHistoryRepository, BlockByHeightRepository,
BlockRepository, MasternodeByHeightRepository, MasternodeRepository, MasternodeStatsRepository,
Expand Down
17 changes: 10 additions & 7 deletions lib/ain-ocean/src/model/transaction_vin.rs
Original file line number Diff line number Diff line change
@@ -1,32 +1,35 @@
use bitcoin::Txid;
use bitcoin::{Amount, ScriptBuf, Sequence, Txid};
use serde::{Deserialize, Serialize};

pub type TransactionVinId = (Txid, Txid, [u8; 4]);
pub type TransactionVinVoutId = (Txid, usize);
#[derive(Debug, Serialize, Deserialize)]
pub struct TransactionVin {
pub id: String,
pub id: TransactionVinId,
pub txid: Txid,
pub coinbase: String,
pub vout: TransactionVinVout,
pub script: TransactionVinScript,
pub tx_in_witness: Vec<String>,
pub sequence: String,
pub sequence: Sequence,
}

#[derive(Debug, Serialize, Deserialize)]
pub struct TransactionVinVout {
pub id: String,
pub id: TransactionVinVoutId,
pub txid: Txid,
pub n: i32,
pub value: String,
pub value: u32,
pub token_id: i32,
pub script: TransactionVinVoutScript,
}

#[derive(Debug, Default, Serialize, Deserialize)]
pub struct TransactionVinScript {
pub hex: String,
pub hex: ScriptBuf,
}

#[derive(Debug, Default, Serialize, Deserialize)]
pub struct TransactionVinVoutScript {
pub hex: String,
pub hex: ScriptBuf,
}
13 changes: 8 additions & 5 deletions lib/ain-ocean/src/model/transaction_vout.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
use bitcoin::{Amount, ScriptBuf, Txid};
use serde::{Deserialize, Serialize};
#[derive(Debug, Default, Serialize, Deserialize)]

pub type TransactionVoutId = (Txid, usize);
#[derive(Debug, Serialize, Deserialize)]
pub struct TransactionVout {
pub id: String,
pub txid: String,
pub id: TransactionVoutId,
pub txid: Txid,
pub n: i32,
pub value: String,
pub value: Amount,
pub token_id: i32,
pub script: TransactionVoutScript,
}

#[derive(Debug, Default, Serialize, Deserialize)]
pub struct TransactionVoutScript {
pub hex: String,
pub hex: ScriptBuf,
pub r#type: String,
}
2 changes: 1 addition & 1 deletion lib/ain-ocean/src/repository/transaction_vout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use crate::{
};

#[derive(Repository)]
#[repository(K = "Txid", V = "TransactionVout")]
#[repository(K = "String", V = "TransactionVout")]
pub struct TransactionVoutRepository {
pub store: Arc<OceanStore>,
col: LedgerColumn<columns::TransactionVout>,
Expand Down
2 changes: 1 addition & 1 deletion lib/ain-ocean/src/storage/columns/transaction_vout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ impl ColumnName for TransactionVout {
}

impl Column for TransactionVout {
type Index = Txid;
type Index = String;
}

impl TypedColumn for TransactionVout {
Expand Down
13 changes: 13 additions & 0 deletions lib/ain-rs-exports/src/ocean.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,16 @@ pub fn ocean_invalidate_block(block: String, b: &BlockV2InfoFFI) -> Result<()> {
fn ocean_try_set_tx_result(tx_type: u8, tx_hash: [u8; 32], result_ptr: usize) -> Result<()> {
ain_ocean::tx_result::index(tx_type, tx_hash, result_ptr)
}

#[ffi_fallible]
pub fn ocean_invalidate_transaction(tx_hash: [u8; 32]) -> Result<()> {
ain_ocean::invalidate_transaction(tx_hash)
}
#[ffi_fallible]
pub fn ocean_invalidate_transaction_vin(tx_hash: String) -> Result<()> {
ain_ocean::invalidate_transaction_vin(tx_hash)
}
#[ffi_fallible]
pub fn ocean_invalidate_transaction_vout(tx_hash: String) -> Result<()> {
ain_ocean::invalidate_transaction_vout(tx_hash)
}
Comment thread
Jouzo marked this conversation as resolved.
Outdated
6 changes: 4 additions & 2 deletions src/dfi/consensus/poolpairs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,8 @@ Res CPoolPairsConsensus::operator()(const CPoolSwapMessage &obj) const {
const auto &tx = txCtx.GetTransaction();
auto &mnview = blockCtx.GetView();

return CPoolSwap(obj, height, std::make_pair(CustomTxType::PoolSwap, tx.GetHash())).ExecuteSwap(mnview, {}, consensus);
return CPoolSwap(obj, height, std::make_pair(CustomTxType::PoolSwap, tx.GetHash()))
.ExecuteSwap(mnview, {}, consensus);
}

Res CPoolPairsConsensus::operator()(const CPoolSwapMessageV2 &obj) const {
Expand All @@ -146,7 +147,8 @@ Res CPoolPairsConsensus::operator()(const CPoolSwapMessageV2 &obj) const {
const auto &tx = txCtx.GetTransaction();
auto &mnview = blockCtx.GetView();

return CPoolSwap(obj.swapInfo, height, std::make_pair(CustomTxType::PoolSwapV2, tx.GetHash())).ExecuteSwap(mnview, obj.poolIDs, consensus);
return CPoolSwap(obj.swapInfo, height, std::make_pair(CustomTxType::PoolSwapV2, tx.GetHash()))
.ExecuteSwap(mnview, obj.poolIDs, consensus);
}

Res CPoolPairsConsensus::operator()(const CLiquidityMessage &obj) const {
Expand Down
4 changes: 2 additions & 2 deletions src/dfi/govvariables/attributes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -774,8 +774,8 @@ static inline void rtrim(std::string &s, unsigned char remove) {
s.erase(std::find_if(s.rbegin(), s.rend(), [&remove](unsigned char ch) { return ch != remove; }).base(), s.end());
}

const std::map<uint8_t, std::map<uint8_t, std::function<ResVal<CAttributeValue>(const std::string &)>>>
&ATTRIBUTES::parseValue() {
const std::map<uint8_t, std::map<uint8_t, std::function<ResVal<CAttributeValue>(const std::string &)>>> &
ATTRIBUTES::parseValue() {
static const std::map<uint8_t, std::map<uint8_t, std::function<ResVal<CAttributeValue>(const std::string &)>>>
parsers{
{AttributeTypes::Token,
Expand Down
4 changes: 2 additions & 2 deletions src/dfi/govvariables/attributes.h
Original file line number Diff line number Diff line change
Expand Up @@ -590,8 +590,8 @@ class ATTRIBUTES : public GovVariable, public AutoRegistrator<GovVariable, ATTRI
static const std::map<std::string, uint8_t> &allowedVaultIDs();
static const std::map<std::string, uint8_t> &allowedRulesIDs();
static const std::map<uint8_t, std::map<std::string, uint8_t>> &allowedKeys();
static const std::map<uint8_t, std::map<uint8_t, std::function<ResVal<CAttributeValue>(const std::string &)>>>
&parseValue();
static const std::map<uint8_t, std::map<uint8_t, std::function<ResVal<CAttributeValue>(const std::string &)>>> &
parseValue();

Res ProcessVariable(const std::string &key,
const std::optional<UniValue> &value,
Expand Down
5 changes: 4 additions & 1 deletion src/dfi/mn_checks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1160,7 +1160,10 @@ Res CPoolSwap::ExecuteSwap(CCustomCSView &view,
if (txInfo) {
const auto &[txType, txHash] = *txInfo;
CrossBoundaryResult ffiResult;
ocean_try_set_tx_result(ffiResult, static_cast<uint8_t>(txType), txHash.GetByteArray(), static_cast<std::size_t>(reinterpret_cast<uintptr_t>(&finalSwapAmount)));
ocean_try_set_tx_result(ffiResult,
static_cast<uint8_t>(txType),
txHash.GetByteArray(),
static_cast<std::size_t>(reinterpret_cast<uintptr_t>(&finalSwapAmount)));
}

return Res::Ok();
Expand Down
4 changes: 3 additions & 1 deletion src/dfi/mn_checks.h
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,9 @@ class CPoolSwap {
public:
std::vector<std::pair<std::string, std::string>> errors;

CPoolSwap(const CPoolSwapMessage &obj, const uint32_t height, const std::optional<std::pair<CustomTxType, uint256>> txInfo = std::nullopt)
CPoolSwap(const CPoolSwapMessage &obj,
const uint32_t height,
const std::optional<std::pair<CustomTxType, uint256>> txInfo = std::nullopt)
: obj(obj),
height(height),
txInfo(txInfo) {}
Expand Down
9 changes: 5 additions & 4 deletions src/dfi/validation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

#include <ain_rs_exports.h>
#include <chain.h>
#include <consensus/validation.h>
#include <dfi/accountshistory.h>
#include <dfi/errors.h>
#include <dfi/govvariables/attributes.h>
Expand All @@ -21,7 +22,6 @@
#include <ffi/ffiexports.h>
#include <ffi/ffihelpers.h>
#include <validation.h>
#include <consensus/validation.h>

#include <boost/asio.hpp>

Expand Down Expand Up @@ -2810,8 +2810,8 @@ Res ProcessDeFiEventFallible(const CBlock &block,
info.size_stripped = GetSerializeSize(block, PROTOCOL_VERSION | SERIALIZE_TRANSACTION_NO_WITNESS);
info.weight = GetBlockWeight(block);
info.stake_modifier = pindex->stakeModifier.ToString();
info.minter = ""; // mn operator address
info.masternode = ""; // mn owner address
info.minter = ""; // mn operator address
info.masternode = ""; // mn owner address

// minter info
CKeyID minter;
Expand All @@ -2822,7 +2822,8 @@ Res ProcessDeFiEventFallible(const CBlock &block,
info.masternode = id->ToString();
auto mn = mnview.GetMasternode(*id);
if (mn) {
auto dest = mn->operatorType == 1 ? CTxDestination(PKHash(minter)) : CTxDestination(WitnessV0KeyHash(minter));
auto dest =
mn->operatorType == 1 ? CTxDestination(PKHash(minter)) : CTxDestination(WitnessV0KeyHash(minter));
info.minter = EncodeDestination(dest);
}
}
Expand Down
Loading