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

poc: rpc prop tests #1083

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
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
6 changes: 6 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,10 @@ issues:
- unused
- deadcode
- varcheck
# Ignore errors intentionally not returned in property-based tests.
# Needed here because the inline ignore directive is broken:
# https://github.com/gostaticanalysis/nilerr/issues/8
- path: rpcserver_test.go
linters:
- nilerr
new-from-rev: c723abd3c9db8a6a2f3f1eaa85ce5aefb52c8170
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -201,9 +201,12 @@ require (
modernc.org/strutil v1.2.0 // indirect
modernc.org/token v1.1.0 // indirect
nhooyr.io/websocket v1.8.7 // indirect
pgregory.net/rapid v1.1.0
sigs.k8s.io/yaml v1.2.0 // indirect
)

// We want to format raw bytes as hex instead of base64. The forked version
// allows us to specify that as an option.
replace google.golang.org/protobuf => github.com/lightninglabs/protobuf-go-hex-display v1.30.0-hex-display

replace pgregory.net/rapid v1.1.0 => github.com/chrisseto/rapid v0.0.0-20240815210052-cdeef406c65c // indirect
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,8 @@ github.com/cespare/xxhash/v2 v2.1.1/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XL
github.com/cespare/xxhash/v2 v2.1.2/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
github.com/cespare/xxhash/v2 v2.2.0 h1:DC2CZ1Ep5Y4k3ZQ899DldepgrayRUGE6BBZ/cd9Cj44=
github.com/cespare/xxhash/v2 v2.2.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
github.com/chrisseto/rapid v0.0.0-20240815210052-cdeef406c65c h1:GZtcJAFTBCr16eM7ytFwWMg9oLaMsRfSsVyi3lTo+mw=
github.com/chrisseto/rapid v0.0.0-20240815210052-cdeef406c65c/go.mod h1:PY5XlDGj0+V1FCq0o192FdRhpKHGTRIWBgqjDBTrq04=
github.com/chzyer/logex v1.1.10/go.mod h1:+Ywpsq7O8HXn0nuIou7OrIPyXbp3wmkHB+jjWRnGsAI=
github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e/go.mod h1:nSuG5e5PlCu98SY8svDHJxuZscDgtXS6KTTbou5AhLI=
github.com/chzyer/test v0.0.0-20180213035817-a1ea475d72b1/go.mod h1:Q3SI9o4m/ZMnBNeIyt5eFwwo7qiLfzFZmjNmxjkiQlU=
Expand Down
129 changes: 75 additions & 54 deletions rpcserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -4668,51 +4668,57 @@ func UnmarshalUniID(rpcID *unirpc.ID) (universe.Identifier, error) {
return universe.Identifier{}, fmt.Errorf("unable to unmarshal "+
"proof type: %w", err)
}

var (
assetIDBytes []byte
groupKeyBytes []byte
)

