Skip to content

Commit

Permalink
Block's version verifies only the major version (#2285)
Browse files Browse the repository at this point in the history
  • Loading branch information
pnowosie authored Nov 25, 2024
1 parent c55d90f commit 4c43251
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 2 deletions.
14 changes: 12 additions & 2 deletions blockchain/blockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ type Reader interface {

var (
ErrParentDoesNotMatchHead = errors.New("block's parent hash does not match head block hash")
supportedStarknetVersion = semver.MustParse("0.13.3")
SupportedStarknetVersion = semver.MustParse("0.13.3")
)

func checkBlockVersion(protocolVersion string) error {
Expand All @@ -62,13 +62,23 @@ func checkBlockVersion(protocolVersion string) error {
return err
}

if blockVer.GreaterThan(supportedStarknetVersion) {
// We ignore changes in patch part of the version
blockVerMM, supportedVerMM := copyWithoutPatch(blockVer), copyWithoutPatch(SupportedStarknetVersion)
if blockVerMM.GreaterThan(supportedVerMM) {
return errors.New("unsupported block version")
}

return nil
}

func copyWithoutPatch(v *semver.Version) *semver.Version {
if v == nil {
return nil
}

return semver.New(v.Major(), v.Minor(), 0, v.Prerelease(), v.Metadata())
}

var _ Reader = (*Blockchain)(nil)

// Blockchain is responsible for keeping track of all things related to the Starknet blockchain
Expand Down
15 changes: 15 additions & 0 deletions blockchain/blockchain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,21 @@ func TestVerifyBlock(t *testing.T) {
require.EqualError(t, chain.Store(mainnetBlock0, &emptyCommitments, mainnetStateUpdate0, nil), "unsupported block version")
})

t.Run("mismatch at patch version is ignored", func(t *testing.T) {
mainnetBlock0.ProtocolVersion = blockchain.SupportedStarknetVersion.IncPatch().String()
assert.NoError(t, chain.VerifyBlock(mainnetBlock0))
})

t.Run("error if mismatch at minor version", func(t *testing.T) {
mainnetBlock0.ProtocolVersion = blockchain.SupportedStarknetVersion.IncMinor().String()
assert.EqualError(t, chain.VerifyBlock(mainnetBlock0), "unsupported block version")
})

t.Run("error if mismatch at minor version", func(t *testing.T) {
mainnetBlock0.ProtocolVersion = blockchain.SupportedStarknetVersion.IncMajor().String()
assert.EqualError(t, chain.VerifyBlock(mainnetBlock0), "unsupported block version")
})

t.Run("no error with no version string", func(t *testing.T) {
mainnetBlock0.ProtocolVersion = ""
require.NoError(t, chain.Store(mainnetBlock0, &emptyCommitments, mainnetStateUpdate0, nil))
Expand Down
1 change: 1 addition & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,7 @@ github.com/onsi/gomega v1.34.1/go.mod h1:kU1QgUvBDLXBJq618Xvm2LUX6rSAfRaFRTcdOeD
github.com/opencontainers/runtime-spec v1.0.2/go.mod h1:jwyrGlmzljRJv/Fgzds9SsS/C5hL+LL3ko9hs6T5lQ0=
github.com/opencontainers/runtime-spec v1.2.0 h1:z97+pHb3uELt/yiAWD691HNHQIF07bE7dzrbT927iTk=
github.com/opencontainers/runtime-spec v1.2.0/go.mod h1:jwyrGlmzljRJv/Fgzds9SsS/C5hL+LL3ko9hs6T5lQ0=
github.com/opentracing/opentracing-go v1.2.0/go.mod h1:GxEUsuufX4nBwe+T+Wl9TAgYrxe9dPLANfrWvHYVTgc=
github.com/openzipkin/zipkin-go v0.1.1/go.mod h1:NtoC/o8u3JlF1lSlyPNswIbeQH9bJTmOf0Erfk+hxe8=
github.com/pascaldekloe/name v1.0.0 h1:n7LKFgHixETzxpRv2R77YgPUFo85QHGZKrdaYm7eY5U=
github.com/pascaldekloe/name v1.0.0/go.mod h1:Z//MfYJnH4jVpQ9wkclwu2I2MkHmXTlT9wR5UZScttM=
Expand Down

0 comments on commit 4c43251

Please sign in to comment.