Skip to content

Commit

Permalink
use PublicKey instead of UnlockKey
Browse files Browse the repository at this point in the history
  • Loading branch information
chris124567 committed Jun 5, 2024
1 parent 535d3da commit 6f52775
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 20 deletions.
4 changes: 2 additions & 2 deletions api/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ type (
UnspentSiafundOutputs(address types.Address, offset, limit uint64) ([]explorer.SiafundOutput, error)
AddressEvents(address types.Address, offset, limit uint64) (events []explorer.Event, err error)
Contracts(ids []types.FileContractID) (result []explorer.FileContract, err error)
ContractsKey(key types.UnlockKey) (result []explorer.FileContract, err error)
ContractsKey(key types.PublicKey) (result []explorer.FileContract, err error)
}
)

Expand Down Expand Up @@ -309,7 +309,7 @@ func (s *server) explorerContractIDHandler(jc jape.Context) {
func (s *server) explorerContractKeyHandler(jc jape.Context) {
errNotFound := errors.New("no contract found")

var key types.UnlockKey
var key types.PublicKey
if jc.DecodeParam("key", &key) != nil {
return
}
Expand Down
4 changes: 2 additions & 2 deletions explorer/explorer.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ type Store interface {
AddressEvents(address types.Address, offset, limit uint64) (events []Event, err error)
Balance(address types.Address) (sc types.Currency, immatureSC types.Currency, sf uint64, err error)
Contracts(ids []types.FileContractID) (result []FileContract, err error)
ContractsKey(key types.UnlockKey) (result []FileContract, err error)
ContractsKey(key types.PublicKey) (result []FileContract, err error)
}

// Explorer implements a Sia explorer.
Expand Down Expand Up @@ -171,6 +171,6 @@ func (e *Explorer) Contracts(ids []types.FileContractID) (result []FileContract,
}

// ContractsKey returns the contracts for a particular ed25519 key.
func (e *Explorer) ContractsKey(key types.UnlockKey) (result []FileContract, err error) {
func (e *Explorer) ContractsKey(key types.PublicKey) (result []FileContract, err error) {
return e.s.ContractsKey(key)
}
15 changes: 9 additions & 6 deletions persist/sqlite/consensus.go
Original file line number Diff line number Diff line change
Expand Up @@ -750,7 +750,7 @@ func addFileContractElements(tx *txn, b types.Block, fces []explorer.FileContrac
}
defer revisionStmt.Close()

fcKeys := make(map[explorer.DBFileContract][]types.UnlockKey)
fcKeys := make(map[explorer.DBFileContract][2]types.PublicKey)
// populate fcKeys using revision UnlockConditions fields
for _, txn := range b.Transactions {
for _, fcr := range txn.FileContractRevisions {
Expand All @@ -760,20 +760,23 @@ func addFileContractElements(tx *txn, b types.Block, fces []explorer.FileContrac

// check for 2 ed25519 keys
ok := true
var result [2]types.PublicKey
for i := 0; i < 2; i++ {
// fewer than 2 keys
if i >= len(uc.PublicKeys) {
ok = false
break
}

// not an ed25519 key
if uc.PublicKeys[i].Algorithm != types.SpecifierEd25519 {
if uc.PublicKeys[i].Algorithm == types.SpecifierEd25519 {
result[i] = types.PublicKey(uc.PublicKeys[i].Key)
} else {
// not an ed25519 key
ok = false
}
}
if ok {
fcKeys[dbFC] = fcr.UnlockConditions.PublicKeys
fcKeys[dbFC] = result
}
}
}
Expand All @@ -792,8 +795,8 @@ func addFileContractElements(tx *txn, b types.Block, fces []explorer.FileContrac
if lastRevision {
var renterKey, hostKey []byte
if keys, ok := fcKeys[dbFC]; ok {
renterKey = keys[0].Key
hostKey = keys[1].Key
renterKey = encode(keys[0]).([]byte)
hostKey = encode(keys[1]).([]byte)
}

if _, err := revisionStmt.Exec(encode(fcID), dbID, renterKey, hostKey, dbID, renterKey, hostKey); err != nil {
Expand Down
16 changes: 8 additions & 8 deletions persist/sqlite/consensus_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -859,11 +859,11 @@ func TestFileContract(t *testing.T) {
syncDB(t, db, cm)

{
renterContracts, err := db.ContractsKey(renterPublicKey.UnlockKey())
renterContracts, err := db.ContractsKey(renterPublicKey)
if err != nil {
t.Fatal(err)
}
hostContracts, err := db.ContractsKey(hostPublicKey.UnlockKey())
hostContracts, err := db.ContractsKey(hostPublicKey)
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -946,11 +946,11 @@ func TestFileContract(t *testing.T) {
}

{
renterContracts, err := db.ContractsKey(renterPublicKey.UnlockKey())
renterContracts, err := db.ContractsKey(renterPublicKey)
if err != nil {
t.Fatal(err)
}
hostContracts, err := db.ContractsKey(hostPublicKey.UnlockKey())
hostContracts, err := db.ContractsKey(hostPublicKey)
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -1100,11 +1100,11 @@ func TestEphemeralFileContract(t *testing.T) {
syncDB(t, db, cm)

{
renterContracts, err := db.ContractsKey(renterPublicKey.UnlockKey())
renterContracts, err := db.ContractsKey(renterPublicKey)
if err != nil {
t.Fatal(err)
}
hostContracts, err := db.ContractsKey(hostPublicKey.UnlockKey())
hostContracts, err := db.ContractsKey(hostPublicKey)
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -1188,11 +1188,11 @@ func TestEphemeralFileContract(t *testing.T) {
}

{
renterContracts, err := db.ContractsKey(renterPublicKey.UnlockKey())
renterContracts, err := db.ContractsKey(renterPublicKey)
if err != nil {
t.Fatal(err)
}
hostContracts, err := db.ContractsKey(hostPublicKey.UnlockKey())
hostContracts, err := db.ContractsKey(hostPublicKey)
if err != nil {
t.Fatal(err)
}
Expand Down
4 changes: 2 additions & 2 deletions persist/sqlite/contracts.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,13 @@ func (s *Store) Contracts(ids []types.FileContractID) (result []explorer.FileCon
}

// ContractsKey implements explorer.Store.
func (s *Store) ContractsKey(key types.UnlockKey) (result []explorer.FileContract, err error) {
func (s *Store) ContractsKey(key types.PublicKey) (result []explorer.FileContract, err error) {
err = s.transaction(func(tx *txn) error {
query := `SELECT fc1.id, fc1.contract_id, fc1.leaf_index, fc1.resolved, fc1.valid, fc1.filesize, fc1.file_merkle_root, fc1.window_start, fc1.window_end, fc1.payout, fc1.unlock_hash, fc1.revision_number
FROM file_contract_elements fc1
INNER JOIN last_contract_revision rev ON (rev.contract_element_id = fc1.id)
WHERE rev.ed25519_renter_key = ? OR rev.ed25519_host_key = ?`
rows, err := tx.Query(query, key.Key, key.Key)
rows, err := tx.Query(query, encode(key), encode(key))
if err != nil {
return err
}
Expand Down

0 comments on commit 6f52775

Please sign in to comment.