Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
67 changes: 8 additions & 59 deletions types/encoding.go
Original file line number Diff line number Diff line change
Expand Up @@ -587,29 +587,10 @@ func (p SpendPolicy) EncodeTo(e *Encoder) {
// EncodeTo implements types.EncoderTo.
func (sp SatisfiedPolicy) EncodeTo(e *Encoder) {
sp.Policy.EncodeTo(e)
var sigi, prei int
var rec func(SpendPolicy)
rec = func(p SpendPolicy) {
switch p := p.Type.(type) {
case PolicyTypePublicKey:
sp.Signatures[sigi].EncodeTo(e)
sigi++
case PolicyTypeHash:
e.Write(sp.Preimages[prei][:])
prei++
case PolicyTypeThreshold:
for i := range p.Of {
rec(p.Of[i])
}
case PolicyTypeUnlockConditions:
for i := range p.PublicKeys {
rec(PolicyPublicKey(*(*PublicKey)(p.PublicKeys[i].Key)))
}
default:
// nothing to do
}
}
rec(sp.Policy)
EncodeSlice(e, sp.Signatures)
EncodeSliceFn(e, sp.Preimages, func(e *Encoder, pre [32]byte) {
Hash256(pre).EncodeTo(e)
})
}

// EncodeTo implements types.EncoderTo.
Expand Down Expand Up @@ -1165,43 +1146,11 @@ func (p *SpendPolicy) DecodeFrom(d *Decoder) {
// DecodeFrom implements types.DecoderFrom.
func (sp *SatisfiedPolicy) DecodeFrom(d *Decoder) {
sp.Policy.DecodeFrom(d)
// if policy decoding fails, the code below (namely the array cast) may
// panic, so abort early
if d.Err() != nil {
DecodeSlice(d, &sp.Signatures)
DecodeSliceFn(d, &sp.Preimages, func(d *Decoder) (pre [32]byte) {
(*Hash256)(&pre).DecodeFrom(d)
return
}

var rec func(SpendPolicy)
rec = func(p SpendPolicy) {
switch p := p.Type.(type) {
case PolicyTypePublicKey:
var s Signature
s.DecodeFrom(d)
sp.Signatures = append(sp.Signatures, s)
case PolicyTypeHash:
var pre [32]byte
d.Read(pre[:])
sp.Preimages = append(sp.Preimages, pre)
case PolicyTypeThreshold:
for i := range p.Of {
rec(p.Of[i])
}
case PolicyTypeUnlockConditions:
for _, uk := range p.PublicKeys {
if len(uk.Key) != 32 {
d.SetErr(fmt.Errorf("invalid public key length: %d", len(uk.Key)))
return
} else if uk.Algorithm != SpecifierEd25519 {
d.SetErr(fmt.Errorf("invalid specifier: %v", uk.Algorithm))
return
}
rec(PolicyPublicKey(*(*PublicKey)(uk.Key)))
}
default:
// nothing to do
}
}
rec(sp.Policy)
})
}

// DecodeFrom implements types.DecoderFrom.
Expand Down
2 changes: 1 addition & 1 deletion types/multiproof_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ func TestBlockCompression(t *testing.T) {
{"4 elements", multiproofTxns(2, 2), 0.90},
{"10 elements", multiproofTxns(2, 5), 0.85},
{"25 elements", multiproofTxns(5, 5), 0.75},
{"100 elements", multiproofTxns(10, 10), 0.70},
{"100 elements", multiproofTxns(10, 10), 0.71},
}
for _, test := range tests {
if r := ratio(test.txns); r >= test.exp {
Expand Down
Loading