Skip to content

Commit c637ca7

Browse files
authored
feat: add Equal() function to data.PlutusData types (#138)
Fixes #137 Signed-off-by: Aurora Gaffney <[email protected]>
1 parent 28ea4b8 commit c637ca7

File tree

2 files changed

+76
-2
lines changed

2 files changed

+76
-2
lines changed

cek/builtins.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import (
99
"fmt"
1010
"math/big"
1111
"math/bits"
12-
"reflect"
1312
"unicode/utf8"
1413

1514
"github.com/blinklabs-io/plutigo/data"
@@ -1885,7 +1884,7 @@ func equalsData[T syn.Eval](m *Machine[T], b *Builtin[T]) (Value[T], error) {
18851884
}
18861885

18871886
result := &syn.Bool{
1888-
Inner: reflect.DeepEqual(arg1, arg2),
1887+
Inner: arg1.Equal(arg2),
18891888
}
18901889

18911890
value := &Constant{result}

data/data.go

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package data
22

33
import (
4+
"bytes"
45
"fmt"
56
"math/big"
67
)
@@ -29,6 +30,7 @@ func (p *PlutusDataWrapper) MarshalCBOR() ([]byte, error) {
2930
type PlutusData interface {
3031
isPlutusData()
3132
Clone() PlutusData
33+
Equal(PlutusData) bool
3234
fmt.Stringer
3335
}
3436

@@ -55,6 +57,25 @@ func (c Constr) Clone() PlutusData {
5557
return &Constr{Tag: c.Tag, Fields: tmpFields, useIndef: tmpIndef}
5658
}
5759

60+
func (c Constr) Equal(pd PlutusData) bool {
61+
pdConstr, ok := pd.(*Constr)
62+
if !ok {
63+
return false
64+
}
65+
if c.Tag != pdConstr.Tag {
66+
return false
67+
}
68+
if len(c.Fields) != len(pdConstr.Fields) {
69+
return false
70+
}
71+
for i := range c.Fields {
72+
if !c.Fields[i].Equal(pdConstr.Fields[i]) {
73+
return false
74+
}
75+
}
76+
return true
77+
}
78+
5879
func (c Constr) String() string {
5980
return fmt.Sprintf("Constr{tag: %d, fields: %v}", c.Tag, c.Fields)
6081
}
@@ -98,6 +119,22 @@ func (m Map) Clone() PlutusData {
98119
return &Map{Pairs: tmpPairs, useIndef: tmpIndef}
99120
}
100121

122+
func (m Map) Equal(pd PlutusData) bool {
123+
pdMap, ok := pd.(*Map)
124+
if !ok {
125+
return false
126+
}
127+
for i, pair := range pdMap.Pairs {
128+
if !pair[0].Equal(pdMap.Pairs[i][0]) {
129+
return false
130+
}
131+
if !pair[1].Equal(pdMap.Pairs[i][1]) {
132+
return false
133+
}
134+
}
135+
return true
136+
}
137+
101138
func (m Map) String() string {
102139
return fmt.Sprintf("Map%v", m.Pairs)
103140
}
@@ -129,6 +166,17 @@ func (i Integer) Clone() PlutusData {
129166
return &Integer{tmpVal}
130167
}
131168

169+
func (i Integer) Equal(pd PlutusData) bool {
170+
pdInt, ok := pd.(*Integer)
171+
if !ok {
172+
return false
173+
}
174+
if i.Inner.Cmp(pdInt.Inner) != 0 {
175+
return false
176+
}
177+
return true
178+
}
179+
132180
func (i Integer) String() string {
133181
return fmt.Sprintf("Integer(%s)", i.Inner.String())
134182
}
@@ -153,6 +201,17 @@ func (b ByteString) Clone() PlutusData {
153201
return &ByteString{tmpVal}
154202
}
155203

204+
func (b ByteString) Equal(pd PlutusData) bool {
205+
pdByteString, ok := pd.(*ByteString)
206+
if !ok {
207+
return false
208+
}
209+
if !bytes.Equal(b.Inner, pdByteString.Inner) {
210+
return false
211+
}
212+
return true
213+
}
214+
156215
func (b ByteString) String() string {
157216
return fmt.Sprintf("ByteString(%x)", b.Inner)
158217
}
@@ -186,6 +245,22 @@ func (l List) Clone() PlutusData {
186245
return &List{Items: tmpItems, useIndef: tmpIndef}
187246
}
188247

248+
func (l List) Equal(pd PlutusData) bool {
249+
pdList, ok := pd.(*List)
250+
if !ok {
251+
return false
252+
}
253+
if len(l.Items) != len(pdList.Items) {
254+
return false
255+
}
256+
for i := range l.Items {
257+
if !l.Items[i].Equal(pdList.Items[i]) {
258+
return false
259+
}
260+
}
261+
return true
262+
}
263+
189264
func (l List) String() string {
190265
return fmt.Sprintf("List%v", l.Items)
191266
}

0 commit comments

Comments
 (0)