Skip to content
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
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,9 @@ require (
go.yaml.in/yaml/v3 v3.0.4 // indirect
golang.org/x/crypto v0.48.0 // indirect
golang.org/x/exp v0.0.0-20250911091902-df9299821621 // indirect
golang.org/x/mod v0.33.0 // indirect
golang.org/x/text v0.34.0 // indirect
golang.org/x/tools v0.42.0 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20251202230838-ff82c1b0f217 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
lukechampine.com/blake3 v1.2.1 // indirect
Expand Down
55 changes: 55 additions & 0 deletions pkg/core/metachain/contracts.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package metachain

import (
neogoconfig "github.com/nspcc-dev/neo-go/pkg/config"
"github.com/nspcc-dev/neo-go/pkg/core/interop"
"github.com/nspcc-dev/neo-go/pkg/core/native"
"github.com/nspcc-dev/neofs-node/pkg/core/metachain/gas"
"github.com/nspcc-dev/neofs-node/pkg/core/metachain/meta"
)

// NewCustomNatives returns custom list of native contracts for metadata
// side chain. Returned contracts:
// - Management
// - Ledger
// - NEO
// - redefined GAS (see [gas.NewGAS] for details)
// - Policy
// - Designate
// - Notary
// - new native metadata contract (see [meta.NewMetadata] for details).
func NewCustomNatives(cfg neogoconfig.ProtocolConfiguration) []interop.Contract {
mgmt := native.NewManagement()
ledger := native.NewLedger()

g := gas.NewGAS()
n := native.NewNEO(cfg)
p := native.NewPolicy()

n.GAS = g
n.Policy = p

mgmt.NEO = n
mgmt.Policy = p
ledger.Policy = p

desig := native.NewDesignate(cfg.Genesis.Roles)
desig.NEO = n

notary := native.NewNotary()
notary.Policy = p
notary.GAS = g
notary.NEO = n
notary.Desig = desig

return []interop.Contract{
mgmt,
ledger,
n,
g,
p,
desig,
notary,
meta.NewMetadata(n),
}
}
170 changes: 170 additions & 0 deletions pkg/core/metachain/gas/gas.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
package gas

import (
"fmt"
"math/big"

"github.com/nspcc-dev/neo-go/pkg/config"
"github.com/nspcc-dev/neo-go/pkg/core/dao"
"github.com/nspcc-dev/neo-go/pkg/core/interop"
"github.com/nspcc-dev/neo-go/pkg/core/interop/contract"
"github.com/nspcc-dev/neo-go/pkg/core/native"
"github.com/nspcc-dev/neo-go/pkg/core/native/nativeids"
"github.com/nspcc-dev/neo-go/pkg/core/native/nativenames"
"github.com/nspcc-dev/neo-go/pkg/smartcontract"
"github.com/nspcc-dev/neo-go/pkg/smartcontract/callflag"
"github.com/nspcc-dev/neo-go/pkg/smartcontract/manifest"
"github.com/nspcc-dev/neo-go/pkg/util"
"github.com/nspcc-dev/neo-go/pkg/vm/stackitem"
)

// DefaultBalance is a balance of every account in redefined [GAS] native
// contract.
const DefaultBalance = 100 * native.GASFactor

var _ = (native.IGAS)(&GAS{})

func (g *GAS) Metadata() *interop.ContractMD {
return &g.ContractMD
}

// GAS represents GAS custom native contract. It always returns [DefaultBalance] as a
// balance, has no-op `Burn`, `Mint`, `Transfer` operations.
type GAS struct {
interop.ContractMD
}
Comment thread
AnnaShaleva marked this conversation as resolved.

// NewGAS returns [GAS] custom native contract.
func NewGAS() *GAS {
g := &GAS{}
defer g.BuildHFSpecificMD(g.ActiveIn())

g.ContractMD = *interop.NewContractMD(nativenames.Gas, nativeids.GasToken, func(m *manifest.Manifest, hf config.Hardfork) {
m.SupportedStandards = []string{manifest.NEP17StandardName}
})

desc := native.NewDescriptor("symbol", smartcontract.StringType)
md := native.NewMethodAndPrice(g.symbol, 0, callflag.NoneFlag)
g.AddMethod(md, desc)

desc = native.NewDescriptor("decimals", smartcontract.IntegerType)
md = native.NewMethodAndPrice(g.decimals, 0, callflag.NoneFlag)
g.AddMethod(md, desc)

desc = native.NewDescriptor("totalSupply", smartcontract.IntegerType)
md = native.NewMethodAndPrice(g.totalSupply, 1<<15, callflag.ReadStates)
g.AddMethod(md, desc)

desc = native.NewDescriptor("balanceOf", smartcontract.IntegerType,
manifest.NewParameter("account", smartcontract.Hash160Type))
md = native.NewMethodAndPrice(g.balanceOf, 1<<15, callflag.ReadStates)
g.AddMethod(md, desc)

transferParams := []manifest.Parameter{
manifest.NewParameter("from", smartcontract.Hash160Type),
manifest.NewParameter("to", smartcontract.Hash160Type),
manifest.NewParameter("amount", smartcontract.IntegerType),
}
desc = native.NewDescriptor("transfer", smartcontract.BoolType,
append(transferParams, manifest.NewParameter("data", smartcontract.AnyType))...,
)
md = native.NewMethodAndPrice(g.transfer, 1<<17, callflag.States|callflag.AllowCall|callflag.AllowNotify)
g.AddMethod(md, desc)

eDesc := native.NewEventDescriptor("Transfer", transferParams...)
eMD := native.NewEvent(eDesc)
g.AddEvent(eMD)

return g
}

// Initialize initializes a GAS contract.
func (g *GAS) Initialize(ic *interop.Context, hf *config.Hardfork, newMD *interop.HFSpecificContractMD) error {
return nil
}

// InitializeCache implements the [interop.Contract] interface.
func (g *GAS) InitializeCache(_ interop.IsHardforkEnabled, blockHeight uint32, d *dao.Simple) error {
return nil
}

// OnPersist implements the [interop.Contract] interface.
func (g *GAS) OnPersist(ic *interop.Context) error {
return nil
}

// PostPersist implements the [interop.Contract] interface.
func (g *GAS) PostPersist(ic *interop.Context) error {
return nil
}

// ActiveIn implements the [interop.Contract] interface.
func (g *GAS) ActiveIn() *config.Hardfork {
return nil
}

// BalanceOf returns native GAS token balance for the acc.
func (g *GAS) BalanceOf(d *dao.Simple, acc util.Uint160) *big.Int {
return big.NewInt(DefaultBalance)
}

func (g *GAS) symbol(_ *interop.Context, _ []stackitem.Item) stackitem.Item {
return stackitem.Make([]byte("GAS"))
}

func (g *GAS) decimals(_ *interop.Context, _ []stackitem.Item) stackitem.Item {
return stackitem.Make(8)
}

func (g *GAS) totalSupply(ic *interop.Context, _ []stackitem.Item) stackitem.Item {
return stackitem.Make(DefaultBalance)
}

func toUint160(s stackitem.Item) util.Uint160 {
u, err := stackitem.ToUint160(s)
if err != nil {
panic(err)
}
return u
}

func toBigInt(s stackitem.Item) *big.Int {
bi, err := s.TryInteger()
if err != nil {
panic(err)
}
return bi
}

func (g *GAS) transfer(ic *interop.Context, args []stackitem.Item) stackitem.Item {
from := toUint160(args[0])
to := toUint160(args[1])
amount := toBigInt(args[2])

paymentArgs := []stackitem.Item{
stackitem.Make(from),
stackitem.Make(amount),
args[3],
}
cs, err := ic.GetContract(to)
if err == nil {
err = contract.CallFromNative(ic, g.Hash, cs, manifest.MethodOnNEP17Payment, paymentArgs, false)
if err != nil {
panic(fmt.Errorf("failed to call %s: %w", manifest.MethodOnNEP17Payment, err))
}
}

return stackitem.Make(true)
}

// balanceOf is the only difference with default native GAS implementation:
// it always returns fixed number of tokens.
func (g *GAS) balanceOf(ic *interop.Context, args []stackitem.Item) stackitem.Item {
return stackitem.Make(DefaultBalance)
}

func (g *GAS) Mint(ic *interop.Context, h util.Uint160, amount *big.Int, callOnPayment bool) {
}

func (g *GAS) Burn(ic *interop.Context, h util.Uint160, amount *big.Int) {
}
43 changes: 43 additions & 0 deletions pkg/core/metachain/gas/gas_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package gas_test

import (
"testing"

"github.com/nspcc-dev/neo-go/pkg/core/native"
"github.com/nspcc-dev/neo-go/pkg/core/native/nativenames"
"github.com/nspcc-dev/neo-go/pkg/neotest"
"github.com/nspcc-dev/neo-go/pkg/neotest/chain"
"github.com/nspcc-dev/neo-go/pkg/vm/stackitem"
"github.com/nspcc-dev/neofs-node/pkg/core/metachain"
)

func newGasClient(t *testing.T) (*neotest.ContractInvoker, *neotest.ContractInvoker) {
ch, validators, committee := chain.NewMultiWithOptions(t, &chain.Options{
NewNatives: metachain.NewCustomNatives,
})
e := neotest.NewExecutor(t, ch, validators, committee)

return e.ValidatorInvoker(e.NativeHash(t, nativenames.Gas)), e.CommitteeInvoker(e.NativeHash(t, nativenames.Gas))
}

const defaultBalance = 100 * native.GASFactor

func TestGAS(t *testing.T) {
gasValidatorsI, gasCommitteeI := newGasClient(t)
hardcodedBalance := stackitem.Make(defaultBalance)

t.Run("committee balance", func(t *testing.T) {
gasCommitteeI.Invoke(t, hardcodedBalance, "balanceOf", gasCommitteeI.Hash)
})

t.Run("new account balance", func(t *testing.T) {
s := gasValidatorsI.NewAccount(t, defaultBalance+1)
gasCommitteeI.WithSigners(s).Invoke(t, hardcodedBalance, "balanceOf", s.ScriptHash())
})

t.Run("transfer does not change balance", func(t *testing.T) {
newAcc := gasValidatorsI.NewAccount(t, defaultBalance+1)
gasCommitteeI.Invoke(t, stackitem.Make(true), "transfer", gasCommitteeI.Hash, newAcc.ScriptHash(), 1, stackitem.Null{})
gasCommitteeI.Invoke(t, hardcodedBalance, "balanceOf", newAcc.ScriptHash())
})
}
29 changes: 29 additions & 0 deletions pkg/core/metachain/meta/const.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package meta

import (
"math"
)

// Metadata contract identifiers.
const (
MetaDataContractID = math.MinInt32
MetaDataContractName = "MetaData"
)

const (
// storage prefixes.
metaContainersPrefix = iota
containerPlacementPrefix

// object prefixes.
addrIndex
typeIndex
lockedByIndex
)

const (
// event names.
objectPutEvent = "ObjectPut"
objectDeletedEvent = "ObjectDeleted"
objectLockedEvent = "ObjectLocked"
)
Loading
Loading