aboutsummaryrefslogtreecommitdiffstats
path: root/core/chain_util.go
diff options
context:
space:
mode:
Diffstat (limited to 'core/chain_util.go')
-rw-r--r--core/chain_util.go25
1 files changed, 25 insertions, 0 deletions
diff --git a/core/chain_util.go b/core/chain_util.go
index 7e3d8eba8..253396ed7 100644
--- a/core/chain_util.go
+++ b/core/chain_util.go
@@ -19,6 +19,7 @@ package core
import (
"bytes"
"math/big"
+ "time"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
@@ -28,6 +29,11 @@ import (
"github.com/ethereum/go-ethereum/rlp"
)
+var (
+ blockHashPre = []byte("block-hash-")
+ blockNumPre = []byte("block-num-")
+)
+
// CalcDifficulty is the difficulty adjustment algorithm. It returns
// the difficulty that a new block b should have when created at time
// given the parent block's time and difficulty.
@@ -118,3 +124,22 @@ func WriteHead(db common.Database, block *types.Block) error {
}
return nil
}
+
+// WriteBlock writes a block to the database
+func WriteBlock(db common.Database, block *types.Block) error {
+ tstart := time.Now()
+
+ enc, _ := rlp.EncodeToBytes((*types.StorageBlock)(block))
+ key := append(blockHashPre, block.Hash().Bytes()...)
+ err := db.Put(key, enc)
+ if err != nil {
+ glog.Fatal("db write fail:", err)
+ return err
+ }
+
+ if glog.V(logger.Debug) {
+ glog.Infof("wrote block #%v %s. Took %v\n", block.Number(), common.PP(block.Hash().Bytes()), time.Since(tstart))
+ }
+
+ return nil
+}