Skip to content

Commit

Permalink
all: update core and coreutils
Browse files Browse the repository at this point in the history
wallet: fix lint
  • Loading branch information
n8maninger committed Oct 30, 2024
1 parent d71cf08 commit 1c79653
Show file tree
Hide file tree
Showing 14 changed files with 278 additions and 212 deletions.
8 changes: 4 additions & 4 deletions api/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -801,11 +801,11 @@ func TestV2(t *testing.T) {
Value: sce.SiacoinOutput.Value,
}},
Signatures: []types.TransactionSignature{{
ParentID: sce.ID,
ParentID: types.Hash256(sce.ID),
CoveredFields: types.CoveredFields{WholeTransaction: true},
}},
}
sig := key.SignHash(cm.TipState().WholeSigHash(txn, sce.ID, 0, 0, nil))
sig := key.SignHash(cm.TipState().WholeSigHash(txn, types.Hash256(sce.ID), 0, 0, nil))
txn.Signatures[0].Signature = sig[:]
if err := addBlock([]types.Transaction{txn}, nil); err != nil {
return err
Expand Down Expand Up @@ -1104,15 +1104,15 @@ func TestP2P(t *testing.T) {
Value: sce.SiacoinOutput.Value,
}},
Signatures: []types.TransactionSignature{{
ParentID: sce.ID,
ParentID: types.Hash256(sce.ID),
CoveredFields: types.CoveredFields{WholeTransaction: true},
}},
}
cs, err := c.ConsensusTipState()
if err != nil {
return err
}
sig := key.SignHash(cs.WholeSigHash(txn, sce.ID, 0, 0, nil))
sig := key.SignHash(cs.WholeSigHash(txn, types.Hash256(sce.ID), 0, 0, nil))
txn.Signatures[0].Signature = sig[:]
if err := c.TxpoolBroadcast([]types.Transaction{txn}, nil); err != nil {
return err
Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ toolchain go1.23.2

require (
github.com/mattn/go-sqlite3 v1.14.24
go.sia.tech/core v0.5.0
go.sia.tech/coreutils v0.5.0
go.sia.tech/core v0.6.1
go.sia.tech/coreutils v0.6.0
go.sia.tech/jape v0.12.1
go.sia.tech/web/walletd v0.23.1
go.uber.org/zap v1.27.0
Expand Down
8 changes: 4 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ 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.5.0 h1:feLC7DSCF+PhU157s/94106hFKyiGrGQ9HC3/dF/l7E=
go.sia.tech/core v0.5.0/go.mod h1:P3C1BWa/7J4XgdzWuaYHBvLo2RzZ0UBaJM4TG1GWB2g=
go.sia.tech/coreutils v0.5.0 h1:/xKxdw83iZy0jjLzI2NGHyG4azyjK5DJscxpkr6nIGQ=
go.sia.tech/coreutils v0.5.0/go.mod h1:VYM4FcmlhVrpDGvglLHjRW+gitoaxPNLvp5mL2quilo=
go.sia.tech/core v0.6.1 h1:eaExM2E2eNr43su2XDkY5J24E3F54YGS7hcC3WtVjVk=
go.sia.tech/core v0.6.1/go.mod h1:P3C1BWa/7J4XgdzWuaYHBvLo2RzZ0UBaJM4TG1GWB2g=
go.sia.tech/coreutils v0.6.0 h1:r0IZt+aVdGG2uIHl7OtaWRYdVx4NQ7ezRoSGa0Ej8GY=
go.sia.tech/coreutils v0.6.0/go.mod h1:XlsnogeYU/Tdjzp/HUNAj5T7tZCdmeBHIBjymbPC+uQ=
go.sia.tech/jape v0.12.1 h1:xr+o9V8FO8ScRqbSaqYf9bjj1UJ2eipZuNcI1nYousU=
go.sia.tech/jape v0.12.1/go.mod h1:wU+h6Wh5olDjkPXjF0tbZ1GDgoZ6VTi4naFw91yyWC4=
go.sia.tech/mux v1.3.0 h1:hgR34IEkqvfBKUJkAzGi31OADeW2y7D6Bmy/Jcbop9c=
Expand Down
16 changes: 8 additions & 8 deletions persist/sqlite/addresses.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,14 +103,14 @@ func (s *Store) AddressSiacoinOutputs(address types.Address, index types.ChainIn
if s.indexMode == wallet.IndexModeFull {
indices := make([]uint64, len(siacoins))
for i, se := range siacoins {
indices[i] = se.LeafIndex
indices[i] = se.StateElement.LeafIndex
}
proofs, err := fillElementProofs(tx, indices)
if err != nil {
return fmt.Errorf("failed to fill element proofs: %w", err)
}
for i, proof := range proofs {
siacoins[i].MerkleProof = proof
siacoins[i].StateElement.MerkleProof = proof
}
}
return nil
Expand Down Expand Up @@ -148,14 +148,14 @@ func (s *Store) AddressSiafundOutputs(address types.Address, offset, limit int)
if s.indexMode == wallet.IndexModeFull {
indices := make([]uint64, len(siafunds))
for i, se := range siafunds {
indices[i] = se.LeafIndex
indices[i] = se.StateElement.LeafIndex
}
proofs, err := fillElementProofs(tx, indices)
if err != nil {
return fmt.Errorf("failed to fill element proofs: %w", err)
}
for i, proof := range proofs {
siafunds[i].MerkleProof = proof
siafunds[i].StateElement.MerkleProof = proof
}
}
return nil
Expand Down Expand Up @@ -244,13 +244,13 @@ func (s *Store) AnnotateV1Events(index types.ChainIndex, timestamp time.Time, v1

for i, output := range txn.SiacoinOutputs {
sce := types.SiacoinElement{
ID: txn.SiacoinOutputID(i),
StateElement: types.StateElement{
ID: types.Hash256(txn.SiacoinOutputID(i)),
LeafIndex: types.UnassignedLeafIndex,
},
SiacoinOutput: output,
}
siacoinElementCache[types.SiacoinOutputID(sce.StateElement.ID)] = sce
siacoinElementCache[sce.ID] = sce
relevant = true
}

Expand All @@ -268,13 +268,13 @@ func (s *Store) AnnotateV1Events(index types.ChainIndex, timestamp time.Time, v1

for i, output := range txn.SiafundOutputs {
sfe := types.SiafundElement{
ID: txn.SiafundOutputID(i),
StateElement: types.StateElement{
ID: types.Hash256(txn.SiafundOutputID(i)),
LeafIndex: types.UnassignedLeafIndex,
},
SiafundOutput: output,
}
siafundElementCache[types.SiafundOutputID(sfe.StateElement.ID)] = sfe
siafundElementCache[sfe.ID] = sfe
relevant = true
}

Expand Down
201 changes: 112 additions & 89 deletions persist/sqlite/consensus.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package sqlite
import (
"bytes"
"database/sql"
"encoding/json"
"errors"
"fmt"

Expand All @@ -26,96 +25,36 @@ type addressRef struct {
Balance wallet.Balance
}

func (ut *updateTx) SiacoinStateElements() ([]types.StateElement, error) {
if ut.indexMode == wallet.IndexModeFull {
panic("SiacoinStateElements called in full index mode")
}

const query = `SELECT id, leaf_index, merkle_proof FROM siacoin_elements`
rows, err := ut.tx.Query(query)
if err != nil {
return nil, fmt.Errorf("failed to query siacoin elements: %w", err)
}
defer rows.Close()

var elements []types.StateElement
for rows.Next() {
se, err := scanStateElement(rows)
if err != nil {
return nil, fmt.Errorf("failed to scan state element: %w", err)
}
elements = append(elements, se)
}
return elements, rows.Err()
type stateElement struct {
ID types.Hash256
types.StateElement
}

func (ut *updateTx) UpdateSiacoinStateElements(elements []types.StateElement) error {
func (ut *updateTx) UpdateStateElementProofs(update wallet.ProofUpdater) error {
if ut.indexMode == wallet.IndexModeFull {
panic("UpdateSiacoinStateElements called in full index mode")
panic("UpdateStateElementProofs called in full index mode")
}

log := ut.tx.log.Named("UpdateSiacoinStateElements")
log.Debug("updating siacoin state elements", zap.Int("count", len(elements)))

const query = `UPDATE siacoin_elements SET merkle_proof=$1, leaf_index=$2 WHERE id=$3 RETURNING id`
stmt, err := ut.tx.Prepare(query)
se, err := getSiacoinStateElements(ut.tx)
if err != nil {
return fmt.Errorf("failed to prepare statement: %w", err)
return fmt.Errorf("failed to get siacoin state elements: %w", err)
}
defer stmt.Close()

for _, se := range elements {
var dummy types.Hash256
err := stmt.QueryRow(encode(se.MerkleProof), se.LeafIndex, encode(se.ID)).Scan(decode(&dummy))
if err != nil {
return fmt.Errorf("failed to execute statement: %w", err)
}
log.Debug("updated element proof", zap.Stringer("id", se.ID), zap.Uint64("leafIndex", se.LeafIndex))
for i := range se {
update.UpdateElementProof(&se[i].StateElement)
}
return nil
}

func (ut *updateTx) SiafundStateElements() ([]types.StateElement, error) {
if ut.indexMode == wallet.IndexModeFull {
panic("SiafundStateElements called in full index mode")
if err := updateSiacoinStateElements(ut.tx, se); err != nil {
return fmt.Errorf("failed to update siacoin state elements: %w", err)
}

const query = `SELECT id, leaf_index, merkle_proof FROM siafund_elements`
rows, err := ut.tx.Query(query)
sfe, err := getSiafundStateElements(ut.tx)
if err != nil {
return nil, fmt.Errorf("failed to query siacoin elements: %w", err)
return fmt.Errorf("failed to get siafund state elements: %w", err)
}
defer rows.Close()

var elements []types.StateElement
for rows.Next() {
se, err := scanStateElement(rows)
if err != nil {
return nil, fmt.Errorf("failed to scan state element: %w", err)
}
elements = append(elements, se)
}
return elements, rows.Err()
}

func (ut *updateTx) UpdateSiafundStateElements(elements []types.StateElement) error {
if ut.indexMode == wallet.IndexModeFull {
panic("UpdateSiafundStateElements called in full index mode")
}

const query = `UPDATE siafund_elements SET merkle_proof=$1, leaf_index=$2 WHERE id=$3 RETURNING id`
stmt, err := ut.tx.Prepare(query)
if err != nil {
return fmt.Errorf("failed to prepare statement: %w", err)
for i := range sfe {
update.UpdateElementProof(&sfe[i].StateElement)
}
defer stmt.Close()

for _, se := range elements {
var dummy types.Hash256
err := stmt.QueryRow(encode(se.MerkleProof), se.LeafIndex, encode(se.ID)).Scan(decode(&dummy))
if err != nil {
return fmt.Errorf("failed to execute statement: %w", err)
}
if err := updateSiafundStateElements(ut.tx, sfe); err != nil {
return fmt.Errorf("failed to update siafund state elements: %w", err)
}
return nil
}
Expand Down Expand Up @@ -319,9 +258,90 @@ func (s *Store) SetIndexMode(mode wallet.IndexMode) error {
})
}

func scanStateElement(s scanner) (se types.StateElement, err error) {
err = s.Scan(decode(&se.ID), &se.LeafIndex, decode(&se.MerkleProof))
return
func getSiacoinStateElements(tx *txn) ([]stateElement, error) {
const query = `SELECT id, leaf_index, merkle_proof FROM siacoin_elements`
rows, err := tx.Query(query)
if err != nil {
return nil, fmt.Errorf("failed to query siacoin elements: %w", err)
}
defer rows.Close()

var elements []stateElement
for rows.Next() {
var se stateElement
if err := rows.Scan(decode(&se.ID), &se.LeafIndex, decode(&se.MerkleProof)); err != nil {
return nil, fmt.Errorf("failed to scan siacoin element: %w", err)
}
elements = append(elements, se)
}
return elements, rows.Err()
}

func getSiafundStateElements(tx *txn) ([]stateElement, error) {
const query = `SELECT id, leaf_index, merkle_proof FROM siafund_elements`
rows, err := tx.Query(query)
if err != nil {
return nil, fmt.Errorf("failed to query siafund elements: %w", err)
}
defer rows.Close()

var elements []stateElement
for rows.Next() {
var se stateElement
if err := rows.Scan(decode(&se.ID), &se.LeafIndex, decode(&se.MerkleProof)); err != nil {
return nil, fmt.Errorf("failed to scan siacoin element: %w", err)
}
elements = append(elements, se)
}
return elements, rows.Err()
}

func updateSiafundStateElements(tx *txn, elements []stateElement) error {
if len(elements) == 0 {
return nil
}
const query = `UPDATE siafund_elements SET merkle_proof=$1, leaf_index=$2 WHERE id=$3`
stmt, err := tx.Prepare(query)
if err != nil {
return fmt.Errorf("failed to prepare statement: %w", err)
}
defer stmt.Close()

for _, se := range elements {
res, err := stmt.Exec(encode(se.MerkleProof), se.LeafIndex, encode(se.ID))
if err != nil {
return fmt.Errorf("failed to execute statement: %w", err)
} else if n, err := res.RowsAffected(); err != nil {
return fmt.Errorf("failed to get rows affected: %w", err)
} else if n != 1 {
return fmt.Errorf("expected 1 row affected, got %v", n)
}
}
return nil
}

func updateSiacoinStateElements(tx *txn, elements []stateElement) error {
if len(elements) == 0 {
return nil
}
const query = `UPDATE siacoin_elements SET merkle_proof=$1, leaf_index=$2 WHERE id=$3`
stmt, err := tx.Prepare(query)
if err != nil {
return fmt.Errorf("failed to prepare statement: %w", err)
}
defer stmt.Close()

for _, se := range elements {
res, err := stmt.Exec(encode(se.MerkleProof), se.LeafIndex, encode(se.ID))
if err != nil {
return fmt.Errorf("failed to execute statement: %w", err)
} else if n, err := res.RowsAffected(); err != nil {
return fmt.Errorf("failed to get rows affected: %w", err)
} else if n != 1 {
return fmt.Errorf("expected 1 row affected, got %v", n)
}
}
return nil
}

func scanAddress(s scanner) (ab addressRef, err error) {
Expand Down Expand Up @@ -530,10 +550,10 @@ func addSiacoinElements(tx *txn, elements []types.SiacoinElement, indexID int64,
// in full index mode, Merkle proofs are stored in the state tree table
// rather than per element.
if indexMode == wallet.IndexModeFull {
se.MerkleProof = nil
se.StateElement.MerkleProof = nil
}

_, err = insertStmt.Exec(encode(se.ID), encode(se.SiacoinOutput.Value), encode(se.MerkleProof), se.LeafIndex, se.MaturityHeight, addrRef.ID, se.MaturityHeight == 0, indexID)
_, err = insertStmt.Exec(encode(se.ID), encode(se.SiacoinOutput.Value), encode(se.StateElement.MerkleProof), se.StateElement.LeafIndex, se.MaturityHeight, addrRef.ID, se.MaturityHeight == 0, indexID)
if err != nil {
return fmt.Errorf("failed to execute statement: %w", err)
}
Expand Down Expand Up @@ -804,10 +824,10 @@ func addSiafundElements(tx *txn, elements []types.SiafundElement, indexID int64,
// in full index mode, Merkle proofs are stored in the state tree table
// rather than per element.
if indexMode == wallet.IndexModeFull {
se.MerkleProof = nil
se.StateElement.MerkleProof = nil
}

_, err = insertStmt.Exec(encode(se.ID), se.SiafundOutput.Value, encode(se.MerkleProof), se.LeafIndex, encode(se.ClaimStart), addrRef.ID, indexID)
_, err = insertStmt.Exec(encode(se.ID), se.SiafundOutput.Value, encode(se.StateElement.MerkleProof), se.StateElement.LeafIndex, encode(se.ClaimStart), addrRef.ID, indexID)
if err != nil {
return fmt.Errorf("failed to execute statement: %w", err)
} else if exists {
Expand Down Expand Up @@ -1052,15 +1072,18 @@ func addEvents(tx *txn, events []wallet.Event, indexID int64) error {
defer relevantAddrStmt.Close()

var buf bytes.Buffer
enc := json.NewEncoder(&buf)
enc := types.NewEncoder(&buf)
for _, event := range events {
buf.Reset()
if err := enc.Encode(event.Data); err != nil {
return fmt.Errorf("failed to encode event: %w", err)
ev, ok := event.Data.(types.EncoderTo)
if !ok {
panic("event data does not implement types.EncoderTo") // developer error
}
ev.EncodeTo(enc)
enc.Flush()

var eventID int64
err = insertEventStmt.QueryRow(encode(event.ID), event.MaturityHeight, encode(event.Timestamp), event.Type, buf.String(), indexID).Scan(&eventID)
err = insertEventStmt.QueryRow(encode(event.ID), event.MaturityHeight, encode(event.Timestamp), event.Type, buf.Bytes(), indexID).Scan(&eventID)
if errors.Is(err, sql.ErrNoRows) {
continue // skip if the event already exists
} else if err != nil {
Expand Down
Loading

0 comments on commit 1c79653

Please sign in to comment.