Fix inconsistent creation fee for free containers #45

Merged
alexvanin merged 2 commits from alexvanin/frostfs-contract:fix/cnr-create-gas/v0.18 into support/v0.18 2024-09-04 19:51:17 +00:00
2 changed files with 48 additions and 4 deletions

View file

@ -458,13 +458,13 @@ func updateBalance(ctx storage.Context, tokenId []byte, acc interop.Hash160, dif
balanceKey := append([]byte{prefixBalance}, acc...)
var balance int
if b := storage.Get(ctx, balanceKey); b != nil {
balance = b.(int)
balance = common.FromFixedWidth64(b.([]byte))
}
balance += diff
if balance == 0 {
storage.Delete(ctx, balanceKey)
} else {
storage.Put(ctx, balanceKey, balance)
storage.Put(ctx, balanceKey, common.ToFixedWidth64(balance))
}
tokenKey := getTokenKey(tokenId)
@ -488,14 +488,14 @@ func postTransfer(from, to interop.Hash160, tokenID []byte, data interface{}) {
// getTotalSupply returns total supply from storage.
func getTotalSupply(ctx storage.Context) int {
val := storage.Get(ctx, []byte{prefixTotalSupply})
return val.(int)
return common.FromFixedWidth64(val.([]byte))
}
// updateTotalSupply adds the specified diff to the total supply.
func updateTotalSupply(ctx storage.Context, diff int) {
tsKey := []byte{prefixTotalSupply}
ts := getTotalSupply(ctx)
storage.Put(ctx, tsKey, ts+diff)
storage.Put(ctx, tsKey, common.ToFixedWidth64(ts+diff))
}
// getTokenKey computes hash160 from the given tokenID.

View file

@ -3,6 +3,7 @@ package tests
import (
"bytes"
"crypto/sha256"
"fmt"
"math/big"
"path"
"testing"
@ -12,6 +13,7 @@ import (
"git.frostfs.info/TrueCloudLab/frostfs-contract/nns"
"github.com/mr-tron/base58"
"github.com/nspcc-dev/neo-go/pkg/core/interop/storage"
"github.com/nspcc-dev/neo-go/pkg/core/transaction"
"github.com/nspcc-dev/neo-go/pkg/encoding/address"
"github.com/nspcc-dev/neo-go/pkg/neotest"
"github.com/nspcc-dev/neo-go/pkg/util"
@ -57,6 +59,24 @@ func newContainerInvoker(t *testing.T) (*neotest.ContractInvoker, *neotest.Contr
return e.CommitteeInvoker(ctrContainer.Hash), e.CommitteeInvoker(ctrBalance.Hash), e.CommitteeInvoker(ctrNetmap.Hash)
}
// TODO(alexvanin): remove this after fix of inconsistent tx cost in balance contract
func newFreeContainerInvoker(t *testing.T) (*neotest.ContractInvoker, *neotest.ContractInvoker, *neotest.ContractInvoker) {
e := newExecutor(t)
ctrNNS := neotest.CompileFile(t, e.CommitteeHash, nnsPath, path.Join(nnsPath, "config.yml"))
ctrNetmap := neotest.CompileFile(t, e.CommitteeHash, netmapPath, path.Join(netmapPath, "config.yml"))
ctrBalance := neotest.CompileFile(t, e.CommitteeHash, balancePath, path.Join(balancePath, "config.yml"))
ctrContainer := neotest.CompileFile(t, e.CommitteeHash, containerPath, path.Join(containerPath, "config.yml"))
e.DeployContract(t, ctrNNS, nil)
deployNetmapContract(t, e, ctrBalance.Hash, ctrContainer.Hash,
container.RegistrationFeeKey, int64(0),
container.AliasFeeKey, int64(0))
deployBalanceContract(t, e, ctrNetmap.Hash, ctrContainer.Hash)
deployContainerContract(t, e, ctrNetmap.Hash, ctrBalance.Hash, ctrNNS.Hash)
return e.CommitteeInvoker(ctrContainer.Hash), e.CommitteeInvoker(ctrBalance.Hash), e.CommitteeInvoker(ctrNetmap.Hash)
}
func setContainerOwner(c []byte, acc neotest.Signer) {
copy(c[6:], signerToOwner(acc))
}
@ -231,6 +251,30 @@ func TestContainerPut(t *testing.T) {
cNNS.Invoke(t, expected, "resolve", "domain.cdn", int64(nns.TXT))
})
})
t.Run("gas costs are the same for all containers in block", func(t *testing.T) {
c, _, _ := newFreeContainerInvoker(t)
const containerPerBlock = 512
acc := c.NewAccount(t)
cnt := dummyContainer(acc)
putArgs := []interface{}{cnt.value, cnt.sig, cnt.pub, cnt.token, "precreated", ""}
c.Invoke(t, stackitem.Null{}, "putNamed", putArgs...)
txs := make([]*transaction.Transaction, 0, containerPerBlock)
for i := 0; i < containerPerBlock; i++ {
cnt := dummyContainer(acc)
name := fmt.Sprintf("name-%.5d", i)
tx := c.PrepareInvoke(t, "putNamed", cnt.value, cnt.sig, cnt.pub, cnt.token, name, "")
txs = append(txs, tx)
}
c.AddNewBlock(t, txs...)
for i := 0; i < containerPerBlock; i++ {
c.CheckHalt(t, txs[i].Hash(), stackitem.Make(stackitem.Null{}))
}
})
}
func addContainer(t *testing.T, c, cBal *neotest.ContractInvoker) (neotest.Signer, testContainer) {