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

fix: calculate auction expiration correctly when extension present #1866

Merged
merged 1 commit into from
Jul 24, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
202 changes: 98 additions & 104 deletions lib/ae_mdw/db/mutations/name_claim_mutation.ex
Original file line number Diff line number Diff line change
Expand Up @@ -90,113 +90,107 @@ defmodule AeMdw.Db.NameClaimMutation do

state2 = State.put(state, Model.PlainName, m_plain_name)

timeout =
if State.exists?(state2, Model.AuctionBid, plain_name) do
:aec_governance.name_claim_bid_extension(plain_name, protocol_version)
else
:aec_governance.name_claim_bid_timeout(plain_name, protocol_version)
end

case timeout do
0 ->
expire = Names.expire_after(height)

m_name =
Model.name(
index: plain_name,
active: height,
expire: expire,
owner: owner_pk,
auction_timeout: 0
)

lock_amount = (lima_or_higher? && name_fee) || :aec_governance.name_claim_locked_fee()

name_claim = Model.name_claim(index: {plain_name, height, txi_idx})

ObjectKeys.put_active_name(state, plain_name)

state2
|> Name.put_active(m_name)
|> State.put(Model.NameClaim, name_claim)
|> Name.delete_inactive(plain_name)
|> IntTransfer.fee({height, txi_idx}, :lock_name, owner_pk, txi_idx, lock_amount)
|> State.inc_stat(:burned_in_auctions, lock_amount)
|> Names.increment_names_count(owner_pk)

timeout ->
auction_end = height + timeout

state3 =
IntTransfer.fee(state2, {height, txi_idx}, :spend_name, owner_pk, txi_idx, name_fee)

state4 =
case State.get(state3, Model.AuctionBid, plain_name) do
:not_found ->
auction_claim = Model.auction_bid_claim(index: {plain_name, height, txi_idx})

m_auction_bid =
Model.auction_bid(
index: plain_name,
start_height: height,
block_index_txi_idx: {block_index, txi_idx},
expire_height: auction_end,
owner: owner_pk
)

m_auction_exp = Model.expiration(index: {auction_end, plain_name})

state3
|> State.inc_stat(:auctions_started)
|> State.put(Model.AuctionBidClaim, auction_claim)
|> State.put(Model.AuctionBid, m_auction_bid)
|> State.put(Model.AuctionExpiration, m_auction_exp)

{:ok,
Model.auction_bid(
start_height: start_height,
block_index_txi_idx: {_bi, prev_txi_idx},
expire_height: prev_auction_end,
owner: prev_owner
) = auction_bid} ->
prev_name_claim_tx = DbUtil.read_node_tx(state, prev_txi_idx)
prev_name_fee = :aens_claim_tx.name_fee(prev_name_claim_tx)

auction_claim = Model.auction_bid_claim(index: {plain_name, start_height, txi_idx})
actual_auction_end = max(auction_end, prev_auction_end)

m_auction_bid =
Model.auction_bid(auction_bid,
block_index_txi_idx: {block_index, txi_idx},
expire_height: actual_auction_end,
owner: owner_pk
)

