rpc: implement getrawmempool RPC

closes #175
This commit is contained in:
Anna Shaleva 2020-03-02 19:13:44 +03:00
parent 252a9f2f31
commit e1fe12a07f
5 changed files with 69 additions and 1 deletions

View file

@ -48,7 +48,7 @@ which would yield the response:
| `getnep5balances` | No (#498) |
| `getnep5transfers` | No (#498) |
| `getpeers` | Yes |
| `getrawmempool` | No (#175) |
| `getrawmempool` | Yes |
| `getrawtransaction` | Yes |
| `getstorage` | Yes |
| `gettxout` | Yes |

View file

@ -83,6 +83,14 @@ var (
},
)
getrawmempoolCalled = prometheus.NewCounter(
prometheus.CounterOpts{
Help: "Number of calls to getrawmempool rpc endpoint",
Name: "getrawmempool_called",
Namespace: "neogo",
},
)
validateaddressCalled = prometheus.NewCounter(
prometheus.CounterOpts{
Help: "Number of calls to validateaddress rpc endpoint",
@ -160,6 +168,7 @@ func init() {
getcontractstateCalled,
getversionCalled,
getpeersCalled,
getrawmempoolCalled,
validateaddressCalled,
getassetstateCalled,
getaccountstateCalled,

View file

@ -210,6 +210,15 @@ Methods:
peers.AddBad(s.coreServer.BadPeers())
results = peers
case "getrawmempool":
getrawmempoolCalled.Inc()
mp := s.chain.GetMemPool()
hashList := make([]util.Uint256, 0)
for _, item := range mp.GetVerifiedTransactions() {
hashList = append(hashList, item.Tx.Hash())
}
results = hashList
case "getstorage":
getstorageCalled.Inc()
results, resultsErr = s.getStorage(reqParams)

View file

@ -9,9 +9,11 @@ import (
"github.com/CityOfZion/neo-go/pkg/core"
"github.com/CityOfZion/neo-go/pkg/core/block"
"github.com/CityOfZion/neo-go/pkg/core/storage"
"github.com/CityOfZion/neo-go/pkg/core/transaction"
"github.com/CityOfZion/neo-go/pkg/io"
"github.com/CityOfZion/neo-go/pkg/network"
"github.com/CityOfZion/neo-go/pkg/rpc/request"
"github.com/CityOfZion/neo-go/pkg/util"
"github.com/stretchr/testify/require"
"go.uber.org/zap/zaptest"
)
@ -66,3 +68,21 @@ func initServerWithInMemoryChain(t *testing.T) (*core.Blockchain, http.HandlerFu
return chain, handler
}
type FeerStub struct{}
func (fs *FeerStub) NetworkFee(*transaction.Transaction) util.Fixed8 {
return 0
}
func (fs *FeerStub) IsLowPriority(util.Fixed8) bool {
return false
}
func (fs *FeerStub) FeePerByte(*transaction.Transaction) util.Fixed8 {
return 0
}
func (fs *FeerStub) SystemFee(*transaction.Transaction) util.Fixed8 {
return 0
}

View file

@ -14,6 +14,7 @@ import (
"github.com/CityOfZion/neo-go/pkg/core"
"github.com/CityOfZion/neo-go/pkg/core/transaction"
"github.com/CityOfZion/neo-go/pkg/internal/random"
"github.com/CityOfZion/neo-go/pkg/rpc/response"
"github.com/CityOfZion/neo-go/pkg/rpc/response/result"
"github.com/CityOfZion/neo-go/pkg/util"
@ -664,6 +665,35 @@ func TestRPC(t *testing.T) {
assert.Equal(t, util.Fixed8FromInt64(100000000), txOut.Value)
assert.Equal(t, "AZ81H31DMWzbSnFDLFkzh9vHwaDLayV7fU", txOut.Address)
})
t.Run("getrawmempool", func(t *testing.T) {
mp := chain.GetMemPool()
// `expected` stores hashes of previously added txs
expected := make([]util.Uint256, 0)
for _, tx := range mp.GetVerifiedTransactions() {
expected = append(expected, tx.Tx.Hash())
}
for i := 0; i < 5; i++ {
tx := &transaction.Transaction{
Type: transaction.MinerType,
Data: &transaction.MinerTX{
Nonce: uint32(random.Int(0, 1000000000)),
},
}
assert.NoError(t, mp.Add(tx, &FeerStub{}))
expected = append(expected, tx.Hash())
}
rpc := `{"jsonrpc": "2.0", "id": 1, "method": "getrawmempool", "params": []}`
body := doRPCCall(rpc, handler, t)
res := checkErrGetResult(t, body, false)
var actual []util.Uint256
err := json.Unmarshal(res, &actual)
require.NoErrorf(t, err, "could not parse response: %s", res)
assert.ElementsMatch(t, expected, actual)
})
}
func (tc rpcTestCase) getResultPair(e *executor) (expected interface{}, res interface{}) {