2019-07-26 04:54:22 +00:00
|
|
|
package types
|
|
|
|
|
|
|
|
import (
|
2019-08-16 00:17:09 +00:00
|
|
|
"bytes"
|
2019-07-26 04:54:22 +00:00
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2019-10-01 18:47:42 +00:00
|
|
|
"sort"
|
2019-07-26 04:54:22 +00:00
|
|
|
|
|
|
|
"github.com/ipfs/go-cid"
|
2019-08-21 23:24:59 +00:00
|
|
|
logging "github.com/ipfs/go-log"
|
2019-07-26 04:54:22 +00:00
|
|
|
)
|
|
|
|
|
2019-08-21 23:24:59 +00:00
|
|
|
var log = logging.Logger("types")
|
|
|
|
|
2019-07-26 04:54:22 +00:00
|
|
|
type TipSet struct {
|
|
|
|
cids []cid.Cid
|
|
|
|
blks []*BlockHeader
|
|
|
|
height uint64
|
|
|
|
}
|
|
|
|
|
|
|
|
// why didnt i just export the fields? Because the struct has methods with the
|
|
|
|
// same names already
|
|
|
|
type expTipSet struct {
|
|
|
|
Cids []cid.Cid
|
|
|
|
Blocks []*BlockHeader
|
|
|
|
Height uint64
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ts *TipSet) MarshalJSON() ([]byte, error) {
|
|
|
|
return json.Marshal(expTipSet{
|
|
|
|
Cids: ts.cids,
|
|
|
|
Blocks: ts.blks,
|
|
|
|
Height: ts.height,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ts *TipSet) UnmarshalJSON(b []byte) error {
|
|
|
|
var ets expTipSet
|
|
|
|
if err := json.Unmarshal(b, &ets); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-10-01 18:47:42 +00:00
|
|
|
ots, err := NewTipSet(ets.Blocks)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
*ts = *ots
|
|
|
|
|
2019-07-26 04:54:22 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-10-01 18:47:42 +00:00
|
|
|
func tipsetSortFunc(blks []*BlockHeader) func(i, j int) bool {
|
|
|
|
return func(i, j int) bool {
|
|
|
|
ti := blks[i].LastTicket()
|
|
|
|
tj := blks[j].LastTicket()
|
|
|
|
|
|
|
|
if ti.Equals(tj) {
|
2019-10-06 03:32:56 +00:00
|
|
|
//log.Warn("blocks have same ticket")
|
2019-10-01 18:47:42 +00:00
|
|
|
return blks[i].Cid().KeyString() < blks[j].Cid().KeyString()
|
|
|
|
}
|
|
|
|
|
|
|
|
return ti.Less(tj)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-26 04:54:22 +00:00
|
|
|
func NewTipSet(blks []*BlockHeader) (*TipSet, error) {
|
2019-10-01 18:47:42 +00:00
|
|
|
sort.Slice(blks, tipsetSortFunc(blks))
|
|
|
|
|
2019-07-26 04:54:22 +00:00
|
|
|
var ts TipSet
|
|
|
|
ts.cids = []cid.Cid{blks[0].Cid()}
|
|
|
|
ts.blks = blks
|
|
|
|
for _, b := range blks[1:] {
|
|
|
|
if b.Height != blks[0].Height {
|
|
|
|
return nil, fmt.Errorf("cannot create tipset with mismatching heights")
|
|
|
|
}
|
|
|
|
ts.cids = append(ts.cids, b.Cid())
|
|
|
|
}
|
|
|
|
ts.height = blks[0].Height
|
|
|
|
|
|
|
|
return &ts, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ts *TipSet) Cids() []cid.Cid {
|
|
|
|
return ts.cids
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ts *TipSet) Height() uint64 {
|
|
|
|
return ts.height
|
|
|
|
}
|
|
|
|
|
2019-08-21 23:24:59 +00:00
|
|
|
func (ts *TipSet) Weight() BigInt {
|
|
|
|
// TODO: implement correctly
|
|
|
|
log.Warn("Called TipSet.Weight: TODO: correct implementation")
|
|
|
|
return BigAdd(ts.blks[0].ParentWeight, NewInt(1))
|
2019-07-26 04:54:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (ts *TipSet) Parents() []cid.Cid {
|
|
|
|
return ts.blks[0].Parents
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ts *TipSet) Blocks() []*BlockHeader {
|
|
|
|
return ts.blks
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ts *TipSet) Equals(ots *TipSet) bool {
|
|
|
|
if len(ts.blks) != len(ots.blks) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, b := range ts.blks {
|
|
|
|
if b.Cid() != ots.blks[i].Cid() {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
2019-08-16 00:17:09 +00:00
|
|
|
|
2019-09-06 06:26:02 +00:00
|
|
|
func (t *Ticket) Less(o *Ticket) bool {
|
|
|
|
return bytes.Compare(t.VDFResult, o.VDFResult) < 0
|
|
|
|
}
|
2019-08-16 00:17:09 +00:00
|
|
|
|
2019-09-06 06:26:02 +00:00
|
|
|
func (ts *TipSet) MinTicket() *Ticket {
|
|
|
|
b := ts.MinTicketBlock()
|
|
|
|
return b.Tickets[len(b.Tickets)-1]
|
2019-08-16 00:17:09 +00:00
|
|
|
}
|
2019-08-30 02:59:54 +00:00
|
|
|
|
|
|
|
func (ts *TipSet) MinTimestamp() uint64 {
|
|
|
|
minTs := ts.Blocks()[0].Timestamp
|
|
|
|
for _, bh := range ts.Blocks()[1:] {
|
|
|
|
if bh.Timestamp < minTs {
|
|
|
|
minTs = bh.Timestamp
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return minTs
|
|
|
|
}
|
2019-09-06 06:26:02 +00:00
|
|
|
|
|
|
|
func (ts *TipSet) MinTicketBlock() *BlockHeader {
|
|
|
|
blks := ts.Blocks()
|
|
|
|
|
|
|
|
min := blks[0]
|
|
|
|
|
|
|
|
for _, b := range blks[1:] {
|
|
|
|
if b.LastTicket().Less(min.LastTicket()) {
|
|
|
|
min = b
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return min
|
|
|
|
}
|
2019-09-18 02:50:03 +00:00
|
|
|
|
2019-10-02 20:03:27 +00:00
|
|
|
func (ts *TipSet) ParentState() cid.Cid {
|
|
|
|
return ts.blks[0].ParentStateRoot
|
|
|
|
}
|
|
|
|
|
2019-09-18 02:50:03 +00:00
|
|
|
func (ts *TipSet) Contains(oc cid.Cid) bool {
|
|
|
|
for _, c := range ts.cids {
|
|
|
|
if c == oc {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|