Merge pull request #2161 from nspcc-dev/rpc-get-version

rpc: return protocol parameters in `getversion`, fix #2160
This commit is contained in:
Roman Khimov 2021-09-13 19:10:43 +03:00 committed by GitHub
commit 621478296c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 53 additions and 4 deletions

View file

@ -119,8 +119,12 @@ func (c *Client) Init() error {
if err != nil {
return fmt.Errorf("failed to get network magic: %w", err)
}
c.network = version.Protocol.Network
c.stateRootInHeader = version.Protocol.StateRootInHeader
if version.Protocol.MillisecondsPerBlock == 0 {
c.network = version.Magic
c.stateRootInHeader = version.StateRootInHeader
}
neoContractHash, err := c.GetContractStateByAddressOrName(nativenames.Neo)
if err != nil {
return fmt.Errorf("failed to get NEO contract scripthash: %w", err)

View file

@ -1,16 +1,37 @@
package result
import "github.com/nspcc-dev/neo-go/pkg/config/netmode"
import (
"github.com/nspcc-dev/neo-go/pkg/config/netmode"
"github.com/nspcc-dev/neo-go/pkg/encoding/fixedn"
)
type (
// Version model used for reporting server version
// info.
Version struct {
// Magic contains network magic.
// Deprecated: use Protocol.StateRootInHeader instead
Magic netmode.Magic `json:"network"`
TCPPort uint16 `json:"tcpport"`
WSPort uint16 `json:"wsport,omitempty"`
Nonce uint32 `json:"nonce"`
UserAgent string `json:"useragent"`
Protocol Protocol `json:"protocol"`
// StateRootInHeader is true if state root is contained in block header.
// Deprecated: use Protocol.StateRootInHeader instead
StateRootInHeader bool `json:"staterootinheader,omitempty"`
}
// Protocol represents network-dependent parameters.
Protocol struct {
AddressVersion byte `json:"addressversion"`
Network netmode.Magic `json:"network"`
MillisecondsPerBlock int `json:"msperblock"`
MaxTraceableBlocks uint32 `json:"maxtraceableblocks"`
MaxValidUntilBlockIncrement uint32 `json:"maxvaliduntilblockincrement"`
MaxTransactionsPerBlock uint16 `json:"maxtransactionsperblock"`
MemoryPoolMaxTransactions int `json:"memorypoolmaxtransactions"`
InitialGasDistribution fixedn.Fixed8 `json:"initialgasdistribution"`
// StateRootInHeader is true if state root is contained in block header.
StateRootInHeader bool `json:"staterootinheader,omitempty"`
}

View file

@ -520,12 +520,25 @@ func (s *Server) getVersion(_ request.Params) (interface{}, *response.Error) {
if err != nil {
return nil, response.NewInternalServerError("Cannot fetch tcp port", err)
}
cfg := s.chain.GetConfig()
return result.Version{
Magic: s.network,
TCPPort: port,
Nonce: s.coreServer.ID(),
UserAgent: s.coreServer.UserAgent,
StateRootInHeader: s.chain.GetConfig().StateRootInHeader,
StateRootInHeader: cfg.StateRootInHeader,
Protocol: result.Protocol{
AddressVersion: address.NEO3Prefix,
Network: cfg.Magic,
MillisecondsPerBlock: cfg.SecondsPerBlock * 1000,
MaxTraceableBlocks: cfg.MaxTraceableBlocks,
MaxValidUntilBlockIncrement: cfg.MaxValidUntilBlockIncrement,
MaxTransactionsPerBlock: cfg.MaxTransactionsPerBlock,
MemoryPoolMaxTransactions: cfg.MemPoolSize,
InitialGasDistribution: cfg.InitialGASSupply,
StateRootInHeader: cfg.StateRootInHeader,
},
}, nil
}

View file

@ -690,6 +690,17 @@ var rpcTestCases = map[string][]rpcTestCase{
resp, ok := ver.(*result.Version)
require.True(t, ok)
require.Equal(t, "/NEO-GO:/", resp.UserAgent)
cfg := e.chain.GetConfig()
require.EqualValues(t, address.NEO3Prefix, resp.Protocol.AddressVersion)
require.EqualValues(t, cfg.Magic, resp.Protocol.Network)
require.EqualValues(t, cfg.SecondsPerBlock*1000, resp.Protocol.MillisecondsPerBlock)
require.EqualValues(t, cfg.MaxTraceableBlocks, resp.Protocol.MaxTraceableBlocks)
require.EqualValues(t, cfg.MaxValidUntilBlockIncrement, resp.Protocol.MaxValidUntilBlockIncrement)
require.EqualValues(t, cfg.MaxTransactionsPerBlock, resp.Protocol.MaxTransactionsPerBlock)
require.EqualValues(t, cfg.MemPoolSize, resp.Protocol.MemoryPoolMaxTransactions)
require.EqualValues(t, cfg.InitialGASSupply, resp.Protocol.InitialGasDistribution)
require.EqualValues(t, false, resp.Protocol.StateRootInHeader)
},
},
},