Skip to content

Commit

Permalink
params: ProtocolVersionV0 struct helper for human-readable encoding call
Browse files Browse the repository at this point in the history
  • Loading branch information
protolambda committed Sep 14, 2023
1 parent 5e58eb5 commit 92eb9d4
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 19 deletions.
2 changes: 1 addition & 1 deletion eth/catalyst/superchain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ func TestSignalSuperchainV1Halt(t *testing.T) {
}
out, err := api.SignalSuperchainV1(&SuperchainSignal{
Recommended: params.OPStackSupport, // required version change should be enough
Required: params.ToProtocolVersion(build, majorSignal, minorSignal, patchSignal, preRelease),
Required: params.ProtocolVersionV0{Build: build, Major: majorSignal, Minor: minorSignal, Patch: patchSignal, PreRelease: preRelease}.Encode(),
})
if err != nil {
t.Fatalf("failed to process signal: %v", err)
Expand Down
19 changes: 12 additions & 7 deletions params/superchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"github.com/ethereum/go-ethereum/common"
)

var OPStackSupport = ToProtocolVersion([8]byte{}, 3, 1, 0, 1)
var OPStackSupport = ProtocolVersionV0{Build: [8]byte{}, Major: 3, Minor: 1, Patch: 0, PreRelease: 1}.Encode()

func init() {
for id, ch := range superchain.OPChains {
Expand Down Expand Up @@ -216,11 +216,16 @@ func (p ProtocolVersion) Compare(other ProtocolVersion) (cmp ProtocolVersionComp
return fn(aPreRelease, bPreRelease, AheadPrerelease, OutdatedPrerelease)
}

func ToProtocolVersion(build [8]byte, major, minor, patch, preRelease uint32) (out ProtocolVersion) {
copy(out[8:16], build[:])
binary.BigEndian.PutUint32(out[16:20], major)
binary.BigEndian.PutUint32(out[20:24], minor)
binary.BigEndian.PutUint32(out[24:28], patch)
binary.BigEndian.PutUint32(out[28:32], preRelease)
type ProtocolVersionV0 struct {
Build [8]byte
Major, Minor, Patch, PreRelease uint32
}

func (v ProtocolVersionV0) Encode() (out ProtocolVersion) {
copy(out[8:16], v.Build[:])
binary.BigEndian.PutUint32(out[16:20], v.Major)
binary.BigEndian.PutUint32(out[20:24], v.Minor)
binary.BigEndian.PutUint32(out[24:28], v.Patch)
binary.BigEndian.PutUint32(out[28:32], v.PreRelease)
return
}
22 changes: 11 additions & 11 deletions params/superchain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,9 @@ func TestProtocolVersion_Compare(t *testing.T) {
}
for i, tc := range testCases {
t.Run(fmt.Sprintf("case_%d", i), func(t *testing.T) {
a := ToProtocolVersion(tc.A.Build, tc.A.Major, tc.A.Minor, tc.A.Patch, tc.A.Prerelease)
a := ProtocolVersionV0{tc.A.Build, tc.A.Major, tc.A.Minor, tc.A.Patch, tc.A.Prerelease}.Encode()
a[0] = tc.A.VersionType
b := ToProtocolVersion(tc.B.Build, tc.B.Major, tc.B.Minor, tc.B.Patch, tc.B.Prerelease)
b := ProtocolVersionV0{tc.B.Build, tc.B.Major, tc.B.Minor, tc.B.Patch, tc.B.Prerelease}.Encode()
b[0] = tc.B.VersionType
cmp := a.Compare(b)
if cmp != tc.Cmp {
Expand All @@ -90,15 +90,15 @@ func TestProtocolVersion_String(t *testing.T) {
version ProtocolVersion
expected string
}{
{ToProtocolVersion([8]byte{}, 0, 0, 0, 0), "v0.0.0"},
{ToProtocolVersion([8]byte{}, 0, 0, 0, 1), "v0.0.0-1"},
{ToProtocolVersion([8]byte{}, 0, 0, 1, 0), "v0.0.1"},
{ToProtocolVersion([8]byte{}, 4, 3, 2, 1), "v4.3.2-1"},
{ToProtocolVersion([8]byte{}, 0, 100, 2, 0), "v0.100.2"},
{ToProtocolVersion([8]byte{'O', 'P', '-', 'm', 'o', 'd'}, 42, 0, 2, 1), "v42.0.2-1+OP-mod"},
{ToProtocolVersion([8]byte{'b', 'e', 't', 'a', '.', '1', '2', '3'}, 1, 0, 0, 0), "v1.0.0+beta.123"},
{ToProtocolVersion([8]byte{'a', 'b', 1}, 42, 0, 2, 0), "v42.0.2+0x6162010000000000"}, // do not render invalid alpha numeric
{ToProtocolVersion([8]byte{1, 2, 3, 4, 5, 6, 7, 8}, 42, 0, 2, 0), "v42.0.2+0x0102030405060708"},
{ProtocolVersionV0{[8]byte{}, 0, 0, 0, 0}.Encode(), "v0.0.0"},
{ProtocolVersionV0{[8]byte{}, 0, 0, 0, 1}.Encode(), "v0.0.0-1"},
{ProtocolVersionV0{[8]byte{}, 0, 0, 1, 0}.Encode(), "v0.0.1"},
{ProtocolVersionV0{[8]byte{}, 4, 3, 2, 1}.Encode(), "v4.3.2-1"},
{ProtocolVersionV0{[8]byte{}, 0, 100, 2, 0}.Encode(), "v0.100.2"},
{ProtocolVersionV0{[8]byte{'O', 'P', '-', 'm', 'o', 'd'}, 42, 0, 2, 1}.Encode(), "v42.0.2-1+OP-mod"},
{ProtocolVersionV0{[8]byte{'b', 'e', 't', 'a', '.', '1', '2', '3'}, 1, 0, 0, 0}.Encode(), "v1.0.0+beta.123"},
{ProtocolVersionV0{[8]byte{'a', 'b', 1}, 42, 0, 2, 0}.Encode(), "v42.0.2+0x6162010000000000"}, // do not render invalid alpha numeric
{ProtocolVersionV0{[8]byte{1, 2, 3, 4, 5, 6, 7, 8}, 42, 0, 2, 0}.Encode(), "v42.0.2+0x0102030405060708"},
}
for _, tc := range testCases {
t.Run(tc.expected, func(t *testing.T) {
Expand Down

0 comments on commit 92eb9d4

Please sign in to comment.