Skip to content

Commit

Permalink
implement TxData
Browse files Browse the repository at this point in the history
  • Loading branch information
buck54321 authored and chappjc committed Nov 11, 2021
1 parent ce823c3 commit 2dac253
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 11 deletions.
2 changes: 1 addition & 1 deletion server/asset/eth/coiner.go
Expand Up @@ -50,7 +50,7 @@ type swapCoin struct {
// an error is returned then if something is different than expected. As such,
// the swapCoin expects Confirmations to be called with confirmations
// available at least once before the swap be trusted for swap initializations.
func newSwapCoin(backend *Backend, coinID []byte, sct swapCoinType) (*swapCoin, error) {
func (backend *Backend) newSwapCoin(coinID []byte, sct swapCoinType) (*swapCoin, error) {
switch sct {
case sctInit, sctRedeem:
default:
Expand Down
15 changes: 9 additions & 6 deletions server/asset/eth/coiner_test.go
Expand Up @@ -26,11 +26,14 @@ func overMaxWei() *big.Int {
return overMaxWei.Add(overMaxWei, gweiFactorBig)
}

func randomAddress() *common.Address {
var addr common.Address
copy(addr[:], encode.RandomBytes(20))
return &addr
}

func TestNewSwapCoin(t *testing.T) {
receiverAddr, contractAddr, randomAddr := new(common.Address), new(common.Address), new(common.Address)
copy(receiverAddr[:], encode.RandomBytes(20))
copy(contractAddr[:], encode.RandomBytes(20))
copy(randomAddr[:], encode.RandomBytes(20))
contractAddr, randomAddr := randomAddress(), randomAddress()
secret, secretHash, txHash := [32]byte{}, [32]byte{}, [32]byte{}
copy(txHash[:], encode.RandomBytes(32))
copy(secret[:], secretSlice)
Expand Down Expand Up @@ -145,7 +148,7 @@ func TestNewSwapCoin(t *testing.T) {
log: tLogger,
contractAddr: *contractAddr,
}
sc, err := newSwapCoin(eth, test.coinID, test.ct)
sc, err := eth.newSwapCoin(test.coinID, test.ct)
if test.wantErr {
if err == nil {
t.Fatalf("expected error for test %q", test.name)
Expand Down Expand Up @@ -303,7 +306,7 @@ func TestConfirmations(t *testing.T) {
contractAddr: *contractAddr,
}

sc, err := newSwapCoin(eth, txCoinIDBytes, test.ct)
sc, err := eth.newSwapCoin(txCoinIDBytes, test.ct)
if err != nil {
t.Fatalf("unexpected error for test %q: %v", test.name, err)
}
Expand Down
2 changes: 1 addition & 1 deletion server/asset/eth/common.go
Expand Up @@ -81,7 +81,7 @@ const (
// to the smart contract. This type of ID is useful to identify coins that
// were sent in transactions that have not yet been mined.
type TxCoinID struct {
TxID [32]byte
TxID common.Hash
}

// String creates a human readable string.
Expand Down
20 changes: 17 additions & 3 deletions server/asset/eth/eth.go
Expand Up @@ -207,7 +207,21 @@ func (eth *Backend) Connect(ctx context.Context) (*sync.WaitGroup, error) {

// TxData fetches the raw transaction data.
func (eth *Backend) TxData(coinID []byte) ([]byte, error) {
return nil, notImplementedErr
cnr, err := DecodeCoinID(coinID)
if err != nil {
return nil, fmt.Errorf("coin ID decoding error: %v", err)
}
c, is := cnr.(*TxCoinID)
if !is {
return nil, fmt.Errorf("wrong type of coin ID, %v", cnr)
}

tx, _, err := eth.node.transaction(eth.rpcCtx, c.TxID)
if err != nil {
return nil, fmt.Errorf("error retrieving transaction: %w", err)
}

return tx.MarshalBinary()
}

// InitTxSize is not size for eth. In ethereum the size of a non-standard
Expand Down Expand Up @@ -249,7 +263,7 @@ func (eth *Backend) BlockChannel(size int) <-chan *asset.BlockUpdate {

// Contract is part of the asset.Backend interface.
func (eth *Backend) Contract(coinID, _ []byte) (*asset.Contract, error) {
sc, err := newSwapCoin(eth, coinID, sctInit)
sc, err := eth.newSwapCoin(coinID, sctInit)
if err != nil {
return nil, fmt.Errorf("unable to create coiner: %w", err)
}
Expand Down Expand Up @@ -299,7 +313,7 @@ func (eth *Backend) Synced() (bool, error) {
// should be the transaction that sent a redemption, while contractCoinID is the
// swap contract this redemption redeems.
func (eth *Backend) Redemption(redeemCoinID, contractCoinID []byte) (asset.Coin, error) {
cnr, err := newSwapCoin(eth, redeemCoinID, sctRedeem)
cnr, err := eth.newSwapCoin(redeemCoinID, sctRedeem)
if err != nil {
return nil, fmt.Errorf("unable to create coiner: %w", err)
}
Expand Down
51 changes: 51 additions & 0 deletions server/asset/eth/eth_test.go
Expand Up @@ -629,3 +629,54 @@ func TestRedemption(t *testing.T) {
}
}
}

func TestTxData(t *testing.T) {
node := &testNode{}
eth := &Backend{
node: node,
}
gasPrice := big.NewInt(3e10)
value := big.NewInt(5e18)
addr := randomAddress()
data := encode.RandomBytes(5)
tx := tTx(gasPrice, value, addr, data)
goodCoinID := (&TxCoinID{TxID: tx.Hash()}).Encode()
node.tx = tx

// initial success
txData, err := eth.TxData(goodCoinID)
if err != nil {
t.Fatalf("TxData error: %v", err)
}
checkB, _ := tx.MarshalBinary()
if !bytes.Equal(txData, checkB) {
t.Fatalf("tx data not transmitted")
}

// bad coin ID
coinID := encode.RandomBytes(2)
_, err = eth.TxData(coinID)
if err == nil {
t.Fatalf("no error for bad coin ID")
}

// Wrong type of coin ID
coinID = (&SwapCoinID{}).Encode()
_, err = eth.TxData(coinID)
if err == nil {
t.Fatalf("no error for wrong coin type")
}

// No transaction
_, err = eth.TxData(coinID)
if err == nil {
t.Fatalf("no error for missing tx")
}

// Success again
node.tx = tx
_, err = eth.TxData(goodCoinID)
if err != nil {
t.Fatalf("TxData error: %v", err)
}
}

0 comments on commit 2dac253

Please sign in to comment.