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-18 17:28:57 +00:00
|
|
|
"math/big"
|
2019-06-06 17:55:59 +00:00
|
|
|
|
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/state"
|
|
|
|
"github.com/ethereum/go-ethereum/core/types"
|
|
|
|
"github.com/ethereum/go-ethereum/rlp"
|
|
|
|
"github.com/ethereum/go-ethereum/rpc"
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
"github.com/spf13/viper"
|
|
|
|
|
|
|
|
"github.com/vulcanize/vulcanizedb/libraries/shared/streamer"
|
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/config"
|
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/core"
|
2019-10-14 14:41:29 +00:00
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/eth/client"
|
2019-06-06 17:55:59 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// streamSubscribeCmd represents the streamSubscribe command
|
|
|
|
var streamSubscribeCmd = &cobra.Command{
|
|
|
|
Use: "streamSubscribe",
|
2019-10-02 14:10:37 +00:00
|
|
|
Short: "This command is used to subscribe to the super node stream with the provided filters",
|
|
|
|
Long: `This command is for demo and testing purposes and is used to subscribe to the super node with the provided subscription configuration parameters.
|
|
|
|
It does not do anything with the data streamed from the super node 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)
|
2019-06-06 17:55:59 +00:00
|
|
|
streamSubscribe()
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
rootCmd.AddCommand(streamSubscribeCmd)
|
|
|
|
}
|
|
|
|
|
|
|
|
func streamSubscribe() {
|
|
|
|
// Prep the subscription config/filters to be sent to the server
|
2019-08-05 17:56:15 +00:00
|
|
|
configureSubscription()
|
2019-06-06 17:55:59 +00:00
|
|
|
|
|
|
|
// Create a new rpc client and a subscription streamer with that client
|
2019-10-14 14:41:29 +00:00
|
|
|
rpcClient := getRPCClient()
|
2019-10-02 14:10:37 +00:00
|
|
|
str := streamer.NewSuperNodeStreamer(rpcClient)
|
2019-06-06 17:55:59 +00:00
|
|
|
|
|
|
|
// Buffered channel for reading subscription payloads
|
2019-10-02 14:10:37 +00:00
|
|
|
payloadChan := make(chan streamer.SuperNodePayload, 20000)
|
2019-06-06 17:55:59 +00:00
|
|
|
|
2019-10-02 14:10:37 +00:00
|
|
|
// Subscribe to the super node service with the given config/filter parameters
|
2019-08-05 17:56:15 +00:00
|
|
|
sub, err := str.Stream(payloadChan, subscriptionConfig)
|
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:
|
2019-06-07 13:42:10 +00:00
|
|
|
if payload.ErrMsg != "" {
|
2019-10-14 14:41:29 +00:00
|
|
|
logWithCommand.Error(payload.ErrMsg)
|
2019-06-07 13:42:10 +00:00
|
|
|
continue
|
2019-06-06 17:55:59 +00:00
|
|
|
}
|
|
|
|
for _, headerRlp := range payload.HeadersRlp {
|
|
|
|
var header types.Header
|
|
|
|
err = rlp.Decode(bytes.NewBuffer(headerRlp), &header)
|
|
|
|
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("Header number %d, hash %s\n", header.Number.Int64(), header.Hash().Hex())
|
|
|
|
fmt.Printf("header: %v\n", header)
|
2019-06-06 17:55:59 +00:00
|
|
|
}
|
|
|
|
for _, trxRlp := range payload.TransactionsRlp {
|
|
|
|
var trx types.Transaction
|
|
|
|
buff := bytes.NewBuffer(trxRlp)
|
|
|
|
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
|
|
|
}
|
|
|
|
for _, rctRlp := range payload.ReceiptsRlp {
|
2019-06-07 16:01:29 +00:00
|
|
|
var rct types.ReceiptForStorage
|
2019-06-06 17:55:59 +00:00
|
|
|
buff := bytes.NewBuffer(rctRlp)
|
|
|
|
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
|
2019-06-06 17:55:59 +00:00
|
|
|
for key, stateRlp := range payload.StateNodesRlp {
|
|
|
|
var acct state.Account
|
|
|
|
err = rlp.Decode(bytes.NewBuffer(stateRlp), &acct)
|
|
|
|
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("Account for key %s, and root %s, with balance %d\n",
|
|
|
|
key.Hex(), acct.Root.Hex(), acct.Balance.Int64())
|
|
|
|
fmt.Printf("state account: %v\n", acct)
|
2019-06-06 17:55:59 +00:00
|
|
|
}
|
|
|
|
for stateKey, mappedRlp := range payload.StorageNodesRlp {
|
2019-06-07 13:42:10 +00:00
|
|
|
fmt.Printf("Storage for state key %s ", stateKey.Hex())
|
2019-06-06 17:55:59 +00:00
|
|
|
for storageKey, storageRlp := range mappedRlp {
|
2019-06-07 13:42:10 +00:00
|
|
|
fmt.Printf("with storage key %s\n", storageKey.Hex())
|
2019-06-06 17:55:59 +00:00
|
|
|
var i []interface{}
|
2019-10-14 14:41:29 +00:00
|
|
|
err := rlp.DecodeBytes(storageRlp, &i)
|
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
|
|
|
|
}
|
|
|
|
// if a leaf node
|
|
|
|
if len(i) == 2 {
|
|
|
|
keyBytes, ok := i[0].([]byte)
|
|
|
|
if !ok {
|
|
|
|
continue
|
|
|
|
}
|
2019-10-14 14:41:29 +00:00
|
|
|
valueBytes, ok := i[1].([]byte)
|
2019-06-07 13:42:10 +00:00
|
|
|
if !ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
fmt.Printf("Storage leaf key: %s, and value hash: %s\n",
|
|
|
|
common.BytesToHash(keyBytes).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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-05 17:56:15 +00:00
|
|
|
func configureSubscription() {
|
2019-10-14 14:41:29 +00:00
|
|
|
logWithCommand.Info("loading subscription config")
|
2019-08-05 17:56:15 +00:00
|
|
|
subscriptionConfig = config.Subscription{
|
2019-06-06 17:55:59 +00:00
|
|
|
// Below default to false, which means we do not backfill by default
|
|
|
|
BackFill: viper.GetBool("subscription.backfill"),
|
|
|
|
BackFillOnly: viper.GetBool("subscription.backfillOnly"),
|
|
|
|
|
|
|
|
// Below default to 0
|
|
|
|
// 0 start means we start at the beginning and 0 end means we continue indefinitely
|
2019-06-18 17:28:57 +00:00
|
|
|
StartingBlock: big.NewInt(viper.GetInt64("subscription.startingBlock")),
|
|
|
|
EndingBlock: big.NewInt(viper.GetInt64("subscription.endingBlock")),
|
2019-06-06 17:55:59 +00:00
|
|
|
|
|
|
|
// Below default to false, which means we get all headers by default
|
|
|
|
HeaderFilter: config.HeaderFilter{
|
2019-10-08 20:31:07 +00:00
|
|
|
Off: viper.GetBool("subscription.headerFilter.off"),
|
|
|
|
Uncles: viper.GetBool("subscription.headerFilter.uncles"),
|
2019-06-06 17:55:59 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
// Below defaults to false and two slices of length 0
|
|
|
|
// Which means we get all transactions by default
|
|
|
|
TrxFilter: config.TrxFilter{
|
|
|
|
Off: viper.GetBool("subscription.trxFilter.off"),
|
|
|
|
Src: viper.GetStringSlice("subscription.trxFilter.src"),
|
|
|
|
Dst: viper.GetStringSlice("subscription.trxFilter.dst"),
|
|
|
|
},
|
|
|
|
|
|
|
|
// Below defaults to false and one slice of length 0
|
|
|
|
// Which means we get all receipts by default
|
|
|
|
ReceiptFilter: config.ReceiptFilter{
|
2019-06-20 15:59:10 +00:00
|
|
|
Off: viper.GetBool("subscription.receiptFilter.off"),
|
|
|
|
Contracts: viper.GetStringSlice("subscription.receiptFilter.contracts"),
|
|
|
|
Topic0s: viper.GetStringSlice("subscription.receiptFilter.topic0s"),
|
2019-06-06 17:55:59 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
// Below defaults to two false, and a slice of length 0
|
|
|
|
// Which means we get all state leafs by default, but no intermediate nodes
|
|
|
|
StateFilter: config.StateFilter{
|
|
|
|
Off: viper.GetBool("subscription.stateFilter.off"),
|
|
|
|
IntermediateNodes: viper.GetBool("subscription.stateFilter.intermediateNodes"),
|
|
|
|
Addresses: viper.GetStringSlice("subscription.stateFilter.addresses"),
|
|
|
|
},
|
|
|
|
|
|
|
|
// Below defaults to two false, and two slices of length 0
|
|
|
|
// Which means we get all storage leafs by default, but no intermediate nodes
|
|
|
|
StorageFilter: config.StorageFilter{
|
|
|
|
Off: viper.GetBool("subscription.storageFilter.off"),
|
|
|
|
IntermediateNodes: viper.GetBool("subscription.storageFilter.intermediateNodes"),
|
|
|
|
Addresses: viper.GetStringSlice("subscription.storageFilter.addresses"),
|
|
|
|
StorageKeys: viper.GetStringSlice("subscription.storageFilter.storageKeys"),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-14 14:41:29 +00:00
|
|
|
func getRPCClient() core.RpcClient {
|
2019-06-10 22:06:38 +00:00
|
|
|
vulcPath := viper.GetString("subscription.path")
|
|
|
|
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
|
|
|
}
|
2019-10-14 14:41:29 +00:00
|
|
|
rawRPCClient, err := rpc.Dial(vulcPath)
|
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
|
|
|
return client.NewRpcClient(rawRPCClient, vulcPath)
|
2019-06-06 17:55:59 +00:00
|
|
|
}
|