forked from decred/dcrdata
-
Notifications
You must be signed in to change notification settings - Fork 0
/
conversion.go
56 lines (51 loc) · 1.86 KB
/
conversion.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
package dbtypes
import (
"github.com/dcrdata/dcrdata/txhelpers"
"github.com/decred/dcrd/chaincfg"
"github.com/decred/dcrd/wire"
)
// MsgBlockToDBBlock creates a dbtypes.Block from a wire.MsgBlock
func MsgBlockToDBBlock(msgBlock *wire.MsgBlock, chainParams *chaincfg.Params) *Block {
// Create the dbtypes.Block structure
blockHeader := msgBlock.Header
// convert each transaction hash to a hex string
var txHashStrs []string
txHashes := msgBlock.TxHashes()
for i := range txHashes {
txHashStrs = append(txHashStrs, txHashes[i].String())
}
var stxHashStrs []string
stxHashes := msgBlock.STxHashes()
for i := range stxHashes {
stxHashStrs = append(stxHashStrs, stxHashes[i].String())
}
// Assemble the block
return &Block{
Hash: blockHeader.BlockHash().String(),
Size: uint32(msgBlock.SerializeSize()),
Height: blockHeader.Height,
Version: uint32(blockHeader.Version),
MerkleRoot: blockHeader.MerkleRoot.String(),
StakeRoot: blockHeader.StakeRoot.String(),
NumTx: uint32(len(msgBlock.Transactions) + len(msgBlock.STransactions)),
// nil []int64 for TxDbIDs
NumRegTx: uint32(len(msgBlock.Transactions)),
Tx: txHashStrs,
NumStakeTx: uint32(len(msgBlock.STransactions)),
STx: stxHashStrs,
Time: uint64(blockHeader.Timestamp.Unix()),
Nonce: uint64(blockHeader.Nonce),
VoteBits: blockHeader.VoteBits,
FinalState: blockHeader.FinalState[:],
Voters: blockHeader.Voters,
FreshStake: blockHeader.FreshStake,
Revocations: blockHeader.Revocations,
PoolSize: blockHeader.PoolSize,
Bits: blockHeader.Bits,
SBits: uint64(blockHeader.SBits),
Difficulty: txhelpers.GetDifficultyRatio(blockHeader.Bits, chainParams),
ExtraData: blockHeader.ExtraData[:],
StakeVersion: blockHeader.StakeVersion,
PreviousHash: blockHeader.PrevBlock.String(),
}
}