Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Soroban xdr value overhaul ScVal.Equals fixes #4815

Merged
Merged
Show file tree
Hide file tree
Changes from 3 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
189 changes: 102 additions & 87 deletions xdr/scval.go
Original file line number Diff line number Diff line change
@@ -1,84 +1,16 @@
package xdr

import "bytes"

func (s Int128Parts) Equals(o Int128Parts) bool {
return s.Lo == o.Lo && s.Hi == o.Hi
}
Shaptic marked this conversation as resolved.
Show resolved Hide resolved

func (s ScContractCode) Equals(o ScContractCode) bool {
func (s ScContractExecutable) Equals(o ScContractExecutable) bool {
if s.Type != o.Type {
return false
}
switch s.Type {
case ScContractCodeTypeSccontractCodeToken:
case ScContractExecutableTypeSccontractExecutableToken:
return true
case ScContractCodeTypeSccontractCodeWasmRef:
case ScContractExecutableTypeSccontractExecutableWasmRef:
return s.MustWasmId().Equals(o.MustWasmId())
default:
panic("unknown ScContractCode type: " + s.Type.String())
}
}

func (s *ScObject) Equals(o *ScObject) bool {
if (s == nil) != (o == nil) {
return false
}
if s == nil {
return true
}
if s.Type != o.Type {
return false
}

switch s.Type {
case ScObjectTypeScoI64:
return s.MustI64() == o.MustI64()
case ScObjectTypeScoContractCode:
return s.MustContractCode().Equals(o.MustContractCode())
case ScObjectTypeScoU128:
return s.MustU128().Equals(o.MustU128())
case ScObjectTypeScoI128:
return s.MustI128().Equals(o.MustI128())
case ScObjectTypeScoBytes:
return bytes.Equal(s.MustBin(), o.MustBin())
case ScObjectTypeScoMap:
myMap := s.MustMap()
otherMap := o.MustMap()
if len(myMap) != len(otherMap) {
return false
}
for i := range myMap {
if !myMap[i].Key.Equals(otherMap[i].Key) ||
!myMap[i].Val.Equals(otherMap[i].Val) {
return false
}
}
return true
case ScObjectTypeScoU64:
return s.MustU64() == o.MustU64()
case ScObjectTypeScoVec:
myVec := s.MustVec()
otherVec := o.MustVec()
if len(myVec) != len(otherVec) {
return false
}
for i := range myVec {
if !myVec[i].Equals(otherVec[i]) {
return false
}
}
return true
case ScObjectTypeScoAddress:
myAddr := s.MustAddress()
otherAddr := o.MustAddress()
return myAddr.Equals(otherAddr)
case ScObjectTypeScoNonceKey:
myAddr := s.MustNonceAddress()
otherAddr := o.MustNonceAddress()
return myAddr.Equals(otherAddr)
default:
panic("unknown ScObject type: " + s.Type.String())
panic("unknown ScContractExecutable type: " + s.Type.String())
}
}

Expand Down Expand Up @@ -119,27 +51,67 @@ func (s ScVal) Equals(o ScVal) bool {
}

switch s.Type {
case ScValTypeScvObject:
return s.MustObj().Equals(o.MustObj())
case ScValTypeScvBitset:
return s.MustBits() == o.MustBits()
case ScValTypeScvStatic:
return s.MustIc() == o.MustIc()
case ScValTypeScvBool:
return s.MustB() == o.MustB()
case ScValTypeScvVoid:
return true
case ScValTypeScvStatus:
return s.MustStatus().Equals(o.MustStatus())
case ScValTypeScvSymbol:
return s.MustSym() == o.MustSym()
case ScValTypeScvI32:
return s.MustI32() == o.MustI32()
return s.MustError().Equals(o.MustError())
case ScValTypeScvU32:
return s.MustU32() == o.MustU32()
case ScValTypeScvU63:
return s.MustU63() == o.MustU63()
case ScValTypeScvI32:
return s.MustI32() == o.MustI32()
case ScValTypeScvU64:
return s.MustU64() == o.MustU64()
case ScValTypeScvI64:
return s.MustI64() == o.MustI64()
case ScValTypeScvTimepoint:
return s.MustTimepoint() == o.MustTimepoint()
case ScValTypeScvDuration:
return s.MustDuration() == o.MustDuration()
case ScValTypeScvU128:
return s.MustU128() == o.MustU128()
case ScValTypeScvI128:
return s.MustI128() == o.MustI128()
case ScValTypeScvU256:
return s.MustU256() == o.MustU256()
case ScValTypeScvI256:
return s.MustI256() == o.MustI256()
case ScValTypeScvBytes:
return s.MustBytes().Equals(o.MustBytes())
case ScValTypeScvString:
return s.MustStr() == o.MustStr()
case ScValTypeScvSymbol:
return s.MustSym() == o.MustSym()
case ScValTypeScvVec:
return s.MustVec().Equals(o.MustVec())
case ScValTypeScvMap:
return s.MustMap().Equals(o.MustMap())
case ScValTypeScvContractExecutable:
return s.MustExec().Equals(o.MustExec())
case ScValTypeScvAddress:
return s.MustAddress().Equals(o.MustAddress())
case ScValTypeScvLedgerKeyContractExecutable:
return true
case ScValTypeScvLedgerKeyNonce:
return s.MustNonceKey().Equals(o.MustNonceKey())
default:
panic("unknown ScVal type: " + s.Type.String())
}
}

