From 9d13ae0bc1095346e6d6dc8b0220dee1d99cb434 Mon Sep 17 00:00:00 2001 From: viteshan Date: Mon, 18 Jul 2022 17:35:46 +0800 Subject: [PATCH] fix: dirtyTx modify fail --- Makefile | 1 + ledger/onroad/pool/contract_pool.go | 2 +- ledger/onroad/pool/types.go | 16 ++++--- ledger/onroad/pool/types_test.go | 70 ++++++++++++++++++++++++++++- 4 files changed, 81 insertions(+), 8 deletions(-) diff --git a/Makefile b/Makefile index d51c798dc..53e7f99ce 100644 --- a/Makefile +++ b/Makefile @@ -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) diff --git a/ledger/onroad/pool/contract_pool.go b/ledger/onroad/pool/contract_pool.go index b2cf85fac..f6e872c3d 100644 --- a/ledger/onroad/pool/contract_pool.go +++ b/ledger/onroad/pool/contract_pool.go @@ -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) } } } diff --git a/ledger/onroad/pool/types.go b/ledger/onroad/pool/types.go index f1b76282a..e0447b12e 100644 --- a/ledger/onroad/pool/types.go +++ b/ledger/onroad/pool/types.go @@ -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) @@ -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 } } } diff --git a/ledger/onroad/pool/types_test.go b/ledger/onroad/pool/types_test.go index 8e34babbe..514a4e91a 100644 --- a/ledger/onroad/pool/types_test.go +++ b/ledger/onroad/pool/types_test.go @@ -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) { @@ -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)) + } +}