Skip to content

Commit

Permalink
Fix subnetworks FromString and disable reversal (#1557)
Browse files Browse the repository at this point in the history
* Remove DomainSubnetworkID reversal

* Fix DomaiNSubnetworkID FromString implementation

* Change RPC conversation logic to use Stringer/FromString

Co-authored-by: Ori Newman <orinewman1@gmail.com>
  • Loading branch information
elichai and someone235 authored Feb 25, 2021
1 parent 6597f24 commit 24a12cf
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 38 deletions.
20 changes: 4 additions & 16 deletions app/appmessage/domainconverters.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,11 +164,7 @@ func outpointToDomainOutpoint(outpoint *Outpoint) *externalapi.DomainOutpoint {
func RPCTransactionToDomainTransaction(rpcTransaction *RPCTransaction) (*externalapi.DomainTransaction, error) {
inputs := make([]*externalapi.DomainTransactionInput, len(rpcTransaction.Inputs))
for i, input := range rpcTransaction.Inputs {
transactionIDBytes, err := hex.DecodeString(input.PreviousOutpoint.TransactionID)
if err != nil {
return nil, err
}
transactionID, err := transactionid.FromBytes(transactionIDBytes)
transactionID, err := transactionid.FromString(input.PreviousOutpoint.TransactionID)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -198,19 +194,11 @@ func RPCTransactionToDomainTransaction(rpcTransaction *RPCTransaction) (*externa
}
}

subnetworkIDBytes, err := hex.DecodeString(rpcTransaction.SubnetworkID)
if err != nil {
return nil, err
}
subnetworkID, err := subnetworks.FromBytes(subnetworkIDBytes)
if err != nil {
return nil, err
}
payloadHashBytes, err := hex.DecodeString(rpcTransaction.PayloadHash)
subnetworkID, err := subnetworks.FromString(rpcTransaction.SubnetworkID)
if err != nil {
return nil, err
}
payloadHash, err := externalapi.NewDomainHashFromByteSlice(payloadHashBytes)
payloadHash, err := externalapi.NewDomainHashFromString(rpcTransaction.PayloadHash)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -255,7 +243,7 @@ func DomainTransactionToRPCTransaction(transaction *externalapi.DomainTransactio
ScriptPublicKey: &RPCScriptPublicKey{Script: scriptPublicKey, Version: output.ScriptPublicKey.Version},
}
}
subnetworkID := hex.EncodeToString(transaction.SubnetworkID[:])
subnetworkID := transaction.SubnetworkID.String()
payloadHash := transaction.PayloadHash.String()
payload := hex.EncodeToString(transaction.Payload)
return &RPCTransaction{
Expand Down
3 changes: 0 additions & 3 deletions domain/consensus/model/externalapi/subnetworkid.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,6 @@ type DomainSubnetworkID [DomainSubnetworkIDSize]byte

// String stringifies a subnetwork ID.
func (id DomainSubnetworkID) String() string {
for i := 0; i < DomainSubnetworkIDSize/2; i++ {
id[i], id[DomainSubnetworkIDSize-1-i] = id[DomainSubnetworkIDSize-1-i], id[i]
}
return hex.EncodeToString(id[:])
}

Expand Down
16 changes: 2 additions & 14 deletions domain/consensus/utils/subnetworks/compare.go
Original file line number Diff line number Diff line change
@@ -1,23 +1,11 @@
package subnetworks

import (
"bytes"
"github.com/kaspanet/kaspad/domain/consensus/model/externalapi"
)

func cmp(a, b externalapi.DomainSubnetworkID) int {
// We compare the hashes backwards because Hash is stored as a little endian byte array.
for i := externalapi.DomainSubnetworkIDSize - 1; i >= 0; i-- {
switch {
case a[i] < b[i]:
return -1
case a[i] > b[i]:
return 1
}
}
return 0
}

// Less returns true iff id a is less than id b
func Less(a, b externalapi.DomainSubnetworkID) bool {
return cmp(a, b) < 0
return bytes.Compare(a[:], b[:]) < 0
}
6 changes: 1 addition & 5 deletions domain/consensus/utils/subnetworks/from_string.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,7 @@ import (

// FromString creates a DomainSubnetworkID from the given byte slice
func FromString(str string) (*externalapi.DomainSubnetworkID, error) {
runes := []rune(str)
for i := 0; i < externalapi.DomainSubnetworkIDSize*2; i++ {
runes[i], runes[externalapi.DomainSubnetworkIDSize-1-i] = runes[externalapi.DomainSubnetworkIDSize-1-i], runes[i]
}
subnetworkIDBytes, err := hex.DecodeString(string(runes))
subnetworkIDBytes, err := hex.DecodeString(str)
if err != nil {
return nil, err
}
Expand Down

0 comments on commit 24a12cf

Please sign in to comment.