Merge pull request #2078 from nspcc-dev/configurable-initial-gas

config: add InitialGASSupply, fix #2073
This commit is contained in:
Roman Khimov 2021-07-20 17:10:25 +03:00 committed by GitHub
commit 35c2c3ae8e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
12 changed files with 45 additions and 22 deletions

View file

@ -1,6 +1,7 @@
ProtocolConfiguration:
Magic: 5195086
MaxTraceableBlocks: 2102400
InitialGASSupply: 52000000
SecondsPerBlock: 15
MemPoolSize: 50000
StandbyCommittee:

View file

@ -1,6 +1,7 @@
ProtocolConfiguration:
Magic: 877933390
MaxTraceableBlocks: 2102400
InitialGASSupply: 52000000
SecondsPerBlock: 15
MemPoolSize: 50000
StandbyCommittee:

View file

@ -6,6 +6,7 @@ import (
"strings"
"testing"
"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/interop/interopnames"
"github.com/nspcc-dev/neo-go/pkg/core/native"
@ -30,7 +31,8 @@ import (
)
func TestContractHashes(t *testing.T) {
cs := native.NewContracts(true, map[string][]uint32{})
cfg := config.ProtocolConfiguration{P2PSigExtensions: true}
cs := native.NewContracts(cfg)
require.Equalf(t, []byte(neo.Hash), cs.NEO.Hash.BytesBE(), "%q", string(cs.NEO.Hash.BytesBE()))
require.Equalf(t, []byte(gas.Hash), cs.GAS.Hash.BytesBE(), "%q", string(cs.GAS.Hash.BytesBE()))
require.Equalf(t, []byte(oracle.Hash), cs.Oracle.Hash.BytesBE(), "%q", string(cs.Oracle.Hash.BytesBE()))
@ -92,7 +94,8 @@ type nativeTestCase struct {
// Here we test that corresponding method does exist, is invoked and correct value is returned.
func TestNativeHelpersCompile(t *testing.T) {
cs := native.NewContracts(true, map[string][]uint32{})
cfg := config.ProtocolConfiguration{P2PSigExtensions: true}
cs := native.NewContracts(cfg)
u160 := `interop.Hash160("aaaaaaaaaaaaaaaaaaaa")`
u256 := `interop.Hash256("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")`
pub := `interop.PublicKey("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")`

View file

@ -2,6 +2,7 @@ package config
import (
"github.com/nspcc-dev/neo-go/pkg/config/netmode"
"github.com/nspcc-dev/neo-go/pkg/encoding/fixedn"
)
// ProtocolConfiguration represents the protocol config.
@ -9,6 +10,9 @@ type (
ProtocolConfiguration struct {
Magic netmode.Magic `yaml:"Magic"`
MemPoolSize int `yaml:"MemPoolSize"`
// InitialGASSupply is the amount of GAS generated in the genesis block.
InitialGASSupply fixedn.Fixed8 `yaml:"InitialGASSupply"`
// P2PNotaryRequestPayloadPoolSize specifies the memory pool size for P2PNotaryRequestPayloads.
// It is valid only if P2PSigExtensions are enabled.
P2PNotaryRequestPayloadPoolSize int `yaml:"P2PNotaryRequestPayloadPoolSize"`

View file

@ -389,13 +389,14 @@ func TestVerifyBlock(t *testing.T) {
require.True(t, srv.verifyBlock(&neoBlock{Block: *b}))
})
t.Run("good conflicting tx", func(t *testing.T) {
initGAS := srv.Chain.GetConfig().InitialGASSupply
tx1 := transaction.New([]byte{byte(opcode.RET)}, 100000)
tx1.NetworkFee = 20_000_000 * native.GASFactor
tx1.NetworkFee = int64(initGAS)/2 + 1
tx1.ValidUntilBlock = 1
addSender(t, tx1)
signTx(t, srv.Chain, tx1)
tx2 := transaction.New([]byte{byte(opcode.RET)}, 100000)
tx2.NetworkFee = 20_000_000 * native.GASFactor
tx2.NetworkFee = int64(initGAS)/2 + 1
tx2.ValidUntilBlock = 1
addSender(t, tx2)
signTx(t, srv.Chain, tx2)

View file

@ -28,6 +28,7 @@ import (
"github.com/nspcc-dev/neo-go/pkg/crypto/hash"
"github.com/nspcc-dev/neo-go/pkg/crypto/keys"
"github.com/nspcc-dev/neo-go/pkg/encoding/bigint"
"github.com/nspcc-dev/neo-go/pkg/encoding/fixedn"
"github.com/nspcc-dev/neo-go/pkg/io"
"github.com/nspcc-dev/neo-go/pkg/smartcontract"
"github.com/nspcc-dev/neo-go/pkg/smartcontract/callflag"
@ -44,6 +45,7 @@ const (
headerBatchCount = 2000
version = "0.1.0"
defaultInitialGAS = 52000000_00000000
defaultMemPoolSize = 50000
defaultP2PNotaryRequestPayloadPoolSize = 1000
defaultMaxBlockSize = 262144
@ -165,6 +167,10 @@ func NewBlockchain(s storage.Store, cfg config.ProtocolConfiguration, log *zap.L
return nil, errors.New("empty logger")
}
if cfg.InitialGASSupply <= 0 {
cfg.InitialGASSupply = fixedn.Fixed8(defaultInitialGAS)
log.Info("initial gas supply is not set or wrong, setting default value", zap.String("InitialGASSupply", cfg.InitialGASSupply.String()))
}
if cfg.MemPoolSize <= 0 {
cfg.MemPoolSize = defaultMemPoolSize
log.Info("mempool size is not set or wrong, setting default value", zap.Int("MemPoolSize", cfg.MemPoolSize))
@ -216,8 +222,7 @@ func NewBlockchain(s storage.Store, cfg config.ProtocolConfiguration, log *zap.L
events: make(chan bcEvent),
subCh: make(chan interface{}),
unsubCh: make(chan interface{}),
contracts: *native.NewContracts(cfg.P2PSigExtensions, cfg.NativeUpdateHistories),
contracts: *native.NewContracts(cfg),
}
bc.stateRoot = stateroot.NewModule(bc, bc.log, bc.dao.Store)

View file

@ -1639,7 +1639,7 @@ func TestConfigNativeUpdateHistory(t *testing.T) {
cfgPath := path.Join(prefixPath, fmt.Sprintf("protocol.%s.yml", cfgFileSuffix))
cfg, err := config.LoadFile(cfgPath)
require.NoError(t, err, fmt.Errorf("failed to load %s", cfgPath))
natives := native.NewContracts(cfg.ProtocolConfiguration.P2PSigExtensions, map[string][]uint32{})
natives := native.NewContracts(cfg.ProtocolConfiguration)
assert.Equal(t, len(natives.Contracts),
len(cfg.ProtocolConfiguration.NativeUpdateHistories),
fmt.Errorf("protocol configuration file %s: extra or missing NativeUpdateHistory in NativeActivations section", cfgPath))

View file

@ -4,12 +4,14 @@ import (
"testing"
"unicode"
"github.com/nspcc-dev/neo-go/pkg/config"
"github.com/stretchr/testify/require"
)
// "C" and "O" can easily be typed by accident.
func TestNamesASCII(t *testing.T) {
cs := NewContracts(true, map[string][]uint32{})
cfg := config.ProtocolConfiguration{P2PSigExtensions: true}
cs := NewContracts(cfg)
for _, c := range cs.Contracts {
require.True(t, isASCII(c.Metadata().Name))
for _, m := range c.Metadata().Methods {

View file

@ -3,6 +3,7 @@ package native
import (
"strings"
"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/interop/interopnames"
"github.com/nspcc-dev/neo-go/pkg/io"
@ -55,7 +56,7 @@ func (cs *Contracts) ByName(name string) interop.Contract {
// NewContracts returns new set of native contracts with new GAS, NEO, Policy, Oracle,
// Designate and (optional) Notary contracts.
func NewContracts(p2pSigExtensionsEnabled bool, nativeUpdateHistories map[string][]uint32) *Contracts {
func NewContracts(cfg config.ProtocolConfiguration) *Contracts {
cs := new(Contracts)
mgmt := newManagement()
@ -74,7 +75,7 @@ func NewContracts(p2pSigExtensionsEnabled bool, nativeUpdateHistories map[string
cs.Ledger = ledger
cs.Contracts = append(cs.Contracts, ledger)
gas := newGAS()
gas := newGAS(int64(cfg.InitialGASSupply))
neo := newNEO()
neo.GAS = gas
gas.NEO = neo
@ -90,7 +91,7 @@ func NewContracts(p2pSigExtensionsEnabled bool, nativeUpdateHistories map[string
cs.Policy = policy
cs.Contracts = append(cs.Contracts, policy)
desig := newDesignate(p2pSigExtensionsEnabled)
desig := newDesignate(cfg.P2PSigExtensions)
desig.NEO = neo
cs.Designate = desig
cs.Contracts = append(cs.Contracts, desig)
@ -102,7 +103,7 @@ func NewContracts(p2pSigExtensionsEnabled bool, nativeUpdateHistories map[string
cs.Oracle = oracle
cs.Contracts = append(cs.Contracts, oracle)
if p2pSigExtensionsEnabled {
if cfg.P2PSigExtensions {
notary := newNotary()
notary.GAS = gas
notary.NEO = neo
@ -111,12 +112,13 @@ func NewContracts(p2pSigExtensionsEnabled bool, nativeUpdateHistories map[string
cs.Contracts = append(cs.Contracts, notary)
}
setDefaultHistory := len(nativeUpdateHistories) == 0
setDefaultHistory := len(cfg.NativeUpdateHistories) == 0
for _, c := range cs.Contracts {
if setDefaultHistory {
nativeUpdateHistories[c.Metadata().Name] = []uint32{0}
var history = []uint32{0}
if !setDefaultHistory {
history = cfg.NativeUpdateHistories[c.Metadata().Name]
}
c.Metadata().NativeContract.UpdateHistory = nativeUpdateHistories[c.Metadata().Name]
c.Metadata().NativeContract.UpdateHistory = history
}
return cs
}

View file

@ -21,17 +21,18 @@ import (
type GAS struct {
nep17TokenNative
NEO *NEO
initialSupply int64
}
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{}
func newGAS(init int64) *GAS {
g := &GAS{initialSupply: init}
defer g.UpdateHash()
nep17 := newNEP17Native(nativenames.Gas, gasContractID)
@ -109,7 +110,7 @@ func (g *GAS) Initialize(ic *interop.Context) error {
if err != nil {
return err
}
g.mint(ic, h, big.NewInt(initialGAS*GASFactor), false)
g.mint(ic, h, big.NewInt(g.initialSupply), false)
return nil
}

View file

@ -4,13 +4,15 @@ import (
"fmt"
"testing"
"github.com/nspcc-dev/neo-go/pkg/config"
"github.com/nspcc-dev/neo-go/pkg/core/native/nativenames"
"github.com/stretchr/testify/require"
)
func TestNativenamesIsValid(t *testing.T) {
// test that all native names has been added to IsValid
contracts := NewContracts(true, map[string][]uint32{})
cfg := config.ProtocolConfiguration{P2PSigExtensions: true}
contracts := NewContracts(cfg)
for _, c := range contracts.Contracts {
require.True(t, nativenames.IsValid(c.Metadata().Name), fmt.Errorf("add %s to nativenames.IsValid(...)", c))
}

View file

@ -3,11 +3,12 @@ package main
import (
"testing"
"github.com/nspcc-dev/neo-go/pkg/config"
"github.com/nspcc-dev/neo-go/pkg/core/native"
"github.com/stretchr/testify/require"
)
func TestCompatibility(t *testing.T) {
cs := native.NewContracts(false, map[string][]uint32{})
cs := native.NewContracts(config.ProtocolConfiguration{})
require.Equal(t, cs.Ledger.ID, int32(ledgerContractID))
}