2019-06-06 17:55:59 +00:00
|
|
|
// Copyright © 2019 Vulcanize, Inc
|
|
|
|
//
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Affero General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Affero General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Affero General Public License
|
|
|
|
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2019-06-07 13:42:10 +00:00
|
|
|
"fmt"
|
2019-06-06 17:55:59 +00:00
|
|
|
|
2023-01-21 01:39:26 +00:00
|
|
|
"github.com/cerc-io/ipld-eth-server/v4/pkg/log"
|
2019-06-07 13:42:10 +00:00
|
|
|
"github.com/ethereum/go-ethereum/common"
|
2019-06-06 17:55:59 +00:00
|
|
|
"github.com/ethereum/go-ethereum/core/types"
|
|
|
|
"github.com/ethereum/go-ethereum/rlp"
|
|
|
|
"github.com/ethereum/go-ethereum/rpc"
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
"github.com/spf13/viper"
|
|
|
|
|
2022-09-20 15:52:06 +00:00
|
|
|
"github.com/cerc-io/ipld-eth-server/v4/pkg/client"
|
|
|
|
"github.com/cerc-io/ipld-eth-server/v4/pkg/eth"
|
|
|
|
w "github.com/cerc-io/ipld-eth-server/v4/pkg/serve"
|
2019-06-06 17:55:59 +00:00
|
|
|
)
|
|
|
|
|
2020-08-31 15:59:15 +00:00
|
|
|
// subscribeCmd represents the subscribe command
|
|
|
|
var subscribeCmd = &cobra.Command{
|
|
|
|
Use: "subscribe",
|
2020-06-30 00:16:52 +00:00
|
|
|
Short: "This command is used to subscribe to the eth ipfs watcher data stream with the provided filters",
|
|
|
|
Long: `This command is for demo and testing purposes and is used to subscribe to the watcher with the provided subscription configuration parameters.
|
|
|
|
It does not do anything with the data streamed from the watcher other than unpack it and print it out for demonstration purposes.`,
|
2019-06-06 17:55:59 +00:00
|
|
|
Run: func(cmd *cobra.Command, args []string) {
|
2019-10-14 14:41:29 +00:00
|
|
|
subCommand = cmd.CalledAs()
|
|
|
|
logWithCommand = *log.WithField("SubCommand", subCommand)
|
2020-08-31 15:59:15 +00:00
|
|
|
subscribe()
|
2019-06-06 17:55:59 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
2020-08-31 15:59:15 +00:00
|
|
|
rootCmd.AddCommand(subscribeCmd)
|
2019-06-06 17:55:59 +00:00
|
|
|
}
|
|
|
|
|
2020-08-31 15:59:15 +00:00
|
|
|
func subscribe() {
|
2019-06-06 17:55:59 +00:00
|
|
|
// Prep the subscription config/filters to be sent to the server
|
2020-02-03 18:22:29 +00:00
|
|
|
ethSubConfig, err := eth.NewEthSubscriptionConfig()
|
2020-01-21 19:12:35 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2019-06-06 17:55:59 +00:00
|
|
|
|
|
|
|
// Create a new rpc client and a subscription streamer with that client
|
2020-06-30 00:16:52 +00:00
|
|
|
rpcClient, err := getRPCClient()
|
|
|
|
if err != nil {
|
|
|
|
logWithCommand.Fatal(err)
|
|
|
|
}
|
|
|
|
subClient := client.NewClient(rpcClient)
|
2019-06-06 17:55:59 +00:00
|
|
|
|
|
|
|
// Buffered channel for reading subscription payloads
|
2020-06-30 00:16:52 +00:00
|
|
|
payloadChan := make(chan w.SubscriptionPayload, 20000)
|
2019-06-06 17:55:59 +00:00
|
|
|
|
2020-06-30 00:16:52 +00:00
|
|
|
// Subscribe to the watcher service with the given config/filter parameters
|
2020-09-02 19:13:51 +00:00
|
|
|
sub, err := subClient.Stream(payloadChan, *ethSubConfig)
|
2019-06-06 17:55:59 +00:00
|
|
|
if err != nil {
|
2019-10-14 14:41:29 +00:00
|
|
|
logWithCommand.Fatal(err)
|
2019-06-06 17:55:59 +00:00
|
|
|
}
|
2019-10-14 14:41:29 +00:00
|
|
|
logWithCommand.Info("awaiting payloads")
|
2019-06-06 17:55:59 +00:00
|
|
|
// Receive response payloads and print out the results
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case payload := <-payloadChan:
|
2020-01-17 23:16:01 +00:00
|
|
|
if payload.Err != "" {
|
|
|
|
logWithCommand.Error(payload.Err)
|
2019-06-07 13:42:10 +00:00
|
|
|
continue
|
2019-06-06 17:55:59 +00:00
|
|
|
}
|
2020-10-20 14:42:09 +00:00
|
|
|
var ethData eth.IPLDs
|
2020-02-20 22:13:19 +00:00
|
|
|
if err := rlp.DecodeBytes(payload.Data, ðData); err != nil {
|
|
|
|
logWithCommand.Error(err)
|
2020-02-23 23:14:29 +00:00
|
|
|
continue
|
2020-01-17 23:16:01 +00:00
|
|
|
}
|
2020-02-23 23:14:29 +00:00
|
|
|
var header types.Header
|
|
|
|
err = rlp.Decode(bytes.NewBuffer(ethData.Header.Data), &header)
|
|
|
|
if err != nil {
|
|
|
|
logWithCommand.Error(err)
|
|
|
|
continue
|
2019-06-06 17:55:59 +00:00
|
|
|
}
|
2020-02-23 23:14:29 +00:00
|
|
|
fmt.Printf("Header number %d, hash %s\n", header.Number.Int64(), header.Hash().Hex())
|
|
|
|
fmt.Printf("header: %v\n", header)
|
2020-02-20 22:13:19 +00:00
|
|
|
for _, trxRlp := range ethData.Transactions {
|
2019-06-06 17:55:59 +00:00
|
|
|
var trx types.Transaction
|
2020-02-20 22:13:19 +00:00
|
|
|
buff := bytes.NewBuffer(trxRlp.Data)
|
2019-06-06 17:55:59 +00:00
|
|
|
stream := rlp.NewStream(buff, 0)
|
|
|
|
err := trx.DecodeRLP(stream)
|
|
|
|
if err != nil {
|
2019-10-14 14:41:29 +00:00
|
|
|
logWithCommand.Error(err)
|
2019-06-07 13:42:10 +00:00
|
|
|
continue
|
2019-06-06 17:55:59 +00:00
|
|
|
}
|
2019-06-07 13:42:10 +00:00
|
|
|
fmt.Printf("Transaction with hash %s\n", trx.Hash().Hex())
|
2019-06-07 16:01:29 +00:00
|
|
|
fmt.Printf("trx: %v\n", trx)
|
2019-06-06 17:55:59 +00:00
|
|
|
}
|
2020-02-20 22:13:19 +00:00
|
|
|
for _, rctRlp := range ethData.Receipts {
|
2020-02-23 23:14:29 +00:00
|
|
|
var rct types.Receipt
|
2020-02-20 22:13:19 +00:00
|
|
|
buff := bytes.NewBuffer(rctRlp.Data)
|
2019-06-06 17:55:59 +00:00
|
|
|
stream := rlp.NewStream(buff, 0)
|
|
|
|
err = rct.DecodeRLP(stream)
|
|
|
|
if err != nil {
|
2019-10-14 14:41:29 +00:00
|
|
|
logWithCommand.Error(err)
|
2019-06-07 13:42:10 +00:00
|
|
|
continue
|
2019-06-06 17:55:59 +00:00
|
|
|
}
|
2019-06-07 13:42:10 +00:00
|
|
|
fmt.Printf("Receipt with block hash %s, trx hash %s\n", rct.BlockHash.Hex(), rct.TxHash.Hex())
|
2019-06-07 16:01:29 +00:00
|
|
|
fmt.Printf("rct: %v\n", rct)
|
2019-06-06 17:55:59 +00:00
|
|
|
for _, l := range rct.Logs {
|
2019-06-07 13:42:10 +00:00
|
|
|
if len(l.Topics) < 1 {
|
2019-10-14 14:41:29 +00:00
|
|
|
logWithCommand.Error(fmt.Sprintf("log only has %d topics", len(l.Topics)))
|
2019-06-07 13:42:10 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
fmt.Printf("Log for block hash %s, trx hash %s, address %s, and with topic0 %s\n",
|
|
|
|
l.BlockHash.Hex(), l.TxHash.Hex(), l.Address.Hex(), l.Topics[0].Hex())
|
|
|
|
fmt.Printf("log: %v\n", l)
|
2019-06-06 17:55:59 +00:00
|
|
|
}
|
|
|
|
}
|
2019-06-07 13:42:10 +00:00
|
|
|
// This assumes leafs only
|
2020-02-20 22:13:19 +00:00
|
|
|
for _, stateNode := range ethData.StateNodes {
|
2021-10-07 09:35:11 +00:00
|
|
|
var acct types.StateAccount
|
2020-02-27 21:07:33 +00:00
|
|
|
err = rlp.DecodeBytes(stateNode.IPLD.Data, &acct)
|
2019-06-06 17:55:59 +00:00
|
|
|
if err != nil {
|
2019-10-14 14:41:29 +00:00
|
|
|
logWithCommand.Error(err)
|
2019-06-07 13:42:10 +00:00
|
|
|
continue
|
2019-06-06 17:55:59 +00:00
|
|
|
}
|
2020-02-27 21:07:33 +00:00
|
|
|
fmt.Printf("Account for key %s, and root %s, with balance %s\n",
|
2020-03-11 18:41:59 +00:00
|
|
|
stateNode.StateLeafKey.Hex(), acct.Root.Hex(), acct.Balance.String())
|
2020-02-27 21:07:33 +00:00
|
|
|
fmt.Printf("state account: %+v\n", acct)
|
2019-06-06 17:55:59 +00:00
|
|
|
}
|
2020-02-20 22:13:19 +00:00
|
|
|
for _, storageNode := range ethData.StorageNodes {
|
2020-03-11 18:41:59 +00:00
|
|
|
fmt.Printf("Storage for state key %s ", storageNode.StateLeafKey.Hex())
|
|
|
|
fmt.Printf("with storage key %s\n", storageNode.StorageLeafKey.Hex())
|
2020-02-20 22:13:19 +00:00
|
|
|
var i []interface{}
|
|
|
|
err := rlp.DecodeBytes(storageNode.IPLD.Data, &i)
|
|
|
|
if err != nil {
|
|
|
|
logWithCommand.Error(err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
// if a value node
|
|
|
|
if len(i) == 1 {
|
|
|
|
valueBytes, ok := i[0].([]byte)
|
|
|
|
if !ok {
|
2019-06-07 13:42:10 +00:00
|
|
|
continue
|
|
|
|
}
|
2020-02-20 22:13:19 +00:00
|
|
|
fmt.Printf("Storage leaf key: %s, and value hash: %s\n",
|
2020-03-11 18:41:59 +00:00
|
|
|
storageNode.StorageLeafKey.Hex(), common.BytesToHash(valueBytes).Hex())
|
2019-06-06 17:55:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
case err = <-sub.Err():
|
2019-10-14 14:41:29 +00:00
|
|
|
logWithCommand.Fatal(err)
|
2019-06-06 17:55:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-30 00:16:52 +00:00
|
|
|
func getRPCClient() (*rpc.Client, error) {
|
|
|
|
vulcPath := viper.GetString("watcher.ethSubscription.wsPath")
|
2019-06-10 22:06:38 +00:00
|
|
|
if vulcPath == "" {
|
2019-10-02 14:10:37 +00:00
|
|
|
vulcPath = "ws://127.0.0.1:8080" // default to and try the default ws url if no path is provided
|
2019-06-10 22:06:38 +00:00
|
|
|
}
|
2020-06-30 00:16:52 +00:00
|
|
|
return rpc.Dial(vulcPath)
|
2019-06-06 17:55:59 +00:00
|
|
|
}
|