-
Notifications
You must be signed in to change notification settings - Fork 6
/
blockchain.go
41 lines (35 loc) · 890 Bytes
/
blockchain.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
package fraudproofs
import (
"github.com/lazyledger/smt"
"crypto/sha512"
)
// Blockchain is a simple blockchain.
type Blockchain struct {
// data structure
length int
last *Block
// implementation specific
stateTree *smt.SparseMerkleTree // sparse Merkle tree storing key-values of the transactions
}
// NewBlockchain creates an empty blockchain.
func NewBlockchain() *Blockchain {
return &Blockchain{0,nil, smt.NewSparseMerkleTree(smt.NewSimpleMap(), sha512.New512_256())}
}
// Append appends a block to the blockchain or returns a fraud proof if the block is not constructed correctly.
func (bc *Blockchain) Append(b *Block) (*FraudProof, error) {
fp, err := b.CheckBlock(bc.stateTree)
if err != nil {
return nil, err
}
if fp != nil {
return fp, nil
}
if bc.length == 0 {
bc.last = b
} else {
b.prev = bc.last
bc.last = b
}
bc.length++
return nil, nil
}