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

server/eth: Implement ValidateContract. #1273

Merged
merged 2 commits into from Nov 22, 2021
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions server/asset/eth/common.go
Expand Up @@ -59,6 +59,9 @@ const (
// which holds the ETH, an amount of ETH in gwei, and a random nonce to
// avoid duplicate coin IDs.
CIDAmount
// SecretHashSize is the byte-length of the hash of the secret key used
// in swaps.
SecretHashSize = 32
Copy link
Member Author

Choose a reason for hiding this comment

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

Will be moved to dex/networks

)

// CoinID is an interface that objects which represent different types of ETH
Expand Down
16 changes: 9 additions & 7 deletions server/asset/eth/eth.go
Expand Up @@ -18,7 +18,7 @@ import (

"decred.org/dcrdex/dex"
"decred.org/dcrdex/dex/encode"
swap "decred.org/dcrdex/dex/networks/eth"
dexeth "decred.org/dcrdex/dex/networks/eth"
"decred.org/dcrdex/server/asset"
"github.com/ethereum/go-ethereum"
"github.com/ethereum/go-ethereum/common"
Expand Down Expand Up @@ -74,7 +74,7 @@ func (d *Driver) DecodeCoinID(coinID []byte) (string, error) {

// UnitInfo returns the dex.UnitInfo for the asset.
func (d *Driver) UnitInfo() dex.UnitInfo {
return swap.UnitInfo
return dexeth.UnitInfo
}

// ethFetcher represents a blockchain information fetcher. In practice, it is
Expand All @@ -94,7 +94,7 @@ type ethFetcher interface {
syncProgress(ctx context.Context) (*ethereum.SyncProgress, error)
blockNumber(ctx context.Context) (uint64, error)
peers(ctx context.Context) ([]*p2p.PeerInfo, error)
swap(ctx context.Context, secretHash [32]byte) (*swap.ETHSwapSwap, error)
swap(ctx context.Context, secretHash [32]byte) (*dexeth.ETHSwapSwap, error)
transaction(ctx context.Context, hash common.Hash) (tx *types.Transaction, isMempool bool, err error)
}

Expand Down Expand Up @@ -345,10 +345,12 @@ func (eth *Backend) ValidateCoinID(coinID []byte) (string, error) {
return coinId.String(), nil
}

// ValidateContract ensures that the swap contract is constructed properly, and
// contains valid sender and receiver addresses.
func (eth *Backend) ValidateContract(contract []byte) error {
return notImplementedErr
// ValidateContract ensures that the secret hash is the correct length.
func (eth *Backend) ValidateContract(secretHash []byte) error {
if len(secretHash) != SecretHashSize {
return fmt.Errorf("secret hash is wrong size: want %d but got %d", SecretHashSize, len(secretHash))
}
return nil
}

// CheckAddress checks that the given address is parseable.
Expand Down
28 changes: 28 additions & 0 deletions server/asset/eth/eth_test.go
Expand Up @@ -686,3 +686,31 @@ func TestTxData(t *testing.T) {
t.Fatalf("TxData error: %v", err)
}
}

func TestValidateContract(t *testing.T) {
tests := []struct {
name string
secretHash []byte
wantErr bool
}{{
name: "ok",
secretHash: make([]byte, 32),
}, {
name: "wrong size",
secretHash: make([]byte, 31),
wantErr: true,
}}
for _, test := range tests {
eth := new(Backend)
err := eth.ValidateContract(test.secretHash)
if test.wantErr {
if err == nil {
t.Fatalf("expected error for test %q", test.name)
}
continue
}
if err != nil {
t.Fatalf("unexpected error for test %q: %v", test.name, err)
}
}
}