bitxhub/internal/ledger/chain_meta.go

34 lines
586 B
Go
Raw Normal View History

2020-03-29 21:32:01 +08:00
package ledger
import (
"fmt"
2020-10-22 13:49:05 +08:00
"github.com/meshplus/bitxhub-kit/types"
"github.com/meshplus/bitxhub-kit/storage"
2020-03-29 21:32:01 +08:00
"github.com/meshplus/bitxhub-model/pb"
)
var (
chainKey = []byte("chain-meta")
)
func loadChainMeta(store storage.Storage) (*pb.ChainMeta, error) {
ok := store.Has(chainKey)
2020-03-29 21:32:01 +08:00
2020-10-22 13:49:05 +08:00
chain := &pb.ChainMeta{
Height: 0,
BlockHash: &types.Hash{},
InterchainTxCount: 0,
}
2020-03-29 21:32:01 +08:00
if ok {
body := store.Get(chainKey)
2020-03-29 21:32:01 +08:00
if err := chain.Unmarshal(body); err != nil {
return nil, fmt.Errorf("unmarshal chain meta: %w", err)
}
}
return chain, nil
}