Encoding/Decoding Deposit Data Fix (#1317)

* fix encoding/decoding

* comments
This commit is contained in:
Nishant Das 2019-01-14 22:42:26 +08:00 committed by GitHub
parent e313e97666
commit c186274891
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 16 additions and 12 deletions

View File

@ -157,9 +157,10 @@ func EncodeDepositData(
timestamp := make([]byte, 8)
binary.BigEndian.PutUint64(timestamp, uint64(depositTimestamp))
depositData = append(depositData, encodedInput...)
depositData = append(depositData, value...)
depositData = append(depositData, timestamp...)
depositData = append(depositData, encodedInput...)
return depositData, nil
}
@ -179,7 +180,9 @@ func DecodeDepositInput(depositData []byte) (*pb.DepositInput, error) {
)
}
depositInput := new(pb.DepositInput)
depositInputBytes := depositData[:len(depositData)-16]
// Since the value deposited and the timestamp are both 8 bytes each,
// the deposit data is the chunk after the first 16 bytes.
depositInputBytes := depositData[16:]
rBuf := bytes.NewReader(depositInputBytes)
if err := ssz.Decode(rBuf, depositInput); err != nil {
return nil, fmt.Errorf("ssz decode failed: %v", err)
@ -199,9 +202,11 @@ func DecodeDepositAmountAndTimeStamp(depositData []byte) (uint64, int64, error)
len(depositData),
)
}
length := len(depositData)
amount := binary.BigEndian.Uint64(depositData[length-16 : length-8])
timestamp := binary.BigEndian.Uint64(depositData[length-8:])
// the amount occupies the first 8 bytes while the
// timestamp occupies the next 8 bytes.
amount := binary.BigEndian.Uint64(depositData[:8])
timestamp := binary.BigEndian.Uint64(depositData[8:16])
return amount, int64(timestamp), nil
}

View File

@ -564,9 +564,9 @@ func ProcessValidatorDeposits(
if err = verifyDeposit(beaconState, deposit); err != nil {
return nil, fmt.Errorf("could not verify deposit #%d: %v", idx, err)
}
// depositData consists of depositInput []byte + depositValue [8]byte +
// depositTimestamp [8]byte.
depositValue := depositData[len(depositData)-16 : len(depositData)-8]
// depositData consists of depositValue [8]byte +
// depositTimestamp [8]byte + depositInput []byte .
depositValue := depositData[:8]
// We then mutate the beacon state with the verified validator deposit.
beaconState, err = v.ProcessDeposit(
beaconState,

View File

@ -9,11 +9,10 @@ import (
"testing"
"time"
"github.com/prysmaticlabs/prysm/shared/trie"
pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1"
"github.com/prysmaticlabs/prysm/shared/params"
"github.com/prysmaticlabs/prysm/shared/ssz"
"github.com/prysmaticlabs/prysm/shared/trie"
)
func TestProcessPOWReceiptRoots_SameRootHash(t *testing.T) {
@ -1280,9 +1279,9 @@ func TestProcessValidatorDeposits_ProcessDepositHelperFuncFails(t *testing.T) {
// We then create a serialized deposit data slice of type []byte
// by appending all 3 items above together.
data = append(data, encodedInput...)
data = append(data, value...)
data = append(data, timestamp...)
data = append(data, encodedInput...)
// We then create a merkle branch for the test.
depositTrie := trie.NewDepositTrie()
@ -1359,9 +1358,9 @@ func TestProcessValidatorDeposits_ProcessCorrectly(t *testing.T) {
// We then create a serialized deposit data slice of type []byte
// by appending all 3 items above together.
data = append(data, encodedInput...)
data = append(data, value...)
data = append(data, timestamp...)
data = append(data, encodedInput...)
// We then create a merkle branch for the test.
depositTrie := trie.NewDepositTrie()