-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Victor Neznaykin
committed
Jan 10, 2024
1 parent
76e3ce4
commit d0d6418
Showing
12 changed files
with
222 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package rpc | ||
|
||
import ( | ||
"context" | ||
|
||
coretypes "github.com/cometbft/cometbft/rpc/core/types" | ||
) | ||
|
||
func (c *Client) GetBlockResults(ctx context.Context, height int64) (*coretypes.ResultBlockResults, error) { | ||
ctx, cancel := context.WithTimeout(ctx, c.cfg.Timeout) | ||
defer cancel() | ||
|
||
result, err := c.RPCClient.BlockResults(ctx, &height) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return result, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package broker | ||
|
||
import ( | ||
"context" | ||
) | ||
|
||
func (b *Broker) PublishRawBlock(_ context.Context, block interface{}) error { | ||
return b.marshalAndProduce(Account, block) | ||
} | ||
|
||
func (b *Broker) PublishRawTransaction(_ context.Context, tx interface{}) error { | ||
return b.marshalAndProduce(Account, tx) | ||
} | ||
|
||
func (b *Broker) PublishRawBlockResults(_ context.Context, br interface{}) error { | ||
return b.marshalAndProduce(Account, br) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package raw | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"fmt" | ||
|
||
jsoniter "github.com/json-iterator/go" | ||
|
||
"github.com/bro-n-bro/spacebox-crawler/types" | ||
) | ||
|
||
func (m *Module) HandleBlock(ctx context.Context, block *types.Block) error { | ||
rawBlock := struct { | ||
Hash string `json:"hash"` | ||
ProposerAddress string `json:"proposer_address"` | ||
Block json.RawMessage `json:"block"` | ||
TotalGas uint64 `json:"total_gas"` | ||
NumTxs uint16 `json:"num_txs"` | ||
}{ | ||
TotalGas: block.TotalGas, | ||
Hash: block.Hash, | ||
ProposerAddress: block.ProposerAddress, | ||
NumTxs: uint16(block.TxNum), | ||
} | ||
|
||
var err error | ||
rawBlock.Block, err = jsoniter.Marshal(block.Raw().Block) | ||
if err != nil { | ||
return fmt.Errorf("failed to marshal block: %w", err) | ||
} | ||
|
||
if err = m.broker.PublishRawBlock(ctx, rawBlock); err != nil { | ||
return fmt.Errorf("failed to publish raw block: %w", err) | ||
} | ||
|
||
return m.publishBlockResults(ctx, block.Height) | ||
} | ||
|
||
func (m *Module) publishBlockResults(ctx context.Context, height int64) error { | ||
rawBR, err := m.rpcClient.GetBlockResults(ctx, height) | ||
if err != nil { | ||
return fmt.Errorf("failed to get block results: %w", err) | ||
} | ||
|
||
return m.broker.PublishRawBlockResults(ctx, rawBR) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package raw | ||
|
||
import "context" | ||
|
||
type broker interface { | ||
PublishRawBlock(ctx context.Context, b interface{}) error | ||
PublishRawTransaction(ctx context.Context, tx interface{}) error | ||
PublishRawBlockResults(ctx context.Context, br interface{}) error | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package raw | ||
|
||
import ( | ||
"github.com/rs/zerolog" | ||
|
||
rpcClient "github.com/bro-n-bro/spacebox-crawler/client/rpc" | ||
"github.com/bro-n-bro/spacebox-crawler/modules/utils" | ||
tb "github.com/bro-n-bro/spacebox-crawler/pkg/mapper/to_broker" | ||
"github.com/bro-n-bro/spacebox-crawler/types" | ||
) | ||
|
||
const ( | ||
ModuleName = "raw" | ||
) | ||
|
||
var ( | ||
_ types.Module = &Module{} | ||
_ types.BlockHandler = &Module{} | ||
) | ||
|
||
type Module struct { | ||
log *zerolog.Logger | ||
rpcClient *rpcClient.Client | ||
broker broker | ||
tbM tb.ToBroker | ||
} | ||
|
||
func New(b broker, cli *rpcClient.Client, tbM tb.ToBroker) *Module { | ||
return &Module{ | ||
log: utils.NewModuleLogger(ModuleName), | ||
broker: b, | ||
rpcClient: cli, | ||
tbM: tbM, | ||
} | ||
} | ||
|
||
func (m *Module) Name() string { return ModuleName } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package raw | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"encoding/json" | ||
"fmt" | ||
"sync" | ||
|
||
"github.com/cosmos/gogoproto/jsonpb" | ||
|
||
"github.com/bro-n-bro/spacebox-crawler/types" | ||
) | ||
|
||
var ( | ||
bufPool = &sync.Pool{ | ||
New: func() interface{} { | ||
return new(bytes.Buffer) | ||
}, | ||
} | ||
|
||
marshler = &jsonpb.Marshaler{ | ||
EmitDefaults: false, // Set to false if you don't want to include default values in the JSON output | ||
} | ||
) | ||
|
||
func (m *Module) HandleTx(ctx context.Context, tx *types.Tx) error { | ||
rawTx := struct { | ||
Signer string `json:"signer"` | ||
TxResponse json.RawMessage `json:"tx_response"` | ||
}{ | ||
Signer: tx.Signer, | ||
} | ||
|
||
b := bufPool.Get().(*bytes.Buffer) //nolint:forcetypeassert | ||
b.Reset() | ||
defer bufPool.Put(b) | ||
|
||
if err := marshler.Marshal(b, tx.TxResponse); err != nil { | ||
return fmt.Errorf("failed to marshal tx response: %w", err) | ||
} | ||
|
||
rawTx.TxResponse = append(rawTx.TxResponse, b.Bytes()...) | ||
|
||
return m.broker.PublishRawTransaction(ctx, rawTx) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters