func (db *T) PutBlock(b *block.Block, last bool) (e error) { writable(&e, db, func(dbtx *bolt.Tx) bool { bucket, e := dbtx.CreateBucketIfNotExists([]byte("blocks")) if e != nil { return false } encoded, e := b.Encode() if e != nil { return false } e = bucket.Put(b.Hash(), encoded) if e != nil { return false } // store a reference to this block as a next block e = bucket.Put(append(b.PreviousBlockHash, []byte("+")...), b.Hash()) if e != nil { return false } for i := range b.Transactions { // transaction -> block mapping e = bucket.Put(append([]byte("T"), b.Transactions[i].Hash()...), b.Hash()) if e != nil { return false } // link next transactions e = bucket.Put(append([]byte(">"), b.Transactions[i].PreviousEnvelopeHash...), b.Transactions[i].Hash()) if e != nil { return false } // update "unspendable key" e = bucket.Delete(append([]byte("<"), b.Transactions[i].PublicKey...)) if e != nil { return false } // update "spendable key", has to happen after updating the "unspendable" one as it might be the same one e = bucket.Put(append([]byte("<"), b.Transactions[i].NextPublicKey...), b.Transactions[i].Hash()) if e != nil { return false } } if last { if e = bucket.Put([]byte("last"), b.Hash()); e != nil { return false } } return true }) return }
func (db *T) PutBlock(b *block.Block, last bool) error { dbtx, err := db.DB.Begin(true) success := false defer func() { if success { dbtx.Commit() } else { dbtx.Rollback() } }() if err != nil { return err } bucket, err := dbtx.CreateBucketIfNotExists([]byte("blocks")) if err != nil { return err } encoded, err := b.Encode() if err != nil { return err } err = bucket.Put(b.Hash(), encoded) if err != nil { return err } for i := range b.Transactions { err = bucket.Put(b.Transactions[i].Hash(), b.Hash()) if err != nil { return err } } if last { bucket.Put([]byte("last"), b.Hash()) } success = true return nil }