switch {
case rpcID.GetAssetId() != nil:
var assetID asset.ID
copy(assetID[:], rpcID.GetAssetId())

return universe.Identifier{
AssetID: assetID,
ProofType: proofType,
}, nil
assetIDBytes = rpcID.GetAssetId()
if len(assetIDBytes) != sha256.Size {
return universe.Identifier{}, fmt.Errorf("asset ID " +
"must be 32 bytes")
}

case rpcID.GetAssetIdStr() != "":
assetIDBytes, err := hex.DecodeString(rpcID.GetAssetIdStr())
if err != nil {
return universe.Identifier{}, err
rpcAssetIDStr := rpcID.GetAssetIdStr()
if len(rpcAssetIDStr) != sha256.Size*2 {
return universe.Identifier{}, fmt.Errorf("asset ID " +
"string must be 64 chars")
}

// TODO(roasbeef): reuse with above

var assetID asset.ID
copy(assetID[:], assetIDBytes)

return universe.Identifier{
AssetID: assetID,
ProofType: proofType,
}, nil

case rpcID.GetGroupKey() != nil:
groupKey, err := parseUserKey(rpcID.GetGroupKey())
assetIDBytes, err = hex.DecodeString(rpcAssetIDStr)
if err != nil {
return universe.Identifier{}, err
}

return universe.Identifier{
GroupKey: groupKey,
ProofType: proofType,
}, nil
case rpcID.GetGroupKey() != nil:
groupKeyBytes = rpcID.GetGroupKey()

case rpcID.GetGroupKeyStr() != "":
groupKeyBytes, err := hex.DecodeString(rpcID.GetGroupKeyStr())
rpcGroupKeyStr := rpcID.GetGroupKeyStr()
groupKeyBytes, err = hex.DecodeString(rpcGroupKeyStr)
if err != nil {
return universe.Identifier{}, err
}

// TODO(roasbeef): reuse with above
default:
return universe.Identifier{}, fmt.Errorf("no id set")
}

switch {
case len(assetIDBytes) != 0:
var assetID asset.ID
copy(assetID[:], assetIDBytes)

return universe.Identifier{
AssetID: assetID,
ProofType: proofType,
}, nil

case len(groupKeyBytes) != 0:
groupKey, err := parseUserKey(groupKeyBytes)
if err != nil {
return universe.Identifier{}, err
Expand All @@ -4724,7 +4730,7 @@ func UnmarshalUniID(rpcID *unirpc.ID) (universe.Identifier, error) {
}, nil

default:
return universe.Identifier{}, fmt.Errorf("no id set")
return universe.Identifier{}, fmt.Errorf("malformed id")
}
}

Expand Down Expand Up @@ -5329,6 +5335,10 @@ func unmarshalUniverseKey(key *unirpc.UniverseKey) (universe.Identifier,

// unmarshalAssetLeaf unmarshals an asset leaf from the RPC form.
func unmarshalAssetLeaf(leaf *unirpc.AssetLeaf) (*universe.Leaf, error) {
if leaf == nil {
return nil, fmt.Errorf("missing asset leaf")
}

// We'll just pull the asset details from the serialized issuance proof
// itself.
var proofAsset asset.Asset
Expand Down Expand Up @@ -5359,6 +5369,10 @@ func unmarshalAssetLeaf(leaf *unirpc.AssetLeaf) (*universe.Leaf, error) {
func (r *rpcServer) InsertProof(ctx context.Context,
req *unirpc.AssetProof) (*unirpc.AssetProofResponse, error) {

if req == nil {
return nil, fmt.Errorf("missing proof and universe key")
}

universeID, leafKey, err := unmarshalUniverseKey(req.Key)
if err != nil {
return nil, err
Expand Down Expand Up @@ -6184,64 +6198,71 @@ func unmarshalAssetSpecifier(req *rfqrpc.AssetSpecifier) (*asset.ID,
// give precedence to the asset ID due to its higher level of
// specificity.
var (
assetID *asset.ID

assetIDBytes []byte
assetID *asset.ID
groupKeyBytes []byte
groupKey *btcec.PublicKey

err error
err error
)

switch {
// Parse the asset ID if it's set.
case len(req.GetAssetId()) > 0:
var assetIdBytes [32]byte
copy(assetIdBytes[:], req.GetAssetId())
id := asset.ID(assetIdBytes)
assetID = &id
assetIDBytes = req.GetAssetId()
if len(assetIDBytes) != sha256.Size {
return nil, nil, fmt.Errorf("asset ID must be 32 bytes")
}

case len(req.GetAssetIdStr()) > 0:
assetIDBytes, err := hex.DecodeString(req.GetAssetIdStr())
reqAssetIDStr := req.GetAssetIdStr()
if len(reqAssetIDStr) != sha256.Size*2 {
return nil, nil, fmt.Errorf("asset ID string must be " +
"64 chars")
}

assetIDBytes, err = hex.DecodeString(reqAssetIDStr)
if err != nil {
return nil, nil, fmt.Errorf("error decoding asset "+
"ID: %w", err)
}

var id asset.ID
copy(id[:], assetIDBytes)
assetID = &id

// Parse the group key if it's set.
case len(req.GetGroupKey()) > 0:
groupKeyBytes = req.GetGroupKey()
groupKey, err = btcec.ParsePubKey(groupKeyBytes)
if err != nil {
return nil, nil, fmt.Errorf("error parsing group "+
"key: %w", err)
}

case len(req.GetGroupKeyStr()) > 0:
groupKeyBytes, err := hex.DecodeString(
groupKeyBytes, err = hex.DecodeString(
req.GetGroupKeyStr(),
)
if err != nil {
return nil, nil, fmt.Errorf("error decoding group "+
"key: %w", err)
}

groupKey, err = btcec.ParsePubKey(groupKeyBytes)
if err != nil {
return nil, nil, fmt.Errorf("error parsing group "+
"key: %w", err)
}

default:
// At this point, we know that neither the asset ID nor the
// group key are specified. Return an error.
return nil, nil, fmt.Errorf("either asset ID or asset group " +
"key must be specified")
}

switch {
case len(assetIDBytes) != 0:
var id asset.ID
copy(id[:], assetIDBytes)
assetID = &id

case len(groupKeyBytes) != 0:
groupKey, err = parseUserKey(groupKeyBytes)
if err != nil {
return nil, nil, fmt.Errorf("error parsing group "+
"key: group key: %w", err)
}

default:
return nil, nil, fmt.Errorf("malformed asset specifier")
}

return assetID, groupKey, nil
}

Expand Down
Loading
Loading