Skip to content
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

feat: Versionable CompressedCoin #1628

Merged
merged 7 commits into from
Jan 26, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
Versionable CompressedCoin
  • Loading branch information
bvrooman committed Jan 25, 2024
commit 27026d2833251562835c57f365404b0929fd7705
12 changes: 5 additions & 7 deletions crates/chain-config/src/config/coin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 58,11 @@ pub struct CoinConfig {

impl GenesisCommitment for CompressedCoin {
fn root(&self) -> anyhow::Result<MerkleRoot> {
let Self {
owner,
amount,
asset_id,
maturity,
tx_pointer,
} = self;
let owner = self.owner();
let amount = self.amount();
let asset_id = self.asset_id();
let maturity = self.maturity();
let tx_pointer = self.tx_pointer();

let coin_hash = *Hasher::default()
.chain(owner)
Expand Down
11 changes: 4 additions & 7 deletions crates/fuel-core/src/coins_query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -950,13 950,10 @@ mod tests {
self.last_coin_index = 1;

let id = UtxoId::new(Bytes32::from([0u8; 32]), index.try_into().unwrap());
let coin = CompressedCoin {
owner,
amount,
asset_id,
maturity: Default::default(),
tx_pointer: Default::default(),
};
let mut coin = CompressedCoin::default();
coin.set_owner(owner);
coin.set_amount(amount);
coin.set_asset_id(asset_id);

let db = &mut self.database;
StorageMutate::<Coins>::insert(db, &id, &coin).unwrap();
Expand Down
16 changes: 8 additions & 8 deletions crates/fuel-core/src/database/coin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 78,7 @@ impl StorageMutate<Coins> for Database {
key: &UtxoId,
value: &CompressedCoin,
) -> Result<Option<CompressedCoin>, Self::Error> {
let coin_by_owner = owner_coin_id_key(&value.owner, key);
let coin_by_owner = owner_coin_id_key(value.owner(), key);
// insert primary record
let insert = self.data.storage_as_mut::<Coins>().insert(key, value)?;
// insert secondary index by owner
Expand All @@ -92,7 92,7 @@ impl StorageMutate<Coins> for Database {

// cleanup secondary index
if let Some(coin) = &coin {
let key = owner_coin_id_key(&coin.owner, key);
let key = owner_coin_id_key(coin.owner(), key);
self.storage_as_mut::<OwnedCoins>().remove(&key)?;
}

Expand Down Expand Up @@ -142,12 142,12 @@ impl Database {
Ok(CoinConfig {
tx_id: Some(*utxo_id.tx_id()),
output_index: Some(utxo_id.output_index()),
tx_pointer_block_height: Some(coin.tx_pointer.block_height()),
tx_pointer_tx_idx: Some(coin.tx_pointer.tx_index()),
maturity: Some(coin.maturity),
owner: coin.owner,
amount: coin.amount,
asset_id: coin.asset_id,
tx_pointer_block_height: Some(coin.tx_pointer().block_height()),
tx_pointer_tx_idx: Some(coin.tx_pointer().tx_index()),
maturity: Some(*coin.maturity()),
owner: *coin.owner(),
amount: *coin.amount(),
asset_id: *coin.asset_id(),
})
})
.collect::<StorageResult<Vec<CoinConfig>>>()?;
Expand Down
117 changes: 40 additions & 77 deletions crates/fuel-core/src/executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1248,32 1248,20 @@ mod tests {
.clone();

let first_input = tx2.inputs()[0].clone();
let mut first_coin = CompressedCoin::default();
first_coin.set_owner(*first_input.input_owner().unwrap());
first_coin.set_amount(100);
let second_input = tx2.inputs()[1].clone();
let mut second_coin = CompressedCoin::default();
second_coin.set_owner(*second_input.input_owner().unwrap());
second_coin.set_amount(100);
let db = &mut Database::default();
// Insert both inputs
db.storage::<Coins>()
.insert(
&first_input.utxo_id().unwrap().clone(),
&CompressedCoin {
owner: *first_input.input_owner().unwrap(),
amount: 100,
asset_id: AssetId::default(),
maturity: Default::default(),
tx_pointer: Default::default(),
},
)
.insert(&first_input.utxo_id().unwrap().clone(), &first_coin)
.unwrap();
db.storage::<Coins>()
.insert(
&second_input.utxo_id().unwrap().clone(),
&CompressedCoin {
owner: *second_input.input_owner().unwrap(),
amount: 100,
asset_id: AssetId::default(),
maturity: Default::default(),
tx_pointer: Default::default(),
},
)
.insert(&second_input.utxo_id().unwrap().clone(), &second_coin)
.unwrap();
let executor = create_executor(
db.clone(),
Expand Down Expand Up @@ -1342,20 1330,14 @@ mod tests {
.clone();

let input = tx.inputs()[0].clone();
let mut coin = CompressedCoin::default();
coin.set_owner(*input.input_owner().unwrap());
coin.set_amount(AMOUNT - 1);
let db = &mut Database::default();

// Inserting a coin with `AMOUNT - 1` should cause a mismatching error during production.
db.storage::<Coins>()
.insert(
&input.utxo_id().unwrap().clone(),
&CompressedCoin {
owner: *input.input_owner().unwrap(),
amount: AMOUNT - 1,
asset_id: AssetId::default(),
maturity: Default::default(),
tx_pointer: Default::default(),
},
)
.insert(&input.utxo_id().unwrap().clone(), &coin)
.unwrap();
let executor = create_executor(
db.clone(),
Expand Down Expand Up @@ -1405,19 1387,13 @@ mod tests {
.clone();

let input = tx.inputs()[1].clone();
let mut coin = CompressedCoin::default();
coin.set_owner(*input.input_owner().unwrap());
coin.set_amount(100);
let db = &mut Database::default();

db.storage::<Coins>()
.insert(
&input.utxo_id().unwrap().clone(),
&CompressedCoin {
owner: *input.input_owner().unwrap(),
amount: 100,
asset_id: AssetId::default(),
maturity: Default::default(),
tx_pointer: Default::default(),
},
)
.insert(&input.utxo_id().unwrap().clone(), &coin)
.unwrap();
let executor = create_executor(
db.clone(),
Expand Down Expand Up @@ -1979,18 1955,12 @@ mod tests {
..
}) = tx.inputs()[0]
{
db.storage::<Coins>()
.insert(
&utxo_id,
&CompressedCoin {
owner,
amount,
asset_id,
maturity: Default::default(),
tx_pointer: TxPointer::new(starting_block, starting_block_tx_idx),
},
)
.unwrap();
let mut coin = CompressedCoin::default();
coin.set_owner(owner);
coin.set_amount(amount);
coin.set_asset_id(asset_id);
coin.set_tx_pointer(TxPointer::new(starting_block, starting_block_tx_idx));
db.storage::<Coins>().insert(&utxo_id, &coin).unwrap();
}

let executor = create_executor(
Expand Down Expand Up @@ -2219,7 2189,7 @@ mod tests {
let maybe_utxo = database.storage::<Coins>().get(&id).unwrap();
assert!(maybe_utxo.is_some());
let utxo = maybe_utxo.unwrap();
assert!(utxo.amount > 0)
assert!(*utxo.amount() > 0)
}
_ => (),
}
Expand Down Expand Up @@ -2397,10 2367,10 @@ mod tests {
.message_is_spent(&message_data.nonce)
.unwrap());
assert_eq!(
block_db_transaction
*block_db_transaction
.coin(&UtxoId::new(tx_id, 0))
.unwrap()
.amount,
.amount(),
amount amount
);
}
Expand Down Expand Up @@ -2460,10 2430,10 @@ mod tests {
.message_is_spent(&message_data.nonce)
.unwrap());
assert_eq!(
block_db_transaction
*block_db_transaction
.coin(&UtxoId::new(tx_id, 0))
.unwrap()
.amount,
.amount(),
amount
);
}
Expand Down Expand Up @@ -2727,18 2697,15 @@ mod tests {
// setup db with coin to spend
let database = &mut &mut Database::default();
let coin_input = &tx.inputs()[0];
let mut coin = CompressedCoin::default();
coin.set_owner(*coin_input.input_owner().unwrap());
coin.set_amount(coin_input.amount().unwrap());
coin.set_asset_id(*coin_input.asset_id(&base_asset_id).unwrap());
coin.set_maturity(coin_input.maturity().unwrap());
coin.set_tx_pointer(TxPointer::new(Default::default(), block_tx_idx));
database
.storage::<Coins>()
.insert(
coin_input.utxo_id().unwrap(),
&CompressedCoin {
owner: *coin_input.input_owner().unwrap(),
amount: coin_input.amount().unwrap(),
asset_id: *coin_input.asset_id(&base_asset_id).unwrap(),
maturity: coin_input.maturity().unwrap(),
tx_pointer: TxPointer::new(Default::default(), block_tx_idx),
},
)
.insert(coin_input.utxo_id().unwrap(), &coin)
.unwrap();

// make executor with db
Expand Down Expand Up @@ -2802,18 2769,14 @@ mod tests {
// setup db with coin to spend
let database = &mut &mut Database::default();
let coin_input = &tx.inputs()[0];
let mut coin = CompressedCoin::default();
coin.set_owner(*coin_input.input_owner().unwrap());
coin.set_amount(coin_input.amount().unwrap());
coin.set_asset_id(*coin_input.asset_id(&base_asset_id).unwrap());
coin.set_maturity(coin_input.maturity().unwrap());
database
.storage::<Coins>()
.insert(
coin_input.utxo_id().unwrap(),
&CompressedCoin {
owner: *coin_input.input_owner().unwrap(),
amount: coin_input.amount().unwrap(),
asset_id: *coin_input.asset_id(&base_asset_id).unwrap(),
maturity: coin_input.maturity().unwrap(),
tx_pointer: TxPointer::default(),
},
)
.insert(coin_input.utxo_id().unwrap(), &coin)
.unwrap();

// make executor with db
Expand Down
31 changes: 18 additions & 13 deletions crates/fuel-core/src/service/genesis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,28 181,33 @@ fn init_coin_state(
}),
);

let coin = CompressedCoin {
owner: coin.owner,
amount: coin.amount,
asset_id: coin.asset_id,
maturity: coin.maturity.unwrap_or_default(),
tx_pointer: TxPointer::new(
coin.tx_pointer_block_height.unwrap_or_default(),
coin.tx_pointer_tx_idx.unwrap_or_default(),
),
};
let mut compressed_coin = CompressedCoin::default();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here we also want to use V1 explicitly

compressed_coin.set_owner(coin.owner);
compressed_coin.set_amount(coin.amount);
compressed_coin.set_asset_id(coin.asset_id);
compressed_coin.set_maturity(coin.maturity.unwrap_or_default());
compressed_coin.set_tx_pointer(TxPointer::new(
coin.tx_pointer_block_height.unwrap_or_default(),
coin.tx_pointer_tx_idx.unwrap_or_default(),
));

// ensure coin can't point to blocks in the future
if coin.tx_pointer.block_height() > state.height.unwrap_or_default() {
if compressed_coin.tx_pointer().block_height()
> state.height.unwrap_or_default()
{
return Err(anyhow!(
"coin tx_pointer height cannot be greater than genesis block"
))
}

if db.storage::<Coins>().insert(&utxo_id, &coin)?.is_some() {
if db
.storage::<Coins>()
.insert(&utxo_id, &compressed_coin)?
.is_some()
{
return Err(anyhow!("Coin should not exist"))
}
coins_tree.push(coin.root()?.as_slice())
coins_tree.push(compressed_coin.root()?.as_slice())
}
}
}
Expand Down
34 changes: 16 additions & 18 deletions crates/services/executor/src/executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1007,9 1007,9 @@ where
| Input::CoinPredicate(CoinPredicate { utxo_id, .. }) => {
if let Some(coin) = db.storage::<Coins>().get(utxo_id)? {
let coin_mature_height = coin
.tx_pointer
.tx_pointer()
.block_height()
.saturating_add(*coin.maturity)
.saturating_add(**coin.maturity())
.into();
if block_height < coin_mature_height {
return Err(TransactionValidityError::CoinHasNotMatured(
Expand Down Expand Up @@ -1192,7 1192,7 @@ where
db, *utxo_id, *owner, *amount, *asset_id, *maturity,
options,
)?;
*tx_pointer = coin.tx_pointer;
*tx_pointer = *coin.tx_pointer();
}
Input::Contract(Contract {
ref mut utxo_id,
Expand Down Expand Up @@ -1240,7 1240,7 @@ where
db, *utxo_id, *owner, *amount, *asset_id, *maturity,
options,
)?;
if tx_pointer != &coin.tx_pointer {
if tx_pointer != coin.tx_pointer() {
return Err(ExecutorError::InvalidTransactionOutcome {
transaction_id: tx_id,
})
Expand Down Expand Up @@ -1371,13 1371,12 @@ where
.map(Cow::into_owned)
} else {
// if utxo validation is disabled, just assign this new input to the original block
Ok(CompressedCoin {
owner,
amount,
asset_id,
maturity,
tx_pointer: Default::default(),
})
let mut coin = CompressedCoin::default();
coin.set_owner(owner);
coin.set_amount(amount);
coin.set_asset_id(asset_id);
coin.set_maturity(maturity);
Ok(coin)
}
}

Expand Down Expand Up @@ -1508,13 1507,12 @@ where
// This is because variable or transfer outputs won't have any value
// if there's a revert or panic and shouldn't be added to the utxo set.
if *amount > Word::MIN {
let coin = CompressedCoin {
owner: *to,
amount: *amount,
asset_id: *asset_id,
maturity: 0u32.into(),
tx_pointer: TxPointer::new(block_height, tx_idx),
};
let mut coin = CompressedCoin::default();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We want to use explicitly V1 here. Plus CI fails=)
image

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops, missed this!

coin.set_owner(*to);
coin.set_amount(*amount);
coin.set_asset_id(*asset_id);
coin.set_maturity(0u32.into());
coin.set_tx_pointer(TxPointer::new(block_height, tx_idx));

if db.storage::<Coins>().insert(&utxo_id, &coin)?.is_some() {
return Err(ExecutorError::OutputAlreadyExists)
Expand Down
Loading
Loading