neoneo-go/pkg/core/native/native_gas.go

109 lines
2.7 KiB
Go
Raw Normal View History

package native
import (
"errors"
"math/big"
"github.com/nspcc-dev/neo-go/pkg/core/interop"
"github.com/nspcc-dev/neo-go/pkg/core/native/nativenames"
"github.com/nspcc-dev/neo-go/pkg/core/state"
"github.com/nspcc-dev/neo-go/pkg/crypto/hash"
"github.com/nspcc-dev/neo-go/pkg/smartcontract"
"github.com/nspcc-dev/neo-go/pkg/util"
)
// GAS represents GAS native contract.
type GAS struct {
2020-11-19 15:01:42 +00:00
nep17TokenNative
NEO *NEO
}
2021-02-15 15:43:10 +00:00
const gasContractID = -6
// GASFactor is a divisor for finding GAS integral value.
const GASFactor = NEOTotalSupply
const initialGAS = 30000000
// newGAS returns GAS native contract.
func newGAS() *GAS {
g := &GAS{}
defer g.UpdateHash()
nep17 := newNEP17Native(nativenames.Gas, gasContractID)
nep17.symbol = "GAS"
2020-11-19 15:01:42 +00:00
nep17.decimals = 8
nep17.factor = GASFactor
nep17.incBalance = g.increaseBalance
2020-11-19 15:01:42 +00:00
g.nep17TokenNative = *nep17
return g
}
func (g *GAS) increaseBalance(_ *interop.Context, _ util.Uint160, si *state.StorageItem, amount *big.Int) error {
acc, err := state.NEP17BalanceStateFromBytes(*si)
if err != nil {
return err
}
if sign := amount.Sign(); sign == 0 {
return nil
} else if sign == -1 && acc.Balance.Cmp(new(big.Int).Neg(amount)) == -1 {
return errors.New("insufficient funds")
}
acc.Balance.Add(&acc.Balance, amount)
if acc.Balance.Sign() != 0 {
*si = acc.Bytes()
} else {
*si = nil
}
return nil
}
// Initialize initializes GAS contract.
func (g *GAS) Initialize(ic *interop.Context) error {
2020-11-19 15:01:42 +00:00
if err := g.nep17TokenNative.Initialize(ic); err != nil {
return err
}
2020-11-19 15:01:42 +00:00
if g.nep17TokenNative.getTotalSupply(ic.DAO).Sign() != 0 {
return errors.New("already initialized")
}
h, err := getStandbyValidatorsHash(ic)
if err != nil {
return err
}
g.mint(ic, h, big.NewInt(initialGAS*GASFactor), false)
return nil
}
// OnPersist implements Contract interface.
func (g *GAS) OnPersist(ic *interop.Context) error {
if len(ic.Block.Transactions) == 0 {
return nil
}
for _, tx := range ic.Block.Transactions {
absAmount := big.NewInt(tx.SystemFee + tx.NetworkFee)
g.burn(ic, tx.Sender(), absAmount)
}
2020-08-28 07:24:54 +00:00
validators := g.NEO.GetNextBlockValidatorsInternal()
2021-03-01 12:20:27 +00:00
primary := validators[ic.Block.PrimaryIndex].GetScriptHash()
var netFee int64
for _, tx := range ic.Block.Transactions {
netFee += tx.NetworkFee
}
g.mint(ic, primary, big.NewInt(int64(netFee)), false)
return nil
}
// PostPersist implements Contract interface.
func (g *GAS) PostPersist(ic *interop.Context) error {
return nil
}
func getStandbyValidatorsHash(ic *interop.Context) (util.Uint160, error) {
s, err := smartcontract.CreateDefaultMultiSigRedeemScript(ic.Chain.GetStandByValidators())
if err != nil {
return util.Uint160{}, err
}
return hash.Hash160(s), nil
}