Skip to content
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

Add host announcements to transaction type #99

Merged
merged 3 commits into from
Sep 24, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
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
36 changes: 21 additions & 15 deletions explorer/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,26 @@ type ChainUpdate interface {
ForEachV2FileContractElement(func(fce types.V2FileContractElement, created bool, rev *types.V2FileContractElement, res types.V2FileContractResolutionType))
}

// ParseHostAnnouncements parses host announcements from transaction arbitrary data.
func ParseHostAnnouncements(arbitraryData [][]byte) (result []HostAnnouncement) {
for _, arb := range arbitraryData {
var prefix types.Specifier
var uk types.UnlockKey
d := types.NewBufDecoder(arb)
prefix.DecodeFrom(d)
netAddress := d.ReadString()
uk.DecodeFrom(d)
if d.Err() == nil && prefix == SpecifierAnnouncement &&
uk.Algorithm == types.SpecifierEd25519 && len(uk.Key) == len(types.PublicKey{}) {
result = append(result, HostAnnouncement{
PublicKey: *(*types.PublicKey)(uk.Key),
NetAddress: netAddress,
})
}
}
return
}

// AppliedEvents extracts a list of relevant events from a chain update.
func AppliedEvents(cs consensus.State, b types.Block, cu ChainUpdate) []Event {
var events []Event
Expand Down Expand Up @@ -193,21 +213,7 @@ func AppliedEvents(cs consensus.State, b types.Block, cu ChainUpdate) []Event {
relevant := relevantTxn(txn)

var e EventTransaction
for _, arb := range txn.ArbitraryData {
var prefix types.Specifier
var uk types.UnlockKey
d := types.NewBufDecoder(arb)
prefix.DecodeFrom(d)
netAddress := d.ReadString()
uk.DecodeFrom(d)
if d.Err() == nil && prefix == SpecifierAnnouncement &&
uk.Algorithm == types.SpecifierEd25519 && len(uk.Key) == len(types.PublicKey{}) {
e.HostAnnouncements = append(e.HostAnnouncements, HostAnnouncement{
PublicKey: *(*types.PublicKey)(uk.Key),
NetAddress: netAddress,
})
}
}
e.HostAnnouncements = ParseHostAnnouncements(txn.ArbitraryData)
chris124567 marked this conversation as resolved.
Show resolved Hide resolved
for i := range txn.MinerFees {
e.Fee = e.Fee.Add(txn.MinerFees[i])
}
Expand Down
2 changes: 2 additions & 0 deletions explorer/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,8 @@ type Transaction struct {
MinerFees []types.Currency `json:"minerFees,omitempty"`
ArbitraryData [][]byte `json:"arbitraryData,omitempty"`
Signatures []types.TransactionSignature `json:"signatures,omitempty"`

HostAnnouncements []HostAnnouncement `json:"hostAnnouncements,omitempty"`
chris124567 marked this conversation as resolved.
Show resolved Hide resolved
}

// A Block is a block containing wrapped transactions and siacoin
Expand Down
38 changes: 38 additions & 0 deletions persist/sqlite/consensus_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2010,6 +2010,17 @@ func TestHostAnnouncement(t *testing.T) {
pk2 := types.GeneratePrivateKey()
pk3 := types.GeneratePrivateKey()

checkHostAnnouncements := func(expectedArbitraryData [][]byte, got []explorer.HostAnnouncement) {
t.Helper()

expected := explorer.ParseHostAnnouncements(expectedArbitraryData)
check(t, "len(hostAnnouncements)", len(expected), len(got))
for i := range expected {
check(t, "host public key", expected[i].PublicKey, got[i].PublicKey)
check(t, "host net address", expected[i].NetAddress, got[i].NetAddress)
}
}

txn1 := types.Transaction{
ArbitraryData: [][]byte{
createAnnouncement(pk1, "127.0.0.1:1234"),
Expand Down Expand Up @@ -2057,6 +2068,33 @@ func TestHostAnnouncement(t *testing.T) {
StorageUtilization: 0,
})

{
dbTxns, err := db.Transactions([]types.TransactionID{txn1.ID()})
if err != nil {
t.Fatal(err)
}
check(t, "len(txns)", 1, len(dbTxns))
checkHostAnnouncements(txn1.ArbitraryData, dbTxns[0].HostAnnouncements)
}

{
dbTxns, err := db.Transactions([]types.TransactionID{txn2.ID()})
if err != nil {
t.Fatal(err)
}
check(t, "len(txns)", 1, len(dbTxns))
checkHostAnnouncements(txn2.ArbitraryData, dbTxns[0].HostAnnouncements)
}

{
dbTxns, err := db.Transactions([]types.TransactionID{txn3.ID()})
if err != nil {
t.Fatal(err)
}
check(t, "len(txns)", 1, len(dbTxns))
checkHostAnnouncements(txn3.ArbitraryData, dbTxns[0].HostAnnouncements)
}

ts := time.Unix(0, 0)
hosts, err := db.HostsForScanning(ts, ts, 0, 100)
if err != nil {
Expand Down
2 changes: 2 additions & 0 deletions persist/sqlite/transactions.go
Original file line number Diff line number Diff line change
Expand Up @@ -553,6 +553,8 @@ func getTransactions(tx *txn, idMap map[int64]transactionID) ([]explorer.Transac
ArbitraryData: txnArbitraryData[dbID],
Signatures: txnSignatures[dbID],
}
txn.HostAnnouncements = explorer.ParseHostAnnouncements(txn.ArbitraryData)

results = append(results, txn)
}
return results, nil
Expand Down
Loading