Skip to content

Commit

Permalink
Use big.Int for bytes sum and int to byte conversion
Browse files Browse the repository at this point in the history
  • Loading branch information
anodar committed Oct 10, 2023
1 parent e79daea commit 6b2174a
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 28 deletions.
17 changes: 1 addition & 16 deletions arbnode/batch_poster.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ package arbnode
import (
"bytes"
"context"
"encoding/binary"
"encoding/hex"
"errors"
"fmt"
Expand Down Expand Up @@ -343,13 +342,7 @@ func AccessList(opts *AccessListOpts) types.AccessList {
{7, opts.SequencerInboxAccs}, // - sequencerInboxAccs.push(...); (keccak256(7, sequencerInboxAccs.length))
{6, opts.AfterDelayedMessagesRead - 1}, // - delayedInboxAccs[afterDelayedMessagesRead - 1]; (keccak256(6, afterDelayedMessagesRead - 1))
} {

sia, err := intToBytes(v.val)
if err != nil {
log.Error("Error converting sequencer inbox accs to bytes", "err", err, "sequencerInboxAccs", opts.SequencerInboxAccs)
continue
}
sb := arbutil.SumBytes(arbutil.PaddedKeccak256([]byte{byte(v.slotIdx)}), sia)
sb := arbutil.SumBytes(arbutil.PaddedKeccak256([]byte{byte(v.slotIdx)}), big.NewInt(int64(v.val)).Bytes())
l[1].StorageKeys = append(l[1].StorageKeys, common.Hash(sb))
}

Expand All @@ -369,14 +362,6 @@ func AccessList(opts *AccessListOpts) types.AccessList {
return l
}

func intToBytes(val int) ([]byte, error) {
buf := new(bytes.Buffer)
if err := binary.Write(buf, binary.BigEndian, int64(val)); err != nil {
return nil, err
}
return buf.Bytes(), nil
}

// checkRevert checks blocks with number in range [from, to] whether they
// contain reverted batch_poster transaction.
// It returns true if it finds batch posting needs to halt, which is true if a batch reverts
Expand Down
17 changes: 5 additions & 12 deletions arbutil/hash.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package arbutil

import (
"math/big"

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/crypto"
)
Expand All @@ -18,16 +20,7 @@ func PaddedKeccak256(args ...[]byte) []byte {
// SumBytes sums two byte slices and returns the result.
// If the sum of bytes are over 32 bytes, it return last 32.
func SumBytes(a, b []byte) []byte {
// Normalize lengths to hash length.
a = common.BytesToHash(a).Bytes()
b = common.BytesToHash(b).Bytes()

sum := make([]byte, common.HashLength)
c := 0
for i := common.HashLength - 1; i >= 0; i-- {
tmp := int(a[i]) + int(b[i]) + c
sum[i] = byte(tmp & 0xFF)
c = tmp >> 8
}
return sum
A := big.NewInt(0).SetBytes(a)
B := big.NewInt(0).SetBytes(b)
return common.BytesToHash((A.Add(A, B)).Bytes()).Bytes()
}

0 comments on commit 6b2174a

Please sign in to comment.