forked from rustyrussell/bitcoin-iterate
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtypes.h
160 lines (140 loc) · 3.63 KB
/
types.h
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
159
160
/*******************************************************************************
*
* = types.h
*
* Defines blockchain data structures.
*
*/
#ifndef BITCOIN_PARSE_TYPES_H
#define BITCOIN_PARSE_TYPES_H
#include <ccan/tal/tal.h>
#include <ccan/short_types/short_types.h>
#include <openssl/sha.h>
#include <sys/types.h>
/* We unpack varints for our in-memory representation */
#define varint_t u64
/**
* block_header - Block header
* @D9B4BEF9: Netmarker
* @len: Length of block in bytes
* @version: Bitcoin protocol version
* @prev_hash: Hash of previous (parent) block
* @merkle_hash: Hash of Merkle root for block's chain
* @timestamp: UNIX timestamp of block
* @target: Difficulty for block
* @nonce: Which solved block
* @transaction_count: Number of transactions in block
*
* Block headers are stored little-endian on disk.
*
*/
struct block_header {
u32 D9B4BEF9;
u32 len;
u32 version;
u8 prev_hash[32];
u8 merkle_hash[32];
u32 timestamp;
u32 target;
u32 nonce;
varint_t transaction_count;
};
/**
* transaction - A single bitcoin transaction
* @version: Bitcoin protocol version
* @input_count: Number of inputs
* @input: Array of inputs
* @output_count: Number of outputs
* @output: Array of outputs
* @lock_time: Lock time for this transaction
* @sha256: Hash for this transaction
* @len: Length of this transaction in bytes
*
*/
struct transaction {
u32 version;
varint_t input_count;
struct input *input;
varint_t output_count;
struct output *output;
u32 lock_time;
/* We calculate these as we read in transaction: */
u8 sha256[SHA256_DIGEST_LENGTH];
u32 len;
};
/**
* output - A single output from a particular bitcoin transaction
* @amount: The number of Satoshis for this output
* @script_length: The length of this output's script
* @script: This output's script
*
*/
struct output {
u64 amount;
varint_t script_length;
u8 *script;
};
/**
* input - A single input from a particular bitcoin transaction
* @hash: The hash of the transaction containing the output this input is spending
* @index: The index of the output in the transaction this input is spending
* @script_length: The length of this input's script
* @script: This input's script
* @sequence_number: This input's sequence number (not currently used)
*
*/
struct input {
u8 hash[SHA256_DIGEST_LENGTH];
u32 index; /* output number referred to by above */
varint_t script_length;
u8 *script;
u32 sequence_number;
};
struct block {
u8 sha[SHA256_DIGEST_LENGTH];
s32 height; /* -1 for not-yet-known */
/* Where is it */
unsigned int filenum;
/* Position of first transaction */
off_t pos;
/* So we can iterate forwards. */
struct block *next;
/* Bitcoin block header. */
struct block_header bh;
};
struct utxo {
/* txid */
u8 tx[SHA256_DIGEST_LENGTH];
/* Timestamp. */
u32 timestamp;
/* Height. */
unsigned int height;
/* Number of outputs. */
u32 num_outputs;
/* Reference count for this tx. */
u32 unspent_outputs;
/* Total amount unspent. */
u64 unspent;
/* Total amount spent. */
u64 spent;
/* Amount for each output. */
u64 amount[];
/* Followed by a char per output for UNKNOWN/PAYMENT/CHANGE */
};
#define OP_PUSHDATA1 0x4C
#define OP_PUSHDATA2 0x4D
#define OP_PUSHDATA4 0x4E
#define OP_NOP 0x61
#define OP_RETURN 0x6A
#define OP_DUP 0x76
#define OP_EQUALVERIFY 0x88
#define OP_CHECKSIG 0xAC
#define OP_HASH160 0xA9
#define UNKNOWN_OUTPUT 0
#define PAYMENT_OUTPUT 1
#define CHANGE_OUTPUT 2
struct block *new_block();
struct block_header *new_block_header();
void free_block(const tal_t *block);
void free_block_header(const tal_t *block_header);
#endif /* BITCOIN_PARSE_TYPES_H */