From 80329186398a035e7603e757b582b3d98e37004c Mon Sep 17 00:00:00 2001 From: Kashif Date: Wed, 11 Nov 2020 01:49:06 +0900 Subject: [PATCH 01/13] add /account/coins api + remove coins from /balance --- go.mod | 2 +- go.sum | 6 ++++++ services/account_service.go | 39 ++++++++++++++++++++++++++++++++++++- 3 files changed, 45 insertions(+), 2 deletions(-) diff --git a/go.mod b/go.mod index 60d9327..940e956 100644 --- a/go.mod +++ b/go.mod @@ -5,7 +5,7 @@ go 1.13 require ( github.com/btcsuite/btcd v0.21.0-beta github.com/btcsuite/btcutil v1.0.2 - github.com/coinbase/rosetta-sdk-go v0.5.9 + github.com/coinbase/rosetta-sdk-go v0.6.0 github.com/dgraph-io/badger/v2 v2.2007.2 github.com/grpc-ecosystem/go-grpc-middleware v1.2.2 github.com/stretchr/testify v1.6.1 diff --git a/go.sum b/go.sum index b2efe12..6c34be9 100644 --- a/go.sum +++ b/go.sum @@ -64,6 +64,8 @@ github.com/cloudflare/cloudflare-go v0.10.2-0.20190916151808-a80f83b9add9/go.mod github.com/cncf/udpa/go v0.0.0-20191209042840-269d4d468f6f/go.mod h1:M8M6+tZqaGXZJjfX53e64911xZQV5JYwmTeXPW+k8Sc= github.com/coinbase/rosetta-sdk-go v0.5.9 h1:CuGQE3HFmYwdEACJnuOtVI9cofqPsGvq6FdFIzaOPKI= github.com/coinbase/rosetta-sdk-go v0.5.9/go.mod h1:xd4wYUhV3LkY78SPH8BUhc88rXfn2jYgN9BfiSjbcvM= +github.com/coinbase/rosetta-sdk-go v0.6.0 h1:U8/NhkPo7CGJR8Ud82Y0HqtujXSVzGZKscmxOHsmS54= +github.com/coinbase/rosetta-sdk-go v0.6.0/go.mod h1:pqBibzTcpz0mLqb7is8fo2qf93tcHjItJ3u7Yud8RI4= github.com/coreos/etcd v3.3.10+incompatible/go.mod h1:uF7uidLiAD3TWHmW31ZFd/JWoc32PjwdhPthX9715RE= github.com/coreos/go-etcd v2.0.0+incompatible/go.mod h1:Jez6KQU2B/sWsbdaef3ED8NzMklzPG4d5KIOhIy30Tk= github.com/coreos/go-semver v0.2.0/go.mod h1:nnelYz7RCh+5ahJtPPxZlU+153eP4D4r3EedlOD2RNk= @@ -102,6 +104,8 @@ github.com/ethereum/go-ethereum v1.9.23/go.mod h1:JIfVb6esrqALTExdz9hRYvrP0xBDf6 github.com/fatih/color v1.3.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5KwzbycvMj4= github.com/fatih/color v1.9.0 h1:8xPHl4/q1VyqGIPif1F+1V3Y3lSmrq01EabUW3CoW5s= github.com/fatih/color v1.9.0/go.mod h1:eQcE1qtQxscV5RaZvpXrrb8Drkc3/DdQ+uUYCNjL+zU= +github.com/fatih/color v1.10.0 h1:s36xzo75JdqLaaWoiEHk767eHiwo0598uUxyfiPkDsg= +github.com/fatih/color v1.10.0/go.mod h1:ELkj/draVOlAH/xkhN6mQ50Qd0MPOk5AAr3maGEBuJM= github.com/fjl/memsize v0.0.0-20180418122429-ca190fb6ffbc/go.mod h1:VvhXpOYNQvB+uIk2RvXzuaQtkQJzzIx6lSBe1xv7hi0= github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo= github.com/fsnotify/fsnotify v1.4.9/go.mod h1:znqG4EE+3YCdAaPaxE2ZRY/06pZUdp0tY4IgpuI1SZQ= @@ -189,6 +193,8 @@ github.com/mattn/go-colorable v0.1.4 h1:snbPLB8fVfU9iwbbo30TPtbLRzwWu6aJS6Xh4eaa github.com/mattn/go-colorable v0.1.4/go.mod h1:U0ppj6V5qS13XJ6of8GYAs25YV2eR4EVcfRqFIhoBtE= github.com/mattn/go-colorable v0.1.7 h1:bQGKb3vps/j0E9GfJQ03JyhRuxsvdAanXlT9BTw3mdw= github.com/mattn/go-colorable v0.1.7/go.mod h1:u6P/XSegPjTcexA+o6vUJrdnUu04hMope9wVRipJSqc= +github.com/mattn/go-colorable v0.1.8 h1:c1ghPdyEDarC70ftn0y+A/Ee++9zz8ljHG1b13eJ0s8= +github.com/mattn/go-colorable v0.1.8/go.mod h1:u6P/XSegPjTcexA+o6vUJrdnUu04hMope9wVRipJSqc= github.com/mattn/go-ieproxy v0.0.0-20190610004146-91bb50d98149/go.mod h1:31jz6HNzdxOmlERGGEc4v/dMssOfmp2p5bT/okiKFFc= github.com/mattn/go-ieproxy v0.0.0-20190702010315-6dee0af9227d/go.mod h1:31jz6HNzdxOmlERGGEc4v/dMssOfmp2p5bT/okiKFFc= github.com/mattn/go-isatty v0.0.5-0.20180830101745-3fb116b82035/go.mod h1:M+lRXTBqGeGNdLjl/ufCoiOlB5xdOkqRJdNxMWT7Zi4= diff --git a/services/account_service.go b/services/account_service.go index ce7d72b..37cc080 100644 --- a/services/account_service.go +++ b/services/account_service.go @@ -68,7 +68,6 @@ func (s *AccountAPIService) AccountBalance( return &types.AccountBalanceResponse{ BlockIdentifier: block, - Coins: coins, Balances: []*types.Amount{ { Value: balance, @@ -97,3 +96,41 @@ func (s *AccountAPIService) AccountBalance( }, }, nil } + +// AccountCoins implements /account/coins. +func (s *AccountAPIService) AccountCoins( + ctx context.Context, + request *types.AccountCoinsRequest, +) (*types.AccountCoinsResponse, *types.Error) { + if s.config.Mode != configuration.Online { + return nil, wrapErr(ErrUnavailableOffline, nil) + } + + coins, block, err := s.i.GetCoins(ctx, request.AccountIdentifier) + if err != nil { + return nil, wrapErr(ErrUnableToGetCoins, err) + } + + result := &types.AccountCoinsResponse{ + BlockIdentifier: block, + Coins: coins, + } + //@Todo include_mempool query + + if len(request.Currencies) > 0 { + filtered := []*types.Coin{} + for _, curr := range request.Currencies { + if curr == nil { + continue + } + for _, coin := range coins { + if coin.Amount.Currency.Symbol == curr.Symbol { + filtered = append(filtered, coin) + } + } + } + result.Coins = filtered + } + + return result, nil +} From 364e9fa6caf7e5024f1264ee5e72d7af3b4b69e6 Mon Sep 17 00:00:00 2001 From: Kashif Date: Wed, 11 Nov 2020 20:44:40 +0900 Subject: [PATCH 02/13] finish up coins api --- services/account_service.go | 11 ++++++----- services/network_service.go | 1 + services/types.go | 3 +++ 3 files changed, 10 insertions(+), 5 deletions(-) diff --git a/services/account_service.go b/services/account_service.go index 37cc080..8c8adff 100644 --- a/services/account_service.go +++ b/services/account_service.go @@ -115,16 +115,17 @@ func (s *AccountAPIService) AccountCoins( BlockIdentifier: block, Coins: coins, } - //@Todo include_mempool query + + //@Todo include_mempool query unsupported + //https://github.com/coinbase/rosetta-bitcoin/issues/36#issuecomment-724992022 + //Once mempoolcoins are supported also change the bool service/types.go:MempoolCoins to true if len(request.Currencies) > 0 { filtered := []*types.Coin{} + for _, curr := range request.Currencies { - if curr == nil { - continue - } for _, coin := range coins { - if coin.Amount.Currency.Symbol == curr.Symbol { + if types.Hash(curr) == types.Hash(coin.Amount.Currency) { filtered = append(filtered, coin) } } diff --git a/services/network_service.go b/services/network_service.go index edeb721..04dcb02 100644 --- a/services/network_service.go +++ b/services/network_service.go @@ -99,6 +99,7 @@ func (s *NetworkAPIService) NetworkOptions( OperationTypes: bitcoin.OperationTypes, Errors: Errors, HistoricalBalanceLookup: HistoricalBalanceLookup, + MempoolCoins: MempoolCoins, }, }, nil } diff --git a/services/types.go b/services/types.go index cf64ed2..21edf0e 100644 --- a/services/types.go +++ b/services/types.go @@ -31,6 +31,9 @@ const ( // that historical balance lookup is supported. HistoricalBalanceLookup = true + //Include mempool coins when fetching account coints + MempoolCoins = false + // inlineFetchLimit is the maximum number // of transactions to fetch inline. inlineFetchLimit = 100 From 86db8fb4bfc075ae0878240890e297eb19f76ea7 Mon Sep 17 00:00:00 2001 From: Kashif Date: Fri, 13 Nov 2020 12:37:03 +0900 Subject: [PATCH 03/13] fix breaking changes from rosetta-sdk --- bitcoin/client.go | 13 ++++++++----- bitcoin/client_test.go | 29 +++++++++++++++-------------- go.sum | 13 ------------- indexer/indexer.go | 2 +- indexer/indexer_test.go | 6 ++++-- main.go | 1 + services/account_service_test.go | 1 - 7 files changed, 29 insertions(+), 36 deletions(-) diff --git a/bitcoin/client.go b/bitcoin/client.go index bdb1efd..a3b768f 100644 --- a/bitcoin/client.go +++ b/bitcoin/client.go @@ -516,9 +516,9 @@ func (b *Client) parseTransactions( "block hash", block.Hash, "transaction hash", transaction.Hash, ) - + status := SuccessStatus for _, op := range txOps { - op.Status = SkippedStatus + op.Status = &status } } @@ -675,13 +675,14 @@ func (b *Client) parseOutputTransactionOperation( coinChange = nil } + status := SuccessStatus return &types.Operation{ OperationIdentifier: &types.OperationIdentifier{ Index: index, NetworkIndex: &networkIndex, }, Type: OutputOpType, - Status: SuccessStatus, + Status: &status, Account: account, Amount: &types.Amount{ Value: strconv.FormatInt(int64(amount), 10), @@ -732,13 +733,14 @@ func (b *Client) parseInputTransactionOperation( return nil, fmt.Errorf("%w: unable to negate previous output", err) } + status := SuccessStatus return &types.Operation{ OperationIdentifier: &types.OperationIdentifier{ Index: index, NetworkIndex: &networkIndex, }, Type: InputOpType, - Status: SuccessStatus, + Status: &status, Account: accountCoin.Account, Amount: &types.Amount{ Value: newValue, @@ -794,13 +796,14 @@ func (b *Client) coinbaseTxOperation( return nil, fmt.Errorf("%w: unable to get input metadata", err) } + status := SuccessStatus return &types.Operation{ OperationIdentifier: &types.OperationIdentifier{ Index: index, NetworkIndex: &networkIndex, }, Type: CoinbaseOpType, - Status: SuccessStatus, + Status: &status, Metadata: metadata, }, nil } diff --git a/bitcoin/client_test.go b/bitcoin/client_test.go index 4e1ab94..c0fc785 100644 --- a/bitcoin/client_test.go +++ b/bitcoin/client_test.go @@ -706,6 +706,7 @@ func mustMarshalMap(v interface{}) map[string]interface{} { } func TestParseBlock(t *testing.T) { + status := SuccessStatus tests := map[string]struct { block *Block coins map[string]*storage.AccountCoin @@ -735,7 +736,7 @@ func TestParseBlock(t *testing.T) { NetworkIndex: int64Pointer(0), }, Type: CoinbaseOpType, - Status: SuccessStatus, + Status: &status, Metadata: mustMarshalMap(&OperationMetadata{ Coinbase: "04ffff001d02fd04", Sequence: 4294967295, @@ -747,7 +748,7 @@ func TestParseBlock(t *testing.T) { NetworkIndex: int64Pointer(0), }, Type: OutputOpType, - Status: SuccessStatus, + Status: &status, Account: &types.AccountIdentifier{ Address: "4104f5eeb2b10c944c6b9fbcfff94c35bdeecd93df977882babc7f3a2cf7f5c81d3b09a68db7f0e04f21de5d4230e75e6dbe7ad16eefe0d4325a62067dc6f369446aac", // nolint }, @@ -788,7 +789,7 @@ func TestParseBlock(t *testing.T) { NetworkIndex: int64Pointer(0), }, Type: OutputOpType, - Status: SuccessStatus, + Status: &status, Account: &types.AccountIdentifier{ Address: "mmtKKnjqTPdkBnBMbNt5Yu2SCwpMaEshEL", // nolint }, @@ -820,7 +821,7 @@ func TestParseBlock(t *testing.T) { NetworkIndex: int64Pointer(1), }, Type: OutputOpType, - Status: SuccessStatus, + Status: &status, Account: &types.AccountIdentifier{ Address: "4852fe372ff7534c16713b3146bbc1e86379c70bea4d5c02fb1fa0112980a081:1", }, @@ -928,7 +929,7 @@ func TestParseBlock(t *testing.T) { NetworkIndex: int64Pointer(0), }, Type: CoinbaseOpType, - Status: SuccessStatus, + Status: &status, Metadata: mustMarshalMap(&OperationMetadata{ Coinbase: "044c86041b020602", Sequence: 4294967295, @@ -940,7 +941,7 @@ func TestParseBlock(t *testing.T) { NetworkIndex: int64Pointer(0), }, Type: OutputOpType, - Status: SuccessStatus, + Status: &status, Account: &types.AccountIdentifier{ Address: "34qkc2iac6RsyxZVfyE2S5U5WcRsbg2dpK", }, @@ -972,7 +973,7 @@ func TestParseBlock(t *testing.T) { NetworkIndex: int64Pointer(1), }, Type: OutputOpType, - Status: SuccessStatus, + Status: &status, Account: &types.AccountIdentifier{ Address: "6a24aa21a9ed10109f4b82aa3ed7ec9d02a2a90246478b3308c8b85daf62fe501d58d05727a4", }, @@ -1007,7 +1008,7 @@ func TestParseBlock(t *testing.T) { NetworkIndex: int64Pointer(0), }, Type: InputOpType, - Status: SuccessStatus, + Status: &status, Amount: &types.Amount{ Value: "-5000000000", Currency: MainnetCurrency, @@ -1035,7 +1036,7 @@ func TestParseBlock(t *testing.T) { NetworkIndex: int64Pointer(0), }, Type: OutputOpType, - Status: SuccessStatus, + Status: &status, Account: &types.AccountIdentifier{ Address: "1JqDybm2nWTENrHvMyafbSXXtTk5Uv5QAn", }, @@ -1067,7 +1068,7 @@ func TestParseBlock(t *testing.T) { NetworkIndex: int64Pointer(1), }, Type: OutputOpType, - Status: SuccessStatus, + Status: &status, Account: &types.AccountIdentifier{ Address: "1EYTGtG4LnFfiMvjJdsU7GMGCQvsRSjYhx", }, @@ -1112,7 +1113,7 @@ func TestParseBlock(t *testing.T) { NetworkIndex: int64Pointer(0), }, Type: InputOpType, - Status: SuccessStatus, + Status: &status, Amount: &types.Amount{ Value: "-3467607", Currency: MainnetCurrency, @@ -1144,7 +1145,7 @@ func TestParseBlock(t *testing.T) { NetworkIndex: int64Pointer(1), }, Type: InputOpType, - Status: SuccessStatus, + Status: &status, Amount: &types.Amount{ Value: "0", Currency: MainnetCurrency, @@ -1172,7 +1173,7 @@ func TestParseBlock(t *testing.T) { NetworkIndex: int64Pointer(2), }, Type: InputOpType, - Status: SuccessStatus, + Status: &status, Amount: &types.Amount{ Value: "-556000000", Currency: MainnetCurrency, @@ -1200,7 +1201,7 @@ func TestParseBlock(t *testing.T) { NetworkIndex: int64Pointer(0), }, Type: OutputOpType, - Status: SuccessStatus, + Status: &status, Account: &types.AccountIdentifier{ Address: "76a914c398efa9c392ba6013c5e04ee729755ef7f58b3288ac", }, diff --git a/go.sum b/go.sum index 6c34be9..ab30588 100644 --- a/go.sum +++ b/go.sum @@ -62,8 +62,6 @@ github.com/client9/misspell v0.3.4 h1:ta993UF76GwbvJcIo3Y68y/M3WxlpEHPWIGDkJYwzJ github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw= github.com/cloudflare/cloudflare-go v0.10.2-0.20190916151808-a80f83b9add9/go.mod h1:1MxXX1Ux4x6mqPmjkUgTP1CdXIBXKX7T+Jk9Gxrmx+U= github.com/cncf/udpa/go v0.0.0-20191209042840-269d4d468f6f/go.mod h1:M8M6+tZqaGXZJjfX53e64911xZQV5JYwmTeXPW+k8Sc= -github.com/coinbase/rosetta-sdk-go v0.5.9 h1:CuGQE3HFmYwdEACJnuOtVI9cofqPsGvq6FdFIzaOPKI= -github.com/coinbase/rosetta-sdk-go v0.5.9/go.mod h1:xd4wYUhV3LkY78SPH8BUhc88rXfn2jYgN9BfiSjbcvM= github.com/coinbase/rosetta-sdk-go v0.6.0 h1:U8/NhkPo7CGJR8Ud82Y0HqtujXSVzGZKscmxOHsmS54= github.com/coinbase/rosetta-sdk-go v0.6.0/go.mod h1:pqBibzTcpz0mLqb7is8fo2qf93tcHjItJ3u7Yud8RI4= github.com/coreos/etcd v3.3.10+incompatible/go.mod h1:uF7uidLiAD3TWHmW31ZFd/JWoc32PjwdhPthX9715RE= @@ -102,8 +100,6 @@ github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7 github.com/ethereum/go-ethereum v1.9.23 h1:SIKhg/z4Q7AbvqcxuPYvMxf36che/Rq/Pp0IdYEkbtw= github.com/ethereum/go-ethereum v1.9.23/go.mod h1:JIfVb6esrqALTExdz9hRYvrP0xBDf6wCncIu1hNwHpM= github.com/fatih/color v1.3.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5KwzbycvMj4= -github.com/fatih/color v1.9.0 h1:8xPHl4/q1VyqGIPif1F+1V3Y3lSmrq01EabUW3CoW5s= -github.com/fatih/color v1.9.0/go.mod h1:eQcE1qtQxscV5RaZvpXrrb8Drkc3/DdQ+uUYCNjL+zU= github.com/fatih/color v1.10.0 h1:s36xzo75JdqLaaWoiEHk767eHiwo0598uUxyfiPkDsg= github.com/fatih/color v1.10.0/go.mod h1:ELkj/draVOlAH/xkhN6mQ50Qd0MPOk5AAr3maGEBuJM= github.com/fjl/memsize v0.0.0-20180418122429-ca190fb6ffbc/go.mod h1:VvhXpOYNQvB+uIk2RvXzuaQtkQJzzIx6lSBe1xv7hi0= @@ -189,18 +185,11 @@ github.com/lucasjones/reggen v0.0.0-20180717132126-cdb49ff09d77 h1:6xiz3+ZczT3M4 github.com/lucasjones/reggen v0.0.0-20180717132126-cdb49ff09d77/go.mod h1:5ELEyG+X8f+meRWHuqUOewBOhvHkl7M76pdGEansxW4= github.com/magiconair/properties v1.8.0/go.mod h1:PppfXfuXeibc/6YijjN8zIbojt8czPbwD3XqdrwzmxQ= github.com/mattn/go-colorable v0.1.0/go.mod h1:9vuHe8Xs5qXnSaW/c/ABM9alt+Vo+STaOChaDxuIBZU= -github.com/mattn/go-colorable v0.1.4 h1:snbPLB8fVfU9iwbbo30TPtbLRzwWu6aJS6Xh4eaaviA= -github.com/mattn/go-colorable v0.1.4/go.mod h1:U0ppj6V5qS13XJ6of8GYAs25YV2eR4EVcfRqFIhoBtE= -github.com/mattn/go-colorable v0.1.7 h1:bQGKb3vps/j0E9GfJQ03JyhRuxsvdAanXlT9BTw3mdw= -github.com/mattn/go-colorable v0.1.7/go.mod h1:u6P/XSegPjTcexA+o6vUJrdnUu04hMope9wVRipJSqc= github.com/mattn/go-colorable v0.1.8 h1:c1ghPdyEDarC70ftn0y+A/Ee++9zz8ljHG1b13eJ0s8= github.com/mattn/go-colorable v0.1.8/go.mod h1:u6P/XSegPjTcexA+o6vUJrdnUu04hMope9wVRipJSqc= github.com/mattn/go-ieproxy v0.0.0-20190610004146-91bb50d98149/go.mod h1:31jz6HNzdxOmlERGGEc4v/dMssOfmp2p5bT/okiKFFc= github.com/mattn/go-ieproxy v0.0.0-20190702010315-6dee0af9227d/go.mod h1:31jz6HNzdxOmlERGGEc4v/dMssOfmp2p5bT/okiKFFc= github.com/mattn/go-isatty v0.0.5-0.20180830101745-3fb116b82035/go.mod h1:M+lRXTBqGeGNdLjl/ufCoiOlB5xdOkqRJdNxMWT7Zi4= -github.com/mattn/go-isatty v0.0.8/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s= -github.com/mattn/go-isatty v0.0.11 h1:FxPOTFNqGkuDUGi3H/qkUbQO4ZiBa2brKq5r0l8TGeM= -github.com/mattn/go-isatty v0.0.11/go.mod h1:PhnuNfih5lzO57/f3n+odYbM4JtupLOxQOAqxQCu2WE= github.com/mattn/go-isatty v0.0.12 h1:wuysRhFDzyxgEmMf5xjvJ2M9dZoWAXNNr5LSBS7uHXY= github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU= github.com/mattn/go-runewidth v0.0.3/go.mod h1:LwmH8dsx7+W8Uxz3IHJYH5QSwggIsqBzpuz5H//U1FU= @@ -363,7 +352,6 @@ golang.org/x/sys v0.0.0-20180909124046-d0be0721c37e/go.mod h1:STP8DvDyc/dI5b8T5h golang.org/x/sys v0.0.0-20181107165924-66b7b1311ac8/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20181205085412-a5c9d58dba9a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= -golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190312061237-fead79001313/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20190422165155-953cdadca894/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= @@ -371,7 +359,6 @@ golang.org/x/sys v0.0.0-20190626221950-04f50cda93cb/go.mod h1:h1NjWce9XRLGQEsW7w golang.org/x/sys v0.0.0-20190904154756-749cb33beabd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20191001151750-bb3f8db39f24/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20191005200804-aed5e4c7ecf9/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20191120155948-bd437916bb0e/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= diff --git a/indexer/indexer.go b/indexer/indexer.go index 47a3649..1f50002 100644 --- a/indexer/indexer.go +++ b/indexer/indexer.go @@ -255,7 +255,7 @@ func (i *Indexer) Sync(ctx context.Context) error { // If previously processed blocks exist in storage, they are fetched. // Otherwise, none are provided to the cache (the syncer will not attempt // a reorg if the cache is empty). - pastBlocks := i.blockStorage.CreateBlockCache(ctx) + pastBlocks := i.blockStorage.CreateBlockCache(ctx, int(head.Index)) syncer := syncer.New( i.network, diff --git a/indexer/indexer_test.go b/indexer/indexer_test.go index 314a2f5..5ea8b4a 100644 --- a/indexer/indexer_test.go +++ b/indexer/indexer_test.go @@ -267,6 +267,7 @@ func TestIndexer_Transactions(t *testing.T) { } transactions := []*types.Transaction{} + status := bitcoin.SuccessStatus for j := 0; j < 5; j++ { rawHash := fmt.Sprintf("block %d transaction %d", i, j) hash := fmt.Sprintf("%x", sha256.Sum256([]byte(rawHash))) @@ -286,7 +287,7 @@ func TestIndexer_Transactions(t *testing.T) { Index: 0, NetworkIndex: &index0, }, - Status: bitcoin.SuccessStatus, + Status: &status, Type: bitcoin.OutputOpType, Account: &types.AccountIdentifier{ Address: rawHash, @@ -486,6 +487,7 @@ func TestIndexer_Reorg(t *testing.T) { } transactions := []*types.Transaction{} + status := bitcoin.SuccessStatus for j := 0; j < 5; j++ { rawHash := fmt.Sprintf("block %d transaction %d", i, j) hash := fmt.Sprintf("%x", sha256.Sum256([]byte(rawHash))) @@ -505,7 +507,7 @@ func TestIndexer_Reorg(t *testing.T) { Index: 0, NetworkIndex: &index0, }, - Status: bitcoin.SuccessStatus, + Status: &status, Type: bitcoin.OutputOpType, Account: &types.AccountIdentifier{ Address: rawHash, diff --git a/main.go b/main.go index 4b5d1c6..e8f110e 100644 --- a/main.go +++ b/main.go @@ -157,6 +157,7 @@ func main() { services.HistoricalBalanceLookup, []*types.NetworkIdentifier{cfg.Network}, nil, + false, ) if err != nil { logger.Fatalw("unable to create new server asserter", "error", err) diff --git a/services/account_service_test.go b/services/account_service_test.go index ce94fb1..323e6c5 100644 --- a/services/account_service_test.go +++ b/services/account_service_test.go @@ -93,7 +93,6 @@ func TestAccountBalance_Online_Current(t *testing.T) { assert.Equal(t, &types.AccountBalanceResponse{ BlockIdentifier: block, - Coins: coins, Balances: []*types.Amount{ { Value: "25", From bbc56790ebb8d58eb4be61218ceeab300fef52c4 Mon Sep 17 00:00:00 2001 From: Patrick O'Grady Date: Fri, 13 Nov 2020 11:13:09 -0800 Subject: [PATCH 04/13] Update rosetta-sdk-go --- go.mod | 2 +- go.sum | 10 ++++++---- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/go.mod b/go.mod index 940e956..45547a5 100644 --- a/go.mod +++ b/go.mod @@ -5,7 +5,7 @@ go 1.13 require ( github.com/btcsuite/btcd v0.21.0-beta github.com/btcsuite/btcutil v1.0.2 - github.com/coinbase/rosetta-sdk-go v0.6.0 + github.com/coinbase/rosetta-sdk-go v0.6.1 github.com/dgraph-io/badger/v2 v2.2007.2 github.com/grpc-ecosystem/go-grpc-middleware v1.2.2 github.com/stretchr/testify v1.6.1 diff --git a/go.sum b/go.sum index ab30588..57fef7a 100644 --- a/go.sum +++ b/go.sum @@ -62,8 +62,8 @@ github.com/client9/misspell v0.3.4 h1:ta993UF76GwbvJcIo3Y68y/M3WxlpEHPWIGDkJYwzJ github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw= github.com/cloudflare/cloudflare-go v0.10.2-0.20190916151808-a80f83b9add9/go.mod h1:1MxXX1Ux4x6mqPmjkUgTP1CdXIBXKX7T+Jk9Gxrmx+U= github.com/cncf/udpa/go v0.0.0-20191209042840-269d4d468f6f/go.mod h1:M8M6+tZqaGXZJjfX53e64911xZQV5JYwmTeXPW+k8Sc= -github.com/coinbase/rosetta-sdk-go v0.6.0 h1:U8/NhkPo7CGJR8Ud82Y0HqtujXSVzGZKscmxOHsmS54= -github.com/coinbase/rosetta-sdk-go v0.6.0/go.mod h1:pqBibzTcpz0mLqb7is8fo2qf93tcHjItJ3u7Yud8RI4= +github.com/coinbase/rosetta-sdk-go v0.6.1 h1:aOb5qstlX0uqP9HRC7wCY+YAZDzZbS2C/i3Qy/lR3xM= +github.com/coinbase/rosetta-sdk-go v0.6.1/go.mod h1:t36UuaD4p2DSXaSH9IwMasZDJ7UPxt9cQi6alS5OPTo= github.com/coreos/etcd v3.3.10+incompatible/go.mod h1:uF7uidLiAD3TWHmW31ZFd/JWoc32PjwdhPthX9715RE= github.com/coreos/go-etcd v2.0.0+incompatible/go.mod h1:Jez6KQU2B/sWsbdaef3ED8NzMklzPG4d5KIOhIy30Tk= github.com/coreos/go-semver v0.2.0/go.mod h1:nnelYz7RCh+5ahJtPPxZlU+153eP4D4r3EedlOD2RNk= @@ -97,8 +97,8 @@ github.com/envoyproxy/go-control-plane v0.9.0/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymF github.com/envoyproxy/go-control-plane v0.9.1-0.20191026205805-5f8ba28d4473/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4= github.com/envoyproxy/go-control-plane v0.9.4/go.mod h1:6rpuAdCZL397s3pYoYcLgu1mIlRU8Am5FuJP05cCM98= github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c= -github.com/ethereum/go-ethereum v1.9.23 h1:SIKhg/z4Q7AbvqcxuPYvMxf36che/Rq/Pp0IdYEkbtw= -github.com/ethereum/go-ethereum v1.9.23/go.mod h1:JIfVb6esrqALTExdz9hRYvrP0xBDf6wCncIu1hNwHpM= +github.com/ethereum/go-ethereum v1.9.24 h1:6AK+ORt3EMDO+FTjzXy/AQwHMbu52J2nYHIjyQX9azQ= +github.com/ethereum/go-ethereum v1.9.24/go.mod h1:JIfVb6esrqALTExdz9hRYvrP0xBDf6wCncIu1hNwHpM= github.com/fatih/color v1.3.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5KwzbycvMj4= github.com/fatih/color v1.10.0 h1:s36xzo75JdqLaaWoiEHk767eHiwo0598uUxyfiPkDsg= github.com/fatih/color v1.10.0/go.mod h1:ELkj/draVOlAH/xkhN6mQ50Qd0MPOk5AAr3maGEBuJM= @@ -263,6 +263,8 @@ github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/ github.com/syndtr/goleveldb v1.0.1-0.20200815110645-5c35d600f0ca/go.mod h1:u2MKkTVTVJWe5D1rCvame8WqhBd88EuIwODJZ1VHCPM= github.com/tidwall/gjson v1.6.1 h1:LRbvNuNuvAiISWg6gxLEFuCe72UKy5hDqhxW/8183ws= github.com/tidwall/gjson v1.6.1/go.mod h1:BaHyNc5bjzYkPqgLq7mdVzeiRtULKULXLgZFKsxEHI0= +github.com/tidwall/gjson v1.6.3 h1:aHoiiem0dr7GHkW001T1SMTJ7X5PvyekH5WX0whWGnI= +github.com/tidwall/gjson v1.6.3/go.mod h1:BaHyNc5bjzYkPqgLq7mdVzeiRtULKULXLgZFKsxEHI0= github.com/tidwall/match v1.0.1 h1:PnKP62LPNxHKTwvHHZZzdOAOCtsJTjo6dZLCwpKm5xc= github.com/tidwall/match v1.0.1/go.mod h1:LujAq0jyVjBy028G1WhWfIzbpQfMO8bBZ6Tyb0+pL9E= github.com/tidwall/pretty v1.0.2 h1:Z7S3cePv9Jwm1KwS0513MRaoUe3S01WPbLNV40pwWZU= From bfa31a74c5e6297fe1d195a7612f4fc7522809ec Mon Sep 17 00:00:00 2001 From: Patrick O'Grady Date: Fri, 13 Nov 2020 11:14:54 -0800 Subject: [PATCH 05/13] Update golang in Dockerfile --- Dockerfile | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Dockerfile b/Dockerfile index 7ae05e4..1e7391a 100644 --- a/Dockerfile +++ b/Dockerfile @@ -44,8 +44,8 @@ RUN mkdir -p /app \ WORKDIR /app RUN apt-get update && apt-get install -y curl make gcc g++ -ENV GOLANG_VERSION 1.15.2 -ENV GOLANG_DOWNLOAD_SHA256 b49fda1ca29a1946d6bb2a5a6982cf07ccd2aba849289508ee0f9918f6bb4552 +ENV GOLANG_VERSION 1.15.5 +ENV GOLANG_DOWNLOAD_SHA256 9a58494e8da722c3aef248c9227b0e9c528c7318309827780f16220998180a0d ENV GOLANG_DOWNLOAD_URL https://golang.org/dl/go$GOLANG_VERSION.linux-amd64.tar.gz RUN curl -fsSL "$GOLANG_DOWNLOAD_URL" -o golang.tar.gz \ From dfec6d4b1a1a1f2439c4e6dee295fe30e6afc935 Mon Sep 17 00:00:00 2001 From: Patrick O'Grady Date: Fri, 13 Nov 2020 11:15:21 -0800 Subject: [PATCH 06/13] update config --- .circleci/config.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.circleci/config.yml b/.circleci/config.yml index c6bbe5c..eea4d04 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -16,7 +16,7 @@ version: 2.1 executors: default: docker: - - image: circleci/golang:1.13 + - image: circleci/golang:1.15 user: root # go directory is owned by root working_directory: /go/src/github.com/coinbase/rosetta-bitcoin environment: From dca1a597ed0f2004d76eabb23298aee59db27922 Mon Sep 17 00:00:00 2001 From: Patrick O'Grady Date: Fri, 13 Nov 2020 11:19:50 -0800 Subject: [PATCH 07/13] remove coin filtering for now --- services/account_service.go | 25 ++++++++----------------- 1 file changed, 8 insertions(+), 17 deletions(-) diff --git a/services/account_service.go b/services/account_service.go index 8c8adff..5edcfa4 100644 --- a/services/account_service.go +++ b/services/account_service.go @@ -49,6 +49,8 @@ func (s *AccountAPIService) AccountBalance( return nil, wrapErr(ErrUnavailableOffline, nil) } + // TODO: filter balances by request currencies + // If we are fetching the current balance, // return all coins for an address and calculate // the balance from those coins. @@ -106,6 +108,12 @@ func (s *AccountAPIService) AccountCoins( return nil, wrapErr(ErrUnavailableOffline, nil) } + // TODO: filter coins by request currencies + + // TODO: support include_mempool query + // https://github.com/coinbase/rosetta-bitcoin/issues/36#issuecomment-724992022 + // Once mempoolcoins are supported also change the bool service/types.go:MempoolCoins to true + coins, block, err := s.i.GetCoins(ctx, request.AccountIdentifier) if err != nil { return nil, wrapErr(ErrUnableToGetCoins, err) @@ -116,22 +124,5 @@ func (s *AccountAPIService) AccountCoins( Coins: coins, } - //@Todo include_mempool query unsupported - //https://github.com/coinbase/rosetta-bitcoin/issues/36#issuecomment-724992022 - //Once mempoolcoins are supported also change the bool service/types.go:MempoolCoins to true - - if len(request.Currencies) > 0 { - filtered := []*types.Coin{} - - for _, curr := range request.Currencies { - for _, coin := range coins { - if types.Hash(curr) == types.Hash(coin.Amount.Currency) { - filtered = append(filtered, coin) - } - } - } - result.Coins = filtered - } - return result, nil } From b34f37b5e7332dbd5afdcbaa20c5e5c1c779674d Mon Sep 17 00:00:00 2001 From: Patrick O'Grady Date: Fri, 13 Nov 2020 11:24:06 -0800 Subject: [PATCH 08/13] Cleanup IncludeMempoolCoins --- main.go | 2 +- services/network_service.go | 2 +- services/types.go | 6 ++++-- 3 files changed, 6 insertions(+), 4 deletions(-) diff --git a/main.go b/main.go index e8f110e..282cd3e 100644 --- a/main.go +++ b/main.go @@ -157,7 +157,7 @@ func main() { services.HistoricalBalanceLookup, []*types.NetworkIdentifier{cfg.Network}, nil, - false, + services.IncludeMempoolCoins, ) if err != nil { logger.Fatalw("unable to create new server asserter", "error", err) diff --git a/services/network_service.go b/services/network_service.go index 04dcb02..ce9f879 100644 --- a/services/network_service.go +++ b/services/network_service.go @@ -99,7 +99,7 @@ func (s *NetworkAPIService) NetworkOptions( OperationTypes: bitcoin.OperationTypes, Errors: Errors, HistoricalBalanceLookup: HistoricalBalanceLookup, - MempoolCoins: MempoolCoins, + MempoolCoins: IncludeMempoolCoins, }, }, nil } diff --git a/services/types.go b/services/types.go index 21edf0e..9dce8ab 100644 --- a/services/types.go +++ b/services/types.go @@ -31,8 +31,10 @@ const ( // that historical balance lookup is supported. HistoricalBalanceLookup = true - //Include mempool coins when fetching account coints - MempoolCoins = false + // IncludeMempoolCoins indicates that + // including mempool coins in the /account/coins + // response is not supported. + IncludeMempoolCoins = false // inlineFetchLimit is the maximum number // of transactions to fetch inline. From 27faa31c7a78192bc36ea0df0a17323187db77e3 Mon Sep 17 00:00:00 2001 From: Patrick O'Grady Date: Fri, 13 Nov 2020 11:29:02 -0800 Subject: [PATCH 09/13] Fix statuses --- bitcoin/client.go | 12 ++++-------- indexer/indexer.go | 2 +- indexer/indexer_test.go | 3 +-- 3 files changed, 6 insertions(+), 11 deletions(-) diff --git a/bitcoin/client.go b/bitcoin/client.go index a3b768f..203c60b 100644 --- a/bitcoin/client.go +++ b/bitcoin/client.go @@ -516,9 +516,8 @@ func (b *Client) parseTransactions( "block hash", block.Hash, "transaction hash", transaction.Hash, ) - status := SuccessStatus for _, op := range txOps { - op.Status = &status + op.Status = types.String(SkippedStatus) } } @@ -675,14 +674,13 @@ func (b *Client) parseOutputTransactionOperation( coinChange = nil } - status := SuccessStatus return &types.Operation{ OperationIdentifier: &types.OperationIdentifier{ Index: index, NetworkIndex: &networkIndex, }, Type: OutputOpType, - Status: &status, + Status: types.String(SuccessStatus), Account: account, Amount: &types.Amount{ Value: strconv.FormatInt(int64(amount), 10), @@ -733,14 +731,13 @@ func (b *Client) parseInputTransactionOperation( return nil, fmt.Errorf("%w: unable to negate previous output", err) } - status := SuccessStatus return &types.Operation{ OperationIdentifier: &types.OperationIdentifier{ Index: index, NetworkIndex: &networkIndex, }, Type: InputOpType, - Status: &status, + Status: types.String(SuccessStatus), Account: accountCoin.Account, Amount: &types.Amount{ Value: newValue, @@ -796,14 +793,13 @@ func (b *Client) coinbaseTxOperation( return nil, fmt.Errorf("%w: unable to get input metadata", err) } - status := SuccessStatus return &types.Operation{ OperationIdentifier: &types.OperationIdentifier{ Index: index, NetworkIndex: &networkIndex, }, Type: CoinbaseOpType, - Status: &status, + Status: types.String(SuccessStatus), Metadata: metadata, }, nil } diff --git a/indexer/indexer.go b/indexer/indexer.go index 1f50002..794789f 100644 --- a/indexer/indexer.go +++ b/indexer/indexer.go @@ -255,7 +255,7 @@ func (i *Indexer) Sync(ctx context.Context) error { // If previously processed blocks exist in storage, they are fetched. // Otherwise, none are provided to the cache (the syncer will not attempt // a reorg if the cache is empty). - pastBlocks := i.blockStorage.CreateBlockCache(ctx, int(head.Index)) + pastBlocks := i.blockStorage.CreateBlockCache(ctx, syncer.DefaultPastBlockLimit) syncer := syncer.New( i.network, diff --git a/indexer/indexer_test.go b/indexer/indexer_test.go index 5ea8b4a..79e3244 100644 --- a/indexer/indexer_test.go +++ b/indexer/indexer_test.go @@ -487,7 +487,6 @@ func TestIndexer_Reorg(t *testing.T) { } transactions := []*types.Transaction{} - status := bitcoin.SuccessStatus for j := 0; j < 5; j++ { rawHash := fmt.Sprintf("block %d transaction %d", i, j) hash := fmt.Sprintf("%x", sha256.Sum256([]byte(rawHash))) @@ -507,7 +506,7 @@ func TestIndexer_Reorg(t *testing.T) { Index: 0, NetworkIndex: &index0, }, - Status: &status, + Status: types.String(bitcoin.SuccessStatus), Type: bitcoin.OutputOpType, Account: &types.AccountIdentifier{ Address: rawHash, From f3d07c5e0f5e9448e55eb751a3879cf45f97e9d6 Mon Sep 17 00:00:00 2001 From: Patrick O'Grady Date: Fri, 13 Nov 2020 11:34:24 -0800 Subject: [PATCH 10/13] Fix parse tests --- bitcoin/client_test.go | 29 ++++++++++++++--------------- 1 file changed, 14 insertions(+), 15 deletions(-) diff --git a/bitcoin/client_test.go b/bitcoin/client_test.go index c0fc785..d575612 100644 --- a/bitcoin/client_test.go +++ b/bitcoin/client_test.go @@ -706,7 +706,6 @@ func mustMarshalMap(v interface{}) map[string]interface{} { } func TestParseBlock(t *testing.T) { - status := SuccessStatus tests := map[string]struct { block *Block coins map[string]*storage.AccountCoin @@ -736,7 +735,7 @@ func TestParseBlock(t *testing.T) { NetworkIndex: int64Pointer(0), }, Type: CoinbaseOpType, - Status: &status, + Status: types.String(SuccessStatus), Metadata: mustMarshalMap(&OperationMetadata{ Coinbase: "04ffff001d02fd04", Sequence: 4294967295, @@ -748,7 +747,7 @@ func TestParseBlock(t *testing.T) { NetworkIndex: int64Pointer(0), }, Type: OutputOpType, - Status: &status, + Status: types.String(SuccessStatus), Account: &types.AccountIdentifier{ Address: "4104f5eeb2b10c944c6b9fbcfff94c35bdeecd93df977882babc7f3a2cf7f5c81d3b09a68db7f0e04f21de5d4230e75e6dbe7ad16eefe0d4325a62067dc6f369446aac", // nolint }, @@ -789,7 +788,7 @@ func TestParseBlock(t *testing.T) { NetworkIndex: int64Pointer(0), }, Type: OutputOpType, - Status: &status, + Status: types.String(SuccessStatus), Account: &types.AccountIdentifier{ Address: "mmtKKnjqTPdkBnBMbNt5Yu2SCwpMaEshEL", // nolint }, @@ -821,7 +820,7 @@ func TestParseBlock(t *testing.T) { NetworkIndex: int64Pointer(1), }, Type: OutputOpType, - Status: &status, + Status: types.String(SuccessStatus), Account: &types.AccountIdentifier{ Address: "4852fe372ff7534c16713b3146bbc1e86379c70bea4d5c02fb1fa0112980a081:1", }, @@ -929,7 +928,7 @@ func TestParseBlock(t *testing.T) { NetworkIndex: int64Pointer(0), }, Type: CoinbaseOpType, - Status: &status, + Status: types.String(SuccessStatus), Metadata: mustMarshalMap(&OperationMetadata{ Coinbase: "044c86041b020602", Sequence: 4294967295, @@ -941,7 +940,7 @@ func TestParseBlock(t *testing.T) { NetworkIndex: int64Pointer(0), }, Type: OutputOpType, - Status: &status, + Status: types.String(SuccessStatus), Account: &types.AccountIdentifier{ Address: "34qkc2iac6RsyxZVfyE2S5U5WcRsbg2dpK", }, @@ -973,7 +972,7 @@ func TestParseBlock(t *testing.T) { NetworkIndex: int64Pointer(1), }, Type: OutputOpType, - Status: &status, + Status: types.String(SuccessStatus), Account: &types.AccountIdentifier{ Address: "6a24aa21a9ed10109f4b82aa3ed7ec9d02a2a90246478b3308c8b85daf62fe501d58d05727a4", }, @@ -1008,7 +1007,7 @@ func TestParseBlock(t *testing.T) { NetworkIndex: int64Pointer(0), }, Type: InputOpType, - Status: &status, + Status: types.String(SuccessStatus), Amount: &types.Amount{ Value: "-5000000000", Currency: MainnetCurrency, @@ -1036,7 +1035,7 @@ func TestParseBlock(t *testing.T) { NetworkIndex: int64Pointer(0), }, Type: OutputOpType, - Status: &status, + Status: types.String(SuccessStatus), Account: &types.AccountIdentifier{ Address: "1JqDybm2nWTENrHvMyafbSXXtTk5Uv5QAn", }, @@ -1068,7 +1067,7 @@ func TestParseBlock(t *testing.T) { NetworkIndex: int64Pointer(1), }, Type: OutputOpType, - Status: &status, + Status: types.String(SuccessStatus), Account: &types.AccountIdentifier{ Address: "1EYTGtG4LnFfiMvjJdsU7GMGCQvsRSjYhx", }, @@ -1113,7 +1112,7 @@ func TestParseBlock(t *testing.T) { NetworkIndex: int64Pointer(0), }, Type: InputOpType, - Status: &status, + Status: types.String(SuccessStatus), Amount: &types.Amount{ Value: "-3467607", Currency: MainnetCurrency, @@ -1145,7 +1144,7 @@ func TestParseBlock(t *testing.T) { NetworkIndex: int64Pointer(1), }, Type: InputOpType, - Status: &status, + Status: types.String(SuccessStatus), Amount: &types.Amount{ Value: "0", Currency: MainnetCurrency, @@ -1173,7 +1172,7 @@ func TestParseBlock(t *testing.T) { NetworkIndex: int64Pointer(2), }, Type: InputOpType, - Status: &status, + Status: types.String(SuccessStatus), Amount: &types.Amount{ Value: "-556000000", Currency: MainnetCurrency, @@ -1201,7 +1200,7 @@ func TestParseBlock(t *testing.T) { NetworkIndex: int64Pointer(0), }, Type: OutputOpType, - Status: &status, + Status: types.String(SuccessStatus), Account: &types.AccountIdentifier{ Address: "76a914c398efa9c392ba6013c5e04ee729755ef7f58b3288ac", }, From be420054412ebd2f3ff7caf87273df9ca117255d Mon Sep 17 00:00:00 2001 From: Patrick O'Grady Date: Fri, 13 Nov 2020 11:36:31 -0800 Subject: [PATCH 11/13] Update middleware version --- services/network_service.go | 2 +- services/network_service_test.go | 2 +- services/types.go | 4 +--- 3 files changed, 3 insertions(+), 5 deletions(-) diff --git a/services/network_service.go b/services/network_service.go index ce9f879..f32a901 100644 --- a/services/network_service.go +++ b/services/network_service.go @@ -92,7 +92,7 @@ func (s *NetworkAPIService) NetworkOptions( Version: &types.Version{ RosettaVersion: types.RosettaAPIVersion, NodeVersion: NodeVersion, - MiddlewareVersion: &MiddlewareVersion, + MiddlewareVersion: types.String(MiddlewareVersion), }, Allow: &types.Allow{ OperationStatuses: bitcoin.OperationStatuses, diff --git a/services/network_service_test.go b/services/network_service_test.go index 10dbd28..43fd014 100644 --- a/services/network_service_test.go +++ b/services/network_service_test.go @@ -27,7 +27,7 @@ import ( ) var ( - middlewareVersion = "0.0.6" + middlewareVersion = "0.0.7" defaultNetworkOptions = &types.NetworkOptionsResponse{ Version: &types.Version{ RosettaVersion: types.RosettaAPIVersion, diff --git a/services/types.go b/services/types.go index 9dce8ab..e8cf044 100644 --- a/services/types.go +++ b/services/types.go @@ -39,15 +39,13 @@ const ( // inlineFetchLimit is the maximum number // of transactions to fetch inline. inlineFetchLimit = 100 -) -var ( // MiddlewareVersion is the version // of rosetta-bitcoin. We set this as a // variable instead of a constant because // we typically need the pointer of this // value. - MiddlewareVersion = "0.0.6" + MiddlewareVersion = "0.0.7" ) // Client is used by the servicers to get Peer information From d9fb29482634d9f00e36a603da030fd2745295f3 Mon Sep 17 00:00:00 2001 From: Patrick O'Grady Date: Fri, 13 Nov 2020 11:42:33 -0800 Subject: [PATCH 12/13] Fix account_service --- services/account_service.go | 28 -------- services/account_service_test.go | 108 +++++++++++++++++++++---------- 2 files changed, 74 insertions(+), 62 deletions(-) diff --git a/services/account_service.go b/services/account_service.go index 5edcfa4..748fcad 100644 --- a/services/account_service.go +++ b/services/account_service.go @@ -51,34 +51,6 @@ func (s *AccountAPIService) AccountBalance( // TODO: filter balances by request currencies - // If we are fetching the current balance, - // return all coins for an address and calculate - // the balance from those coins. - if request.BlockIdentifier == nil { - coins, block, err := s.i.GetCoins(ctx, request.AccountIdentifier) - if err != nil { - return nil, wrapErr(ErrUnableToGetCoins, err) - } - - balance := "0" - for _, coin := range coins { - balance, err = types.AddValues(balance, coin.Amount.Value) - if err != nil { - return nil, wrapErr(ErrUnableToParseIntermediateResult, err) - } - } - - return &types.AccountBalanceResponse{ - BlockIdentifier: block, - Balances: []*types.Amount{ - { - Value: balance, - Currency: s.config.Currency, - }, - }, - }, nil - } - // If we are fetching a historical balance, // use balance storage and don't return coins. amount, block, err := s.i.GetBalance( diff --git a/services/account_service_test.go b/services/account_service_test.go index 323e6c5..172661d 100644 --- a/services/account_service_test.go +++ b/services/account_service_test.go @@ -38,6 +38,10 @@ func TestAccountBalance_Offline(t *testing.T) { assert.Nil(t, bal) assert.Equal(t, ErrUnavailableOffline.Code, err.Code) + coins, err := servicer.AccountCoins(ctx, &types.AccountCoinsRequest{}) + assert.Nil(t, coins) + assert.Equal(t, ErrUnavailableOffline.Code, err.Code) + mockIndexer.AssertExpectations(t) } @@ -49,55 +53,33 @@ func TestAccountBalance_Online_Current(t *testing.T) { mockIndexer := &mocks.Indexer{} servicer := NewAccountAPIService(cfg, mockIndexer) ctx := context.Background() - account := &types.AccountIdentifier{ Address: "hello", } - - coins := []*types.Coin{ - { - Amount: &types.Amount{ - Value: "10", - }, - CoinIdentifier: &types.CoinIdentifier{ - Identifier: "coin 1", - }, - }, - { - Amount: &types.Amount{ - Value: "15", - }, - CoinIdentifier: &types.CoinIdentifier{ - Identifier: "coin 2", - }, - }, - { - Amount: &types.Amount{ - Value: "0", - }, - CoinIdentifier: &types.CoinIdentifier{ - Identifier: "coin 3", - }, - }, - } block := &types.BlockIdentifier{ Index: 1000, Hash: "block 1000", } - mockIndexer.On("GetCoins", ctx, account).Return(coins, block, nil).Once() + amount := &types.Amount{ + Value: "25", + Currency: bitcoin.MainnetCurrency, + } + mockIndexer.On( + "GetBalance", + ctx, + account, + bitcoin.MainnetCurrency, + (*types.PartialBlockIdentifier)(nil), + ).Return(amount, block, nil).Once() bal, err := servicer.AccountBalance(ctx, &types.AccountBalanceRequest{ AccountIdentifier: account, }) assert.Nil(t, err) - assert.Equal(t, &types.AccountBalanceResponse{ BlockIdentifier: block, Balances: []*types.Amount{ - { - Value: "25", - Currency: bitcoin.MainnetCurrency, - }, + amount, }, }, bal) @@ -148,3 +130,61 @@ func TestAccountBalance_Online_Historical(t *testing.T) { mockIndexer.AssertExpectations(t) } + +func TestAccountCoins_Online(t *testing.T) { + cfg := &configuration.Configuration{ + Mode: configuration.Online, + Currency: bitcoin.MainnetCurrency, + } + mockIndexer := &mocks.Indexer{} + servicer := NewAccountAPIService(cfg, mockIndexer) + ctx := context.Background() + + account := &types.AccountIdentifier{ + Address: "hello", + } + + coins := []*types.Coin{ + { + Amount: &types.Amount{ + Value: "10", + }, + CoinIdentifier: &types.CoinIdentifier{ + Identifier: "coin 1", + }, + }, + { + Amount: &types.Amount{ + Value: "15", + }, + CoinIdentifier: &types.CoinIdentifier{ + Identifier: "coin 2", + }, + }, + { + Amount: &types.Amount{ + Value: "0", + }, + CoinIdentifier: &types.CoinIdentifier{ + Identifier: "coin 3", + }, + }, + } + block := &types.BlockIdentifier{ + Index: 1000, + Hash: "block 1000", + } + mockIndexer.On("GetCoins", ctx, account).Return(coins, block, nil).Once() + + bal, err := servicer.AccountCoins(ctx, &types.AccountCoinsRequest{ + AccountIdentifier: account, + }) + assert.Nil(t, err) + + assert.Equal(t, &types.AccountCoinsResponse{ + BlockIdentifier: block, + Coins: coins, + }, bal) + + mockIndexer.AssertExpectations(t) +} From 0b061b9e2772845a6dc51233258addd244693618 Mon Sep 17 00:00:00 2001 From: Patrick O'Grady Date: Fri, 13 Nov 2020 11:49:32 -0800 Subject: [PATCH 13/13] nits --- indexer/indexer_test.go | 3 +-- main.go | 2 +- services/network_service.go | 2 +- services/types.go | 4 ++-- 4 files changed, 5 insertions(+), 6 deletions(-) diff --git a/indexer/indexer_test.go b/indexer/indexer_test.go index 79e3244..d91f477 100644 --- a/indexer/indexer_test.go +++ b/indexer/indexer_test.go @@ -267,7 +267,6 @@ func TestIndexer_Transactions(t *testing.T) { } transactions := []*types.Transaction{} - status := bitcoin.SuccessStatus for j := 0; j < 5; j++ { rawHash := fmt.Sprintf("block %d transaction %d", i, j) hash := fmt.Sprintf("%x", sha256.Sum256([]byte(rawHash))) @@ -287,7 +286,7 @@ func TestIndexer_Transactions(t *testing.T) { Index: 0, NetworkIndex: &index0, }, - Status: &status, + Status: types.String(bitcoin.SuccessStatus), Type: bitcoin.OutputOpType, Account: &types.AccountIdentifier{ Address: rawHash, diff --git a/main.go b/main.go index 282cd3e..eaba0ce 100644 --- a/main.go +++ b/main.go @@ -157,7 +157,7 @@ func main() { services.HistoricalBalanceLookup, []*types.NetworkIdentifier{cfg.Network}, nil, - services.IncludeMempoolCoins, + services.MempoolCoins, ) if err != nil { logger.Fatalw("unable to create new server asserter", "error", err) diff --git a/services/network_service.go b/services/network_service.go index f32a901..899dcd1 100644 --- a/services/network_service.go +++ b/services/network_service.go @@ -99,7 +99,7 @@ func (s *NetworkAPIService) NetworkOptions( OperationTypes: bitcoin.OperationTypes, Errors: Errors, HistoricalBalanceLookup: HistoricalBalanceLookup, - MempoolCoins: IncludeMempoolCoins, + MempoolCoins: MempoolCoins, }, }, nil } diff --git a/services/types.go b/services/types.go index e8cf044..19c472e 100644 --- a/services/types.go +++ b/services/types.go @@ -31,10 +31,10 @@ const ( // that historical balance lookup is supported. HistoricalBalanceLookup = true - // IncludeMempoolCoins indicates that + // MempoolCoins indicates that // including mempool coins in the /account/coins // response is not supported. - IncludeMempoolCoins = false + MempoolCoins = false // inlineFetchLimit is the maximum number // of transactions to fetch inline.