-
Notifications
You must be signed in to change notification settings - Fork 27
/
block_p2p.go
69 lines (56 loc) · 1.92 KB
/
block_p2p.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
package p2p
import (
"bytes"
"encoding/gob"
"fmt"
log "github.com/sirupsen/logrus"
"github.com/codingpeasant/blocace/blockchain"
)
// BlockP2P represents a block from a peer
type BlockP2P struct {
PeerId []byte
Timestamp int64
PrevBlockHash []byte
Height uint64
Hash []byte
IsTip bool
TotalTransactions int
Transactions []blockchain.Transaction
}
// Marshal serializes BlockP2P
func (b BlockP2P) Marshal() []byte {
var result bytes.Buffer
encoder := gob.NewEncoder(&result)
err := encoder.Encode(b)
if err != nil {
log.Error(err)
}
return result.Bytes()
}
// MapToBlock verified each transaction in the BlockP2P on wire and convert it to blockchain.Block
func (b BlockP2P) MapToBlock() (*blockchain.Block, error) {
var transactions []*blockchain.Transaction
// cannot use range here because the memory address for elements being iterated is always the same one
for i := 0; i < len(b.Transactions); i++ {
if bytes.Compare(b.Transactions[i].Signature, []byte{}) == 0 { // skipping for genisis transation and bulk loading
transactions = append(transactions, &b.Transactions[i])
} else if blockchain.IsValidSig(b.Transactions[i].RawData, b.Transactions[i].PubKey, b.Transactions[i].Signature) {
transactions = append(transactions, &b.Transactions[i])
} else {
transactions = nil
return nil, fmt.Errorf("transaction signature verification failed, abandon this block")
}
}
return &blockchain.Block{Timestamp: b.Timestamp, PrevBlockHash: b.PrevBlockHash,
Height: b.Height, Hash: b.Hash, TotalTransactions: b.TotalTransactions, Transactions: transactions}, nil
}
// unmarshalBlockP2P deserializes encoded bytes to BlockP2P object
func unmarshalBlockP2P(b []byte) (BlockP2P, error) {
var blockP2P BlockP2P
decoder := gob.NewDecoder(bytes.NewReader(b))
err := decoder.Decode(&blockP2P)
if err != nil {
log.Error(err)
}
return blockP2P, err
}