Skip to content

ACP-77: Implement ids.ID#Append #3518

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Nov 5, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions ids/id.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,22 @@ func (id ID) Prefix(prefixes ...uint64) ID {
return hashing.ComputeHash256Array(packer.Bytes)
}

// Append this id to create a more selective id.
//
// This is used to generate the ACP-77 validationIDs.
func (id ID) Append(suffixes ...uint32) ID {
packer := wrappers.Packer{
Bytes: make([]byte, IDLen+len(suffixes)*wrappers.IntLen),
}

packer.PackFixedBytes(id[:])
for _, suffix := range suffixes {
packer.PackInt(suffix)
}

return hashing.ComputeHash256Array(packer.Bytes)
}

// XOR this id and the provided id and return the resulting id.
//
// Note: this id is not modified.
Expand Down
86 changes: 86 additions & 0 deletions ids/id_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@ package ids
import (
"encoding/json"
"fmt"
"slices"
"testing"

"github.com/stretchr/testify/require"

"github.com/ava-labs/avalanchego/utils"
"github.com/ava-labs/avalanchego/utils/cb58"
"github.com/ava-labs/avalanchego/utils/hashing"
)

func TestID(t *testing.T) {
Expand All @@ -25,6 +27,90 @@ func TestID(t *testing.T) {
require.Equal(prefixed, id.Prefix(0))
}

func TestIDPrefix(t *testing.T) {
id := GenerateTestID()
tests := []struct {
name string
id ID
prefix []uint64
expectedPreimage []byte
}{
{
name: "empty prefix",
id: id,
prefix: []uint64{},
expectedPreimage: id[:],
},
{
name: "1 prefix",
id: id,
prefix: []uint64{1},
expectedPreimage: slices.Concat(
[]byte{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01},
id[:],
),
},
{
name: "multiple prefixes",
id: id,
prefix: []uint64{1, 256},
expectedPreimage: slices.Concat(
[]byte{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01},
[]byte{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00},
id[:],
),
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
expected := ID(hashing.ComputeHash256Array(test.expectedPreimage))
require.Equal(t, expected, test.id.Prefix(test.prefix...))
})
}
}

func TestIDAppend(t *testing.T) {
id := GenerateTestID()
tests := []struct {
name string
id ID
suffix []uint32
expectedPreimage []byte
}{
{
name: "empty suffix",
id: id,
suffix: []uint32{},
expectedPreimage: id[:],
},
{
name: "1 suffix",
id: id,
suffix: []uint32{1},
expectedPreimage: slices.Concat(
id[:],
[]byte{0x00, 0x00, 0x00, 0x01},
),
},
{
name: "multiple suffixes",
id: id,
suffix: []uint32{1, 256},
expectedPreimage: slices.Concat(
id[:],
[]byte{0x00, 0x00, 0x00, 0x01},
[]byte{0x00, 0x00, 0x01, 0x00},
),
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
expected := ID(hashing.ComputeHash256Array(test.expectedPreimage))
require.Equal(t, expected, test.id.Append(test.suffix...))
})
}
}

func TestIDXOR(t *testing.T) {
require := require.New(t)

Expand Down
Loading