Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 22 additions & 9 deletions blockproducer/blocknode.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
package blockproducer

import (
"sync/atomic"

"github.com/CovenantSQL/CovenantSQL/crypto/hash"
"github.com/CovenantSQL/CovenantSQL/types"
)
Expand All @@ -27,12 +29,13 @@ type blockNode struct {
count uint32
height uint32
// Cached fields for quick reference
hash hash.Hash
block *types.BPBlock
hash hash.Hash
txCount int
block atomic.Value
}

func newBlockNode(h uint32, b *types.BPBlock, p *blockNode) *blockNode {
return &blockNode{
func newBlockNode(h uint32, b *types.BPBlock, p *blockNode) (node *blockNode) {
node = &blockNode{
parent: p,

count: func() uint32 {
Expand All @@ -43,17 +46,27 @@ func newBlockNode(h uint32, b *types.BPBlock, p *blockNode) *blockNode {
}(),
height: h,

hash: b.SignedHeader.DataHash,
block: b,
hash: b.SignedHeader.DataHash,
txCount: len(b.Transactions),
}
node.block.Store(b)
return
}

func (n *blockNode) load() *types.BPBlock {
return n.block.Load().(*types.BPBlock)
}

func (n *blockNode) clear() {
n.block.Store((*types.BPBlock)(nil))
}

// fetchNodeList returns the block node list within range (from, n.count] from node head n.
// fetchNodeList returns the block node list within range [from, n.count] from node head n.
func (n *blockNode) fetchNodeList(from uint32) (bl []*blockNode) {
if n.count <= from {
if n.count < from {
return
}
bl = make([]*blockNode, n.count-from)
bl = make([]*blockNode, n.count-from+1)
var iter = n
for i := len(bl) - 1; i >= 0; i-- {
bl[i] = iter
Expand Down
6 changes: 3 additions & 3 deletions blockproducer/blocknode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,11 +115,11 @@ func TestBlockNode(t *testing.T) {
So(n0.count, ShouldEqual, 0)
So(n1.count, ShouldEqual, n0.count+1)

So(n0.fetchNodeList(0), ShouldBeEmpty)
So(n0.fetchNodeList(1), ShouldBeEmpty)
So(n0.fetchNodeList(2), ShouldBeEmpty)
So(n3.fetchNodeList(0), ShouldResemble, []*blockNode{n1, n2, n3})
So(n4p.fetchNodeList(2), ShouldResemble, []*blockNode{n3p, n4p})
So(n0.fetchNodeList(3), ShouldBeEmpty)
So(n3.fetchNodeList(1), ShouldResemble, []*blockNode{n1, n2, n3})
So(n4p.fetchNodeList(3), ShouldResemble, []*blockNode{n3p, n4p})

So(n0.ancestor(1), ShouldBeNil)
So(n3.ancestor(3), ShouldEqual, n3)
Expand Down
21 changes: 12 additions & 9 deletions blockproducer/branch.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and * limitations under the License.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package blockproducer
Expand Down Expand Up @@ -43,7 +44,7 @@ func newBranch(
br *branch, err error,
) {
var (
list = headNode.fetchNodeList(baseNode.count)
list = headNode.fetchNodeList(baseNode.count + 1)
inst = &branch{
head: headNode,
preview: baseState.makeCopy(),
Expand All @@ -57,11 +58,12 @@ func newBranch(
}
// Apply new blocks to view and pool
for _, bn := range list {
if len(bn.block.Transactions) > conf.MaxTransactionsPerBlock {
if bn.txCount > conf.MaxTransactionsPerBlock {
return nil, ErrTooManyTransactionsInBlock
}

for _, v := range bn.block.Transactions {
var block = bn.load()
for _, v := range block.Transactions {
var k = v.Hash()
// Check in tx pool
if _, ok := inst.unpacked[k]; ok {
Expand Down Expand Up @@ -126,17 +128,18 @@ func (b *branch) addTx(tx pi.Transaction) {
}

func (b *branch) applyBlock(n *blockNode) (br *branch, err error) {
if !b.head.hash.IsEqual(n.block.ParentHash()) {
var block = n.load()
if !b.head.hash.IsEqual(block.ParentHash()) {
err = ErrParentNotMatch
return
}
var cpy = b.makeArena()

if len(n.block.Transactions) > conf.MaxTransactionsPerBlock {
if n.txCount > conf.MaxTransactionsPerBlock {
return nil, ErrTooManyTransactionsInBlock
}

for _, v := range n.block.Transactions {
for _, v := range block.Transactions {
var k = v.Hash()
// Check in tx pool
if _, ok := cpy.unpacked[k]; ok {
Expand Down Expand Up @@ -258,13 +261,13 @@ func (b *branch) sprint(from uint32) (buff string) {
if i == 0 {
var p = v.parent
buff += fmt.Sprintf("* #%d:%d %s {%d}",
p.height, p.count, p.hash.Short(4), len(p.block.Transactions))
p.height, p.count, p.hash.Short(4), p.txCount)
}
if d := v.height - v.parent.height; d > 1 {
buff += fmt.Sprintf(" <-- (skip %d blocks)", d-1)
}
buff += fmt.Sprintf(" <-- #%d:%d %s {%d}",
v.height, v.count, v.hash.Short(4), len(v.block.Transactions))
v.height, v.count, v.hash.Short(4), v.txCount)
}
return
}
Loading