state3
|> State.delete(Model.AuctionBid, plain_name)
|> State.delete(Model.AuctionOwner, {prev_owner, plain_name})
|> State.delete(
Model.AuctionExpiration,
{prev_auction_end, plain_name}
timeout = :aec_governance.name_claim_bid_timeout(plain_name, protocol_version)

if timeout == 0 do
expire = Names.expire_after(height)

m_name =
Model.name(
index: plain_name,
active: height,
expire: expire,
owner: owner_pk,
auction_timeout: 0
)

lock_amount = (lima_or_higher? && name_fee) || :aec_governance.name_claim_locked_fee()

name_claim = Model.name_claim(index: {plain_name, height, txi_idx})

ObjectKeys.put_active_name(state, plain_name)

state2
|> Name.put_active(m_name)
|> State.put(Model.NameClaim, name_claim)
|> Name.delete_inactive(plain_name)
|> IntTransfer.fee({height, txi_idx}, :lock_name, owner_pk, txi_idx, lock_amount)
|> State.inc_stat(:burned_in_auctions, lock_amount)
|> Names.increment_names_count(owner_pk)
else
state3 =
IntTransfer.fee(state2, {height, txi_idx}, :spend_name, owner_pk, txi_idx, name_fee)

state4 =
case State.get(state3, Model.AuctionBid, plain_name) do
:not_found ->
auction_claim = Model.auction_bid_claim(index: {plain_name, height, txi_idx})
auction_end = height + timeout

m_auction_bid =
Model.auction_bid(
index: plain_name,
start_height: height,
block_index_txi_idx: {block_index, txi_idx},
expire_height: auction_end,
owner: owner_pk
)
|> IntTransfer.fee(
{height, txi_idx},
:refund_name,
prev_owner,
prev_txi_idx,
prev_name_fee
)
|> State.inc_stat(:locked_in_auctions, name_fee - prev_name_fee)
|> State.put(Model.AuctionBidClaim, auction_claim)
|> State.put(Model.AuctionBid, m_auction_bid)
|> State.put(
Model.AuctionExpiration,
Model.expiration(index: {actual_auction_end, plain_name})

m_auction_exp = Model.expiration(index: {auction_end, plain_name})

state3
|> State.inc_stat(:auctions_started)
|> State.put(Model.AuctionBidClaim, auction_claim)
|> State.put(Model.AuctionBid, m_auction_bid)
|> State.put(Model.AuctionExpiration, m_auction_exp)

{:ok,
Model.auction_bid(
start_height: start_height,
block_index_txi_idx: {_bi, prev_txi_idx},
expire_height: prev_auction_end,
owner: prev_owner
) = auction_bid} ->
auction_end =
prev_auction_end +
:aec_governance.name_claim_bid_extension(plain_name, protocol_version)
Copy link
Contributor

Choose a reason for hiding this comment

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

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It's extended when auction already exists, which is this case, am I missing anything? https://github.com/aeternity/aeternity/blob/master/apps/aeprimop/src/aeprimop.erl#L936

Copy link
Contributor

Choose a reason for hiding this comment

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

It's ok in that case


prev_name_claim_tx = DbUtil.read_node_tx(state, prev_txi_idx)
prev_name_fee = :aens_claim_tx.name_fee(prev_name_claim_tx)
auction_claim = Model.auction_bid_claim(index: {plain_name, start_height, txi_idx})

m_auction_bid =
Model.auction_bid(auction_bid,
block_index_txi_idx: {block_index, txi_idx},
expire_height: auction_end,
owner: owner_pk
)
end

state4
|> State.put(Model.AuctionOwner, Model.owner(index: {owner_pk, plain_name}))
state3
|> State.delete(Model.AuctionBid, plain_name)
|> State.delete(Model.AuctionOwner, {prev_owner, plain_name})
|> State.delete(
Model.AuctionExpiration,
{prev_auction_end, plain_name}
)
|> IntTransfer.fee(
{height, txi_idx},
:refund_name,
prev_owner,
prev_txi_idx,
prev_name_fee
)
|> State.inc_stat(:locked_in_auctions, name_fee - prev_name_fee)
|> State.put(Model.AuctionBidClaim, auction_claim)
|> State.put(Model.AuctionBid, m_auction_bid)
|> State.put(
Model.AuctionExpiration,
Model.expiration(index: {auction_end, plain_name})
)
end

state4
|> State.put(Model.AuctionOwner, Model.owner(index: {owner_pk, plain_name}))
end
end
end
17 changes: 10 additions & 7 deletions test/ae_mdw/db/name_claim_mutation_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ defmodule AeMdw.Db.NameClaimMutationTest do
block_index = {claim_height, 0}
state = State.new(store)
timeout = :aec_governance.name_claim_bid_timeout(plain_name, protocol_version)
extended = :aec_governance.name_claim_bid_extension(plain_name, protocol_version)
extension = :aec_governance.name_claim_bid_extension(plain_name, protocol_version)
tx_hash = <<1::256>>
expire_height = claim_height + timeout

Expand Down Expand Up @@ -187,7 +187,7 @@ defmodule AeMdw.Db.NameClaimMutationTest do
name_fee,
false,
next_txi_idx,
{claim_height + 1, 0},
{claim_height, 0},
protocol_version
)

Expand All @@ -202,6 +202,7 @@ defmodule AeMdw.Db.NameClaimMutationTest do
})

{:name_claim_tx, claim_tx} = :aetx.specialize_type(claim_aetx)
expire_height = expire_height + extension

with_mocks [
{AeMdw.Node.Db, [:passthrough],
Expand Down Expand Up @@ -243,20 +244,20 @@ defmodule AeMdw.Db.NameClaimMutationTest do
name_fee,
false,
{almost_expired_txi, -1},
{expire_height - 1, 0},
{claim_height + 100, 0},
protocol_version
)

state = Mutation.execute(bid_mutation_2, state)

new_expire_height = expire_height - 1 + extended
expire_height = expire_height + extension

assert {:ok,
Model.auction_bid(
index: ^plain_name,
start_height: ^claim_height,
owner: ^owner_pk,
expire_height: ^new_expire_height
expire_height: ^expire_height
)} = State.get(state, Model.AuctionBid, plain_name)

bid_mutation_3 =
Expand All @@ -267,18 +268,20 @@ defmodule AeMdw.Db.NameClaimMutationTest do
name_fee,
false,
{almost_expired_txi + 1, -1},
{new_expire_height - extended - 1, 0},
{claim_height + 200, 0},
protocol_version
)

state = Mutation.execute(bid_mutation_3, state)

expire_height = expire_height + extension

assert {:ok,
Model.auction_bid(
index: ^plain_name,
start_height: ^claim_height,
owner: ^owner_pk,
expire_height: ^new_expire_height
expire_height: ^expire_height
)} = State.get(state, Model.AuctionBid, plain_name)
end
end
Expand Down
Loading