Skip to content

Commit cc7ca59

Browse files
authored
Merge pull request #264 from CovenantSQL/feature/promote_beta
Promote beta branch
2 parents c87b6ca + 572b893 commit cc7ca59

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+612
-1018
lines changed

api/service_test.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,14 @@ import (
88
"testing"
99
"time"
1010

11-
"github.com/pkg/errors"
12-
13-
"github.com/CovenantSQL/CovenantSQL/api"
14-
"github.com/CovenantSQL/CovenantSQL/api/models"
15-
1611
"github.com/gorilla/websocket"
12+
"github.com/pkg/errors"
1713
. "github.com/smartystreets/goconvey/convey"
1814
"github.com/sourcegraph/jsonrpc2"
1915
wsstream "github.com/sourcegraph/jsonrpc2/websocket"
16+
17+
"github.com/CovenantSQL/CovenantSQL/api"
18+
"github.com/CovenantSQL/CovenantSQL/api/models"
2019
)
2120

2221
const (

blockproducer/blocknode.go

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
package blockproducer
1818

1919
import (
20+
"sync/atomic"
21+
2022
"github.com/CovenantSQL/CovenantSQL/crypto/hash"
2123
"github.com/CovenantSQL/CovenantSQL/types"
2224
)
@@ -27,12 +29,13 @@ type blockNode struct {
2729
count uint32
2830
height uint32
2931
// Cached fields for quick reference
30-
hash hash.Hash
31-
block *types.BPBlock
32+
hash hash.Hash
33+
txCount int
34+
block atomic.Value
3235
}
3336

34-
func newBlockNode(h uint32, b *types.BPBlock, p *blockNode) *blockNode {
35-
return &blockNode{
37+
func newBlockNode(h uint32, b *types.BPBlock, p *blockNode) (node *blockNode) {
38+
node = &blockNode{
3639
parent: p,
3740

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

46-
hash: b.SignedHeader.DataHash,
47-
block: b,
49+
hash: b.SignedHeader.DataHash,
50+
txCount: len(b.Transactions),
4851
}
52+
node.block.Store(b)
53+
return
54+
}
55+
56+
func (n *blockNode) load() *types.BPBlock {
57+
return n.block.Load().(*types.BPBlock)
58+
}
59+
60+
func (n *blockNode) clear() {
61+
n.block.Store((*types.BPBlock)(nil))
4962
}
5063

51-
// fetchNodeList returns the block node list within range (from, n.count] from node head n.
64+
// fetchNodeList returns the block node list within range [from, n.count] from node head n.
5265
func (n *blockNode) fetchNodeList(from uint32) (bl []*blockNode) {
53-
if n.count <= from {
66+
if n.count < from {
5467
return
5568
}
56-
bl = make([]*blockNode, n.count-from)
69+
bl = make([]*blockNode, n.count-from+1)
5770
var iter = n
5871
for i := len(bl) - 1; i >= 0; i-- {
5972
bl[i] = iter

blockproducer/blocknode_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -116,11 +116,11 @@ func TestBlockNode(t *testing.T) {
116116
So(n0.count, ShouldEqual, 0)
117117
So(n1.count, ShouldEqual, n0.count+1)
118118

119-
So(n0.fetchNodeList(0), ShouldBeEmpty)
120119
So(n0.fetchNodeList(1), ShouldBeEmpty)
121120
So(n0.fetchNodeList(2), ShouldBeEmpty)
122-
So(n3.fetchNodeList(0), ShouldResemble, []*blockNode{n1, n2, n3})
123-
So(n4p.fetchNodeList(2), ShouldResemble, []*blockNode{n3p, n4p})
121+
So(n0.fetchNodeList(3), ShouldBeEmpty)
122+
So(n3.fetchNodeList(1), ShouldResemble, []*blockNode{n1, n2, n3})
123+
So(n4p.fetchNodeList(3), ShouldResemble, []*blockNode{n3p, n4p})
124124

125125
So(n0.ancestor(1), ShouldBeNil)
126126
So(n3.ancestor(3), ShouldEqual, n3)

blockproducer/branch.go

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@
1010
* Unless required by applicable law or agreed to in writing, software
1111
* distributed under the License is distributed on an "AS IS" BASIS,
1212
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13-
* See the License for the specific language governing permissions and * limitations under the License.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
1415
*/
1516

1617
package blockproducer
@@ -44,7 +45,7 @@ func newBranch(
4445
br *branch, err error,
4546
) {
4647
var (
47-
list = headNode.fetchNodeList(baseNode.count)
48+
list = headNode.fetchNodeList(baseNode.count + 1)
4849
inst = &branch{
4950
head: headNode,
5051
preview: baseState.makeCopy(),
@@ -58,11 +59,12 @@ func newBranch(
5859
}
5960
// Apply new blocks to view and pool
6061
for _, bn := range list {
61-
if len(bn.block.Transactions) > conf.MaxTransactionsPerBlock {
62+
if bn.txCount > conf.MaxTransactionsPerBlock {
6263
return nil, ErrTooManyTransactionsInBlock
6364
}
6465

65-
for _, v := range bn.block.Transactions {
66+
var block = bn.load()
67+
for _, v := range block.Transactions {
6668
var k = v.Hash()
6769
// Check in tx pool
6870
if _, ok := inst.unpacked[k]; ok {
@@ -127,17 +129,18 @@ func (b *branch) addTx(tx pi.Transaction) {
127129
}
128130

129131
func (b *branch) applyBlock(n *blockNode) (br *branch, err error) {
130-
if !b.head.hash.IsEqual(n.block.ParentHash()) {
132+
var block = n.load()
133+
if !b.head.hash.IsEqual(block.ParentHash()) {
131134
err = ErrParentNotMatch
132135
return
133136
}
134137
var cpy = b.makeArena()
135138

136-
if len(n.block.Transactions) > conf.MaxTransactionsPerBlock {
139+
if n.txCount > conf.MaxTransactionsPerBlock {
137140
return nil, ErrTooManyTransactionsInBlock
138141
}
139142

140-
for _, v := range n.block.Transactions {
143+
for _, v := range block.Transactions {
141144
var k = v.Hash()
142145
// Check in tx pool
143146
if _, ok := cpy.unpacked[k]; ok {
@@ -259,13 +262,13 @@ func (b *branch) sprint(from uint32) (buff string) {
259262
if i == 0 {
260263
var p = v.parent
261264
buff += fmt.Sprintf("* #%d:%d %s {%d}",
262-
p.height, p.count, p.hash.Short(4), len(p.block.Transactions))
265+
p.height, p.count, p.hash.Short(4), p.txCount)
263266
}
264267
if d := v.height - v.parent.height; d > 1 {
265268
buff += fmt.Sprintf(" <-- (skip %d blocks)", d-1)
266269
}
267270
buff += fmt.Sprintf(" <-- #%d:%d %s {%d}",
268-
v.height, v.count, v.hash.Short(4), len(v.block.Transactions))
271+
v.height, v.count, v.hash.Short(4), v.txCount)
269272
}
270273
return
271274
}

0 commit comments

Comments
 (0)