From 1d44bacdbfdbb3d001a91a98f26a0c75cbe0be4f Mon Sep 17 00:00:00 2001 From: David Holtz Date: Sat, 13 Jul 2019 16:49:13 -0400 Subject: [PATCH 1/4] wip - add tx reqs --- examples/main.go | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/examples/main.go b/examples/main.go index 03c2e2c..0938fcb 100644 --- a/examples/main.go +++ b/examples/main.go @@ -14,7 +14,7 @@ func main() { } defer c.Close() - acc := "8cd377191fe0ef113455c8e8d769f0c0147d5bb618bf195c0af31a05fbfd0969" + acc := "82258967750773ea090d125f9aeb9b9e42f6c7ff6df0742336992bfc52b4096a" accState, err := c.GetAccountState(acc) if err != nil { panic(err) @@ -23,4 +23,13 @@ func main() { fmt.Printf("Raw account state: 0x%x\n", accState.Blob) fmt.Println() fmt.Printf("Account resource: %v\n", accState.AccountResource) + + txlist, err := c.GetTransactionList() + if err != nil { + panic(err) + } + + fmt.Printf("Raw account state: 0x%x\n", txlist.Blob) + fmt.Println() + fmt.Printf("Transaction list resource: %v\n", txlist.TransactionListResource) } From 2f7c8d915e4091cce8178b7d01fcce82d254b8f0 Mon Sep 17 00:00:00 2001 From: David Holtz Date: Sat, 13 Jul 2019 16:51:45 -0400 Subject: [PATCH 2/4] wip - add tx reqs II --- client.go | 36 ++++++++++++++++++++++++++++++++++++ tx.go | 55 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 91 insertions(+) diff --git a/client.go b/client.go index 81ed25d..467de36 100644 --- a/client.go +++ b/client.go @@ -60,6 +60,42 @@ func (c Client) GetAccountState(accountAddr string) (AccountState, error) { return FromAccountStateBlob(accStateBlob) } +// GetTransactionList +func (c Client) GetTransactionList() (TransactionList, error) { + // Types that are valid to be assigned to RequestedItems: + // *RequestItem_GetAccountStateRequest + // *RequestItem_GetAccountTransactionBySequenceNumberRequest + // *RequestItem_GetEventsByEventAccessPathRequest + // *RequestItem_GetTransactionsRequest + requestedItems := []*types.RequestItem{ + &types.RequestItem{ + RequestedItems: &types.RequestItem_GetTransactionsRequest{ + GetTransactionsRequest: &types.GetTransactionsRequest{ + StartVersion: 1, + Limit: 3, + FetchEvents: true, + }, + }, + }, + } + knownVersion := uint64(0) // TODO: Does this make a difference for accounts? Or only for events? Might need to be a method parameter. + updateLedgerRequest := types.UpdateToLatestLedgerRequest{ + ClientKnownVersion: knownVersion, + RequestedItems: requestedItems, + } + updateLedgerResponse, err := c.acc.UpdateToLatestLedger(context.Background(), &updateLedgerRequest) + if err != nil { + return TransactionList{}, err + } + + txListBlob := updateLedgerResponse.GetResponseItems()[0].GetGetTransactionsResponse().GetTxnListWithProof().GetInfos() + + // just used as a placeholder + if txListBlob != nil { + } + return TransactionList{}, err +} + // SendTx sends a transaction to the connected validator node. func (c Client) SendTx(tx Transaction) error { txRequest := admission_control.SubmitTransactionRequest{ diff --git a/tx.go b/tx.go index f903502..1a16440 100644 --- a/tx.go +++ b/tx.go @@ -1,8 +1,63 @@ package libra +import ( + "bytes" + "encoding/binary" + "fmt" +) + // Transaction is a transaction of Libra Coins. type Transaction struct { RawBytes []byte SenderPubKey []byte SenderSig []byte } + +// TransactionList +type TransactionList struct { + // The whole account state as raw bytes + Blob []byte + TransactionListResource TransactionListResource +} + +// FromTransactionListResourceBlob +func FromTransactionListResourceBlob(transactionListResourceBlob []byte) (TransactionListResource, error) { + result := TransactionListResource{} + + r := bytes.NewReader(transactionListResourceBlob) + + var balance uint64 + err := binary.Read(r, binary.LittleEndian, &balance) + if err != nil { + return result, err + } + result.Balance = balance + + return result, nil +} + +// TransactionListResource +type TransactionListResource struct { + AuthKey []byte + Balance uint64 + ReceivedEvents uint64 + SentEvents uint64 + SequenceNo uint64 +} + +// String formats the account state similarly to the Libra CLI. +// Numbers are formatted as string because the numbers are uint64, +// whose max value exceeds JSON's "save integer", +// which can lead to parsing errors. +func (ar TransactionListResource) String() string { + return fmt.Sprintf("{\"var_1\": \"0x%x\" }", + ar.AuthKey) +} + +// FromAccountStateBlob converts an account state blob into an object of the AccountState struct. +func FromTransactionListBlob(transactionListBlob []byte) (TransactionList, error) { + result := TransactionList{ + Blob: transactionListBlob, + } + return result, nil +} From c8f5b04887689338665b6a44fc5daccc676a8566 Mon Sep 17 00:00:00 2001 From: David Holtz Date: Mon, 22 Jul 2019 19:51:43 -0400 Subject: [PATCH 3/4] pull out TX list --- examples/main.go | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/examples/main.go b/examples/main.go index 0938fcb..b7ed248 100644 --- a/examples/main.go +++ b/examples/main.go @@ -24,12 +24,10 @@ func main() { fmt.Println() fmt.Printf("Account resource: %v\n", accState.AccountResource) - txlist, err := c.GetTransactionList() + txlist, err := c.GetTransactionList(1) if err != nil { panic(err) } - - fmt.Printf("Raw account state: 0x%x\n", txlist.Blob) fmt.Println() - fmt.Printf("Transaction list resource: %v\n", txlist.TransactionListResource) + fmt.Printf("Transaction list resource: %v\n", txlist.Transactions) } From d5c21ba8d67bdef83109342754fbb4b164817d7a Mon Sep 17 00:00:00 2001 From: David Holtz Date: Mon, 22 Jul 2019 20:32:44 -0400 Subject: [PATCH 4/4] added missing files --- client.go | 11 ++++------- tx.go | 25 +++++++++++++++---------- 2 files changed, 19 insertions(+), 17 deletions(-) diff --git a/client.go b/client.go index 467de36..7359825 100644 --- a/client.go +++ b/client.go @@ -61,7 +61,7 @@ func (c Client) GetAccountState(accountAddr string) (AccountState, error) { } // GetTransactionList -func (c Client) GetTransactionList() (TransactionList, error) { +func (c Client) GetTransactionList(limit int) (TransactionList, error) { // Types that are valid to be assigned to RequestedItems: // *RequestItem_GetAccountStateRequest // *RequestItem_GetAccountTransactionBySequenceNumberRequest @@ -72,7 +72,7 @@ func (c Client) GetTransactionList() (TransactionList, error) { RequestedItems: &types.RequestItem_GetTransactionsRequest{ GetTransactionsRequest: &types.GetTransactionsRequest{ StartVersion: 1, - Limit: 3, + Limit: uint64(limit), FetchEvents: true, }, }, @@ -88,12 +88,9 @@ func (c Client) GetTransactionList() (TransactionList, error) { return TransactionList{}, err } - txListBlob := updateLedgerResponse.GetResponseItems()[0].GetGetTransactionsResponse().GetTxnListWithProof().GetInfos() + txList := updateLedgerResponse.GetResponseItems()[0].GetGetTransactionsResponse().GetTxnListWithProof().GetTransactions() - // just used as a placeholder - if txListBlob != nil { - } - return TransactionList{}, err + return FromTransactionList(txList) } // SendTx sends a transaction to the connected validator node. diff --git a/tx.go b/tx.go index 1a16440..7563b43 100644 --- a/tx.go +++ b/tx.go @@ -4,6 +4,8 @@ import ( "bytes" "encoding/binary" "fmt" + + "github.com/philippgille/libra-sdk-go/rpc/types" ) // Transaction is a transaction of Libra Coins. @@ -15,9 +17,7 @@ type Transaction struct { // TransactionList type TransactionList struct { - // The whole account state as raw bytes - Blob []byte - TransactionListResource TransactionListResource + Transactions []Transaction } // FromTransactionListResourceBlob @@ -49,15 +49,20 @@ type TransactionListResource struct { // Numbers are formatted as string because the numbers are uint64, // whose max value exceeds JSON's "save integer", // which can lead to parsing errors. -func (ar TransactionListResource) String() string { - return fmt.Sprintf("{\"var_1\": \"0x%x\" }", - ar.AuthKey) +func (tx Transaction) String() string { + return fmt.Sprintf("{\"raw_bytes\": \"0x%x\", \"sender_pub_key\": \"0x%x\", \"sender_sig\": \"0x%x\"}", + tx.RawBytes, tx.SenderPubKey, tx.SenderSig) } // FromAccountStateBlob converts an account state blob into an object of the AccountState struct. -func FromTransactionListBlob(transactionListBlob []byte) (TransactionList, error) { - result := TransactionList{ - Blob: transactionListBlob, +func FromTransactionList(transactionList []*types.SignedTransaction) (TransactionList, error) { + results := TransactionList{} + for _, x := range transactionList { + result := Transaction{} + result.RawBytes = x.GetRawTxnBytes() + result.SenderPubKey = x.GetSenderPublicKey() + result.SenderSig = x.GetSenderSignature() + results.Transactions = append(results.Transactions, result) } - return result, nil + return results, nil }