Skip to content

Commit

Permalink
fix: dirtyTx modify fail
Browse files Browse the repository at this point in the history
  • Loading branch information
viteshan committed Jul 18, 2022
1 parent c97b1f2 commit 9d13ae0
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 8 deletions.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ test:
GO111MODULE=on go test ./tools/toposort
GO111MODULE=on go test ./vm
GO111MODULE=on go test ./wallet
GO111MODULE=on go test ./ledger/onroad/pool

build_linux_amd64:
env GOOS=linux CGO_ENABLED=0 GO111MODULE=on GOARCH=amd64 go build -o $(BUILD_DIR)/gvite-$(VITE_VERSION)-linux/gvite $(MAIN)
Expand Down
2 changes: 1 addition & 1 deletion ledger/onroad/pool/contract_pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -382,7 +382,7 @@ func (cc *callerCache) lazyUpdateFrontTx(reader chainReader, hv *orHeightValue)
if sub.FromHash == sendBlock.Hash {
j := uint32(i)
sub.FromIndex = &j
cc.storage.updateSubIndex(sub)
cc.storage.updateSubIndex(*sub)
}
}
}
Expand Down
16 changes: 10 additions & 6 deletions ledger/onroad/pool/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,21 @@ import (
ledger "github.com/vitelabs/go-vite/v2/interfaces/core"
)

type orHeightValue []OnroadTx
type orHeightValue []*OnroadTx

func newOrHeightValueFromOnroadTxs(txs []OnroadTx) (orHeightValue, error) {
return txs, nil
result := make([]*OnroadTx, len(txs))
for index := range txs {
result[index] = &txs[index]
}
return result, nil
}

func (hv orHeightValue) isEmpty() bool {
return len(hv) == 0
}
func (hv orHeightValue) dirtyTxs() []OnroadTx {
var result []OnroadTx
func (hv orHeightValue) dirtyTxs() []*OnroadTx {
var result []*OnroadTx
for _, sub := range hv {
if sub.FromIndex == nil {
result = append(result, sub)
Expand All @@ -36,10 +40,10 @@ func (hv orHeightValue) minTx() (*OnroadTx, error) {
}
tmp := sub
if min == nil {
min = &tmp
min = tmp
} else {
if *sub.FromIndex < *min.FromIndex {
min = &tmp
min = tmp
}
}
}
Expand Down
70 changes: 69 additions & 1 deletion ledger/onroad/pool/types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"testing"

"github.com/stretchr/testify/assert"
"github.com/vitelabs/go-vite/v2/common/types"
)

func Test_orHeightValue(t *testing.T) {
Expand Down Expand Up @@ -34,9 +35,76 @@ func Test_orHeightValue(t *testing.T) {
},
}

val := orHeightValue(txs)
val, err := newOrHeightValueFromOnroadTxs(txs)
assert.NoError(t, err)

tx, err := val.minTx()
assert.NoError(t, err)
assert.Equal(t, uint32(0), *tx.FromIndex)
}

func Test_orHeightValue_dirtyTxs(t *testing.T) {
i0 := uint32(0)
i1 := uint32(1)
i2 := uint32(2)
txs := []OnroadTx{
{
FromAddr: [21]byte{},
ToAddr: [21]byte{},
FromHeight: 2,
FromHash: [32]byte{2},
FromIndex: &i2,
},
{
FromAddr: [21]byte{},
ToAddr: [21]byte{},
FromHeight: 0,
FromHash: [32]byte{0},
FromIndex: &i0,
},
{
FromAddr: [21]byte{},
ToAddr: [21]byte{},
FromHeight: 0,
FromHash: types.DataHash([]byte{3}),
FromIndex: nil,
},
{
FromAddr: [21]byte{},
ToAddr: [21]byte{},
FromHeight: 0,
FromHash: [32]byte{1},
FromIndex: &i1,
},
{
FromAddr: [21]byte{},
ToAddr: [21]byte{},
FromHeight: 0,
FromHash: types.DataHash([]byte{4}),
FromIndex: nil,
},
}

val, err := newOrHeightValueFromOnroadTxs(txs)
assert.NoError(t, err)

dirtyTxs := val.dirtyTxs()
// for _, tx := range dirtyTxs {
// t.Log(tx.String())
// }
assert.Equal(t, 2, len(dirtyTxs))
assert.Equal(t, types.DataHash([]byte{3}), dirtyTxs[0].FromHash)
assert.Equal(t, types.DataHash([]byte{4}), dirtyTxs[1].FromHash)

for i, sub := range dirtyTxs {
j := uint32(i)
sub.FromIndex = &j
}
{
dirtyTxs := val.dirtyTxs()
// for _, tx := range dirtyTxs {
// t.Log(tx.String())
// }
assert.Equal(t, 0, len(dirtyTxs))
}
}

0 comments on commit 9d13ae0

Please sign in to comment.