Skip to content

Commit

Permalink
feat(resolver): order querytxs param
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanswrt committed Jul 9, 2021
1 parent 0ba8c3e commit 4d15ef2
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 12 deletions.
6 changes: 3 additions & 3 deletions compat/v0/compat.go
Original file line number Diff line number Diff line change
Expand Up @@ -564,7 +564,7 @@ func V1TxFromV0Mint(ctx context.Context, v0tx Tx, bindings *binding.Binding) (tx
txid := txidB.CloneBytes()
txindex := pack.NewU32(uint32(vout))

client := bindings.BtcClient(selector.Asset().OriginChain())
client := bindings.UTXOClient(selector.Asset().OriginChain())
output, _, err := client.Output(ctx, multichain.UTXOutpoint{
Hash: txid,
Index: pack.NewU32(uint32(vout)),
Expand Down Expand Up @@ -685,7 +685,7 @@ func V1TxFromV0Burn(ctx context.Context, v0tx Tx, bindings *binding.Binding, net
return tx.NewTx(selector, pack.Typed(input.(pack.Struct)))
}

func V0TxHashFromTx(tx Tx) (B32, error){
func V0TxHashFromTx(tx Tx) (B32, error) {
var hash B32
if IsShiftIn(tx.To) {
payload := pack.NewBytes(tx.In.Get("p").Value.(ExtEthCompatPayload).Value[:])
Expand All @@ -706,4 +706,4 @@ func V0TxHashFromTx(tx Tx) (B32, error){
copy(hash[:], crypto.Keccak256([]byte(fmt.Sprintf("txHash_%s_%d", tx.To, ref.Int.Int64()))))
}
return hash, nil
}
}
17 changes: 13 additions & 4 deletions db/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ type DB interface {
Tx(hash id.Hash) (tx.Tx, error)

// Txs returns transactions with the given pagination options.
Txs(offset, limit int) ([]tx.Tx, error)
Txs(offset, limit int, latest bool) ([]tx.Tx, error)

// Txs returns transactions with the given pagination options.
TxsByTxid(id pack.Bytes) ([]tx.Tx, error)
Expand Down Expand Up @@ -183,7 +183,8 @@ func (db database) GatewayCount() (int, error) {
// Returns a page of stored gateway information
func (db database) Gateways(offset, limit int) ([]tx.Tx, error) {
gateways := make([]tx.Tx, 0, limit)
rows, err := db.db.Query(`SELECT selector, payload, phash, to_address, nonce, nhash, gpubkey, ghash, version FROM txs ORDER BY created_time ASC LIMIT $1 OFFSET $2;`, limit, offset)

rows, err := db.db.Query(`SELECT gateway_address, selector, payload, phash, to_address, nonce, nhash, gpubkey, ghash, version FROM gateways ORDER BY created_time DESC LIMIT $1 OFFSET $2;`, limit, offset)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -368,9 +369,17 @@ func (db database) Tx(txHash id.Hash) (tx.Tx, error) {
}

// Txs implements the DB interface.
func (db database) Txs(offset, limit int) ([]tx.Tx, error) {
func (db database) Txs(offset, limit int, latest bool) ([]tx.Tx, error) {
txs := make([]tx.Tx, 0, limit)
rows, err := db.db.Query(`SELECT hash, selector, txid, txindex, amount, payload, phash, to_address, nonce, nhash, gpubkey, ghash, version FROM txs ORDER BY created_time ASC LIMIT $1 OFFSET $2;`, limit, offset)
order := "ASC"
if latest {
order = "DESC"
}
// We cant make a prepared statement with variable order directives,
// so we need to generate the query manually
queryString := fmt.Sprintf(`SELECT hash, selector, txid, txindex, amount, payload, phash, to_address, nonce, nhash, gpubkey, ghash, version FROM txs ORDER BY created_time %s LIMIT $1 OFFSET $2;`, order)

rows, err := db.db.Query(queryString, limit, offset)
if err != nil {
return nil, err
}
Expand Down
37 changes: 35 additions & 2 deletions db/db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,14 +171,47 @@ var _ = Describe("Lightnode db", func() {
})
})

Context("when querying gateways", func() {
It("should return a page of gateways", func() {
sqlDB := init(dbname)
defer close(sqlDB)
db := New(sqlDB, 100)

r := rand.New(rand.NewSource(GinkgoRandomSeed()))
test := func() bool {
Expect(db.Init()).Should(Succeed())
defer cleanUp(sqlDB)

for i := 0; i < 50; i++ {
transaction := txutil.RandomGoodTx(r)
transaction.Output = nil
v := r.Intn(2)
if v == 0 {
transaction.Version = tx.Version0
}
gatewayAddress := transaction.Hash.String()

Expect(db.InsertGateway(gatewayAddress, transaction)).To(Succeed())
}

txsPage, err := db.Gateways(0, 10)
Expect(err).NotTo(HaveOccurred())
Expect(len(txsPage)).Should(Equal(10))
return true
}

Expect(quick.Check(test, &quick.Config{MaxCount: 10})).NotTo(HaveOccurred())
})
})

Context("when querying txs", func() {
It("should return a page of txs", func() {
sqlDB := init(dbname)
defer close(sqlDB)
db := New(sqlDB, 100)

r := rand.New(rand.NewSource(GinkgoRandomSeed()))
test := func() bool {
test := func(order bool) bool {
Expect(db.Init()).Should(Succeed())
defer cleanUp(sqlDB)

Expand All @@ -195,7 +228,7 @@ var _ = Describe("Lightnode db", func() {
Expect(db.InsertTx(transaction)).To(Succeed())
}

txsPage, err := db.Txs(0, 10)
txsPage, err := db.Txs(0, 10, order)
Expect(err).NotTo(HaveOccurred())
Expect(len(txsPage)).Should(Equal(10))
for _, tx := range txsPage {
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ require (
github.com/onsi/ginkgo v1.14.0
github.com/onsi/gomega v1.10.1
github.com/renproject/aw v0.4.1-0.20210604011747-50d6a643dc76
github.com/renproject/darknode v0.5.3-0.20210629111346-e4f394804f14
github.com/renproject/darknode v0.5.3-0.20210708063137-4d3b96c44e31
github.com/renproject/id v0.4.2
github.com/renproject/kv v1.1.2
github.com/renproject/multichain v0.3.16
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -1610,6 +1610,8 @@ github.com/renproject/aw v0.4.1-0.20210604011747-50d6a643dc76 h1:pp21sp4scg61RYR
github.com/renproject/aw v0.4.1-0.20210604011747-50d6a643dc76/go.mod h1:WzE3LgCNZSMCwg8tObbLXz+AvZQ0zRW3jNgWqg6dEyQ=
github.com/renproject/darknode v0.5.3-0.20210629111346-e4f394804f14 h1:VfqzKf/9P6pcR5d3hTC6eEGcH2XnRrpPNTZkh8FVBgU=
github.com/renproject/darknode v0.5.3-0.20210629111346-e4f394804f14/go.mod h1:9I7UvwFCtMD1yTd7LcCLExv4kEPqO40KSypIlZsHfTI=
github.com/renproject/darknode v0.5.3-0.20210708063137-4d3b96c44e31 h1:cAxTtWK7gAuAUJ5jxqzPwUNgUbLFI6eFS7E1vR9XePA=
github.com/renproject/darknode v0.5.3-0.20210708063137-4d3b96c44e31/go.mod h1:9I7UvwFCtMD1yTd7LcCLExv4kEPqO40KSypIlZsHfTI=
github.com/renproject/hyperdrive v0.4.8 h1:p66FEsHjkhn+PegvWzk21GYeHF5S4iHJiEs5DA+p7D0=
github.com/renproject/hyperdrive v0.4.8/go.mod h1:ck1cKJ0M95xwfO0vuEj7ifDjNVYFrPvat5INU70wbUc=
github.com/renproject/id v0.4.2 h1:XseNDPPCJtsZjIWR7Qgf+zxy0Gt5xsLrfwpQxJt5wFQ=
Expand Down
8 changes: 6 additions & 2 deletions resolver/resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ func (resolver *Resolver) QueryBlocks(ctx context.Context, id interface{}, param
func (resolver *Resolver) SubmitTx(ctx context.Context, id interface{}, params *jsonrpc.ParamsSubmitTx, req *http.Request) jsonrpc.Response {
// Check if the tx is a v1 tx or v0 tx.
txVersion := params.Tx.Version
if params.Tx.Version == tx.Version0{
if params.Tx.Version == tx.Version0 {
// We need to always make sure the tx to submit is a v1 tx as
// darknode won't accept v0 tx.
params.Tx.Version = tx.Version1
Expand Down Expand Up @@ -665,8 +665,12 @@ func (resolver *Resolver) QueryTxs(ctx context.Context, id interface{}, params *
limit = int(*params.Limit)
}

latest := false
if params.Latest != nil {
latest = bool(*params.Latest)
}
// Fetch the matching transactions from the database.
txs, err := resolver.db.Txs(offset, limit)
txs, err := resolver.db.Txs(offset, limit, latest)
if err != nil {
jsonErr := jsonrpc.NewError(jsonrpc.ErrorCodeInternal, fmt.Sprintf("failed to fetch txs: %v", err), nil)
return jsonrpc.NewResponse(id, nil, &jsonErr)
Expand Down

0 comments on commit 4d15ef2

Please sign in to comment.