mirror of
https://gitlab.com/pulsechaincom/erigon-pulse.git
synced 2024-12-25 13:07:17 +00:00
add lenth package (#104)
This commit is contained in:
parent
240f7f1212
commit
ac51da3a55
13
common/length/length.go
Normal file
13
common/length/length.go
Normal file
@ -0,0 +1,13 @@
|
||||
package length
|
||||
|
||||
// Lengths of hashes and addresses in bytes.
|
||||
const (
|
||||
// Hash is the expected length of the hash (in bytes)
|
||||
Hash = 32
|
||||
// Addr is the expected length of the address (in bytes)
|
||||
Addr = 20
|
||||
// BlockNumberLen length of uint64 big endian
|
||||
BlockNum = 8
|
||||
// Incarnation length of uint64 for contract incarnations
|
||||
Incarnation = 8
|
||||
)
|
@ -21,10 +21,11 @@ import (
|
||||
"fmt"
|
||||
|
||||
"github.com/ledgerwatch/erigon-lib/common"
|
||||
"github.com/ledgerwatch/erigon-lib/common/length"
|
||||
"github.com/ledgerwatch/erigon-lib/rlp"
|
||||
)
|
||||
|
||||
type NewPooledTransactionHashesPacket [][32]byte
|
||||
type NewPooledTransactionHashesPacket [][length.Hash]byte
|
||||
|
||||
// ParseHashesCount looks at the RLP length Prefix for list of 32-byte hashes
|
||||
// and returns number of hashes in the list to expect
|
||||
@ -44,7 +45,7 @@ func ParseHashesCount(payload []byte, pos int) (count int, dataPos int, err erro
|
||||
// there is there is enough capacity.
|
||||
// The first returned value is the slice where encodinfg
|
||||
func EncodeHashes(hashes []byte, encodeBuf []byte) []byte {
|
||||
hashesLen := len(hashes) / 32 * 33
|
||||
hashesLen := len(hashes) / length.Hash * 33
|
||||
dataLen := hashesLen
|
||||
encodeBuf = common.EnsureEnoughSize(encodeBuf, rlp.ListPrefixLen(hashesLen)+dataLen)
|
||||
rlp.EncodeHashes(hashes, encodeBuf)
|
||||
@ -57,7 +58,7 @@ func EncodeHashes(hashes []byte, encodeBuf []byte) []byte {
|
||||
// The second returned value is the new position in the RLP payload after the extraction
|
||||
// of the hash.
|
||||
func ParseHash(payload []byte, pos int, hashbuf []byte) ([]byte, int, error) {
|
||||
hashbuf = common.EnsureEnoughSize(hashbuf, 32)
|
||||
hashbuf = common.EnsureEnoughSize(hashbuf, length.Hash)
|
||||
pos, err := rlp.ParseHash(payload, pos, hashbuf)
|
||||
if err != nil {
|
||||
return nil, 0, fmt.Errorf("%s: hash len: %w", rlp.ParseHashErrorPrefix, err)
|
||||
@ -68,7 +69,7 @@ func ParseHash(payload []byte, pos int, hashbuf []byte) ([]byte, int, error) {
|
||||
// EncodeGetPooledTransactions66 produces encoding of GetPooledTransactions66 packet
|
||||
func EncodeGetPooledTransactions66(hashes []byte, requestId uint64, encodeBuf []byte) ([]byte, error) {
|
||||
pos := 0
|
||||
hashesLen := len(hashes) / 32 * 33
|
||||
hashesLen := len(hashes) / length.Hash * 33
|
||||
dataLen := rlp.ListPrefixLen(hashesLen) + hashesLen + rlp.U64Len(requestId)
|
||||
encodeBuf = common.EnsureEnoughSize(encodeBuf, rlp.ListPrefixLen(dataLen)+dataLen)
|
||||
// Length Prefix for the entire structure
|
||||
@ -94,10 +95,10 @@ func ParseGetPooledTransactions66(payload []byte, pos int, hashbuf []byte) (requ
|
||||
if err != nil {
|
||||
return 0, hashes, 0, err
|
||||
}
|
||||
hashes = common.EnsureEnoughSize(hashbuf, 32*hashesCount)
|
||||
hashes = common.EnsureEnoughSize(hashbuf, length.Hash*hashesCount)
|
||||
|
||||
for i := 0; pos < len(payload); i++ {
|
||||
pos, err = rlp.ParseHash(payload, pos, hashes[i*32:])
|
||||
pos, err = rlp.ParseHash(payload, pos, hashes[i*length.Hash:])
|
||||
if err != nil {
|
||||
return 0, hashes, 0, err
|
||||
}
|
||||
@ -111,10 +112,10 @@ func ParseGetPooledTransactions65(payload []byte, pos int, hashbuf []byte) (hash
|
||||
if err != nil {
|
||||
return hashes, 0, err
|
||||
}
|
||||
hashes = common.EnsureEnoughSize(hashbuf, 32*hashesCount)
|
||||
hashes = common.EnsureEnoughSize(hashbuf, length.Hash*hashesCount)
|
||||
|
||||
for i := 0; pos < len(payload); i++ {
|
||||
pos, err = rlp.ParseHash(payload, pos, hashes[i*32:])
|
||||
pos, err = rlp.ParseHash(payload, pos, hashes[i*length.Hash:])
|
||||
if err != nil {
|
||||
return hashes, 0, err
|
||||
}
|
||||
|
@ -26,6 +26,7 @@ import (
|
||||
|
||||
"github.com/holiman/uint256"
|
||||
"github.com/ledgerwatch/erigon-lib/chain"
|
||||
"github.com/ledgerwatch/erigon-lib/common/length"
|
||||
"github.com/ledgerwatch/erigon-lib/common/u256"
|
||||
"github.com/ledgerwatch/erigon-lib/gointerfaces/types"
|
||||
"github.com/ledgerwatch/erigon-lib/rlp"
|
||||
@ -457,13 +458,13 @@ type PeerID *types.H512
|
||||
|
||||
type Hashes []byte // flatten list of 32-byte hashes
|
||||
|
||||
func (h Hashes) At(i int) []byte { return h[i*32 : (i+1)*32] }
|
||||
func (h Hashes) Len() int { return len(h) / 32 }
|
||||
func (h Hashes) At(i int) []byte { return h[i*length.Hash : (i+1)*length.Hash] }
|
||||
func (h Hashes) Len() int { return len(h) / length.Hash }
|
||||
|
||||
type Addresses []byte // flatten list of 20-byte addresses
|
||||
|
||||
func (h Addresses) At(i int) []byte { return h[i*20 : (i+1)*20] }
|
||||
func (h Addresses) Len() int { return len(h) / 20 }
|
||||
func (h Addresses) At(i int) []byte { return h[i*length.Addr : (i+1)*length.Addr] }
|
||||
func (h Addresses) Len() int { return len(h) / length.Addr }
|
||||
|
||||
type TxSlots struct {
|
||||
txs []*TxSlot
|
||||
@ -494,7 +495,7 @@ func (s *TxSlots) Resize(targetSize uint) {
|
||||
}
|
||||
//todo: set nil to overflow txs
|
||||
s.txs = s.txs[:targetSize]
|
||||
s.senders = s.senders[:20*targetSize]
|
||||
s.senders = s.senders[:length.Addr*targetSize]
|
||||
s.isLocal = s.isLocal[:targetSize]
|
||||
}
|
||||
func (s *TxSlots) Append(slot *TxSlot, sender []byte, isLocal bool) {
|
||||
@ -524,11 +525,11 @@ func (s *TxsRlp) Resize(targetSize uint) {
|
||||
}
|
||||
//todo: set nil to overflow txs
|
||||
s.Txs = s.Txs[:targetSize]
|
||||
s.Senders = s.Senders[:20*targetSize]
|
||||
s.Senders = s.Senders[:length.Addr*targetSize]
|
||||
s.IsLocal = s.IsLocal[:targetSize]
|
||||
}
|
||||
|
||||
var addressesGrowth = make([]byte, 20)
|
||||
var addressesGrowth = make([]byte, length.Addr)
|
||||
|
||||
func EncodeSenderLengthForStorage(nonce uint64, balance uint256.Int) uint {
|
||||
var structLength uint = 1 // 1 byte for fieldset
|
||||
|
Loading…
Reference in New Issue
Block a user