Skip to content

Commit

Permalink
add contracts and search endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
chris124567 committed Sep 19, 2024
1 parent 739c667 commit 9ea23db
Showing 1 changed file with 145 additions and 9 deletions.
154 changes: 145 additions & 9 deletions api/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,30 @@ func TestAPI(t *testing.T) {
}
}

checkFC := func(resolved, valid bool, expected types.FileContract, got explorer.FileContract) {
check(t, "resolved state", resolved, got.Resolved)
check(t, "valid state", valid, got.Valid)

gotFC := got.FileContract
check(t, "filesize", expected.Filesize, gotFC.Filesize)
check(t, "file merkle root", expected.FileMerkleRoot, gotFC.FileMerkleRoot)
check(t, "window start", expected.WindowStart, gotFC.WindowStart)
check(t, "window end", expected.WindowEnd, gotFC.WindowEnd)
check(t, "payout", expected.Payout, gotFC.Payout)
check(t, "unlock hash", expected.UnlockHash, gotFC.UnlockHash)
check(t, "revision number", expected.RevisionNumber, gotFC.RevisionNumber)
check(t, "valid proof outputs", len(expected.ValidProofOutputs), len(gotFC.ValidProofOutputs))
for i := range expected.ValidProofOutputs {
check(t, "valid proof output address", expected.ValidProofOutputs[i].Address, gotFC.ValidProofOutputs[i].Address)
check(t, "valid proof output value", expected.ValidProofOutputs[i].Value, gotFC.ValidProofOutputs[i].Value)
}
check(t, "missed proof outputs", len(expected.MissedProofOutputs), len(gotFC.MissedProofOutputs))
for i := range expected.MissedProofOutputs {
check(t, "missed proof output address", expected.MissedProofOutputs[i].Address, gotFC.MissedProofOutputs[i].Address)
check(t, "missed proof output value", expected.MissedProofOutputs[i].Value, gotFC.MissedProofOutputs[i].Value)
}
}

scOutputID := genesisBlock.Transactions[0].SiacoinOutputID(0)
sfOutputID := genesisBlock.Transactions[0].SiafundOutputID(0)
unlockConditions := types.StandardUnlockConditions(pk1.PublicKey())
Expand Down Expand Up @@ -358,12 +382,13 @@ func TestAPI(t *testing.T) {
},
SignaturesRequired: 2,
}
fc.RevisionNumber++
revFC := fc
revFC.RevisionNumber++
reviseTxn := types.Transaction{
FileContractRevisions: []types.FileContractRevision{{
ParentID: fcID,
UnlockConditions: uc,
FileContract: fc,
FileContract: revFC,
}},
}
signTxn(&reviseTxn)
Expand Down Expand Up @@ -509,27 +534,138 @@ func TestAPI(t *testing.T) {
}

{
transaction, err := client.Transaction(txn1.ID())
resp, err := client.Transaction(txn1.ID())
if err != nil {
t.Fatal(err)
}
checkTransaction(txn1, resp)
}

{
resp, err := client.Transactions([]types.TransactionID{txn2.ID()})
if err != nil {
t.Fatal(err)
}
checkTransaction(txn2, resp[0])
}

{
resp, err := client.TransactionChainIndices(txn2.ID(), 0, 500)
if err != nil {
t.Fatal(err)
}
check(t, "len(chainIndices)", 1, len(resp))
check(t, "chain index", cm.Tip(), resp[0])
}

{
resp, err := client.AddressSiacoinUTXOs(addr1, 0, 500)
if err != nil {
t.Fatal(err)
}
check(t, "len(scos)", 1, len(resp))
check(t, "output source", explorer.SourceTransaction, resp[0].Source)
check(t, "output spent index", nil, resp[0].SpentIndex)
check(t, "output address", txn1.SiacoinOutputs[0].Address, resp[0].SiacoinOutput.Address)
check(t, "output value", txn1.SiacoinOutputs[0].Value, resp[0].SiacoinOutput.Value)
}

{
resp, err := client.AddressSiacoinUTXOs(addr1, 1, 500)
if err != nil {
t.Fatal(err)
}
check(t, "len(scos)", 0, len(resp))
}

{
resp, err := client.AddressSiacoinUTXOs(addr1, 0, 0)
if err != nil {
t.Fatal(err)
}
check(t, "len(scos)", 0, len(resp))
}

{
resp, err := client.AddressSiafundUTXOs(addr1, 0, 500)
if err != nil {
t.Fatal(err)
}
check(t, "len(sfos)", 1, len(resp))
check(t, "output spent index", nil, resp[0].SpentIndex)
check(t, "output address", txn2.SiafundOutputs[1].Address, resp[0].SiafundOutput.Address)
check(t, "output value", txn2.SiafundOutputs[1].Value, resp[0].SiafundOutput.Value)
}

{
resp, err := client.AddressBalance(addr1)
if err != nil {
t.Fatal(err)
}
check(t, "unspent siacoins", txn1.SiacoinOutputs[0].Value, resp.UnspentSiacoins)
check(t, "immature siacoins", types.ZeroCurrency, resp.ImmatureSiacoins)
check(t, "unspent siafunds", txn2.SiafundOutputs[1].Value, resp.UnspentSiafunds)
}

// There is an issue with JSON unmarshaling of events.
// TODO: fix when explorer.Events are replaced with wallet.Events
// {
// resp, err := client.AddressEvents(addr1, 0, 500)
// if err != nil {
// t.Fatal(err)
// }
// if len(resp) == 0 {
// t.Fatal("no events for addr1")
// }
// }

{
resp, err := client.Contract(txn1.FileContractID(0))
if err != nil {
t.Fatal(err)
}
checkFC(false, false, revFC, resp)
}

{
resp, err := client.Contracts([]types.FileContractID{txn1.FileContractID(0)})
if err != nil {
t.Fatal(err)
}
check(t, "len(contracts)", 1, len(resp))
checkFC(false, false, revFC, resp[0])
}

{
resp, err := client.ContractsKey(renterPublicKey)
if err != nil {
t.Fatal(err)
}
check(t, "len(contracts)", 1, len(resp))
checkFC(false, false, revFC, resp[0])
}

{
resp, err := client.Search(types.Hash256(txn1.SiacoinOutputID(0)))
if err != nil {
t.Fatal(err)
}
checkTransaction(txn1, transaction)
check(t, "search type", explorer.SearchTypeSiacoinElement, resp)
}

{
transactions, err := client.Transactions([]types.TransactionID{txn2.ID()})
resp, err := client.Search(types.Hash256(txn2.SiafundOutputID(1)))
if err != nil {
t.Fatal(err)
}
checkTransaction(txn2, transactions[0])
check(t, "search type", explorer.SearchTypeSiafundElement, resp)
}

{
chainIndices, err := client.TransactionChainIndices(txn2.ID(), 0, 500)
resp, err := client.Search(types.Hash256(txn1.FileContractID(0)))
if err != nil {
t.Fatal(err)
}
check(t, "len(chainIndices)", 1, len(chainIndices))
check(t, "chain index", cm.Tip(), chainIndices[0])
check(t, "search type", explorer.SearchTypeContract, resp)
}
}

0 comments on commit 9ea23db

Please sign in to comment.