mirror of
https://github.com/alexgo-io/gaze-indexer.git
synced 2026-04-26 12:55:20 +08:00
refactor: prepare for moving integration tests.
This commit is contained in:
@@ -11,9 +11,9 @@ import (
|
||||
"github.com/samber/lo"
|
||||
)
|
||||
|
||||
func (p *Processor) processDelegate(ctx context.Context, qtx datagateway.NodeSaleDataGatewayWithTx, block *types.Block, event nodeSaleEvent) error {
|
||||
func (p *Processor) ProcessDelegate(ctx context.Context, qtx datagateway.NodeSaleDataGatewayWithTx, block *types.Block, event NodeSaleEvent) error {
|
||||
validator := delegatevalidator.New()
|
||||
delegate := event.eventMessage.Delegate
|
||||
delegate := event.EventMessage.Delegate
|
||||
|
||||
_, nodes, err := validator.NodesExist(ctx, qtx, delegate.DeployID, delegate.NodeIDs)
|
||||
if err != nil {
|
||||
@@ -21,23 +21,23 @@ func (p *Processor) processDelegate(ctx context.Context, qtx datagateway.NodeSal
|
||||
}
|
||||
|
||||
for _, node := range nodes {
|
||||
valid := validator.EqualXonlyPublicKey(node.OwnerPublicKey, event.txPubkey)
|
||||
valid := validator.EqualXonlyPublicKey(node.OwnerPublicKey, event.TxPubkey)
|
||||
if !valid {
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
err = qtx.CreateEvent(ctx, entity.NodeSaleEvent{
|
||||
TxHash: event.transaction.TxHash.String(),
|
||||
TxIndex: int32(event.transaction.Index),
|
||||
Action: int32(event.eventMessage.Action),
|
||||
RawMessage: event.rawData,
|
||||
ParsedMessage: event.eventJson,
|
||||
TxHash: event.Transaction.TxHash.String(),
|
||||
TxIndex: int32(event.Transaction.Index),
|
||||
Action: int32(event.EventMessage.Action),
|
||||
RawMessage: event.RawData,
|
||||
ParsedMessage: event.EventJson,
|
||||
BlockTimestamp: block.Header.Timestamp,
|
||||
BlockHash: event.transaction.BlockHash.String(),
|
||||
BlockHeight: event.transaction.BlockHeight,
|
||||
BlockHash: event.Transaction.BlockHash.String(),
|
||||
BlockHeight: event.Transaction.BlockHeight,
|
||||
Valid: validator.Valid,
|
||||
WalletAddress: p.pubkeyToPkHashAddress(event.txPubkey).EncodeAddress(),
|
||||
WalletAddress: p.PubkeyToPkHashAddress(event.TxPubkey).EncodeAddress(),
|
||||
Metadata: nil,
|
||||
Reason: validator.Reason,
|
||||
})
|
||||
@@ -51,7 +51,7 @@ func (p *Processor) processDelegate(ctx context.Context, qtx datagateway.NodeSal
|
||||
SaleBlock: int64(delegate.DeployID.Block),
|
||||
SaleTxIndex: int32(delegate.DeployID.TxIndex),
|
||||
Delegatee: delegate.DelegateePublicKey,
|
||||
DelegateTxHash: event.transaction.TxHash.String(),
|
||||
DelegateTxHash: event.Transaction.TxHash.String(),
|
||||
NodeIds: nodeIds,
|
||||
})
|
||||
if err != nil {
|
||||
|
||||
@@ -24,7 +24,7 @@ func TestDelegate(t *testing.T) {
|
||||
sellerPrivateKey, err := btcec.NewPrivateKey()
|
||||
require.NoError(t, err)
|
||||
sellerPubkeyHex := hex.EncodeToString(sellerPrivateKey.PubKey().SerializeCompressed())
|
||||
sellerWallet := p.pubkeyToPkHashAddress(sellerPrivateKey.PubKey())
|
||||
sellerWallet := p.PubkeyToPkHashAddress(sellerPrivateKey.PubKey())
|
||||
startAt := time.Now().Add(time.Hour * -1)
|
||||
endAt := time.Now().Add(time.Hour * 1)
|
||||
deployMessage := &protobuf.NodeSaleEvent{
|
||||
@@ -57,7 +57,7 @@ func TestDelegate(t *testing.T) {
|
||||
},
|
||||
}
|
||||
event, block := assembleTestEvent(sellerPrivateKey, "111111", "111111", 0, 0, deployMessage)
|
||||
err = p.processDeploy(ctx, qtx, block, event)
|
||||
err = p.ProcessDeploy(ctx, qtx, block, event)
|
||||
require.NoError(t, err)
|
||||
|
||||
buyerPrivateKey, _ := btcec.NewPrivateKey()
|
||||
@@ -89,15 +89,15 @@ func TestDelegate(t *testing.T) {
|
||||
|
||||
event, block = assembleTestEvent(buyerPrivateKey, "1212121212", "1212121212", 0, 0, message)
|
||||
|
||||
addr, _ := btcutil.NewAddressPubKey(sellerPrivateKey.PubKey().SerializeCompressed(), p.network.ChainParams())
|
||||
addr, _ := btcutil.NewAddressPubKey(sellerPrivateKey.PubKey().SerializeCompressed(), p.Network.ChainParams())
|
||||
pkscript, _ := txscript.PayToAddrScript(addr.AddressPubKeyHash())
|
||||
event.transaction.TxOut = []*types.TxOut{
|
||||
event.Transaction.TxOut = []*types.TxOut{
|
||||
{
|
||||
PkScript: pkscript,
|
||||
Value: 600,
|
||||
},
|
||||
}
|
||||
p.processPurchase(ctx, qtx, block, event)
|
||||
p.ProcessPurchase(ctx, qtx, block, event)
|
||||
|
||||
delegateePrivateKey, _ := btcec.NewPrivateKey()
|
||||
delegateePubkeyHex := hex.EncodeToString(delegateePrivateKey.PubKey().SerializeCompressed())
|
||||
@@ -113,7 +113,7 @@ func TestDelegate(t *testing.T) {
|
||||
},
|
||||
}
|
||||
event, block = assembleTestEvent(buyerPrivateKey, "131313131313", "131313131313", 0, 0, delegateMessage)
|
||||
p.processDelegate(ctx, qtx, block, event)
|
||||
p.ProcessDelegate(ctx, qtx, block, event)
|
||||
|
||||
nodes, _ := qtx.GetNodesByIds(ctx, datagateway.GetNodesByIdsParams{
|
||||
SaleBlock: testBlockHeight - 3,
|
||||
|
||||
@@ -12,24 +12,24 @@ import (
|
||||
"google.golang.org/protobuf/encoding/protojson"
|
||||
)
|
||||
|
||||
func (p *Processor) processDeploy(ctx context.Context, qtx datagateway.NodeSaleDataGatewayWithTx, block *types.Block, event nodeSaleEvent) error {
|
||||
deploy := event.eventMessage.Deploy
|
||||
func (p *Processor) ProcessDeploy(ctx context.Context, qtx datagateway.NodeSaleDataGatewayWithTx, block *types.Block, event NodeSaleEvent) error {
|
||||
deploy := event.EventMessage.Deploy
|
||||
|
||||
validator := validator.New()
|
||||
|
||||
validator.EqualXonlyPublicKey(deploy.SellerPublicKey, event.txPubkey)
|
||||
validator.EqualXonlyPublicKey(deploy.SellerPublicKey, event.TxPubkey)
|
||||
|
||||
err := qtx.CreateEvent(ctx, entity.NodeSaleEvent{
|
||||
TxHash: event.transaction.TxHash.String(),
|
||||
TxIndex: int32(event.transaction.Index),
|
||||
Action: int32(event.eventMessage.Action),
|
||||
RawMessage: event.rawData,
|
||||
ParsedMessage: event.eventJson,
|
||||
TxHash: event.Transaction.TxHash.String(),
|
||||
TxIndex: int32(event.Transaction.Index),
|
||||
Action: int32(event.EventMessage.Action),
|
||||
RawMessage: event.RawData,
|
||||
ParsedMessage: event.EventJson,
|
||||
BlockTimestamp: block.Header.Timestamp,
|
||||
BlockHash: event.transaction.BlockHash.String(),
|
||||
BlockHeight: event.transaction.BlockHeight,
|
||||
BlockHash: event.Transaction.BlockHash.String(),
|
||||
BlockHeight: event.Transaction.BlockHeight,
|
||||
Valid: validator.Valid,
|
||||
WalletAddress: p.pubkeyToPkHashAddress(event.txPubkey).EncodeAddress(),
|
||||
WalletAddress: p.PubkeyToPkHashAddress(event.TxPubkey).EncodeAddress(),
|
||||
Metadata: nil,
|
||||
Reason: validator.Reason,
|
||||
})
|
||||
@@ -46,15 +46,15 @@ func (p *Processor) processDeploy(ctx context.Context, qtx datagateway.NodeSaleD
|
||||
tiers[i] = tierJson
|
||||
}
|
||||
err = qtx.CreateNodeSale(ctx, entity.NodeSale{
|
||||
BlockHeight: event.transaction.BlockHeight,
|
||||
TxIndex: int32(event.transaction.Index),
|
||||
BlockHeight: event.Transaction.BlockHeight,
|
||||
TxIndex: int32(event.Transaction.Index),
|
||||
Name: deploy.Name,
|
||||
StartsAt: time.Unix(int64(deploy.StartsAt), 0),
|
||||
EndsAt: time.Unix(int64(deploy.EndsAt), 0),
|
||||
Tiers: tiers,
|
||||
SellerPublicKey: deploy.SellerPublicKey,
|
||||
MaxPerAddress: int32(deploy.MaxPerAddress),
|
||||
DeployTxHash: event.transaction.TxHash.String(),
|
||||
DeployTxHash: event.Transaction.TxHash.String(),
|
||||
MaxDiscountPercentage: int32(deploy.MaxDiscountPercentage),
|
||||
SellerWallet: deploy.SellerWallet,
|
||||
})
|
||||
|
||||
@@ -20,7 +20,7 @@ func TestDeployInvalid(t *testing.T) {
|
||||
strangerKey, err := btcec.NewPrivateKey()
|
||||
require.NoError(t, err)
|
||||
strangerPubkeyHex := hex.EncodeToString(strangerKey.PubKey().SerializeCompressed())
|
||||
sellerWallet := p.pubkeyToPkHashAddress(prvKey.PubKey())
|
||||
sellerWallet := p.PubkeyToPkHashAddress(prvKey.PubKey())
|
||||
message := &protobuf.NodeSaleEvent{
|
||||
Action: protobuf.Action_ACTION_DEPLOY,
|
||||
Deploy: &protobuf.ActionDeploy{
|
||||
@@ -47,7 +47,7 @@ func TestDeployInvalid(t *testing.T) {
|
||||
}
|
||||
|
||||
event, block := assembleTestEvent(prvKey, "0101010101", "0101010101", 0, 0, message)
|
||||
p.processDeploy(ctx, qtx, block, event)
|
||||
p.ProcessDeploy(ctx, qtx, block, event)
|
||||
|
||||
nodeSales, _ := qtx.GetNodeSale(ctx, datagateway.GetNodeSaleParams{
|
||||
BlockHeight: testBlockHeight - 1,
|
||||
@@ -62,7 +62,7 @@ func TestDeployValid(t *testing.T) {
|
||||
}
|
||||
privateKey, _ := btcec.NewPrivateKey()
|
||||
pubkeyHex := hex.EncodeToString(privateKey.PubKey().SerializeCompressed())
|
||||
sellerWallet := p.pubkeyToPkHashAddress(privateKey.PubKey())
|
||||
sellerWallet := p.PubkeyToPkHashAddress(privateKey.PubKey())
|
||||
startAt := time.Now().Add(time.Hour * -1)
|
||||
endAt := time.Now().Add(time.Hour * 1)
|
||||
message := &protobuf.NodeSaleEvent{
|
||||
@@ -91,7 +91,7 @@ func TestDeployValid(t *testing.T) {
|
||||
}
|
||||
|
||||
event, block := assembleTestEvent(privateKey, "0202020202", "0202020202", 0, 0, message)
|
||||
p.processDeploy(ctx, qtx, block, event)
|
||||
p.ProcessDeploy(ctx, qtx, block, event)
|
||||
|
||||
nodeSales, _ := qtx.GetNodeSale(ctx, datagateway.GetNodeSaleParams{
|
||||
BlockHeight: testBlockHeight - 1,
|
||||
|
||||
@@ -41,9 +41,9 @@ func New(injector do.Injector) (indexer.IndexerWorker, error) {
|
||||
repository := repository.NewRepository(pg)
|
||||
|
||||
processor := &Processor{
|
||||
nodeSaleDg: repository,
|
||||
btcClient: datasource,
|
||||
network: conf.Network,
|
||||
NodeSaleDg: repository,
|
||||
BtcClient: datasource,
|
||||
Network: conf.Network,
|
||||
cleanupFuncs: cleanupFuncs,
|
||||
lastBlockDefault: conf.Modules.NodeSale.LastBlockDefault,
|
||||
}
|
||||
|
||||
@@ -56,12 +56,12 @@ func TestMain(m *testing.M) {
|
||||
datagateway := repository.NewRepository(db)
|
||||
|
||||
p = &Processor{
|
||||
nodeSaleDg: datagateway,
|
||||
network: common.NetworkMainnet,
|
||||
NodeSaleDg: datagateway,
|
||||
Network: common.NetworkMainnet,
|
||||
}
|
||||
repo.ClearEvents(ctx)
|
||||
|
||||
qtx, err = p.nodeSaleDg.BeginNodeSaleTx(ctx)
|
||||
qtx, err = p.NodeSaleDg.BeginNodeSaleTx(ctx)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
@@ -70,7 +70,7 @@ func TestMain(m *testing.M) {
|
||||
qtx.Commit(ctx)
|
||||
}
|
||||
|
||||
func assembleTestEvent(privateKey *secp256k1.PrivateKey, blockHashHex, txHashHex string, blockHeight int64, txIndex int, message *protobuf.NodeSaleEvent) (nodeSaleEvent, *types.Block) {
|
||||
func assembleTestEvent(privateKey *secp256k1.PrivateKey, blockHashHex, txHashHex string, blockHeight int64, txIndex int, message *protobuf.NodeSaleEvent) (NodeSaleEvent, *types.Block) {
|
||||
blockHash, _ := chainhash.NewHashFromStr(blockHashHex)
|
||||
txHash, _ := chainhash.NewHashFromStr(txHashHex)
|
||||
|
||||
@@ -93,17 +93,17 @@ func assembleTestEvent(privateKey *secp256k1.PrivateKey, blockHashHex, txHashHex
|
||||
testTxIndex++
|
||||
}
|
||||
|
||||
event := nodeSaleEvent{
|
||||
transaction: &types.Transaction{
|
||||
event := NodeSaleEvent{
|
||||
Transaction: &types.Transaction{
|
||||
BlockHeight: int64(blockHeight),
|
||||
BlockHash: *blockHash,
|
||||
Index: uint32(txIndex),
|
||||
TxHash: *txHash,
|
||||
},
|
||||
rawData: rawData,
|
||||
eventMessage: message,
|
||||
eventJson: messageJson,
|
||||
txPubkey: privateKey.PubKey(),
|
||||
RawData: rawData,
|
||||
EventMessage: message,
|
||||
EventJson: messageJson,
|
||||
TxPubkey: privateKey.PubKey(),
|
||||
}
|
||||
block := &types.Block{
|
||||
Header: types.BlockHeader{
|
||||
|
||||
@@ -22,6 +22,29 @@ import (
|
||||
"github.com/gaze-network/indexer-network/modules/nodesale/protobuf"
|
||||
)
|
||||
|
||||
type NodeSaleEvent struct {
|
||||
Transaction *types.Transaction
|
||||
EventMessage *protobuf.NodeSaleEvent
|
||||
EventJson []byte
|
||||
TxPubkey *btcec.PublicKey
|
||||
RawData []byte
|
||||
}
|
||||
|
||||
func NewProcessor(repository datagateway.NodeSaleDataGateway,
|
||||
datasource *datasources.BitcoinNodeDatasource,
|
||||
network common.Network,
|
||||
cleanupFuncs []func(context.Context) error,
|
||||
lastBlockDefault int64,
|
||||
) *Processor {
|
||||
return &Processor{
|
||||
NodeSaleDg: repository,
|
||||
BtcClient: datasource,
|
||||
Network: network,
|
||||
cleanupFuncs: cleanupFuncs,
|
||||
lastBlockDefault: lastBlockDefault,
|
||||
}
|
||||
}
|
||||
|
||||
func (p *Processor) Shutdown(ctx context.Context) error {
|
||||
for _, cleanupFunc := range p.cleanupFuncs {
|
||||
err := cleanupFunc(ctx)
|
||||
@@ -33,20 +56,20 @@ func (p *Processor) Shutdown(ctx context.Context) error {
|
||||
}
|
||||
|
||||
type Processor struct {
|
||||
nodeSaleDg datagateway.NodeSaleDataGateway
|
||||
btcClient *datasources.BitcoinNodeDatasource
|
||||
network common.Network
|
||||
NodeSaleDg datagateway.NodeSaleDataGateway
|
||||
BtcClient *datasources.BitcoinNodeDatasource
|
||||
Network common.Network
|
||||
cleanupFuncs []func(context.Context) error
|
||||
lastBlockDefault int64
|
||||
}
|
||||
|
||||
// CurrentBlock implements indexer.Processor.
|
||||
func (p *Processor) CurrentBlock(ctx context.Context) (types.BlockHeader, error) {
|
||||
block, err := p.nodeSaleDg.GetLastProcessedBlock(ctx)
|
||||
block, err := p.NodeSaleDg.GetLastProcessedBlock(ctx)
|
||||
if err != nil {
|
||||
logger.InfoContext(ctx, "Couldn't get last processed block. Start from NODESALE_LAST_BLOCK_DEFAULT.",
|
||||
slogx.Int64("currentBlock", p.lastBlockDefault))
|
||||
header, err := p.btcClient.GetBlockHeader(ctx, p.lastBlockDefault)
|
||||
header, err := p.BtcClient.GetBlockHeader(ctx, p.lastBlockDefault)
|
||||
if err != nil {
|
||||
return types.BlockHeader{}, errors.Wrap(err, "Cannot get default block from bitcoin node")
|
||||
}
|
||||
@@ -68,7 +91,7 @@ func (p *Processor) CurrentBlock(ctx context.Context) (types.BlockHeader, error)
|
||||
|
||||
// GetIndexedBlock implements indexer.Processor.
|
||||
func (p *Processor) GetIndexedBlock(ctx context.Context, height int64) (types.BlockHeader, error) {
|
||||
block, err := p.nodeSaleDg.GetBlock(ctx, height)
|
||||
block, err := p.NodeSaleDg.GetBlock(ctx, height)
|
||||
if err != nil {
|
||||
return types.BlockHeader{}, errors.Wrapf(err, "Block %d not found", height)
|
||||
}
|
||||
@@ -126,16 +149,8 @@ func extractNodeSaleData(witness [][]byte) (data []byte, internalPubkey *btcec.P
|
||||
return []byte{}, nil, false
|
||||
}
|
||||
|
||||
type nodeSaleEvent struct {
|
||||
transaction *types.Transaction
|
||||
eventMessage *protobuf.NodeSaleEvent
|
||||
eventJson []byte
|
||||
txPubkey *btcec.PublicKey
|
||||
rawData []byte
|
||||
}
|
||||
|
||||
func (p *Processor) parseTransactions(ctx context.Context, transactions []*types.Transaction) ([]nodeSaleEvent, error) {
|
||||
var events []nodeSaleEvent
|
||||
func (p *Processor) parseTransactions(ctx context.Context, transactions []*types.Transaction) ([]NodeSaleEvent, error) {
|
||||
var events []NodeSaleEvent
|
||||
for _, t := range transactions {
|
||||
for _, txIn := range t.TxIn {
|
||||
data, txPubkey, isNodeSale := extractNodeSaleData(txIn.Witness)
|
||||
@@ -153,15 +168,15 @@ func (p *Processor) parseTransactions(ctx context.Context, transactions []*types
|
||||
}
|
||||
eventJson, err := protojson.Marshal(event)
|
||||
if err != nil {
|
||||
return []nodeSaleEvent{}, errors.Wrap(err, "Failed to parse protobuf to json")
|
||||
return []NodeSaleEvent{}, errors.Wrap(err, "Failed to parse protobuf to json")
|
||||
}
|
||||
|
||||
events = append(events, nodeSaleEvent{
|
||||
transaction: t,
|
||||
eventMessage: event,
|
||||
eventJson: eventJson,
|
||||
rawData: data,
|
||||
txPubkey: txPubkey,
|
||||
events = append(events, NodeSaleEvent{
|
||||
Transaction: t,
|
||||
EventMessage: event,
|
||||
EventJson: eventJson,
|
||||
RawData: data,
|
||||
TxPubkey: txPubkey,
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -180,7 +195,7 @@ func (p *Processor) Process(ctx context.Context, inputs []*types.Block) error {
|
||||
return errors.Wrap(err, "Invalid data from bitcoin client")
|
||||
}
|
||||
// open transaction
|
||||
qtx, err := p.nodeSaleDg.BeginNodeSaleTx(ctx)
|
||||
qtx, err := p.NodeSaleDg.BeginNodeSaleTx(ctx)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "Failed to create transaction")
|
||||
}
|
||||
@@ -203,29 +218,29 @@ func (p *Processor) Process(ctx context.Context, inputs []*types.Block) error {
|
||||
// for each events
|
||||
for _, event := range events {
|
||||
logger.InfoContext(ctx, "NodeSale processing event",
|
||||
slogx.Uint32("txIndex", event.transaction.Index),
|
||||
slogx.Uint32("txIndex", event.Transaction.Index),
|
||||
slogx.Int64("blockHeight", block.Header.Height),
|
||||
slogx.Stringer("blockhash", block.Header.Hash),
|
||||
)
|
||||
eventMessage := event.eventMessage
|
||||
eventMessage := event.EventMessage
|
||||
switch eventMessage.Action {
|
||||
case protobuf.Action_ACTION_DEPLOY:
|
||||
err = p.processDeploy(ctx, qtx, block, event)
|
||||
err = p.ProcessDeploy(ctx, qtx, block, event)
|
||||
if err != nil {
|
||||
return errors.Wrapf(err, "Failed to deploy at block %d", block.Header.Height)
|
||||
}
|
||||
case protobuf.Action_ACTION_DELEGATE:
|
||||
err = p.processDelegate(ctx, qtx, block, event)
|
||||
err = p.ProcessDelegate(ctx, qtx, block, event)
|
||||
if err != nil {
|
||||
return errors.Wrapf(err, "Failed to delegate at block %d", block.Header.Height)
|
||||
}
|
||||
case protobuf.Action_ACTION_PURCHASE:
|
||||
err = p.processPurchase(ctx, qtx, block, event)
|
||||
err = p.ProcessPurchase(ctx, qtx, block, event)
|
||||
if err != nil {
|
||||
return errors.Wrapf(err, "Failed to purchase at block %d", block.Header.Height)
|
||||
}
|
||||
default:
|
||||
logger.DebugContext(ctx, "Invalid event ACTION", slogx.Stringer("txHash", (event.transaction.TxHash)))
|
||||
logger.DebugContext(ctx, "Invalid event ACTION", slogx.Stringer("txHash", (event.Transaction.TxHash)))
|
||||
}
|
||||
}
|
||||
// close transaction
|
||||
@@ -242,7 +257,7 @@ func (p *Processor) Process(ctx context.Context, inputs []*types.Block) error {
|
||||
|
||||
// RevertData implements indexer.Processor.
|
||||
func (p *Processor) RevertData(ctx context.Context, from int64) error {
|
||||
qtx, err := p.nodeSaleDg.BeginNodeSaleTx(ctx)
|
||||
qtx, err := p.NodeSaleDg.BeginNodeSaleTx(ctx)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "Failed to create transaction")
|
||||
}
|
||||
|
||||
@@ -5,8 +5,8 @@ import (
|
||||
"github.com/btcsuite/btcd/btcutil"
|
||||
)
|
||||
|
||||
func (p *Processor) pubkeyToPkHashAddress(pubKey *btcec.PublicKey) btcutil.Address {
|
||||
addrPubKey, _ := btcutil.NewAddressPubKey(pubKey.SerializeCompressed(), p.network.ChainParams())
|
||||
func (p *Processor) PubkeyToPkHashAddress(pubKey *btcec.PublicKey) btcutil.Address {
|
||||
addrPubKey, _ := btcutil.NewAddressPubKey(pubKey.SerializeCompressed(), p.Network.ChainParams())
|
||||
addrPubKeyHash := addrPubKey.AddressPubKeyHash()
|
||||
return addrPubKeyHash
|
||||
}
|
||||
|
||||
@@ -10,13 +10,13 @@ import (
|
||||
purchasevalidator "github.com/gaze-network/indexer-network/modules/nodesale/internal/validator/purchase"
|
||||
)
|
||||
|
||||
func (p *Processor) processPurchase(ctx context.Context, qtx datagateway.NodeSaleDataGatewayWithTx, block *types.Block, event nodeSaleEvent) error {
|
||||
purchase := event.eventMessage.Purchase
|
||||
func (p *Processor) ProcessPurchase(ctx context.Context, qtx datagateway.NodeSaleDataGatewayWithTx, block *types.Block, event NodeSaleEvent) error {
|
||||
purchase := event.EventMessage.Purchase
|
||||
payload := purchase.Payload
|
||||
|
||||
validator := purchasevalidator.New()
|
||||
|
||||
validator.EqualXonlyPublicKey(payload.BuyerPublicKey, event.txPubkey)
|
||||
validator.EqualXonlyPublicKey(payload.BuyerPublicKey, event.TxPubkey)
|
||||
|
||||
_, deploy, err := validator.NodeSaleExists(ctx, qtx, payload)
|
||||
if err != nil {
|
||||
@@ -24,7 +24,7 @@ func (p *Processor) processPurchase(ctx context.Context, qtx datagateway.NodeSal
|
||||
}
|
||||
|
||||
validator.ValidTimestamp(deploy, block.Header.Timestamp)
|
||||
validator.WithinTimeoutBlock(payload.TimeOutBlock, uint64(event.transaction.BlockHeight))
|
||||
validator.WithinTimeoutBlock(payload.TimeOutBlock, uint64(event.Transaction.BlockHeight))
|
||||
|
||||
validator.VerifySignature(purchase, deploy)
|
||||
|
||||
@@ -39,7 +39,7 @@ func (p *Processor) processPurchase(ctx context.Context, qtx datagateway.NodeSal
|
||||
return errors.Wrap(err, "cannot query. Something wrong.")
|
||||
}
|
||||
|
||||
_, meta := validator.ValidPaidAmount(payload, deploy, event.transaction.TxOut, tiers, buyingTiersCount, p.network.ChainParams())
|
||||
_, meta := validator.ValidPaidAmount(payload, deploy, event.Transaction.TxOut, tiers, buyingTiersCount, p.Network.ChainParams())
|
||||
|
||||
_, err = validator.WithinLimit(ctx, qtx, payload, deploy, tiers, buyingTiersCount)
|
||||
if err != nil {
|
||||
@@ -47,16 +47,16 @@ func (p *Processor) processPurchase(ctx context.Context, qtx datagateway.NodeSal
|
||||
}
|
||||
|
||||
err = qtx.CreateEvent(ctx, entity.NodeSaleEvent{
|
||||
TxHash: event.transaction.TxHash.String(),
|
||||
TxIndex: int32(event.transaction.Index),
|
||||
Action: int32(event.eventMessage.Action),
|
||||
RawMessage: event.rawData,
|
||||
ParsedMessage: event.eventJson,
|
||||
TxHash: event.Transaction.TxHash.String(),
|
||||
TxIndex: int32(event.Transaction.Index),
|
||||
Action: int32(event.EventMessage.Action),
|
||||
RawMessage: event.RawData,
|
||||
ParsedMessage: event.EventJson,
|
||||
BlockTimestamp: block.Header.Timestamp,
|
||||
BlockHash: event.transaction.BlockHash.String(),
|
||||
BlockHeight: event.transaction.BlockHeight,
|
||||
BlockHash: event.Transaction.BlockHash.String(),
|
||||
BlockHeight: event.Transaction.BlockHeight,
|
||||
Valid: validator.Valid,
|
||||
WalletAddress: p.pubkeyToPkHashAddress(event.txPubkey).EncodeAddress(),
|
||||
WalletAddress: p.PubkeyToPkHashAddress(event.TxPubkey).EncodeAddress(),
|
||||
Metadata: meta,
|
||||
Reason: validator.Reason,
|
||||
})
|
||||
@@ -74,7 +74,7 @@ func (p *Processor) processPurchase(ctx context.Context, qtx datagateway.NodeSal
|
||||
TierIndex: nodeIdToTier[nodeId],
|
||||
DelegatedTo: "",
|
||||
OwnerPublicKey: payload.BuyerPublicKey,
|
||||
PurchaseTxHash: event.transaction.TxHash.String(),
|
||||
PurchaseTxHash: event.Transaction.TxHash.String(),
|
||||
DelegateTxHash: "",
|
||||
})
|
||||
if err != nil {
|
||||
|
||||
@@ -43,7 +43,7 @@ func TestInvalidPurchase(t *testing.T) {
|
||||
|
||||
event, block := assembleTestEvent(buyerPrivateKey, "030303030303", "030303030303", 0, 0, message)
|
||||
|
||||
p.processPurchase(ctx, qtx, block, event)
|
||||
p.ProcessPurchase(ctx, qtx, block, event)
|
||||
|
||||
nodes, _ := qtx.GetNodesByIds(ctx, datagateway.GetNodesByIdsParams{
|
||||
SaleBlock: 111,
|
||||
@@ -59,7 +59,7 @@ func TestInvalidTimestamp(t *testing.T) {
|
||||
}
|
||||
sellerPrivateKey, _ := btcec.NewPrivateKey()
|
||||
sellerPubkeyHex := hex.EncodeToString(sellerPrivateKey.PubKey().SerializeCompressed())
|
||||
sellerWallet := p.pubkeyToPkHashAddress(sellerPrivateKey.PubKey())
|
||||
sellerWallet := p.PubkeyToPkHashAddress(sellerPrivateKey.PubKey())
|
||||
startAt := time.Now().Add(time.Hour * -1)
|
||||
endAt := time.Now().Add(time.Hour * 1)
|
||||
deployMessage := &protobuf.NodeSaleEvent{
|
||||
@@ -87,7 +87,7 @@ func TestInvalidTimestamp(t *testing.T) {
|
||||
},
|
||||
}
|
||||
event, block := assembleTestEvent(sellerPrivateKey, "040404040404", "040404040404", 0, 0, deployMessage)
|
||||
p.processDeploy(ctx, qtx, block, event)
|
||||
p.ProcessDeploy(ctx, qtx, block, event)
|
||||
|
||||
buyerPrivateKey, _ := btcec.NewPrivateKey()
|
||||
buyerPubkeyHex := hex.EncodeToString(buyerPrivateKey.PubKey().SerializeCompressed())
|
||||
@@ -110,7 +110,7 @@ func TestInvalidTimestamp(t *testing.T) {
|
||||
|
||||
event, block = assembleTestEvent(buyerPrivateKey, "050505050505", "050505050505", 0, 0, message)
|
||||
block.Header.Timestamp = time.Now().UTC().Add(time.Hour * 2)
|
||||
p.processPurchase(ctx, qtx, block, event)
|
||||
p.ProcessPurchase(ctx, qtx, block, event)
|
||||
|
||||
nodes, _ := qtx.GetNodesByIds(ctx, datagateway.GetNodesByIdsParams{
|
||||
SaleBlock: testBlockHeight - 2,
|
||||
@@ -126,7 +126,7 @@ func TestInvalidBuyerKey(t *testing.T) {
|
||||
}
|
||||
sellerPrivateKey, _ := btcec.NewPrivateKey()
|
||||
sellerPubkeyHex := hex.EncodeToString(sellerPrivateKey.PubKey().SerializeCompressed())
|
||||
sellerWallet := p.pubkeyToPkHashAddress(sellerPrivateKey.PubKey())
|
||||
sellerWallet := p.PubkeyToPkHashAddress(sellerPrivateKey.PubKey())
|
||||
startAt := time.Now().Add(time.Hour * -1)
|
||||
endAt := time.Now().Add(time.Hour * 1)
|
||||
deployMessage := &protobuf.NodeSaleEvent{
|
||||
@@ -154,7 +154,7 @@ func TestInvalidBuyerKey(t *testing.T) {
|
||||
},
|
||||
}
|
||||
event, block := assembleTestEvent(sellerPrivateKey, "060606060606", "060606060606", 0, 0, deployMessage)
|
||||
p.processDeploy(ctx, qtx, block, event)
|
||||
p.ProcessDeploy(ctx, qtx, block, event)
|
||||
|
||||
buyerPrivateKey, _ := btcec.NewPrivateKey()
|
||||
|
||||
@@ -176,7 +176,7 @@ func TestInvalidBuyerKey(t *testing.T) {
|
||||
|
||||
event, block = assembleTestEvent(buyerPrivateKey, "0707070707", "0707070707", 0, 0, message)
|
||||
block.Header.Timestamp = time.Now().UTC().Add(time.Hour * 2)
|
||||
p.processPurchase(ctx, qtx, block, event)
|
||||
p.ProcessPurchase(ctx, qtx, block, event)
|
||||
nodes, _ := qtx.GetNodesByIds(ctx, datagateway.GetNodesByIdsParams{
|
||||
SaleBlock: testBlockHeight - 2,
|
||||
SaleTxIndex: int32(testTxIndex) - 2,
|
||||
@@ -191,7 +191,7 @@ func TestTimeOut(t *testing.T) {
|
||||
}
|
||||
sellerPrivateKey, _ := btcec.NewPrivateKey()
|
||||
sellerPubkeyHex := hex.EncodeToString(sellerPrivateKey.PubKey().SerializeCompressed())
|
||||
sellerWallet := p.pubkeyToPkHashAddress(sellerPrivateKey.PubKey())
|
||||
sellerWallet := p.PubkeyToPkHashAddress(sellerPrivateKey.PubKey())
|
||||
startAt := time.Now().Add(time.Hour * -1)
|
||||
endAt := time.Now().Add(time.Hour * 1)
|
||||
deployMessage := &protobuf.NodeSaleEvent{
|
||||
@@ -219,7 +219,7 @@ func TestTimeOut(t *testing.T) {
|
||||
},
|
||||
}
|
||||
event, block := assembleTestEvent(sellerPrivateKey, "0808080808", "0808080808", 0, 0, deployMessage)
|
||||
p.processDeploy(ctx, qtx, block, event)
|
||||
p.ProcessDeploy(ctx, qtx, block, event)
|
||||
|
||||
buyerPrivateKey, _ := btcec.NewPrivateKey()
|
||||
buyerPubkeyHex := hex.EncodeToString(buyerPrivateKey.PubKey().SerializeCompressed())
|
||||
@@ -241,7 +241,7 @@ func TestTimeOut(t *testing.T) {
|
||||
}
|
||||
|
||||
event, block = assembleTestEvent(buyerPrivateKey, "090909090909", "090909090909", 0, 0, message)
|
||||
p.processPurchase(ctx, qtx, block, event)
|
||||
p.ProcessPurchase(ctx, qtx, block, event)
|
||||
nodes, _ := qtx.GetNodesByIds(ctx, datagateway.GetNodesByIdsParams{
|
||||
SaleBlock: testBlockHeight - 2,
|
||||
SaleTxIndex: int32(testTxIndex) - 2,
|
||||
@@ -256,7 +256,7 @@ func TestSignatureInvalid(t *testing.T) {
|
||||
}
|
||||
sellerPrivateKey, _ := btcec.NewPrivateKey()
|
||||
sellerPubkeyHex := hex.EncodeToString(sellerPrivateKey.PubKey().SerializeCompressed())
|
||||
sellerWallet := p.pubkeyToPkHashAddress(sellerPrivateKey.PubKey())
|
||||
sellerWallet := p.PubkeyToPkHashAddress(sellerPrivateKey.PubKey())
|
||||
startAt := time.Now().Add(time.Hour * -1)
|
||||
endAt := time.Now().Add(time.Hour * 1)
|
||||
deployMessage := &protobuf.NodeSaleEvent{
|
||||
@@ -284,7 +284,7 @@ func TestSignatureInvalid(t *testing.T) {
|
||||
},
|
||||
}
|
||||
event, block := assembleTestEvent(sellerPrivateKey, "0A0A0A0A", "0A0A0A0A", 0, 0, deployMessage)
|
||||
p.processDeploy(ctx, qtx, block, event)
|
||||
p.ProcessDeploy(ctx, qtx, block, event)
|
||||
|
||||
buyerPrivateKey, _ := btcec.NewPrivateKey()
|
||||
buyerPubkeyHex := hex.EncodeToString(buyerPrivateKey.PubKey().SerializeCompressed())
|
||||
@@ -313,7 +313,7 @@ func TestSignatureInvalid(t *testing.T) {
|
||||
}
|
||||
|
||||
event, block = assembleTestEvent(buyerPrivateKey, "0B0B0B", "0B0B0B", 0, 0, message)
|
||||
p.processPurchase(ctx, qtx, block, event)
|
||||
p.ProcessPurchase(ctx, qtx, block, event)
|
||||
nodes, _ := qtx.GetNodesByIds(ctx, datagateway.GetNodesByIdsParams{
|
||||
SaleBlock: testBlockHeight - 2,
|
||||
SaleTxIndex: int32(testTxIndex) - 2,
|
||||
@@ -328,7 +328,7 @@ func TestValidPurchase(t *testing.T) {
|
||||
}
|
||||
sellerPrivateKey, _ := btcec.NewPrivateKey()
|
||||
sellerPubkeyHex := hex.EncodeToString(sellerPrivateKey.PubKey().SerializeCompressed())
|
||||
sellerWallet := p.pubkeyToPkHashAddress(sellerPrivateKey.PubKey())
|
||||
sellerWallet := p.PubkeyToPkHashAddress(sellerPrivateKey.PubKey())
|
||||
startAt := time.Now().Add(time.Hour * -1)
|
||||
endAt := time.Now().Add(time.Hour * 1)
|
||||
deployMessage := &protobuf.NodeSaleEvent{
|
||||
@@ -361,7 +361,7 @@ func TestValidPurchase(t *testing.T) {
|
||||
},
|
||||
}
|
||||
event, block := assembleTestEvent(sellerPrivateKey, "0C0C0C0C0C", "0C0C0C0C0C", 0, 0, deployMessage)
|
||||
p.processDeploy(ctx, qtx, block, event)
|
||||
p.ProcessDeploy(ctx, qtx, block, event)
|
||||
|
||||
buyerPrivateKey, _ := btcec.NewPrivateKey()
|
||||
buyerPubkeyHex := hex.EncodeToString(buyerPrivateKey.PubKey().SerializeCompressed())
|
||||
@@ -392,15 +392,15 @@ func TestValidPurchase(t *testing.T) {
|
||||
|
||||
event, block = assembleTestEvent(buyerPrivateKey, "0D0D0D0D", "0D0D0D0D", 0, 0, message)
|
||||
|
||||
addr, _ := btcutil.NewAddressPubKey(sellerPrivateKey.PubKey().SerializeCompressed(), p.network.ChainParams())
|
||||
addr, _ := btcutil.NewAddressPubKey(sellerPrivateKey.PubKey().SerializeCompressed(), p.Network.ChainParams())
|
||||
pkscript, _ := txscript.PayToAddrScript(addr.AddressPubKeyHash())
|
||||
event.transaction.TxOut = []*types.TxOut{
|
||||
event.Transaction.TxOut = []*types.TxOut{
|
||||
{
|
||||
PkScript: pkscript,
|
||||
Value: 500,
|
||||
},
|
||||
}
|
||||
p.processPurchase(ctx, qtx, block, event)
|
||||
p.ProcessPurchase(ctx, qtx, block, event)
|
||||
|
||||
nodes, _ := qtx.GetNodesByIds(ctx, datagateway.GetNodesByIdsParams{
|
||||
SaleBlock: testBlockHeight - 2,
|
||||
@@ -424,7 +424,7 @@ func TestBuyingLimit(t *testing.T) {
|
||||
}
|
||||
sellerPrivateKey, _ := btcec.NewPrivateKey()
|
||||
sellerPubkeyHex := hex.EncodeToString(sellerPrivateKey.PubKey().SerializeCompressed())
|
||||
sellerWallet := p.pubkeyToPkHashAddress(sellerPrivateKey.PubKey())
|
||||
sellerWallet := p.PubkeyToPkHashAddress(sellerPrivateKey.PubKey())
|
||||
startAt := time.Now().Add(time.Hour * -1)
|
||||
endAt := time.Now().Add(time.Hour * 1)
|
||||
deployMessage := &protobuf.NodeSaleEvent{
|
||||
@@ -457,7 +457,7 @@ func TestBuyingLimit(t *testing.T) {
|
||||
},
|
||||
}
|
||||
event, block := assembleTestEvent(sellerPrivateKey, "2121212121", "2121212121", 0, 0, deployMessage)
|
||||
p.processDeploy(ctx, qtx, block, event)
|
||||
p.ProcessDeploy(ctx, qtx, block, event)
|
||||
|
||||
buyerPrivateKey, _ := btcec.NewPrivateKey()
|
||||
buyerPubkeyHex := hex.EncodeToString(buyerPrivateKey.PubKey().SerializeCompressed())
|
||||
@@ -488,18 +488,18 @@ func TestBuyingLimit(t *testing.T) {
|
||||
|
||||
event, block = assembleTestEvent(buyerPrivateKey, "2020202020", "2020202020", 0, 0, message)
|
||||
|
||||
addr, _ := btcutil.NewAddressPubKey(sellerPrivateKey.PubKey().SerializeCompressed(), p.network.ChainParams())
|
||||
addr, _ := btcutil.NewAddressPubKey(sellerPrivateKey.PubKey().SerializeCompressed(), p.Network.ChainParams())
|
||||
pkscript, _ := txscript.PayToAddrScript(addr.AddressPubKeyHash())
|
||||
event.transaction.TxOut = []*types.TxOut{
|
||||
event.Transaction.TxOut = []*types.TxOut{
|
||||
{
|
||||
PkScript: pkscript,
|
||||
Value: 600,
|
||||
},
|
||||
}
|
||||
p.processPurchase(ctx, qtx, block, event)
|
||||
p.ProcessPurchase(ctx, qtx, block, event)
|
||||
|
||||
qtx.Commit(ctx)
|
||||
qtx, _ = p.nodeSaleDg.BeginNodeSaleTx(ctx)
|
||||
qtx, _ = p.NodeSaleDg.BeginNodeSaleTx(ctx)
|
||||
|
||||
payload = &protobuf.PurchasePayload{
|
||||
DeployID: &protobuf.ActionID{
|
||||
@@ -527,15 +527,15 @@ func TestBuyingLimit(t *testing.T) {
|
||||
|
||||
event, block = assembleTestEvent(buyerPrivateKey, "22222222", "22222222", 0, 0, message)
|
||||
|
||||
addr, _ = btcutil.NewAddressPubKey(sellerPrivateKey.PubKey().SerializeCompressed(), p.network.ChainParams())
|
||||
addr, _ = btcutil.NewAddressPubKey(sellerPrivateKey.PubKey().SerializeCompressed(), p.Network.ChainParams())
|
||||
pkscript, _ = txscript.PayToAddrScript(addr.AddressPubKeyHash())
|
||||
event.transaction.TxOut = []*types.TxOut{
|
||||
event.Transaction.TxOut = []*types.TxOut{
|
||||
{
|
||||
PkScript: pkscript,
|
||||
Value: 600,
|
||||
},
|
||||
}
|
||||
p.processPurchase(ctx, qtx, block, event)
|
||||
p.ProcessPurchase(ctx, qtx, block, event)
|
||||
|
||||
nodes, _ := qtx.GetNodesByIds(ctx, datagateway.GetNodesByIdsParams{
|
||||
SaleBlock: testBlockHeight - 3,
|
||||
@@ -557,7 +557,7 @@ func TestBuyingTierLimit(t *testing.T) {
|
||||
}
|
||||
sellerPrivateKey, _ := btcec.NewPrivateKey()
|
||||
sellerPubkeyHex := hex.EncodeToString(sellerPrivateKey.PubKey().SerializeCompressed())
|
||||
sellerWallet := p.pubkeyToPkHashAddress(sellerPrivateKey.PubKey())
|
||||
sellerWallet := p.PubkeyToPkHashAddress(sellerPrivateKey.PubKey())
|
||||
startAt := time.Now().Add(time.Hour * -1)
|
||||
endAt := time.Now().Add(time.Hour * 1)
|
||||
deployMessage := &protobuf.NodeSaleEvent{
|
||||
@@ -590,7 +590,7 @@ func TestBuyingTierLimit(t *testing.T) {
|
||||
},
|
||||
}
|
||||
event, block := assembleTestEvent(sellerPrivateKey, "0E0E0E0E", "0E0E0E0E", 0, 0, deployMessage)
|
||||
p.processDeploy(ctx, qtx, block, event)
|
||||
p.ProcessDeploy(ctx, qtx, block, event)
|
||||
|
||||
buyerPrivateKey, _ := btcec.NewPrivateKey()
|
||||
buyerPubkeyHex := hex.EncodeToString(buyerPrivateKey.PubKey().SerializeCompressed())
|
||||
@@ -621,18 +621,18 @@ func TestBuyingTierLimit(t *testing.T) {
|
||||
|
||||
event, block = assembleTestEvent(buyerPrivateKey, "0F0F0F0F0F", "0F0F0F0F0F", 0, 0, message)
|
||||
|
||||
addr, _ := btcutil.NewAddressPubKey(sellerPrivateKey.PubKey().SerializeCompressed(), p.network.ChainParams())
|
||||
addr, _ := btcutil.NewAddressPubKey(sellerPrivateKey.PubKey().SerializeCompressed(), p.Network.ChainParams())
|
||||
pkscript, _ := txscript.PayToAddrScript(addr.AddressPubKeyHash())
|
||||
event.transaction.TxOut = []*types.TxOut{
|
||||
event.Transaction.TxOut = []*types.TxOut{
|
||||
{
|
||||
PkScript: pkscript,
|
||||
Value: 600,
|
||||
},
|
||||
}
|
||||
p.processPurchase(ctx, qtx, block, event)
|
||||
p.ProcessPurchase(ctx, qtx, block, event)
|
||||
|
||||
qtx.Commit(ctx)
|
||||
qtx, _ = p.nodeSaleDg.BeginNodeSaleTx(ctx)
|
||||
qtx, _ = p.NodeSaleDg.BeginNodeSaleTx(ctx)
|
||||
|
||||
payload = &protobuf.PurchasePayload{
|
||||
DeployID: &protobuf.ActionID{
|
||||
@@ -660,15 +660,15 @@ func TestBuyingTierLimit(t *testing.T) {
|
||||
|
||||
event, block = assembleTestEvent(buyerPrivateKey, "10101010", "10101010", 0, 0, message)
|
||||
|
||||
addr, _ = btcutil.NewAddressPubKey(sellerPrivateKey.PubKey().SerializeCompressed(), p.network.ChainParams())
|
||||
addr, _ = btcutil.NewAddressPubKey(sellerPrivateKey.PubKey().SerializeCompressed(), p.Network.ChainParams())
|
||||
pkscript, _ = txscript.PayToAddrScript(addr.AddressPubKeyHash())
|
||||
event.transaction.TxOut = []*types.TxOut{
|
||||
event.Transaction.TxOut = []*types.TxOut{
|
||||
{
|
||||
PkScript: pkscript,
|
||||
Value: 600,
|
||||
},
|
||||
}
|
||||
p.processPurchase(ctx, qtx, block, event)
|
||||
p.ProcessPurchase(ctx, qtx, block, event)
|
||||
|
||||
nodes, _ := qtx.GetNodesByIds(ctx, datagateway.GetNodesByIdsParams{
|
||||
SaleBlock: testBlockHeight - 3,
|
||||
|
||||
Reference in New Issue
Block a user