Skip to content

Commit

Permalink
rhp4: implement rhp4 server and basic client
Browse files Browse the repository at this point in the history
  • Loading branch information
n8maninger committed Sep 13, 2024
1 parent 9cc63ee commit aee1098
Show file tree
Hide file tree
Showing 13 changed files with 3,717 additions and 32 deletions.
12 changes: 6 additions & 6 deletions chain/chain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,8 +183,8 @@ func TestV2Attestations(t *testing.T) {
ms.Sync(t, cm)

sk := types.GeneratePrivateKey()
ann := chain.HostAnnouncement{
NetAddress: "foo.bar:1234",
ann := chain.V2HostAnnouncement{
chain.NetAddress{Address: "foo.bar:1234", Protocol: "tcp"},
}
se := ms.SpendableElement(t)
txn := types.V2Transaction{
Expand Down Expand Up @@ -232,8 +232,8 @@ func TestV2Attestations(t *testing.T) {
ms.Sync(t, cm)

sk := types.GeneratePrivateKey()
ann := chain.HostAnnouncement{
NetAddress: "foo.bar:1234",
ann := chain.V2HostAnnouncement{
chain.NetAddress{Address: "foo.bar:1234", Protocol: "tcp"},
}
txn := types.V2Transaction{
ArbitraryData: frand.Bytes(16),
Expand Down Expand Up @@ -276,8 +276,8 @@ func TestV2Attestations(t *testing.T) {
ms.Sync(t, cm)

sk := types.GeneratePrivateKey()
ann := chain.HostAnnouncement{
NetAddress: "foo.bar:1234",
ann := chain.V2HostAnnouncement{
chain.NetAddress{Address: "foo.bar:1234", Protocol: "tcp"},
}
se := ms.SpendableElement(t)
minerFee := types.Siacoins(1)
Expand Down
57 changes: 45 additions & 12 deletions chain/hostannouncement.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package chain

import (
"bytes"
"errors"

"go.sia.tech/core/consensus"
"go.sia.tech/core/types"
Expand All @@ -12,29 +13,57 @@ const attestationHostAnnouncement = "HostAnnouncement"
var specifierHostAnnouncement = types.NewSpecifier("HostAnnouncement")

// A HostAnnouncement represents a signed announcement of a host's network
// address. Announcements may be made via arbitrary data (in a v1 transaction)
// or via attestation (in a v2 transaction).
type HostAnnouncement struct {
NetAddress string
// address. Announcements may be made via arbitrary data
type (
HostAnnouncement struct {
NetAddress string
}

// A NetAddress is a pair of protocol and address that a host may be reached on
NetAddress struct {
Protocol string `json:"protocol"`
Address string `json:"address"`
}

// A V2HostAnnouncement lists all the network addresses a host may be reached on
V2HostAnnouncement []NetAddress
)

// EncodeTo implements types.EncoderTo.
func (na NetAddress) EncodeTo(e *types.Encoder) {
e.WriteString(na.Protocol)
e.WriteString(na.Address)
}

// DecodeFrom implements types.DecoderFrom.
func (na *NetAddress) DecodeFrom(d *types.Decoder) {
na.Protocol = d.ReadString()
na.Address = d.ReadString()
}

// ToAttestation encodes a host announcement as an attestation.
func (ha HostAnnouncement) ToAttestation(cs consensus.State, sk types.PrivateKey) types.Attestation {
func (ha V2HostAnnouncement) ToAttestation(cs consensus.State, sk types.PrivateKey) types.Attestation {
buf := bytes.NewBuffer(nil)
e := types.NewEncoder(buf)
types.EncodeSlice(e, ha)
e.Flush()
a := types.Attestation{
PublicKey: sk.PublicKey(),
Key: attestationHostAnnouncement,
Value: []byte(ha.NetAddress),
Value: buf.Bytes(),
}
a.Signature = sk.SignHash(cs.AttestationSigHash(a))
return a
}

func (ha *HostAnnouncement) fromAttestation(a types.Attestation) bool {
// FromAttestation decodes a host announcement from an attestation.
func (ha *V2HostAnnouncement) FromAttestation(a types.Attestation) error {
if a.Key != attestationHostAnnouncement {
return false
return errors.New("not a host announcement")
}
ha.NetAddress = string(a.Value)
return true
d := types.NewBufDecoder(a.Value)
types.DecodeSlice(d, (*[]NetAddress)(ha))
return d.Err()
}

// ToArbitraryData encodes a host announcement as arbitrary data.
Expand Down Expand Up @@ -81,10 +110,14 @@ func ForEachHostAnnouncement(b types.Block, fn func(types.PublicKey, HostAnnounc
}
}
}
}

// ForEachV2HostAnnouncement calls fn on each v2 host announcement in a block.
func ForEachV2HostAnnouncement(b types.Block, fn func(types.PublicKey, []NetAddress)) {
for _, txn := range b.V2Transactions() {
for _, a := range txn.Attestations {
var ha HostAnnouncement
if ha.fromAttestation(a) {
var ha V2HostAnnouncement
if err := ha.FromAttestation(a); err == nil {
fn(a.PublicKey, ha)
}
}
Expand Down
30 changes: 27 additions & 3 deletions chain/hostannouncement_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,41 @@ func TestForEachHostAnnouncement(t *testing.T) {
Transactions: []types.Transaction{
{ArbitraryData: [][]byte{ha.ToArbitraryData(sk)}},
},
}
ForEachHostAnnouncement(b, func(pk types.PublicKey, a HostAnnouncement) {
if pk != sk.PublicKey() {
t.Error("pubkey mismatch")
} else if a.NetAddress != ha.NetAddress {
t.Error("address mismatch:", a, ha)
}
})
}

func TestForEachV2HostAnnouncement(t *testing.T) {
sk := types.GeneratePrivateKey()
ha := V2HostAnnouncement([]NetAddress{
{Protocol: "tcp", Address: "foo.bar:1234"},
{Protocol: "tcp6", Address: "baz.qux:5678"},
{Protocol: "webtransport", Address: "quux.corge:91011"},
})
b := types.Block{
V2: &types.V2BlockData{
Transactions: []types.V2Transaction{
{Attestations: []types.Attestation{ha.ToAttestation(consensus.State{}, sk)}},
},
},
}
ForEachHostAnnouncement(b, func(pk types.PublicKey, a HostAnnouncement) {
ForEachV2HostAnnouncement(b, func(pk types.PublicKey, addresses []NetAddress) {
if pk != sk.PublicKey() {
t.Error("pubkey mismatch")
} else if a.NetAddress != ha.NetAddress {
t.Error("address mismatch:", a, ha)
} else if len(addresses) != len(ha) {
t.Error("length mismatch")
} else {
for i := range addresses {
if addresses[i] != ha[i] {
t.Error("address mismatch:", addresses[i], ha[i])
}
}
}
})
}
2 changes: 1 addition & 1 deletion chain/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -941,7 +941,7 @@ func (m *Manager) UnconfirmedParents(txn types.Transaction) []types.Transaction

// V2TransactionSet returns the full transaction set and basis necessary for
// broadcasting a transaction. If the provided basis does not match the current
// tip the transaction will be updated. The transaction set includes the parents
// tip, the transaction will be updated. The transaction set includes the parents
// and the transaction itself in an order valid for broadcasting.
func (m *Manager) V2TransactionSet(basis types.ChainIndex, txn types.V2Transaction) (types.ChainIndex, []types.V2Transaction, error) {
m.mu.Lock()
Expand Down
8 changes: 3 additions & 5 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,20 +1,18 @@
module go.sia.tech/coreutils

go 1.22

toolchain go1.23.0
go 1.23.0

require (
go.etcd.io/bbolt v1.3.11
go.sia.tech/core v0.4.6
go.sia.tech/core v0.4.7-0.20240913031448-c46d83451426
go.sia.tech/mux v1.2.0
go.uber.org/zap v1.27.0
golang.org/x/crypto v0.27.0
lukechampine.com/frand v1.4.2
)

require (
github.com/aead/chacha20 v0.0.0-20180709150244-8b13a72661da // indirect
go.sia.tech/mux v1.2.0 // indirect
go.uber.org/multierr v1.11.0 // indirect
golang.org/x/sys v0.25.0 // indirect
)
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKs
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
go.etcd.io/bbolt v1.3.11 h1:yGEzV1wPz2yVCLsD8ZAiGHhHVlczyC9d1rP43/VCRJ0=
go.etcd.io/bbolt v1.3.11/go.mod h1:dksAq7YMXoljX0xu6VF5DMZGbhYYoLUalEiSySYAS4I=
go.sia.tech/core v0.4.6 h1:QLm97a7GWBonfnMEOokqWRAqsWCUPL7kzo6k3Adwx8E=
go.sia.tech/core v0.4.6/go.mod h1:Zuq0Tn2aIXJyO0bjGu8cMeVWe+vwQnUfZhG1LCmjD5c=
go.sia.tech/core v0.4.7-0.20240913031448-c46d83451426 h1:FW7aPkAlee5gEmIHwQYKD7fTylmaEm+4V9SyHNkdzGE=
go.sia.tech/core v0.4.7-0.20240913031448-c46d83451426/go.mod h1:S7IRFqUy/vo2+oPtDq/o4rlAegNyoYQ7TWqsulE9R+w=
go.sia.tech/mux v1.2.0 h1:ofa1Us9mdymBbGMY2XH/lSpY8itFsKIo/Aq8zwe+GHU=
go.sia.tech/mux v1.2.0/go.mod h1:Yyo6wZelOYTyvrHmJZ6aQfRoer3o4xyKQ4NmQLJrBSo=
go.uber.org/goleak v1.3.0 h1:2K3zAYmnTNqV73imy9J1T3WC+gmCePx2hEGkimedGto=
Expand Down
Loading

0 comments on commit aee1098

Please sign in to comment.