From 1542700bb87d29932aba243cdc97d7568c1c0628 Mon Sep 17 00:00:00 2001 From: gokch Date: Tue, 6 Feb 2024 04:33:03 +0000 Subject: [PATCH 1/3] rollback receipt and event format --- cmd/aergocli/cmd/event.go | 6 +-- cmd/aergocli/cmd/receipt.go | 3 +- rpc/web3/v1.go | 3 +- types/jsonrpc/receipt.go | 104 +++++------------------------------- 4 files changed, 18 insertions(+), 98 deletions(-) diff --git a/cmd/aergocli/cmd/event.go b/cmd/aergocli/cmd/event.go index 4c50acaac..6b01bc17a 100644 --- a/cmd/aergocli/cmd/event.go +++ b/cmd/aergocli/cmd/event.go @@ -82,8 +82,7 @@ func execListEvent(cmd *cobra.Command, args []string) { return } for _, event := range events.GetEvents() { - res := jsonrpc.ConvEvent(event) - cmd.Println(jsonrpc.MarshalJSON(res)) + cmd.Println(jsonrpc.MarshalJSON(event)) } } @@ -109,7 +108,6 @@ func execStreamEvent(cmd *cobra.Command, args []string) { cmd.Printf("Failed: %s\n", err.Error()) return } - res := jsonrpc.ConvEvent(event) - cmd.Println(jsonrpc.MarshalJSON(res)) + cmd.Println(jsonrpc.MarshalJSON(event)) } } diff --git a/cmd/aergocli/cmd/receipt.go b/cmd/aergocli/cmd/receipt.go index 3a033707c..63df8b5fb 100644 --- a/cmd/aergocli/cmd/receipt.go +++ b/cmd/aergocli/cmd/receipt.go @@ -36,8 +36,7 @@ func init() { if err != nil { log.Fatal(err) } - res := jsonrpc.ConvReceipt(msg) - cmd.Println(jsonrpc.MarshalJSON(res)) + cmd.Println(jsonrpc.MarshalJSON(msg)) }, }, ) diff --git a/rpc/web3/v1.go b/rpc/web3/v1.go index 46db2c9ec..92272aa1d 100644 --- a/rpc/web3/v1.go +++ b/rpc/web3/v1.go @@ -664,8 +664,7 @@ func (api *Web3APIv1) GetReceipt() (handler http.Handler, ok bool) { return commonResponseHandler(&types.Empty{}, err), true } - output := jsonrpc.ConvReceipt(result) - return stringResponseHandler(jsonrpc.MarshalJSON(output), nil), true + return stringResponseHandler(jsonrpc.MarshalJSON(result), nil), true } func (api *Web3APIv1) GetReceipts() (handler http.Handler, ok bool) { diff --git a/types/jsonrpc/receipt.go b/types/jsonrpc/receipt.go index c937c3b52..ed89262c6 100644 --- a/types/jsonrpc/receipt.go +++ b/types/jsonrpc/receipt.go @@ -1,85 +1,9 @@ package jsonrpc import ( - "math/big" - - "github.com/aergoio/aergo/v2/internal/enc/base58" "github.com/aergoio/aergo/v2/types" ) -func ConvReceipt(msg *types.Receipt) *InOutReceipt { - if msg == nil { - return nil - } - r := &InOutReceipt{} - r.ContractAddress = types.EncodeAddress(msg.ContractAddress) - r.Status = msg.Status - r.Ret = msg.Ret - r.TxHash = base58.Encode(msg.TxHash) - r.FeeUsed = new(big.Int).SetBytes(msg.FeeUsed).String() - r.CumulativeFeeUsed = new(big.Int).SetBytes(msg.CumulativeFeeUsed).String() - r.Bloom = msg.Bloom - if msg.Events != nil { - r.Events = make([]*InOutEvent, len(msg.Events)) - for i, e := range msg.Events { - r.Events[i] = ConvEvent(e) - } - } - r.BlockHash = base58.Encode(msg.BlockHash) - r.BlockNo = msg.BlockNo - r.TxIndex = msg.TxIndex - r.From = types.EncodeAddress(msg.From) - r.To = types.EncodeAddress(msg.To) - r.FeeDelegation = msg.FeeDelegation - r.GasUsed = msg.GasUsed - return r -} - -type InOutReceipt struct { - ContractAddress string `json:"contractAddress"` - Status string `json:"status"` - Ret string `json:"ret"` - TxHash string `json:"txHash"` - FeeUsed string `json:"feeUsed"` - CumulativeFeeUsed string `json:"cumulativeFeeUsed,omitempty"` - Bloom []byte `json:"bloom,omitempty"` - Events []*InOutEvent `json:"events,omitempty"` - BlockHash string `json:"blockHash,omitempty"` - BlockNo uint64 `json:"blockNo,omitempty"` - TxIndex int32 `json:"txIndex,omitempty"` - From string `json:"from,omitempty"` - To string `json:"to,omitempty"` - FeeDelegation bool `json:"feeDelegation,omitempty"` - GasUsed uint64 `json:"gasUsed,omitempty"` -} - -func ConvEvent(msg *types.Event) *InOutEvent { - if msg == nil { - return nil - } - return &InOutEvent{ - ContractAddress: types.EncodeAddress(msg.ContractAddress), - EventName: msg.EventName, - JsonArgs: msg.JsonArgs, - EventIdx: msg.EventIdx, - TxHash: base58.Encode(msg.TxHash), - BlockHash: base58.Encode(msg.BlockHash), - BlockNo: msg.BlockNo, - TxIndex: msg.TxIndex, - } -} - -type InOutEvent struct { - ContractAddress string `json:"contractAddress"` - EventName string `json:"eventName"` - JsonArgs string `json:"jsonArgs"` - EventIdx int32 `json:"eventIdx"` - TxHash string `json:"txHash"` - BlockHash string `json:"blockHash"` - BlockNo uint64 `json:"blockNo"` - TxIndex int32 `json:"txIndex"` -} - func ConvAbi(msg *types.ABI) *InOutAbi { if msg == nil { return nil @@ -166,16 +90,16 @@ func ConvReceipts(msg *types.Receipts) *InOutReceipts { rs := &InOutReceipts{} rs.BlockNo = msg.GetBlockNo() - rs.Receipts = make([]*InOutReceipt, len(msg.Get())) + rs.Receipts = make([]*types.Receipt, len(msg.Get())) for i, receipt := range msg.Get() { - rs.Receipts[i] = ConvReceipt(receipt) + rs.Receipts[i] = receipt } return rs } type InOutReceipts struct { - Receipts []*InOutReceipt `json:"receipts"` - BlockNo uint64 `json:"blockNo,omitempty"` + Receipts []*types.Receipt `json:"receipts"` + BlockNo uint64 `json:"blockNo,omitempty"` } func ConvReceiptsPaged(msg *types.ReceiptsPaged) *InOutReceiptsPaged { @@ -188,20 +112,20 @@ func ConvReceiptsPaged(msg *types.ReceiptsPaged) *InOutReceiptsPaged { rp.Offset = msg.GetOffset() rp.Size = msg.GetSize() rp.BlockNo = msg.GetBlockNo() - rp.Receipts = make([]*InOutReceipt, len(msg.Get())) + rp.Receipts = make([]*types.Receipt, len(msg.Get())) for i, receipt := range msg.Get() { - rp.Receipts[i] = ConvReceipt(receipt) + rp.Receipts[i] = receipt } return rp } type InOutReceiptsPaged struct { - Total uint32 `json:"total,omitempty"` - Offset uint32 `json:"offset,omitempty"` - Size uint32 `json:"size,omitempty"` - Receipts []*InOutReceipt `json:"receipts"` - BlockNo uint64 `json:"blockNo,omitempty"` + Total uint32 `json:"total,omitempty"` + Offset uint32 `json:"offset,omitempty"` + Size uint32 `json:"size,omitempty"` + Receipts []*types.Receipt `json:"receipts"` + BlockNo uint64 `json:"blockNo,omitempty"` } func ConvEvents(msg *types.EventList) *InOutEventList { @@ -210,13 +134,13 @@ func ConvEvents(msg *types.EventList) *InOutEventList { } rs := &InOutEventList{} - rs.Events = make([]*InOutEvent, len(msg.Events)) + rs.Events = make([]*types.Event, len(msg.Events)) for i, event := range msg.Events { - rs.Events[i] = ConvEvent(event) + rs.Events[i] = event } return rs } type InOutEventList struct { - Events []*InOutEvent `json:"events,omitempty"` + Events []*types.Event `json:"events,omitempty"` } From 47f82642742bd216b075cd7c09aaf3a649c80fac Mon Sep 17 00:00:00 2001 From: gokch Date: Wed, 7 Feb 2024 02:42:36 +0000 Subject: [PATCH 2/3] change receipt json format --- types/receipt.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/types/receipt.go b/types/receipt.go index a351df0fa..552aa40b3 100644 --- a/types/receipt.go +++ b/types/receipt.go @@ -358,7 +358,7 @@ func (r *Receipt) MarshalJSON() ([]byte, error) { b.WriteString(`","status":"`) b.WriteString(r.Status) if len(r.Ret) == 0 { - b.WriteString(`","ret": {}`) + b.WriteString(`","ret": ""`) } else if r.Status == "ERROR" { js, _ := json.Marshal(r.Ret) b.WriteString(`","ret": `) From cc53115edc39f42dd4719a2457d3cd666dd303c9 Mon Sep 17 00:00:00 2001 From: Hayarobi Park Date: Thu, 1 Feb 2024 11:46:57 +0900 Subject: [PATCH 3/3] Improved usability - Added the features for signtx and verifytx to read transactions from a file. --- cmd/aergocli/cmd/signtx.go | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/cmd/aergocli/cmd/signtx.go b/cmd/aergocli/cmd/signtx.go index 86b90bfc8..39f515177 100644 --- a/cmd/aergocli/cmd/signtx.go +++ b/cmd/aergocli/cmd/signtx.go @@ -17,11 +17,13 @@ import ( func init() { rootCmd.AddCommand(signCmd) signCmd.Flags().StringVar(&jsonTx, "jsontx", "", "transaction json to sign") + signCmd.Flags().StringVar(&jsonPath, "jsontxpath", "", "transaction json file path to sign") signCmd.Flags().StringVar(&address, "address", "1", "address of account to use for signing") signCmd.Flags().StringVar(&pw, "password", "", "local account password") signCmd.Flags().StringVar(&privKey, "key", "", "base58 encoded key for sign") rootCmd.AddCommand(verifyCmd) verifyCmd.Flags().StringVar(&jsonTx, "jsontx", "", "transaction list json to verify") + verifyCmd.Flags().StringVar(&jsonPath, "jsontxpath", "", "transaction json file path to verify") verifyCmd.Flags().BoolVar(&remote, "remote", false, "verify in the node") } @@ -32,10 +34,18 @@ var signCmd = &cobra.Command{ PreRun: preConnectAergo, Run: func(cmd *cobra.Command, args []string) { var err error - if jsonTx == "" { + if jsonTx == "" && jsonPath == "" { cmd.Printf("need to transaction json input") return } + if jsonTx == "" { + b, readerr := os.ReadFile(jsonPath) + if readerr != nil { + cmd.Printf("Failed to read --jsontxpath\n" + readerr.Error()) + return + } + jsonTx = string(b) + } param, err := jsonrpc.ParseBase58TxBody([]byte(jsonTx)) if err != nil { cmd.Printf("Failed: %s\n", err.Error()) @@ -102,10 +112,19 @@ var verifyCmd = &cobra.Command{ Short: "Verify transaction", PreRun: preConnectAergo, Run: func(cmd *cobra.Command, args []string) { - if jsonTx == "" { + if jsonTx == "" && jsonPath == "" { cmd.Printf("need to transaction json input") return } + if jsonTx == "" { + b, readerr := os.ReadFile(jsonPath) + if readerr != nil { + cmd.Printf("Failed to read --jsontxpath\n" + readerr.Error()) + return + } + jsonTx = string(b) + } + param, err := jsonrpc.ParseBase58Tx([]byte(jsonTx)) if err != nil { cmd.Printf("Failed: %s\n", err.Error())