This repository has been archived by the owner on Jan 13, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 77
/
publish_tx.go
96 lines (74 loc) · 2.04 KB
/
publish_tx.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package block
import (
"context"
"log"
d "github.com/itzmeanjan/ette/app/data"
"github.com/itzmeanjan/ette/app/db"
)
// PublishTxs - Publishes all transactions in a block to redis pubsub
// channel
func PublishTxs(blockNumber uint64, txs []*db.PackedTransaction, redis *d.RedisInfo) bool {
if txs == nil {
return false
}
var eventCount uint64
var status bool
for _, t := range txs {
status = PublishTx(blockNumber, t, redis)
if !status {
break
}
// how many events are present in this block, in total
eventCount += uint64(len(t.Events))
}
if !status {
return status
}
log.Printf("📎 Published %d transactions of block %d\n", len(txs), blockNumber)
log.Printf("📎 Published %d events of block %d\n", eventCount, blockNumber)
return status
}
// PublishTx - Publishes tx & events in tx, related data to respective
// Redis pubsub channel
func PublishTx(blockNumber uint64, tx *db.PackedTransaction, redis *d.RedisInfo) bool {
if tx == nil {
return false
}
var pTx *d.Transaction
if tx.Tx.To == "" {
// This is a contract creation tx
pTx = &d.Transaction{
Hash: tx.Tx.Hash,
From: tx.Tx.From,
Contract: tx.Tx.Contract,
Value: tx.Tx.Value,
Data: tx.Tx.Data,
Gas: tx.Tx.Gas,
GasPrice: tx.Tx.GasPrice,
Cost: tx.Tx.Cost,
Nonce: tx.Tx.Nonce,
State: tx.Tx.State,
BlockHash: tx.Tx.BlockHash,
}
} else {
// This is a normal tx, so we keep contract field empty
pTx = &d.Transaction{
Hash: tx.Tx.Hash,
From: tx.Tx.From,
To: tx.Tx.To,
Value: tx.Tx.Value,
Data: tx.Tx.Data,
Gas: tx.Tx.Gas,
GasPrice: tx.Tx.GasPrice,
Cost: tx.Tx.Cost,
Nonce: tx.Tx.Nonce,
State: tx.Tx.State,
BlockHash: tx.Tx.BlockHash,
}
}
if err := redis.Client.Publish(context.Background(), redis.TxPublishTopic, pTx).Err(); err != nil {
log.Printf("❗️ Failed to publish transaction from block %d : %s\n", blockNumber, err.Error())
return false
}
return PublishEvents(blockNumber, tx.Events, redis)
}