mirror of https://github.com/poanetwork/gecko.git
Merge pull request #144 from StephenButtolph/get-tx
Added GetTx method to the AVM endpoint
This commit is contained in:
commit
ac9eeef3da
|
@ -96,6 +96,36 @@ func (service *Service) GetTxStatus(r *http.Request, args *GetTxStatusArgs, repl
|
|||
return nil
|
||||
}
|
||||
|
||||
// GetTxArgs are arguments for passing into GetTx requests
|
||||
type GetTxArgs struct {
|
||||
TxID ids.ID `json:"txID"`
|
||||
}
|
||||
|
||||
// GetTxReply defines the GetTxStatus replies returned from the API
|
||||
type GetTxReply struct {
|
||||
Tx formatting.CB58 `json:"tx"`
|
||||
}
|
||||
|
||||
// GetTx returns the specified transaction
|
||||
func (service *Service) GetTx(r *http.Request, args *GetTxArgs, reply *GetTxReply) error {
|
||||
service.vm.ctx.Log.Verbo("GetTx called with %s", args.TxID)
|
||||
|
||||
if args.TxID.IsZero() {
|
||||
return errNilTxID
|
||||
}
|
||||
|
||||
tx := UniqueTx{
|
||||
vm: service.vm,
|
||||
txID: args.TxID,
|
||||
}
|
||||
if status := tx.Status(); !status.Fetched() {
|
||||
return errUnknownTx
|
||||
}
|
||||
|
||||
reply.Tx.Bytes = tx.Bytes()
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetUTXOsArgs are arguments for passing into GetUTXOs requests
|
||||
type GetUTXOsArgs struct {
|
||||
Addresses []string `json:"addresses"`
|
||||
|
|
|
@ -7,9 +7,10 @@ import (
|
|||
"fmt"
|
||||
"testing"
|
||||
|
||||
"github.com/ava-labs/gecko/snow/choices"
|
||||
"github.com/stretchr/testify/assert"
|
||||
|
||||
"github.com/ava-labs/gecko/ids"
|
||||
"github.com/ava-labs/gecko/snow/choices"
|
||||
"github.com/ava-labs/gecko/utils/formatting"
|
||||
)
|
||||
|
||||
|
@ -87,6 +88,49 @@ func TestServiceGetTxStatus(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
func TestServiceGetTx(t *testing.T) {
|
||||
genesisBytes, vm, s := setup(t)
|
||||
defer func() {
|
||||
vm.Shutdown()
|
||||
ctx.Lock.Unlock()
|
||||
}()
|
||||
|
||||
genesisTx := GetFirstTxFromGenesisTest(genesisBytes, t)
|
||||
genesisTxBytes := genesisTx.Bytes()
|
||||
txID := genesisTx.ID()
|
||||
|
||||
reply := GetTxReply{}
|
||||
err := s.GetTx(nil, &GetTxArgs{
|
||||
TxID: txID,
|
||||
}, &reply)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, genesisTxBytes, reply.Tx.Bytes, "Wrong tx returned from service.GetTx")
|
||||
}
|
||||
|
||||
func TestServiceGetNilTx(t *testing.T) {
|
||||
_, vm, s := setup(t)
|
||||
defer func() {
|
||||
vm.Shutdown()
|
||||
ctx.Lock.Unlock()
|
||||
}()
|
||||
|
||||
reply := GetTxReply{}
|
||||
err := s.GetTx(nil, &GetTxArgs{}, &reply)
|
||||
assert.Error(t, err, "Nil TxID should have returned an error")
|
||||
}
|
||||
|
||||
func TestServiceGetUnknownTx(t *testing.T) {
|
||||
_, vm, s := setup(t)
|
||||
defer func() {
|
||||
vm.Shutdown()
|
||||
ctx.Lock.Unlock()
|
||||
}()
|
||||
|
||||
reply := GetTxReply{}
|
||||
err := s.GetTx(nil, &GetTxArgs{TxID: ids.Empty}, &reply)
|
||||
assert.Error(t, err, "Unknown TxID should have returned an error")
|
||||
}
|
||||
|
||||
func TestServiceGetUTXOsInvalidAddress(t *testing.T) {
|
||||
_, vm, s := setup(t)
|
||||
defer func() {
|
||||
|
|
Loading…
Reference in New Issue