Skip to content

Commit

Permalink
newcopybytes instead
Browse files Browse the repository at this point in the history
  • Loading branch information
igorcrevar committed May 3, 2023
1 parent 1873d4f commit 8b48874
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 26 deletions.
17 changes: 7 additions & 10 deletions consensus/polybft/stake_manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -362,10 +362,10 @@ func Test_MarshalBug(t *testing.T) {
return obj(ar).MarshalTo(nil)
}

emptyArray := [8]byte{}
corruptedSlice := make([]byte, 32)
corruptedSlice[29], corruptedSlice[30], corruptedSlice[31] = 5, 126, 64

intOfCorruption := uint64(8989232334)
intOfCorruption := uint64(18_446_744_073_709_551_615) // 2^64-1

marshalOne := func(ar *fastrlp.Arena) *fastrlp.Value {
return ar.NewBytes(corruptedSlice)
Expand All @@ -375,14 +375,11 @@ func Test_MarshalBug(t *testing.T) {
return ar.NewUint(intOfCorruption)
}

check := func() {
for i := 0; i < 8; i++ {
require.Equal(t, byte(0), corruptedSlice[i])
}
}

marshal(marshalOne)
check()

require.Equal(t, emptyArray[:], corruptedSlice[:len(emptyArray)])

marshal(marshalTwo) // without fixing this, marshaling will cause corruption of the corrupted slice
check()

require.NotEqual(t, emptyArray[:], corruptedSlice[:len(emptyArray)])
}
2 changes: 0 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -228,5 +228,3 @@ require (
gotest.tools/v3 v3.0.2 // indirect
inet.af/netaddr v0.0.0-20220617031823-097006376321 // indirect
)

