persistence

This commit is contained in:
alex.sharov 2021-08-23 16:13:06 +07:00
parent 224d851465
commit 8133abfd56
4 changed files with 37 additions and 9 deletions

View File

@ -334,8 +334,7 @@ func (sc *SendersCache) fromDB(tx kv.Tx) error {
return err
}
id := binary.BigEndian.Uint64(v)
sc.senderID = id
sc.senderIDs[string(v)] = id
sc.senderIDs[string(k)] = id
}
c, err = tx.Cursor(kv.PooledSender)
@ -358,11 +357,16 @@ func (sc *SendersCache) fromDB(tx kv.Tx) error {
return err
}
sc.blockHeight.Store(binary.BigEndian.Uint64(height))
v, err := tx.GetOne(kv.PoolInfo, []byte("sender_cache_id"))
if err != nil {
return err
}
sc.senderID = binary.BigEndian.Uint64(v)
return nil
}
var SenderCacheHeightKey = []byte("s")
var SenderCacheHeightKey = []byte("sender_cache_heght")
func (sc *SendersCache) flush(tx kv.RwTx) error {
sc.lock.RLock()
@ -392,6 +396,11 @@ func (sc *SendersCache) flush(tx kv.RwTx) error {
if err != nil {
return err
}
binary.BigEndian.PutUint64(encID, sc.senderID)
err = tx.Put(kv.PoolInfo, []byte("sender_cache_id"), encID)
if err != nil {
return err
}
//TODO: flush and evict
@ -771,7 +780,8 @@ func (p *TxPool) fromDB(tx kv.Tx, senders *SendersCache) error {
return err
}
txs.txs[i] = &TxSlot{}
_, err := parseCtx.ParseTransaction(v, 8+8, txs.txs[i], nil)
_, err := parseCtx.ParseTransaction(v[8+8:], 0, txs.txs[i], nil)
if err != nil {
return err
}

View File

@ -534,10 +534,10 @@ func FuzzOnNewBlocks11(f *testing.F) {
// }
//}
assert.Equal(sendersCache.senderID, s2.senderID)
//assert.Equal(sendersCache.blockHeight.Load(), s2.blockHeight.Load())
//require.Equal(t, len(sendersCache.senderIDs), len(s2.senderIDs))
//require.Equal(t, len(sendersCache.senderInfo), len(s2.senderInfo))
//require.Equal(t, len(pool.byHash), len(p2.byHash))
assert.Equal(sendersCache.blockHeight.Load(), s2.blockHeight.Load())
require.Equal(t, len(sendersCache.senderIDs), len(s2.senderIDs))
require.Equal(t, len(sendersCache.senderInfo), len(s2.senderInfo))
require.Equal(t, len(pool.byHash), len(p2.byHash))
//assert.Equal(pool.pending.Len(), p2.pending.Len())
//assert.Equal(pool.baseFee.Len(), p2.baseFee.Len())
//assert.Equal(pool.queued.Len(), p2.queued.Len())

View File

@ -129,7 +129,6 @@ func (ctx *TxParseContext) ParseTransaction(payload []byte, pos int, slot *TxSlo
if dataLen > txMaxSize {
return 0, fmt.Errorf("%s: too large tx.size=%dKb", ParseTransactionErrorPrefix, len(payload)/1024)
}
slot.rlp = payload[pos : dataPos+dataLen]
//if dataPos+dataLen != len(payload) {
// return 0, fmt.Errorf("%s: transaction must be either 1 list or 1 string", ParseTransactionErrorPrefix)
@ -160,6 +159,8 @@ func (ctx *TxParseContext) ParseTransaction(payload []byte, pos int, slot *TxSlo
}
p = dataPos
}
slot.rlp = payload[pos : dataPos+dataLen]
// Remember where signing hash data begins (it will need to be wrapped in an RLP list)
sigHashPos := p
// If it is non-legacy tx, chainId follows, but we skip it

View File

@ -57,6 +57,23 @@ var txParseTests = []struct {
signHashStr: "35fbc0cd33a181e62b7432338f172106886a1396e1e3647ddf1e756740d81ae1", nonce: 3},
}
func TestName(t *testing.T) {
a1 := decodeHex("02f864010301018261a894b94f5374fce5edbc8e2a8697c15331677e6ebf0b0a825544c001a0c9519f4f2b30335884581971573fadf60c6204f59a911df35ee8a540456b2660a032f1e8e2c5dd761f9e4f88f41c8310aeaba26a8bfcdacfedfa12ec3862d37521")
a2 := decodeHex("02e001031419808002940000000000000000000000000000000000000005c0010101")
ctx := NewTxParseContext()
{
tx, txSender := &TxSlot{}, [20]byte{}
_, err := ctx.ParseTransaction(a1, 0, tx, txSender[:])
require.NoError(t, err)
fmt.Printf("%d,%d\n", tx.nonce, tx.tip)
}
{
tx, txSender := &TxSlot{}, [20]byte{}
_, err := ctx.ParseTransaction(a2, 0, tx, txSender[:])
require.NoError(t, err)
fmt.Printf("%d,%d\n", tx.nonce, tx.tip)
}
}
func TestParseTransactionRLP(t *testing.T) {
ctx := NewTxParseContext()
for i, tt := range txParseTests {