func (s ScBytes) Equals(o ScBytes) bool {
if len(s) != len(o) {
return false
}
for i := range s {
if s[i] != o[i] {
return false
}
}
return true
paulbellamy marked this conversation as resolved.
Show resolved Hide resolved
}

func (s ScAddress) Equals(o ScAddress) bool {
if s.Type != o.Type {
return false
Expand All @@ -158,6 +130,49 @@ func (s ScAddress) Equals(o ScAddress) bool {

// IsBool returns true if the given ScVal is a boolean
func (s ScVal) IsBool() bool {
ic, ok := s.GetIc()
return ok && (ic == ScStaticScsTrue || ic == ScStaticScsFalse)
return s.Type == ScValTypeScvBool
}

func (s *ScVec) Equals(o *ScVec) bool {
if s == nil && o == nil {
return true
}
if s == nil || o == nil {
return false
}
if len(*s) != len(*o) {
return false
}
for i := range *s {
if !(*s)[i].Equals((*o)[i]) {
return false
}
}
return true
}

func (s *ScMap) Equals(o *ScMap) bool {
if s == nil && o == nil {
return true
}
if s == nil || o == nil {
return false
}
if len(*s) != len(*o) {
return false
}
for i, entry := range *s {
if !entry.Equals((*o)[i]) {
return false
}
}
return true
}

func (s ScMapEntry) Equals(o ScMapEntry) bool {
return s.Key.Equals(o.Key) && s.Val.Equals(o.Val)
}

func (s ScNonceKey) Equals(o ScNonceKey) bool {
return s.NonceAddress.Equals(o.NonceAddress)
}
2 changes: 1 addition & 1 deletion xdr/scval_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,6 @@ func TestScValEqualsCoverage(t *testing.T) {

clonedScVal := ScVal{}
assert.NoError(t, gxdr.Convert(shape, &clonedScVal))
assert.True(t, scVal.Equals(clonedScVal))
assert.True(t, scVal.Equals(clonedScVal), "scVal: %#v, clonedScVal: %#v", scVal, clonedScVal)
paulbellamy marked this conversation as resolved.
Show resolved Hide resolved
}
}
2 changes: 1 addition & 1 deletion xdr/xdr_generated.go
Original file line number Diff line number Diff line change
Expand Up @@ -51046,7 +51046,7 @@ func (u *ScVal) DecodeFrom(d *xdr.Decoder) (int, error) {
switch ScValType(u.Type) {
case ScValTypeScvBool:
u.B = new(bool)
nTmp, err = d.Decode((*u.B))
nTmp, err = d.Decode(u.B)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you fix this in xdrgen?

Copy link
Contributor Author

@paulbellamy paulbellamy Mar 20, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's another problem at:

func (s *DiagnosticEvent) DecodeFrom(d *xdr.Decoder) (int, error) {
	var err error
	var n, nTmp int
	nTmp, err = d.Decode(s.InSuccessfulContractCall)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we simply need to add bool support to xdrgen

n += nTmp
if err != nil {
return n, fmt.Errorf("decoding Bool: %s", err)
Expand Down