replace github.com/umbracle/fastrlp => github.com/igorcrevar/fastrlp v0.0.0-20230503111411-4717b7e20c16
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -327,8 +327,6 @@ github.com/huin/goupnp v1.0.3 h1:N8No57ls+MnjlB+JPiCVSOyy/ot7MJTqlo7rn+NYSqQ=
github.com/huin/goupnp v1.0.3/go.mod h1:ZxNlw5WqJj6wSsRK5+YfflQGXYfccj5VgQsMNixHM7Y=
github.com/huin/goutil v0.0.0-20170803182201-1ca381bf3150/go.mod h1:PpLOETDnJ0o3iZrZfqZzyLl6l7F3c6L1oWn7OICBi6o=
github.com/ianlancetaylor/demangle v0.0.0-20200824232613-28f6c0f3b639/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc=
github.com/igorcrevar/fastrlp v0.0.0-20230503111411-4717b7e20c16 h1:V48deMz/8zv8Cc/7kLjkTiaHz6Y1D3ZI7rQw0b0QRVs=
github.com/igorcrevar/fastrlp v0.0.0-20230503111411-4717b7e20c16/go.mod h1:5RHgqiFjd4vLJESMWagP/E7su+5Gzk0iqqmrotR8WdA=
github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
Expand Down Expand Up @@ -680,6 +678,8 @@ github.com/ugorji/go/codec v1.1.7 h1:2SvQaVZ1ouYrrKKwoSk2pzd4A9evlKJb9oTL+OaLUSs
github.com/ugorji/go/codec v1.1.7/go.mod h1:Ax+UKWsSmolVDwsd+7N3ZtXu+yMGCf907BLYF3GoBXY=
github.com/umbracle/ethgo v0.1.4-0.20230326234627-15b1df435098 h1:OXYAHR6AKpV2A/BjQNECXod9rR8r5XOT2QCakD2r0YQ=
github.com/umbracle/ethgo v0.1.4-0.20230326234627-15b1df435098/go.mod h1:bjxSp984qsxCStKoKjqz5fgugi4uWj6+/tFkSEpjk3A=
github.com/umbracle/fastrlp v0.1.0 h1:V0W3f6ZKWqbu1KggdhnRWOi+t7+PfL3VyAffJqayI5s=
github.com/umbracle/fastrlp v0.1.0/go.mod h1:5RHgqiFjd4vLJESMWagP/E7su+5Gzk0iqqmrotR8WdA=
github.com/umbracle/go-eth-bn256 v0.0.0-20230125114011-47cb310d9b0b h1:5/xofhZiOG0I9DQXqDSPxqYObk6QI7mBGMJI+ngyIgc=
github.com/umbracle/go-eth-bn256 v0.0.0-20230125114011-47cb310d9b0b/go.mod h1:H8SeC2PWEciymT92Mt07Qcfjr2FMEuCz/V+KPtPTy+U=
github.com/urfave/cli v1.22.2/go.mod h1:Gos4lmkARVdJ6EkW0WaNv/tZAAMe9V7XWyB60NtXRu0=
Expand Down
24 changes: 12 additions & 12 deletions types/rlp_marshal.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,12 @@ func (h *Header) MarshalRLPTo(dst []byte) []byte {
func (h *Header) MarshalRLPWith(arena *fastrlp.Arena) *fastrlp.Value {
vv := arena.NewArray()

vv.Set(arena.NewBytes(h.ParentHash.Bytes()))
vv.Set(arena.NewBytes(h.Sha3Uncles.Bytes()))
vv.Set(arena.NewCopyBytes(h.ParentHash.Bytes()))
vv.Set(arena.NewCopyBytes(h.Sha3Uncles.Bytes()))
vv.Set(arena.NewCopyBytes(h.Miner[:]))
vv.Set(arena.NewBytes(h.StateRoot.Bytes()))
vv.Set(arena.NewBytes(h.TxRoot.Bytes()))
vv.Set(arena.NewBytes(h.ReceiptsRoot.Bytes()))
vv.Set(arena.NewCopyBytes(h.StateRoot.Bytes()))
vv.Set(arena.NewCopyBytes(h.TxRoot.Bytes()))
vv.Set(arena.NewCopyBytes(h.ReceiptsRoot.Bytes()))
vv.Set(arena.NewCopyBytes(h.LogsBloom[:]))

vv.Set(arena.NewUint(h.Difficulty))
Expand All @@ -90,7 +90,7 @@ func (h *Header) MarshalRLPWith(arena *fastrlp.Arena) *fastrlp.Value {
vv.Set(arena.NewUint(h.Timestamp))

vv.Set(arena.NewCopyBytes(h.ExtraData))
vv.Set(arena.NewBytes(h.MixHash.Bytes()))
vv.Set(arena.NewCopyBytes(h.MixHash.Bytes()))
vv.Set(arena.NewCopyBytes(h.Nonce[:]))

vv.Set(arena.NewUint(h.BaseFee))
Expand Down Expand Up @@ -135,7 +135,7 @@ func (r *Receipt) MarshalRLPWith(a *fastrlp.Arena) *fastrlp.Value {
if r.Status != nil {
vv.Set(a.NewUint(uint64(*r.Status)))
} else {
vv.Set(a.NewBytes(r.Root[:]))
vv.Set(a.NewCopyBytes(r.Root[:]))
}

vv.Set(a.NewUint(r.CumulativeGasUsed))
Expand Down Expand Up @@ -163,15 +163,15 @@ func (r *Receipt) MarshalLogsWith(a *fastrlp.Arena) *fastrlp.Value {

func (l *Log) MarshalRLPWith(a *fastrlp.Arena) *fastrlp.Value {
v := a.NewArray()
v.Set(a.NewBytes(l.Address.Bytes()))
v.Set(a.NewCopyBytes(l.Address.Bytes()))

topics := a.NewArray()
for _, t := range l.Topics {
topics.Set(a.NewBytes(t.Bytes()))
topics.Set(a.NewCopyBytes(t.Bytes()))
}

v.Set(topics)
v.Set(a.NewBytes(l.Data))
v.Set(a.NewCopyBytes(l.Data))

return v
}
Expand Down Expand Up @@ -215,7 +215,7 @@ func (t *Transaction) MarshalRLPWith(arena *fastrlp.Arena) *fastrlp.Value {

// Address may be empty
if t.To != nil {
vv.Set(arena.NewBytes((*t.To).Bytes()))
vv.Set(arena.NewCopyBytes((*t.To).Bytes()))
} else {
vv.Set(arena.NewNull())
}
Expand All @@ -237,7 +237,7 @@ func (t *Transaction) MarshalRLPWith(arena *fastrlp.Arena) *fastrlp.Value {
vv.Set(arena.NewBigInt(t.S))

if t.Type == StateTx {
vv.Set(arena.NewBytes((t.From).Bytes()))
vv.Set(arena.NewCopyBytes(t.From.Bytes()))
}

return vv
Expand Down

0 comments on commit 8b48874

Please sign in to comment.