diff --git a/pkg/neorpc/result/version.go b/pkg/neorpc/result/version.go index deb4f33d40..0b97686dce 100644 --- a/pkg/neorpc/result/version.go +++ b/pkg/neorpc/result/version.go @@ -2,7 +2,9 @@ package result import ( "encoding/json" + "fmt" + "github.com/nspcc-dev/neo-go/pkg/config" "github.com/nspcc-dev/neo-go/pkg/config/netmode" "github.com/nspcc-dev/neo-go/pkg/encoding/fixedn" ) @@ -18,6 +20,12 @@ type ( Protocol Protocol `json:"protocol"` } + // Hardfork represents network hardfork. + Hardfork struct { + Name config.Hardfork + Height uint32 + } + // Protocol represents network-dependent parameters. Protocol struct { AddressVersion byte @@ -29,6 +37,8 @@ type ( MemoryPoolMaxTransactions int ValidatorsCount byte InitialGasDistribution fixedn.Fixed8 + // Hardforks is the list of network hardforks sorted by name. + Hardforks []Hardfork // Below are NeoGo-specific extensions to the protocol that are // returned by the server in case they're enabled. @@ -54,16 +64,30 @@ type ( MemoryPoolMaxTransactions int `json:"memorypoolmaxtransactions"` ValidatorsCount byte `json:"validatorscount"` InitialGasDistribution int64 `json:"initialgasdistribution"` + Hardforks []hardforkAux `json:"hardforks"` CommitteeHistory map[uint32]uint32 `json:"committeehistory,omitempty"` P2PSigExtensions bool `json:"p2psigextensions,omitempty"` StateRootInHeader bool `json:"staterootinheader,omitempty"` ValidatorsHistory map[uint32]uint32 `json:"validatorshistory,omitempty"` } + + // hardforkAux is an auxiliary struct used for Hardfork JSON marshalling. + hardforkAux struct { + Name string `json:"name"` + Height uint32 `json:"blockheight"` + } ) // MarshalJSON implements the JSON marshaler interface. func (p Protocol) MarshalJSON() ([]byte, error) { + hf := make([]hardforkAux, len(p.Hardforks)) + for i := range hf { + hf[i] = hardforkAux{ + Name: p.Hardforks[i].Name.String(), + Height: p.Hardforks[i].Height, + } + } aux := protocolMarshallerAux{ AddressVersion: p.AddressVersion, Network: p.Network, @@ -74,6 +98,7 @@ func (p Protocol) MarshalJSON() ([]byte, error) { MemoryPoolMaxTransactions: p.MemoryPoolMaxTransactions, ValidatorsCount: p.ValidatorsCount, InitialGasDistribution: int64(p.InitialGasDistribution), + Hardforks: hf, CommitteeHistory: p.CommitteeHistory, P2PSigExtensions: p.P2PSigExtensions, @@ -104,5 +129,24 @@ func (p *Protocol) UnmarshalJSON(data []byte) error { p.ValidatorsHistory = aux.ValidatorsHistory p.InitialGasDistribution = fixedn.Fixed8(aux.InitialGasDistribution) + // Filter out unknown hardforks. + for _, hf := range aux.Hardforks { + if !config.IsHardforkValid(hf.Name) { + return fmt.Errorf("unexpected hardfork: %s", hf.Name) + } + } + // Keep them sorted. + p.Hardforks = make([]Hardfork, 0, len(aux.Hardforks)) + for _, cfgHf := range config.Hardforks { + for _, auxHf := range aux.Hardforks { + if auxHf.Name == cfgHf.String() { + p.Hardforks = append(p.Hardforks, Hardfork{ + Name: cfgHf, + Height: auxHf.Height, + }) + } + } + } + return nil } diff --git a/pkg/neorpc/result/version_test.go b/pkg/neorpc/result/version_test.go index d0388cca0f..9d4a5b4c06 100644 --- a/pkg/neorpc/result/version_test.go +++ b/pkg/neorpc/result/version_test.go @@ -4,6 +4,7 @@ import ( "encoding/json" "testing" + "github.com/nspcc-dev/neo-go/pkg/config" "github.com/nspcc-dev/neo-go/pkg/encoding/fixedn" "github.com/stretchr/testify/require" ) @@ -38,7 +39,8 @@ func TestVersion_MarshalUnmarshalJSON(t *testing.T) { "memorypoolmaxtransactions": 50000, "msperblock": 15000, "network": 860833102, - "validatorscount": 7 + "validatorscount": 7, + "hardforks": [{"name": "Aspidochelone", "blockheight": 123}, {"name": "Basilisk", "blockheight": 1234}] }, "tcpport": 10333, "useragent": "/NEO-GO:0.98.6/", @@ -55,7 +57,8 @@ func TestVersion_MarshalUnmarshalJSON(t *testing.T) { "memorypoolmaxtransactions": 50000, "msperblock": 15000, "network": 860833102, - "validatorscount": 7 + "validatorscount": 7, + "hardforks": [{"name": "Aspidochelone", "blockheight": 123}, {"name": "Basilisk", "blockheight": 1234}] }, "tcpport": 10333, "useragent": "/Neo:3.1.0/", @@ -78,6 +81,7 @@ func TestVersion_MarshalUnmarshalJSON(t *testing.T) { // Unmarshalled InitialGasDistribution should always be a valid Fixed8 for both old and new clients. InitialGasDistribution: fixedn.Fixed8FromInt64(52000000), StateRootInHeader: false, + Hardforks: []Hardfork{{Name: config.HFAspidochelone, Height: 123}, {Name: config.HFBasilisk, Height: 1234}}, }, } t.Run("MarshalJSON", func(t *testing.T) { diff --git a/pkg/services/rpcsrv/client_test.go b/pkg/services/rpcsrv/client_test.go index d65274a2ab..fcccbe1b05 100644 --- a/pkg/services/rpcsrv/client_test.go +++ b/pkg/services/rpcsrv/client_test.go @@ -2356,3 +2356,23 @@ func TestClient_GetStorageHistoric(t *testing.T) { _, err = c.GetStorageByHashHistoric(earlyRoot.Root, h, key) require.ErrorIs(t, neorpc.ErrUnknownStorageItem, err) } + +func TestClient_GetVersion_Hardforks(t *testing.T) { + chain, rpcSrv, httpSrv := initServerWithInMemoryChain(t) + defer chain.Close() + defer rpcSrv.Shutdown() + + c, err := rpcclient.New(context.Background(), httpSrv.URL, rpcclient.Options{}) + require.NoError(t, err) + require.NoError(t, c.Init()) + + v, err := c.GetVersion() + require.NoError(t, err) + expected := []result.Hardfork{ + { + Name: config.HFAspidochelone, + Height: 25, + }, + } + require.Equal(t, expected, v.Protocol.Hardforks) +} diff --git a/pkg/services/rpcsrv/server.go b/pkg/services/rpcsrv/server.go index 3037d99c2f..0b976c0fc8 100644 --- a/pkg/services/rpcsrv/server.go +++ b/pkg/services/rpcsrv/server.go @@ -839,6 +839,17 @@ func (s *Server) getVersion(_ params.Params) (any, *neorpc.Error) { } cfg := s.chain.GetConfig() + hfs := make([]result.Hardfork, 0, len(cfg.Hardforks)) + for _, cfgHf := range config.Hardforks { + height, ok := cfg.Hardforks[cfgHf.String()] + if !ok { + continue + } + hfs = append(hfs, result.Hardfork{ + Name: cfgHf, + Height: height, + }) + } return &result.Version{ TCPPort: port, Nonce: s.coreServer.ID(), @@ -853,6 +864,7 @@ func (s *Server) getVersion(_ params.Params) (any, *neorpc.Error) { MemoryPoolMaxTransactions: cfg.MemPoolSize, ValidatorsCount: byte(cfg.GetNumOfCNs(s.chain.BlockHeight())), InitialGasDistribution: cfg.InitialGASSupply, + Hardforks: hfs, CommitteeHistory: cfg.CommitteeHistory, P2PSigExtensions: cfg.P2PSigExtensions,