Skip to content

Commit

Permalink
chain: expose From and To for host announcements
Browse files Browse the repository at this point in the history
  • Loading branch information
n8maninger committed Sep 19, 2024
1 parent 9cc63ee commit 521b10a
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 14 deletions.
34 changes: 22 additions & 12 deletions chain/hostannouncement.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,42 +15,51 @@ var specifierHostAnnouncement = types.NewSpecifier("HostAnnouncement")
// address. Announcements may be made via arbitrary data (in a v1 transaction)
// or via attestation (in a v2 transaction).
type HostAnnouncement struct {
NetAddress string
PublicKey types.PublicKey `json:"publicKey"`
NetAddress string `json:"netAddress"`
}

// ToAttestation encodes a host announcement as an attestation.
func (ha HostAnnouncement) ToAttestation(cs consensus.State, sk types.PrivateKey) types.Attestation {
if ha.PublicKey != sk.PublicKey() {
panic("key mismatch") // developer error
}
a := types.Attestation{
PublicKey: sk.PublicKey(),
PublicKey: ha.PublicKey,
Key: attestationHostAnnouncement,
Value: []byte(ha.NetAddress),
}
a.Signature = sk.SignHash(cs.AttestationSigHash(a))
return a
}

func (ha *HostAnnouncement) fromAttestation(a types.Attestation) bool {
func (ha *HostAnnouncement) FromAttestation(a types.Attestation) bool {

Check failure on line 36 in chain/hostannouncement.go

View workflow job for this annotation

GitHub Actions / test / test (1.22, ubuntu-latest)

exported: exported method HostAnnouncement.FromAttestation should have comment or be unexported (revive)
if a.Key != attestationHostAnnouncement {
return false
}
ha.PublicKey = a.PublicKey
ha.NetAddress = string(a.Value)
return true
}

// ToArbitraryData encodes a host announcement as arbitrary data.
func (ha HostAnnouncement) ToArbitraryData(sk types.PrivateKey) []byte {
if ha.PublicKey != sk.PublicKey() {
panic("key mismatch") // developer error
}

buf := new(bytes.Buffer)
e := types.NewEncoder(buf)
specifierHostAnnouncement.EncodeTo(e)
e.WriteString(ha.NetAddress)
sk.PublicKey().UnlockKey().EncodeTo(e)
ha.PublicKey.UnlockKey().EncodeTo(e)
e.Flush()
sk.SignHash(types.HashBytes(buf.Bytes())).EncodeTo(e)
e.Flush()
return buf.Bytes()
}

func (ha *HostAnnouncement) fromArbitraryData(arb []byte) (types.PublicKey, bool) {
func (ha *HostAnnouncement) FromArbitraryData(arb []byte) bool {

Check failure on line 62 in chain/hostannouncement.go

View workflow job for this annotation

GitHub Actions / test / test (1.22, ubuntu-latest)

exported: exported method HostAnnouncement.FromArbitraryData should have comment or be unexported (revive)
var s types.Specifier
var uk types.UnlockKey
var sig types.Signature
Expand All @@ -65,27 +74,28 @@ func (ha *HostAnnouncement) fromArbitraryData(arb []byte) (types.PublicKey, bool
len(uk.Key) < 32 ||
len(arb) < len(sig) ||
!types.PublicKey(uk.Key).VerifyHash(types.HashBytes(arb[:len(arb)-len(sig)]), sig) {
return types.PublicKey{}, false
return false
}
ha.NetAddress = addr
return types.PublicKey(uk.Key), true
ha.PublicKey = types.PublicKey(uk.Key)
return true
}

// ForEachHostAnnouncement calls fn on each host announcement in a block.
func ForEachHostAnnouncement(b types.Block, fn func(types.PublicKey, HostAnnouncement)) {
func ForEachHostAnnouncement(b types.Block, fn func(HostAnnouncement)) {
for _, txn := range b.Transactions {
for _, arb := range txn.ArbitraryData {
var ha HostAnnouncement
if pk, ok := ha.fromArbitraryData(arb); ok {
fn(pk, ha)
if ha.FromArbitraryData(arb) {
fn(ha)
}
}
}
for _, txn := range b.V2Transactions() {
for _, a := range txn.Attestations {
var ha HostAnnouncement
if ha.fromAttestation(a) {
fn(a.PublicKey, ha)
if ha.FromAttestation(a) {
fn(ha)
}
}
}
Expand Down
5 changes: 3 additions & 2 deletions chain/hostannouncement_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ func TestForEachHostAnnouncement(t *testing.T) {
sk := types.GeneratePrivateKey()
ha := HostAnnouncement{
NetAddress: "foo.bar:1234",
PublicKey: sk.PublicKey(),
}
b := types.Block{
Transactions: []types.Transaction{
Expand All @@ -22,8 +23,8 @@ func TestForEachHostAnnouncement(t *testing.T) {
},
},
}
ForEachHostAnnouncement(b, func(pk types.PublicKey, a HostAnnouncement) {
if pk != sk.PublicKey() {
ForEachHostAnnouncement(b, func(a HostAnnouncement) {
if a.PublicKey != sk.PublicKey() {
t.Error("pubkey mismatch")
} else if a.NetAddress != ha.NetAddress {
t.Error("address mismatch:", a, ha)
Expand Down

0 comments on commit 521b10a

Please sign in to comment.