Skip to content

Commit 44dadc5

Browse files
committed
Ignore or add digits to block version string as neccessary (#739)
to account for non-sem-ver complaint starknet versions
1 parent 541ffe1 commit 44dadc5

File tree

2 files changed

+20
-5
lines changed

2 files changed

+20
-5
lines changed

blockchain/blockchain.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"bytes"
55
"encoding/binary"
66
"errors"
7+
"strings"
78

89
"github.com/Masterminds/semver/v3"
910
"github.com/NethermindEth/juno/core"
@@ -41,7 +42,13 @@ func checkBlockVersion(protocolVersion string) error {
4142
return nil
4243
}
4344

44-
blockVer, err := semver.NewVersion(protocolVersion)
45+
sep := "."
46+
digits := strings.Split(protocolVersion, sep)
47+
// pad with 3 zeros in case version has less than 3 digits
48+
digits = append(digits, []string{"0", "0", "0"}...)
49+
50+
// get first 3 digits only
51+
blockVer, err := semver.NewVersion(strings.Join(digits[:3], sep))
4552
if err != nil {
4653
return err
4754
}

blockchain/blockchain_test.go

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55
"fmt"
66
"testing"
77

8-
"github.com/Masterminds/semver/v3"
98
"github.com/NethermindEth/juno/blockchain"
109
"github.com/NethermindEth/juno/clients/feeder"
1110
"github.com/NethermindEth/juno/core"
@@ -141,10 +140,19 @@ func TestVerifyBlock(t *testing.T) {
141140
require.Error(t, chain.Store(mainnetBlock0, mainnetStateUpdate0, nil))
142141
})
143142

144-
t.Run("error if version is unsupported", func(t *testing.T) {
143+
t.Run("needs padding", func(t *testing.T) {
144+
mainnetBlock0.ProtocolVersion = "99.0" // should be padded to "99.0.0"
145+
require.EqualError(t, chain.Store(mainnetBlock0, mainnetStateUpdate0, nil), "unsupported block version")
146+
})
147+
148+
t.Run("needs truncating", func(t *testing.T) {
149+
mainnetBlock0.ProtocolVersion = "99.0.0.0" // last 0 digit should be ignored
150+
require.EqualError(t, chain.Store(mainnetBlock0, mainnetStateUpdate0, nil), "unsupported block version")
151+
})
152+
153+
t.Run("greater than supportedStarknetVersion", func(t *testing.T) {
145154
mainnetBlock0.ProtocolVersion = "99.0.0"
146-
semver.MustParse(mainnetBlock0.ProtocolVersion)
147-
require.Error(t, chain.Store(mainnetBlock0, mainnetStateUpdate0, nil))
155+
require.EqualError(t, chain.Store(mainnetBlock0, mainnetStateUpdate0, nil), "unsupported block version")
148156
})
149157

150158
t.Run("no error with no version string", func(t *testing.T) {

0 commit comments

Comments
 (0)