mirror of
https://github.com/alexgo-io/gaze-indexer.git
synced 2026-04-30 12:41:59 +08:00
fix: fix bug delegate
This commit is contained in:
@@ -14,8 +14,8 @@ func (p *Processor) processDelegate(ctx context.Context, qtx gen.Querier, block
|
||||
valid := true
|
||||
delegate := event.eventMessage.Delegate
|
||||
nodeIds := make([]int32, len(delegate.NodeIDs))
|
||||
for _, id := range delegate.NodeIDs {
|
||||
nodeIds = append(nodeIds, int32(id))
|
||||
for i, id := range delegate.NodeIDs {
|
||||
nodeIds[i] = int32(id)
|
||||
}
|
||||
nodes, err := qtx.GetNodes(ctx, gen.GetNodesParams{
|
||||
SaleBlock: int32(delegate.DeployID.Block),
|
||||
@@ -27,6 +27,8 @@ func (p *Processor) processDelegate(ctx context.Context, qtx gen.Querier, block
|
||||
}
|
||||
|
||||
if len(nodeIds) != len(nodes) {
|
||||
fmt.Println(nodeIds)
|
||||
fmt.Println(nodes)
|
||||
valid = false
|
||||
}
|
||||
|
||||
@@ -60,8 +62,8 @@ func (p *Processor) processDelegate(ctx context.Context, qtx gen.Querier, block
|
||||
}
|
||||
if valid {
|
||||
_, err = qtx.SetDelegates(ctx, gen.SetDelegatesParams{
|
||||
SaleBlock: int32(event.transaction.BlockHeight),
|
||||
SaleTxIndex: int32(event.transaction.Index),
|
||||
SaleBlock: int32(delegate.DeployID.Block),
|
||||
SaleTxIndex: int32(delegate.DeployID.TxIndex),
|
||||
Delegatee: pgtype.Text{
|
||||
String: delegate.DelegateePublicKey,
|
||||
Valid: true,
|
||||
|
||||
@@ -1 +1,108 @@
|
||||
package nodesale
|
||||
|
||||
import (
|
||||
"crypto/sha256"
|
||||
"encoding/hex"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/btcsuite/btcd/btcec/v2"
|
||||
"github.com/btcsuite/btcd/btcutil"
|
||||
"github.com/btcsuite/btcd/txscript"
|
||||
"github.com/decred/dcrd/dcrec/secp256k1/v4/ecdsa"
|
||||
"github.com/gaze-network/indexer-network/core/types"
|
||||
"github.com/gaze-network/indexer-network/modules/nodesale/protobuf"
|
||||
"google.golang.org/protobuf/proto"
|
||||
)
|
||||
|
||||
func TestDelegate(t *testing.T) {
|
||||
sellerPrivateKey, _ := btcec.NewPrivateKey()
|
||||
sellerPubkeyHex := hex.EncodeToString(sellerPrivateKey.PubKey().SerializeCompressed())
|
||||
startAt := time.Now().Add(time.Hour * -1)
|
||||
endAt := time.Now().Add(time.Hour * 1)
|
||||
deployMessage := &protobuf.NodeSaleEvent{
|
||||
Action: protobuf.Action_ACTION_DEPLOY,
|
||||
Deploy: &protobuf.ActionDeploy{
|
||||
Name: "deploy_for_puchase",
|
||||
StartsAt: uint32(startAt.UTC().Unix()),
|
||||
EndsAt: uint32(endAt.UTC().Unix()),
|
||||
Tiers: []*protobuf.Tier{
|
||||
{
|
||||
PriceSat: 100,
|
||||
Limit: 5,
|
||||
MaxPerAddress: 100,
|
||||
},
|
||||
{
|
||||
PriceSat: 200,
|
||||
Limit: 4,
|
||||
MaxPerAddress: 2,
|
||||
},
|
||||
{
|
||||
PriceSat: 400,
|
||||
Limit: 50,
|
||||
MaxPerAddress: 3,
|
||||
},
|
||||
},
|
||||
SellerPublicKey: sellerPubkeyHex,
|
||||
MaxPerAddress: 100,
|
||||
MaxDiscountPercentage: 50,
|
||||
},
|
||||
}
|
||||
event, block := assembleTestEvent(sellerPrivateKey, "111111", "111111", 0, 0, deployMessage)
|
||||
p.processDeploy(ctx, qtx, block, event)
|
||||
|
||||
buyerPrivateKey, _ := btcec.NewPrivateKey()
|
||||
buyerPubkeyHex := hex.EncodeToString(buyerPrivateKey.PubKey().SerializeCompressed())
|
||||
|
||||
payload := &protobuf.PurchasePayload{
|
||||
DeployID: &protobuf.ActionID{
|
||||
Block: uint64(testBlockHeigh) - 1,
|
||||
TxIndex: uint32(testTxIndex) - 1,
|
||||
},
|
||||
BuyerPublicKey: buyerPubkeyHex,
|
||||
TimeOutBlock: uint64(testBlockHeigh) + 5,
|
||||
NodeIDs: []uint32{9, 10, 11},
|
||||
TotalAmountSat: 600,
|
||||
}
|
||||
|
||||
payloadBytes, _ := proto.Marshal(payload)
|
||||
payloadHash := sha256.Sum256(payloadBytes)
|
||||
signature := ecdsa.Sign(sellerPrivateKey, payloadHash[:])
|
||||
signatureHex := hex.EncodeToString(signature.Serialize())
|
||||
|
||||
message := &protobuf.NodeSaleEvent{
|
||||
Action: protobuf.Action_ACTION_PURCHASE,
|
||||
Purchase: &protobuf.ActionPurchase{
|
||||
Payload: payload,
|
||||
SellerSignature: signatureHex,
|
||||
},
|
||||
}
|
||||
|
||||
event, block = assembleTestEvent(buyerPrivateKey, "1212121212", "1212121212", 0, 0, message)
|
||||
|
||||
addr, _ := btcutil.NewAddressPubKey(sellerPrivateKey.PubKey().SerializeCompressed(), p.network.ChainParams())
|
||||
pkscript, _ := txscript.PayToAddrScript(addr.AddressPubKeyHash())
|
||||
event.transaction.TxOut = []*types.TxOut{
|
||||
{
|
||||
PkScript: pkscript,
|
||||
Value: 600,
|
||||
},
|
||||
}
|
||||
p.processPurchase(ctx, qtx, block, event)
|
||||
|
||||
delegateePrivateKey, _ := btcec.NewPrivateKey()
|
||||
delegateePubkeyHex := hex.EncodeToString(delegateePrivateKey.PubKey().SerializeCompressed())
|
||||
delegateMessage := &protobuf.NodeSaleEvent{
|
||||
Action: protobuf.Action_ACTION_DELEGATE,
|
||||
Delegate: &protobuf.ActionDelegate{
|
||||
DelegateePublicKey: delegateePubkeyHex,
|
||||
NodeIDs: []uint32{9, 10},
|
||||
DeployID: &protobuf.ActionID{
|
||||
Block: uint64(testBlockHeigh) - 2,
|
||||
TxIndex: uint32(testTxIndex) - 2,
|
||||
},
|
||||
},
|
||||
}
|
||||
event, block = assembleTestEvent(buyerPrivateKey, "131313131313", "131313131313", 0, 0, delegateMessage)
|
||||
p.processDelegate(ctx, qtx, block, event)
|
||||
}
|
||||
|
||||
@@ -1,70 +1,13 @@
|
||||
package nodesale
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/hex"
|
||||
"os"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/btcsuite/btcd/btcec/v2"
|
||||
"github.com/btcsuite/btcd/btcec/v2/schnorr"
|
||||
"github.com/btcsuite/btcd/btcutil"
|
||||
"github.com/btcsuite/btcd/chaincfg/chainhash"
|
||||
"github.com/btcsuite/btcd/txscript"
|
||||
"github.com/decred/dcrd/dcrec/secp256k1/v4"
|
||||
"github.com/gaze-network/indexer-network/common"
|
||||
"github.com/gaze-network/indexer-network/core/types"
|
||||
"github.com/gaze-network/indexer-network/internal/postgres"
|
||||
"github.com/gaze-network/indexer-network/modules/nodesale/protobuf"
|
||||
repository "github.com/gaze-network/indexer-network/modules/nodesale/repository/postgres"
|
||||
"github.com/gaze-network/indexer-network/modules/nodesale/repository/postgres/gen"
|
||||
"github.com/jackc/pgx/v5"
|
||||
"google.golang.org/protobuf/encoding/protojson"
|
||||
"google.golang.org/protobuf/proto"
|
||||
)
|
||||
|
||||
var p *Processor
|
||||
|
||||
var postgresConf postgres.Config = postgres.Config{
|
||||
User: "postgres",
|
||||
Password: "P@ssw0rd",
|
||||
}
|
||||
|
||||
var qtx gen.Querier
|
||||
|
||||
var ctx context.Context
|
||||
|
||||
var tx pgx.Tx
|
||||
|
||||
var (
|
||||
testBlockHeigh int = 101
|
||||
testTxIndex int = 1
|
||||
)
|
||||
|
||||
func TestMain(m *testing.M) {
|
||||
// call flag.Parse() here if TestMain uses flags
|
||||
|
||||
ctx = context.Background()
|
||||
|
||||
db, _ := postgres.NewPool(ctx, postgresConf)
|
||||
|
||||
repo := repository.NewRepository(db)
|
||||
|
||||
p = &Processor{
|
||||
repository: repo,
|
||||
network: common.NetworkMainnet,
|
||||
}
|
||||
repo.Queries.ClearEvents(ctx)
|
||||
|
||||
tx, _ = p.repository.Db.Begin(ctx)
|
||||
qtx = p.repository.WithTx(tx)
|
||||
|
||||
res := m.Run()
|
||||
tx.Commit(ctx)
|
||||
os.Exit(res)
|
||||
}
|
||||
|
||||
func TestDeployInvalid(t *testing.T) {
|
||||
prvKey, _ := btcec.NewPrivateKey()
|
||||
message := &protobuf.NodeSaleEvent{
|
||||
@@ -94,57 +37,6 @@ func TestDeployInvalid(t *testing.T) {
|
||||
p.processDeploy(ctx, qtx, block, event)
|
||||
}
|
||||
|
||||
func assembleTestEvent(privateKey *secp256k1.PrivateKey, blockHashHex, txHashHex string, blockHeight, txIndex int, message *protobuf.NodeSaleEvent) (nodesaleEvent, *types.Block) {
|
||||
blockHash, _ := chainhash.NewHashFromStr(blockHashHex)
|
||||
txHash, _ := chainhash.NewHashFromStr(txHashHex)
|
||||
|
||||
rawData, _ := proto.Marshal(message)
|
||||
|
||||
builder := txscript.NewScriptBuilder()
|
||||
builder.AddOp(txscript.OP_FALSE)
|
||||
builder.AddOp(txscript.OP_IF)
|
||||
builder.AddData(rawData)
|
||||
builder.AddOp(txscript.OP_ENDIF)
|
||||
script, _ := builder.Script()
|
||||
tapleaf := txscript.NewBaseTapLeaf(script)
|
||||
scriptTree := txscript.AssembleTaprootScriptTree(tapleaf)
|
||||
rootHash := scriptTree.RootNode.TapHash()
|
||||
tapkey := txscript.ComputeTaprootOutputKey(privateKey.PubKey(), rootHash[:])
|
||||
|
||||
addressTaproot, _ := btcutil.NewAddressTaproot(schnorr.SerializePubKey(tapkey), p.network.ChainParams())
|
||||
|
||||
messageJson, _ := protojson.Marshal(message)
|
||||
|
||||
if blockHeight == 0 {
|
||||
blockHeight = testBlockHeigh
|
||||
testBlockHeigh++
|
||||
}
|
||||
if txIndex == 0 {
|
||||
txIndex = testTxIndex
|
||||
testTxIndex++
|
||||
}
|
||||
|
||||
event := nodesaleEvent{
|
||||
transaction: &types.Transaction{
|
||||
BlockHeight: int64(blockHeight),
|
||||
BlockHash: *blockHash,
|
||||
Index: uint32(txIndex),
|
||||
TxHash: *txHash,
|
||||
},
|
||||
rawData: rawData,
|
||||
eventMessage: message,
|
||||
eventJson: messageJson,
|
||||
txAddress: addressTaproot,
|
||||
rawScript: script,
|
||||
}
|
||||
block := &types.Block{
|
||||
Header: types.BlockHeader{
|
||||
Timestamp: time.Now().UTC(),
|
||||
},
|
||||
}
|
||||
return event, block
|
||||
}
|
||||
|
||||
func TestDeployValid(t *testing.T) {
|
||||
privateKey, _ := btcec.NewPrivateKey()
|
||||
pubkeyHex := hex.EncodeToString(privateKey.PubKey().SerializeCompressed())
|
||||
|
||||
115
modules/nodesale/nodesale_test.go
Normal file
115
modules/nodesale/nodesale_test.go
Normal file
@@ -0,0 +1,115 @@
|
||||
package nodesale
|
||||
|
||||
import (
|
||||
"context"
|
||||
"os"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/btcsuite/btcd/btcec/v2/schnorr"
|
||||
"github.com/btcsuite/btcd/btcutil"
|
||||
"github.com/btcsuite/btcd/chaincfg/chainhash"
|
||||
"github.com/btcsuite/btcd/txscript"
|
||||
"github.com/decred/dcrd/dcrec/secp256k1/v4"
|
||||
"github.com/gaze-network/indexer-network/common"
|
||||
"github.com/gaze-network/indexer-network/core/types"
|
||||
"github.com/gaze-network/indexer-network/internal/postgres"
|
||||
"github.com/gaze-network/indexer-network/modules/nodesale/protobuf"
|
||||
repository "github.com/gaze-network/indexer-network/modules/nodesale/repository/postgres"
|
||||
"github.com/gaze-network/indexer-network/modules/nodesale/repository/postgres/gen"
|
||||
"github.com/jackc/pgx/v5"
|
||||
"google.golang.org/protobuf/encoding/protojson"
|
||||
"google.golang.org/protobuf/proto"
|
||||
)
|
||||
|
||||
var p *Processor
|
||||
|
||||
var postgresConf postgres.Config = postgres.Config{
|
||||
User: "postgres",
|
||||
Password: "P@ssw0rd",
|
||||
}
|
||||
|
||||
var qtx gen.Querier
|
||||
|
||||
var ctx context.Context
|
||||
|
||||
var tx pgx.Tx
|
||||
|
||||
var (
|
||||
testBlockHeigh int = 101
|
||||
testTxIndex int = 1
|
||||
)
|
||||
|
||||
func TestMain(m *testing.M) {
|
||||
// call flag.Parse() here if TestMain uses flags
|
||||
|
||||
ctx = context.Background()
|
||||
|
||||
db, _ := postgres.NewPool(ctx, postgresConf)
|
||||
|
||||
repo := repository.NewRepository(db)
|
||||
|
||||
p = &Processor{
|
||||
repository: repo,
|
||||
network: common.NetworkMainnet,
|
||||
}
|
||||
repo.Queries.ClearEvents(ctx)
|
||||
|
||||
tx, _ = p.repository.Db.Begin(ctx)
|
||||
qtx = p.repository.WithTx(tx)
|
||||
|
||||
res := m.Run()
|
||||
tx.Commit(ctx)
|
||||
os.Exit(res)
|
||||
}
|
||||
|
||||
func assembleTestEvent(privateKey *secp256k1.PrivateKey, blockHashHex, txHashHex string, blockHeight, txIndex int, message *protobuf.NodeSaleEvent) (nodesaleEvent, *types.Block) {
|
||||
blockHash, _ := chainhash.NewHashFromStr(blockHashHex)
|
||||
txHash, _ := chainhash.NewHashFromStr(txHashHex)
|
||||
|
||||
rawData, _ := proto.Marshal(message)
|
||||
|
||||
builder := txscript.NewScriptBuilder()
|
||||
builder.AddOp(txscript.OP_FALSE)
|
||||
builder.AddOp(txscript.OP_IF)
|
||||
builder.AddData(rawData)
|
||||
builder.AddOp(txscript.OP_ENDIF)
|
||||
script, _ := builder.Script()
|
||||
tapleaf := txscript.NewBaseTapLeaf(script)
|
||||
scriptTree := txscript.AssembleTaprootScriptTree(tapleaf)
|
||||
rootHash := scriptTree.RootNode.TapHash()
|
||||
tapkey := txscript.ComputeTaprootOutputKey(privateKey.PubKey(), rootHash[:])
|
||||
|
||||
addressTaproot, _ := btcutil.NewAddressTaproot(schnorr.SerializePubKey(tapkey), p.network.ChainParams())
|
||||
|
||||
messageJson, _ := protojson.Marshal(message)
|
||||
|
||||
if blockHeight == 0 {
|
||||
blockHeight = testBlockHeigh
|
||||
testBlockHeigh++
|
||||
}
|
||||
if txIndex == 0 {
|
||||
txIndex = testTxIndex
|
||||
testTxIndex++
|
||||
}
|
||||
|
||||
event := nodesaleEvent{
|
||||
transaction: &types.Transaction{
|
||||
BlockHeight: int64(blockHeight),
|
||||
BlockHash: *blockHash,
|
||||
Index: uint32(txIndex),
|
||||
TxHash: *txHash,
|
||||
},
|
||||
rawData: rawData,
|
||||
eventMessage: message,
|
||||
eventJson: messageJson,
|
||||
txAddress: addressTaproot,
|
||||
rawScript: script,
|
||||
}
|
||||
block := &types.Block{
|
||||
Header: types.BlockHeader{
|
||||
Timestamp: time.Now().UTC(),
|
||||
},
|
||||
}
|
||||
return event, block
|
||||
}
|
||||
Reference in New Issue
Block a user