From fda6e9896cc4a66e3d3113f271aab33d8deea938 Mon Sep 17 00:00:00 2001 From: JoeGruff Date: Mon, 15 Mar 2021 10:59:40 +0900 Subject: [PATCH] Add flags to coinID. --- server/asset/eth/eth.go | 29 +++++++++++++++++------------ server/asset/eth/eth_test.go | 15 +++++++++++++-- 2 files changed, 30 insertions(+), 14 deletions(-) diff --git a/server/asset/eth/eth.go b/server/asset/eth/eth.go index 7e50a2d289..13df01cfa4 100644 --- a/server/asset/eth/eth.go +++ b/server/asset/eth/eth.go @@ -5,6 +5,7 @@ package eth import ( "context" + "encoding/binary" "errors" "fmt" "sync" @@ -21,8 +22,10 @@ func init() { } const ( - assetName = "eth" - coinIDSize = 52 + assetName = "eth" + // coinIdSize = flags (2) + smart contract address where funds are locked (20) + secret + // hash map key (32) + coinIDSize = 54 // The blockPollInterval is the delay between calls to bestBlockHash to // check for new blocks. blockPollInterval = time.Second @@ -351,29 +354,31 @@ out: wg.Wait() } -// decodeCoinID decodes the coin ID into a contract address and secret hash. -func decodeCoinID(coinID []byte) (common.Address, []byte, error) { +// decodeCoinID decodes the coin ID into flags, a contract address, and secret hash. +func decodeCoinID(coinID []byte) (uint16, common.Address, []byte, error) { if len(coinID) != coinIDSize { - return common.Address{}, nil, fmt.Errorf("coin ID wrong length. expected %d, got %d", + return 0, common.Address{}, nil, fmt.Errorf("coin ID wrong length. expected %d, got %d", coinIDSize, len(coinID)) } secretHash := make([]byte, 32) - copy(secretHash, coinID[20:]) - return common.BytesToAddress(coinID[:20]), secretHash, nil + copy(secretHash, coinID[22:]) + return binary.BigEndian.Uint16(coinID[:2]), common.BytesToAddress(coinID[2:22]), secretHash, nil } func coinIDToString(coinID []byte) (string, error) { - addr, secretHash, err := decodeCoinID(coinID) + flags, addr, secretHash, err := decodeCoinID(coinID) if err != nil { return "", err } - return fmt.Sprintf("%x:%x", addr, secretHash), err + return fmt.Sprintf("%x:%x:%x", flags, addr, secretHash), nil } // toCoinID converts the address and secret hash to a coin ID. -func toCoinID(addr *common.Address, secretHash []byte) []byte { +func toCoinID(flags uint16, addr *common.Address, secretHash []byte) []byte { b := make([]byte, coinIDSize) - copy(b[:], addr[:]) - copy(b[20:], secretHash[:]) + b[0] = byte(flags) + b[1] = byte(flags >> 8) + copy(b[2:], addr[:]) + copy(b[22:], secretHash[:]) return b } diff --git a/server/asset/eth/eth_test.go b/server/asset/eth/eth_test.go index 6dcf22618b..65335f55ec 100644 --- a/server/asset/eth/eth_test.go +++ b/server/asset/eth/eth_test.go @@ -85,12 +85,14 @@ func TestLoad(t *testing.T) { func TestDecodeCoinID(t *testing.T) { tests := []struct { name string + wantFlags uint16 wantAddr common.Address coinID, wantSecretHash []byte wantErr bool }{{ name: "ok", coinID: []byte{ + 0xFF, 0x01, // 2 byte flags 0x18, 0xd6, 0x5f, 0xb8, 0xd6, 0x0c, 0x11, 0x99, 0xbb, 0x1a, 0xd3, 0x81, 0xbe, 0x47, 0xaa, 0x69, 0x2b, 0x48, 0x26, 0x05, // 20 byte addr @@ -99,6 +101,7 @@ func TestDecodeCoinID(t *testing.T) { 0x8f, 0xd7, 0x99, 0x49, 0x98, 0xbb, 0x3e, 0x50, 0x48, 0x56, 0x7f, 0x2f, 0x07, 0x3c, // 32 byte secret hash }, + wantFlags: 65281, wantAddr: common.Address{ 0x18, 0xd6, 0x5f, 0xb8, 0xd6, 0x0c, 0x11, 0x99, 0xbb, 0x1a, 0xd3, 0x81, 0xbe, 0x47, 0xaa, 0x69, 0x2b, 0x48, @@ -113,6 +116,7 @@ func TestDecodeCoinID(t *testing.T) { }, { name: "wrong length", coinID: []byte{ + 0xFF, 0x01, // 2 byte flags 0x18, 0xd6, 0x5f, 0xb8, 0xd6, 0x0c, 0x11, 0x99, 0xbb, 0x1a, 0xd3, 0x81, 0xbe, 0x47, 0xaa, 0x69, 0x2b, 0x48, 0x26, 0x05, // 20 byte addr @@ -125,7 +129,7 @@ func TestDecodeCoinID(t *testing.T) { }} for _, test := range tests { - addr, secretHash, err := decodeCoinID(test.coinID) + flags, addr, secretHash, err := decodeCoinID(test.coinID) if test.wantErr { if err == nil { t.Fatalf("expected error for test %v", test.name) @@ -135,6 +139,10 @@ func TestDecodeCoinID(t *testing.T) { if err != nil { t.Fatalf("unexpected error for test %v: %v", test.name, err) } + if flags != test.wantFlags { + t.Fatalf("want flags value of %v but got %v for test %v", + test.wantFlags, flags, test.name) + } if addr != test.wantAddr { t.Fatalf("want addr value of %v but got %v for test %v", test.wantAddr, addr, test.name) @@ -147,6 +155,7 @@ func TestDecodeCoinID(t *testing.T) { } func TestCoinIDToString(t *testing.T) { + flags := "ff01" addr := "18d65fb8d60c1199bb1ad381be47aa692b482605" secretHash := "71d810d39333296b518c846a3e49eca55f998fd7994998bb3e5048567f2f073c" tests := []struct { @@ -156,6 +165,7 @@ func TestCoinIDToString(t *testing.T) { }{{ name: "ok", coinID: []byte{ + 0xFF, 0x01, // 2 byte flags 0x18, 0xd6, 0x5f, 0xb8, 0xd6, 0x0c, 0x11, 0x99, 0xbb, 0x1a, 0xd3, 0x81, 0xbe, 0x47, 0xaa, 0x69, 0x2b, 0x48, 0x26, 0x05, // 20 byte addr @@ -164,10 +174,11 @@ func TestCoinIDToString(t *testing.T) { 0x8f, 0xd7, 0x99, 0x49, 0x98, 0xbb, 0x3e, 0x50, 0x48, 0x56, 0x7f, 0x2f, 0x07, 0x3c, // 32 byte secret hash }, - wantCoinID: addr + ":" + secretHash, + wantCoinID: flags + ":" + addr + ":" + secretHash, }, { name: "wrong length", coinID: []byte{ + 0xFF, 0x01, // 2 byte flags 0x18, 0xd6, 0x5f, 0xb8, 0xd6, 0x0c, 0x11, 0x99, 0xbb, 0x1a, 0xd3, 0x81, 0xbe, 0x47, 0xaa, 0x69, 0x2b, 0x48, 0x26, 0x05, // 20 byte addr