Skip to content

Commit

Permalink
Merge pull request #226 from SiaFoundation/nate/fix-attestation-id-panic
Browse files Browse the repository at this point in the history
Implement interfaces for AttestationID
  • Loading branch information
lukechampine authored Oct 30, 2024
2 parents 9fe97b7 + f81c41e commit 00682da
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 1 deletion.
13 changes: 12 additions & 1 deletion consensus/update_test.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package consensus

import (
"encoding/hex"
"reflect"
"strings"
"testing"
"time"

"go.sia.tech/core/internal/blake2b"
"go.sia.tech/core/types"
"lukechampine.com/frand"
)

func checkApplyUpdate(t *testing.T, cs State, au ApplyUpdate) {
Expand Down Expand Up @@ -893,6 +895,9 @@ func TestApplyRevertBlockV2(t *testing.T) {
db, cs := newConsensusDB(n, genesisBlock)

signTxn := func(cs State, txn *types.V2Transaction) {
for i := range txn.Attestations {
txn.Attestations[i].Signature = giftPrivateKey.SignHash(cs.AttestationSigHash(txn.Attestations[i]))
}
for i := range txn.SiacoinInputs {
txn.SiacoinInputs[i].SatisfiedPolicy.Signatures = []types.Signature{giftPrivateKey.SignHash(cs.InputSigHash(*txn))}
}
Expand Down Expand Up @@ -1023,9 +1028,15 @@ func TestApplyRevertBlockV2(t *testing.T) {
checkApplyUpdate(t, cs, au)
checkUpdateElements(au, addedSCEs, spentSCEs, addedSFEs, spentSFEs)
}

// block that spends part of the gift transaction
txnB2 := types.V2Transaction{
Attestations: []types.Attestation{
{
PublicKey: giftPublicKey,
Key: hex.EncodeToString(frand.Bytes(16)),
Value: frand.Bytes(16),
},
},
SiacoinInputs: []types.V2SiacoinInput{{
Parent: db.sces[giftTxn.SiacoinOutputID(0)],
SatisfiedPolicy: satisfiedPolicy(types.StandardUnlockConditions(giftPublicKey)),
Expand Down
6 changes: 6 additions & 0 deletions types/encoding.go
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,9 @@ func (index ChainIndex) EncodeTo(e *Encoder) {
index.ID.EncodeTo(e)
}

// EncodeTo implements types.EncoderTo.
func (id AttestationID) EncodeTo(e *Encoder) { e.Write(id[:]) }

// V1SiacoinOutput provides v1 encoding for SiacoinOutput.
type V1SiacoinOutput SiacoinOutput

Expand Down Expand Up @@ -915,6 +918,9 @@ func (id *BlockID) DecodeFrom(d *Decoder) { d.Read(id[:]) }
// DecodeFrom implements types.DecoderFrom.
func (id *TransactionID) DecodeFrom(d *Decoder) { d.Read(id[:]) }

// DecodeFrom implements types.DecoderFrom.
func (id *AttestationID) DecodeFrom(d *Decoder) { d.Read(id[:]) }

// DecodeFrom implements types.DecoderFrom.
func (a *Address) DecodeFrom(d *Decoder) { d.Read(a[:]) }

Expand Down
13 changes: 13 additions & 0 deletions types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -1015,6 +1015,19 @@ func (tid TransactionID) MarshalText() ([]byte, error) {
// UnmarshalText implements encoding.TextUnmarshaler.
func (tid *TransactionID) UnmarshalText(b []byte) error { return unmarshalHex(tid[:], b) }

// String implements fmt.Stringer.
func (aid AttestationID) String() string { return hex.EncodeToString(aid[:]) }

// MarshalText implements encoding.TextMarshaler.
func (aid AttestationID) MarshalText() ([]byte, error) {
return []byte(hex.EncodeToString(aid[:])), nil
}

// UnmarshalText implements encoding.TextUnmarshaler.
func (aid *AttestationID) UnmarshalText(b []byte) error {
return unmarshalHex(aid[:], b)
}

// String implements fmt.Stringer.
func (scoid SiacoinOutputID) String() string { return hex.EncodeToString(scoid[:]) }

Expand Down

0 comments on commit 00682da

Please sign in to comment.