-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathblock.go
158 lines (139 loc) · 3.25 KB
/
block.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
package ethereum
import (
"fmt"
"math/big"
"strings"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
)
// TransactionStatus is receipt status of transaction.
type TransactionStatus uint
const (
// TransactionFailed is the status code of a transaction if execution failed.
TransactionFailed = TransactionStatus(0)
// TransactionSuccessful is the status code of a transaction if execution succeeded.
TransactionSuccessful = TransactionStatus(1)
)
// Block holds information about Ethereum block.
type Block struct {
Difficulty *big.Int
ExtraData []byte
GasLimit *big.Int
GasUsed *big.Int
Hash common.Hash
Miner common.Address
Number *big.Int
Timestamp uint64
Transactions Transactions
}
func (b *Block) String() string {
str := fmt.Sprintf(`Block(#%v): {
Difficulty: %v
ExtraData: %s
GasLimit: %v
GasUsed: %v
Hash: %x
Miner: %x
Timestamp: %v
Transactions:
%v
}
`, b.Number, b.Difficulty, b.ExtraData, b.GasLimit, b.GasUsed, b.Hash[:], b.Miner[:], b.Timestamp, b.Transactions)
return str
}
// Transactions slice type.
type Transactions []*Transaction
// Transaction holds information about Ethereum transaction.
type Transaction struct {
BlockNumber *big.Int
From common.Address
GasLimit *big.Int
GasPrice *big.Int
GasUsed *big.Int
Hash common.Hash
Input []byte
Nonce uint64
To *common.Address // nil means contract creation
TransactionIndex uint64
Value *big.Int
ContractAddress *common.Address
Status *TransactionStatus
Logs []*types.Log
}
func (t *Transaction) String() string {
var to string
if t.To == nil {
to = "[contract creation]"
} else {
to = fmt.Sprintf("%x", t.To[:])
}
var status string
if t.Status == nil {
status = "[unknown status]"
} else {
if *t.Status == TransactionSuccessful {
status = "successful"
} else {
status = "failed"
}
}
return fmt.Sprintf(`
TX(%s)
BlockNumber: %#v
TxIndex: %v
Contract: %v
ContractAddress: %v
From: %x
To: %s
GasPrice: %#v
GasLimit: %#v
GasUsed: %#v
Input: %x
Nonce: %v
Value: %#v
Status: %v
Logs: %v
`,
t.Hash.String(),
t.BlockNumber,
t.TransactionIndex,
t.To == nil,
t.ContractAddress,
t.From[:],
to,
t.GasPrice,
t.GasLimit,
t.GasUsed,
t.Input,
t.Nonce,
t.Value,
status,
logsToString(" ", t.Logs),
)
}
func logsToString(ident string, ls []*types.Log) string {
sb := strings.Builder{}
sb.WriteString("[")
anyLogs := false
for _, l := range ls {
anyLogs = true
sb.WriteString("\n")
sb.WriteString(ident)
sb.WriteString(ident)
sb.WriteString(fmt.Sprintf("Address: %v\n", l.Address.Hex()))
sb.WriteString(ident)
sb.WriteString(ident)
sb.WriteString(fmt.Sprintf("Topics: %x\n", l.Topics))
sb.WriteString(ident)
sb.WriteString(ident)
sb.WriteString(fmt.Sprintf("Data: %x\n", l.Data))
sb.WriteString(ident)
sb.WriteString(ident)
sb.WriteString(fmt.Sprintf("Removed: %v\n", l.Removed))
}
if anyLogs {
sb.WriteString(ident)
}
sb.WriteString("]")
return sb.String()
}