From 6212b5bf72a6d0e4f920ead617852336243348ef Mon Sep 17 00:00:00 2001 From: Evgenii Stratonikov Date: Thu, 5 Oct 2023 15:01:30 +0300 Subject: [PATCH] [#42] container: Make GAS costs more predictable in Delete() Persisting a transaction is done in 2 stages: 1. TestInvoke 2. Sign and send to the network. 3. At some point the tx is persisted. Some time passes between 1 and 3, this could lead to different GAS costs. It is a known issue for container delete: different epoch can have different size in bytes and thus different cost to store. Here we introduce fixed-length encoding for integers, so that the problem can be avoided. Signed-off-by: Evgenii Stratonikov --- container/container_contract.go | 16 +++++++++++++--- tests/container_test.go | 2 -- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/container/container_contract.go b/container/container_contract.go index e8e6bb1..f45c1c9 100644 --- a/container/container_contract.go +++ b/container/container_contract.go @@ -343,6 +343,11 @@ type DelInfo struct { Epoch int } +type delInfo struct { + Owner []byte + Epoch []byte +} + // DeletionInfo method returns container deletion info. // If the container had never existed, NotFoundError is throwed. // It can be used to check whether non-existing container was indeed deleted @@ -354,7 +359,12 @@ func DeletionInfo(containerID []byte) DelInfo { if data == nil { panic(NotFoundError) } - return std.Deserialize(data).(DelInfo) + + d := std.Deserialize(data).(delInfo) + return DelInfo{ + Owner: d.Owner, + Epoch: common.FromFixedWidth64(d.Epoch), + } } // Get method returns a structure that contains a stable marshaled Container structure, @@ -631,9 +641,9 @@ func removeContainer(ctx storage.Context, id []byte, owner []byte) { graveKey := append([]byte{graveKeyPrefix}, id...) netmapContractAddr := storage.Get(ctx, netmapContractKey).(interop.Hash160) epoch := contract.Call(netmapContractAddr, "epoch", contract.ReadOnly).(int) - common.SetSerialized(ctx, graveKey, DelInfo{ + common.SetSerialized(ctx, graveKey, delInfo{ Owner: owner, - Epoch: epoch, + Epoch: common.ToFixedWidth64(epoch), }) } diff --git a/tests/container_test.go b/tests/container_test.go index 2f5d9d5..0508c3c 100644 --- a/tests/container_test.go +++ b/tests/container_test.go @@ -283,8 +283,6 @@ func TestContainerDelete(t *testing.T) { }) t.Run("gas costs are the same for different epochs", func(t *testing.T) { - t.Skip() - _, cnt2 := addContainer(t, c, cBal) args := []interface{}{cnt2.id[:], cnt2.sig, cnt2.pub, cnt2.token}